blob: f0e5c5185c4a8534d0f3710b989bdc53d9b934a9 [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);
206static void undisplay_dollar(void);
207static void insert_special(int, int, int);
208static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
209static void check_auto_format(int);
210static void redo_literal(int c);
211static void start_arrow(pos_T *end_insert_pos);
212static void start_arrow_with_change(pos_T *end_insert_pos, int change);
213static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000214#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void check_spell_redraw(void);
216static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000217static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000218#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
220static int echeck_abbr(int);
221static int replace_pop(void);
222static void replace_join(int off);
223static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void replace_flush(void);
228static void replace_do_bs(int limit_col);
229static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100233static void ins_reg(void);
234static void ins_ctrl_g(void);
235static void ins_ctrl_hat(void);
236static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static int ins_start_select(int c);
241static void ins_insert(int replaceState);
242static void ins_ctrl_o(void);
243static void ins_shift(int c, int lastc);
244static void ins_del(void);
245static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ins_mouse(int c);
248static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000250#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000252#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void ins_left(int end_change);
254static void ins_home(int c);
255static void ins_end(int c);
256static void ins_s_left(void);
257static void ins_right(int end_change);
258static void ins_s_right(void);
259static void ins_up(int startcol);
260static void ins_pageup(void);
261static void ins_down(int startcol);
262static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100266static int ins_tab(void);
267static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100273static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100275static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100276#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100277static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280static colnr_T Insstart_textlen; /* length of line when insert started */
281static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100282static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284static char_u *last_insert = NULL; /* the text of the previous insert,
285 K_SPECIAL and CSI are escaped */
286static int last_insert_skip; /* nr of chars in front of previous insert */
287static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000288static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289
290#ifdef FEAT_CINDENT
291static int can_cindent; /* may do cindenting on this line */
292#endif
293
294static int old_indent = 0; /* for ^^D command in insert mode */
295
296#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000297static int revins_on; /* reverse insert mode on */
298static int revins_chars; /* how much to skip after edit */
299static int revins_legal; /* was the last char 'legal'? */
300static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#endif
302
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303static int ins_need_undo; /* call u_save() before inserting a
304 char. Set when edit() is called.
305 after that arrow_used is used. */
306
307static int did_add_space = FALSE; /* auto_format() added an extra space
308 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200309static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
310 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
312/*
313 * edit(): Start inserting text.
314 *
315 * "cmdchar" can be:
316 * 'i' normal insert command
317 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100318 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 * 'R' replace command
320 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
321 * but still only one <CR> is inserted. The <Esc> is not used for redo.
322 * 'g' "gI" command.
323 * 'V' "gR" command for Virtual Replace mode.
324 * 'v' "gr" command for single character Virtual Replace mode.
325 *
326 * This function is not called recursively. For CTRL-O commands, it returns
327 * and lets the caller handle the Normal-mode command.
328 *
329 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
330 */
331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100332edit(
333 int cmdchar,
334 int startln, /* if set, insert at start of line */
335 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 int c = 0;
338 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100339 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000340 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 int i;
343 int did_backspace = TRUE; /* previous char was backspace */
344#ifdef FEAT_CINDENT
345 int line_is_white = FALSE; /* line is empty before insert */
346#endif
347 linenr_T old_topline = 0; /* topline before insertion */
348#ifdef FEAT_DIFF
349 int old_topfill = -1;
350#endif
351 int inserted_space = FALSE; /* just inserted a space */
352 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000353 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000355 /* Remember whether editing was restarted after CTRL-O. */
356 did_restart_edit = restart_edit;
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 /* sleep before redrawing, needed for "CTRL-O :" that results in an
359 * error message */
360 check_for_delay(TRUE);
361
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100362 /* set Insstart_orig to Insstart */
363 update_Insstart_orig = TRUE;
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365#ifdef HAVE_SANDBOX
366 /* Don't allow inserting in the sandbox. */
367 if (sandbox != 0)
368 {
369 EMSG(_(e_sandbox));
370 return FALSE;
371 }
372#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000373 /* Don't allow changes in the buffer while editing the cmdline. The
374 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000375 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000376 {
377 EMSG(_(e_secure));
378 return FALSE;
379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380
381#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000382 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000383 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000384 {
385 EMSG(_(e_secure));
386 return FALSE;
387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 ins_compl_clear(); /* clear stuff for CTRL-X mode */
389#endif
390
Bram Moolenaar843ee412004-06-30 16:16:41 +0000391#ifdef FEAT_AUTOCMD
392 /*
393 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
394 */
395 if (cmdchar != 'r' && cmdchar != 'v')
396 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100397 pos_T save_cursor = curwin->w_cursor;
398
Bram Moolenaar1e015462005-09-25 22:16:38 +0000399# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000400 if (cmdchar == 'R')
401 ptr = (char_u *)"r";
402 else if (cmdchar == 'V')
403 ptr = (char_u *)"v";
404 else
405 ptr = (char_u *)"i";
406 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200407 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000408# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000409 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100410
Bram Moolenaar097c9922013-05-19 21:15:15 +0200411 /* Make sure the cursor didn't move. Do call check_cursor_col() in
412 * case the text was modified. Since Insert mode was not started yet
413 * a call to check_cursor_col() may move the cursor, especially with
414 * the "A" command, thus set State to avoid that. Also check that the
415 * line number is still valid (lines may have been deleted).
416 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100417 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaar097c9922013-05-19 21:15:15 +0200418# ifdef FEAT_EVAL
419 && *get_vim_var_str(VV_CHAR) == NUL
420# endif
421 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100422 {
423 int save_state = State;
424
425 curwin->w_cursor = save_cursor;
426 State = INSERT;
427 check_cursor_col();
428 State = save_state;
429 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000430 }
431#endif
432
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200433#ifdef FEAT_CONCEAL
434 /* Check if the cursor line needs redrawing before changing State. If
435 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200436 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200437#endif
438
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439#ifdef FEAT_MOUSE
440 /*
441 * When doing a paste with the middle mouse button, Insstart is set to
442 * where the paste started.
443 */
444 if (where_paste_started.lnum != 0)
445 Insstart = where_paste_started;
446 else
447#endif
448 {
449 Insstart = curwin->w_cursor;
450 if (startln)
451 Insstart.col = 0;
452 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000453 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 Insstart_blank_vcol = MAXCOL;
455 if (!did_ai)
456 ai_col = 0;
457
458 if (cmdchar != NUL && restart_edit == 0)
459 {
460 ResetRedobuff();
461 AppendNumberToRedobuff(count);
462#ifdef FEAT_VREPLACE
463 if (cmdchar == 'V' || cmdchar == 'v')
464 {
465 /* "gR" or "gr" command */
466 AppendCharToRedobuff('g');
467 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
468 }
469 else
470#endif
471 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100472 if (cmdchar == K_PS)
473 AppendCharToRedobuff('a');
474 else
475 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 if (cmdchar == 'g') /* "gI" command */
477 AppendCharToRedobuff('I');
478 else if (cmdchar == 'r') /* "r<CR>" command */
479 count = 1; /* insert only one <CR> */
480 }
481 }
482
483 if (cmdchar == 'R')
484 {
485#ifdef FEAT_FKMAP
486 if (p_fkmap && p_ri)
487 {
488 beep_flush();
489 EMSG(farsi_text_3); /* encoded in Farsi */
490 State = INSERT;
491 }
492 else
493#endif
494 State = REPLACE;
495 }
496#ifdef FEAT_VREPLACE
497 else if (cmdchar == 'V' || cmdchar == 'v')
498 {
499 State = VREPLACE;
500 replaceState = VREPLACE;
501 orig_line_count = curbuf->b_ml.ml_line_count;
502 vr_lines_changed = 1;
503 }
504#endif
505 else
506 State = INSERT;
507
508 stop_insert_mode = FALSE;
509
510 /*
511 * Need to recompute the cursor position, it might move when the cursor is
512 * on a TAB or special character.
513 */
514 curs_columns(TRUE);
515
516 /*
517 * Enable langmap or IME, indicated by 'iminsert'.
518 * Note that IME may enabled/disabled without us noticing here, thus the
519 * 'iminsert' value may not reflect what is actually used. It is updated
520 * when hitting <Esc>.
521 */
522 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
523 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100524#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
526#endif
527
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528#ifdef FEAT_MOUSE
529 setmouse();
530#endif
531#ifdef FEAT_CMDL_INFO
532 clear_showcmd();
533#endif
534#ifdef FEAT_RIGHTLEFT
535 /* there is no reverse replace mode */
536 revins_on = (State == INSERT && p_ri);
537 if (revins_on)
538 undisplay_dollar();
539 revins_chars = 0;
540 revins_legal = 0;
541 revins_scol = -1;
542#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100543 if (!p_ek)
544 /* Disable bracketed paste mode, we won't recognize the escape
545 * sequences. */
546 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
548 /*
549 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100550 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
551 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
553 if (restart_edit != 0 && stuff_empty())
554 {
555#ifdef FEAT_MOUSE
556 /*
557 * After a paste we consider text typed to be part of the insert for
558 * the pasted text. You can backspace over the pasted text too.
559 */
560 if (where_paste_started.lnum)
561 arrow_used = FALSE;
562 else
563#endif
564 arrow_used = TRUE;
565 restart_edit = 0;
566
567 /*
568 * If the cursor was after the end-of-line before the CTRL-O and it is
569 * now at the end-of-line, put it after the end-of-line (this is not
570 * correct in very rare cases).
571 * Also do this if curswant is greater than the current virtual
572 * column. Eg after "^O$" or "^O80|".
573 */
574 validate_virtcol();
575 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000576 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 || curwin->w_curswant > curwin->w_virtcol)
578 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
579 {
580 if (ptr[1] == NUL)
581 ++curwin->w_cursor.col;
582#ifdef FEAT_MBYTE
583 else if (has_mbyte)
584 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000585 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (ptr[i] == NUL)
587 curwin->w_cursor.col += i;
588 }
589#endif
590 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000591 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
593 else
594 arrow_used = FALSE;
595
596 /* we are in insert mode now, don't need to start it anymore */
597 need_start_insertmode = FALSE;
598
599 /* Need to save the line for undo before inserting the first char. */
600 ins_need_undo = TRUE;
601
602#ifdef FEAT_MOUSE
603 where_paste_started.lnum = 0;
604#endif
605#ifdef FEAT_CINDENT
606 can_cindent = TRUE;
607#endif
608#ifdef FEAT_FOLDING
609 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
610 * restarting. */
611 if (!p_im && did_restart_edit == 0)
612 foldOpenCursor();
613#endif
614
615 /*
616 * If 'showmode' is set, show the current (insert/replace/..) mode.
617 * A warning message for changing a readonly file is given here, before
618 * actually changing anything. It's put after the mode, if any.
619 */
620 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000621 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 i = showmode();
623
624 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000625 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627#ifdef CURSOR_SHAPE
628 ui_cursor_shape(); /* may show different cursor shape */
629#endif
630#ifdef FEAT_DIGRAPHS
631 do_digraph(-1); /* clear digraphs */
632#endif
633
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000634 /*
635 * Get the current length of the redo buffer, those characters have to be
636 * skipped if we want to get to the inserted characters.
637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ptr = get_inserted();
639 if (ptr == NULL)
640 new_insert_skip = 0;
641 else
642 {
643 new_insert_skip = (int)STRLEN(ptr);
644 vim_free(ptr);
645 }
646
647 old_indent = 0;
648
649 /*
650 * Main loop in Insert mode: repeat until Insert mode is left.
651 */
652 for (;;)
653 {
654#ifdef FEAT_RIGHTLEFT
655 if (!revins_legal)
656 revins_scol = -1; /* reset on illegal motions */
657 else
658 revins_legal = 0;
659#endif
660 if (arrow_used) /* don't repeat insert when arrow key used */
661 count = 0;
662
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100663 if (update_Insstart_orig)
664 Insstart_orig = Insstart;
665
Bram Moolenaar00672e12016-06-26 18:38:13 +0200666 if (stop_insert_mode
667#ifdef FEAT_INS_EXPAND
668 && !pum_visible()
669#endif
670 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
672 /* ":stopinsert" used or 'insertmode' reset */
673 count = 0;
674 goto doESCkey;
675 }
676
677 /* set curwin->w_curswant for next K_DOWN or K_UP */
678 if (!arrow_used)
679 curwin->w_set_curswant = TRUE;
680
681 /* If there is no typeahead may check for timestamps (e.g., for when a
682 * menu invoked a shell command). */
683 if (stuff_empty())
684 {
685 did_check_timestamps = FALSE;
686 if (need_check_timestamps)
687 check_timestamps(FALSE);
688 }
689
690 /*
691 * When emsg() was called msg_scroll will have been set.
692 */
693 msg_scroll = FALSE;
694
695#ifdef FEAT_GUI
696 /* When 'mousefocus' is set a mouse movement may have taken us to
697 * another window. "need_mouse_correct" may then be set because of an
698 * autocommand. */
699 if (need_mouse_correct)
700 gui_mouse_correct();
701#endif
702
703#ifdef FEAT_FOLDING
704 /* Open fold at the cursor line, according to 'foldopen'. */
705 if (fdo_flags & FDO_INSERT)
706 foldOpenCursor();
707 /* Close folds where the cursor isn't, according to 'foldclose' */
708 if (!char_avail())
709 foldCheckClose();
710#endif
711
712 /*
713 * If we inserted a character at the last position of the last line in
714 * the window, scroll the window one line up. This avoids an extra
715 * redraw.
716 * This is detected when the cursor column is smaller after inserting
717 * something.
718 * Don't do this when the topline changed already, it has
719 * already been adjusted (by insertchar() calling open_line())).
720 */
721 if (curbuf->b_mod_set
722 && curwin->w_p_wrap
723 && !did_backspace
724 && curwin->w_topline == old_topline
725#ifdef FEAT_DIFF
726 && curwin->w_topfill == old_topfill
727#endif
728 )
729 {
730 mincol = curwin->w_wcol;
731 validate_cursor_col();
732
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000733 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 && curwin->w_wrow == W_WINROW(curwin)
735 + curwin->w_height - 1 - p_so
736 && (curwin->w_cursor.lnum != curwin->w_topline
737#ifdef FEAT_DIFF
738 || curwin->w_topfill > 0
739#endif
740 ))
741 {
742#ifdef FEAT_DIFF
743 if (curwin->w_topfill > 0)
744 --curwin->w_topfill;
745 else
746#endif
747#ifdef FEAT_FOLDING
748 if (hasFolding(curwin->w_topline, NULL, &old_topline))
749 set_topline(curwin, old_topline + 1);
750 else
751#endif
752 set_topline(curwin, curwin->w_topline + 1);
753 }
754 }
755
756 /* May need to adjust w_topline to show the cursor. */
757 update_topline();
758
759 did_backspace = FALSE;
760
761 validate_cursor(); /* may set must_redraw */
762
763 /*
764 * Redraw the display when no characters are waiting.
765 * Also shows mode, ruler and positions cursor.
766 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000767 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
769#ifdef FEAT_SCROLLBIND
770 if (curwin->w_p_scb)
771 do_check_scrollbind(TRUE);
772#endif
773
Bram Moolenaar860cae12010-06-05 23:22:07 +0200774#ifdef FEAT_CURSORBIND
775 if (curwin->w_p_crb)
776 do_check_cursorbind();
777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 update_curswant();
779 old_topline = curwin->w_topline;
780#ifdef FEAT_DIFF
781 old_topfill = curwin->w_topfill;
782#endif
783
784#ifdef USE_ON_FLY_SCROLL
785 dont_scroll = FALSE; /* allow scrolling here */
786#endif
787
788 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100789 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100791 if (c != K_CURSORHOLD)
792 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200793
794 /* After using CTRL-G U the next cursor key will not break undo. */
795 if (dont_sync_undo == MAYBE)
796 dont_sync_undo = TRUE;
797 else
798 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100799 if (cmdchar == K_PS)
800 /* Got here from normal mode when bracketed paste started. */
801 c = K_PS;
802 else
803 do
804 {
805 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100806 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000808#ifdef FEAT_AUTOCMD
809 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
810 did_cursorhold = TRUE;
811#endif
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#ifdef FEAT_RIGHTLEFT
814 if (p_hkmap && KeyTyped)
815 c = hkmap(c); /* Hebrew mode mapping */
816#endif
817#ifdef FEAT_FKMAP
818 if (p_fkmap && KeyTyped)
819 c = fkmap(c); /* Farsi mode mapping */
820#endif
821
822#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000823 /*
824 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000825 * and the cursor is still in the completed word. Only when there is
826 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000827 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000828 if (compl_started
829 && pum_wanted()
830 && curwin->w_cursor.col >= compl_col
831 && (compl_shown_match == NULL
832 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000833 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000834 /* BS: Delete one character from "compl_leader". */
835 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000836 && curwin->w_cursor.col > compl_col
837 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000838 continue;
839
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000840 /* When no match was selected or it was edited. */
841 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000842 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000843 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000844 * "compl_leader". Except when at the original match and
845 * there is nothing to add, CTRL-L works like CTRL-P then. */
846 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100847 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000848 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000849 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000850 {
851 ins_compl_addfrommatch();
852 continue;
853 }
854
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000855 /* A non-white character that fits in with the current
856 * completion: Add to "compl_leader". */
857 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000858 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100859#ifdef FEAT_AUTOCMD
860 /* Trigger InsertCharPre. */
861 char_u *str = do_insert_char_pre(c);
862 char_u *p;
863
864 if (str != NULL)
865 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100866 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100867 ins_compl_addleader(PTR2CHAR(p));
868 vim_free(str);
869 }
870 else
871#endif
872 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000873 continue;
874 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000875
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000876 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000877 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200878 if ((c == Ctrl_Y || (compl_enter_selects
879 && (c == CAR || c == K_KENTER || c == NL)))
880 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000881 {
882 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200883 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000884 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000885 }
886 }
887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
889 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000890 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000891 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000892 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#endif
894
Bram Moolenaar488c6512005-08-11 20:09:58 +0000895 /* CTRL-\ CTRL-N goes to Normal mode,
896 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
897 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (c == Ctrl_BSL)
899 {
900 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000901 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 ++no_mapping;
903 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000904 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 --no_mapping;
906 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000907 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000909 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 vungetc(c);
911 c = Ctrl_BSL;
912 }
913 else if (c == Ctrl_G && p_im)
914 continue;
915 else
916 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000917 if (c == Ctrl_O)
918 {
919 ins_ctrl_o();
920 ins_at_eol = FALSE; /* cursor keeps its column */
921 nomove = TRUE;
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 count = 0;
924 goto doESCkey;
925 }
926 }
927
928#ifdef FEAT_DIGRAPHS
929 c = do_digraph(c);
930#endif
931
932#ifdef FEAT_INS_EXPAND
933 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
934 goto docomplete;
935#endif
936 if (c == Ctrl_V || c == Ctrl_Q)
937 {
938 ins_ctrl_v();
939 c = Ctrl_V; /* pretend CTRL-V is last typed character */
940 continue;
941 }
942
943#ifdef FEAT_CINDENT
944 if (cindent_on()
945# ifdef FEAT_INS_EXPAND
946 && ctrl_x_mode == 0
947# endif
948 )
949 {
950 /* A key name preceded by a bang means this key is not to be
951 * inserted. Skip ahead to the re-indenting below.
952 * A key name preceded by a star means that indenting has to be
953 * done before inserting the key. */
954 line_is_white = inindent(0);
955 if (in_cinkeys(c, '!', line_is_white))
956 goto force_cindent;
957 if (can_cindent && in_cinkeys(c, '*', line_is_white)
958 && stop_arrow() == OK)
959 do_c_expr_indent();
960 }
961#endif
962
963#ifdef FEAT_RIGHTLEFT
964 if (curwin->w_p_rl)
965 switch (c)
966 {
967 case K_LEFT: c = K_RIGHT; break;
968 case K_S_LEFT: c = K_S_RIGHT; break;
969 case K_C_LEFT: c = K_C_RIGHT; break;
970 case K_RIGHT: c = K_LEFT; break;
971 case K_S_RIGHT: c = K_S_LEFT; break;
972 case K_C_RIGHT: c = K_C_LEFT; break;
973 }
974#endif
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 /*
977 * If 'keymodel' contains "startsel", may start selection. If it
978 * does, a CTRL-O and c will be stuffed, we need to get these
979 * characters.
980 */
981 if (ins_start_select(c))
982 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984 /*
985 * The big switch to handle a character in insert mode.
986 */
987 switch (c)
988 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000989 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 if (echeck_abbr(ESC + ABBR_OFF))
991 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200992 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#ifdef FEAT_CMDWIN
996 if (c == Ctrl_C && cmdwin_type != 0)
997 {
998 /* Close the cmdline window. */
999 cmdwin_result = K_IGNORE;
1000 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001001 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 goto doESCkey;
1003 }
1004#endif
1005
1006#ifdef UNIX
1007do_intr:
1008#endif
1009 /* when 'insertmode' set, and not halfway a mapping, don't leave
1010 * Insert mode */
1011 if (goto_im())
1012 {
1013 if (got_int)
1014 {
1015 (void)vgetc(); /* flush all buffers */
1016 got_int = FALSE;
1017 }
1018 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001019 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 break;
1021 }
1022doESCkey:
1023 /*
1024 * This is the ONLY return from edit()!
1025 */
1026 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1027 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001028 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 o_lnum = curwin->w_cursor.lnum;
1030
Bram Moolenaar488c6512005-08-11 20:09:58 +00001031 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001032 {
1033#ifdef FEAT_AUTOCMD
1034 if (cmdchar != 'r' && cmdchar != 'v')
1035 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1036 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001037 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 continue;
1042
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 case Ctrl_Z: /* suspend when 'insertmode' set */
1044 if (!p_im)
1045 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001046 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001047#ifdef CURSOR_SHAPE
1048 ui_cursor_shape(); /* may need to update cursor shape */
1049#endif
1050 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001051
1052 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001053#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001054 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 goto docomplete;
1056#endif
1057 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1058 break;
1059 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001060
1061#ifdef FEAT_VIRTUALEDIT
1062 /* don't move the cursor left when 'virtualedit' has "onemore". */
1063 if (ve_flags & VE_ONEMORE)
1064 {
1065 ins_at_eol = FALSE;
1066 nomove = TRUE;
1067 }
1068#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001069 count = 0;
1070 goto doESCkey;
1071
Bram Moolenaar572cb562005-08-05 21:35:02 +00001072 case K_INS: /* toggle insert/replace mode */
1073 case K_KINS:
1074 ins_insert(replaceState);
1075 break;
1076
1077 case K_SELECT: /* end of Select mode mapping - ignore */
1078 break;
1079
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 case K_HELP: /* Help key works like <ESC> <Help> */
1081 case K_F1:
1082 case K_XF1:
1083 stuffcharReadbuff(K_HELP);
1084 if (p_im)
1085 need_start_insertmode = TRUE;
1086 goto doESCkey;
1087
1088#ifdef FEAT_NETBEANS_INTG
1089 case K_F21: /* NetBeans command */
1090 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001091 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 --no_mapping;
1093 netbeans_keycommand(i);
1094 break;
1095#endif
1096
1097 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 case NUL:
1099 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 /* For ^@ the trailing ESC will end the insert, unless there is an
1101 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1103 && c != Ctrl_A && !p_im)
1104 goto doESCkey; /* quit insert mode */
1105 inserted_space = FALSE;
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_reg();
1110 auto_format(FALSE, TRUE);
1111 inserted_space = FALSE;
1112 break;
1113
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001114 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 ins_ctrl_g();
1116 break;
1117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case Ctrl_HAT: /* switch input mode and/or langmap */
1119 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
1122#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001123 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (!p_ari)
1125 goto normalchar;
1126 ins_ctrl_();
1127 break;
1128#endif
1129
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001130 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1132 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1133 goto docomplete;
1134#endif
1135 /* FALLTHROUGH */
1136
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138# ifdef FEAT_INS_EXPAND
1139 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1140 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001141 if (has_compl_option(FALSE))
1142 goto docomplete;
1143 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145# endif
1146 ins_shift(c, lastc);
1147 auto_format(FALSE, TRUE);
1148 inserted_space = FALSE;
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case K_KDEL:
1153 ins_del();
1154 auto_format(FALSE, TRUE);
1155 break;
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 case Ctrl_H:
1159 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1160 auto_format(FALSE, TRUE);
1161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1165 auto_format(FALSE, TRUE);
1166 break;
1167
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001168 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001169# ifdef FEAT_COMPL_FUNC
1170 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001171 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001172 goto docomplete;
1173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1175 auto_format(FALSE, TRUE);
1176 inserted_space = FALSE;
1177 break;
1178
1179#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 case K_LEFTMOUSE_NM:
1182 case K_LEFTDRAG:
1183 case K_LEFTRELEASE:
1184 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001185 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 case K_MIDDLEMOUSE:
1187 case K_MIDDLEDRAG:
1188 case K_MIDDLERELEASE:
1189 case K_RIGHTMOUSE:
1190 case K_RIGHTDRAG:
1191 case K_RIGHTRELEASE:
1192 case K_X1MOUSE:
1193 case K_X1DRAG:
1194 case K_X1RELEASE:
1195 case K_X2MOUSE:
1196 case K_X2DRAG:
1197 case K_X2RELEASE:
1198 ins_mouse(c);
1199 break;
1200
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001201 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001202 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 break;
1204
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001205 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001206 ins_mousescroll(MSCR_UP);
1207 break;
1208
1209 case K_MOUSELEFT: /* Scroll wheel left */
1210 ins_mousescroll(MSCR_LEFT);
1211 break;
1212
1213 case K_MOUSERIGHT: /* Scroll wheel right */
1214 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 break;
1216#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001217 case K_PS:
1218 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1219 if (cmdchar == K_PS)
1220 /* invoked from normal mode, bail out */
1221 goto doESCkey;
1222 break;
1223 case K_PE:
1224 /* Got K_PE without K_PS, ignore. */
1225 break;
1226
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001227#ifdef FEAT_GUI_TABLINE
1228 case K_TABLINE:
1229 case K_TABMENU:
1230 ins_tabline(c);
1231 break;
1232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 break;
1236
Bram Moolenaar754b5602006-02-09 23:53:20 +00001237#ifdef FEAT_AUTOCMD
1238 case K_CURSORHOLD: /* Didn't type something for a while. */
1239 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1240 did_cursorhold = TRUE;
1241 break;
1242#endif
1243
Bram Moolenaar4770d092006-01-12 23:22:24 +00001244#ifdef FEAT_GUI_W32
1245 /* On Win32 ignore <M-F4>, we get it when closing the window was
1246 * cancelled. */
1247 case K_F4:
1248 if (mod_mask != MOD_MASK_ALT)
1249 goto normalchar;
1250 break;
1251#endif
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253#ifdef FEAT_GUI
1254 case K_VER_SCROLLBAR:
1255 ins_scroll();
1256 break;
1257
1258 case K_HOR_SCROLLBAR:
1259 ins_horscroll();
1260 break;
1261#endif
1262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 case K_S_HOME:
1266 case K_C_HOME:
1267 ins_home(c);
1268 break;
1269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001270 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 case K_S_END:
1273 case K_C_END:
1274 ins_end(c);
1275 break;
1276
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001277 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001278 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1279 ins_s_left();
1280 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001281 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 break;
1283
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001284 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 case K_C_LEFT:
1286 ins_s_left();
1287 break;
1288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001289 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001290 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1291 ins_s_right();
1292 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001293 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 break;
1295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 case K_C_RIGHT:
1298 ins_s_right();
1299 break;
1300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001301 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001302#ifdef FEAT_INS_EXPAND
1303 if (pum_visible())
1304 goto docomplete;
1305#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001306 if (mod_mask & MOD_MASK_SHIFT)
1307 ins_pageup();
1308 else
1309 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 break;
1311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001312 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 case K_PAGEUP:
1314 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001315#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001316 if (pum_visible())
1317 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 ins_pageup();
1320 break;
1321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001322 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001323#ifdef FEAT_INS_EXPAND
1324 if (pum_visible())
1325 goto docomplete;
1326#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001327 if (mod_mask & MOD_MASK_SHIFT)
1328 ins_pagedown();
1329 else
1330 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 break;
1332
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001333 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 case K_PAGEDOWN:
1335 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001336#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001337 if (pum_visible())
1338 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 ins_pagedown();
1341 break;
1342
1343#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 ins_drop();
1346 break;
1347#endif
1348
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001349 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 c = TAB;
1351 /* FALLTHROUGH */
1352
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001353 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1355 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1356 goto docomplete;
1357#endif
1358 inserted_space = FALSE;
1359 if (ins_tab())
1360 goto normalchar; /* insert TAB as a normal char */
1361 auto_format(FALSE, TRUE);
1362 break;
1363
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001364 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 c = CAR;
1366 /* FALLTHROUGH */
1367 case CAR:
1368 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001369#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /* In a quickfix window a <CR> jumps to the error under the
1371 * cursor. */
1372 if (bt_quickfix(curbuf) && c == CAR)
1373 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001374 if (curwin->w_llist_ref == NULL) /* quickfix window */
1375 do_cmdline_cmd((char_u *)".cc");
1376 else /* location list window */
1377 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 break;
1379 }
1380#endif
1381#ifdef FEAT_CMDWIN
1382 if (cmdwin_type != 0)
1383 {
1384 /* Execute the command in the cmdline window. */
1385 cmdwin_result = CAR;
1386 goto doESCkey;
1387 }
1388#endif
1389 if (ins_eol(c) && !p_im)
1390 goto doESCkey; /* out of memory */
1391 auto_format(FALSE, FALSE);
1392 inserted_space = FALSE;
1393 break;
1394
Bram Moolenaar860cae12010-06-05 23:22:07 +02001395#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001396 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397# ifdef FEAT_INS_EXPAND
1398 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1399 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001400 if (has_compl_option(TRUE))
1401 goto docomplete;
1402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404# endif
1405# ifdef FEAT_DIGRAPHS
1406 c = ins_digraph();
1407 if (c == NUL)
1408 break;
1409# endif
1410 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
1413#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001414 case Ctrl_X: /* Enter CTRL-X mode */
1415 ins_ctrl_x();
1416 break;
1417
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001418 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (ctrl_x_mode != CTRL_X_TAGS)
1420 goto normalchar;
1421 goto docomplete;
1422
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001423 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (ctrl_x_mode != CTRL_X_FILES)
1425 goto normalchar;
1426 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001427
1428 case 's': /* Spelling completion after ^X */
1429 case Ctrl_S:
1430 if (ctrl_x_mode != CTRL_X_SPELL)
1431 goto normalchar;
1432 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433#endif
1434
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001435 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436#ifdef FEAT_INS_EXPAND
1437 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1438#endif
1439 {
1440 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1441 if (p_im)
1442 {
1443 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1444 break;
1445 goto doESCkey;
1446 }
1447 goto normalchar;
1448 }
1449#ifdef FEAT_INS_EXPAND
1450 /* FALLTHROUGH */
1451
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001452 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 case Ctrl_N:
1454 /* if 'complete' is empty then plain ^P is no longer special,
1455 * but it is under other ^X modes */
1456 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001457 && (ctrl_x_mode == CTRL_X_NORMAL
1458 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001459 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 goto normalchar;
1461
1462docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001463 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001464#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001465 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001466#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001467 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001468 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001469#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001470 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001471#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001472 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 break;
1474#endif /* FEAT_INS_EXPAND */
1475
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001476 case Ctrl_Y: /* copy from previous line or scroll down */
1477 case Ctrl_E: /* copy from next line or scroll up */
1478 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 break;
1480
1481 default:
1482#ifdef UNIX
1483 if (c == intr_char) /* special interrupt char */
1484 goto do_intr;
1485#endif
1486
Bram Moolenaare659c952011-05-19 17:25:41 +02001487normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001489 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001491#ifdef FEAT_AUTOCMD
1492 if (!p_paste)
1493 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001494 /* Trigger InsertCharPre. */
1495 char_u *str = do_insert_char_pre(c);
1496 char_u *p;
1497
1498 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001499 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001500 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001501 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001502 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001503 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001504 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001505 c = PTR2CHAR(p);
1506 if (c == CAR || c == K_KENTER || c == NL)
1507 ins_eol(c);
1508 else
1509 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001510 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001511 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001512 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001513 vim_free(str);
1514 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001515 }
1516
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001517 /* If the new value is already inserted or an empty string
1518 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001519 if (c == NUL)
1520 break;
1521 }
1522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523#ifdef FEAT_SMARTINDENT
1524 /* Try to perform smart-indenting. */
1525 ins_try_si(c);
1526#endif
1527
1528 if (c == ' ')
1529 {
1530 inserted_space = TRUE;
1531#ifdef FEAT_CINDENT
1532 if (inindent(0))
1533 can_cindent = FALSE;
1534#endif
1535 if (Insstart_blank_vcol == MAXCOL
1536 && curwin->w_cursor.lnum == Insstart.lnum)
1537 Insstart_blank_vcol = get_nolist_virtcol();
1538 }
1539
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001540 /* Insert a normal character and check for abbreviations on a
1541 * special character. Let CTRL-] expand abbreviations without
1542 * inserting it. */
1543 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544#ifdef FEAT_MBYTE
1545 /* Add ABBR_OFF for characters above 0x100, this is
1546 * what check_abbr() expects. */
1547 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1548#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001549 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551 insert_special(c, FALSE, FALSE);
1552#ifdef FEAT_RIGHTLEFT
1553 revins_legal++;
1554 revins_chars++;
1555#endif
1556 }
1557
1558 auto_format(FALSE, TRUE);
1559
1560#ifdef FEAT_FOLDING
1561 /* When inserting a character the cursor line must never be in a
1562 * closed fold. */
1563 foldOpenCursor();
1564#endif
1565 break;
1566 } /* end of switch (c) */
1567
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001568#ifdef FEAT_AUTOCMD
1569 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001570 if (c != K_CURSORHOLD
1571# ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001572 /* but not in CTRL-X mode, a script can't restore the state */
1573 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar245c4102016-04-20 17:37:41 +02001574# endif
1575 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001576 did_cursorhold = FALSE;
1577#endif
1578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 /* If the cursor was moved we didn't just insert a space */
1580 if (arrow_used)
1581 inserted_space = FALSE;
1582
1583#ifdef FEAT_CINDENT
1584 if (can_cindent && cindent_on()
1585# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001586 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587# endif
1588 )
1589 {
1590force_cindent:
1591 /*
1592 * Indent now if a key was typed that is in 'cinkeys'.
1593 */
1594 if (in_cinkeys(c, ' ', line_is_white))
1595 {
1596 if (stop_arrow() == OK)
1597 /* re-indent the current line */
1598 do_c_expr_indent();
1599 }
1600 }
1601#endif /* FEAT_CINDENT */
1602
1603 } /* for (;;) */
1604 /* NOTREACHED */
1605}
1606
1607/*
1608 * Redraw for Insert mode.
1609 * This is postponed until getting the next character to make '$' in the 'cpo'
1610 * option work correctly.
1611 * Only redraw when there are no characters available. This speeds up
1612 * inserting sequences of characters (e.g., for CTRL-R).
1613 */
1614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001615ins_redraw(
1616 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001618#ifdef FEAT_CONCEAL
1619 linenr_T conceal_old_cursor_line = 0;
1620 linenr_T conceal_new_cursor_line = 0;
1621 int conceal_update_lines = FALSE;
1622#endif
1623
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624 if (char_avail())
1625 return;
1626
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001627#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001628 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1629 * visible, the command might delete it. */
1630 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001631# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001632 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001633# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001634# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001635 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001636# endif
1637# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001638 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001639# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001640 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001641# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001642 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001643# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001644# ifdef FEAT_INS_EXPAND
1645 && !pum_visible()
1646# endif
1647 )
1648 {
1649# ifdef FEAT_SYN_HL
1650 /* Need to update the screen first, to make sure syntax
1651 * highlighting is correct after making a change (e.g., inserting
1652 * a "(". The autocommand may also require a redraw, so it's done
1653 * again below, unfortunately. */
1654 if (syntax_present(curwin) && must_redraw)
1655 update_screen(0);
1656# endif
1657# ifdef FEAT_AUTOCMD
1658 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001659 {
1660 /* Make sure curswant is correct, an autocommand may call
1661 * getcurpos(). */
1662 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001663 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001664 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665# endif
1666# ifdef FEAT_CONCEAL
1667 if (curwin->w_p_cole > 0)
1668 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001669# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001670 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001671# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001672 conceal_new_cursor_line = curwin->w_cursor.lnum;
1673 conceal_update_lines = TRUE;
1674 }
1675# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001676# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001677 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001678# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001679 }
1680#endif
1681
1682#ifdef FEAT_AUTOCMD
1683 /* Trigger TextChangedI if b_changedtick differs. */
1684 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001685 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001686# ifdef FEAT_INS_EXPAND
1687 && !pum_visible()
1688# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001689 )
1690 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001691 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1692 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001694
1695# ifdef FEAT_INS_EXPAND
1696 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1697 * TextChangedI will need to trigger for backwards compatibility, thus use
1698 * different b_last_changedtick* variables. */
1699 if (ready && has_textchangedP()
1700 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1701 && pum_visible())
1702 {
1703 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
1704 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1705 }
1706# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001707#endif
1708
1709 if (must_redraw)
1710 update_screen(0);
1711 else if (clear_cmdline || redraw_cmdline)
1712 showmode(); /* clear cmdline and show mode */
1713# if defined(FEAT_CONCEAL)
1714 if ((conceal_update_lines
1715 && (conceal_old_cursor_line != conceal_new_cursor_line
1716 || conceal_cursor_line(curwin)))
1717 || need_cursor_line_redraw)
1718 {
1719 if (conceal_old_cursor_line != conceal_new_cursor_line)
1720 update_single_line(curwin, conceal_old_cursor_line);
1721 update_single_line(curwin, conceal_new_cursor_line == 0
1722 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1723 curwin->w_valid &= ~VALID_CROW;
1724 }
1725# endif
1726 showruler(FALSE);
1727 setcursor();
1728 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729}
1730
1731/*
1732 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1733 */
1734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001735ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736{
1737 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001738 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001741 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
1743 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001744 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001746 did_putchar = TRUE;
1747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1749
1750#ifdef FEAT_CMDL_INFO
1751 add_to_showcmd_c(Ctrl_V);
1752#endif
1753
1754 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001755 if (did_putchar)
1756 /* when the line fits in 'columns' the '^' is at the start of the next
1757 * line and will not removed by the redraw */
1758 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759#ifdef FEAT_CMDL_INFO
1760 clear_showcmd();
1761#endif
1762 insert_special(c, FALSE, TRUE);
1763#ifdef FEAT_RIGHTLEFT
1764 revins_chars++;
1765 revins_legal++;
1766#endif
1767}
1768
1769/*
1770 * Put a character directly onto the screen. It's not stored in a buffer.
1771 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1772 */
1773static int pc_status;
1774#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1775#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1776#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1777#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779static int pc_attr;
1780static int pc_row;
1781static int pc_col;
1782
1783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001784edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 int attr;
1787
1788 if (ScreenLines != NULL)
1789 {
1790 update_topline(); /* just in case w_topline isn't valid */
1791 validate_cursor();
1792 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001793 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 else
1795 attr = 0;
1796 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001797 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1799 pc_status = PC_STATUS_UNSET;
1800#endif
1801#ifdef FEAT_RIGHTLEFT
1802 if (curwin->w_p_rl)
1803 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001804 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805# ifdef FEAT_MBYTE
1806 if (has_mbyte)
1807 {
1808 int fix_col = mb_fix_col(pc_col, pc_row);
1809
1810 if (fix_col != pc_col)
1811 {
1812 screen_putchar(' ', pc_row, fix_col, attr);
1813 --curwin->w_wcol;
1814 pc_status = PC_STATUS_RIGHT;
1815 }
1816 }
1817# endif
1818 }
1819 else
1820#endif
1821 {
1822 pc_col += curwin->w_wcol;
1823#ifdef FEAT_MBYTE
1824 if (mb_lefthalve(pc_row, pc_col))
1825 pc_status = PC_STATUS_LEFT;
1826#endif
1827 }
1828
1829 /* save the character to be able to put it back */
1830#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1831 if (pc_status == PC_STATUS_UNSET)
1832#endif
1833 {
1834 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1835 pc_status = PC_STATUS_SET;
1836 }
1837 screen_putchar(c, pc_row, pc_col, attr);
1838 }
1839}
1840
1841/*
1842 * Undo the previous edit_putchar().
1843 */
1844 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001845edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846{
1847 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1848 {
1849#if defined(FEAT_MBYTE)
1850 if (pc_status == PC_STATUS_RIGHT)
1851 ++curwin->w_wcol;
1852 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1853 redrawWinline(curwin->w_cursor.lnum, FALSE);
1854 else
1855#endif
1856 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1857 }
1858}
1859
1860/*
1861 * Called when p_dollar is set: display a '$' at the end of the changed text
1862 * Only works when cursor is in the line that changes.
1863 */
1864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001865display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 colnr_T save_col;
1868
1869 if (!redrawing())
1870 return;
1871
1872 cursor_off();
1873 save_col = curwin->w_cursor.col;
1874 curwin->w_cursor.col = col;
1875#ifdef FEAT_MBYTE
1876 if (has_mbyte)
1877 {
1878 char_u *p;
1879
1880 /* If on the last byte of a multi-byte move to the first byte. */
1881 p = ml_get_curline();
1882 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1883 }
1884#endif
1885 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001886 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
1888 edit_putchar('$', FALSE);
1889 dollar_vcol = curwin->w_virtcol;
1890 }
1891 curwin->w_cursor.col = save_col;
1892}
1893
1894/*
1895 * Call this function before moving the cursor from the normal insert position
1896 * in insert mode.
1897 */
1898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001899undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001901 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001903 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 redrawWinline(curwin->w_cursor.lnum, FALSE);
1905 }
1906}
1907
1908/*
1909 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1910 * Keep the cursor on the same character.
1911 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1912 * type == INDENT_DEC decrease indent (for CTRL-D)
1913 * type == INDENT_SET set indent to "amount"
1914 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1915 */
1916 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001917change_indent(
1918 int type,
1919 int amount,
1920 int round,
1921 int replaced, /* replaced character, put on replace stack */
1922 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923{
1924 int vcol;
1925 int last_vcol;
1926 int insstart_less; /* reduction for Insstart.col */
1927 int new_cursor_col;
1928 int i;
1929 char_u *ptr;
1930 int save_p_list;
1931 int start_col;
1932 colnr_T vc;
1933#ifdef FEAT_VREPLACE
1934 colnr_T orig_col = 0; /* init for GCC */
1935 char_u *new_line, *orig_line = NULL; /* init for GCC */
1936
1937 /* VREPLACE mode needs to know what the line was like before changing */
1938 if (State & VREPLACE_FLAG)
1939 {
1940 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1941 orig_col = curwin->w_cursor.col;
1942 }
1943#endif
1944
1945 /* for the following tricks we don't want list mode */
1946 save_p_list = curwin->w_p_list;
1947 curwin->w_p_list = FALSE;
1948 vc = getvcol_nolist(&curwin->w_cursor);
1949 vcol = vc;
1950
1951 /*
1952 * For Replace mode we need to fix the replace stack later, which is only
1953 * possible when the cursor is in the indent. Remember the number of
1954 * characters before the cursor if it's possible.
1955 */
1956 start_col = curwin->w_cursor.col;
1957
1958 /* determine offset from first non-blank */
1959 new_cursor_col = curwin->w_cursor.col;
1960 beginline(BL_WHITE);
1961 new_cursor_col -= curwin->w_cursor.col;
1962
1963 insstart_less = curwin->w_cursor.col;
1964
1965 /*
1966 * If the cursor is in the indent, compute how many screen columns the
1967 * cursor is to the left of the first non-blank.
1968 */
1969 if (new_cursor_col < 0)
1970 vcol = get_indent() - vcol;
1971
1972 if (new_cursor_col > 0) /* can't fix replace stack */
1973 start_col = -1;
1974
1975 /*
1976 * Set the new indent. The cursor will be put on the first non-blank.
1977 */
1978 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001979 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 else
1981 {
1982#ifdef FEAT_VREPLACE
1983 int save_State = State;
1984
1985 /* Avoid being called recursively. */
1986 if (State & VREPLACE_FLAG)
1987 State = INSERT;
1988#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001989 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990#ifdef FEAT_VREPLACE
1991 State = save_State;
1992#endif
1993 }
1994 insstart_less -= curwin->w_cursor.col;
1995
1996 /*
1997 * Try to put cursor on same character.
1998 * If the cursor is at or after the first non-blank in the line,
1999 * compute the cursor column relative to the column of the first
2000 * non-blank character.
2001 * If we are not in insert mode, leave the cursor on the first non-blank.
2002 * If the cursor is before the first non-blank, position it relative
2003 * to the first non-blank, counted in screen columns.
2004 */
2005 if (new_cursor_col >= 0)
2006 {
2007 /*
2008 * When changing the indent while the cursor is touching it, reset
2009 * Insstart_col to 0.
2010 */
2011 if (new_cursor_col == 0)
2012 insstart_less = MAXCOL;
2013 new_cursor_col += curwin->w_cursor.col;
2014 }
2015 else if (!(State & INSERT))
2016 new_cursor_col = curwin->w_cursor.col;
2017 else
2018 {
2019 /*
2020 * Compute the screen column where the cursor should be.
2021 */
2022 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002023 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
2025 /*
2026 * Advance the cursor until we reach the right screen column.
2027 */
2028 vcol = last_vcol = 0;
2029 new_cursor_col = -1;
2030 ptr = ml_get_curline();
2031 while (vcol <= (int)curwin->w_virtcol)
2032 {
2033 last_vcol = vcol;
2034#ifdef FEAT_MBYTE
2035 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002036 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 else
2038#endif
2039 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002040 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
2042 vcol = last_vcol;
2043
2044 /*
2045 * May need to insert spaces to be able to position the cursor on
2046 * the right screen column.
2047 */
2048 if (vcol != (int)curwin->w_virtcol)
2049 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002050 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002052 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 if (ptr != NULL)
2054 {
2055 new_cursor_col += i;
2056 ptr[i] = NUL;
2057 while (--i >= 0)
2058 ptr[i] = ' ';
2059 ins_str(ptr);
2060 vim_free(ptr);
2061 }
2062 }
2063
2064 /*
2065 * When changing the indent while the cursor is in it, reset
2066 * Insstart_col to 0.
2067 */
2068 insstart_less = MAXCOL;
2069 }
2070
2071 curwin->w_p_list = save_p_list;
2072
2073 if (new_cursor_col <= 0)
2074 curwin->w_cursor.col = 0;
2075 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002076 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 curwin->w_set_curswant = TRUE;
2078 changed_cline_bef_curs();
2079
2080 /*
2081 * May have to adjust the start of the insert.
2082 */
2083 if (State & INSERT)
2084 {
2085 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2086 {
2087 if ((int)Insstart.col <= insstart_less)
2088 Insstart.col = 0;
2089 else
2090 Insstart.col -= insstart_less;
2091 }
2092 if ((int)ai_col <= insstart_less)
2093 ai_col = 0;
2094 else
2095 ai_col -= insstart_less;
2096 }
2097
2098 /*
2099 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2100 * If the number of characters before the cursor decreased, need to pop a
2101 * few characters from the replace stack.
2102 * If the number of characters before the cursor increased, need to push a
2103 * few NULs onto the replace stack.
2104 */
2105 if (REPLACE_NORMAL(State) && start_col >= 0)
2106 {
2107 while (start_col > (int)curwin->w_cursor.col)
2108 {
2109 replace_join(0); /* remove a NUL from the replace stack */
2110 --start_col;
2111 }
2112 while (start_col < (int)curwin->w_cursor.col || replaced)
2113 {
2114 replace_push(NUL);
2115 if (replaced)
2116 {
2117 replace_push(replaced);
2118 replaced = NUL;
2119 }
2120 ++start_col;
2121 }
2122 }
2123
2124#ifdef FEAT_VREPLACE
2125 /*
2126 * For VREPLACE mode, we also have to fix the replace stack. In this case
2127 * it is always possible because we backspace over the whole line and then
2128 * put it back again the way we wanted it.
2129 */
2130 if (State & VREPLACE_FLAG)
2131 {
2132 /* If orig_line didn't allocate, just return. At least we did the job,
2133 * even if you can't backspace. */
2134 if (orig_line == NULL)
2135 return;
2136
2137 /* Save new line */
2138 new_line = vim_strsave(ml_get_curline());
2139 if (new_line == NULL)
2140 return;
2141
2142 /* We only put back the new line up to the cursor */
2143 new_line[curwin->w_cursor.col] = NUL;
2144
2145 /* Put back original line */
2146 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2147 curwin->w_cursor.col = orig_col;
2148
2149 /* Backspace from cursor to start of line */
2150 backspace_until_column(0);
2151
2152 /* Insert new stuff into line again */
2153 ins_bytes(new_line);
2154
2155 vim_free(new_line);
2156 }
2157#endif
2158}
2159
2160/*
2161 * Truncate the space at the end of a line. This is to be used only in an
2162 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2163 * modes.
2164 */
2165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002166truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167{
2168 int i;
2169
2170 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002171 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
2173 if (State & REPLACE_FLAG)
2174 replace_join(0); /* remove a NUL from the replace stack */
2175 }
2176 line[i + 1] = NUL;
2177}
2178
2179#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2180 || defined(FEAT_COMMENTS) || defined(PROTO)
2181/*
2182 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2183 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002184 * Will attempt not to go before "col" even when there is a composing
2185 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 */
2187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002188backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189{
2190 while ((int)curwin->w_cursor.col > col)
2191 {
2192 curwin->w_cursor.col--;
2193 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002194 replace_do_bs(col);
2195 else if (!del_char_after_col(col))
2196 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 }
2198}
2199#endif
2200
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002201/*
2202 * Like del_char(), but make sure not to go before column "limit_col".
2203 * Only matters when there are composing characters.
2204 * Return TRUE when something was deleted.
2205 */
2206 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002207del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002208{
2209#ifdef FEAT_MBYTE
2210 if (enc_utf8 && limit_col >= 0)
2211 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002212 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002213
2214 /* Make sure the cursor is at the start of a character, but
2215 * skip forward again when going too far back because of a
2216 * composing character. */
2217 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002218 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002219 {
2220 int l = utf_ptr2len(ml_get_cursor());
2221
2222 if (l == 0) /* end of line */
2223 break;
2224 curwin->w_cursor.col += l;
2225 }
2226 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2227 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002228 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002229 }
2230 else
2231#endif
2232 (void)del_char(FALSE);
2233 return TRUE;
2234}
2235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2237/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002238 * CTRL-X pressed in Insert mode.
2239 */
2240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002241ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002242{
2243 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2244 * CTRL-V works like CTRL-N */
2245 if (ctrl_x_mode != CTRL_X_CMDLINE)
2246 {
2247 /* if the next ^X<> won't ADD nothing, then reset
2248 * compl_cont_status */
2249 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002250 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002251 else
2252 compl_cont_status = 0;
2253 /* We're not sure which CTRL-X mode it will be yet */
2254 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2255 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2256 edit_submode_pre = NULL;
2257 showmode();
2258 }
2259}
2260
2261/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002262 * Whether other than default completion has been selected.
2263 */
2264 int
2265ctrl_x_mode_not_default(void)
2266{
2267 return ctrl_x_mode != CTRL_X_NORMAL;
2268}
2269
2270/*
2271 * Whether CTRL-X was typed without a following character.
2272 */
2273 int
2274ctrl_x_mode_not_defined_yet(void)
2275{
2276 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2277}
2278
2279/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002280 * Return TRUE if the 'dict' or 'tsr' option can be used.
2281 */
2282 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002283has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002284{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002285 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002286# ifdef FEAT_SPELL
2287 && !curwin->w_p_spell
2288# endif
2289 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002290 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2291 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002292 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002293 edit_submode = NULL;
2294 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2295 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002296 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002297 if (emsg_silent == 0)
2298 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002299 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002300 setcursor();
2301 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002302#ifdef FEAT_EVAL
2303 if (!get_vim_var_nr(VV_TESTING))
2304#endif
2305 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002306 }
2307 return FALSE;
2308 }
2309 return TRUE;
2310}
2311
2312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2314 * This depends on the current mode.
2315 */
2316 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002317vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
2319 /* Always allow ^R - let it's results then be checked */
2320 if (c == Ctrl_R)
2321 return TRUE;
2322
Bram Moolenaare3226be2005-12-18 22:10:00 +00002323 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002324 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002325 return TRUE;
2326
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 switch (ctrl_x_mode)
2328 {
2329 case 0: /* Not in any CTRL-X mode */
2330 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2331 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002332 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2334 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2335 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002336 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002337 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 case CTRL_X_SCROLL:
2339 return (c == Ctrl_Y || c == Ctrl_E);
2340 case CTRL_X_WHOLE_LINE:
2341 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2342 case CTRL_X_FILES:
2343 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2344 case CTRL_X_DICTIONARY:
2345 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2346 case CTRL_X_THESAURUS:
2347 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2348 case CTRL_X_TAGS:
2349 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2350#ifdef FEAT_FIND_ID
2351 case CTRL_X_PATH_PATTERNS:
2352 return (c == Ctrl_P || c == Ctrl_N);
2353 case CTRL_X_PATH_DEFINES:
2354 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2355#endif
2356 case CTRL_X_CMDLINE:
2357 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2358 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002359#ifdef FEAT_COMPL_FUNC
2360 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002361 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002362 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002363 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002364#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002365 case CTRL_X_SPELL:
2366 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002367 case CTRL_X_EVAL:
2368 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002370 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 return FALSE;
2372}
2373
2374/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002375 * Return TRUE when character "c" is part of the item currently being
2376 * completed. Used to decide whether to abandon complete mode when the menu
2377 * is visible.
2378 */
2379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002380ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002381{
2382 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2383 /* When expanding an identifier only accept identifier chars. */
2384 return vim_isIDc(c);
2385
2386 switch (ctrl_x_mode)
2387 {
2388 case CTRL_X_FILES:
2389 /* When expanding file name only accept file name chars. But not
2390 * path separators, so that "proto/<Tab>" expands files in
2391 * "proto", not "proto/" as a whole */
2392 return vim_isfilec(c) && !vim_ispathsep(c);
2393
2394 case CTRL_X_CMDLINE:
2395 case CTRL_X_OMNI:
2396 /* Command line and Omni completion can work with just about any
2397 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002398 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002399
2400 case CTRL_X_WHOLE_LINE:
2401 /* For while line completion a space can be part of the line. */
2402 return vim_isprintc(c);
2403 }
2404 return vim_iswordc(c);
2405}
2406
2407/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002408 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002410 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 * the rest of the word to be in -- webb
2412 */
2413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002414ins_compl_add_infercase(
2415 char_u *str,
2416 int len,
2417 int icase,
2418 char_u *fname,
2419 int dir,
2420 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002422 char_u *p;
2423 int i, c;
2424 int actual_len; /* Take multi-byte characters */
2425 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002426 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002427 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 int has_lower = FALSE;
2429 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002431 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002433 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
Bram Moolenaara2993e12007-08-12 14:38:46 +00002435 /* Find actual length of completion. */
2436#ifdef FEAT_MBYTE
2437 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002439 p = str;
2440 actual_len = 0;
2441 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002443 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002444 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
2446 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002447 else
2448#endif
2449 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaara2993e12007-08-12 14:38:46 +00002451 /* Find actual length of original text. */
2452#ifdef FEAT_MBYTE
2453 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002455 p = compl_orig_text;
2456 actual_compl_length = 0;
2457 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002459 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002460 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
2462 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002463 else
2464#endif
2465 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002467 /* "actual_len" may be smaller than "actual_compl_length" when using
2468 * thesaurus, only use the minimum when comparing. */
2469 min_len = actual_len < actual_compl_length
2470 ? actual_len : actual_compl_length;
2471
Bram Moolenaara2993e12007-08-12 14:38:46 +00002472 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002473 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002474 if (wca != NULL)
2475 {
2476 p = str;
2477 for (i = 0; i < actual_len; ++i)
2478#ifdef FEAT_MBYTE
2479 if (has_mbyte)
2480 wca[i] = mb_ptr2char_adv(&p);
2481 else
2482#endif
2483 wca[i] = *(p++);
2484
2485 /* Rule 1: Were any chars converted to lower? */
2486 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002487 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002488 {
2489#ifdef FEAT_MBYTE
2490 if (has_mbyte)
2491 c = mb_ptr2char_adv(&p);
2492 else
2493#endif
2494 c = *(p++);
2495 if (MB_ISLOWER(c))
2496 {
2497 has_lower = TRUE;
2498 if (MB_ISUPPER(wca[i]))
2499 {
2500 /* Rule 1 is satisfied. */
2501 for (i = actual_compl_length; i < actual_len; ++i)
2502 wca[i] = MB_TOLOWER(wca[i]);
2503 break;
2504 }
2505 }
2506 }
2507
2508 /*
2509 * Rule 2: No lower case, 2nd consecutive letter converted to
2510 * upper case.
2511 */
2512 if (!has_lower)
2513 {
2514 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002515 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002516 {
2517#ifdef FEAT_MBYTE
2518 if (has_mbyte)
2519 c = mb_ptr2char_adv(&p);
2520 else
2521#endif
2522 c = *(p++);
2523 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2524 {
2525 /* Rule 2 is satisfied. */
2526 for (i = actual_compl_length; i < actual_len; ++i)
2527 wca[i] = MB_TOUPPER(wca[i]);
2528 break;
2529 }
2530 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2531 }
2532 }
2533
2534 /* Copy the original case of the part we typed. */
2535 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002536 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002537 {
2538#ifdef FEAT_MBYTE
2539 if (has_mbyte)
2540 c = mb_ptr2char_adv(&p);
2541 else
2542#endif
2543 c = *(p++);
2544 if (MB_ISLOWER(c))
2545 wca[i] = MB_TOLOWER(wca[i]);
2546 else if (MB_ISUPPER(c))
2547 wca[i] = MB_TOUPPER(wca[i]);
2548 }
2549
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002550 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002551 * Generate encoding specific output from wide character array.
2552 * Multi-byte characters can occupy up to five bytes more than
2553 * ASCII characters, and we also need one byte for NUL, so stay
2554 * six bytes away from the edge of IObuff.
2555 */
2556 p = IObuff;
2557 i = 0;
2558 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2559#ifdef FEAT_MBYTE
2560 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002561 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002562 else
2563#endif
2564 *(p++) = wca[i++];
2565 *p = NUL;
2566
2567 vim_free(wca);
2568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002570 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2571 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002573 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574}
2575
2576/*
2577 * Add a match to the list of matches.
2578 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002579 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002580 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002582 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002583ins_compl_add(
2584 char_u *str,
2585 int len,
2586 int icase,
2587 char_u *fname,
2588 char_u **cptext, /* extra text for popup menu or NULL */
2589 int cdir,
2590 int flags,
2591 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002593 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002594 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
2596 ui_breakcheck();
2597 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002598 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 if (len < 0)
2600 len = (int)STRLEN(str);
2601
2602 /*
2603 * If the same match is already present, don't add it.
2604 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002605 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002607 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 do
2609 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002610 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002611 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002612 && match->cp_str[len] == NUL)
2613 return NOTDONE;
2614 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002615 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 }
2617
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002618 /* Remove any popup menu before changing the list of matches. */
2619 ins_compl_del_pum();
2620
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 /*
2622 * Allocate a new match structure.
2623 * Copy the values to the new match structure.
2624 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002625 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002627 return FAIL;
2628 match->cp_number = -1;
2629 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002630 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002631 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
2633 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002634 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002636 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002639 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2641 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002642 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002643 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002644 && compl_curr_match->cp_fname != NULL
2645 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002646 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002647 else if (fname != NULL)
2648 {
2649 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002650 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002653 match->cp_fname = NULL;
2654 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002655
2656 if (cptext != NULL)
2657 {
2658 int i;
2659
2660 for (i = 0; i < CPT_COUNT; ++i)
2661 if (cptext[i] != NULL && *cptext[i] != NUL)
2662 match->cp_text[i] = vim_strsave(cptext[i]);
2663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
2665 /*
2666 * Link the new match structure in the list of matches.
2667 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002668 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002669 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 else if (dir == FORWARD)
2671 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002672 match->cp_next = compl_curr_match->cp_next;
2673 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 }
2675 else /* BACKWARD */
2676 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002677 match->cp_next = compl_curr_match;
2678 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002680 if (match->cp_next)
2681 match->cp_next->cp_prev = match;
2682 if (match->cp_prev)
2683 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002685 compl_first_match = match;
2686 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002688 /*
2689 * Find the longest common string if still doing that.
2690 */
2691 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2692 ins_compl_longest_match(match);
2693
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 return OK;
2695}
2696
2697/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002698 * Return TRUE if "str[len]" matches with match->cp_str, considering
2699 * match->cp_icase.
2700 */
2701 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002702ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002703{
2704 if (match->cp_icase)
2705 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2706 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2707}
2708
2709/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002710 * Reduce the longest common string for match "match".
2711 */
2712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002713ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002714{
2715 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002716 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002717 int had_match;
2718
2719 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002720 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002721 /* First match, use it as a whole. */
2722 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002723 if (compl_leader != NULL)
2724 {
2725 had_match = (curwin->w_cursor.col > compl_col);
2726 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002727 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002728 ins_redraw(FALSE);
2729
2730 /* When the match isn't there (to avoid matching itself) remove it
2731 * again after redrawing. */
2732 if (!had_match)
2733 ins_compl_delete();
2734 compl_used_match = FALSE;
2735 }
2736 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002737 else
2738 {
2739 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002740 p = compl_leader;
2741 s = match->cp_str;
2742 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002743 {
2744#ifdef FEAT_MBYTE
2745 if (has_mbyte)
2746 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002747 c1 = mb_ptr2char(p);
2748 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002749 }
2750 else
2751#endif
2752 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002753 c1 = *p;
2754 c2 = *s;
2755 }
2756 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2757 : (c1 != c2))
2758 break;
2759#ifdef FEAT_MBYTE
2760 if (has_mbyte)
2761 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002762 MB_PTR_ADV(p);
2763 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002764 }
2765 else
2766#endif
2767 {
2768 ++p;
2769 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002770 }
2771 }
2772
2773 if (*p != NUL)
2774 {
2775 /* Leader was shortened, need to change the inserted text. */
2776 *p = NUL;
2777 had_match = (curwin->w_cursor.col > compl_col);
2778 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002779 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002780 ins_redraw(FALSE);
2781
2782 /* When the match isn't there (to avoid matching itself) remove it
2783 * again after redrawing. */
2784 if (!had_match)
2785 ins_compl_delete();
2786 }
2787
2788 compl_used_match = FALSE;
2789 }
2790}
2791
2792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 * Add an array of matches to the list of matches.
2794 * Frees matches[].
2795 */
2796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002797ins_compl_add_matches(
2798 int num_matches,
2799 char_u **matches,
2800 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
2802 int i;
2803 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002804 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
Bram Moolenaar572cb562005-08-05 21:35:02 +00002806 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002807 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002808 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002810 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 FreeWild(num_matches, matches);
2812}
2813
2814/* Make the completion list cyclic.
2815 * Return the number of matches (excluding the original).
2816 */
2817 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002818ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002820 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 int count = 0;
2822
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002823 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
2825 /*
2826 * Find the end of the list.
2827 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002828 match = compl_first_match;
2829 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002830 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002832 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 ++count;
2834 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002835 match->cp_next = compl_first_match;
2836 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
2838 return count;
2839}
2840
Bram Moolenaara94bc432006-03-10 21:42:59 +00002841/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002842 * Set variables that store noselect and noinsert behavior from the
2843 * 'completeopt' value.
2844 */
2845 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002846completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002847{
2848 compl_no_insert = FALSE;
2849 compl_no_select = FALSE;
2850 if (strstr((char *)p_cot, "noselect") != NULL)
2851 compl_no_select = TRUE;
2852 if (strstr((char *)p_cot, "noinsert") != NULL)
2853 compl_no_insert = TRUE;
2854}
2855
2856/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002857 * Start completion for the complete() function.
2858 * "startcol" is where the matched text starts (1 is first column).
2859 * "list" is the list of matches.
2860 */
2861 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002862set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002863{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002864 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002865 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002866
Bram Moolenaara94bc432006-03-10 21:42:59 +00002867 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002868 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002869 ins_compl_prep(' ');
2870 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002871 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002872
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002873 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002874 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002875 startcol = curwin->w_cursor.col;
2876 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002877 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002878 /* compl_pattern doesn't need to be set */
2879 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2880 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002881 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002882 return;
2883
Bram Moolenaare4214502015-03-05 18:08:43 +01002884 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002885
2886 ins_compl_add_list(list);
2887 compl_matches = ins_compl_make_cyclic();
2888 compl_started = TRUE;
2889 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002890 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002891
2892 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002893 if (compl_no_insert || compl_no_select)
2894 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002895 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002896 if (compl_no_select)
2897 /* Down/Up has no real effect. */
2898 ins_complete(K_UP, FALSE);
2899 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002900 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002901 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002902 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002903
2904 /* Lazily show the popup menu, unless we got interrupted. */
2905 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002906 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002907 out_flush();
2908}
2909
2910
Bram Moolenaar9372a112005-12-06 19:59:18 +00002911/* "compl_match_array" points the currently displayed list of entries in the
2912 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002913static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002914static int compl_match_arraysize;
2915
2916/*
2917 * Update the screen and when there is any scrolling remove the popup menu.
2918 */
2919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002920ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002921{
2922 int h;
2923
2924 if (compl_match_array != NULL)
2925 {
2926 h = curwin->w_cline_height;
2927 update_screen(0);
2928 if (h != curwin->w_cline_height)
2929 ins_compl_del_pum();
2930 }
2931}
2932
2933/*
2934 * Remove any popup menu.
2935 */
2936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002937ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002938{
2939 if (compl_match_array != NULL)
2940 {
2941 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01002942 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002943 }
2944}
2945
2946/*
2947 * Return TRUE if the popup menu should be displayed.
2948 */
2949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002950pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002951{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002952 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002953 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002954 return FALSE;
2955
2956 /* The display looks bad on a B&W display. */
2957 if (t_colors < 8
2958#ifdef FEAT_GUI
2959 && !gui.in_use
2960#endif
2961 )
2962 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002963 return TRUE;
2964}
2965
2966/*
2967 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002968 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002969 */
2970 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002971pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002972{
2973 compl_T *compl;
2974 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002975
2976 /* Don't display the popup menu if there are no matches or there is only
2977 * one (ignoring the original text). */
2978 compl = compl_first_match;
2979 i = 0;
2980 do
2981 {
2982 if (compl == NULL
2983 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2984 break;
2985 compl = compl->cp_next;
2986 } while (compl != compl_first_match);
2987
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002988 if (strstr((char *)p_cot, "menuone") != NULL)
2989 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002990 return (i >= 2);
2991}
2992
2993/*
2994 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002995 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002996 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002998ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002999{
3000 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003001 compl_T *shown_compl = NULL;
3002 int did_find_shown_match = FALSE;
3003 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003004 int i;
3005 int cur = -1;
3006 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003007 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003008
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003009 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003010 return;
3011
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003012#if defined(FEAT_EVAL)
3013 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3014 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3015#endif
3016
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003017 /* Update the screen before drawing the popup menu over it. */
3018 update_screen(0);
3019
3020 if (compl_match_array == NULL)
3021 {
3022 /* Need to build the popup menu list. */
3023 compl_match_arraysize = 0;
3024 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003025 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003026 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003027 do
3028 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003029 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3030 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003031 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003032 ++compl_match_arraysize;
3033 compl = compl->cp_next;
3034 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003035 if (compl_match_arraysize == 0)
3036 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003037 compl_match_array = (pumitem_T *)alloc_clear(
3038 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003039 * compl_match_arraysize));
3040 if (compl_match_array != NULL)
3041 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003042 /* If the current match is the original text don't find the first
3043 * match after it, don't highlight anything. */
3044 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3045 shown_match_ok = TRUE;
3046
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003047 i = 0;
3048 compl = compl_first_match;
3049 do
3050 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003051 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3052 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003053 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003054 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003055 if (!shown_match_ok)
3056 {
3057 if (compl == compl_shown_match || did_find_shown_match)
3058 {
3059 /* This item is the shown match or this is the
3060 * first displayed item after the shown match. */
3061 compl_shown_match = compl;
3062 did_find_shown_match = TRUE;
3063 shown_match_ok = TRUE;
3064 }
3065 else
3066 /* Remember this displayed match for when the
3067 * shown match is just below it. */
3068 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003069 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003070 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003071
3072 if (compl->cp_text[CPT_ABBR] != NULL)
3073 compl_match_array[i].pum_text =
3074 compl->cp_text[CPT_ABBR];
3075 else
3076 compl_match_array[i].pum_text = compl->cp_str;
3077 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3078 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3079 if (compl->cp_text[CPT_MENU] != NULL)
3080 compl_match_array[i++].pum_extra =
3081 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003082 else
3083 compl_match_array[i++].pum_extra = compl->cp_fname;
3084 }
3085
3086 if (compl == compl_shown_match)
3087 {
3088 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003089
3090 /* When the original text is the shown match don't set
3091 * compl_shown_match. */
3092 if (compl->cp_flags & ORIGINAL_TEXT)
3093 shown_match_ok = TRUE;
3094
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003095 if (!shown_match_ok && shown_compl != NULL)
3096 {
3097 /* The shown match isn't displayed, set it to the
3098 * previously displayed match. */
3099 compl_shown_match = shown_compl;
3100 shown_match_ok = TRUE;
3101 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003102 }
3103 compl = compl->cp_next;
3104 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003105
3106 if (!shown_match_ok) /* no displayed match at all */
3107 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003108 }
3109 }
3110 else
3111 {
3112 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003113 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003114 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3115 || compl_match_array[i].pum_text
3116 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003117 {
3118 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003119 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003120 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003121 }
3122
3123 if (compl_match_array != NULL)
3124 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003125 /* In Replace mode when a $ is displayed at the end of the line only
3126 * part of the screen would be updated. We do need to redraw here. */
3127 dollar_vcol = -1;
3128
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003129 /* Compute the screen column of the start of the completed text.
3130 * Use the cursor to get all wrapping and other settings right. */
3131 col = curwin->w_cursor.col;
3132 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003133 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003134 curwin->w_cursor.col = col;
3135 }
3136}
3137
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#define DICT_FIRST (1) /* use just first element in "dict" */
3139#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003140
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003142 * Add any identifiers that match the given pattern in the list of dictionary
3143 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 */
3145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003146ins_compl_dictionaries(
3147 char_u *dict_start,
3148 char_u *pat,
3149 int flags, /* DICT_FIRST and/or DICT_EXACT */
3150 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003152 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 char_u *ptr;
3154 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 char_u **files;
3157 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003159 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
Bram Moolenaar0b238792006-03-02 22:49:12 +00003161 if (*dict == NUL)
3162 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003163#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003164 /* When 'dictionary' is empty and spell checking is enabled use
3165 * "spell". */
3166 if (!thesaurus && curwin->w_p_spell)
3167 dict = (char_u *)"spell";
3168 else
3169#endif
3170 return;
3171 }
3172
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003174 if (buf == NULL)
3175 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003176 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003177
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 /* If 'infercase' is set, don't use 'smartcase' here */
3179 save_p_scs = p_scs;
3180 if (curbuf->b_p_inf)
3181 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003182
3183 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3184 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003185 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003186 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003187 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003188 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003189 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003190
3191 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003192 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003193 len = STRLEN(pat_esc) + 10;
3194 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003195 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003196 {
3197 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003198 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003199 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003200 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003201 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3202 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003203 vim_free(ptr);
3204 }
3205 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003206 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003207 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003208 if (regmatch.regprog == NULL)
3209 goto theend;
3210 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3213 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003214 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
3216 /* copy one dictionary file name into buf */
3217 if (flags == DICT_EXACT)
3218 {
3219 count = 1;
3220 files = &dict;
3221 }
3222 else
3223 {
3224 /* Expand wildcards in the dictionary name, but do not allow
3225 * backticks (for security, the 'dict' option may have been set in
3226 * a modeline). */
3227 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003228# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003229 if (!thesaurus && STRCMP(buf, "spell") == 0)
3230 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003231 else
3232# endif
3233 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 || expand_wildcards(1, &buf, &count, &files,
3235 EW_FILE|EW_SILENT) != OK)
3236 count = 0;
3237 }
3238
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003239# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003240 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003242 /* Complete from active spelling. Skip "\<" in the pattern, we
3243 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003244 if (pat[0] == '\\' && pat[1] == '<')
3245 ptr = pat + 2;
3246 else
3247 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003248 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003250 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003251# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003252 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003253 {
3254 ins_compl_files(count, files, thesaurus, flags,
3255 &regmatch, buf, &dir);
3256 if (flags != DICT_EXACT)
3257 FreeWild(count, files);
3258 }
3259 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 break;
3261 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003262
3263theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003265 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 vim_free(buf);
3267}
3268
Bram Moolenaar0b238792006-03-02 22:49:12 +00003269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003270ins_compl_files(
3271 int count,
3272 char_u **files,
3273 int thesaurus,
3274 int flags,
3275 regmatch_T *regmatch,
3276 char_u *buf,
3277 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003278{
3279 char_u *ptr;
3280 int i;
3281 FILE *fp;
3282 int add_r;
3283
3284 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3285 {
3286 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3287 if (flags != DICT_EXACT)
3288 {
3289 vim_snprintf((char *)IObuff, IOSIZE,
3290 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003291 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003292 }
3293
3294 if (fp != NULL)
3295 {
3296 /*
3297 * Read dictionary file line by line.
3298 * Check each line for a match.
3299 */
3300 while (!got_int && !compl_interrupted
3301 && !vim_fgets(buf, LSIZE, fp))
3302 {
3303 ptr = buf;
3304 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3305 {
3306 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003307 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003308 ptr = find_line_end(ptr);
3309 else
3310 ptr = find_word_end(ptr);
3311 add_r = ins_compl_add_infercase(regmatch->startp[0],
3312 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003313 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003314 if (thesaurus)
3315 {
3316 char_u *wstart;
3317
3318 /*
3319 * Add the other matches on the line
3320 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003321 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003322 while (!got_int)
3323 {
3324 /* Find start of the next word. Skip white
3325 * space and punctuation. */
3326 ptr = find_word_start(ptr);
3327 if (*ptr == NUL || *ptr == NL)
3328 break;
3329 wstart = ptr;
3330
Bram Moolenaara2993e12007-08-12 14:38:46 +00003331 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003332#ifdef FEAT_MBYTE
3333 if (has_mbyte)
3334 /* Japanese words may have characters in
3335 * different classes, only separate words
3336 * with single-byte non-word characters. */
3337 while (*ptr != NUL)
3338 {
3339 int l = (*mb_ptr2len)(ptr);
3340
3341 if (l < 2 && !vim_iswordc(*ptr))
3342 break;
3343 ptr += l;
3344 }
3345 else
3346#endif
3347 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003348
3349 /* Add the word. Skip the regexp match. */
3350 if (wstart != regmatch->startp[0])
3351 add_r = ins_compl_add_infercase(wstart,
3352 (int)(ptr - wstart),
3353 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003354 }
3355 }
3356 if (add_r == OK)
3357 /* if dir was BACKWARD then honor it just once */
3358 *dir = FORWARD;
3359 else if (add_r == FAIL)
3360 break;
3361 /* avoid expensive call to vim_regexec() when at end
3362 * of line */
3363 if (*ptr == '\n' || got_int)
3364 break;
3365 }
3366 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003367 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003368 }
3369 fclose(fp);
3370 }
3371 }
3372}
3373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374/*
3375 * Find the start of the next word.
3376 * Returns a pointer to the first char of the word. Also stops at a NUL.
3377 */
3378 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003379find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
3381#ifdef FEAT_MBYTE
3382 if (has_mbyte)
3383 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003384 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 else
3386#endif
3387 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3388 ++ptr;
3389 return ptr;
3390}
3391
3392/*
3393 * Find the end of the word. Assumes it starts inside a word.
3394 * Returns a pointer to just after the word.
3395 */
3396 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003397find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399#ifdef FEAT_MBYTE
3400 int start_class;
3401
3402 if (has_mbyte)
3403 {
3404 start_class = mb_get_class(ptr);
3405 if (start_class > 1)
3406 while (*ptr != NUL)
3407 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003408 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (mb_get_class(ptr) != start_class)
3410 break;
3411 }
3412 }
3413 else
3414#endif
3415 while (vim_iswordc(*ptr))
3416 ++ptr;
3417 return ptr;
3418}
3419
3420/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003421 * Find the end of the line, omitting CR and NL at the end.
3422 * Returns a pointer to just after the line.
3423 */
3424 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003425find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003426{
3427 char_u *s;
3428
3429 s = ptr + STRLEN(ptr);
3430 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3431 --s;
3432 return s;
3433}
3434
3435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 * Free the list of completions
3437 */
3438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003439ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003441 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003442 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaard23a8232018-02-10 18:45:26 +01003444 VIM_CLEAR(compl_pattern);
3445 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003447 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003449
3450 ins_compl_del_pum();
3451 pum_clear();
3452
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003453 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 do
3455 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003456 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003457 compl_curr_match = compl_curr_match->cp_next;
3458 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003460 if (match->cp_flags & FREE_FNAME)
3461 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003462 for (i = 0; i < CPT_COUNT; ++i)
3463 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003465 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3466 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003467 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003468 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469}
3470
3471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003472ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003474 compl_cont_status = 0;
3475 compl_started = FALSE;
3476 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003477 VIM_CLEAR(compl_pattern);
3478 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003480 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003481 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003482 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003483 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484}
3485
3486/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003487 * Return TRUE when Insert completion is active.
3488 */
3489 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003490ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003491{
3492 return compl_started;
3493}
3494
3495/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003496 * Delete one character before the cursor and show the subset of the matches
3497 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003498 * Returns the character to be used, NUL if the work is done and another char
3499 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003500 */
3501 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003502ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003503{
3504 char_u *line;
3505 char_u *p;
3506
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003507 line = ml_get_curline();
3508 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003509 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003510
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003511 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003512 * allow the word to be deleted, we won't match everything.
3513 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003514 if ((int)(p - line) - (int)compl_col < 0
3515 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003516 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3517 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3518 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003519 return K_BS;
3520
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003521 /* Deleted more than what was used to find matches or didn't finish
3522 * finding all matches: need to look for matches all over again. */
3523 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003524 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003525 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003526
Bram Moolenaara6557602006-02-04 22:43:20 +00003527 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003528 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003529 if (compl_leader != NULL)
3530 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003531 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003532 if (compl_shown_match != NULL)
3533 /* Make sure current match is not a hidden item. */
3534 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003535 return NUL;
3536 }
3537 return K_BS;
3538}
Bram Moolenaara6557602006-02-04 22:43:20 +00003539
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003540/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003541 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3542 * be called.
3543 */
3544 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003545ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003546{
3547 /* Return TRUE if we didn't complete finding matches or when the
3548 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3549 return compl_was_interrupted
3550 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3551 && compl_opt_refresh_always);
3552}
3553
3554/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003555 * Called after changing "compl_leader".
3556 * Show the popup menu with a different set of matches.
3557 * May also search for matches again if the previous search was interrupted.
3558 */
3559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003560ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003561{
3562 ins_compl_del_pum();
3563 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003564 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003565 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003566
3567 if (compl_started)
3568 ins_compl_set_original_text(compl_leader);
3569 else
3570 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003571#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003572 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003573#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003574 /*
3575 * Matches were cleared, need to search for them now. First display
3576 * the changed text before the cursor. Set "compl_restarting" to
3577 * avoid that the first match is inserted.
3578 */
3579 update_screen(0);
3580#ifdef FEAT_GUI
3581 if (gui.in_use)
3582 {
3583 /* Show the cursor after the match, not after the redrawn text. */
3584 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003585 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003586 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003587#endif
3588 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003589 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003590 compl_cont_status = 0;
3591 compl_restarting = FALSE;
3592 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003593
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003594 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003595
3596 /* Show the popup menu with a different set of matches. */
3597 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003598
3599 /* Don't let Enter select the original text when there is no popup menu. */
3600 if (compl_match_array == NULL)
3601 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003602}
3603
3604/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003605 * Return the length of the completion, from the completion start column to
3606 * the cursor column. Making sure it never goes below zero.
3607 */
3608 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003609ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003610{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003611 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003612
3613 if (off < 0)
3614 return 0;
3615 return off;
3616}
3617
3618/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003619 * Append one character to the match leader. May reduce the number of
3620 * matches.
3621 */
3622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003623ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003624{
3625#ifdef FEAT_MBYTE
3626 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003627#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003628
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003629 if (stop_arrow() == FAIL)
3630 return;
3631#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003632 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3633 {
3634 char_u buf[MB_MAXBYTES + 1];
3635
3636 (*mb_char2bytes)(c, buf);
3637 buf[cc] = NUL;
3638 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003639 if (compl_opt_refresh_always)
3640 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003641 }
3642 else
3643#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003644 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003645 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003646 if (compl_opt_refresh_always)
3647 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003648 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003649
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003650 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003651 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003652 ins_compl_restart();
3653
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003654 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003655 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003656 * break redo. */
3657 if (!compl_opt_refresh_always)
3658 {
3659 vim_free(compl_leader);
3660 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003661 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003662 if (compl_leader != NULL)
3663 ins_compl_new_leader();
3664 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003665}
3666
3667/*
3668 * Setup for finding completions again without leaving CTRL-X mode. Used when
3669 * BS or a key was typed while still searching for matches.
3670 */
3671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003672ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003673{
3674 ins_compl_free();
3675 compl_started = FALSE;
3676 compl_matches = 0;
3677 compl_cont_status = 0;
3678 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003679}
3680
3681/*
3682 * Set the first match, the original text.
3683 */
3684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003685ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003686{
3687 char_u *p;
3688
3689 /* Replace the original text entry. */
3690 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3691 {
3692 p = vim_strsave(str);
3693 if (p != NULL)
3694 {
3695 vim_free(compl_first_match->cp_str);
3696 compl_first_match->cp_str = p;
3697 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003698 }
3699}
3700
3701/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003702 * Append one character to the match leader. May reduce the number of
3703 * matches.
3704 */
3705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003706ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003707{
3708 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003709 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003710 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003711 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003712
3713 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003714 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003715 {
3716 /* When still at the original match use the first entry that matches
3717 * the leader. */
3718 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3719 {
3720 p = NULL;
3721 for (cp = compl_shown_match->cp_next; cp != NULL
3722 && cp != compl_first_match; cp = cp->cp_next)
3723 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003724 if (compl_leader == NULL
3725 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003726 (int)STRLEN(compl_leader)))
3727 {
3728 p = cp->cp_str;
3729 break;
3730 }
3731 }
3732 if (p == NULL || (int)STRLEN(p) <= len)
3733 return;
3734 }
3735 else
3736 return;
3737 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003738 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003739 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003740 ins_compl_addleader(c);
3741}
3742
3743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003745 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003746 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003748 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003749ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
3751 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003753 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
3755 /* Forget any previous 'special' messages if this is actually
3756 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3757 */
3758 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3759 edit_submode_extra = NULL;
3760
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003761 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003762 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3763 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003764 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003766 /* Set "compl_get_longest" when finding the first matches. */
3767 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003768 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003769 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003770 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003771 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003772
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003773 }
3774
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3776 {
3777 /*
3778 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3779 * it will be yet. Now we decide.
3780 */
3781 switch (c)
3782 {
3783 case Ctrl_E:
3784 case Ctrl_Y:
3785 ctrl_x_mode = CTRL_X_SCROLL;
3786 if (!(State & REPLACE_FLAG))
3787 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3788 else
3789 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3790 edit_submode_pre = NULL;
3791 showmode();
3792 break;
3793 case Ctrl_L:
3794 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3795 break;
3796 case Ctrl_F:
3797 ctrl_x_mode = CTRL_X_FILES;
3798 break;
3799 case Ctrl_K:
3800 ctrl_x_mode = CTRL_X_DICTIONARY;
3801 break;
3802 case Ctrl_R:
3803 /* Simply allow ^R to happen without affecting ^X mode */
3804 break;
3805 case Ctrl_T:
3806 ctrl_x_mode = CTRL_X_THESAURUS;
3807 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003808#ifdef FEAT_COMPL_FUNC
3809 case Ctrl_U:
3810 ctrl_x_mode = CTRL_X_FUNCTION;
3811 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003812 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003813 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003814 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003815#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003816 case 's':
3817 case Ctrl_S:
3818 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003819#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003820 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003821 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003822 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003823#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003824 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 case Ctrl_RSB:
3826 ctrl_x_mode = CTRL_X_TAGS;
3827 break;
3828#ifdef FEAT_FIND_ID
3829 case Ctrl_I:
3830 case K_S_TAB:
3831 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3832 break;
3833 case Ctrl_D:
3834 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3835 break;
3836#endif
3837 case Ctrl_V:
3838 case Ctrl_Q:
3839 ctrl_x_mode = CTRL_X_CMDLINE;
3840 break;
3841 case Ctrl_P:
3842 case Ctrl_N:
3843 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3844 * just started ^X mode, or there were enough ^X's to cancel
3845 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3846 * do normal expansion when interrupting a different mode (say
3847 * ^X^F^X^P or ^P^X^X^P, see below)
3848 * nothing changes if interrupting mode 0, (eg, the flag
3849 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003850 if (!(compl_cont_status & CONT_INTRPT))
3851 compl_cont_status |= CONT_LOCAL;
3852 else if (compl_cont_mode != 0)
3853 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 /* FALLTHROUGH */
3855 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003856 /* If we have typed at least 2 ^X's... for modes != 0, we set
3857 * compl_cont_status = 0 (eg, as if we had just started ^X
3858 * mode).
3859 * For mode 0, we set "compl_cont_mode" to an impossible
3860 * value, in both cases ^X^X can be used to restart the same
3861 * mode (avoiding ADDING mode).
3862 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3863 * 'complete' and local ^P expansions respectively.
3864 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3865 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (c == Ctrl_X)
3867 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003868 if (compl_cont_mode != 0)
3869 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003873 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 edit_submode = NULL;
3875 showmode();
3876 break;
3877 }
3878 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003879 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
3881 /* We're already in CTRL-X mode, do we stay in it? */
3882 if (!vim_is_ctrl_x_key(c))
3883 {
3884 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003885 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 else
3887 ctrl_x_mode = CTRL_X_FINISHED;
3888 edit_submode = NULL;
3889 }
3890 showmode();
3891 }
3892
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003893 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
3895 /* Show error message from attempted keyword completion (probably
3896 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003897 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003899 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3900 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 || ctrl_x_mode == CTRL_X_FINISHED)
3902 {
3903 /* Get here when we have finished typing a sequence of ^N and
3904 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003905 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003906 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
3908 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003909 * If any of the original typed text has been changed, eg when
3910 * ignorecase is set, we must add back-spaces to the redo
3911 * buffer. We add as few as necessary to delete just the part
3912 * of the original text that has changed.
3913 * When using the longest match, edited the match or used
3914 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003916 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3917 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003918 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003919 ptr = NULL;
3920 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922
3923#ifdef FEAT_CINDENT
3924 want_cindent = (can_cindent && cindent_on());
3925#endif
3926 /*
3927 * When completing whole lines: fix indent for 'cindent'.
3928 * Otherwise, break line if it's too long.
3929 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003930 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
3932#ifdef FEAT_CINDENT
3933 /* re-indent the current line */
3934 if (want_cindent)
3935 {
3936 do_c_expr_indent();
3937 want_cindent = FALSE; /* don't do it again */
3938 }
3939#endif
3940 }
3941 else
3942 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003943 int prev_col = curwin->w_cursor.col;
3944
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003946 if (prev_col > 0)
3947 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003948 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003949 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003951 if (prev_col > 0
3952 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3953 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
3955
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003956 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003957 * the selection without inserting anything. When
3958 * compl_enter_selects is set the Enter key does the same. */
3959 if ((c == Ctrl_Y || (compl_enter_selects
3960 && (c == CAR || c == K_KENTER || c == NL)))
3961 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003962 retval = TRUE;
3963
Bram Moolenaar47247282016-08-02 22:36:02 +02003964 /* CTRL-E means completion is Ended, go back to the typed text.
3965 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003966 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003967 {
3968 ins_compl_delete();
3969 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003970 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003971 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003972 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003973 retval = TRUE;
3974 }
3975
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003976 auto_format(FALSE, TRUE);
3977
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003979 compl_started = FALSE;
3980 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003981 if (!shortmess(SHM_COMPLETIONMENU))
3982 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003983 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003984 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 if (edit_submode != NULL)
3986 {
3987 edit_submode = NULL;
3988 showmode();
3989 }
3990
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003991#ifdef FEAT_CMDWIN
3992 if (c == Ctrl_C && cmdwin_type != 0)
3993 /* Avoid the popup menu remains displayed when leaving the
3994 * command line window. */
3995 update_screen(0);
3996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#ifdef FEAT_CINDENT
3998 /*
3999 * Indent now if a key was typed that is in 'cinkeys'.
4000 */
4001 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4002 do_c_expr_indent();
4003#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004004#ifdef FEAT_AUTOCMD
4005 /* Trigger the CompleteDone event to give scripts a chance to act
4006 * upon the completion. */
4007 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
4008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
4010 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004011#ifdef FEAT_AUTOCMD
4012 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4013 /* Trigger the CompleteDone event to give scripts a chance to act
4014 * upon the (possibly failed) completion. */
4015 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
4016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
4018 /* reset continue_* if we left expansion-mode, if we stay they'll be
4019 * (re)set properly in ins_complete() */
4020 if (!vim_is_ctrl_x_key(c))
4021 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004022 compl_cont_status = 0;
4023 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004025
4026 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027}
4028
4029/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004030 * Fix the redo buffer for the completion leader replacing some of the typed
4031 * text. This inserts backspaces and appends the changed text.
4032 * "ptr" is the known leader text or NUL.
4033 */
4034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004035ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004036{
4037 int len;
4038 char_u *p;
4039 char_u *ptr = ptr_arg;
4040
4041 if (ptr == NULL)
4042 {
4043 if (compl_leader != NULL)
4044 ptr = compl_leader;
4045 else
4046 return; /* nothing to do */
4047 }
4048 if (compl_orig_text != NULL)
4049 {
4050 p = compl_orig_text;
4051 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4052 ;
4053#ifdef FEAT_MBYTE
4054 if (len > 0)
4055 len -= (*mb_head_off)(p, p + len);
4056#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004057 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004058 AppendCharToRedobuff(K_BS);
4059 }
4060 else
4061 len = 0;
4062 if (ptr != NULL)
4063 AppendToRedobuffLit(ptr + len, -1);
4064}
4065
4066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4068 * (depending on flag) starting from buf and looking for a non-scanned
4069 * buffer (other than curbuf). curbuf is special, if it is called with
4070 * buf=curbuf then it has to be the first call for a given flag/expansion.
4071 *
4072 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4073 */
4074 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004075ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079 if (flag == 'w') /* just windows */
4080 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 if (buf == curbuf) /* first call for this flag/expansion */
4082 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004083 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 && wp->w_buffer->b_scanned)
4085 ;
4086 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 else
4089 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4090 * (unlisted buffers)
4091 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004092 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 && ((flag == 'U'
4094 ? buf->b_p_bl
4095 : (!buf->b_p_bl
4096 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004097 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 ;
4099 return buf;
4100}
4101
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004102#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004103/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004104 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004105 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004106 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004108expand_by_function(
4109 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4110 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004111{
Bram Moolenaar82139082011-09-14 16:52:09 +02004112 list_T *matchlist = NULL;
4113 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004114 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004115 char_u *funcname;
4116 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004117 win_T *curwin_save;
4118 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004119 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004120
Bram Moolenaare344bea2005-09-01 20:46:49 +00004121 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4122 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004123 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004124
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004125 /* Call 'completefunc' to obtain the list of matches. */
4126 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004127 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004128
Bram Moolenaare344bea2005-09-01 20:46:49 +00004129 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004130 curwin_save = curwin;
4131 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004132
4133 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004134 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004135 {
4136 switch (rettv.v_type)
4137 {
4138 case VAR_LIST:
4139 matchlist = rettv.vval.v_list;
4140 break;
4141 case VAR_DICT:
4142 matchdict = rettv.vval.v_dict;
4143 break;
4144 default:
4145 /* TODO: Give error message? */
4146 clear_tv(&rettv);
4147 break;
4148 }
4149 }
4150
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004151 if (curwin_save != curwin || curbuf_save != curbuf)
4152 {
4153 EMSG(_(e_complwin));
4154 goto theend;
4155 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004156 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004157 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004158 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004159 {
4160 EMSG(_(e_compldel));
4161 goto theend;
4162 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004163
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004164 if (matchlist != NULL)
4165 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004166 else if (matchdict != NULL)
4167 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004168
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004169theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004170 if (matchdict != NULL)
4171 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004172 if (matchlist != NULL)
4173 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004174}
4175#endif /* FEAT_COMPL_FUNC */
4176
Bram Moolenaar39f05632006-03-19 22:15:26 +00004177#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004178/*
4179 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004180 */
4181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004182ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004183{
4184 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004185 int dir = compl_direction;
4186
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004187 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004188 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004189 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004190 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4191 /* if dir was BACKWARD then honor it just once */
4192 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004193 else if (did_emsg)
4194 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004195 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004196}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004197
4198/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004199 * Add completions from a dict.
4200 */
4201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004202ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004203{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004204 dictitem_T *di_refresh;
4205 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004206
4207 /* Check for optional "refresh" item. */
4208 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004209 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4210 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004211 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004212 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004213
4214 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4215 compl_opt_refresh_always = TRUE;
4216 }
4217
4218 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004219 di_words = dict_find(dict, (char_u *)"words", 5);
4220 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4221 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004222}
4223
4224/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004225 * Add a match to the list of matches from a typeval_T.
4226 * If the given string is already in the list of completions, then return
4227 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4228 * maybe because alloc() returns NULL, then FAIL is returned.
4229 */
4230 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004231ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004232{
4233 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004234 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004235 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004236 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004237 char_u *(cptext[CPT_COUNT]);
4238
4239 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4240 {
4241 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4242 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4243 (char_u *)"abbr", FALSE);
4244 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4245 (char_u *)"menu", FALSE);
4246 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4247 (char_u *)"kind", FALSE);
4248 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4249 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004250 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4251 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004252 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4253 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004254 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004255 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004256 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4257 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004258 }
4259 else
4260 {
4261 word = get_tv_string_chk(tv);
4262 vim_memset(cptext, 0, sizeof(cptext));
4263 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004264 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004265 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004266 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004267}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004268#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004270/*
4271 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004272 * The search starts at position "ini" in curbuf and in the direction
4273 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004274 * When "compl_started" is FALSE start at that position, otherwise continue
4275 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004276 * This may return before finding all the matches.
4277 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 */
4279 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004280ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281{
4282 static pos_T first_match_pos;
4283 static pos_T last_match_pos;
4284 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004285 static int found_all = FALSE; /* Found all matches of a
4286 certain type. */
4287 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
Bram Moolenaar572cb562005-08-05 21:35:02 +00004289 pos_T *pos;
4290 char_u **matches;
4291 int save_p_scs;
4292 int save_p_ws;
4293 int save_p_ic;
4294 int i;
4295 int num_matches;
4296 int len;
4297 int found_new_match;
4298 int type = ctrl_x_mode;
4299 char_u *ptr;
4300 char_u *dict = NULL;
4301 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004302 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004304 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004306 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 ins_buf->b_scanned = 0;
4308 found_all = FALSE;
4309 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004310 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004311 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 last_match_pos = first_match_pos = *ini;
4313 }
4314
Bram Moolenaar4475b622017-05-01 20:46:52 +02004315 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004316 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4318 for (;;)
4319 {
4320 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004321 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 * or if found_all says this entry is done. For ^X^L only use the
4325 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004326 if ((ctrl_x_mode == CTRL_X_NORMAL
4327 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004328 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330 found_all = FALSE;
4331 while (*e_cpt == ',' || *e_cpt == ' ')
4332 e_cpt++;
4333 if (*e_cpt == '.' && !curbuf->b_scanned)
4334 {
4335 ins_buf = curbuf;
4336 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004337 /* Move the cursor back one character so that ^N can match the
4338 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004339 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004340 {
4341 /* Move the cursor to after the last character in the
4342 * buffer, so that word at start of buffer is found
4343 * correctly. */
4344 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4345 first_match_pos.col =
4346 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 last_match_pos = first_match_pos;
4349 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004350
4351 /* Remember the first match so that the loop stops when we
4352 * wrap and come back there a second time. */
4353 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4356 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4357 {
4358 /* Scan a buffer, but not the current one. */
4359 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4360 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004361 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 first_match_pos.col = last_match_pos.col = 0;
4363 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4364 last_match_pos.lnum = 0;
4365 type = 0;
4366 }
4367 else /* unloaded buffer, scan like dictionary */
4368 {
4369 found_all = TRUE;
4370 if (ins_buf->b_fname == NULL)
4371 continue;
4372 type = CTRL_X_DICTIONARY;
4373 dict = ins_buf->b_fname;
4374 dict_f = DICT_EXACT;
4375 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004376 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 ins_buf->b_fname == NULL
4378 ? buf_spname(ins_buf)
4379 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004380 ? ins_buf->b_fname
4381 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004382 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384 else if (*e_cpt == NUL)
4385 break;
4386 else
4387 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004388 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 type = -1;
4390 else if (*e_cpt == 'k' || *e_cpt == 's')
4391 {
4392 if (*e_cpt == 'k')
4393 type = CTRL_X_DICTIONARY;
4394 else
4395 type = CTRL_X_THESAURUS;
4396 if (*++e_cpt != ',' && *e_cpt != NUL)
4397 {
4398 dict = e_cpt;
4399 dict_f = DICT_FIRST;
4400 }
4401 }
4402#ifdef FEAT_FIND_ID
4403 else if (*e_cpt == 'i')
4404 type = CTRL_X_PATH_PATTERNS;
4405 else if (*e_cpt == 'd')
4406 type = CTRL_X_PATH_DEFINES;
4407#endif
4408 else if (*e_cpt == ']' || *e_cpt == 't')
4409 {
4410 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004411 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004412 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 else
4415 type = -1;
4416
4417 /* in any case e_cpt is advanced to the next entry */
4418 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4419
4420 found_all = TRUE;
4421 if (type == -1)
4422 continue;
4423 }
4424 }
4425
Bram Moolenaar4475b622017-05-01 20:46:52 +02004426 /* If complete() was called then compl_pattern has been reset. The
4427 * following won't work then, bail out. */
4428 if (compl_pattern == NULL)
4429 break;
4430
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 switch (type)
4432 {
4433 case -1:
4434 break;
4435#ifdef FEAT_FIND_ID
4436 case CTRL_X_PATH_PATTERNS:
4437 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004438 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004439 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004441 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4443 (linenr_T)1, (linenr_T)MAXLNUM);
4444 break;
4445#endif
4446
4447 case CTRL_X_DICTIONARY:
4448 case CTRL_X_THESAURUS:
4449 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004450 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 : (type == CTRL_X_THESAURUS
4452 ? (*curbuf->b_p_tsr == NUL
4453 ? p_tsr
4454 : curbuf->b_p_tsr)
4455 : (*curbuf->b_p_dict == NUL
4456 ? p_dict
4457 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004458 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004459 dict != NULL ? dict_f
4460 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 dict = NULL;
4462 break;
4463
4464 case CTRL_X_TAGS:
4465 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4466 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004467 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004469 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004470 * of matches is found when compl_pattern is empty */
4471 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004472 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4473 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4475 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004476 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 p_ic = save_p_ic;
4479 break;
4480
4481 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004482 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4484 {
4485
4486 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004487 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004488 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490 break;
4491
4492 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004493 if (expand_cmdline(&compl_xp, compl_pattern,
4494 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004496 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 break;
4498
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004499#ifdef FEAT_COMPL_FUNC
4500 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004501 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004502 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004503 break;
4504#endif
4505
Bram Moolenaar488c6512005-08-11 20:09:58 +00004506 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004507#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004508 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004509 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004510 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004511 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004512#endif
4513 break;
4514
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 default: /* normal ^P/^N and ^X^L */
4516 /*
4517 * If 'infercase' is set, don't use 'smartcase' here
4518 */
4519 save_p_scs = p_scs;
4520 if (ins_buf->b_p_inf)
4521 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004522
Bram Moolenaar361aa502014-01-23 22:45:58 +01004523 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 * end but never from the middle, thus setting nowrapscan in this
4525 * buffers is a good idea, on the other hand, we always set
4526 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4527 save_p_ws = p_ws;
4528 if (ins_buf != curbuf)
4529 p_ws = FALSE;
4530 else if (*e_cpt == '.')
4531 p_ws = TRUE;
4532 for (;;)
4533 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004534 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004536 ++msg_silent; /* Don't want messages for wrapscan. */
4537
Bram Moolenaare4214502015-03-05 18:08:43 +01004538 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4539 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004540 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004541 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004542 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004544 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004546 found_new_match = searchit(NULL, ins_buf, pos,
4547 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004548 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004549 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004550 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004551 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004553 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 first_match_pos = *pos;
4556 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004557 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
4559 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004560 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 found_new_match = FAIL;
4562 if (found_new_match == FAIL)
4563 {
4564 if (ins_buf == curbuf)
4565 found_all = TRUE;
4566 break;
4567 }
4568
4569 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004570 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 && ini->lnum == pos->lnum
4572 && ini->col == pos->col)
4573 continue;
4574 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004575 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004577 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
4579 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4580 continue;
4581 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4582 if (!p_paste)
4583 ptr = skipwhite(ptr);
4584 }
4585 len = (int)STRLEN(ptr);
4586 }
4587 else
4588 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004589 char_u *tmp_ptr = ptr;
4590
4591 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004593 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 /* Skip if already inside a word. */
4595 if (vim_iswordp(tmp_ptr))
4596 continue;
4597 /* Find start of next word. */
4598 tmp_ptr = find_word_start(tmp_ptr);
4599 }
4600 /* Find end of this word. */
4601 tmp_ptr = find_word_end(tmp_ptr);
4602 len = (int)(tmp_ptr - ptr);
4603
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004604 if ((compl_cont_status & CONT_ADDING)
4605 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
4607 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4608 {
4609 /* Try next line, if any. the new word will be
4610 * "join" as if the normal command "J" was used.
4611 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 * works -- Acevedo */
4614 STRNCPY(IObuff, ptr, len);
4615 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4616 tmp_ptr = ptr = skipwhite(ptr);
4617 /* Find start of next word. */
4618 tmp_ptr = find_word_start(tmp_ptr);
4619 /* Find end of next word. */
4620 tmp_ptr = find_word_end(tmp_ptr);
4621 if (tmp_ptr > ptr)
4622 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004623 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004625 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 IObuff[len++] = ' ';
4627 /* IObuf =~ "\k.* ", thus len >= 2 */
4628 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004629 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 || (vim_strchr(p_cpo, CPO_JOINSP)
4631 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004632 && (IObuff[len - 2] == '?'
4633 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 IObuff[len++] = ' ';
4635 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004636 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (tmp_ptr - ptr >= IOSIZE - len)
4638 tmp_ptr = ptr + IOSIZE - len - 1;
4639 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4640 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004641 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 }
4643 IObuff[len] = NUL;
4644 ptr = IObuff;
4645 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004646 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 continue;
4648 }
4649 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004650 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004651 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004652 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
4654 found_new_match = OK;
4655 break;
4656 }
4657 }
4658 p_scs = save_p_scs;
4659 p_ws = save_p_ws;
4660 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004661
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004662 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004663 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004664 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 found_new_match = OK;
4666
4667 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004668 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4669 * match */
4670 if ((ctrl_x_mode != CTRL_X_NORMAL
4671 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004672 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004673 {
4674 if (got_int)
4675 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004676 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004677 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004678 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004679
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004680 if ((ctrl_x_mode != CTRL_X_NORMAL
4681 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004682 || compl_interrupted)
4683 break;
4684 compl_started = TRUE;
4685 }
4686 else
4687 {
4688 /* Mark a buffer scanned when it has been scanned completely */
4689 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4690 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004692 compl_started = FALSE;
4693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004695 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004697 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 && *e_cpt == NUL) /* Got to end of 'complete' */
4699 found_new_match = FAIL;
4700
4701 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004702 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4703 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 i = ins_compl_make_cyclic();
4705
Bram Moolenaar4475b622017-05-01 20:46:52 +02004706 if (compl_old_match != NULL)
4707 {
4708 /* If several matches were added (FORWARD) or the search failed and has
4709 * just been made cyclic then we have to move compl_curr_match to the
4710 * next or previous entry (if any) -- Acevedo */
4711 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4712 : compl_old_match->cp_prev;
4713 if (compl_curr_match == NULL)
4714 compl_curr_match = compl_old_match;
4715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 return i;
4717}
4718
4719/* Delete the old text being completed. */
4720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004721ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004723 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724
4725 /*
4726 * In insert mode: Delete the typed part.
4727 * In replace mode: Put the old characters back, if any.
4728 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004729 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4730 if ((int)curwin->w_cursor.col > col)
4731 {
4732 if (stop_arrow() == FAIL)
4733 return;
4734 backspace_until_column(col);
4735 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004736
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004737 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4738 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004740 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004741 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742}
4743
Bram Moolenaar472e8592016-10-15 17:06:47 +02004744/*
4745 * Insert the new text being completed.
4746 * "in_compl_func" is TRUE when called from complete_check().
4747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004749ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004751 dict_T *dict;
4752
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004753 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004754 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4755 compl_used_match = FALSE;
4756 else
4757 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004758
4759 /* Set completed item. */
4760 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004761 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004762 if (dict != NULL)
4763 {
4764 dict_add_nr_str(dict, "word", 0L,
4765 EMPTY_IF_NULL(compl_shown_match->cp_str));
4766 dict_add_nr_str(dict, "abbr", 0L,
4767 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4768 dict_add_nr_str(dict, "menu", 0L,
4769 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4770 dict_add_nr_str(dict, "kind", 0L,
4771 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4772 dict_add_nr_str(dict, "info", 0L,
4773 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004774 dict_add_nr_str(dict, "user_data", 0L,
4775 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
Bram Moolenaar42a45122015-07-10 17:56:23 +02004776 }
4777 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004778 if (!in_compl_func)
4779 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780}
4781
4782/*
4783 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004784 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4785 * get more completions. If it is FALSE, then we just do nothing when there
4786 * are no more completions in a given direction. The latter case is used when
4787 * we are still in the middle of finding completions, to allow browsing
4788 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 * Return the total number of matches, or -1 if still unknown -- webb.
4790 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004791 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4792 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 *
4794 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004795 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4796 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 */
4798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004799ins_compl_next(
4800 int allow_get_expansion,
4801 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004802 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004803 int insert_match, /* Insert the newly selected match */
4804 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805{
4806 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004807 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004808 compl_T *found_compl = NULL;
4809 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004810 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004811 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004813 /* When user complete function return -1 for findstart which is next
4814 * time of 'always', compl_shown_match become NULL. */
4815 if (compl_shown_match == NULL)
4816 return -1;
4817
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004818 if (compl_leader != NULL
4819 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004821 /* Set "compl_shown_match" to the actually shown match, it may differ
4822 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004823 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004824 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004825 && compl_shown_match->cp_next != NULL
4826 && compl_shown_match->cp_next != compl_first_match)
4827 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004828
4829 /* If we didn't find it searching forward, and compl_shows_dir is
4830 * backward, find the last match. */
4831 if (compl_shows_dir == BACKWARD
4832 && !ins_compl_equal(compl_shown_match,
4833 compl_leader, (int)STRLEN(compl_leader))
4834 && (compl_shown_match->cp_next == NULL
4835 || compl_shown_match->cp_next == compl_first_match))
4836 {
4837 while (!ins_compl_equal(compl_shown_match,
4838 compl_leader, (int)STRLEN(compl_leader))
4839 && compl_shown_match->cp_prev != NULL
4840 && compl_shown_match->cp_prev != compl_first_match)
4841 compl_shown_match = compl_shown_match->cp_prev;
4842 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004843 }
4844
4845 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004846 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 /* Delete old text to be replaced */
4848 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004849
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004850 /* When finding the longest common text we stick at the original text,
4851 * don't let CTRL-N or CTRL-P move to the first match. */
4852 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4853
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004854 /* When restarting the search don't insert the first match either. */
4855 if (compl_restarting)
4856 {
4857 advance = FALSE;
4858 compl_restarting = FALSE;
4859 }
4860
Bram Moolenaare3226be2005-12-18 22:10:00 +00004861 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4862 * around. */
4863 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004865 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004867 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004868 found_end = (compl_first_match != NULL
4869 && (compl_shown_match->cp_next == compl_first_match
4870 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004871 }
4872 else if (compl_shows_dir == BACKWARD
4873 && compl_shown_match->cp_prev != NULL)
4874 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004875 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004876 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004877 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004880 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004881 if (!allow_get_expansion)
4882 {
4883 if (advance)
4884 {
4885 if (compl_shows_dir == BACKWARD)
4886 compl_pending -= todo + 1;
4887 else
4888 compl_pending += todo + 1;
4889 }
4890 return -1;
4891 }
4892
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004893 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004894 {
4895 if (compl_shows_dir == BACKWARD)
4896 --compl_pending;
4897 else
4898 ++compl_pending;
4899 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004900
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004901 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004902 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004903
4904 /* handle any pending completions */
4905 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004906 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004907 {
4908 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4909 {
4910 compl_shown_match = compl_shown_match->cp_next;
4911 --compl_pending;
4912 }
4913 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4914 {
4915 compl_shown_match = compl_shown_match->cp_prev;
4916 ++compl_pending;
4917 }
4918 else
4919 break;
4920 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004921 found_end = FALSE;
4922 }
4923 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4924 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004925 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004926 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004927 ++todo;
4928 else
4929 /* Remember a matching item. */
4930 found_compl = compl_shown_match;
4931
4932 /* Stop at the end of the list when we found a usable match. */
4933 if (found_end)
4934 {
4935 if (found_compl != NULL)
4936 {
4937 compl_shown_match = found_compl;
4938 break;
4939 }
4940 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004944 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004945 if (compl_no_insert && !started)
4946 {
4947 ins_bytes(compl_orig_text + ins_compl_len());
4948 compl_used_match = FALSE;
4949 }
4950 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004951 {
4952 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004953 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004954 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004955 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004956 }
4957 else
4958 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
4960 if (!allow_get_expansion)
4961 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004962 /* may undisplay the popup menu first */
4963 ins_compl_upd_pum();
4964
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004965 /* redraw to show the user what was inserted */
4966 update_screen(0);
4967
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004968 /* display the updated popup menu */
4969 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004970#ifdef FEAT_GUI
4971 if (gui.in_use)
4972 {
4973 /* Show the cursor after the match, not after the redrawn text. */
4974 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004975 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00004976 }
4977#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004978
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 /* Delete old text to be replaced, since we're still searching and
4980 * don't want to match ourselves! */
4981 ins_compl_delete();
4982 }
4983
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004984 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004985 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004986 if (compl_no_insert && !started)
4987 compl_enter_selects = TRUE;
4988 else
4989 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004990
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 /*
4992 * Show the file name for the match (if any)
4993 * Truncate the file name to avoid a wait for return.
4994 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004995 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02004997 char *lead = _("match in file");
4998 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4999 char_u *s;
5000 char_u *e;
5001
5002 if (space > 0)
5003 {
5004 /* We need the tail that fits. With double-byte encoding going
5005 * back from the end is very slow, thus go from the start and keep
5006 * the text that fits in "space" between "s" and "e". */
5007 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5008 {
5009 space -= ptr2cells(e);
5010 while (space < 0)
5011 {
5012 space += ptr2cells(s);
5013 MB_PTR_ADV(s);
5014 }
5015 }
5016 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5017 s > compl_shown_match->cp_fname ? "<" : "", s);
5018 msg(IObuff);
5019 redraw_cmdline = FALSE; /* don't overwrite! */
5020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022
5023 return num_matches;
5024}
5025
5026/*
5027 * Call this while finding completions, to check whether the user has hit a key
5028 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005029 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005031 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005032 * "in_compl_func" is TRUE when called from complete_check(), don't set
5033 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 */
5035 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005036ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037{
5038 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005039 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005041 /* Don't check when reading keys from a script, :normal or feedkeys().
5042 * That would break the test scripts. But do check for keys when called
5043 * from complete_check(). */
5044 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 return;
5046
5047 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005048 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 return;
5050 count = 0;
5051
Bram Moolenaara260a972006-06-23 19:36:29 +00005052 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5053 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (c != NUL)
5056 {
5057 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5058 {
5059 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005060 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005061 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005062 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005064 else
5065 {
5066 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005067 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005068 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005069 if (c != K_IGNORE)
5070 {
5071 /* Don't interrupt completion when the character wasn't typed,
5072 * e.g., when doing @q to replay keys. */
5073 if (c != Ctrl_R && KeyTyped)
5074 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005075
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005076 vungetc(c);
5077 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005080 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005081 {
5082 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5083
5084 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005085 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005086 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005087}
5088
5089/*
5090 * Decide the direction of Insert mode complete from the key typed.
5091 * Returns BACKWARD or FORWARD.
5092 */
5093 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005094ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005095{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005096 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005097 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005098 return BACKWARD;
5099 return FORWARD;
5100}
5101
5102/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005103 * Return TRUE for keys that are used for completion only when the popup menu
5104 * is visible.
5105 */
5106 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005107ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005108{
5109 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005110 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5111 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005112}
5113
5114/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005115 * Decide the number of completions to move forward.
5116 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5117 */
5118 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005119ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005120{
5121 int h;
5122
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005123 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005124 {
5125 h = pum_get_height();
5126 if (h > 3)
5127 h -= 2; /* keep some context */
5128 return h;
5129 }
5130 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131}
5132
5133/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005134 * Return TRUE if completion with "c" should insert the match, FALSE if only
5135 * to change the currently selected completion.
5136 */
5137 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005138ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005139{
5140 switch (c)
5141 {
5142 case K_UP:
5143 case K_DOWN:
5144 case K_PAGEDOWN:
5145 case K_KPAGEDOWN:
5146 case K_S_DOWN:
5147 case K_PAGEUP:
5148 case K_KPAGEUP:
5149 case K_S_UP:
5150 return FALSE;
5151 }
5152 return TRUE;
5153}
5154
5155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 * Do Insert mode completion.
5157 * Called when character "c" was typed, which has a meaning for completion.
5158 * Returns OK if completion was done, FAIL if something failed (out of mem).
5159 */
5160 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005161ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005163 char_u *line;
5164 int startcol = 0; /* column where searched text starts */
5165 colnr_T curs_col; /* cursor column */
5166 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005167 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005168 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005169 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005170 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171
Bram Moolenaare3226be2005-12-18 22:10:00 +00005172 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005173 insert_match = ins_compl_use_match(c);
5174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005175 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
5177 /* First time we hit ^N or ^P (in a row, I mean) */
5178
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 did_ai = FALSE;
5180#ifdef FEAT_SMARTINDENT
5181 did_si = FALSE;
5182 can_si = FALSE;
5183 can_si_back = FALSE;
5184#endif
5185 if (stop_arrow() == FAIL)
5186 return FAIL;
5187
5188 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005189 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005190 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005192 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005193 * "compl_startpos" to the cursor as a pattern to add a new word
5194 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005195 * "compl_startpos" is not in the same line as the cursor then fix it
5196 * (the line has been split because it was longer than 'tw'). if SOL
5197 * is set then skip the previous pattern, a word at the beginning of
5198 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005199 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5200 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
5202 /*
5203 * it is a continued search
5204 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005205 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005206 if (ctrl_x_mode == CTRL_X_NORMAL
5207 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5208 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005210 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005212 /* line (probably) wrapped, set compl_startpos to the
5213 * first non_blank in the line, if it is not a wordchar
5214 * include it to get a better pattern, but then we don't
5215 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005216 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005217 compl_startpos.col = compl_col;
5218 compl_startpos.lnum = curwin->w_cursor.lnum;
5219 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
5221 else
5222 {
5223 /* S_IPOS was set when we inserted a word that was at the
5224 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005225 * mode but first we need to redefine compl_startpos */
5226 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 compl_cont_status |= CONT_SOL;
5229 compl_startpos.col = (colnr_T)(skipwhite(
5230 line + compl_length
5231 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005233 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005235 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005236 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005237 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005239 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005241 compl_cont_status &= ~CONT_SOL;
5242 compl_length = (IOSIZE - MIN_SPACE);
5243 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005245 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5246 if (compl_length < 1)
5247 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005249 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005250 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005252 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 }
5254 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005255 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005259 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005260 if (ctrl_x_mode != CTRL_X_NORMAL)
5261 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005262 compl_cont_status = 0;
5263 compl_cont_status |= CONT_N_ADDS;
5264 compl_startpos = curwin->w_cursor;
5265 startcol = (int)curs_col;
5266 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 }
5268
5269 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005270 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005272 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5274 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005275 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005277 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005279 compl_col += ++startcol;
5280 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005283 compl_pattern = str_foldcase(line + compl_col,
5284 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005286 compl_pattern = vim_strnsave(line + compl_col,
5287 compl_length);
5288 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 return FAIL;
5290 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
5293 char_u *prefix = (char_u *)"\\<";
5294
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005295 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005296 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005297 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005298 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005300 if (!vim_iswordp(line + compl_col)
5301 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 && (
5303#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005304 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005306 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307#endif
5308 )))
5309 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005310 STRCPY((char *)compl_pattern, prefix);
5311 (void)quote_meta(compl_pattern + STRLEN(prefix),
5312 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005314 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005316 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005318 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319#endif
5320 )
5321 {
5322 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005323 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5324 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005326 compl_col += curs_col;
5327 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329 else
5330 {
5331#ifdef FEAT_MBYTE
5332 /* Search the point of change class of multibyte character
5333 * or not a word single byte character backward. */
5334 if (has_mbyte)
5335 {
5336 int base_class;
5337 int head_off;
5338
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005339 startcol -= (*mb_head_off)(line, line + startcol);
5340 base_class = mb_get_class(line + startcol);
5341 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005343 head_off = (*mb_head_off)(line, line + startcol);
5344 if (base_class != mb_get_class(line + startcol
5345 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005347 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
5349 }
5350 else
5351#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005352 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005354 compl_col += ++startcol;
5355 compl_length = (int)curs_col - startcol;
5356 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {
5358 /* Only match word with at least two chars -- webb
5359 * there's no need to call quote_meta,
5360 * alloc(7) is enough -- Acevedo
5361 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 compl_pattern = alloc(7);
5363 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 STRCPY((char *)compl_pattern, "\\<");
5366 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5367 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369 else
5370 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005371 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005372 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005373 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 STRCPY((char *)compl_pattern, "\\<");
5376 (void)quote_meta(compl_pattern + 2, line + compl_col,
5377 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 }
5379 }
5380 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005381 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005383 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005384 compl_length = (int)curs_col - (int)compl_col;
5385 if (compl_length < 0) /* cursor in indent: empty pattern */
5386 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005388 compl_pattern = str_foldcase(line + compl_col, compl_length,
5389 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005391 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5392 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 return FAIL;
5394 }
5395 else if (ctrl_x_mode == CTRL_X_FILES)
5396 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005397 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005398 if (startcol > 0)
5399 {
5400 char_u *p = line + startcol;
5401
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005402 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005403 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005404 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005405 if (p == line && vim_isfilec(PTR2CHAR(p)))
5406 startcol = 0;
5407 else
5408 startcol = (int)(p - line) + 1;
5409 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005410
Bram Moolenaar0300e462013-09-08 16:03:45 +02005411 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005412 compl_length = (int)curs_col - startcol;
5413 compl_pattern = addstar(line + compl_col, compl_length,
5414 EXPAND_FILES);
5415 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 return FAIL;
5417 }
5418 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5419 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005420 compl_pattern = vim_strnsave(line, curs_col);
5421 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005423 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005424 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005425 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5426 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005427 /* No completion possible, use an empty pattern to get a
5428 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005429 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005430 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005431 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5432 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005434 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005435 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005436#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005437 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005438 * Call user defined function 'completefunc' with "a:findstart"
5439 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005440 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005441 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005442 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005443 char_u *funcname;
5444 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005445 win_T *curwin_save;
5446 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005447
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005448 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005449 * string */
5450 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5451 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5452 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005453 {
5454 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5455 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005456 /* restore did_ai, so that adding comment leader works */
5457 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005458 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005459 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005460
5461 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005462 args[1] = NULL;
5463 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005464 curwin_save = curwin;
5465 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005466 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005467 if (curwin_save != curwin || curbuf_save != curbuf)
5468 {
5469 EMSG(_(e_complwin));
5470 return FAIL;
5471 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005472 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005473 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005474 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005475 {
5476 EMSG(_(e_compldel));
5477 return FAIL;
5478 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005479
Bram Moolenaar6110a002012-01-26 18:58:38 +01005480 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005481 * cancel the complete without an error.
5482 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005483 if (col == -2)
5484 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005485 if (col == -3)
5486 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005487 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005488 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005489 if (!shortmess(SHM_COMPLETIONMENU))
5490 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005491 return FAIL;
5492 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005493
Bram Moolenaar82139082011-09-14 16:52:09 +02005494 /*
5495 * Reset extended parameters of completion, when start new
5496 * completion.
5497 */
5498 compl_opt_refresh_always = FALSE;
5499
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005500 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005501 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005502 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005503 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005504 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005505
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005506 /* Setup variables for completion. Need to obtain "line" again,
5507 * it may have become invalid. */
5508 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005509 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005510 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5511 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005512#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005513 return FAIL;
5514 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005515 else if (ctrl_x_mode == CTRL_X_SPELL)
5516 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005517#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005518 if (spell_bad_len > 0)
5519 compl_col = curs_col - spell_bad_len;
5520 else
5521 compl_col = spell_word_start(startcol);
5522 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005523 {
5524 compl_length = 0;
5525 compl_col = curs_col;
5526 }
5527 else
5528 {
5529 spell_expand_check_cap(compl_col);
5530 compl_length = (int)curs_col - compl_col;
5531 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005532 /* Need to obtain "line" again, it may have become invalid. */
5533 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005534 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5535 if (compl_pattern == NULL)
5536#endif
5537 return FAIL;
5538 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005539 else
5540 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005541 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005542 return FAIL;
5543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005545 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 {
5547 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005548 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
5550 /* Insert a new line, keep indentation but ignore 'comments' */
5551#ifdef FEAT_COMMENTS
5552 char_u *old = curbuf->b_p_com;
5553
5554 curbuf->b_p_com = (char_u *)"";
5555#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005556 compl_startpos.lnum = curwin->w_cursor.lnum;
5557 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 ins_eol('\r');
5559#ifdef FEAT_COMMENTS
5560 curbuf->b_p_com = old;
5561#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005562 compl_length = 0;
5563 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 }
5565 }
5566 else
5567 {
5568 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005569 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005572 if (compl_cont_status & CONT_LOCAL)
5573 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 else
5575 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5576
Bram Moolenaar62951b12011-09-21 18:23:05 +02005577 /* If any of the original typed text has been changed we need to fix
5578 * the redo buffer. */
5579 ins_compl_fixRedoBufForLeader(NULL);
5580
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005581 /* Always add completion for the original text. */
5582 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005583 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5584 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005585 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005587 VIM_CLEAR(compl_pattern);
5588 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 return FAIL;
5590 }
5591
5592 /* showmode might reset the internal line pointers, so it must
5593 * be called before line = ml_get(), or when this address is no
5594 * longer needed. -- Acevedo.
5595 */
5596 edit_submode_extra = (char_u *)_("-- Searching...");
5597 edit_submode_highl = HLF_COUNT;
5598 showmode();
5599 edit_submode_extra = NULL;
5600 out_flush();
5601 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005602 else if (insert_match && stop_arrow() == FAIL)
5603 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005605 compl_shown_match = compl_curr_match;
5606 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607
5608 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005609 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005611 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005612 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005613 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005615 /* may undisplay the popup menu */
5616 ins_compl_upd_pum();
5617
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005618 if (n > 1) /* all matches have been found */
5619 compl_matches = n;
5620 compl_curr_match = compl_shown_match;
5621 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
Bram Moolenaard68071d2006-05-02 22:08:30 +00005623 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5624 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 if (got_int && !global_busy)
5626 {
5627 (void)vgetc();
5628 got_int = FALSE;
5629 }
5630
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005631 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005632 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005634 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5635 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5637 edit_submode_highl = HLF_E;
5638 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5639 * because we couldn't expand anything at first place, but if we used
5640 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5641 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005642 if ( compl_length > 1
5643 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005644 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5646 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005647 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
5649
Bram Moolenaar572cb562005-08-05 21:35:02 +00005650 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005651 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005653 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
5655 if (edit_submode_extra == NULL)
5656 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005657 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 {
5659 edit_submode_extra = (char_u *)_("Back at original");
5660 edit_submode_highl = HLF_W;
5661 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005662 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 {
5664 edit_submode_extra = (char_u *)_("Word from other line");
5665 edit_submode_highl = HLF_COUNT;
5666 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005667 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 edit_submode_extra = (char_u *)_("The only match");
5670 edit_submode_highl = HLF_COUNT;
5671 }
5672 else
5673 {
5674 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005675 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005677 int number = 0;
5678 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005680 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 {
5682 /* search backwards for the first valid (!= -1) number.
5683 * This should normally succeed already at the first loop
5684 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005685 for (match = compl_curr_match->cp_prev; match != NULL
5686 && match != compl_first_match;
5687 match = match->cp_prev)
5688 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005690 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 break;
5692 }
5693 if (match != NULL)
5694 /* go up and assign all numbers which are not assigned
5695 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005696 for (match = match->cp_next;
5697 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005698 match = match->cp_next)
5699 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
5701 else /* BACKWARD */
5702 {
5703 /* search forwards (upwards) for the first valid (!= -1)
5704 * number. This should normally succeed already at the
5705 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005706 for (match = compl_curr_match->cp_next; match != NULL
5707 && match != compl_first_match;
5708 match = match->cp_next)
5709 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005711 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 break;
5713 }
5714 if (match != NULL)
5715 /* go down and assign all numbers which are not
5716 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005717 for (match = match->cp_prev; match
5718 && match->cp_number == -1;
5719 match = match->cp_prev)
5720 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 }
5723
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005724 /* The match should always have a sequence number now, this is
5725 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005726 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005728 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5729 * Translations may need more than twice that. */
5730 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005732 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005733 vim_snprintf((char *)match_ref, sizeof(match_ref),
5734 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005735 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005737 vim_snprintf((char *)match_ref, sizeof(match_ref),
5738 _("match %d"),
5739 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 edit_submode_extra = match_ref;
5741 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005742 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 curs_columns(FALSE);
5744 }
5745 }
5746 }
5747
5748 /* Show a message about what (completion) mode we're in. */
5749 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005750 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005752 if (edit_submode_extra != NULL)
5753 {
5754 if (!p_smd)
5755 msg_attr(edit_submode_extra,
5756 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005757 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005758 }
5759 else
5760 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
Bram Moolenaard68071d2006-05-02 22:08:30 +00005763 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005764 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005765 show_pum(save_w_wrow, save_w_leftcol);
5766
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005767 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005768 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005769
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 return OK;
5771}
5772
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005773 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005774show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005775{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005776 /* RedrawingDisabled may be set when invoked through complete(). */
5777 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005778
Bram Moolenaarcb036422017-03-01 12:29:10 +01005779 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005780
Bram Moolenaarcb036422017-03-01 12:29:10 +01005781 /* If the cursor moved or the display scrolled we need to remove the pum
5782 * first. */
5783 setcursor();
5784 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5785 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005786
Bram Moolenaarcb036422017-03-01 12:29:10 +01005787 ins_compl_show_pum();
5788 setcursor();
5789 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005790}
5791
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792/*
5793 * Looks in the first "len" chars. of "src" for search-metachars.
5794 * If dest is not NULL the chars. are copied there quoting (with
5795 * a backslash) the metachars, and dest would be NUL terminated.
5796 * Returns the length (needed) of dest
5797 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005798 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005799quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005801 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005803 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {
5805 switch (*src)
5806 {
5807 case '.':
5808 case '*':
5809 case '[':
5810 if (ctrl_x_mode == CTRL_X_DICTIONARY
5811 || ctrl_x_mode == CTRL_X_THESAURUS)
5812 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005813 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 case '~':
5815 if (!p_magic) /* quote these only if magic is set */
5816 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005817 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 case '\\':
5819 if (ctrl_x_mode == CTRL_X_DICTIONARY
5820 || ctrl_x_mode == CTRL_X_THESAURUS)
5821 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005822 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 case '^': /* currently it's not needed. */
5824 case '$':
5825 m++;
5826 if (dest != NULL)
5827 *dest++ = '\\';
5828 break;
5829 }
5830 if (dest != NULL)
5831 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005832# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 /* Copy remaining bytes of a multibyte character. */
5834 if (has_mbyte)
5835 {
5836 int i, mb_len;
5837
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005838 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 if (mb_len > 0 && len >= mb_len)
5840 for (i = 0; i < mb_len; ++i)
5841 {
5842 --len;
5843 ++src;
5844 if (dest != NULL)
5845 *dest++ = *src;
5846 }
5847 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 }
5850 if (dest != NULL)
5851 *dest = NUL;
5852
5853 return m;
5854}
5855#endif /* FEAT_INS_EXPAND */
5856
5857/*
5858 * Next character is interpreted literally.
5859 * A one, two or three digit decimal number is interpreted as its byte value.
5860 * If one or two digits are entered, the next character is given to vungetc().
5861 * For Unicode a character > 255 may be returned.
5862 */
5863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005864get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 int cc;
5867 int nc;
5868 int i;
5869 int hex = FALSE;
5870 int octal = FALSE;
5871#ifdef FEAT_MBYTE
5872 int unicode = 0;
5873#endif
5874
5875 if (got_int)
5876 return Ctrl_C;
5877
5878#ifdef FEAT_GUI
5879 /*
5880 * In GUI there is no point inserting the internal code for a special key.
5881 * It is more useful to insert the string "<KEY>" instead. This would
5882 * probably be useful in a text window too, but it would not be
5883 * vi-compatible (maybe there should be an option for it?) -- webb
5884 */
5885 if (gui.in_use)
5886 ++allow_keys;
5887#endif
5888#ifdef USE_ON_FLY_SCROLL
5889 dont_scroll = TRUE; /* disallow scrolling here */
5890#endif
5891 ++no_mapping; /* don't map the next key hits */
5892 cc = 0;
5893 i = 0;
5894 for (;;)
5895 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005896 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897#ifdef FEAT_CMDL_INFO
5898 if (!(State & CMDLINE)
5899# ifdef FEAT_MBYTE
5900 && MB_BYTE2LEN_CHECK(nc) == 1
5901# endif
5902 )
5903 add_to_showcmd(nc);
5904#endif
5905 if (nc == 'x' || nc == 'X')
5906 hex = TRUE;
5907 else if (nc == 'o' || nc == 'O')
5908 octal = TRUE;
5909#ifdef FEAT_MBYTE
5910 else if (nc == 'u' || nc == 'U')
5911 unicode = nc;
5912#endif
5913 else
5914 {
5915 if (hex
5916#ifdef FEAT_MBYTE
5917 || unicode != 0
5918#endif
5919 )
5920 {
5921 if (!vim_isxdigit(nc))
5922 break;
5923 cc = cc * 16 + hex2nr(nc);
5924 }
5925 else if (octal)
5926 {
5927 if (nc < '0' || nc > '7')
5928 break;
5929 cc = cc * 8 + nc - '0';
5930 }
5931 else
5932 {
5933 if (!VIM_ISDIGIT(nc))
5934 break;
5935 cc = cc * 10 + nc - '0';
5936 }
5937
5938 ++i;
5939 }
5940
5941 if (cc > 255
5942#ifdef FEAT_MBYTE
5943 && unicode == 0
5944#endif
5945 )
5946 cc = 255; /* limit range to 0-255 */
5947 nc = 0;
5948
5949 if (hex) /* hex: up to two chars */
5950 {
5951 if (i >= 2)
5952 break;
5953 }
5954#ifdef FEAT_MBYTE
5955 else if (unicode) /* Unicode: up to four or eight chars */
5956 {
5957 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5958 break;
5959 }
5960#endif
5961 else if (i >= 3) /* decimal or octal: up to three chars */
5962 break;
5963 }
5964 if (i == 0) /* no number entered */
5965 {
5966 if (nc == K_ZERO) /* NUL is stored as NL */
5967 {
5968 cc = '\n';
5969 nc = 0;
5970 }
5971 else
5972 {
5973 cc = nc;
5974 nc = 0;
5975 }
5976 }
5977
5978 if (cc == 0) /* NUL is stored as NL */
5979 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005980#ifdef FEAT_MBYTE
5981 if (enc_dbcs && (cc & 0xff) == 0)
5982 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5983 second byte will cause trouble! */
5984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985
5986 --no_mapping;
5987#ifdef FEAT_GUI
5988 if (gui.in_use)
5989 --allow_keys;
5990#endif
5991 if (nc)
5992 vungetc(nc);
5993 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5994 return cc;
5995}
5996
5997/*
5998 * Insert character, taking care of special keys and mod_mask
5999 */
6000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006001insert_special(
6002 int c,
6003 int allow_modmask,
6004 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
6006 char_u *p;
6007 int len;
6008
6009 /*
6010 * Special function key, translate into "<Key>". Up to the last '>' is
6011 * inserted with ins_str(), so as not to replace characters in replace
6012 * mode.
6013 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6014 * unless 'allow_modmask' is TRUE.
6015 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006016#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 /* Command-key never produces a normal key */
6018 if (mod_mask & MOD_MASK_CMD)
6019 allow_modmask = TRUE;
6020#endif
6021 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6022 {
6023 p = get_special_key_name(c, mod_mask);
6024 len = (int)STRLEN(p);
6025 c = p[len - 1];
6026 if (len > 2)
6027 {
6028 if (stop_arrow() == FAIL)
6029 return;
6030 p[len - 1] = NUL;
6031 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006032 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 ctrlv = FALSE;
6034 }
6035 }
6036 if (stop_arrow() == OK)
6037 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6038}
6039
6040/*
6041 * Special characters in this context are those that need processing other
6042 * than the simple insertion that can be performed here. This includes ESC
6043 * which terminates the insert, and CR/NL which need special processing to
6044 * open up a new line. This routine tries to optimize insertions performed by
6045 * the "redo", "undo" or "put" commands, so it needs to know when it should
6046 * stop and defer processing to the "normal" mechanism.
6047 * '0' and '^' are special, because they can be followed by CTRL-D.
6048 */
6049#ifdef EBCDIC
6050# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6051#else
6052# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6053#endif
6054
6055#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006056# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006058# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059#endif
6060
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006061/*
6062 * "flags": INSCHAR_FORMAT - force formatting
6063 * INSCHAR_CTRLV - char typed just after CTRL-V
6064 * INSCHAR_NO_FEX - don't use 'formatexpr'
6065 *
6066 * NOTE: passes the flags value straight through to internal_format() which,
6067 * beside INSCHAR_FORMAT (above), is also looking for these:
6068 * INSCHAR_DO_COM - format comments
6069 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006072insertchar(
6073 int c, /* character to insert or NUL */
6074 int flags, /* INSCHAR_FORMAT, etc. */
6075 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 int textwidth;
6078#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006082 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006084 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
6087 /*
6088 * Try to break the line in two or more pieces when:
6089 * - Always do this if we have been called to do formatting only.
6090 * - Always do this when 'formatoptions' has the 'a' flag and the line
6091 * ends in white space.
6092 * - Otherwise:
6093 * - Don't do this if inserting a blank
6094 * - Don't do this if an existing character is being replaced, unless
6095 * we're in VREPLACE mode.
6096 * - Do this if the cursor is not on the line where insert started
6097 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6098 * before the insert.
6099 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6100 * before 'textwidth'
6101 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006102 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006103 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006104 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 && !((State & REPLACE_FLAG)
6106#ifdef FEAT_VREPLACE
6107 && !(State & VREPLACE_FLAG)
6108#endif
6109 && *ml_get_cursor() != NUL)
6110 && (curwin->w_cursor.lnum != Insstart.lnum
6111 || ((!has_format_option(FO_INS_LONG)
6112 || Insstart_textlen <= (colnr_T)textwidth)
6113 && (!fo_ins_blank
6114 || Insstart_blank_vcol <= (colnr_T)textwidth
6115 ))))))
6116 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006117 /* Format with 'formatexpr' when it's set. Use internal formatting
6118 * when 'formatexpr' isn't set or it returns non-zero. */
6119#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006120 int do_internal = TRUE;
6121 colnr_T virtcol = get_nolist_virtcol()
6122 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006123
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006124 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6125 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006126 {
6127 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6128 /* It may be required to save for undo again, e.g. when setline()
6129 * was called. */
6130 ins_need_undo = TRUE;
6131 }
6132 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006134 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006136
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 if (c == NUL) /* only formatting was wanted */
6138 return;
6139
6140#ifdef FEAT_COMMENTS
6141 /* Check whether this character should end a comment. */
6142 if (did_ai && (int)c == end_comment_pending)
6143 {
6144 char_u *line;
6145 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6146 int middle_len, end_len;
6147 int i;
6148
6149 /*
6150 * Need to remove existing (middle) comment leader and insert end
6151 * comment leader. First, check what comment leader we can find.
6152 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006153 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6155 {
6156 /* Skip middle-comment string */
6157 while (*p && p[-1] != ':') /* find end of middle flags */
6158 ++p;
6159 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6160 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006161 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 --middle_len;
6163
6164 /* Find the end-comment string */
6165 while (*p && p[-1] != ':') /* find end of end flags */
6166 ++p;
6167 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6168
6169 /* Skip white space before the cursor */
6170 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006171 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 ;
6173 i++;
6174
6175 /* Skip to before the middle leader */
6176 i -= middle_len;
6177
6178 /* Check some expected things before we go on */
6179 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6180 {
6181 /* Backspace over all the stuff we want to replace */
6182 backspace_until_column(i);
6183
6184 /*
6185 * Insert the end-comment string, except for the last
6186 * character, which will get inserted as normal later.
6187 */
6188 ins_bytes_len(lead_end, end_len - 1);
6189 }
6190 }
6191 }
6192 end_comment_pending = NUL;
6193#endif
6194
6195 did_ai = FALSE;
6196#ifdef FEAT_SMARTINDENT
6197 did_si = FALSE;
6198 can_si = FALSE;
6199 can_si_back = FALSE;
6200#endif
6201
6202 /*
6203 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6204 * This speeds up normal text input considerably.
6205 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6206 * need to re-indent at a ':', or any other character (but not what
6207 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006208 * Don't do this when there an InsertCharPre autocommand is defined,
6209 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 */
6211#ifdef USE_ON_FLY_SCROLL
6212 dont_scroll = FALSE; /* allow scrolling here */
6213#endif
6214
6215 if ( !ISSPECIAL(c)
6216#ifdef FEAT_MBYTE
6217 && (!has_mbyte || (*mb_char2len)(c) == 1)
6218#endif
6219 && vpeekc() != NUL
6220 && !(State & REPLACE_FLAG)
6221#ifdef FEAT_CINDENT
6222 && !cindent_on()
6223#endif
6224#ifdef FEAT_RIGHTLEFT
6225 && !p_ri
6226#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006227#ifdef FEAT_AUTOCMD
6228 && !has_insertcharpre()
6229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 )
6231 {
6232#define INPUT_BUFLEN 100
6233 char_u buf[INPUT_BUFLEN + 1];
6234 int i;
6235 colnr_T virtcol = 0;
6236
6237 buf[0] = c;
6238 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006239 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 virtcol = get_nolist_virtcol();
6241 /*
6242 * Stop the string when:
6243 * - no more chars available
6244 * - finding a special character (command key)
6245 * - buffer is full
6246 * - running into the 'textwidth' boundary
6247 * - need to check for abbreviation: A non-word char after a word-char
6248 */
6249 while ( (c = vpeekc()) != NUL
6250 && !ISSPECIAL(c)
6251#ifdef FEAT_MBYTE
6252 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6253#endif
6254 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006255# ifdef FEAT_FKMAP
6256 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 && (textwidth == 0
6259 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6260 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6261 {
6262#ifdef FEAT_RIGHTLEFT
6263 c = vgetc();
6264 if (p_hkmap && KeyTyped)
6265 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 buf[i++] = c;
6267#else
6268 buf[i++] = vgetc();
6269#endif
6270 }
6271
6272#ifdef FEAT_DIGRAPHS
6273 do_digraph(-1); /* clear digraphs */
6274 do_digraph(buf[i-1]); /* may be the start of a digraph */
6275#endif
6276 buf[i] = NUL;
6277 ins_str(buf);
6278 if (flags & INSCHAR_CTRLV)
6279 {
6280 redo_literal(*buf);
6281 i = 1;
6282 }
6283 else
6284 i = 0;
6285 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006286 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 }
6288 else
6289 {
6290#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006291 int cc;
6292
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6294 {
6295 char_u buf[MB_MAXBYTES + 1];
6296
6297 (*mb_char2bytes)(c, buf);
6298 buf[cc] = NUL;
6299 ins_char_bytes(buf, cc);
6300 AppendCharToRedobuff(c);
6301 }
6302 else
6303#endif
6304 {
6305 ins_char(c);
6306 if (flags & INSCHAR_CTRLV)
6307 redo_literal(c);
6308 else
6309 AppendCharToRedobuff(c);
6310 }
6311 }
6312}
6313
6314/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006315 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006316 *
6317 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6318 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006319 */
6320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006321internal_format(
6322 int textwidth,
6323 int second_indent,
6324 int flags,
6325 int format_only,
6326 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006327{
6328 int cc;
6329 int save_char = NUL;
6330 int haveto_redraw = FALSE;
6331 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6332#ifdef FEAT_MBYTE
6333 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6334#endif
6335 int fo_white_par = has_format_option(FO_WHITE_PAR);
6336 int first_line = TRUE;
6337#ifdef FEAT_COMMENTS
6338 colnr_T leader_len;
6339 int no_leader = FALSE;
6340 int do_comments = (flags & INSCHAR_DO_COM);
6341#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006342#ifdef FEAT_LINEBREAK
6343 int has_lbr = curwin->w_p_lbr;
6344
6345 /* make sure win_lbr_chartabsize() counts correctly */
6346 curwin->w_p_lbr = FALSE;
6347#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006348
6349 /*
6350 * When 'ai' is off we don't want a space under the cursor to be
6351 * deleted. Replace it with an 'x' temporarily.
6352 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006353 if (!curbuf->b_p_ai
6354#ifdef FEAT_VREPLACE
6355 && !(State & VREPLACE_FLAG)
6356#endif
6357 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006358 {
6359 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006360 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006361 {
6362 save_char = cc;
6363 pchar_cursor('x');
6364 }
6365 }
6366
6367 /*
6368 * Repeat breaking lines, until the current line is not too long.
6369 */
6370 while (!got_int)
6371 {
6372 int startcol; /* Cursor column at entry */
6373 int wantcol; /* column at textwidth border */
6374 int foundcol; /* column for start of spaces */
6375 int end_foundcol = 0; /* column for start of word */
6376 colnr_T len;
6377 colnr_T virtcol;
6378#ifdef FEAT_VREPLACE
6379 int orig_col = 0;
6380 char_u *saved_text = NULL;
6381#endif
6382 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006383 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006384
Bram Moolenaar97b98102009-11-17 16:41:01 +00006385 virtcol = get_nolist_virtcol()
6386 + char2cells(c != NUL ? c : gchar_cursor());
6387 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006388 break;
6389
6390#ifdef FEAT_COMMENTS
6391 if (no_leader)
6392 do_comments = FALSE;
6393 else if (!(flags & INSCHAR_FORMAT)
6394 && has_format_option(FO_WRAP_COMS))
6395 do_comments = TRUE;
6396
6397 /* Don't break until after the comment leader */
6398 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006399 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006400 else
6401 leader_len = 0;
6402
6403 /* If the line doesn't start with a comment leader, then don't
6404 * start one in a following broken line. Avoids that a %word
6405 * moved to the start of the next line causes all following lines
6406 * to start with %. */
6407 if (leader_len == 0)
6408 no_leader = TRUE;
6409#endif
6410 if (!(flags & INSCHAR_FORMAT)
6411#ifdef FEAT_COMMENTS
6412 && leader_len == 0
6413#endif
6414 && !has_format_option(FO_WRAP))
6415
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006416 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006417 if ((startcol = curwin->w_cursor.col) == 0)
6418 break;
6419
6420 /* find column of textwidth border */
6421 coladvance((colnr_T)textwidth);
6422 wantcol = curwin->w_cursor.col;
6423
Bram Moolenaar97b98102009-11-17 16:41:01 +00006424 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006425 foundcol = 0;
6426
6427 /*
6428 * Find position to break at.
6429 * Stop at first entered white when 'formatoptions' has 'v'
6430 */
6431 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006432 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006433 || curwin->w_cursor.lnum != Insstart.lnum
6434 || curwin->w_cursor.col >= Insstart.col)
6435 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006436 if (curwin->w_cursor.col == startcol && c != NUL)
6437 cc = c;
6438 else
6439 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440 if (WHITECHAR(cc))
6441 {
6442 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006443 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006444
6445 /* find start of sequence of blanks */
6446 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6447 {
6448 dec_cursor();
6449 cc = gchar_cursor();
6450 }
6451 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6452 break; /* only spaces in front of text */
6453#ifdef FEAT_COMMENTS
6454 /* Don't break until after the comment leader */
6455 if (curwin->w_cursor.col < leader_len)
6456 break;
6457#endif
6458 if (has_format_option(FO_ONE_LETTER))
6459 {
6460 /* do not break after one-letter words */
6461 if (curwin->w_cursor.col == 0)
6462 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006463#ifdef FEAT_COMMENTS
6464 /* do not break "#a b" when 'tw' is 2 */
6465 if (curwin->w_cursor.col <= leader_len)
6466 break;
6467#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006468 col = curwin->w_cursor.col;
6469 dec_cursor();
6470 cc = gchar_cursor();
6471
6472 if (WHITECHAR(cc))
6473 continue; /* one-letter, continue */
6474 curwin->w_cursor.col = col;
6475 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006476
6477 inc_cursor();
6478
6479 end_foundcol = end_col + 1;
6480 foundcol = curwin->w_cursor.col;
6481 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006482 break;
6483 }
6484#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006485 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006486 {
6487 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006488 if (curwin->w_cursor.col != startcol)
6489 {
6490#ifdef FEAT_COMMENTS
6491 /* Don't break until after the comment leader */
6492 if (curwin->w_cursor.col < leader_len)
6493 break;
6494#endif
6495 col = curwin->w_cursor.col;
6496 inc_cursor();
6497 /* Don't change end_foundcol if already set. */
6498 if (foundcol != curwin->w_cursor.col)
6499 {
6500 foundcol = curwin->w_cursor.col;
6501 end_foundcol = foundcol;
6502 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6503 break;
6504 }
6505 curwin->w_cursor.col = col;
6506 }
6507
6508 if (curwin->w_cursor.col == 0)
6509 break;
6510
6511 col = curwin->w_cursor.col;
6512
6513 dec_cursor();
6514 cc = gchar_cursor();
6515
6516 if (WHITECHAR(cc))
6517 continue; /* break with space */
6518#ifdef FEAT_COMMENTS
6519 /* Don't break until after the comment leader */
6520 if (curwin->w_cursor.col < leader_len)
6521 break;
6522#endif
6523
6524 curwin->w_cursor.col = col;
6525
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006526 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006527 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006528 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6529 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006530 }
6531#endif
6532 if (curwin->w_cursor.col == 0)
6533 break;
6534 dec_cursor();
6535 }
6536
6537 if (foundcol == 0) /* no spaces, cannot break line */
6538 {
6539 curwin->w_cursor.col = startcol;
6540 break;
6541 }
6542
6543 /* Going to break the line, remove any "$" now. */
6544 undisplay_dollar();
6545
6546 /*
6547 * Offset between cursor position and line break is used by replace
6548 * stack functions. VREPLACE does not use this, and backspaces
6549 * over the text instead.
6550 */
6551#ifdef FEAT_VREPLACE
6552 if (State & VREPLACE_FLAG)
6553 orig_col = startcol; /* Will start backspacing from here */
6554 else
6555#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006556 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006557
6558 /*
6559 * adjust startcol for spaces that will be deleted and
6560 * characters that will remain on top line
6561 */
6562 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006563 while ((cc = gchar_cursor(), WHITECHAR(cc))
6564 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006565 inc_cursor();
6566 startcol -= curwin->w_cursor.col;
6567 if (startcol < 0)
6568 startcol = 0;
6569
6570#ifdef FEAT_VREPLACE
6571 if (State & VREPLACE_FLAG)
6572 {
6573 /*
6574 * In VREPLACE mode, we will backspace over the text to be
6575 * wrapped, so save a copy now to put on the next line.
6576 */
6577 saved_text = vim_strsave(ml_get_cursor());
6578 curwin->w_cursor.col = orig_col;
6579 if (saved_text == NULL)
6580 break; /* Can't do it, out of memory */
6581 saved_text[startcol] = NUL;
6582
6583 /* Backspace over characters that will move to the next line */
6584 if (!fo_white_par)
6585 backspace_until_column(foundcol);
6586 }
6587 else
6588#endif
6589 {
6590 /* put cursor after pos. to break line */
6591 if (!fo_white_par)
6592 curwin->w_cursor.col = foundcol;
6593 }
6594
6595 /*
6596 * Split the line just before the margin.
6597 * Only insert/delete lines, but don't really redraw the window.
6598 */
6599 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6600 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6601#ifdef FEAT_COMMENTS
6602 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006603 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006604#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006605 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6606 if (!(flags & INSCHAR_COM_LIST))
6607 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006608
6609 replace_offset = 0;
6610 if (first_line)
6611 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006612 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006613 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006614 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006615 * This section is for auto-wrap of numeric lists. When not
6616 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6617 * flag will be set and open_line() will handle it (as seen
6618 * above). The code here (and in get_number_indent()) will
6619 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006620 */
6621 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006622 second_indent =
6623 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006624 if (second_indent >= 0)
6625 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006626#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006627 if (State & VREPLACE_FLAG)
6628 change_indent(INDENT_SET, second_indent,
6629 FALSE, NUL, TRUE);
6630 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006631#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006632#ifdef FEAT_COMMENTS
6633 if (leader_len > 0 && second_indent - leader_len > 0)
6634 {
6635 int i;
6636 int padding = second_indent - leader_len;
6637
6638 /* We started at the first_line of a numbered list
6639 * that has a comment. the open_line() function has
6640 * inserted the proper comment leader and positioned
6641 * the cursor at the end of the split line. Now we
6642 * add the additional whitespace needed after the
6643 * comment leader for the numbered list. */
6644 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006645 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006646 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006647 }
6648 else
6649 {
6650#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006651 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006652#ifdef FEAT_COMMENTS
6653 }
6654#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006655 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006656 }
6657 first_line = FALSE;
6658 }
6659
6660#ifdef FEAT_VREPLACE
6661 if (State & VREPLACE_FLAG)
6662 {
6663 /*
6664 * In VREPLACE mode we have backspaced over the text to be
6665 * moved, now we re-insert it into the new line.
6666 */
6667 ins_bytes(saved_text);
6668 vim_free(saved_text);
6669 }
6670 else
6671#endif
6672 {
6673 /*
6674 * Check if cursor is not past the NUL off the line, cindent
6675 * may have added or removed indent.
6676 */
6677 curwin->w_cursor.col += startcol;
6678 len = (colnr_T)STRLEN(ml_get_curline());
6679 if (curwin->w_cursor.col > len)
6680 curwin->w_cursor.col = len;
6681 }
6682
6683 haveto_redraw = TRUE;
6684#ifdef FEAT_CINDENT
6685 can_cindent = TRUE;
6686#endif
6687 /* moved the cursor, don't autoindent or cindent now */
6688 did_ai = FALSE;
6689#ifdef FEAT_SMARTINDENT
6690 did_si = FALSE;
6691 can_si = FALSE;
6692 can_si_back = FALSE;
6693#endif
6694 line_breakcheck();
6695 }
6696
6697 if (save_char != NUL) /* put back space after cursor */
6698 pchar_cursor(save_char);
6699
Bram Moolenaar0026d472014-09-09 16:32:39 +02006700#ifdef FEAT_LINEBREAK
6701 curwin->w_p_lbr = has_lbr;
6702#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006703 if (!format_only && haveto_redraw)
6704 {
6705 update_topline();
6706 redraw_curbuf_later(VALID);
6707 }
6708}
6709
6710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 * Called after inserting or deleting text: When 'formatoptions' includes the
6712 * 'a' flag format from the current line until the end of the paragraph.
6713 * Keep the cursor at the same position relative to the text.
6714 * The caller must have saved the cursor line for undo, following ones will be
6715 * saved here.
6716 */
6717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006718auto_format(
6719 int trailblank, /* when TRUE also format with trailing blank */
6720 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
6722 pos_T pos;
6723 colnr_T len;
6724 char_u *old;
6725 char_u *new, *pnew;
6726 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006727 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728
6729 if (!has_format_option(FO_AUTO))
6730 return;
6731
6732 pos = curwin->w_cursor;
6733 old = ml_get_curline();
6734
6735 /* may remove added space */
6736 check_auto_format(FALSE);
6737
6738 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6739 * user might insert normal text next. Also skip formatting when "1" is
6740 * in 'formatoptions' and there is a single character before the cursor.
6741 * Otherwise the line would be broken and when typing another non-white
6742 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006743 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 if (*old != NUL && !trailblank && wasatend)
6745 {
6746 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006747 cc = gchar_cursor();
6748 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6749 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006751 cc = gchar_cursor();
6752 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {
6754 curwin->w_cursor = pos;
6755 return;
6756 }
6757 curwin->w_cursor = pos;
6758 }
6759
6760#ifdef FEAT_COMMENTS
6761 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6762 * comments. */
6763 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006764 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 return;
6766#endif
6767
6768 /*
6769 * May start formatting in a previous line, so that after "x" a word is
6770 * moved to the previous line if it fits there now. Only when this is not
6771 * the start of a paragraph.
6772 */
6773 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6774 {
6775 --curwin->w_cursor.lnum;
6776 if (u_save_cursor() == FAIL)
6777 return;
6778 }
6779
6780 /*
6781 * Do the formatting and restore the cursor position. "saved_cursor" will
6782 * be adjusted for the text formatting.
6783 */
6784 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006785 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 curwin->w_cursor = saved_cursor;
6787 saved_cursor.lnum = 0;
6788
6789 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6790 {
6791 /* "cannot happen" */
6792 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6793 coladvance((colnr_T)MAXCOL);
6794 }
6795 else
6796 check_cursor_col();
6797
6798 /* Insert mode: If the cursor is now after the end of the line while it
6799 * previously wasn't, the line was broken. Because of the rule above we
6800 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6801 * formatted. */
6802 if (!wasatend && has_format_option(FO_WHITE_PAR))
6803 {
6804 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006805 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (curwin->w_cursor.col == len)
6807 {
6808 pnew = vim_strnsave(new, len + 2);
6809 pnew[len] = ' ';
6810 pnew[len + 1] = NUL;
6811 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6812 /* remove the space later */
6813 did_add_space = TRUE;
6814 }
6815 else
6816 /* may remove added space */
6817 check_auto_format(FALSE);
6818 }
6819
6820 check_cursor();
6821}
6822
6823/*
6824 * When an extra space was added to continue a paragraph for auto-formatting,
6825 * delete it now. The space must be under the cursor, just after the insert
6826 * position.
6827 */
6828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006829check_auto_format(
6830 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831{
6832 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006833 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834
6835 if (did_add_space)
6836 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006837 cc = gchar_cursor();
6838 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 /* Somehow the space was removed already. */
6840 did_add_space = FALSE;
6841 else
6842 {
6843 if (!end_insert)
6844 {
6845 inc_cursor();
6846 c = gchar_cursor();
6847 dec_cursor();
6848 }
6849 if (c != NUL)
6850 {
6851 /* The space is no longer at the end of the line, delete it. */
6852 del_char(FALSE);
6853 did_add_space = FALSE;
6854 }
6855 }
6856 }
6857}
6858
6859/*
6860 * Find out textwidth to be used for formatting:
6861 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006862 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 * if invalid value, use 0.
6864 * Set default to window width (maximum 79) for "gq" operator.
6865 */
6866 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006867comp_textwidth(
6868 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 int textwidth;
6871
6872 textwidth = curbuf->b_p_tw;
6873 if (textwidth == 0 && curbuf->b_p_wm)
6874 {
6875 /* The width is the window width minus 'wrapmargin' minus all the
6876 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006877 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878#ifdef FEAT_CMDWIN
6879 if (cmdwin_type != 0)
6880 textwidth -= 1;
6881#endif
6882#ifdef FEAT_FOLDING
6883 textwidth -= curwin->w_p_fdc;
6884#endif
6885#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006886 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 textwidth -= 1;
6888#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006889 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 textwidth -= 8;
6891 }
6892 if (textwidth < 0)
6893 textwidth = 0;
6894 if (ff && textwidth == 0)
6895 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006896 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 if (textwidth > 79)
6898 textwidth = 79;
6899 }
6900 return textwidth;
6901}
6902
6903/*
6904 * Put a character in the redo buffer, for when just after a CTRL-V.
6905 */
6906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006907redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908{
6909 char_u buf[10];
6910
6911 /* Only digits need special treatment. Translate them into a string of
6912 * three digits. */
6913 if (VIM_ISDIGIT(c))
6914 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006915 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 AppendToRedobuff(buf);
6917 }
6918 else
6919 AppendCharToRedobuff(c);
6920}
6921
6922/*
6923 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006924 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 */
6926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006927start_arrow(
6928 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006930 start_arrow_common(end_insert_pos, TRUE);
6931}
6932
6933/*
6934 * Like start_arrow() but with end_change argument.
6935 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6936 */
6937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006938start_arrow_with_change(
6939 pos_T *end_insert_pos, /* can be NULL */
6940 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006941{
6942 start_arrow_common(end_insert_pos, end_change);
6943 if (!end_change)
6944 {
6945 AppendCharToRedobuff(Ctrl_G);
6946 AppendCharToRedobuff('U');
6947 }
6948}
6949
6950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006951start_arrow_common(
6952 pos_T *end_insert_pos, /* can be NULL */
6953 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006954{
6955 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {
6957 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006958 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 arrow_used = TRUE; /* this means we stopped the current insert */
6960 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006961#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006962 check_spell_redraw();
6963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964}
6965
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006966#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006967/*
6968 * If we skipped highlighting word at cursor, do it now.
6969 * It may be skipped again, thus reset spell_redraw_lnum first.
6970 */
6971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006972check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006973{
6974 if (spell_redraw_lnum != 0)
6975 {
6976 linenr_T lnum = spell_redraw_lnum;
6977
6978 spell_redraw_lnum = 0;
6979 redrawWinline(lnum, FALSE);
6980 }
6981}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006982
6983/*
6984 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6985 * spelled word, if there is one.
6986 */
6987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006988spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006989{
6990 pos_T tpos = curwin->w_cursor;
6991
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006992 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006993 if (curwin->w_cursor.col != tpos.col)
6994 start_arrow(&tpos);
6995}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006996#endif
6997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998/*
6999 * stop_arrow() is called before a change is made in insert mode.
7000 * If an arrow key has been used, start a new insertion.
7001 * Returns FAIL if undo is impossible, shouldn't insert then.
7002 */
7003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007004stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
7006 if (arrow_used)
7007 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007008 Insstart = curwin->w_cursor; /* new insertion starts here */
7009 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7010 /* Don't update the original insert position when moved to the
7011 * right, except when nothing was inserted yet. */
7012 update_Insstart_orig = FALSE;
7013 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7014
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 if (u_save_cursor() == OK)
7016 {
7017 arrow_used = FALSE;
7018 ins_need_undo = FALSE;
7019 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007020
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 ai_col = 0;
7022#ifdef FEAT_VREPLACE
7023 if (State & VREPLACE_FLAG)
7024 {
7025 orig_line_count = curbuf->b_ml.ml_line_count;
7026 vr_lines_changed = 1;
7027 }
7028#endif
7029 ResetRedobuff();
7030 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007031 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
7033 else if (ins_need_undo)
7034 {
7035 if (u_save_cursor() == OK)
7036 ins_need_undo = FALSE;
7037 }
7038
7039#ifdef FEAT_FOLDING
7040 /* Always open fold at the cursor line when inserting something. */
7041 foldOpenCursor();
7042#endif
7043
7044 return (arrow_used || ins_need_undo ? FAIL : OK);
7045}
7046
7047/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007048 * Do a few things to stop inserting.
7049 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7050 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 */
7052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007053stop_insert(
7054 pos_T *end_insert_pos,
7055 int esc, /* called by ins_esc() */
7056 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007058 int cc;
7059 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
7061 stop_redo_ins();
7062 replace_flush(); /* abandon replace stack */
7063
7064 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007065 * Save the inserted text for later redo with ^@ and CTRL-A.
7066 * Don't do it when "restart_edit" was set and nothing was inserted,
7067 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007069 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007070 if (did_restart_edit == 0 || (ptr != NULL
7071 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007072 {
7073 vim_free(last_insert);
7074 last_insert = ptr;
7075 last_insert_skip = new_insert_skip;
7076 }
7077 else
7078 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007080 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 {
7082 /* Auto-format now. It may seem strange to do this when stopping an
7083 * insertion (or moving the cursor), but it's required when appending
7084 * a line and having it end in a space. But only do it when something
7085 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007086 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007088 pos_T tpos = curwin->w_cursor;
7089
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 /* When the cursor is at the end of the line after a space the
7091 * formatting will move it to the following word. Avoid that by
7092 * moving the cursor onto the space. */
7093 cc = 'x';
7094 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7095 {
7096 dec_cursor();
7097 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007098 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007099 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 }
7101
7102 auto_format(TRUE, FALSE);
7103
Bram Moolenaar1c465442017-03-12 20:10:05 +01007104 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007105 {
7106 if (gchar_cursor() != NUL)
7107 inc_cursor();
7108#ifdef FEAT_VIRTUALEDIT
7109 /* If the cursor is still at the same character, also keep
7110 * the "coladd". */
7111 if (gchar_cursor() == NUL
7112 && curwin->w_cursor.lnum == tpos.lnum
7113 && curwin->w_cursor.col == tpos.col)
7114 curwin->w_cursor.coladd = tpos.coladd;
7115#endif
7116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 }
7118
7119 /* If a space was inserted for auto-formatting, remove it now. */
7120 check_auto_format(TRUE);
7121
7122 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007123 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007124 * Do this when ESC was used or moving the cursor up/down.
7125 * Check for the old position still being valid, just in case the text
7126 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007127 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007128 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7129 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007131 pos_T tpos = curwin->w_cursor;
7132
7133 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007134 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007135 for (;;)
7136 {
7137 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7138 --curwin->w_cursor.col;
7139 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007140 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007141 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007142 if (del_char(TRUE) == FAIL)
7143 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007144 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007145 if (curwin->w_cursor.lnum != tpos.lnum)
7146 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007147 else
7148 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007149 /* reset tpos, could have been invalidated in the loop above */
7150 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007151 tpos.col++;
7152 if (cc != NUL && gchar_pos(&tpos) == NUL)
7153 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 /* <C-S-Right> may have started Visual mode, adjust the position for
7157 * deleted characters. */
7158 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7159 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007160 int len = (int)STRLEN(ml_get_curline());
7161
7162 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007164 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007165#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 }
7169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 }
7171 }
7172 did_ai = FALSE;
7173#ifdef FEAT_SMARTINDENT
7174 did_si = FALSE;
7175 can_si = FALSE;
7176 can_si_back = FALSE;
7177#endif
7178
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007179 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7180 * now in a different buffer. */
7181 if (end_insert_pos != NULL)
7182 {
7183 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007184 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007185 curbuf->b_op_end = *end_insert_pos;
7186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187}
7188
7189/*
7190 * Set the last inserted text to a single character.
7191 * Used for the replace command.
7192 */
7193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007194set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 char_u *s;
7197
7198 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 if (last_insert != NULL)
7201 {
7202 s = last_insert;
7203 /* Use the CTRL-V only when entering a special char */
7204 if (c < ' ' || c == DEL)
7205 *s++ = Ctrl_V;
7206 s = add_char2buf(c, s);
7207 *s++ = ESC;
7208 *s++ = NUL;
7209 last_insert_skip = 0;
7210 }
7211}
7212
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007213#if defined(EXITFREE) || defined(PROTO)
7214 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007215free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007216{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007217 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007218# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007219 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007220# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007221}
7222#endif
7223
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224/*
7225 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7226 * and CSI. Handle multi-byte characters.
7227 * Returns a pointer to after the added bytes.
7228 */
7229 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007230add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231{
7232#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007233 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 int i;
7235 int len;
7236
7237 len = (*mb_char2bytes)(c, temp);
7238 for (i = 0; i < len; ++i)
7239 {
7240 c = temp[i];
7241#endif
7242 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7243 if (c == K_SPECIAL)
7244 {
7245 *s++ = K_SPECIAL;
7246 *s++ = KS_SPECIAL;
7247 *s++ = KE_FILLER;
7248 }
7249#ifdef FEAT_GUI
7250 else if (c == CSI)
7251 {
7252 *s++ = CSI;
7253 *s++ = KS_EXTRA;
7254 *s++ = (int)KE_CSI;
7255 }
7256#endif
7257 else
7258 *s++ = c;
7259#ifdef FEAT_MBYTE
7260 }
7261#endif
7262 return s;
7263}
7264
7265/*
7266 * move cursor to start of line
7267 * if flags & BL_WHITE move to first non-white
7268 * if flags & BL_SOL move to first non-white if startofline is set,
7269 * otherwise keep "curswant" column
7270 * if flags & BL_FIX don't leave the cursor on a NUL.
7271 */
7272 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007273beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
7275 if ((flags & BL_SOL) && !p_sol)
7276 coladvance(curwin->w_curswant);
7277 else
7278 {
7279 curwin->w_cursor.col = 0;
7280#ifdef FEAT_VIRTUALEDIT
7281 curwin->w_cursor.coladd = 0;
7282#endif
7283
7284 if (flags & (BL_WHITE | BL_SOL))
7285 {
7286 char_u *ptr;
7287
Bram Moolenaar1c465442017-03-12 20:10:05 +01007288 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7290 ++curwin->w_cursor.col;
7291 }
7292 curwin->w_set_curswant = TRUE;
7293 }
7294}
7295
7296/*
7297 * oneright oneleft cursor_down cursor_up
7298 *
7299 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007300 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 * Return OK when successful, FAIL when we hit a line of file boundary.
7302 */
7303
7304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007305oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306{
7307 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309
7310#ifdef FEAT_VIRTUALEDIT
7311 if (virtual_active())
7312 {
7313 pos_T prevpos = curwin->w_cursor;
7314
7315 /* Adjust for multi-wide char (excluding TAB) */
7316 ptr = ml_get_cursor();
7317 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007318# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007320# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 ))
7324 ? ptr2cells(ptr) : 1));
7325 curwin->w_set_curswant = TRUE;
7326 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7327 return (prevpos.col != curwin->w_cursor.col
7328 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7329 }
7330#endif
7331
7332 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007333 if (*ptr == NUL)
7334 return FAIL; /* already at the very end */
7335
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007337 if (has_mbyte)
7338 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 else
7340#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007341 l = 1;
7342
7343 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7344 * contains "onemore". */
7345 if (ptr[l] == NUL
7346#ifdef FEAT_VIRTUALEDIT
7347 && (ve_flags & VE_ONEMORE) == 0
7348#endif
7349 )
7350 return FAIL;
7351 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352
7353 curwin->w_set_curswant = TRUE;
7354 return OK;
7355}
7356
7357 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007358oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359{
7360#ifdef FEAT_VIRTUALEDIT
7361 if (virtual_active())
7362 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007363# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 int v = getviscol();
7367
7368 if (v == 0)
7369 return FAIL;
7370
7371# ifdef FEAT_LINEBREAK
7372 /* We might get stuck on 'showbreak', skip over it. */
7373 width = 1;
7374 for (;;)
7375 {
7376 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007377 /* getviscol() is slow, skip it when 'showbreak' is empty,
7378 * 'breakindent' is not set and there are no multi-byte
7379 * characters */
7380 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381# ifdef FEAT_MBYTE
7382 && !has_mbyte
7383# endif
7384 ) || getviscol() < v)
7385 break;
7386 ++width;
7387 }
7388# else
7389 coladvance(v - 1);
7390# endif
7391
7392 if (curwin->w_cursor.coladd == 1)
7393 {
7394 char_u *ptr;
7395
7396 /* Adjust for multi-wide char (not a TAB) */
7397 ptr = ml_get_cursor();
7398 if (*ptr != TAB && vim_isprintc(
7399# ifdef FEAT_MBYTE
7400 (*mb_ptr2char)(ptr)
7401# else
7402 *ptr
7403# endif
7404 ) && ptr2cells(ptr) > 1)
7405 curwin->w_cursor.coladd = 0;
7406 }
7407
7408 curwin->w_set_curswant = TRUE;
7409 return OK;
7410 }
7411#endif
7412
7413 if (curwin->w_cursor.col == 0)
7414 return FAIL;
7415
7416 curwin->w_set_curswant = TRUE;
7417 --curwin->w_cursor.col;
7418
7419#ifdef FEAT_MBYTE
7420 /* if the character on the left of the current cursor is a multi-byte
7421 * character, move to its first byte */
7422 if (has_mbyte)
7423 mb_adjust_cursor();
7424#endif
7425 return OK;
7426}
7427
7428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007429cursor_up(
7430 long n,
7431 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432{
7433 linenr_T lnum;
7434
7435 if (n > 0)
7436 {
7437 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007438 /* This fails if the cursor is already in the first line or the count
7439 * is larger than the line number and '-' is in 'cpoptions' */
7440 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 return FAIL;
7442 if (n >= lnum)
7443 lnum = 1;
7444 else
7445#ifdef FEAT_FOLDING
7446 if (hasAnyFolding(curwin))
7447 {
7448 /*
7449 * Count each sequence of folded lines as one logical line.
7450 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007451 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 (void)hasFolding(lnum, &lnum, NULL);
7453
7454 while (n--)
7455 {
7456 /* move up one line */
7457 --lnum;
7458 if (lnum <= 1)
7459 break;
7460 /* If we entered a fold, move to the beginning, unless in
7461 * Insert mode or when 'foldopen' contains "all": it will open
7462 * in a moment. */
7463 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7464 (void)hasFolding(lnum, &lnum, NULL);
7465 }
7466 if (lnum < 1)
7467 lnum = 1;
7468 }
7469 else
7470#endif
7471 lnum -= n;
7472 curwin->w_cursor.lnum = lnum;
7473 }
7474
7475 /* try to advance to the column we want to be at */
7476 coladvance(curwin->w_curswant);
7477
7478 if (upd_topline)
7479 update_topline(); /* make sure curwin->w_topline is valid */
7480
7481 return OK;
7482}
7483
7484/*
7485 * Cursor down a number of logical lines.
7486 */
7487 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007488cursor_down(
7489 long n,
7490 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
7492 linenr_T lnum;
7493
7494 if (n > 0)
7495 {
7496 lnum = curwin->w_cursor.lnum;
7497#ifdef FEAT_FOLDING
7498 /* Move to last line of fold, will fail if it's the end-of-file. */
7499 (void)hasFolding(lnum, NULL, &lnum);
7500#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007501 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007502 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007503 if (lnum >= curbuf->b_ml.ml_line_count
7504 || (lnum + n > curbuf->b_ml.ml_line_count
7505 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 return FAIL;
7507 if (lnum + n >= curbuf->b_ml.ml_line_count)
7508 lnum = curbuf->b_ml.ml_line_count;
7509 else
7510#ifdef FEAT_FOLDING
7511 if (hasAnyFolding(curwin))
7512 {
7513 linenr_T last;
7514
7515 /* count each sequence of folded lines as one logical line */
7516 while (n--)
7517 {
7518 if (hasFolding(lnum, NULL, &last))
7519 lnum = last + 1;
7520 else
7521 ++lnum;
7522 if (lnum >= curbuf->b_ml.ml_line_count)
7523 break;
7524 }
7525 if (lnum > curbuf->b_ml.ml_line_count)
7526 lnum = curbuf->b_ml.ml_line_count;
7527 }
7528 else
7529#endif
7530 lnum += n;
7531 curwin->w_cursor.lnum = lnum;
7532 }
7533
7534 /* try to advance to the column we want to be at */
7535 coladvance(curwin->w_curswant);
7536
7537 if (upd_topline)
7538 update_topline(); /* make sure curwin->w_topline is valid */
7539
7540 return OK;
7541}
7542
7543/*
7544 * Stuff the last inserted text in the read buffer.
7545 * Last_insert actually is a copy of the redo buffer, so we
7546 * first have to remove the command.
7547 */
7548 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007549stuff_inserted(
7550 int c, /* Command character to be inserted */
7551 long count, /* Repeat this many times */
7552 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
7554 char_u *esc_ptr;
7555 char_u *ptr;
7556 char_u *last_ptr;
7557 char_u last = NUL;
7558
7559 ptr = get_last_insert();
7560 if (ptr == NULL)
7561 {
7562 EMSG(_(e_noinstext));
7563 return FAIL;
7564 }
7565
7566 /* may want to stuff the command character, to start Insert mode */
7567 if (c != NUL)
7568 stuffcharReadbuff(c);
7569 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7570 *esc_ptr = NUL; /* remove the ESC */
7571
7572 /* when the last char is either "0" or "^" it will be quoted if no ESC
7573 * comes after it OR if it will inserted more than once and "ptr"
7574 * starts with ^D. -- Acevedo
7575 */
7576 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7577 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7578 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7579 {
7580 last = *last_ptr;
7581 *last_ptr = NUL;
7582 }
7583
7584 do
7585 {
7586 stuffReadbuff(ptr);
7587 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7588 if (last)
7589 stuffReadbuff((char_u *)(last == '0'
7590 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7591 : IF_EB("\026^", CTRL_V_STR "^")));
7592 }
7593 while (--count > 0);
7594
7595 if (last)
7596 *last_ptr = last;
7597
7598 if (esc_ptr != NULL)
7599 *esc_ptr = ESC; /* put the ESC back */
7600
7601 /* may want to stuff a trailing ESC, to get out of Insert mode */
7602 if (!no_esc)
7603 stuffcharReadbuff(ESC);
7604
7605 return OK;
7606}
7607
7608 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007609get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610{
7611 if (last_insert == NULL)
7612 return NULL;
7613 return last_insert + last_insert_skip;
7614}
7615
7616/*
7617 * Get last inserted string, and remove trailing <Esc>.
7618 * Returns pointer to allocated memory (must be freed) or NULL.
7619 */
7620 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007621get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622{
7623 char_u *s;
7624 int len;
7625
7626 if (last_insert == NULL)
7627 return NULL;
7628 s = vim_strsave(last_insert + last_insert_skip);
7629 if (s != NULL)
7630 {
7631 len = (int)STRLEN(s);
7632 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7633 s[len - 1] = NUL;
7634 }
7635 return s;
7636}
7637
7638/*
7639 * Check the word in front of the cursor for an abbreviation.
7640 * Called when the non-id character "c" has been entered.
7641 * When an abbreviation is recognized it is removed from the text and
7642 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7643 */
7644 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007645echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646{
7647 /* Don't check for abbreviation in paste mode, when disabled and just
7648 * after moving around with cursor keys. */
7649 if (p_paste || no_abbr || arrow_used)
7650 return FALSE;
7651
7652 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7653 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7654}
7655
7656/*
7657 * replace-stack functions
7658 *
7659 * When replacing characters, the replaced characters are remembered for each
7660 * new character. This is used to re-insert the old text when backspacing.
7661 *
7662 * There is a NUL headed list of characters for each character that is
7663 * currently in the file after the insertion point. When BS is used, one NUL
7664 * headed list is put back for the deleted character.
7665 *
7666 * For a newline, there are two NUL headed lists. One contains the characters
7667 * that the NL replaced. The extra one stores the characters after the cursor
7668 * that were deleted (always white space).
7669 *
7670 * Replace_offset is normally 0, in which case replace_push will add a new
7671 * character at the end of the stack. If replace_offset is not 0, that many
7672 * characters will be left on the stack above the newly inserted character.
7673 */
7674
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007675static char_u *replace_stack = NULL;
7676static long replace_stack_nr = 0; /* next entry in replace stack */
7677static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678
7679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007680replace_push(
7681 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
7683 char_u *p;
7684
7685 if (replace_stack_nr < replace_offset) /* nothing to do */
7686 return;
7687 if (replace_stack_len <= replace_stack_nr)
7688 {
7689 replace_stack_len += 50;
7690 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7691 if (p == NULL) /* out of memory */
7692 {
7693 replace_stack_len -= 50;
7694 return;
7695 }
7696 if (replace_stack != NULL)
7697 {
7698 mch_memmove(p, replace_stack,
7699 (size_t)(replace_stack_nr * sizeof(char_u)));
7700 vim_free(replace_stack);
7701 }
7702 replace_stack = p;
7703 }
7704 p = replace_stack + replace_stack_nr - replace_offset;
7705 if (replace_offset)
7706 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7707 *p = c;
7708 ++replace_stack_nr;
7709}
7710
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007711#if defined(FEAT_MBYTE) || defined(PROTO)
7712/*
7713 * Push a character onto the replace stack. Handles a multi-byte character in
7714 * reverse byte order, so that the first byte is popped off first.
7715 * Return the number of bytes done (includes composing characters).
7716 */
7717 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007718replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007719{
7720 int l = (*mb_ptr2len)(p);
7721 int j;
7722
7723 for (j = l - 1; j >= 0; --j)
7724 replace_push(p[j]);
7725 return l;
7726}
7727#endif
7728
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729/*
7730 * Pop one item from the replace stack.
7731 * return -1 if stack empty
7732 * return replaced character or NUL otherwise
7733 */
7734 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007735replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737 if (replace_stack_nr == 0)
7738 return -1;
7739 return (int)replace_stack[--replace_stack_nr];
7740}
7741
7742/*
7743 * Join the top two items on the replace stack. This removes to "off"'th NUL
7744 * encountered.
7745 */
7746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007747replace_join(
7748 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749{
7750 int i;
7751
7752 for (i = replace_stack_nr; --i >= 0; )
7753 if (replace_stack[i] == NUL && off-- <= 0)
7754 {
7755 --replace_stack_nr;
7756 mch_memmove(replace_stack + i, replace_stack + i + 1,
7757 (size_t)(replace_stack_nr - i));
7758 return;
7759 }
7760}
7761
7762/*
7763 * Pop bytes from the replace stack until a NUL is found, and insert them
7764 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7765 */
7766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007767replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769 int cc;
7770 int oldState = State;
7771
7772 State = NORMAL; /* don't want REPLACE here */
7773 while ((cc = replace_pop()) > 0)
7774 {
7775#ifdef FEAT_MBYTE
7776 mb_replace_pop_ins(cc);
7777#else
7778 ins_char(cc);
7779#endif
7780 dec_cursor();
7781 }
7782 State = oldState;
7783}
7784
7785#ifdef FEAT_MBYTE
7786/*
7787 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7788 * indicates a multi-byte char, pop the other bytes too.
7789 */
7790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007791mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007794 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 int i;
7796 int c;
7797
7798 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7799 {
7800 buf[0] = cc;
7801 for (i = 1; i < n; ++i)
7802 buf[i] = replace_pop();
7803 ins_bytes_len(buf, n);
7804 }
7805 else
7806 ins_char(cc);
7807
7808 if (enc_utf8)
7809 /* Handle composing chars. */
7810 for (;;)
7811 {
7812 c = replace_pop();
7813 if (c == -1) /* stack empty */
7814 break;
7815 if ((n = MB_BYTE2LEN(c)) == 1)
7816 {
7817 /* Not a multi-byte char, put it back. */
7818 replace_push(c);
7819 break;
7820 }
7821 else
7822 {
7823 buf[0] = c;
7824 for (i = 1; i < n; ++i)
7825 buf[i] = replace_pop();
7826 if (utf_iscomposing(utf_ptr2char(buf)))
7827 ins_bytes_len(buf, n);
7828 else
7829 {
7830 /* Not a composing char, put it back. */
7831 for (i = n - 1; i >= 0; --i)
7832 replace_push(buf[i]);
7833 break;
7834 }
7835 }
7836 }
7837}
7838#endif
7839
7840/*
7841 * make the replace stack empty
7842 * (called when exiting replace mode)
7843 */
7844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007845replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007847 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 replace_stack_len = 0;
7849 replace_stack_nr = 0;
7850}
7851
7852/*
7853 * Handle doing a BS for one character.
7854 * cc < 0: replace stack empty, just move cursor
7855 * cc == 0: character was inserted, delete it
7856 * cc > 0: character was replaced, put cc (first byte of original char) back
7857 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007858 * When "limit_col" is >= 0, don't delete before this column. Matters when
7859 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 */
7861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007862replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
7864 int cc;
7865#ifdef FEAT_VREPLACE
7866 int orig_len = 0;
7867 int ins_len;
7868 int orig_vcols = 0;
7869 colnr_T start_vcol;
7870 char_u *p;
7871 int i;
7872 int vcol;
7873#endif
7874
7875 cc = replace_pop();
7876 if (cc > 0)
7877 {
7878#ifdef FEAT_VREPLACE
7879 if (State & VREPLACE_FLAG)
7880 {
7881 /* Get the number of screen cells used by the character we are
7882 * going to delete. */
7883 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7884 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7885 }
7886#endif
7887#ifdef FEAT_MBYTE
7888 if (has_mbyte)
7889 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007890 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891# ifdef FEAT_VREPLACE
7892 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007893 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894# endif
7895 replace_push(cc);
7896 }
7897 else
7898#endif
7899 {
7900 pchar_cursor(cc);
7901#ifdef FEAT_VREPLACE
7902 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007903 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904#endif
7905 }
7906 replace_pop_ins();
7907
7908#ifdef FEAT_VREPLACE
7909 if (State & VREPLACE_FLAG)
7910 {
7911 /* Get the number of screen cells used by the inserted characters */
7912 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007913 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 vcol = start_vcol;
7915 for (i = 0; i < ins_len; ++i)
7916 {
7917 vcol += chartabsize(p + i, vcol);
7918#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007919 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920#endif
7921 }
7922 vcol -= start_vcol;
7923
7924 /* Delete spaces that were inserted after the cursor to keep the
7925 * text aligned. */
7926 curwin->w_cursor.col += ins_len;
7927 while (vcol > orig_vcols && gchar_cursor() == ' ')
7928 {
7929 del_char(FALSE);
7930 ++orig_vcols;
7931 }
7932 curwin->w_cursor.col -= ins_len;
7933 }
7934#endif
7935
7936 /* mark the buffer as changed and prepare for displaying */
7937 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7938 }
7939 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007940 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941}
7942
7943#ifdef FEAT_CINDENT
7944/*
7945 * Return TRUE if C-indenting is on.
7946 */
7947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007948cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949{
7950 return (!p_paste && (curbuf->b_p_cin
7951# ifdef FEAT_EVAL
7952 || *curbuf->b_p_inde != NUL
7953# endif
7954 ));
7955}
7956#endif
7957
7958#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7959/*
7960 * Re-indent the current line, based on the current contents of it and the
7961 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7962 * confused what all the part that handles Control-T is doing that I'm not.
7963 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7964 */
7965
7966 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007967fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007969 int amount = get_the_indent();
7970
7971 if (amount >= 0)
7972 {
7973 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7974 if (linewhite(curwin->w_cursor.lnum))
7975 did_ai = TRUE; /* delete the indent if the line stays empty */
7976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977}
7978
7979 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007980fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981{
7982 if (p_paste)
7983 return;
7984# ifdef FEAT_LISP
7985 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7986 fixthisline(get_lisp_indent);
7987# endif
7988# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7989 else
7990# endif
7991# ifdef FEAT_CINDENT
7992 if (cindent_on())
7993 do_c_expr_indent();
7994# endif
7995}
7996
7997#endif
7998
7999#ifdef FEAT_CINDENT
8000/*
8001 * return TRUE if 'cinkeys' contains the key "keytyped",
8002 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008003 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8005 *
8006 * "keytyped" can have a few special values:
8007 * KEY_OPEN_FORW
8008 * KEY_OPEN_BACK
8009 * KEY_COMPLETE just finished completion.
8010 *
8011 * If line_is_empty is TRUE accept keys with '0' before them.
8012 */
8013 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008014in_cinkeys(
8015 int keytyped,
8016 int when,
8017 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018{
8019 char_u *look;
8020 int try_match;
8021 int try_match_word;
8022 char_u *p;
8023 char_u *line;
8024 int icase;
8025 int i;
8026
Bram Moolenaar30848942009-12-24 14:46:12 +00008027 if (keytyped == NUL)
8028 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8029 return FALSE;
8030
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031#ifdef FEAT_EVAL
8032 if (*curbuf->b_p_inde != NUL)
8033 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8034 else
8035#endif
8036 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8037 while (*look)
8038 {
8039 /*
8040 * Find out if we want to try a match with this key, depending on
8041 * 'when' and a '*' or '!' before the key.
8042 */
8043 switch (when)
8044 {
8045 case '*': try_match = (*look == '*'); break;
8046 case '!': try_match = (*look == '!'); break;
8047 default: try_match = (*look != '*'); break;
8048 }
8049 if (*look == '*' || *look == '!')
8050 ++look;
8051
8052 /*
8053 * If there is a '0', only accept a match if the line is empty.
8054 * But may still match when typing last char of a word.
8055 */
8056 if (*look == '0')
8057 {
8058 try_match_word = try_match;
8059 if (!line_is_empty)
8060 try_match = FALSE;
8061 ++look;
8062 }
8063 else
8064 try_match_word = FALSE;
8065
8066 /*
8067 * does it look like a control character?
8068 */
8069 if (*look == '^'
8070#ifdef EBCDIC
8071 && (Ctrl_chr(look[1]) != 0)
8072#else
8073 && look[1] >= '?' && look[1] <= '_'
8074#endif
8075 )
8076 {
8077 if (try_match && keytyped == Ctrl_chr(look[1]))
8078 return TRUE;
8079 look += 2;
8080 }
8081 /*
8082 * 'o' means "o" command, open forward.
8083 * 'O' means "O" command, open backward.
8084 */
8085 else if (*look == 'o')
8086 {
8087 if (try_match && keytyped == KEY_OPEN_FORW)
8088 return TRUE;
8089 ++look;
8090 }
8091 else if (*look == 'O')
8092 {
8093 if (try_match && keytyped == KEY_OPEN_BACK)
8094 return TRUE;
8095 ++look;
8096 }
8097
8098 /*
8099 * 'e' means to check for "else" at start of line and just before the
8100 * cursor.
8101 */
8102 else if (*look == 'e')
8103 {
8104 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8105 {
8106 p = ml_get_curline();
8107 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8108 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8109 return TRUE;
8110 }
8111 ++look;
8112 }
8113
8114 /*
8115 * ':' only causes an indent if it is at the end of a label or case
8116 * statement, or when it was before typing the ':' (to fix
8117 * class::method for C++).
8118 */
8119 else if (*look == ':')
8120 {
8121 if (try_match && keytyped == ':')
8122 {
8123 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008124 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008126 /* Need to get the line again after cin_islabel(). */
8127 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 if (curwin->w_cursor.col > 2
8129 && p[curwin->w_cursor.col - 1] == ':'
8130 && p[curwin->w_cursor.col - 2] == ':')
8131 {
8132 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008133 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008134 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 p = ml_get_curline();
8136 p[curwin->w_cursor.col - 1] = ':';
8137 if (i)
8138 return TRUE;
8139 }
8140 }
8141 ++look;
8142 }
8143
8144
8145 /*
8146 * Is it a key in <>, maybe?
8147 */
8148 else if (*look == '<')
8149 {
8150 if (try_match)
8151 {
8152 /*
8153 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8154 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8155 * >, *, : and ! keys if they really really want to.
8156 */
8157 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8158 && keytyped == look[1])
8159 return TRUE;
8160
8161 if (keytyped == get_special_key_code(look + 1))
8162 return TRUE;
8163 }
8164 while (*look && *look != '>')
8165 look++;
8166 while (*look == '>')
8167 look++;
8168 }
8169
8170 /*
8171 * Is it a word: "=word"?
8172 */
8173 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8174 {
8175 ++look;
8176 if (*look == '~')
8177 {
8178 icase = TRUE;
8179 ++look;
8180 }
8181 else
8182 icase = FALSE;
8183 p = vim_strchr(look, ',');
8184 if (p == NULL)
8185 p = look + STRLEN(look);
8186 if ((try_match || try_match_word)
8187 && curwin->w_cursor.col >= (colnr_T)(p - look))
8188 {
8189 int match = FALSE;
8190
8191#ifdef FEAT_INS_EXPAND
8192 if (keytyped == KEY_COMPLETE)
8193 {
8194 char_u *s;
8195
8196 /* Just completed a word, check if it starts with "look".
8197 * search back for the start of a word. */
8198 line = ml_get_curline();
8199# ifdef FEAT_MBYTE
8200 if (has_mbyte)
8201 {
8202 char_u *n;
8203
8204 for (s = line + curwin->w_cursor.col; s > line; s = n)
8205 {
8206 n = mb_prevptr(line, s);
8207 if (!vim_iswordp(n))
8208 break;
8209 }
8210 }
8211 else
8212# endif
8213 for (s = line + curwin->w_cursor.col; s > line; --s)
8214 if (!vim_iswordc(s[-1]))
8215 break;
8216 if (s + (p - look) <= line + curwin->w_cursor.col
8217 && (icase
8218 ? MB_STRNICMP(s, look, p - look)
8219 : STRNCMP(s, look, p - look)) == 0)
8220 match = TRUE;
8221 }
8222 else
8223#endif
8224 /* TODO: multi-byte */
8225 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8226 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8227 {
8228 line = ml_get_cursor();
8229 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8230 || !vim_iswordc(line[-(p - look) - 1]))
8231 && (icase
8232 ? MB_STRNICMP(line - (p - look), look, p - look)
8233 : STRNCMP(line - (p - look), look, p - look))
8234 == 0)
8235 match = TRUE;
8236 }
8237 if (match && try_match_word && !try_match)
8238 {
8239 /* "0=word": Check if there are only blanks before the
8240 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008241 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 (int)(curwin->w_cursor.col - (p - look)))
8243 match = FALSE;
8244 }
8245 if (match)
8246 return TRUE;
8247 }
8248 look = p;
8249 }
8250
8251 /*
8252 * ok, it's a boring generic character.
8253 */
8254 else
8255 {
8256 if (try_match && *look == keytyped)
8257 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008258 if (*look != NUL)
8259 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 }
8261
8262 /*
8263 * Skip over ", ".
8264 */
8265 look = skip_to_option_part(look);
8266 }
8267 return FALSE;
8268}
8269#endif /* FEAT_CINDENT */
8270
8271#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8272/*
8273 * Map Hebrew keyboard when in hkmap mode.
8274 */
8275 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008276hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277{
8278 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8279 {
8280 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8281 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8282 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8283 static char_u map[26] =
8284 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8285 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8286 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8287 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8288 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8289 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8290 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8291 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8292 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8293
8294 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8295 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8296 /* '-1'='sofit' */
8297 else if (c == 'x')
8298 return 'X';
8299 else if (c == 'q')
8300 return '\''; /* {geresh}={'} */
8301 else if (c == 246)
8302 return ' '; /* \"o --> ' ' for a german keyboard */
8303 else if (c == 228)
8304 return ' '; /* \"a --> ' ' -- / -- */
8305 else if (c == 252)
8306 return ' '; /* \"u --> ' ' -- / -- */
8307#ifdef EBCDIC
8308 else if (islower(c))
8309#else
8310 /* NOTE: islower() does not do the right thing for us on Linux so we
8311 * do this the same was as 5.7 and previous, so it works correctly on
8312 * all systems. Specifically, the e.g. Delete and Arrow keys are
8313 * munged and won't work if e.g. searching for Hebrew text.
8314 */
8315 else if (c >= 'a' && c <= 'z')
8316#endif
8317 return (int)(map[CharOrdLow(c)] + p_aleph);
8318 else
8319 return c;
8320 }
8321 else
8322 {
8323 switch (c)
8324 {
8325 case '`': return ';';
8326 case '/': return '.';
8327 case '\'': return ',';
8328 case 'q': return '/';
8329 case 'w': return '\'';
8330
8331 /* Hebrew letters - set offset from 'a' */
8332 case ',': c = '{'; break;
8333 case '.': c = 'v'; break;
8334 case ';': c = 't'; break;
8335 default: {
8336 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8337
8338#ifdef EBCDIC
8339 /* see note about islower() above */
8340 if (!islower(c))
8341#else
8342 if (c < 'a' || c > 'z')
8343#endif
8344 return c;
8345 c = str[CharOrdLow(c)];
8346 break;
8347 }
8348 }
8349
8350 return (int)(CharOrdLow(c) + p_aleph);
8351 }
8352}
8353#endif
8354
8355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008356ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357{
8358 int need_redraw = FALSE;
8359 int regname;
8360 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008361 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362
8363 /*
8364 * If we are going to wait for a character, show a '"'.
8365 */
8366 pc_status = PC_STATUS_UNSET;
8367 if (redrawing() && !char_avail())
8368 {
8369 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008370 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371
8372 edit_putchar('"', TRUE);
8373#ifdef FEAT_CMDL_INFO
8374 add_to_showcmd_c(Ctrl_R);
8375#endif
8376 }
8377
8378#ifdef USE_ON_FLY_SCROLL
8379 dont_scroll = TRUE; /* disallow scrolling here */
8380#endif
8381
8382 /*
8383 * Don't map the register name. This also prevents the mode message to be
8384 * deleted when ESC is hit.
8385 */
8386 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008387 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8390 {
8391 /* Get a third key for literal register insertion */
8392 literally = regname;
8393#ifdef FEAT_CMDL_INFO
8394 add_to_showcmd_c(literally);
8395#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008396 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 }
8399 --no_mapping;
8400
8401#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008402 /* Don't call u_sync() while typing the expression or giving an error
8403 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 ++no_u_sync;
8405 if (regname == '=')
8406 {
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008407# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008409# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008410 /* Sync undo when evaluating the expression calls setline() or
8411 * append(), so that it can be undone separately. */
8412 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008413
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 regname = get_expr_register();
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008415# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 /* Restore the Input Method. */
8417 if (im_on)
8418 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008419# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008421 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8422 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008423 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 else
8427 {
8428#endif
8429 if (literally == Ctrl_O || literally == Ctrl_P)
8430 {
8431 /* Append the command to the redo buffer. */
8432 AppendCharToRedobuff(Ctrl_R);
8433 AppendCharToRedobuff(literally);
8434 AppendCharToRedobuff(regname);
8435
8436 do_put(regname, BACKWARD, 1L,
8437 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8438 }
8439 else if (insert_reg(regname, literally) == FAIL)
8440 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008441 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 need_redraw = TRUE; /* remove the '"' */
8443 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008444 else if (stop_insert_mode)
8445 /* When the '=' register was used and a function was invoked that
8446 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8447 * insert anything, need to remove the '"' */
8448 need_redraw = TRUE;
8449
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450#ifdef FEAT_EVAL
8451 }
8452 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008453 if (u_sync_once == 1)
8454 ins_need_undo = TRUE;
8455 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456#endif
8457#ifdef FEAT_CMDL_INFO
8458 clear_showcmd();
8459#endif
8460
8461 /* If the inserted register is empty, we need to remove the '"' */
8462 if (need_redraw || stuff_empty())
8463 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008464
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008465 /* Disallow starting Visual mode here, would get a weird mode. */
8466 if (!vis_active && VIsual_active)
8467 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468}
8469
8470/*
8471 * CTRL-G commands in Insert mode.
8472 */
8473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008474ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475{
8476 int c;
8477
8478#ifdef FEAT_INS_EXPAND
8479 /* Right after CTRL-X the cursor will be after the ruler. */
8480 setcursor();
8481#endif
8482
8483 /*
8484 * Don't map the second key. This also prevents the mode message to be
8485 * deleted when ESC is hit.
8486 */
8487 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008488 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 --no_mapping;
8490 switch (c)
8491 {
8492 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8493 case K_UP:
8494 case Ctrl_K:
8495 case 'k': ins_up(TRUE);
8496 break;
8497
8498 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8499 case K_DOWN:
8500 case Ctrl_J:
8501 case 'j': ins_down(TRUE);
8502 break;
8503
8504 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008505 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008507
8508 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008509 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008510 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008511 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 break;
8513
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008514 /* CTRL-G U: do not break undo with the next char */
8515 case 'U':
8516 /* Allow one left/right cursor movement with the next char,
8517 * without breaking undo. */
8518 dont_sync_undo = MAYBE;
8519 break;
8520
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008522 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 }
8524}
8525
8526/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008527 * CTRL-^ in Insert mode.
8528 */
8529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008530ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008531{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008532 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008533 {
8534 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8535 if (State & LANGMAP)
8536 {
8537 curbuf->b_p_iminsert = B_IMODE_NONE;
8538 State &= ~LANGMAP;
8539 }
8540 else
8541 {
8542 curbuf->b_p_iminsert = B_IMODE_LMAP;
8543 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008544#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008545 im_set_active(FALSE);
8546#endif
8547 }
8548 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008549#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008550 else
8551 {
8552 /* There are no ":lmap" mappings, toggle IM */
8553 if (im_get_status())
8554 {
8555 curbuf->b_p_iminsert = B_IMODE_NONE;
8556 im_set_active(FALSE);
8557 }
8558 else
8559 {
8560 curbuf->b_p_iminsert = B_IMODE_IM;
8561 State &= ~LANGMAP;
8562 im_set_active(TRUE);
8563 }
8564 }
8565#endif
8566 set_iminsert_global();
8567 showmode();
8568#ifdef FEAT_GUI
8569 /* may show different cursor shape or color */
8570 if (gui.in_use)
8571 gui_update_cursor(TRUE, FALSE);
8572#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008573#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008574 /* Show/unshow value of 'keymap' in status lines. */
8575 status_redraw_curbuf();
8576#endif
8577}
8578
8579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 * Handle ESC in insert mode.
8581 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8582 * insert.
8583 */
8584 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008585ins_esc(
8586 long *count,
8587 int cmdchar,
8588 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590 int temp;
8591 static int disabled_redraw = FALSE;
8592
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008593#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008594 check_spell_redraw();
8595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596#if defined(FEAT_HANGULIN)
8597# if defined(ESC_CHG_TO_ENG_MODE)
8598 hangul_input_state_set(0);
8599# endif
8600 if (composing_hangul)
8601 {
8602 push_raw_key(composing_hangul_buffer, 2);
8603 composing_hangul = 0;
8604 }
8605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
8607 temp = curwin->w_cursor.col;
8608 if (disabled_redraw)
8609 {
8610 --RedrawingDisabled;
8611 disabled_redraw = FALSE;
8612 }
8613 if (!arrow_used)
8614 {
8615 /*
8616 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008617 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8618 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 */
8620 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008621 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622
8623 /*
8624 * Repeating insert may take a long time. Check for
8625 * interrupt now and then.
8626 */
8627 if (*count > 0)
8628 {
8629 line_breakcheck();
8630 if (got_int)
8631 *count = 0;
8632 }
8633
8634 if (--*count > 0) /* repeat what was typed */
8635 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008636 /* Vi repeats the insert without replacing characters. */
8637 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8638 State &= ~REPLACE_FLAG;
8639
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 (void)start_redo_ins();
8641 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008642 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 ++RedrawingDisabled;
8644 disabled_redraw = TRUE;
8645 return FALSE; /* repeat the insert */
8646 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008647 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 undisplay_dollar();
8649 }
8650
8651 /* When an autoindent was removed, curswant stays after the
8652 * indent */
8653 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8654 curwin->w_set_curswant = TRUE;
8655
8656 /* Remember the last Insert position in the '^ mark. */
8657 if (!cmdmod.keepjumps)
8658 curbuf->b_last_insert = curwin->w_cursor;
8659
8660 /*
8661 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008662 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008664 if (!nomove
8665 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666#ifdef FEAT_VIRTUALEDIT
8667 || curwin->w_cursor.coladd > 0
8668#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008669 )
8670 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008671 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672#ifdef FEAT_RIGHTLEFT
8673 && !revins_on
8674#endif
8675 )
8676 {
8677#ifdef FEAT_VIRTUALEDIT
8678 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8679 {
8680 oneleft();
8681 if (restart_edit != NUL)
8682 ++curwin->w_cursor.coladd;
8683 }
8684 else
8685#endif
8686 {
8687 --curwin->w_cursor.col;
8688#ifdef FEAT_MBYTE
8689 /* Correct cursor for multi-byte character. */
8690 if (has_mbyte)
8691 mb_adjust_cursor();
8692#endif
8693 }
8694 }
8695
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008696#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 /* Disable IM to allow typing English directly for Normal mode commands.
8698 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8699 * well). */
8700 if (!(State & LANGMAP))
8701 im_save_status(&curbuf->b_p_iminsert);
8702 im_set_active(FALSE);
8703#endif
8704
8705 State = NORMAL;
8706 /* need to position cursor again (e.g. when on a TAB ) */
8707 changed_cline_bef_curs();
8708
8709#ifdef FEAT_MOUSE
8710 setmouse();
8711#endif
8712#ifdef CURSOR_SHAPE
8713 ui_cursor_shape(); /* may show different cursor shape */
8714#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008715 if (!p_ek)
8716 /* Re-enable bracketed paste mode. */
8717 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718
8719 /*
8720 * When recording or for CTRL-O, need to display the new mode.
8721 * Otherwise remove the mode message.
8722 */
8723 if (Recording || restart_edit != NUL)
8724 showmode();
8725 else if (p_smd)
8726 MSG("");
8727
8728 return TRUE; /* exit Insert mode */
8729}
8730
8731#ifdef FEAT_RIGHTLEFT
8732/*
8733 * Toggle language: hkmap and revins_on.
8734 * Move to end of reverse inserted text.
8735 */
8736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008737ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738{
8739 if (revins_on && revins_chars && revins_scol >= 0)
8740 {
8741 while (gchar_cursor() != NUL && revins_chars--)
8742 ++curwin->w_cursor.col;
8743 }
8744 p_ri = !p_ri;
8745 revins_on = (State == INSERT && p_ri);
8746 if (revins_on)
8747 {
8748 revins_scol = curwin->w_cursor.col;
8749 revins_legal++;
8750 revins_chars = 0;
8751 undisplay_dollar();
8752 }
8753 else
8754 revins_scol = -1;
8755#ifdef FEAT_FKMAP
8756 if (p_altkeymap)
8757 {
8758 /*
8759 * to be consistent also for redo command, using '.'
8760 * set arrow_used to true and stop it - causing to redo
8761 * characters entered in one mode (normal/reverse insert).
8762 */
8763 arrow_used = TRUE;
8764 (void)stop_arrow();
8765 p_fkmap = curwin->w_p_rl ^ p_ri;
8766 if (p_fkmap && p_ri)
8767 State = INSERT;
8768 }
8769 else
8770#endif
8771 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8772 showmode();
8773}
8774#endif
8775
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776/*
8777 * If 'keymodel' contains "startsel", may start selection.
8778 * Returns TRUE when a CTRL-O and other keys stuffed.
8779 */
8780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008781ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
8783 if (km_startsel)
8784 switch (c)
8785 {
8786 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 case K_PAGEUP:
8789 case K_KPAGEUP:
8790 case K_PAGEDOWN:
8791 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008792# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 case K_LEFT:
8794 case K_RIGHT:
8795 case K_UP:
8796 case K_DOWN:
8797 case K_END:
8798 case K_HOME:
8799# endif
8800 if (!(mod_mask & MOD_MASK_SHIFT))
8801 break;
8802 /* FALLTHROUGH */
8803 case K_S_LEFT:
8804 case K_S_RIGHT:
8805 case K_S_UP:
8806 case K_S_DOWN:
8807 case K_S_END:
8808 case K_S_HOME:
8809 /* Start selection right away, the cursor can move with
8810 * CTRL-O when beyond the end of the line. */
8811 start_selection();
8812
8813 /* Execute the key in (insert) Select mode. */
8814 stuffcharReadbuff(Ctrl_O);
8815 if (mod_mask)
8816 {
8817 char_u buf[4];
8818
8819 buf[0] = K_SPECIAL;
8820 buf[1] = KS_MODIFIER;
8821 buf[2] = mod_mask;
8822 buf[3] = NUL;
8823 stuffReadbuff(buf);
8824 }
8825 stuffcharReadbuff(c);
8826 return TRUE;
8827 }
8828 return FALSE;
8829}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830
8831/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008832 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008833 */
8834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008835ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008836{
8837#ifdef FEAT_FKMAP
8838 if (p_fkmap && p_ri)
8839 {
8840 beep_flush();
8841 EMSG(farsi_text_3); /* encoded in Farsi */
8842 return;
8843 }
8844#endif
8845
8846#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008847# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008848 set_vim_var_string(VV_INSERTMODE,
8849 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008850# ifdef FEAT_VREPLACE
8851 replaceState == VREPLACE ? "v" :
8852# endif
8853 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008854# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008855 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8856#endif
8857 if (State & REPLACE_FLAG)
8858 State = INSERT | (State & LANGMAP);
8859 else
8860 State = replaceState | (State & LANGMAP);
8861 AppendCharToRedobuff(K_INS);
8862 showmode();
8863#ifdef CURSOR_SHAPE
8864 ui_cursor_shape(); /* may show different cursor shape */
8865#endif
8866}
8867
8868/*
8869 * Pressed CTRL-O in Insert mode.
8870 */
8871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008872ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008873{
8874#ifdef FEAT_VREPLACE
8875 if (State & VREPLACE_FLAG)
8876 restart_edit = 'V';
8877 else
8878#endif
8879 if (State & REPLACE_FLAG)
8880 restart_edit = 'R';
8881 else
8882 restart_edit = 'I';
8883#ifdef FEAT_VIRTUALEDIT
8884 if (virtual_active())
8885 ins_at_eol = FALSE; /* cursor always keeps its column */
8886 else
8887#endif
8888 ins_at_eol = (gchar_cursor() == NUL);
8889}
8890
8891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 * If the cursor is on an indent, ^T/^D insert/delete one
8893 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008894 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 * with vi. But vi only supports ^T and ^D after an
8896 * autoindent, we support it everywhere.
8897 */
8898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008899ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900{
8901 if (stop_arrow() == FAIL)
8902 return;
8903 AppendCharToRedobuff(c);
8904
8905 /*
8906 * 0^D and ^^D: remove all indent.
8907 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008908 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8909 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 {
8911 --curwin->w_cursor.col;
8912 (void)del_char(FALSE); /* delete the '^' or '0' */
8913 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8914 if (State & REPLACE_FLAG)
8915 replace_pop_ins();
8916 if (lastc == '^')
8917 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008918 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 }
8920 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008921 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922
8923 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8924 did_ai = FALSE;
8925#ifdef FEAT_SMARTINDENT
8926 did_si = FALSE;
8927 can_si = FALSE;
8928 can_si_back = FALSE;
8929#endif
8930#ifdef FEAT_CINDENT
8931 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8932#endif
8933}
8934
8935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008936ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937{
8938 int temp;
8939
8940 if (stop_arrow() == FAIL)
8941 return;
8942 if (gchar_cursor() == NUL) /* delete newline */
8943 {
8944 temp = curwin->w_cursor.col;
8945 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008946 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008947 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 else
8949 curwin->w_cursor.col = temp;
8950 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008951 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8952 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 did_ai = FALSE;
8954#ifdef FEAT_SMARTINDENT
8955 did_si = FALSE;
8956 can_si = FALSE;
8957 can_si_back = FALSE;
8958#endif
8959 AppendCharToRedobuff(K_DEL);
8960}
8961
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008962static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008963
8964/*
8965 * Delete one character for ins_bs().
8966 */
8967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008968ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008969{
8970 dec_cursor();
8971 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8972 if (State & REPLACE_FLAG)
8973 {
8974 /* Don't delete characters before the insert point when in
8975 * Replace mode */
8976 if (curwin->w_cursor.lnum != Insstart.lnum
8977 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008978 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008979 }
8980 else
8981 (void)del_char(FALSE);
8982}
8983
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984/*
8985 * Handle Backspace, delete-word and delete-line in Insert mode.
8986 * Return TRUE when backspace was actually used.
8987 */
8988 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008989ins_bs(
8990 int c,
8991 int mode,
8992 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
8994 linenr_T lnum;
8995 int cc;
8996 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008997 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 colnr_T mincol;
8999 int did_backspace = FALSE;
9000 int in_indent;
9001 int oldState;
9002#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009003 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004#endif
9005
9006 /*
9007 * can't delete anything in an empty file
9008 * can't backup past first character in buffer
9009 * can't backup past starting point unless 'backspace' > 1
9010 * can backup to a previous line if 'backspace' == 0
9011 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009012 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 || (
9014#ifdef FEAT_RIGHTLEFT
9015 !revins_on &&
9016#endif
9017 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9018 || (!can_bs(BS_START)
9019 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009020 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9021 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9023 && curwin->w_cursor.col <= ai_col)
9024 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9025 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009026 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 return FALSE;
9028 }
9029
9030 if (stop_arrow() == FAIL)
9031 return FALSE;
9032 in_indent = inindent(0);
9033#ifdef FEAT_CINDENT
9034 if (in_indent)
9035 can_cindent = FALSE;
9036#endif
9037#ifdef FEAT_COMMENTS
9038 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9039#endif
9040#ifdef FEAT_RIGHTLEFT
9041 if (revins_on) /* put cursor after last inserted char */
9042 inc_cursor();
9043#endif
9044
9045#ifdef FEAT_VIRTUALEDIT
9046 /* Virtualedit:
9047 * BACKSPACE_CHAR eats a virtual space
9048 * BACKSPACE_WORD eats all coladd
9049 * BACKSPACE_LINE eats all coladd and keeps going
9050 */
9051 if (curwin->w_cursor.coladd > 0)
9052 {
9053 if (mode == BACKSPACE_CHAR)
9054 {
9055 --curwin->w_cursor.coladd;
9056 return TRUE;
9057 }
9058 if (mode == BACKSPACE_WORD)
9059 {
9060 curwin->w_cursor.coladd = 0;
9061 return TRUE;
9062 }
9063 curwin->w_cursor.coladd = 0;
9064 }
9065#endif
9066
9067 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009068 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 */
9070 if (curwin->w_cursor.col == 0)
9071 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009072 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009073 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074#ifdef FEAT_RIGHTLEFT
9075 || revins_on
9076#endif
9077 )
9078 {
9079 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9080 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9081 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009082 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009083 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 }
9085 /*
9086 * In replace mode:
9087 * cc < 0: NL was inserted, delete it
9088 * cc >= 0: NL was replaced, put original characters back
9089 */
9090 cc = -1;
9091 if (State & REPLACE_FLAG)
9092 cc = replace_pop(); /* returns -1 if NL was inserted */
9093 /*
9094 * In replace mode, in the line we started replacing, we only move the
9095 * cursor.
9096 */
9097 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9098 {
9099 dec_cursor();
9100 }
9101 else
9102 {
9103#ifdef FEAT_VREPLACE
9104 if (!(State & VREPLACE_FLAG)
9105 || curwin->w_cursor.lnum > orig_line_count)
9106#endif
9107 {
9108 temp = gchar_cursor(); /* remember current char */
9109 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009110
9111 /* When "aw" is in 'formatoptions' we must delete the space at
9112 * the end of the line, otherwise the line will be broken
9113 * again when auto-formatting. */
9114 if (has_format_option(FO_AUTO)
9115 && has_format_option(FO_WHITE_PAR))
9116 {
9117 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9118 TRUE);
9119 int len;
9120
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009121 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009122 if (len > 0 && ptr[len - 1] == ' ')
9123 ptr[len - 1] = NUL;
9124 }
9125
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009126 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 if (temp == NUL && gchar_cursor() != NUL)
9128 inc_cursor();
9129 }
9130#ifdef FEAT_VREPLACE
9131 else
9132 dec_cursor();
9133#endif
9134
9135 /*
9136 * In REPLACE mode we have to put back the text that was replaced
9137 * by the NL. On the replace stack is first a NUL-terminated
9138 * sequence of characters that were deleted and then the
9139 * characters that NL replaced.
9140 */
9141 if (State & REPLACE_FLAG)
9142 {
9143 /*
9144 * Do the next ins_char() in NORMAL state, to
9145 * prevent ins_char() from replacing characters and
9146 * avoiding showmatch().
9147 */
9148 oldState = State;
9149 State = NORMAL;
9150 /*
9151 * restore characters (blanks) deleted after cursor
9152 */
9153 while (cc > 0)
9154 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009155 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156#ifdef FEAT_MBYTE
9157 mb_replace_pop_ins(cc);
9158#else
9159 ins_char(cc);
9160#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009161 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 cc = replace_pop();
9163 }
9164 /* restore the characters that NL replaced */
9165 replace_pop_ins();
9166 State = oldState;
9167 }
9168 }
9169 did_ai = FALSE;
9170 }
9171 else
9172 {
9173 /*
9174 * Delete character(s) before the cursor.
9175 */
9176#ifdef FEAT_RIGHTLEFT
9177 if (revins_on) /* put cursor on last inserted char */
9178 dec_cursor();
9179#endif
9180 mincol = 0;
9181 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009182 if (mode == BACKSPACE_LINE
9183 && (curbuf->b_p_ai
9184#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009185 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009186#endif
9187 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188#ifdef FEAT_RIGHTLEFT
9189 && !revins_on
9190#endif
9191 )
9192 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009193 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009195 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009197 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 }
9199
9200 /*
9201 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9202 */
9203 if ( mode == BACKSPACE_CHAR
9204 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009205 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009206 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 && (*(ml_get_cursor() - 1) == TAB
9208 || (*(ml_get_cursor() - 1) == ' '
9209 && (!*inserted_space_p
9210 || arrow_used))))))
9211 {
9212 int ts;
9213 colnr_T vcol;
9214 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009215 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216
9217 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009218 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009219 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009221 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 /* Compute the virtual column where we want to be. Since
9223 * 'showbreak' may get in the way, need to get the last column of
9224 * the previous character. */
9225 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009226 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 dec_cursor();
9228 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9229 inc_cursor();
9230 want_vcol = (want_vcol / ts) * ts;
9231
9232 /* delete characters until we are at or before want_vcol */
9233 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009234 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009235 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
9237 /* insert extra spaces until we are at want_vcol */
9238 while (vcol < want_vcol)
9239 {
9240 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009241 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9242 && curwin->w_cursor.col < Insstart_orig.col)
9243 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244
9245#ifdef FEAT_VREPLACE
9246 if (State & VREPLACE_FLAG)
9247 ins_char(' ');
9248 else
9249#endif
9250 {
9251 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009252 if ((State & REPLACE_FLAG))
9253 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 }
9255 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9256 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009257
9258 /* If we are now back where we started delete one character. Can
9259 * happen when using 'sts' and 'linebreak'. */
9260 if (vcol >= start_vcol)
9261 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 }
9263
9264 /*
9265 * Delete upto starting point, start of line or previous word.
9266 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009269#ifdef FEAT_MBYTE
9270 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009272 if (has_mbyte)
9273 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009275 do
9276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009278 if (!revins_on) /* put cursor on char to be deleted */
9279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009281
9282 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009284 /* look multi-byte character class */
9285 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009287 prev_cclass = cclass;
9288 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009291
9292 /* start of word? */
9293 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9294 {
9295 mode = BACKSPACE_WORD_NOT_SPACE;
9296 temp = vim_iswordc(cc);
9297 }
9298 /* end of word? */
9299 else if (mode == BACKSPACE_WORD_NOT_SPACE
9300 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9301#ifdef FEAT_MBYTE
9302 || prev_cclass != cclass
9303#endif
9304 ))
9305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009307 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009309 inc_cursor();
9310#ifdef FEAT_RIGHTLEFT
9311 else if (State & REPLACE_FLAG)
9312 dec_cursor();
9313#endif
9314 break;
9315 }
9316 if (State & REPLACE_FLAG)
9317 replace_do_bs(-1);
9318 else
9319 {
9320#ifdef FEAT_MBYTE
9321 if (enc_utf8 && p_deco)
9322 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9323#endif
9324 (void)del_char(FALSE);
9325#ifdef FEAT_MBYTE
9326 /*
9327 * If there are combining characters and 'delcombine' is set
9328 * move the cursor back. Don't back up before the base
9329 * character.
9330 */
9331 if (enc_utf8 && p_deco && cpc[0] != NUL)
9332 inc_cursor();
9333#endif
9334#ifdef FEAT_RIGHTLEFT
9335 if (revins_chars)
9336 {
9337 revins_chars--;
9338 revins_legal++;
9339 }
9340 if (revins_on && gchar_cursor() == NUL)
9341 break;
9342#endif
9343 }
9344 /* Just a single backspace?: */
9345 if (mode == BACKSPACE_CHAR)
9346 break;
9347 } while (
9348#ifdef FEAT_RIGHTLEFT
9349 revins_on ||
9350#endif
9351 (curwin->w_cursor.col > mincol
9352 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9353 || curwin->w_cursor.col != Insstart_orig.col)));
9354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 did_backspace = TRUE;
9356 }
9357#ifdef FEAT_SMARTINDENT
9358 did_si = FALSE;
9359 can_si = FALSE;
9360 can_si_back = FALSE;
9361#endif
9362 if (curwin->w_cursor.col <= 1)
9363 did_ai = FALSE;
9364 /*
9365 * It's a little strange to put backspaces into the redo
9366 * buffer, but it makes auto-indent a lot easier to deal
9367 * with.
9368 */
9369 AppendCharToRedobuff(c);
9370
9371 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009372 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9373 && curwin->w_cursor.col < Insstart_orig.col)
9374 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375
9376 /* vi behaviour: the cursor moves backward but the character that
9377 * was there remains visible
9378 * Vim behaviour: the cursor moves backward and the character that
9379 * was there is erased from the screen.
9380 * We can emulate the vi behaviour by pretending there is a dollar
9381 * displayed even when there isn't.
9382 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009383 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 dollar_vcol = curwin->w_virtcol;
9385
Bram Moolenaarce3be472008-01-14 19:12:28 +00009386#ifdef FEAT_FOLDING
9387 /* When deleting a char the cursor line must never be in a closed fold.
9388 * E.g., when 'foldmethod' is indent and deleting the first non-white
9389 * char before a Tab. */
9390 if (did_backspace)
9391 foldOpenCursor();
9392#endif
9393
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 return did_backspace;
9395}
9396
9397#ifdef FEAT_MOUSE
9398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009399ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400{
9401 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009402 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403
9404# ifdef FEAT_GUI
9405 /* When GUI is active, also move/paste when 'mouse' is empty */
9406 if (!gui.in_use)
9407# endif
9408 if (!mouse_has(MOUSE_INSERT))
9409 return;
9410
9411 undisplay_dollar();
9412 tpos = curwin->w_cursor;
9413 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9414 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009415 win_T *new_curwin = curwin;
9416
9417 if (curwin != old_curwin && win_valid(old_curwin))
9418 {
9419 /* Mouse took us to another window. We need to go back to the
9420 * previous one to stop insert there properly. */
9421 curwin = old_curwin;
9422 curbuf = curwin->w_buffer;
9423 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009424 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009425 if (curwin != new_curwin && win_valid(new_curwin))
9426 {
9427 curwin = new_curwin;
9428 curbuf = curwin->w_buffer;
9429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430# ifdef FEAT_CINDENT
9431 can_cindent = TRUE;
9432# endif
9433 }
9434
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 /* redraw status lines (in case another window became active) */
9436 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437}
9438
9439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009440ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441{
9442 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009443 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009444# ifdef FEAT_INS_EXPAND
9445 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446# endif
9447
9448 tpos = curwin->w_cursor;
9449
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009450 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 {
9452 int row, col;
9453
9454 row = mouse_row;
9455 col = mouse_col;
9456
9457 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009458 wp = mouse_find_win(&row, &col);
9459 if (wp == NULL)
9460 return;
9461 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 curbuf = curwin->w_buffer;
9463 }
9464 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 undisplay_dollar();
9466
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009467# ifdef FEAT_INS_EXPAND
9468 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009469 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009470# endif
9471 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009472 if (dir == MSCR_DOWN || dir == MSCR_UP)
9473 {
9474 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9475 scroll_redraw(dir,
9476 (long)(curwin->w_botline - curwin->w_topline));
9477 else
9478 scroll_redraw(dir, 3L);
9479 }
9480#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009481 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009482 {
9483 int val, step = 6;
9484
9485 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009486 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009487 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9488 if (val < 0)
9489 val = 0;
9490 gui_do_horiz_scroll(val, TRUE);
9491 }
9492#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009493# ifdef FEAT_INS_EXPAND
9494 did_scroll = TRUE;
9495# endif
9496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 curwin->w_redr_status = TRUE;
9499
9500 curwin = old_curwin;
9501 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009503# ifdef FEAT_INS_EXPAND
9504 /* The popup menu may overlay the window, need to redraw it.
9505 * TODO: Would be more efficient to only redraw the windows that are
9506 * overlapped by the popup menu. */
9507 if (pum_visible() && did_scroll)
9508 {
9509 redraw_all_later(NOT_VALID);
9510 ins_compl_show_pum();
9511 }
9512# endif
9513
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009514 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 {
9516 start_arrow(&tpos);
9517# ifdef FEAT_CINDENT
9518 can_cindent = TRUE;
9519# endif
9520 }
9521}
9522#endif
9523
Bram Moolenaarec2da362017-01-21 20:04:22 +01009524/*
9525 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9526 * P_PE literally.
9527 * When "drop" is TRUE then consume the text and drop it.
9528 */
9529 int
9530bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9531{
9532 int c;
9533 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9534 int idx = 0;
9535 char_u *end = find_termcode((char_u *)"PE");
9536 int ret_char = -1;
9537 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009538 int save_paste = p_paste;
9539 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009540
9541 /* If the end code is too long we can't detect it, read everything. */
9542 if (STRLEN(end) >= NUMBUFLEN)
9543 end = NULL;
9544 ++no_mapping;
9545 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009546 p_paste = TRUE;
9547 curbuf->b_p_ai = FALSE;
9548
Bram Moolenaarec2da362017-01-21 20:04:22 +01009549 for (;;)
9550 {
9551 /* When the end is not defined read everything. */
9552 if (end == NULL && vpeekc() == NUL)
9553 break;
9554 c = plain_vgetc();
9555#ifdef FEAT_MBYTE
9556 if (has_mbyte)
9557 idx += (*mb_char2bytes)(c, buf + idx);
9558 else
9559#endif
9560 buf[idx++] = c;
9561 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009562 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009563 {
9564 if (end[idx] == NUL)
9565 break; /* Found the end of paste code. */
9566 continue;
9567 }
9568 if (!drop)
9569 {
9570 switch (mode)
9571 {
9572 case PASTE_CMDLINE:
9573 put_on_cmdline(buf, idx, TRUE);
9574 break;
9575
9576 case PASTE_EX:
9577 if (gap != NULL && ga_grow(gap, idx) == OK)
9578 {
9579 mch_memmove((char *)gap->ga_data + gap->ga_len,
9580 buf, (size_t)idx);
9581 gap->ga_len += idx;
9582 }
9583 break;
9584
9585 case PASTE_INSERT:
9586 if (stop_arrow() == OK)
9587 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009588 c = buf[0];
9589 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9590 ins_eol(c);
9591 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009592 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009593 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009594 AppendToRedobuffLit(buf, idx);
9595 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009596 }
9597 break;
9598
9599 case PASTE_ONE_CHAR:
9600 if (ret_char == -1)
9601 {
9602#ifdef FEAT_MBYTE
9603 if (has_mbyte)
9604 ret_char = (*mb_ptr2char)(buf);
9605 else
9606#endif
9607 ret_char = buf[0];
9608 }
9609 break;
9610 }
9611 }
9612 idx = 0;
9613 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009614
Bram Moolenaarec2da362017-01-21 20:04:22 +01009615 --no_mapping;
9616 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009617 p_paste = save_paste;
9618 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009619
9620 return ret_char;
9621}
9622
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009623#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009625ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009626{
9627 /* We will be leaving the current window, unless closing another tab. */
9628 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9629 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9630 {
9631 undisplay_dollar();
9632 start_arrow(&curwin->w_cursor);
9633# ifdef FEAT_CINDENT
9634 can_cindent = TRUE;
9635# endif
9636 }
9637
9638 if (c == K_TABLINE)
9639 goto_tabpage(current_tab);
9640 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009641 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009642 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009643 redraw_statuslines(); /* will redraw the tabline when needed */
9644 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009645}
9646#endif
9647
9648#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009650ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
9652 pos_T tpos;
9653
9654 undisplay_dollar();
9655 tpos = curwin->w_cursor;
9656 if (gui_do_scroll())
9657 {
9658 start_arrow(&tpos);
9659# ifdef FEAT_CINDENT
9660 can_cindent = TRUE;
9661# endif
9662 }
9663}
9664
9665 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009666ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667{
9668 pos_T tpos;
9669
9670 undisplay_dollar();
9671 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009672 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 {
9674 start_arrow(&tpos);
9675# ifdef FEAT_CINDENT
9676 can_cindent = TRUE;
9677# endif
9678 }
9679}
9680#endif
9681
9682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009683ins_left(
9684 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685{
9686 pos_T tpos;
9687
9688#ifdef FEAT_FOLDING
9689 if ((fdo_flags & FDO_HOR) && KeyTyped)
9690 foldOpenCursor();
9691#endif
9692 undisplay_dollar();
9693 tpos = curwin->w_cursor;
9694 if (oneleft() == OK)
9695 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009696#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9697 /* Only call start_arrow() when not busy with preediting, it will
9698 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009699 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009700#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009701 {
9702 start_arrow_with_change(&tpos, end_change);
9703 if (!end_change)
9704 AppendCharToRedobuff(K_LEFT);
9705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706#ifdef FEAT_RIGHTLEFT
9707 /* If exit reversed string, position is fixed */
9708 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9709 revins_legal++;
9710 revins_chars++;
9711#endif
9712 }
9713
9714 /*
9715 * if 'whichwrap' set for cursor in insert mode may go to
9716 * previous line
9717 */
9718 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9719 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009720 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 start_arrow(&tpos);
9722 --(curwin->w_cursor.lnum);
9723 coladvance((colnr_T)MAXCOL);
9724 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9725 }
9726 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009727 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009728 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729}
9730
9731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009732ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733{
9734 pos_T tpos;
9735
9736#ifdef FEAT_FOLDING
9737 if ((fdo_flags & FDO_HOR) && KeyTyped)
9738 foldOpenCursor();
9739#endif
9740 undisplay_dollar();
9741 tpos = curwin->w_cursor;
9742 if (c == K_C_HOME)
9743 curwin->w_cursor.lnum = 1;
9744 curwin->w_cursor.col = 0;
9745#ifdef FEAT_VIRTUALEDIT
9746 curwin->w_cursor.coladd = 0;
9747#endif
9748 curwin->w_curswant = 0;
9749 start_arrow(&tpos);
9750}
9751
9752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009753ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754{
9755 pos_T tpos;
9756
9757#ifdef FEAT_FOLDING
9758 if ((fdo_flags & FDO_HOR) && KeyTyped)
9759 foldOpenCursor();
9760#endif
9761 undisplay_dollar();
9762 tpos = curwin->w_cursor;
9763 if (c == K_C_END)
9764 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9765 coladvance((colnr_T)MAXCOL);
9766 curwin->w_curswant = MAXCOL;
9767
9768 start_arrow(&tpos);
9769}
9770
9771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009772ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773{
9774#ifdef FEAT_FOLDING
9775 if ((fdo_flags & FDO_HOR) && KeyTyped)
9776 foldOpenCursor();
9777#endif
9778 undisplay_dollar();
9779 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9780 {
9781 start_arrow(&curwin->w_cursor);
9782 (void)bck_word(1L, FALSE, FALSE);
9783 curwin->w_set_curswant = TRUE;
9784 }
9785 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009786 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787}
9788
9789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009790ins_right(
9791 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792{
9793#ifdef FEAT_FOLDING
9794 if ((fdo_flags & FDO_HOR) && KeyTyped)
9795 foldOpenCursor();
9796#endif
9797 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009798 if (gchar_cursor() != NUL
9799#ifdef FEAT_VIRTUALEDIT
9800 || virtual_active()
9801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 )
9803 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009804 start_arrow_with_change(&curwin->w_cursor, end_change);
9805 if (!end_change)
9806 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 curwin->w_set_curswant = TRUE;
9808#ifdef FEAT_VIRTUALEDIT
9809 if (virtual_active())
9810 oneright();
9811 else
9812#endif
9813 {
9814#ifdef FEAT_MBYTE
9815 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009816 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 else
9818#endif
9819 ++curwin->w_cursor.col;
9820 }
9821
9822#ifdef FEAT_RIGHTLEFT
9823 revins_legal++;
9824 if (revins_chars)
9825 revins_chars--;
9826#endif
9827 }
9828 /* if 'whichwrap' set for cursor in insert mode, may move the
9829 * cursor to the next line */
9830 else if (vim_strchr(p_ww, ']') != NULL
9831 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9832 {
9833 start_arrow(&curwin->w_cursor);
9834 curwin->w_set_curswant = TRUE;
9835 ++curwin->w_cursor.lnum;
9836 curwin->w_cursor.col = 0;
9837 }
9838 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009839 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009840 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841}
9842
9843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009844ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
9846#ifdef FEAT_FOLDING
9847 if ((fdo_flags & FDO_HOR) && KeyTyped)
9848 foldOpenCursor();
9849#endif
9850 undisplay_dollar();
9851 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9852 || gchar_cursor() != NUL)
9853 {
9854 start_arrow(&curwin->w_cursor);
9855 (void)fwd_word(1L, FALSE, 0);
9856 curwin->w_set_curswant = TRUE;
9857 }
9858 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009859 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860}
9861
9862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009863ins_up(
9864 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865{
9866 pos_T tpos;
9867 linenr_T old_topline = curwin->w_topline;
9868#ifdef FEAT_DIFF
9869 int old_topfill = curwin->w_topfill;
9870#endif
9871
9872 undisplay_dollar();
9873 tpos = curwin->w_cursor;
9874 if (cursor_up(1L, TRUE) == OK)
9875 {
9876 if (startcol)
9877 coladvance(getvcol_nolist(&Insstart));
9878 if (old_topline != curwin->w_topline
9879#ifdef FEAT_DIFF
9880 || old_topfill != curwin->w_topfill
9881#endif
9882 )
9883 redraw_later(VALID);
9884 start_arrow(&tpos);
9885#ifdef FEAT_CINDENT
9886 can_cindent = TRUE;
9887#endif
9888 }
9889 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009890 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891}
9892
9893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009894ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895{
9896 pos_T tpos;
9897
9898 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009899
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009900 if (mod_mask & MOD_MASK_CTRL)
9901 {
9902 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009903 if (first_tabpage->tp_next != NULL)
9904 {
9905 start_arrow(&curwin->w_cursor);
9906 goto_tabpage(-1);
9907 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009908 return;
9909 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009910
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 tpos = curwin->w_cursor;
9912 if (onepage(BACKWARD, 1L) == OK)
9913 {
9914 start_arrow(&tpos);
9915#ifdef FEAT_CINDENT
9916 can_cindent = TRUE;
9917#endif
9918 }
9919 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009920 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921}
9922
9923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009924ins_down(
9925 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926{
9927 pos_T tpos;
9928 linenr_T old_topline = curwin->w_topline;
9929#ifdef FEAT_DIFF
9930 int old_topfill = curwin->w_topfill;
9931#endif
9932
9933 undisplay_dollar();
9934 tpos = curwin->w_cursor;
9935 if (cursor_down(1L, TRUE) == OK)
9936 {
9937 if (startcol)
9938 coladvance(getvcol_nolist(&Insstart));
9939 if (old_topline != curwin->w_topline
9940#ifdef FEAT_DIFF
9941 || old_topfill != curwin->w_topfill
9942#endif
9943 )
9944 redraw_later(VALID);
9945 start_arrow(&tpos);
9946#ifdef FEAT_CINDENT
9947 can_cindent = TRUE;
9948#endif
9949 }
9950 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009951 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952}
9953
9954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009955ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956{
9957 pos_T tpos;
9958
9959 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009960
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009961 if (mod_mask & MOD_MASK_CTRL)
9962 {
9963 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009964 if (first_tabpage->tp_next != NULL)
9965 {
9966 start_arrow(&curwin->w_cursor);
9967 goto_tabpage(0);
9968 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009969 return;
9970 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009971
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 tpos = curwin->w_cursor;
9973 if (onepage(FORWARD, 1L) == OK)
9974 {
9975 start_arrow(&tpos);
9976#ifdef FEAT_CINDENT
9977 can_cindent = TRUE;
9978#endif
9979 }
9980 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009981 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982}
9983
9984#ifdef FEAT_DND
9985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009986ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987{
9988 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9989}
9990#endif
9991
9992/*
9993 * Handle TAB in Insert or Replace mode.
9994 * Return TRUE when the TAB needs to be inserted like a normal character.
9995 */
9996 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009997ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998{
9999 int ind;
10000 int i;
10001 int temp;
10002
10003 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10004 Insstart_blank_vcol = get_nolist_virtcol();
10005 if (echeck_abbr(TAB + ABBR_OFF))
10006 return FALSE;
10007
10008 ind = inindent(0);
10009#ifdef FEAT_CINDENT
10010 if (ind)
10011 can_cindent = FALSE;
10012#endif
10013
10014 /*
10015 * When nothing special, insert TAB like a normal character
10016 */
10017 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010018 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010019 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 return TRUE;
10021
10022 if (stop_arrow() == FAIL)
10023 return TRUE;
10024
10025 did_ai = FALSE;
10026#ifdef FEAT_SMARTINDENT
10027 did_si = FALSE;
10028 can_si = FALSE;
10029 can_si_back = FALSE;
10030#endif
10031 AppendToRedobuff((char_u *)"\t");
10032
10033 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010034 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010035 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10036 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 else /* otherwise use 'tabstop' */
10038 temp = (int)curbuf->b_p_ts;
10039 temp -= get_nolist_virtcol() % temp;
10040
10041 /*
10042 * Insert the first space with ins_char(). It will delete one char in
10043 * replace mode. Insert the rest with ins_str(); it will not delete any
10044 * chars. For VREPLACE mode, we use ins_char() for all characters.
10045 */
10046 ins_char(' ');
10047 while (--temp > 0)
10048 {
10049#ifdef FEAT_VREPLACE
10050 if (State & VREPLACE_FLAG)
10051 ins_char(' ');
10052 else
10053#endif
10054 {
10055 ins_str((char_u *)" ");
10056 if (State & REPLACE_FLAG) /* no char replaced */
10057 replace_push(NUL);
10058 }
10059 }
10060
10061 /*
10062 * When 'expandtab' not set: Replace spaces by TABs where possible.
10063 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010064 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 {
10066 char_u *ptr;
10067#ifdef FEAT_VREPLACE
10068 char_u *saved_line = NULL; /* init for GCC */
10069 pos_T pos;
10070#endif
10071 pos_T fpos;
10072 pos_T *cursor;
10073 colnr_T want_vcol, vcol;
10074 int change_col = -1;
10075 int save_list = curwin->w_p_list;
10076
10077 /*
10078 * Get the current line. For VREPLACE mode, don't make real changes
10079 * yet, just work on a copy of the line.
10080 */
10081#ifdef FEAT_VREPLACE
10082 if (State & VREPLACE_FLAG)
10083 {
10084 pos = curwin->w_cursor;
10085 cursor = &pos;
10086 saved_line = vim_strsave(ml_get_curline());
10087 if (saved_line == NULL)
10088 return FALSE;
10089 ptr = saved_line + pos.col;
10090 }
10091 else
10092#endif
10093 {
10094 ptr = ml_get_cursor();
10095 cursor = &curwin->w_cursor;
10096 }
10097
10098 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10099 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10100 curwin->w_p_list = FALSE;
10101
10102 /* Find first white before the cursor */
10103 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010104 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 {
10106 --fpos.col;
10107 --ptr;
10108 }
10109
10110 /* In Replace mode, don't change characters before the insert point. */
10111 if ((State & REPLACE_FLAG)
10112 && fpos.lnum == Insstart.lnum
10113 && fpos.col < Insstart.col)
10114 {
10115 ptr += Insstart.col - fpos.col;
10116 fpos.col = Insstart.col;
10117 }
10118
10119 /* compute virtual column numbers of first white and cursor */
10120 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10121 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10122
Bram Moolenaar597a4222014-06-25 14:39:50 +020010123 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10124 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010125 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010127 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 if (vcol + i > want_vcol)
10129 break;
10130 if (*ptr != TAB)
10131 {
10132 *ptr = TAB;
10133 if (change_col < 0)
10134 {
10135 change_col = fpos.col; /* Column of first change */
10136 /* May have to adjust Insstart */
10137 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10138 Insstart.col = fpos.col;
10139 }
10140 }
10141 ++fpos.col;
10142 ++ptr;
10143 vcol += i;
10144 }
10145
10146 if (change_col >= 0)
10147 {
10148 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010149 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150
10151 /* Skip over the spaces we need. */
10152 while (vcol < want_vcol && *ptr == ' ')
10153 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010154 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 ++ptr;
10156 ++repl_off;
10157 }
10158 if (vcol > want_vcol)
10159 {
10160 /* Must have a char with 'showbreak' just before it. */
10161 --ptr;
10162 --repl_off;
10163 }
10164 fpos.col += repl_off;
10165
10166 /* Delete following spaces. */
10167 i = cursor->col - fpos.col;
10168 if (i > 0)
10169 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010170 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 /* correct replace stack. */
10172 if ((State & REPLACE_FLAG)
10173#ifdef FEAT_VREPLACE
10174 && !(State & VREPLACE_FLAG)
10175#endif
10176 )
10177 for (temp = i; --temp >= 0; )
10178 replace_join(repl_off);
10179 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010180#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010181 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010182 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010183 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010184 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10185 (char_u *)"\t", 1);
10186 }
10187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 cursor->col -= i;
10189
10190#ifdef FEAT_VREPLACE
10191 /*
10192 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10193 * backspacing over the changed spacing and then inserting the new
10194 * spacing.
10195 */
10196 if (State & VREPLACE_FLAG)
10197 {
10198 /* Backspace from real cursor to change_col */
10199 backspace_until_column(change_col);
10200
10201 /* Insert each char in saved_line from changed_col to
10202 * ptr-cursor */
10203 ins_bytes_len(saved_line + change_col,
10204 cursor->col - change_col);
10205 }
10206#endif
10207 }
10208
10209#ifdef FEAT_VREPLACE
10210 if (State & VREPLACE_FLAG)
10211 vim_free(saved_line);
10212#endif
10213 curwin->w_p_list = save_list;
10214 }
10215
10216 return FALSE;
10217}
10218
10219/*
10220 * Handle CR or NL in insert mode.
10221 * Return TRUE when out of memory or can't undo.
10222 */
10223 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010224ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225{
10226 int i;
10227
10228 if (echeck_abbr(c + ABBR_OFF))
10229 return FALSE;
10230 if (stop_arrow() == FAIL)
10231 return TRUE;
10232 undisplay_dollar();
10233
10234 /*
10235 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10236 * character under the cursor. Only push a NUL on the replace stack,
10237 * nothing to put back when the NL is deleted.
10238 */
10239 if ((State & REPLACE_FLAG)
10240#ifdef FEAT_VREPLACE
10241 && !(State & VREPLACE_FLAG)
10242#endif
10243 )
10244 replace_push(NUL);
10245
10246 /*
10247 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10248 * replacing the next line, so we push all of the characters left on the
10249 * line onto the replace stack. This is not done here though, it is done
10250 * in open_line().
10251 */
10252
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010253#ifdef FEAT_VIRTUALEDIT
10254 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10255 * CTRL-O). */
10256 if (virtual_active() && curwin->w_cursor.coladd > 0)
10257 coladvance(getviscol());
10258#endif
10259
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260#ifdef FEAT_RIGHTLEFT
10261# ifdef FEAT_FKMAP
10262 if (p_altkeymap && p_fkmap)
10263 fkmap(NL);
10264# endif
10265 /* NL in reverse insert will always start in the end of
10266 * current line. */
10267 if (revins_on)
10268 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10269#endif
10270
10271 AppendToRedobuff(NL_STR);
10272 i = open_line(FORWARD,
10273#ifdef FEAT_COMMENTS
10274 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10275#endif
10276 0, old_indent);
10277 old_indent = 0;
10278#ifdef FEAT_CINDENT
10279 can_cindent = TRUE;
10280#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010281#ifdef FEAT_FOLDING
10282 /* When inserting a line the cursor line must never be in a closed fold. */
10283 foldOpenCursor();
10284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285
10286 return (!i);
10287}
10288
10289#ifdef FEAT_DIGRAPHS
10290/*
10291 * Handle digraph in insert mode.
10292 * Returns character still to be inserted, or NUL when nothing remaining to be
10293 * done.
10294 */
10295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010296ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297{
10298 int c;
10299 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010300 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301
10302 pc_status = PC_STATUS_UNSET;
10303 if (redrawing() && !char_avail())
10304 {
10305 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010306 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307
10308 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010309 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310#ifdef FEAT_CMDL_INFO
10311 add_to_showcmd_c(Ctrl_K);
10312#endif
10313 }
10314
10315#ifdef USE_ON_FLY_SCROLL
10316 dont_scroll = TRUE; /* disallow scrolling here */
10317#endif
10318
10319 /* don't map the digraph chars. This also prevents the
10320 * mode message to be deleted when ESC is hit */
10321 ++no_mapping;
10322 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010323 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 --no_mapping;
10325 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010326 if (did_putchar)
10327 /* when the line fits in 'columns' the '?' is at the start of the next
10328 * line and will not be removed by the redraw */
10329 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010330
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 if (IS_SPECIAL(c) || mod_mask) /* special key */
10332 {
10333#ifdef FEAT_CMDL_INFO
10334 clear_showcmd();
10335#endif
10336 insert_special(c, TRUE, FALSE);
10337 return NUL;
10338 }
10339 if (c != ESC)
10340 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010341 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 if (redrawing() && !char_avail())
10343 {
10344 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010345 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346
10347 if (char2cells(c) == 1)
10348 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010349 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010351 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 }
10353#ifdef FEAT_CMDL_INFO
10354 add_to_showcmd_c(c);
10355#endif
10356 }
10357 ++no_mapping;
10358 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010359 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 --no_mapping;
10361 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010362 if (did_putchar)
10363 /* when the line fits in 'columns' the '?' is at the start of the
10364 * next line and will not be removed by a redraw */
10365 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 if (cc != ESC)
10367 {
10368 AppendToRedobuff((char_u *)CTRL_V_STR);
10369 c = getdigraph(c, cc, TRUE);
10370#ifdef FEAT_CMDL_INFO
10371 clear_showcmd();
10372#endif
10373 return c;
10374 }
10375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376#ifdef FEAT_CMDL_INFO
10377 clear_showcmd();
10378#endif
10379 return NUL;
10380}
10381#endif
10382
10383/*
10384 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10385 * Returns the char to be inserted, or NUL if none found.
10386 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010387 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010388ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389{
10390 int c;
10391 int temp;
10392 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010393 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394
10395 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10396 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010397 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 return NUL;
10399 }
10400
10401 /* try to advance to the cursor column */
10402 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010403 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 prev_ptr = ptr;
10405 validate_virtcol();
10406 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10407 {
10408 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010409 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 }
10411 if ((colnr_T)temp > curwin->w_virtcol)
10412 ptr = prev_ptr;
10413
10414#ifdef FEAT_MBYTE
10415 c = (*mb_ptr2char)(ptr);
10416#else
10417 c = *ptr;
10418#endif
10419 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010420 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 return c;
10422}
10423
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010424/*
10425 * CTRL-Y or CTRL-E typed in Insert mode.
10426 */
10427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010428ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010429{
10430 int c = tc;
10431
10432#ifdef FEAT_INS_EXPAND
10433 if (ctrl_x_mode == CTRL_X_SCROLL)
10434 {
10435 if (c == Ctrl_Y)
10436 scrolldown_clamp();
10437 else
10438 scrollup_clamp();
10439 redraw_later(VALID);
10440 }
10441 else
10442#endif
10443 {
10444 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10445 if (c != NUL)
10446 {
10447 long tw_save;
10448
10449 /* The character must be taken literally, insert like it
10450 * was typed after a CTRL-V, and pretend 'textwidth'
10451 * wasn't set. Digits, 'o' and 'x' are special after a
10452 * CTRL-V, don't use it for these. */
10453 if (c < 256 && !isalnum(c))
10454 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10455 tw_save = curbuf->b_p_tw;
10456 curbuf->b_p_tw = -1;
10457 insert_special(c, TRUE, FALSE);
10458 curbuf->b_p_tw = tw_save;
10459#ifdef FEAT_RIGHTLEFT
10460 revins_chars++;
10461 revins_legal++;
10462#endif
10463 c = Ctrl_V; /* pretend CTRL-V is last character */
10464 auto_format(FALSE, TRUE);
10465 }
10466 }
10467 return c;
10468}
10469
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470#ifdef FEAT_SMARTINDENT
10471/*
10472 * Try to do some very smart auto-indenting.
10473 * Used when inserting a "normal" character.
10474 */
10475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010476ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477{
10478 pos_T *pos, old_pos;
10479 char_u *ptr;
10480 int i;
10481 int temp;
10482
10483 /*
10484 * do some very smart indenting when entering '{' or '}'
10485 */
10486 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10487 {
10488 /*
10489 * for '}' set indent equal to indent of line containing matching '{'
10490 */
10491 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10492 {
10493 old_pos = curwin->w_cursor;
10494 /*
10495 * If the matching '{' has a ')' immediately before it (ignoring
10496 * white-space), then line up with the start of the line
10497 * containing the matching '(' if there is one. This handles the
10498 * case where an "if (..\n..) {" statement continues over multiple
10499 * lines -- webb
10500 */
10501 ptr = ml_get(pos->lnum);
10502 i = pos->col;
10503 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010504 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 ;
10506 curwin->w_cursor.lnum = pos->lnum;
10507 curwin->w_cursor.col = i;
10508 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10509 curwin->w_cursor = *pos;
10510 i = get_indent();
10511 curwin->w_cursor = old_pos;
10512#ifdef FEAT_VREPLACE
10513 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010514 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 else
10516#endif
10517 (void)set_indent(i, SIN_CHANGED);
10518 }
10519 else if (curwin->w_cursor.col > 0)
10520 {
10521 /*
10522 * when inserting '{' after "O" reduce indent, but not
10523 * more than indent of previous line
10524 */
10525 temp = TRUE;
10526 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10527 {
10528 old_pos = curwin->w_cursor;
10529 i = get_indent();
10530 while (curwin->w_cursor.lnum > 1)
10531 {
10532 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10533
10534 /* ignore empty lines and lines starting with '#'. */
10535 if (*ptr != '#' && *ptr != NUL)
10536 break;
10537 }
10538 if (get_indent() >= i)
10539 temp = FALSE;
10540 curwin->w_cursor = old_pos;
10541 }
10542 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010543 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 }
10545 }
10546
10547 /*
10548 * set indent of '#' always to 0
10549 */
10550 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10551 {
10552 /* remember current indent for next line */
10553 old_indent = get_indent();
10554 (void)set_indent(0, SIN_CHANGED);
10555 }
10556
10557 /* Adjust ai_col, the char at this position can be deleted. */
10558 if (ai_col > curwin->w_cursor.col)
10559 ai_col = curwin->w_cursor.col;
10560}
10561#endif
10562
10563/*
10564 * Get the value that w_virtcol would have when 'list' is off.
10565 * Unless 'cpo' contains the 'L' flag.
10566 */
10567 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010568get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569{
10570 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10571 return getvcol_nolist(&curwin->w_cursor);
10572 validate_virtcol();
10573 return curwin->w_virtcol;
10574}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010575
10576#ifdef FEAT_AUTOCMD
10577/*
10578 * Handle the InsertCharPre autocommand.
10579 * "c" is the character that was typed.
10580 * Return a pointer to allocated memory with the replacement string.
10581 * Return NULL to continue inserting "c".
10582 */
10583 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010584do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010585{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010586 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010587 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010588
10589 /* Return quickly when there is nothing to do. */
10590 if (!has_insertcharpre())
10591 return NULL;
10592
Bram Moolenaar704984a2012-06-01 14:57:51 +020010593#ifdef FEAT_MBYTE
10594 if (has_mbyte)
10595 buf[(*mb_char2bytes)(c, buf)] = NUL;
10596 else
10597#endif
10598 {
10599 buf[0] = c;
10600 buf[1] = NUL;
10601 }
10602
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010603 /* Lock the text to avoid weird things from happening. */
10604 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010605 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010606
Bram Moolenaar704984a2012-06-01 14:57:51 +020010607 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010608 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010609 {
10610 /* Get the value of v:char. It may be empty or more than one
10611 * character. Only use it when changed, otherwise continue with the
10612 * original character to avoid breaking autoindent. */
10613 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10614 res = vim_strsave(get_vim_var_str(VV_CHAR));
10615 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010616
10617 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10618 --textlock;
10619
10620 return res;
10621}
10622#endif