blob: bd840d1475666047ba5475ac318d5562b20485f5 [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);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100158static void ins_compl_longest_match(compl_T *match);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100159static void ins_compl_del_pum(void);
160static int pum_wanted(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100161static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
162static char_u *find_line_end(char_u *ptr);
163static void ins_compl_free(void);
164static void ins_compl_clear(void);
165static int ins_compl_bs(void);
166static int ins_compl_need_restart(void);
167static void ins_compl_new_leader(void);
168static void ins_compl_addleader(int c);
169static int ins_compl_len(void);
170static void ins_compl_restart(void);
171static void ins_compl_set_original_text(char_u *str);
172static void ins_compl_addfrommatch(void);
173static int ins_compl_prep(int c);
174static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100175# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100176static void ins_compl_add_list(list_T *list);
177static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100178# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100179static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200180static void ins_compl_insert(int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100181static int ins_compl_key2dir(int c);
182static int ins_compl_pum_key(int c);
183static int ins_compl_key2count(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100184static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100185static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#endif /* FEAT_INS_EXPAND */
188
189#define BACKSPACE_CHAR 1
190#define BACKSPACE_WORD 2
191#define BACKSPACE_WORD_NOT_SPACE 3
192#define BACKSPACE_LINE 4
193
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100194static void ins_redraw(int ready);
195static void ins_ctrl_v(void);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200196#ifdef FEAT_JOB_CHANNEL
197static void init_prompt(int cmdchar_todo);
198#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100199static void undisplay_dollar(void);
200static void insert_special(int, int, int);
201static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
202static void check_auto_format(int);
203static void redo_literal(int c);
204static void start_arrow(pos_T *end_insert_pos);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100205static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000206#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100207static void check_spell_redraw(void);
208static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000209static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000210#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100211static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
212static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void replace_join(int off);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void replace_flush(void);
218static void replace_do_bs(int limit_col);
219static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ins_reg(void);
224static void ins_ctrl_g(void);
225static void ins_ctrl_hat(void);
226static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static int ins_start_select(int c);
231static void ins_insert(int replaceState);
232static void ins_ctrl_o(void);
233static void ins_shift(int c, int lastc);
234static void ins_del(void);
235static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100237static void ins_mouse(int c);
238static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000240#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000242#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static void ins_left(int end_change);
244static void ins_home(int c);
245static void ins_end(int c);
246static void ins_s_left(void);
247static void ins_right(int end_change);
248static void ins_s_right(void);
249static void ins_up(int startcol);
250static void ins_pageup(void);
251static void ins_down(int startcol);
252static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100254static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static int ins_tab(void);
257static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100265#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100266static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100267#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200268static int ins_apply_autocmds(event_T event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269
270static colnr_T Insstart_textlen; /* length of line when insert started */
271static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100272static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274static char_u *last_insert = NULL; /* the text of the previous insert,
275 K_SPECIAL and CSI are escaped */
276static int last_insert_skip; /* nr of chars in front of previous insert */
277static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000278static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280#ifdef FEAT_CINDENT
281static int can_cindent; /* may do cindenting on this line */
282#endif
283
284static int old_indent = 0; /* for ^^D command in insert mode */
285
286#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000287static int revins_on; /* reverse insert mode on */
288static int revins_chars; /* how much to skip after edit */
289static int revins_legal; /* was the last char 'legal'? */
290static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291#endif
292
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static int ins_need_undo; /* call u_save() before inserting a
294 char. Set when edit() is called.
295 after that arrow_used is used. */
296
297static int did_add_space = FALSE; /* auto_format() added an extra space
298 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200299static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
300 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
302/*
303 * edit(): Start inserting text.
304 *
305 * "cmdchar" can be:
306 * 'i' normal insert command
307 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100308 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 * 'R' replace command
310 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
311 * but still only one <CR> is inserted. The <Esc> is not used for redo.
312 * 'g' "gI" command.
313 * 'V' "gR" command for Virtual Replace mode.
314 * 'v' "gr" command for single character Virtual Replace mode.
315 *
316 * This function is not called recursively. For CTRL-O commands, it returns
317 * and lets the caller handle the Normal-mode command.
318 *
319 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
320 */
321 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100322edit(
323 int cmdchar,
324 int startln, /* if set, insert at start of line */
325 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326{
327 int c = 0;
328 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100329 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000330 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 int i;
333 int did_backspace = TRUE; /* previous char was backspace */
334#ifdef FEAT_CINDENT
335 int line_is_white = FALSE; /* line is empty before insert */
336#endif
337 linenr_T old_topline = 0; /* topline before insertion */
338#ifdef FEAT_DIFF
339 int old_topfill = -1;
340#endif
341 int inserted_space = FALSE; /* just inserted a space */
342 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000343 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200344#ifdef FEAT_JOB_CHANNEL
345 int cmdchar_todo = cmdchar;
346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000348 /* Remember whether editing was restarted after CTRL-O. */
349 did_restart_edit = restart_edit;
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 /* sleep before redrawing, needed for "CTRL-O :" that results in an
352 * error message */
353 check_for_delay(TRUE);
354
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100355 /* set Insstart_orig to Insstart */
356 update_Insstart_orig = TRUE;
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifdef HAVE_SANDBOX
359 /* Don't allow inserting in the sandbox. */
360 if (sandbox != 0)
361 {
362 EMSG(_(e_sandbox));
363 return FALSE;
364 }
365#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000366 /* Don't allow changes in the buffer while editing the cmdline. The
367 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000368 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000369 {
370 EMSG(_(e_secure));
371 return FALSE;
372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
374#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000375 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000376 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000377 {
378 EMSG(_(e_secure));
379 return FALSE;
380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 ins_compl_clear(); /* clear stuff for CTRL-X mode */
382#endif
383
Bram Moolenaar843ee412004-06-30 16:16:41 +0000384 /*
385 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
386 */
387 if (cmdchar != 'r' && cmdchar != 'v')
388 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100389 pos_T save_cursor = curwin->w_cursor;
390
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100391#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000392 if (cmdchar == 'R')
393 ptr = (char_u *)"r";
394 else if (cmdchar == 'V')
395 ptr = (char_u *)"v";
396 else
397 ptr = (char_u *)"i";
398 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200399 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100400#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200401 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100402
Bram Moolenaar097c9922013-05-19 21:15:15 +0200403 /* Make sure the cursor didn't move. Do call check_cursor_col() in
404 * case the text was modified. Since Insert mode was not started yet
405 * a call to check_cursor_col() may move the cursor, especially with
406 * the "A" command, thus set State to avoid that. Also check that the
407 * line number is still valid (lines may have been deleted).
408 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100409 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100410#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200411 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100412#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200413 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100414 {
415 int save_state = State;
416
417 curwin->w_cursor = save_cursor;
418 State = INSERT;
419 check_cursor_col();
420 State = save_state;
421 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000422 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000423
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200424#ifdef FEAT_CONCEAL
425 /* Check if the cursor line needs redrawing before changing State. If
426 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200427 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200428#endif
429
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430#ifdef FEAT_MOUSE
431 /*
432 * When doing a paste with the middle mouse button, Insstart is set to
433 * where the paste started.
434 */
435 if (where_paste_started.lnum != 0)
436 Insstart = where_paste_started;
437 else
438#endif
439 {
440 Insstart = curwin->w_cursor;
441 if (startln)
442 Insstart.col = 0;
443 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000444 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 Insstart_blank_vcol = MAXCOL;
446 if (!did_ai)
447 ai_col = 0;
448
449 if (cmdchar != NUL && restart_edit == 0)
450 {
451 ResetRedobuff();
452 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 if (cmdchar == 'V' || cmdchar == 'v')
454 {
455 /* "gR" or "gr" command */
456 AppendCharToRedobuff('g');
457 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
458 }
459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100461 if (cmdchar == K_PS)
462 AppendCharToRedobuff('a');
463 else
464 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 if (cmdchar == 'g') /* "gI" command */
466 AppendCharToRedobuff('I');
467 else if (cmdchar == 'r') /* "r<CR>" command */
468 count = 1; /* insert only one <CR> */
469 }
470 }
471
472 if (cmdchar == 'R')
473 {
474#ifdef FEAT_FKMAP
475 if (p_fkmap && p_ri)
476 {
477 beep_flush();
478 EMSG(farsi_text_3); /* encoded in Farsi */
479 State = INSERT;
480 }
481 else
482#endif
483 State = REPLACE;
484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 else if (cmdchar == 'V' || cmdchar == 'v')
486 {
487 State = VREPLACE;
488 replaceState = VREPLACE;
489 orig_line_count = curbuf->b_ml.ml_line_count;
490 vr_lines_changed = 1;
491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 else
493 State = INSERT;
494
495 stop_insert_mode = FALSE;
496
497 /*
498 * Need to recompute the cursor position, it might move when the cursor is
499 * on a TAB or special character.
500 */
501 curs_columns(TRUE);
502
503 /*
504 * Enable langmap or IME, indicated by 'iminsert'.
505 * Note that IME may enabled/disabled without us noticing here, thus the
506 * 'iminsert' value may not reflect what is actually used. It is updated
507 * when hitting <Esc>.
508 */
509 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
510 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100511#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
513#endif
514
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515#ifdef FEAT_MOUSE
516 setmouse();
517#endif
518#ifdef FEAT_CMDL_INFO
519 clear_showcmd();
520#endif
521#ifdef FEAT_RIGHTLEFT
522 /* there is no reverse replace mode */
523 revins_on = (State == INSERT && p_ri);
524 if (revins_on)
525 undisplay_dollar();
526 revins_chars = 0;
527 revins_legal = 0;
528 revins_scol = -1;
529#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100530 if (!p_ek)
531 /* Disable bracketed paste mode, we won't recognize the escape
532 * sequences. */
533 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
535 /*
536 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100537 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
538 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 */
540 if (restart_edit != 0 && stuff_empty())
541 {
542#ifdef FEAT_MOUSE
543 /*
544 * After a paste we consider text typed to be part of the insert for
545 * the pasted text. You can backspace over the pasted text too.
546 */
547 if (where_paste_started.lnum)
548 arrow_used = FALSE;
549 else
550#endif
551 arrow_used = TRUE;
552 restart_edit = 0;
553
554 /*
555 * If the cursor was after the end-of-line before the CTRL-O and it is
556 * now at the end-of-line, put it after the end-of-line (this is not
557 * correct in very rare cases).
558 * Also do this if curswant is greater than the current virtual
559 * column. Eg after "^O$" or "^O80|".
560 */
561 validate_virtcol();
562 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000563 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 || curwin->w_curswant > curwin->w_virtcol)
565 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
566 {
567 if (ptr[1] == NUL)
568 ++curwin->w_cursor.col;
569#ifdef FEAT_MBYTE
570 else if (has_mbyte)
571 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000572 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 if (ptr[i] == NUL)
574 curwin->w_cursor.col += i;
575 }
576#endif
577 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000578 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580 else
581 arrow_used = FALSE;
582
583 /* we are in insert mode now, don't need to start it anymore */
584 need_start_insertmode = FALSE;
585
586 /* Need to save the line for undo before inserting the first char. */
587 ins_need_undo = TRUE;
588
589#ifdef FEAT_MOUSE
590 where_paste_started.lnum = 0;
591#endif
592#ifdef FEAT_CINDENT
593 can_cindent = TRUE;
594#endif
595#ifdef FEAT_FOLDING
596 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
597 * restarting. */
598 if (!p_im && did_restart_edit == 0)
599 foldOpenCursor();
600#endif
601
602 /*
603 * If 'showmode' is set, show the current (insert/replace/..) mode.
604 * A warning message for changing a readonly file is given here, before
605 * actually changing anything. It's put after the mode, if any.
606 */
607 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000608 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 i = showmode();
610
611 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000612 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
614#ifdef CURSOR_SHAPE
615 ui_cursor_shape(); /* may show different cursor shape */
616#endif
617#ifdef FEAT_DIGRAPHS
618 do_digraph(-1); /* clear digraphs */
619#endif
620
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000621 /*
622 * Get the current length of the redo buffer, those characters have to be
623 * skipped if we want to get to the inserted characters.
624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 ptr = get_inserted();
626 if (ptr == NULL)
627 new_insert_skip = 0;
628 else
629 {
630 new_insert_skip = (int)STRLEN(ptr);
631 vim_free(ptr);
632 }
633
634 old_indent = 0;
635
636 /*
637 * Main loop in Insert mode: repeat until Insert mode is left.
638 */
639 for (;;)
640 {
641#ifdef FEAT_RIGHTLEFT
642 if (!revins_legal)
643 revins_scol = -1; /* reset on illegal motions */
644 else
645 revins_legal = 0;
646#endif
647 if (arrow_used) /* don't repeat insert when arrow key used */
648 count = 0;
649
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100650 if (update_Insstart_orig)
651 Insstart_orig = Insstart;
652
Bram Moolenaar00672e12016-06-26 18:38:13 +0200653 if (stop_insert_mode
654#ifdef FEAT_INS_EXPAND
655 && !pum_visible()
656#endif
657 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {
659 /* ":stopinsert" used or 'insertmode' reset */
660 count = 0;
661 goto doESCkey;
662 }
663
664 /* set curwin->w_curswant for next K_DOWN or K_UP */
665 if (!arrow_used)
666 curwin->w_set_curswant = TRUE;
667
668 /* If there is no typeahead may check for timestamps (e.g., for when a
669 * menu invoked a shell command). */
670 if (stuff_empty())
671 {
672 did_check_timestamps = FALSE;
673 if (need_check_timestamps)
674 check_timestamps(FALSE);
675 }
676
677 /*
678 * When emsg() was called msg_scroll will have been set.
679 */
680 msg_scroll = FALSE;
681
682#ifdef FEAT_GUI
683 /* When 'mousefocus' is set a mouse movement may have taken us to
684 * another window. "need_mouse_correct" may then be set because of an
685 * autocommand. */
686 if (need_mouse_correct)
687 gui_mouse_correct();
688#endif
689
690#ifdef FEAT_FOLDING
691 /* Open fold at the cursor line, according to 'foldopen'. */
692 if (fdo_flags & FDO_INSERT)
693 foldOpenCursor();
694 /* Close folds where the cursor isn't, according to 'foldclose' */
695 if (!char_avail())
696 foldCheckClose();
697#endif
698
Bram Moolenaarf2732452018-06-03 14:47:35 +0200699#ifdef FEAT_JOB_CHANNEL
700 if (bt_prompt(curbuf))
701 {
702 init_prompt(cmdchar_todo);
703 cmdchar_todo = NUL;
704 }
705#endif
706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 /*
708 * If we inserted a character at the last position of the last line in
709 * the window, scroll the window one line up. This avoids an extra
710 * redraw.
711 * This is detected when the cursor column is smaller after inserting
712 * something.
713 * Don't do this when the topline changed already, it has
714 * already been adjusted (by insertchar() calling open_line())).
715 */
716 if (curbuf->b_mod_set
717 && curwin->w_p_wrap
718 && !did_backspace
719 && curwin->w_topline == old_topline
720#ifdef FEAT_DIFF
721 && curwin->w_topfill == old_topfill
722#endif
723 )
724 {
725 mincol = curwin->w_wcol;
726 validate_cursor_col();
727
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200728 if (
729#ifdef FEAT_VARTABS
730 (int)curwin->w_wcol < mincol - tabstop_at(
731 get_nolist_virtcol(), curbuf->b_p_ts,
732 curbuf->b_p_vts_array)
733#else
734 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 && curwin->w_wrow == W_WINROW(curwin)
737 + curwin->w_height - 1 - p_so
738 && (curwin->w_cursor.lnum != curwin->w_topline
739#ifdef FEAT_DIFF
740 || curwin->w_topfill > 0
741#endif
742 ))
743 {
744#ifdef FEAT_DIFF
745 if (curwin->w_topfill > 0)
746 --curwin->w_topfill;
747 else
748#endif
749#ifdef FEAT_FOLDING
750 if (hasFolding(curwin->w_topline, NULL, &old_topline))
751 set_topline(curwin, old_topline + 1);
752 else
753#endif
754 set_topline(curwin, curwin->w_topline + 1);
755 }
756 }
757
758 /* May need to adjust w_topline to show the cursor. */
759 update_topline();
760
761 did_backspace = FALSE;
762
763 validate_cursor(); /* may set must_redraw */
764
765 /*
766 * Redraw the display when no characters are waiting.
767 * Also shows mode, ruler and positions cursor.
768 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000769 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (curwin->w_p_scb)
772 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
Bram Moolenaar860cae12010-06-05 23:22:07 +0200774 if (curwin->w_p_crb)
775 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 update_curswant();
777 old_topline = curwin->w_topline;
778#ifdef FEAT_DIFF
779 old_topfill = curwin->w_topfill;
780#endif
781
782#ifdef USE_ON_FLY_SCROLL
783 dont_scroll = FALSE; /* allow scrolling here */
784#endif
785
786 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100787 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100789 if (c != K_CURSORHOLD)
790 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200791
792 /* After using CTRL-G U the next cursor key will not break undo. */
793 if (dont_sync_undo == MAYBE)
794 dont_sync_undo = TRUE;
795 else
796 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100797 if (cmdchar == K_PS)
798 /* Got here from normal mode when bracketed paste started. */
799 c = K_PS;
800 else
801 do
802 {
803 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200804
805 if (stop_insert_mode)
806 {
807 // Insert mode ended, possibly from a callback.
808 count = 0;
809 nomove = TRUE;
810 goto doESCkey;
811 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100812 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000814 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
815 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817#ifdef FEAT_RIGHTLEFT
818 if (p_hkmap && KeyTyped)
819 c = hkmap(c); /* Hebrew mode mapping */
820#endif
821#ifdef FEAT_FKMAP
822 if (p_fkmap && KeyTyped)
823 c = fkmap(c); /* Farsi mode mapping */
824#endif
825
826#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000827 /*
828 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000829 * and the cursor is still in the completed word. Only when there is
830 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000831 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000832 if (compl_started
833 && pum_wanted()
834 && curwin->w_cursor.col >= compl_col
835 && (compl_shown_match == NULL
836 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000837 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000838 /* BS: Delete one character from "compl_leader". */
839 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000840 && curwin->w_cursor.col > compl_col
841 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000842 continue;
843
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 /* When no match was selected or it was edited. */
845 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000846 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000847 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000848 * "compl_leader". Except when at the original match and
849 * there is nothing to add, CTRL-L works like CTRL-P then. */
850 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100851 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000852 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000853 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000854 {
855 ins_compl_addfrommatch();
856 continue;
857 }
858
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000859 /* A non-white character that fits in with the current
860 * completion: Add to "compl_leader". */
861 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000862 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100863#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100864 /* Trigger InsertCharPre. */
865 char_u *str = do_insert_char_pre(c);
866 char_u *p;
867
868 if (str != NULL)
869 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100870 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100871 ins_compl_addleader(PTR2CHAR(p));
872 vim_free(str);
873 }
874 else
875#endif
876 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000877 continue;
878 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000879
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000880 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000881 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200882 if ((c == Ctrl_Y || (compl_enter_selects
883 && (c == CAR || c == K_KENTER || c == NL)))
884 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000885 {
886 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200887 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000888 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000889 }
890 }
891
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
893 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000894 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000895 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000896 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897#endif
898
Bram Moolenaar488c6512005-08-11 20:09:58 +0000899 /* CTRL-\ CTRL-N goes to Normal mode,
900 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
901 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (c == Ctrl_BSL)
903 {
904 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000905 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 ++no_mapping;
907 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000908 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 --no_mapping;
910 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000911 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000913 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 vungetc(c);
915 c = Ctrl_BSL;
916 }
917 else if (c == Ctrl_G && p_im)
918 continue;
919 else
920 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000921 if (c == Ctrl_O)
922 {
923 ins_ctrl_o();
924 ins_at_eol = FALSE; /* cursor keeps its column */
925 nomove = TRUE;
926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 count = 0;
928 goto doESCkey;
929 }
930 }
931
932#ifdef FEAT_DIGRAPHS
933 c = do_digraph(c);
934#endif
935
936#ifdef FEAT_INS_EXPAND
937 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
938 goto docomplete;
939#endif
940 if (c == Ctrl_V || c == Ctrl_Q)
941 {
942 ins_ctrl_v();
943 c = Ctrl_V; /* pretend CTRL-V is last typed character */
944 continue;
945 }
946
947#ifdef FEAT_CINDENT
948 if (cindent_on()
949# ifdef FEAT_INS_EXPAND
950 && ctrl_x_mode == 0
951# endif
952 )
953 {
954 /* A key name preceded by a bang means this key is not to be
955 * inserted. Skip ahead to the re-indenting below.
956 * A key name preceded by a star means that indenting has to be
957 * done before inserting the key. */
958 line_is_white = inindent(0);
959 if (in_cinkeys(c, '!', line_is_white))
960 goto force_cindent;
961 if (can_cindent && in_cinkeys(c, '*', line_is_white)
962 && stop_arrow() == OK)
963 do_c_expr_indent();
964 }
965#endif
966
967#ifdef FEAT_RIGHTLEFT
968 if (curwin->w_p_rl)
969 switch (c)
970 {
971 case K_LEFT: c = K_RIGHT; break;
972 case K_S_LEFT: c = K_S_RIGHT; break;
973 case K_C_LEFT: c = K_C_RIGHT; break;
974 case K_RIGHT: c = K_LEFT; break;
975 case K_S_RIGHT: c = K_S_LEFT; break;
976 case K_C_RIGHT: c = K_C_LEFT; break;
977 }
978#endif
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 /*
981 * If 'keymodel' contains "startsel", may start selection. If it
982 * does, a CTRL-O and c will be stuffed, we need to get these
983 * characters.
984 */
985 if (ins_start_select(c))
986 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 /*
989 * The big switch to handle a character in insert mode.
990 */
991 switch (c)
992 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000993 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (echeck_abbr(ESC + ABBR_OFF))
995 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200996 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000998 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#ifdef FEAT_CMDWIN
1000 if (c == Ctrl_C && cmdwin_type != 0)
1001 {
1002 /* Close the cmdline window. */
1003 cmdwin_result = K_IGNORE;
1004 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001005 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 goto doESCkey;
1007 }
1008#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001009#ifdef FEAT_JOB_CHANNEL
1010 if (c == Ctrl_C && bt_prompt(curbuf))
1011 {
1012 if (invoke_prompt_interrupt())
1013 {
1014 if (!bt_prompt(curbuf))
1015 // buffer changed to a non-prompt buffer, get out of
1016 // Insert mode
1017 goto doESCkey;
1018 break;
1019 }
1020 }
1021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
1023#ifdef UNIX
1024do_intr:
1025#endif
1026 /* when 'insertmode' set, and not halfway a mapping, don't leave
1027 * Insert mode */
1028 if (goto_im())
1029 {
1030 if (got_int)
1031 {
1032 (void)vgetc(); /* flush all buffers */
1033 got_int = FALSE;
1034 }
1035 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001036 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 break;
1038 }
1039doESCkey:
1040 /*
1041 * This is the ONLY return from edit()!
1042 */
1043 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1044 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001045 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 o_lnum = curwin->w_cursor.lnum;
1047
Bram Moolenaar488c6512005-08-11 20:09:58 +00001048 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001049 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001050 // When CTRL-C was typed got_int will be set, with the result
1051 // that the autocommands won't be executed. When mapped got_int
1052 // is not set, but let's keep the behavior the same.
1053 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001054 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001055 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 continue;
1059
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001060 case Ctrl_Z: /* suspend when 'insertmode' set */
1061 if (!p_im)
1062 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001063 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001064#ifdef CURSOR_SHAPE
1065 ui_cursor_shape(); /* may need to update cursor shape */
1066#endif
1067 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001068
1069 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001070#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001071 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 goto docomplete;
1073#endif
1074 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1075 break;
1076 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001077
1078#ifdef FEAT_VIRTUALEDIT
1079 /* don't move the cursor left when 'virtualedit' has "onemore". */
1080 if (ve_flags & VE_ONEMORE)
1081 {
1082 ins_at_eol = FALSE;
1083 nomove = TRUE;
1084 }
1085#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 count = 0;
1087 goto doESCkey;
1088
Bram Moolenaar572cb562005-08-05 21:35:02 +00001089 case K_INS: /* toggle insert/replace mode */
1090 case K_KINS:
1091 ins_insert(replaceState);
1092 break;
1093
1094 case K_SELECT: /* end of Select mode mapping - ignore */
1095 break;
1096
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001097 case K_HELP: /* Help key works like <ESC> <Help> */
1098 case K_F1:
1099 case K_XF1:
1100 stuffcharReadbuff(K_HELP);
1101 if (p_im)
1102 need_start_insertmode = TRUE;
1103 goto doESCkey;
1104
1105#ifdef FEAT_NETBEANS_INTG
1106 case K_F21: /* NetBeans command */
1107 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001108 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 --no_mapping;
1110 netbeans_keycommand(i);
1111 break;
1112#endif
1113
1114 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 case NUL:
1116 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 /* For ^@ the trailing ESC will end the insert, unless there is an
1118 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1120 && c != Ctrl_A && !p_im)
1121 goto doESCkey; /* quit insert mode */
1122 inserted_space = FALSE;
1123 break;
1124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001125 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 ins_reg();
1127 auto_format(FALSE, TRUE);
1128 inserted_space = FALSE;
1129 break;
1130
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001131 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 ins_ctrl_g();
1133 break;
1134
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 case Ctrl_HAT: /* switch input mode and/or langmap */
1136 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 break;
1138
1139#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001140 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 if (!p_ari)
1142 goto normalchar;
1143 ins_ctrl_();
1144 break;
1145#endif
1146
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001147 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1149 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1150 goto docomplete;
1151#endif
1152 /* FALLTHROUGH */
1153
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001154 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155# ifdef FEAT_INS_EXPAND
1156 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1157 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 if (has_compl_option(FALSE))
1159 goto docomplete;
1160 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 }
1162# endif
1163 ins_shift(c, lastc);
1164 auto_format(FALSE, TRUE);
1165 inserted_space = FALSE;
1166 break;
1167
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001168 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 case K_KDEL:
1170 ins_del();
1171 auto_format(FALSE, TRUE);
1172 break;
1173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 case Ctrl_H:
1176 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1177 auto_format(FALSE, TRUE);
1178 break;
1179
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001181#ifdef FEAT_JOB_CHANNEL
1182 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1183 {
1184 // In a prompt window CTRL-W is used for window commands.
1185 // Use Shift-CTRL-W to delete a word.
1186 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001187 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001188 nomove = TRUE;
1189 count = 0;
1190 goto doESCkey;
1191 }
1192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1194 auto_format(FALSE, TRUE);
1195 break;
1196
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001197 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001198# ifdef FEAT_COMPL_FUNC
1199 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001200 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001201 goto docomplete;
1202# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1204 auto_format(FALSE, TRUE);
1205 inserted_space = FALSE;
1206 break;
1207
1208#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001209 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 case K_LEFTMOUSE_NM:
1211 case K_LEFTDRAG:
1212 case K_LEFTRELEASE:
1213 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001214 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 case K_MIDDLEMOUSE:
1216 case K_MIDDLEDRAG:
1217 case K_MIDDLERELEASE:
1218 case K_RIGHTMOUSE:
1219 case K_RIGHTDRAG:
1220 case K_RIGHTRELEASE:
1221 case K_X1MOUSE:
1222 case K_X1DRAG:
1223 case K_X1RELEASE:
1224 case K_X2MOUSE:
1225 case K_X2DRAG:
1226 case K_X2RELEASE:
1227 ins_mouse(c);
1228 break;
1229
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001230 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001231 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 break;
1233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001235 ins_mousescroll(MSCR_UP);
1236 break;
1237
1238 case K_MOUSELEFT: /* Scroll wheel left */
1239 ins_mousescroll(MSCR_LEFT);
1240 break;
1241
1242 case K_MOUSERIGHT: /* Scroll wheel right */
1243 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 break;
1245#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001246 case K_PS:
1247 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1248 if (cmdchar == K_PS)
1249 /* invoked from normal mode, bail out */
1250 goto doESCkey;
1251 break;
1252 case K_PE:
1253 /* Got K_PE without K_PS, ignore. */
1254 break;
1255
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001256#ifdef FEAT_GUI_TABLINE
1257 case K_TABLINE:
1258 case K_TABMENU:
1259 ins_tabline(c);
1260 break;
1261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 break;
1265
Bram Moolenaar754b5602006-02-09 23:53:20 +00001266 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001267 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001268 did_cursorhold = TRUE;
1269 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001270
Bram Moolenaar4770d092006-01-12 23:22:24 +00001271#ifdef FEAT_GUI_W32
1272 /* On Win32 ignore <M-F4>, we get it when closing the window was
1273 * cancelled. */
1274 case K_F4:
1275 if (mod_mask != MOD_MASK_ALT)
1276 goto normalchar;
1277 break;
1278#endif
1279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280#ifdef FEAT_GUI
1281 case K_VER_SCROLLBAR:
1282 ins_scroll();
1283 break;
1284
1285 case K_HOR_SCROLLBAR:
1286 ins_horscroll();
1287 break;
1288#endif
1289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001290 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 case K_S_HOME:
1293 case K_C_HOME:
1294 ins_home(c);
1295 break;
1296
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001297 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 case K_S_END:
1300 case K_C_END:
1301 ins_end(c);
1302 break;
1303
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001304 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001305 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1306 ins_s_left();
1307 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001308 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 break;
1310
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001311 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 case K_C_LEFT:
1313 ins_s_left();
1314 break;
1315
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001316 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001317 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1318 ins_s_right();
1319 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001320 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 break;
1322
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001323 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 case K_C_RIGHT:
1325 ins_s_right();
1326 break;
1327
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001328 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001329#ifdef FEAT_INS_EXPAND
1330 if (pum_visible())
1331 goto docomplete;
1332#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001333 if (mod_mask & MOD_MASK_SHIFT)
1334 ins_pageup();
1335 else
1336 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 break;
1338
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 case K_PAGEUP:
1341 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001342#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001343 if (pum_visible())
1344 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 ins_pageup();
1347 break;
1348
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001349 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001350#ifdef FEAT_INS_EXPAND
1351 if (pum_visible())
1352 goto docomplete;
1353#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001354 if (mod_mask & MOD_MASK_SHIFT)
1355 ins_pagedown();
1356 else
1357 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 break;
1359
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001360 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 case K_PAGEDOWN:
1362 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001363#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001364 if (pum_visible())
1365 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 ins_pagedown();
1368 break;
1369
1370#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001371 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 ins_drop();
1373 break;
1374#endif
1375
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001376 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 c = TAB;
1378 /* FALLTHROUGH */
1379
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001380 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1382 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1383 goto docomplete;
1384#endif
1385 inserted_space = FALSE;
1386 if (ins_tab())
1387 goto normalchar; /* insert TAB as a normal char */
1388 auto_format(FALSE, TRUE);
1389 break;
1390
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001391 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 c = CAR;
1393 /* FALLTHROUGH */
1394 case CAR:
1395 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001396#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 /* In a quickfix window a <CR> jumps to the error under the
1398 * cursor. */
1399 if (bt_quickfix(curbuf) && c == CAR)
1400 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001401 if (curwin->w_llist_ref == NULL) /* quickfix window */
1402 do_cmdline_cmd((char_u *)".cc");
1403 else /* location list window */
1404 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 break;
1406 }
1407#endif
1408#ifdef FEAT_CMDWIN
1409 if (cmdwin_type != 0)
1410 {
1411 /* Execute the command in the cmdline window. */
1412 cmdwin_result = CAR;
1413 goto doESCkey;
1414 }
1415#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001416#ifdef FEAT_JOB_CHANNEL
1417 if (bt_prompt(curbuf))
1418 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001419 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001420 if (!bt_prompt(curbuf))
1421 // buffer changed to a non-prompt buffer, get out of
1422 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001423 goto doESCkey;
1424 break;
1425 }
1426#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001427 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 goto doESCkey; /* out of memory */
1429 auto_format(FALSE, FALSE);
1430 inserted_space = FALSE;
1431 break;
1432
Bram Moolenaar860cae12010-06-05 23:22:07 +02001433#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001434 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435# ifdef FEAT_INS_EXPAND
1436 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1437 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001438 if (has_compl_option(TRUE))
1439 goto docomplete;
1440 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442# endif
1443# ifdef FEAT_DIGRAPHS
1444 c = ins_digraph();
1445 if (c == NUL)
1446 break;
1447# endif
1448 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001452 case Ctrl_X: /* Enter CTRL-X mode */
1453 ins_ctrl_x();
1454 break;
1455
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001456 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 if (ctrl_x_mode != CTRL_X_TAGS)
1458 goto normalchar;
1459 goto docomplete;
1460
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001461 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (ctrl_x_mode != CTRL_X_FILES)
1463 goto normalchar;
1464 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001465
1466 case 's': /* Spelling completion after ^X */
1467 case Ctrl_S:
1468 if (ctrl_x_mode != CTRL_X_SPELL)
1469 goto normalchar;
1470 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471#endif
1472
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001473 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474#ifdef FEAT_INS_EXPAND
1475 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1476#endif
1477 {
1478 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1479 if (p_im)
1480 {
1481 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1482 break;
1483 goto doESCkey;
1484 }
1485 goto normalchar;
1486 }
1487#ifdef FEAT_INS_EXPAND
1488 /* FALLTHROUGH */
1489
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001490 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 case Ctrl_N:
1492 /* if 'complete' is empty then plain ^P is no longer special,
1493 * but it is under other ^X modes */
1494 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001495 && (ctrl_x_mode == CTRL_X_NORMAL
1496 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001497 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 goto normalchar;
1499
1500docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001501 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001502#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001503 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001504#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001505 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001506 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001507#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001508 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001509#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001510 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 break;
1512#endif /* FEAT_INS_EXPAND */
1513
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001514 case Ctrl_Y: /* copy from previous line or scroll down */
1515 case Ctrl_E: /* copy from next line or scroll up */
1516 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 break;
1518
1519 default:
1520#ifdef UNIX
1521 if (c == intr_char) /* special interrupt char */
1522 goto do_intr;
1523#endif
1524
Bram Moolenaare659c952011-05-19 17:25:41 +02001525normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001527 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001529#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001530 if (!p_paste)
1531 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001532 /* Trigger InsertCharPre. */
1533 char_u *str = do_insert_char_pre(c);
1534 char_u *p;
1535
1536 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001537 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001538 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001539 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001540 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001541 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001542 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001543 c = PTR2CHAR(p);
1544 if (c == CAR || c == K_KENTER || c == NL)
1545 ins_eol(c);
1546 else
1547 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001548 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001549 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001550 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001551 vim_free(str);
1552 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001553 }
1554
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001555 /* If the new value is already inserted or an empty string
1556 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001557 if (c == NUL)
1558 break;
1559 }
1560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561#ifdef FEAT_SMARTINDENT
1562 /* Try to perform smart-indenting. */
1563 ins_try_si(c);
1564#endif
1565
1566 if (c == ' ')
1567 {
1568 inserted_space = TRUE;
1569#ifdef FEAT_CINDENT
1570 if (inindent(0))
1571 can_cindent = FALSE;
1572#endif
1573 if (Insstart_blank_vcol == MAXCOL
1574 && curwin->w_cursor.lnum == Insstart.lnum)
1575 Insstart_blank_vcol = get_nolist_virtcol();
1576 }
1577
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001578 /* Insert a normal character and check for abbreviations on a
1579 * special character. Let CTRL-] expand abbreviations without
1580 * inserting it. */
1581 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582#ifdef FEAT_MBYTE
1583 /* Add ABBR_OFF for characters above 0x100, this is
1584 * what check_abbr() expects. */
1585 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1586#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001587 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 {
1589 insert_special(c, FALSE, FALSE);
1590#ifdef FEAT_RIGHTLEFT
1591 revins_legal++;
1592 revins_chars++;
1593#endif
1594 }
1595
1596 auto_format(FALSE, TRUE);
1597
1598#ifdef FEAT_FOLDING
1599 /* When inserting a character the cursor line must never be in a
1600 * closed fold. */
1601 foldOpenCursor();
1602#endif
1603 break;
1604 } /* end of switch (c) */
1605
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001606 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001607 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001608#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001609 /* but not in CTRL-X mode, a script can't restore the state */
1610 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001611#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001612 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001613 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 /* If the cursor was moved we didn't just insert a space */
1616 if (arrow_used)
1617 inserted_space = FALSE;
1618
1619#ifdef FEAT_CINDENT
1620 if (can_cindent && cindent_on()
1621# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001622 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623# endif
1624 )
1625 {
1626force_cindent:
1627 /*
1628 * Indent now if a key was typed that is in 'cinkeys'.
1629 */
1630 if (in_cinkeys(c, ' ', line_is_white))
1631 {
1632 if (stop_arrow() == OK)
1633 /* re-indent the current line */
1634 do_c_expr_indent();
1635 }
1636 }
1637#endif /* FEAT_CINDENT */
1638
1639 } /* for (;;) */
1640 /* NOTREACHED */
1641}
1642
1643/*
1644 * Redraw for Insert mode.
1645 * This is postponed until getting the next character to make '$' in the 'cpo'
1646 * option work correctly.
1647 * Only redraw when there are no characters available. This speeds up
1648 * inserting sequences of characters (e.g., for CTRL-R).
1649 */
1650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001651ins_redraw(
1652 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001654#ifdef FEAT_CONCEAL
1655 linenr_T conceal_old_cursor_line = 0;
1656 linenr_T conceal_new_cursor_line = 0;
1657 int conceal_update_lines = FALSE;
1658#endif
1659
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001660 if (char_avail())
1661 return;
1662
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001663#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001664 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1665 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001666 if (ready && (has_cursormovedI()
1667# if defined(FEAT_CONCEAL)
1668 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001669# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001670 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001671 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001672# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001673 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001674# endif
1675 )
1676 {
1677# ifdef FEAT_SYN_HL
1678 /* Need to update the screen first, to make sure syntax
1679 * highlighting is correct after making a change (e.g., inserting
1680 * a "(". The autocommand may also require a redraw, so it's done
1681 * again below, unfortunately. */
1682 if (syntax_present(curwin) && must_redraw)
1683 update_screen(0);
1684# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001685 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001686 {
1687 /* Make sure curswant is correct, an autocommand may call
1688 * getcurpos(). */
1689 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001690 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001691 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001692# ifdef FEAT_CONCEAL
1693 if (curwin->w_p_cole > 0)
1694 {
1695 conceal_old_cursor_line = last_cursormoved.lnum;
1696 conceal_new_cursor_line = curwin->w_cursor.lnum;
1697 conceal_update_lines = TRUE;
1698 }
1699# endif
1700 last_cursormoved = curwin->w_cursor;
1701 }
1702#endif
1703
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001704 /* Trigger TextChangedI if b_changedtick differs. */
1705 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001706 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001707#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001708 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001709#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001710 )
1711 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001712 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001713 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001714
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001715 // save and restore curwin and curbuf, in case the autocmd changes them
1716 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001717 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001718 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001719 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001720 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1721 u_save(curwin->w_cursor.lnum,
1722 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001724
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001725#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001726 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1727 * TextChangedI will need to trigger for backwards compatibility, thus use
1728 * different b_last_changedtick* variables. */
1729 if (ready && has_textchangedP()
1730 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1731 && pum_visible())
1732 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001733 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001734 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001735
1736 // save and restore curwin and curbuf, in case the autocmd changes them
1737 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001738 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001739 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001740 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001741 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1742 u_save(curwin->w_cursor.lnum,
1743 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001744 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001745#endif
1746
1747 if (must_redraw)
1748 update_screen(0);
1749 else if (clear_cmdline || redraw_cmdline)
1750 showmode(); /* clear cmdline and show mode */
1751# if defined(FEAT_CONCEAL)
1752 if ((conceal_update_lines
1753 && (conceal_old_cursor_line != conceal_new_cursor_line
1754 || conceal_cursor_line(curwin)))
1755 || need_cursor_line_redraw)
1756 {
1757 if (conceal_old_cursor_line != conceal_new_cursor_line)
1758 update_single_line(curwin, conceal_old_cursor_line);
1759 update_single_line(curwin, conceal_new_cursor_line == 0
1760 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1761 curwin->w_valid &= ~VALID_CROW;
1762 }
1763# endif
1764 showruler(FALSE);
1765 setcursor();
1766 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767}
1768
1769/*
1770 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1771 */
1772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001773ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001776 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001779 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
1781 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001782 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001784 did_putchar = TRUE;
1785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1787
1788#ifdef FEAT_CMDL_INFO
1789 add_to_showcmd_c(Ctrl_V);
1790#endif
1791
1792 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001793 if (did_putchar)
1794 /* when the line fits in 'columns' the '^' is at the start of the next
1795 * line and will not removed by the redraw */
1796 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797#ifdef FEAT_CMDL_INFO
1798 clear_showcmd();
1799#endif
1800 insert_special(c, FALSE, TRUE);
1801#ifdef FEAT_RIGHTLEFT
1802 revins_chars++;
1803 revins_legal++;
1804#endif
1805}
1806
1807/*
1808 * Put a character directly onto the screen. It's not stored in a buffer.
1809 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1810 */
1811static int pc_status;
1812#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1813#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1814#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1815#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817static int pc_attr;
1818static int pc_row;
1819static int pc_col;
1820
1821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 int attr;
1825
1826 if (ScreenLines != NULL)
1827 {
1828 update_topline(); /* just in case w_topline isn't valid */
1829 validate_cursor();
1830 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001831 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 else
1833 attr = 0;
1834 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001835 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1837 pc_status = PC_STATUS_UNSET;
1838#endif
1839#ifdef FEAT_RIGHTLEFT
1840 if (curwin->w_p_rl)
1841 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001842 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843# ifdef FEAT_MBYTE
1844 if (has_mbyte)
1845 {
1846 int fix_col = mb_fix_col(pc_col, pc_row);
1847
1848 if (fix_col != pc_col)
1849 {
1850 screen_putchar(' ', pc_row, fix_col, attr);
1851 --curwin->w_wcol;
1852 pc_status = PC_STATUS_RIGHT;
1853 }
1854 }
1855# endif
1856 }
1857 else
1858#endif
1859 {
1860 pc_col += curwin->w_wcol;
1861#ifdef FEAT_MBYTE
1862 if (mb_lefthalve(pc_row, pc_col))
1863 pc_status = PC_STATUS_LEFT;
1864#endif
1865 }
1866
1867 /* save the character to be able to put it back */
1868#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1869 if (pc_status == PC_STATUS_UNSET)
1870#endif
1871 {
1872 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1873 pc_status = PC_STATUS_SET;
1874 }
1875 screen_putchar(c, pc_row, pc_col, attr);
1876 }
1877}
1878
Bram Moolenaarf2732452018-06-03 14:47:35 +02001879#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1880/*
1881 * Return the effective prompt for the current buffer.
1882 */
1883 char_u *
1884prompt_text(void)
1885{
1886 if (curbuf->b_prompt_text == NULL)
1887 return (char_u *)"% ";
1888 return curbuf->b_prompt_text;
1889}
1890
1891/*
1892 * Prepare for prompt mode: Make sure the last line has the prompt text.
1893 * Move the cursor to this line.
1894 */
1895 static void
1896init_prompt(int cmdchar_todo)
1897{
1898 char_u *prompt = prompt_text();
1899 char_u *text;
1900
1901 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1902 text = ml_get_curline();
1903 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1904 {
1905 // prompt is missing, insert it or append a line with it
1906 if (*text == NUL)
1907 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1908 else
1909 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1910 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1911 coladvance((colnr_T)MAXCOL);
1912 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1913 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001914
1915 // Insert always starts after the prompt, allow editing text after it.
1916 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1917 || Insstart_orig.col != (int)STRLEN(prompt))
1918 {
1919 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001920 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001921 Insstart_orig = Insstart;
1922 Insstart_textlen = Insstart.col;
1923 Insstart_blank_vcol = MAXCOL;
1924 arrow_used = FALSE;
1925 }
1926
Bram Moolenaarf2732452018-06-03 14:47:35 +02001927 if (cmdchar_todo == 'A')
1928 coladvance((colnr_T)MAXCOL);
1929 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001930 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001931 /* Make sure the cursor is in a valid position. */
1932 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001933}
1934
1935/*
1936 * Return TRUE if the cursor is in the editable position of the prompt line.
1937 */
1938 int
1939prompt_curpos_editable()
1940{
1941 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1942 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1943}
1944#endif
1945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946/*
1947 * Undo the previous edit_putchar().
1948 */
1949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001950edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951{
1952 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1953 {
1954#if defined(FEAT_MBYTE)
1955 if (pc_status == PC_STATUS_RIGHT)
1956 ++curwin->w_wcol;
1957 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001958 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 else
1960#endif
1961 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1962 }
1963}
1964
1965/*
1966 * Called when p_dollar is set: display a '$' at the end of the changed text
1967 * Only works when cursor is in the line that changes.
1968 */
1969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001970display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
1972 colnr_T save_col;
1973
1974 if (!redrawing())
1975 return;
1976
1977 cursor_off();
1978 save_col = curwin->w_cursor.col;
1979 curwin->w_cursor.col = col;
1980#ifdef FEAT_MBYTE
1981 if (has_mbyte)
1982 {
1983 char_u *p;
1984
1985 /* If on the last byte of a multi-byte move to the first byte. */
1986 p = ml_get_curline();
1987 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1988 }
1989#endif
1990 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001991 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
1993 edit_putchar('$', FALSE);
1994 dollar_vcol = curwin->w_virtcol;
1995 }
1996 curwin->w_cursor.col = save_col;
1997}
1998
1999/*
2000 * Call this function before moving the cursor from the normal insert position
2001 * in insert mode.
2002 */
2003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002004undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002006 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002008 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002009 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011}
2012
2013/*
2014 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
2015 * Keep the cursor on the same character.
2016 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2017 * type == INDENT_DEC decrease indent (for CTRL-D)
2018 * type == INDENT_SET set indent to "amount"
2019 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2020 */
2021 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002022change_indent(
2023 int type,
2024 int amount,
2025 int round,
2026 int replaced, /* replaced character, put on replace stack */
2027 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
2029 int vcol;
2030 int last_vcol;
2031 int insstart_less; /* reduction for Insstart.col */
2032 int new_cursor_col;
2033 int i;
2034 char_u *ptr;
2035 int save_p_list;
2036 int start_col;
2037 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 colnr_T orig_col = 0; /* init for GCC */
2039 char_u *new_line, *orig_line = NULL; /* init for GCC */
2040
2041 /* VREPLACE mode needs to know what the line was like before changing */
2042 if (State & VREPLACE_FLAG)
2043 {
2044 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2045 orig_col = curwin->w_cursor.col;
2046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
2048 /* for the following tricks we don't want list mode */
2049 save_p_list = curwin->w_p_list;
2050 curwin->w_p_list = FALSE;
2051 vc = getvcol_nolist(&curwin->w_cursor);
2052 vcol = vc;
2053
2054 /*
2055 * For Replace mode we need to fix the replace stack later, which is only
2056 * possible when the cursor is in the indent. Remember the number of
2057 * characters before the cursor if it's possible.
2058 */
2059 start_col = curwin->w_cursor.col;
2060
2061 /* determine offset from first non-blank */
2062 new_cursor_col = curwin->w_cursor.col;
2063 beginline(BL_WHITE);
2064 new_cursor_col -= curwin->w_cursor.col;
2065
2066 insstart_less = curwin->w_cursor.col;
2067
2068 /*
2069 * If the cursor is in the indent, compute how many screen columns the
2070 * cursor is to the left of the first non-blank.
2071 */
2072 if (new_cursor_col < 0)
2073 vcol = get_indent() - vcol;
2074
2075 if (new_cursor_col > 0) /* can't fix replace stack */
2076 start_col = -1;
2077
2078 /*
2079 * Set the new indent. The cursor will be put on the first non-blank.
2080 */
2081 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002082 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 else
2084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 int save_State = State;
2086
2087 /* Avoid being called recursively. */
2088 if (State & VREPLACE_FLAG)
2089 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002090 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093 insstart_less -= curwin->w_cursor.col;
2094
2095 /*
2096 * Try to put cursor on same character.
2097 * If the cursor is at or after the first non-blank in the line,
2098 * compute the cursor column relative to the column of the first
2099 * non-blank character.
2100 * If we are not in insert mode, leave the cursor on the first non-blank.
2101 * If the cursor is before the first non-blank, position it relative
2102 * to the first non-blank, counted in screen columns.
2103 */
2104 if (new_cursor_col >= 0)
2105 {
2106 /*
2107 * When changing the indent while the cursor is touching it, reset
2108 * Insstart_col to 0.
2109 */
2110 if (new_cursor_col == 0)
2111 insstart_less = MAXCOL;
2112 new_cursor_col += curwin->w_cursor.col;
2113 }
2114 else if (!(State & INSERT))
2115 new_cursor_col = curwin->w_cursor.col;
2116 else
2117 {
2118 /*
2119 * Compute the screen column where the cursor should be.
2120 */
2121 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002122 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
2124 /*
2125 * Advance the cursor until we reach the right screen column.
2126 */
2127 vcol = last_vcol = 0;
2128 new_cursor_col = -1;
2129 ptr = ml_get_curline();
2130 while (vcol <= (int)curwin->w_virtcol)
2131 {
2132 last_vcol = vcol;
2133#ifdef FEAT_MBYTE
2134 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002135 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 else
2137#endif
2138 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002139 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
2141 vcol = last_vcol;
2142
2143 /*
2144 * May need to insert spaces to be able to position the cursor on
2145 * the right screen column.
2146 */
2147 if (vcol != (int)curwin->w_virtcol)
2148 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002149 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002151 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (ptr != NULL)
2153 {
2154 new_cursor_col += i;
2155 ptr[i] = NUL;
2156 while (--i >= 0)
2157 ptr[i] = ' ';
2158 ins_str(ptr);
2159 vim_free(ptr);
2160 }
2161 }
2162
2163 /*
2164 * When changing the indent while the cursor is in it, reset
2165 * Insstart_col to 0.
2166 */
2167 insstart_less = MAXCOL;
2168 }
2169
2170 curwin->w_p_list = save_p_list;
2171
2172 if (new_cursor_col <= 0)
2173 curwin->w_cursor.col = 0;
2174 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002175 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 curwin->w_set_curswant = TRUE;
2177 changed_cline_bef_curs();
2178
2179 /*
2180 * May have to adjust the start of the insert.
2181 */
2182 if (State & INSERT)
2183 {
2184 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2185 {
2186 if ((int)Insstart.col <= insstart_less)
2187 Insstart.col = 0;
2188 else
2189 Insstart.col -= insstart_less;
2190 }
2191 if ((int)ai_col <= insstart_less)
2192 ai_col = 0;
2193 else
2194 ai_col -= insstart_less;
2195 }
2196
2197 /*
2198 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2199 * If the number of characters before the cursor decreased, need to pop a
2200 * few characters from the replace stack.
2201 * If the number of characters before the cursor increased, need to push a
2202 * few NULs onto the replace stack.
2203 */
2204 if (REPLACE_NORMAL(State) && start_col >= 0)
2205 {
2206 while (start_col > (int)curwin->w_cursor.col)
2207 {
2208 replace_join(0); /* remove a NUL from the replace stack */
2209 --start_col;
2210 }
2211 while (start_col < (int)curwin->w_cursor.col || replaced)
2212 {
2213 replace_push(NUL);
2214 if (replaced)
2215 {
2216 replace_push(replaced);
2217 replaced = NUL;
2218 }
2219 ++start_col;
2220 }
2221 }
2222
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 /*
2224 * For VREPLACE mode, we also have to fix the replace stack. In this case
2225 * it is always possible because we backspace over the whole line and then
2226 * put it back again the way we wanted it.
2227 */
2228 if (State & VREPLACE_FLAG)
2229 {
2230 /* If orig_line didn't allocate, just return. At least we did the job,
2231 * even if you can't backspace. */
2232 if (orig_line == NULL)
2233 return;
2234
2235 /* Save new line */
2236 new_line = vim_strsave(ml_get_curline());
2237 if (new_line == NULL)
2238 return;
2239
2240 /* We only put back the new line up to the cursor */
2241 new_line[curwin->w_cursor.col] = NUL;
2242
2243 /* Put back original line */
2244 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2245 curwin->w_cursor.col = orig_col;
2246
2247 /* Backspace from cursor to start of line */
2248 backspace_until_column(0);
2249
2250 /* Insert new stuff into line again */
2251 ins_bytes(new_line);
2252
2253 vim_free(new_line);
2254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255}
2256
2257/*
2258 * Truncate the space at the end of a line. This is to be used only in an
2259 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2260 * modes.
2261 */
2262 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002263truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 int i;
2266
2267 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002268 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
2270 if (State & REPLACE_FLAG)
2271 replace_join(0); /* remove a NUL from the replace stack */
2272 }
2273 line[i + 1] = NUL;
2274}
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276/*
2277 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2278 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002279 * Will attempt not to go before "col" even when there is a composing
2280 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 */
2282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002283backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
2285 while ((int)curwin->w_cursor.col > col)
2286 {
2287 curwin->w_cursor.col--;
2288 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002289 replace_do_bs(col);
2290 else if (!del_char_after_col(col))
2291 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
2293}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002295/*
2296 * Like del_char(), but make sure not to go before column "limit_col".
2297 * Only matters when there are composing characters.
2298 * Return TRUE when something was deleted.
2299 */
2300 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002301del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002302{
2303#ifdef FEAT_MBYTE
2304 if (enc_utf8 && limit_col >= 0)
2305 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002306 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002307
2308 /* Make sure the cursor is at the start of a character, but
2309 * skip forward again when going too far back because of a
2310 * composing character. */
2311 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002312 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002313 {
2314 int l = utf_ptr2len(ml_get_cursor());
2315
2316 if (l == 0) /* end of line */
2317 break;
2318 curwin->w_cursor.col += l;
2319 }
2320 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2321 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002322 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002323 }
2324 else
2325#endif
2326 (void)del_char(FALSE);
2327 return TRUE;
2328}
2329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2331/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002332 * CTRL-X pressed in Insert mode.
2333 */
2334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002335ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002336{
2337 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2338 * CTRL-V works like CTRL-N */
2339 if (ctrl_x_mode != CTRL_X_CMDLINE)
2340 {
2341 /* if the next ^X<> won't ADD nothing, then reset
2342 * compl_cont_status */
2343 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002344 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002345 else
2346 compl_cont_status = 0;
2347 /* We're not sure which CTRL-X mode it will be yet */
2348 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2349 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2350 edit_submode_pre = NULL;
2351 showmode();
2352 }
2353}
2354
2355/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002356 * Whether other than default completion has been selected.
2357 */
2358 int
2359ctrl_x_mode_not_default(void)
2360{
2361 return ctrl_x_mode != CTRL_X_NORMAL;
2362}
2363
2364/*
2365 * Whether CTRL-X was typed without a following character.
2366 */
2367 int
2368ctrl_x_mode_not_defined_yet(void)
2369{
2370 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2371}
2372
2373/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002374 * Return TRUE if the 'dict' or 'tsr' option can be used.
2375 */
2376 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002377has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002378{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002379 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002380# ifdef FEAT_SPELL
2381 && !curwin->w_p_spell
2382# endif
2383 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002384 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2385 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002386 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002387 edit_submode = NULL;
2388 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2389 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002390 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002391 if (emsg_silent == 0)
2392 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002393 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002394 setcursor();
2395 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002396#ifdef FEAT_EVAL
2397 if (!get_vim_var_nr(VV_TESTING))
2398#endif
2399 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002400 }
2401 return FALSE;
2402 }
2403 return TRUE;
2404}
2405
2406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2408 * This depends on the current mode.
2409 */
2410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002411vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01002413 // Always allow ^R - let its results then be checked
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (c == Ctrl_R)
2415 return TRUE;
2416
Bram Moolenaare3226be2005-12-18 22:10:00 +00002417 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002418 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002419 return TRUE;
2420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 switch (ctrl_x_mode)
2422 {
2423 case 0: /* Not in any CTRL-X mode */
2424 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2425 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002426 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2428 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2429 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002430 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002431 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 case CTRL_X_SCROLL:
2433 return (c == Ctrl_Y || c == Ctrl_E);
2434 case CTRL_X_WHOLE_LINE:
2435 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2436 case CTRL_X_FILES:
2437 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2438 case CTRL_X_DICTIONARY:
2439 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2440 case CTRL_X_THESAURUS:
2441 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2442 case CTRL_X_TAGS:
2443 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2444#ifdef FEAT_FIND_ID
2445 case CTRL_X_PATH_PATTERNS:
2446 return (c == Ctrl_P || c == Ctrl_N);
2447 case CTRL_X_PATH_DEFINES:
2448 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2449#endif
2450 case CTRL_X_CMDLINE:
2451 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2452 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002453#ifdef FEAT_COMPL_FUNC
2454 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002455 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002456 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002457 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002458#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002459 case CTRL_X_SPELL:
2460 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002461 case CTRL_X_EVAL:
2462 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002464 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 return FALSE;
2466}
2467
2468/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002469 * Return TRUE when character "c" is part of the item currently being
2470 * completed. Used to decide whether to abandon complete mode when the menu
2471 * is visible.
2472 */
2473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002474ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002475{
2476 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2477 /* When expanding an identifier only accept identifier chars. */
2478 return vim_isIDc(c);
2479
2480 switch (ctrl_x_mode)
2481 {
2482 case CTRL_X_FILES:
2483 /* When expanding file name only accept file name chars. But not
2484 * path separators, so that "proto/<Tab>" expands files in
2485 * "proto", not "proto/" as a whole */
2486 return vim_isfilec(c) && !vim_ispathsep(c);
2487
2488 case CTRL_X_CMDLINE:
2489 case CTRL_X_OMNI:
2490 /* Command line and Omni completion can work with just about any
2491 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002492 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002493
2494 case CTRL_X_WHOLE_LINE:
2495 /* For while line completion a space can be part of the line. */
2496 return vim_isprintc(c);
2497 }
2498 return vim_iswordc(c);
2499}
2500
2501/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002502 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002504 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 * the rest of the word to be in -- webb
2506 */
2507 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002508ins_compl_add_infercase(
2509 char_u *str,
2510 int len,
2511 int icase,
2512 char_u *fname,
2513 int dir,
2514 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002516 char_u *p;
2517 int i, c;
2518 int actual_len; /* Take multi-byte characters */
2519 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002520 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002521 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 int has_lower = FALSE;
2523 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002525 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002527 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaara2993e12007-08-12 14:38:46 +00002529 /* Find actual length of completion. */
2530#ifdef FEAT_MBYTE
2531 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002533 p = str;
2534 actual_len = 0;
2535 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002537 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002538 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
2540 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002541 else
2542#endif
2543 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
Bram Moolenaara2993e12007-08-12 14:38:46 +00002545 /* Find actual length of original text. */
2546#ifdef FEAT_MBYTE
2547 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002549 p = compl_orig_text;
2550 actual_compl_length = 0;
2551 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002553 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002554 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 }
2556 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002557 else
2558#endif
2559 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002561 /* "actual_len" may be smaller than "actual_compl_length" when using
2562 * thesaurus, only use the minimum when comparing. */
2563 min_len = actual_len < actual_compl_length
2564 ? actual_len : actual_compl_length;
2565
Bram Moolenaara2993e12007-08-12 14:38:46 +00002566 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002567 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002568 if (wca != NULL)
2569 {
2570 p = str;
2571 for (i = 0; i < actual_len; ++i)
2572#ifdef FEAT_MBYTE
2573 if (has_mbyte)
2574 wca[i] = mb_ptr2char_adv(&p);
2575 else
2576#endif
2577 wca[i] = *(p++);
2578
2579 /* Rule 1: Were any chars converted to lower? */
2580 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002581 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002582 {
2583#ifdef FEAT_MBYTE
2584 if (has_mbyte)
2585 c = mb_ptr2char_adv(&p);
2586 else
2587#endif
2588 c = *(p++);
2589 if (MB_ISLOWER(c))
2590 {
2591 has_lower = TRUE;
2592 if (MB_ISUPPER(wca[i]))
2593 {
2594 /* Rule 1 is satisfied. */
2595 for (i = actual_compl_length; i < actual_len; ++i)
2596 wca[i] = MB_TOLOWER(wca[i]);
2597 break;
2598 }
2599 }
2600 }
2601
2602 /*
2603 * Rule 2: No lower case, 2nd consecutive letter converted to
2604 * upper case.
2605 */
2606 if (!has_lower)
2607 {
2608 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002609 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002610 {
2611#ifdef FEAT_MBYTE
2612 if (has_mbyte)
2613 c = mb_ptr2char_adv(&p);
2614 else
2615#endif
2616 c = *(p++);
2617 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2618 {
2619 /* Rule 2 is satisfied. */
2620 for (i = actual_compl_length; i < actual_len; ++i)
2621 wca[i] = MB_TOUPPER(wca[i]);
2622 break;
2623 }
2624 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2625 }
2626 }
2627
2628 /* Copy the original case of the part we typed. */
2629 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002630 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002631 {
2632#ifdef FEAT_MBYTE
2633 if (has_mbyte)
2634 c = mb_ptr2char_adv(&p);
2635 else
2636#endif
2637 c = *(p++);
2638 if (MB_ISLOWER(c))
2639 wca[i] = MB_TOLOWER(wca[i]);
2640 else if (MB_ISUPPER(c))
2641 wca[i] = MB_TOUPPER(wca[i]);
2642 }
2643
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002644 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002645 * Generate encoding specific output from wide character array.
2646 * Multi-byte characters can occupy up to five bytes more than
2647 * ASCII characters, and we also need one byte for NUL, so stay
2648 * six bytes away from the edge of IObuff.
2649 */
2650 p = IObuff;
2651 i = 0;
2652 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2653#ifdef FEAT_MBYTE
2654 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002655 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002656 else
2657#endif
2658 *(p++) = wca[i++];
2659 *p = NUL;
2660
2661 vim_free(wca);
2662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002664 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2665 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002667 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668}
2669
2670/*
2671 * Add a match to the list of matches.
2672 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002673 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002674 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002676 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002677ins_compl_add(
2678 char_u *str,
2679 int len,
2680 int icase,
2681 char_u *fname,
2682 char_u **cptext, /* extra text for popup menu or NULL */
2683 int cdir,
2684 int flags,
2685 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002687 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002688 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690 ui_breakcheck();
2691 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002692 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (len < 0)
2694 len = (int)STRLEN(str);
2695
2696 /*
2697 * If the same match is already present, don't add it.
2698 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002699 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002701 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 do
2703 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002704 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002705 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002706 && match->cp_str[len] == NUL)
2707 return NOTDONE;
2708 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002709 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
2711
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002712 /* Remove any popup menu before changing the list of matches. */
2713 ins_compl_del_pum();
2714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 /*
2716 * Allocate a new match structure.
2717 * Copy the values to the new match structure.
2718 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002719 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002721 return FAIL;
2722 match->cp_number = -1;
2723 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002724 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002725 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
2727 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002728 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002730 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002731
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002733 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2735 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002736 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002737 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002738 && compl_curr_match->cp_fname != NULL
2739 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002740 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002741 else if (fname != NULL)
2742 {
2743 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002744 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002747 match->cp_fname = NULL;
2748 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002749
2750 if (cptext != NULL)
2751 {
2752 int i;
2753
2754 for (i = 0; i < CPT_COUNT; ++i)
2755 if (cptext[i] != NULL && *cptext[i] != NUL)
2756 match->cp_text[i] = vim_strsave(cptext[i]);
2757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759 /*
2760 * Link the new match structure in the list of matches.
2761 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002762 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002763 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 else if (dir == FORWARD)
2765 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002766 match->cp_next = compl_curr_match->cp_next;
2767 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 else /* BACKWARD */
2770 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002771 match->cp_next = compl_curr_match;
2772 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002774 if (match->cp_next)
2775 match->cp_next->cp_prev = match;
2776 if (match->cp_prev)
2777 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002779 compl_first_match = match;
2780 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002782 /*
2783 * Find the longest common string if still doing that.
2784 */
2785 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2786 ins_compl_longest_match(match);
2787
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 return OK;
2789}
2790
2791/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002792 * Return TRUE if "str[len]" matches with match->cp_str, considering
2793 * match->cp_icase.
2794 */
2795 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002796ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002797{
2798 if (match->cp_icase)
2799 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2800 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2801}
2802
2803/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002804 * Reduce the longest common string for match "match".
2805 */
2806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002807ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002808{
2809 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002810 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002811 int had_match;
2812
2813 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002814 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002815 /* First match, use it as a whole. */
2816 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002817 if (compl_leader != NULL)
2818 {
2819 had_match = (curwin->w_cursor.col > compl_col);
2820 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002821 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002822 ins_redraw(FALSE);
2823
2824 /* When the match isn't there (to avoid matching itself) remove it
2825 * again after redrawing. */
2826 if (!had_match)
2827 ins_compl_delete();
2828 compl_used_match = FALSE;
2829 }
2830 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002831 else
2832 {
2833 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002834 p = compl_leader;
2835 s = match->cp_str;
2836 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002837 {
2838#ifdef FEAT_MBYTE
2839 if (has_mbyte)
2840 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002841 c1 = mb_ptr2char(p);
2842 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002843 }
2844 else
2845#endif
2846 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002847 c1 = *p;
2848 c2 = *s;
2849 }
2850 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2851 : (c1 != c2))
2852 break;
2853#ifdef FEAT_MBYTE
2854 if (has_mbyte)
2855 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002856 MB_PTR_ADV(p);
2857 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002858 }
2859 else
2860#endif
2861 {
2862 ++p;
2863 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002864 }
2865 }
2866
2867 if (*p != NUL)
2868 {
2869 /* Leader was shortened, need to change the inserted text. */
2870 *p = NUL;
2871 had_match = (curwin->w_cursor.col > compl_col);
2872 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002873 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002874 ins_redraw(FALSE);
2875
2876 /* When the match isn't there (to avoid matching itself) remove it
2877 * again after redrawing. */
2878 if (!had_match)
2879 ins_compl_delete();
2880 }
2881
2882 compl_used_match = FALSE;
2883 }
2884}
2885
2886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 * Add an array of matches to the list of matches.
2888 * Frees matches[].
2889 */
2890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002891ins_compl_add_matches(
2892 int num_matches,
2893 char_u **matches,
2894 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
2896 int i;
2897 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002898 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
Bram Moolenaar572cb562005-08-05 21:35:02 +00002900 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002901 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002902 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002904 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 FreeWild(num_matches, matches);
2906}
2907
2908/* Make the completion list cyclic.
2909 * Return the number of matches (excluding the original).
2910 */
2911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002912ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002914 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 int count = 0;
2916
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002917 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
2919 /*
2920 * Find the end of the list.
2921 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002922 match = compl_first_match;
2923 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002924 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002926 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 ++count;
2928 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002929 match->cp_next = compl_first_match;
2930 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 }
2932 return count;
2933}
2934
Bram Moolenaara94bc432006-03-10 21:42:59 +00002935/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002936 * Set variables that store noselect and noinsert behavior from the
2937 * 'completeopt' value.
2938 */
2939 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002940completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002941{
2942 compl_no_insert = FALSE;
2943 compl_no_select = FALSE;
2944 if (strstr((char *)p_cot, "noselect") != NULL)
2945 compl_no_select = TRUE;
2946 if (strstr((char *)p_cot, "noinsert") != NULL)
2947 compl_no_insert = TRUE;
2948}
2949
2950/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002951 * Start completion for the complete() function.
2952 * "startcol" is where the matched text starts (1 is first column).
2953 * "list" is the list of matches.
2954 */
2955 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002956set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002957{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002958 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002959 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002960
Bram Moolenaara94bc432006-03-10 21:42:59 +00002961 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002962 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002963 ins_compl_prep(' ');
2964 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002965 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002966
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002967 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002968 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002969 startcol = curwin->w_cursor.col;
2970 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002971 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002972 /* compl_pattern doesn't need to be set */
2973 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2974 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002975 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002976 return;
2977
Bram Moolenaare4214502015-03-05 18:08:43 +01002978 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002979
2980 ins_compl_add_list(list);
2981 compl_matches = ins_compl_make_cyclic();
2982 compl_started = TRUE;
2983 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002984 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002985
2986 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002987 if (compl_no_insert || compl_no_select)
2988 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002989 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002990 if (compl_no_select)
2991 /* Down/Up has no real effect. */
2992 ins_complete(K_UP, FALSE);
2993 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002994 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002995 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002996 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002997
2998 /* Lazily show the popup menu, unless we got interrupted. */
2999 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01003000 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003001 out_flush();
3002}
3003
3004
Bram Moolenaar9372a112005-12-06 19:59:18 +00003005/* "compl_match_array" points the currently displayed list of entries in the
3006 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003007static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003008static int compl_match_arraysize;
3009
3010/*
3011 * Update the screen and when there is any scrolling remove the popup menu.
3012 */
3013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003014ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003015{
3016 int h;
3017
3018 if (compl_match_array != NULL)
3019 {
3020 h = curwin->w_cline_height;
3021 update_screen(0);
3022 if (h != curwin->w_cline_height)
3023 ins_compl_del_pum();
3024 }
3025}
3026
3027/*
3028 * Remove any popup menu.
3029 */
3030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003031ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003032{
3033 if (compl_match_array != NULL)
3034 {
3035 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003036 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003037 }
3038}
3039
3040/*
3041 * Return TRUE if the popup menu should be displayed.
3042 */
3043 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003044pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003045{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003046 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003047 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003048 return FALSE;
3049
3050 /* The display looks bad on a B&W display. */
3051 if (t_colors < 8
3052#ifdef FEAT_GUI
3053 && !gui.in_use
3054#endif
3055 )
3056 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003057 return TRUE;
3058}
3059
3060/*
3061 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003062 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003063 */
3064 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003065pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003066{
3067 compl_T *compl;
3068 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003069
3070 /* Don't display the popup menu if there are no matches or there is only
3071 * one (ignoring the original text). */
3072 compl = compl_first_match;
3073 i = 0;
3074 do
3075 {
3076 if (compl == NULL
3077 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3078 break;
3079 compl = compl->cp_next;
3080 } while (compl != compl_first_match);
3081
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003082 if (strstr((char *)p_cot, "menuone") != NULL)
3083 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003084 return (i >= 2);
3085}
3086
3087/*
3088 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003089 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003090 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003091 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003092ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003093{
3094 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003095 compl_T *shown_compl = NULL;
3096 int did_find_shown_match = FALSE;
3097 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003098 int i;
3099 int cur = -1;
3100 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003101 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003102
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003103 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003104 return;
3105
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003106#if defined(FEAT_EVAL)
3107 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3108 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3109#endif
3110
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003111 /* Update the screen before drawing the popup menu over it. */
3112 update_screen(0);
3113
3114 if (compl_match_array == NULL)
3115 {
3116 /* Need to build the popup menu list. */
3117 compl_match_arraysize = 0;
3118 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003119 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003120 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003121 do
3122 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003123 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3124 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003125 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003126 ++compl_match_arraysize;
3127 compl = compl->cp_next;
3128 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003129 if (compl_match_arraysize == 0)
3130 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003131 compl_match_array = (pumitem_T *)alloc_clear(
3132 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003133 * compl_match_arraysize));
3134 if (compl_match_array != NULL)
3135 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003136 /* If the current match is the original text don't find the first
3137 * match after it, don't highlight anything. */
3138 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3139 shown_match_ok = TRUE;
3140
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003141 i = 0;
3142 compl = compl_first_match;
3143 do
3144 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003145 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3146 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003147 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003148 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003149 if (!shown_match_ok)
3150 {
3151 if (compl == compl_shown_match || did_find_shown_match)
3152 {
3153 /* This item is the shown match or this is the
3154 * first displayed item after the shown match. */
3155 compl_shown_match = compl;
3156 did_find_shown_match = TRUE;
3157 shown_match_ok = TRUE;
3158 }
3159 else
3160 /* Remember this displayed match for when the
3161 * shown match is just below it. */
3162 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003163 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003164 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003165
3166 if (compl->cp_text[CPT_ABBR] != NULL)
3167 compl_match_array[i].pum_text =
3168 compl->cp_text[CPT_ABBR];
3169 else
3170 compl_match_array[i].pum_text = compl->cp_str;
3171 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3172 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3173 if (compl->cp_text[CPT_MENU] != NULL)
3174 compl_match_array[i++].pum_extra =
3175 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003176 else
3177 compl_match_array[i++].pum_extra = compl->cp_fname;
3178 }
3179
3180 if (compl == compl_shown_match)
3181 {
3182 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003183
3184 /* When the original text is the shown match don't set
3185 * compl_shown_match. */
3186 if (compl->cp_flags & ORIGINAL_TEXT)
3187 shown_match_ok = TRUE;
3188
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003189 if (!shown_match_ok && shown_compl != NULL)
3190 {
3191 /* The shown match isn't displayed, set it to the
3192 * previously displayed match. */
3193 compl_shown_match = shown_compl;
3194 shown_match_ok = TRUE;
3195 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003196 }
3197 compl = compl->cp_next;
3198 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003199
3200 if (!shown_match_ok) /* no displayed match at all */
3201 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003202 }
3203 }
3204 else
3205 {
3206 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003207 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003208 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3209 || compl_match_array[i].pum_text
3210 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003211 {
3212 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003213 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003214 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003215 }
3216
3217 if (compl_match_array != NULL)
3218 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003219 /* In Replace mode when a $ is displayed at the end of the line only
3220 * part of the screen would be updated. We do need to redraw here. */
3221 dollar_vcol = -1;
3222
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003223 /* Compute the screen column of the start of the completed text.
3224 * Use the cursor to get all wrapping and other settings right. */
3225 col = curwin->w_cursor.col;
3226 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003227 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003228 curwin->w_cursor.col = col;
3229 }
3230}
3231
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232#define DICT_FIRST (1) /* use just first element in "dict" */
3233#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003236 * Add any identifiers that match the given pattern in the list of dictionary
3237 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 */
3239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003240ins_compl_dictionaries(
3241 char_u *dict_start,
3242 char_u *pat,
3243 int flags, /* DICT_FIRST and/or DICT_EXACT */
3244 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003246 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 char_u *ptr;
3248 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 char_u **files;
3251 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003253 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
Bram Moolenaar0b238792006-03-02 22:49:12 +00003255 if (*dict == NUL)
3256 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003257#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003258 /* When 'dictionary' is empty and spell checking is enabled use
3259 * "spell". */
3260 if (!thesaurus && curwin->w_p_spell)
3261 dict = (char_u *)"spell";
3262 else
3263#endif
3264 return;
3265 }
3266
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003268 if (buf == NULL)
3269 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003270 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 /* If 'infercase' is set, don't use 'smartcase' here */
3273 save_p_scs = p_scs;
3274 if (curbuf->b_p_inf)
3275 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003276
3277 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3278 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003279 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003280 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003281 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003282 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003283 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003284
3285 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003286 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003287 len = STRLEN(pat_esc) + 10;
3288 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003289 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003290 {
3291 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003292 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003293 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003294 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003295 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3296 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003297 vim_free(ptr);
3298 }
3299 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003300 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003301 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003302 if (regmatch.regprog == NULL)
3303 goto theend;
3304 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003305
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3307 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003308 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
3310 /* copy one dictionary file name into buf */
3311 if (flags == DICT_EXACT)
3312 {
3313 count = 1;
3314 files = &dict;
3315 }
3316 else
3317 {
3318 /* Expand wildcards in the dictionary name, but do not allow
3319 * backticks (for security, the 'dict' option may have been set in
3320 * a modeline). */
3321 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003322# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003323 if (!thesaurus && STRCMP(buf, "spell") == 0)
3324 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003325 else
3326# endif
3327 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 || expand_wildcards(1, &buf, &count, &files,
3329 EW_FILE|EW_SILENT) != OK)
3330 count = 0;
3331 }
3332
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003333# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003334 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003336 /* Complete from active spelling. Skip "\<" in the pattern, we
3337 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003338 if (pat[0] == '\\' && pat[1] == '<')
3339 ptr = pat + 2;
3340 else
3341 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003342 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003344 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003345# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003346 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003347 {
3348 ins_compl_files(count, files, thesaurus, flags,
3349 &regmatch, buf, &dir);
3350 if (flags != DICT_EXACT)
3351 FreeWild(count, files);
3352 }
3353 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 break;
3355 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003356
3357theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003359 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 vim_free(buf);
3361}
3362
Bram Moolenaar0b238792006-03-02 22:49:12 +00003363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003364ins_compl_files(
3365 int count,
3366 char_u **files,
3367 int thesaurus,
3368 int flags,
3369 regmatch_T *regmatch,
3370 char_u *buf,
3371 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003372{
3373 char_u *ptr;
3374 int i;
3375 FILE *fp;
3376 int add_r;
3377
3378 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3379 {
3380 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3381 if (flags != DICT_EXACT)
3382 {
3383 vim_snprintf((char *)IObuff, IOSIZE,
3384 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003385 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003386 }
3387
3388 if (fp != NULL)
3389 {
3390 /*
3391 * Read dictionary file line by line.
3392 * Check each line for a match.
3393 */
3394 while (!got_int && !compl_interrupted
3395 && !vim_fgets(buf, LSIZE, fp))
3396 {
3397 ptr = buf;
3398 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3399 {
3400 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003401 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003402 ptr = find_line_end(ptr);
3403 else
3404 ptr = find_word_end(ptr);
3405 add_r = ins_compl_add_infercase(regmatch->startp[0],
3406 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003407 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003408 if (thesaurus)
3409 {
3410 char_u *wstart;
3411
3412 /*
3413 * Add the other matches on the line
3414 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003415 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003416 while (!got_int)
3417 {
3418 /* Find start of the next word. Skip white
3419 * space and punctuation. */
3420 ptr = find_word_start(ptr);
3421 if (*ptr == NUL || *ptr == NL)
3422 break;
3423 wstart = ptr;
3424
Bram Moolenaara2993e12007-08-12 14:38:46 +00003425 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003426#ifdef FEAT_MBYTE
3427 if (has_mbyte)
3428 /* Japanese words may have characters in
3429 * different classes, only separate words
3430 * with single-byte non-word characters. */
3431 while (*ptr != NUL)
3432 {
3433 int l = (*mb_ptr2len)(ptr);
3434
3435 if (l < 2 && !vim_iswordc(*ptr))
3436 break;
3437 ptr += l;
3438 }
3439 else
3440#endif
3441 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003442
3443 /* Add the word. Skip the regexp match. */
3444 if (wstart != regmatch->startp[0])
3445 add_r = ins_compl_add_infercase(wstart,
3446 (int)(ptr - wstart),
3447 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003448 }
3449 }
3450 if (add_r == OK)
3451 /* if dir was BACKWARD then honor it just once */
3452 *dir = FORWARD;
3453 else if (add_r == FAIL)
3454 break;
3455 /* avoid expensive call to vim_regexec() when at end
3456 * of line */
3457 if (*ptr == '\n' || got_int)
3458 break;
3459 }
3460 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003461 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003462 }
3463 fclose(fp);
3464 }
3465 }
3466}
3467
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468/*
3469 * Find the start of the next word.
3470 * Returns a pointer to the first char of the word. Also stops at a NUL.
3471 */
3472 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003473find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
3475#ifdef FEAT_MBYTE
3476 if (has_mbyte)
3477 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003478 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 else
3480#endif
3481 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3482 ++ptr;
3483 return ptr;
3484}
3485
3486/*
3487 * Find the end of the word. Assumes it starts inside a word.
3488 * Returns a pointer to just after the word.
3489 */
3490 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003491find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492{
3493#ifdef FEAT_MBYTE
3494 int start_class;
3495
3496 if (has_mbyte)
3497 {
3498 start_class = mb_get_class(ptr);
3499 if (start_class > 1)
3500 while (*ptr != NUL)
3501 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003502 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 if (mb_get_class(ptr) != start_class)
3504 break;
3505 }
3506 }
3507 else
3508#endif
3509 while (vim_iswordc(*ptr))
3510 ++ptr;
3511 return ptr;
3512}
3513
3514/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003515 * Find the end of the line, omitting CR and NL at the end.
3516 * Returns a pointer to just after the line.
3517 */
3518 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003519find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003520{
3521 char_u *s;
3522
3523 s = ptr + STRLEN(ptr);
3524 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3525 --s;
3526 return s;
3527}
3528
3529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 * Free the list of completions
3531 */
3532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003533ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003535 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003536 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
Bram Moolenaard23a8232018-02-10 18:45:26 +01003538 VIM_CLEAR(compl_pattern);
3539 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003541 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003543
3544 ins_compl_del_pum();
3545 pum_clear();
3546
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003547 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 do
3549 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003550 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003551 compl_curr_match = compl_curr_match->cp_next;
3552 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003554 if (match->cp_flags & FREE_FNAME)
3555 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003556 for (i = 0; i < CPT_COUNT; ++i)
3557 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003559 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3560 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003561 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003562 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563}
3564
3565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003566ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003568 compl_cont_status = 0;
3569 compl_started = FALSE;
3570 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003571 VIM_CLEAR(compl_pattern);
3572 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003574 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003575 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003576 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003577 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578}
3579
3580/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003581 * Return TRUE when Insert completion is active.
3582 */
3583 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003584ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003585{
3586 return compl_started;
3587}
3588
3589/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003590 * Delete one character before the cursor and show the subset of the matches
3591 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003592 * Returns the character to be used, NUL if the work is done and another char
3593 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003594 */
3595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003596ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003597{
3598 char_u *line;
3599 char_u *p;
3600
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003601 line = ml_get_curline();
3602 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003603 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003604
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003605 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003606 * allow the word to be deleted, we won't match everything.
3607 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003608 if ((int)(p - line) - (int)compl_col < 0
3609 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003610 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3611 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3612 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003613 return K_BS;
3614
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003615 /* Deleted more than what was used to find matches or didn't finish
3616 * finding all matches: need to look for matches all over again. */
3617 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003618 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003619 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003620
Bram Moolenaara6557602006-02-04 22:43:20 +00003621 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003622 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003623 if (compl_leader != NULL)
3624 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003625 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003626 if (compl_shown_match != NULL)
3627 /* Make sure current match is not a hidden item. */
3628 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003629 return NUL;
3630 }
3631 return K_BS;
3632}
Bram Moolenaara6557602006-02-04 22:43:20 +00003633
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003634/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003635 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3636 * be called.
3637 */
3638 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003639ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003640{
3641 /* Return TRUE if we didn't complete finding matches or when the
3642 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3643 return compl_was_interrupted
3644 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3645 && compl_opt_refresh_always);
3646}
3647
3648/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003649 * Called after changing "compl_leader".
3650 * Show the popup menu with a different set of matches.
3651 * May also search for matches again if the previous search was interrupted.
3652 */
3653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003654ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003655{
3656 ins_compl_del_pum();
3657 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003658 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003659 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003660
3661 if (compl_started)
3662 ins_compl_set_original_text(compl_leader);
3663 else
3664 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003665#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003666 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003667#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003668 /*
3669 * Matches were cleared, need to search for them now. First display
3670 * the changed text before the cursor. Set "compl_restarting" to
3671 * avoid that the first match is inserted.
3672 */
3673 update_screen(0);
3674#ifdef FEAT_GUI
3675 if (gui.in_use)
3676 {
3677 /* Show the cursor after the match, not after the redrawn text. */
3678 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003679 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003680 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003681#endif
3682 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003683 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003684 compl_cont_status = 0;
3685 compl_restarting = FALSE;
3686 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003687
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003688 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003689
3690 /* Show the popup menu with a different set of matches. */
3691 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003692
3693 /* Don't let Enter select the original text when there is no popup menu. */
3694 if (compl_match_array == NULL)
3695 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003696}
3697
3698/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003699 * Return the length of the completion, from the completion start column to
3700 * the cursor column. Making sure it never goes below zero.
3701 */
3702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003703ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003704{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003705 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003706
3707 if (off < 0)
3708 return 0;
3709 return off;
3710}
3711
3712/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003713 * Append one character to the match leader. May reduce the number of
3714 * matches.
3715 */
3716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003717ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003718{
3719#ifdef FEAT_MBYTE
3720 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003721#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003722
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003723 if (stop_arrow() == FAIL)
3724 return;
3725#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003726 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3727 {
3728 char_u buf[MB_MAXBYTES + 1];
3729
3730 (*mb_char2bytes)(c, buf);
3731 buf[cc] = NUL;
3732 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003733 if (compl_opt_refresh_always)
3734 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003735 }
3736 else
3737#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003738 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003739 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003740 if (compl_opt_refresh_always)
3741 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003742 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003743
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003744 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003745 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003746 ins_compl_restart();
3747
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003748 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003749 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003750 * break redo. */
3751 if (!compl_opt_refresh_always)
3752 {
3753 vim_free(compl_leader);
3754 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003755 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003756 if (compl_leader != NULL)
3757 ins_compl_new_leader();
3758 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003759}
3760
3761/*
3762 * Setup for finding completions again without leaving CTRL-X mode. Used when
3763 * BS or a key was typed while still searching for matches.
3764 */
3765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003766ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003767{
3768 ins_compl_free();
3769 compl_started = FALSE;
3770 compl_matches = 0;
3771 compl_cont_status = 0;
3772 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003773}
3774
3775/*
3776 * Set the first match, the original text.
3777 */
3778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003779ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003780{
3781 char_u *p;
3782
Bram Moolenaare87edf32018-04-17 22:14:32 +02003783 /* Replace the original text entry.
3784 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3785 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003786 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3787 {
3788 p = vim_strsave(str);
3789 if (p != NULL)
3790 {
3791 vim_free(compl_first_match->cp_str);
3792 compl_first_match->cp_str = p;
3793 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003794 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003795 else if (compl_first_match->cp_prev != NULL
3796 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3797 {
3798 p = vim_strsave(str);
3799 if (p != NULL)
3800 {
3801 vim_free(compl_first_match->cp_prev->cp_str);
3802 compl_first_match->cp_prev->cp_str = p;
3803 }
3804 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003805}
3806
3807/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003808 * Append one character to the match leader. May reduce the number of
3809 * matches.
3810 */
3811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003812ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003813{
3814 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003815 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003816 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003817 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003818
3819 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003820 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003821 {
3822 /* When still at the original match use the first entry that matches
3823 * the leader. */
3824 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3825 {
3826 p = NULL;
3827 for (cp = compl_shown_match->cp_next; cp != NULL
3828 && cp != compl_first_match; cp = cp->cp_next)
3829 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003830 if (compl_leader == NULL
3831 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003832 (int)STRLEN(compl_leader)))
3833 {
3834 p = cp->cp_str;
3835 break;
3836 }
3837 }
3838 if (p == NULL || (int)STRLEN(p) <= len)
3839 return;
3840 }
3841 else
3842 return;
3843 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003844 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003845 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003846 ins_compl_addleader(c);
3847}
3848
3849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003851 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003852 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003855ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
3857 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003859 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
3861 /* Forget any previous 'special' messages if this is actually
3862 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3863 */
3864 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3865 edit_submode_extra = NULL;
3866
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003867 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003868 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3869 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003870 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003872 /* Set "compl_get_longest" when finding the first matches. */
3873 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003874 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003875 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003876 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003877 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003878
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003879 }
3880
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3882 {
3883 /*
3884 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3885 * it will be yet. Now we decide.
3886 */
3887 switch (c)
3888 {
3889 case Ctrl_E:
3890 case Ctrl_Y:
3891 ctrl_x_mode = CTRL_X_SCROLL;
3892 if (!(State & REPLACE_FLAG))
3893 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3894 else
3895 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3896 edit_submode_pre = NULL;
3897 showmode();
3898 break;
3899 case Ctrl_L:
3900 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3901 break;
3902 case Ctrl_F:
3903 ctrl_x_mode = CTRL_X_FILES;
3904 break;
3905 case Ctrl_K:
3906 ctrl_x_mode = CTRL_X_DICTIONARY;
3907 break;
3908 case Ctrl_R:
3909 /* Simply allow ^R to happen without affecting ^X mode */
3910 break;
3911 case Ctrl_T:
3912 ctrl_x_mode = CTRL_X_THESAURUS;
3913 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003914#ifdef FEAT_COMPL_FUNC
3915 case Ctrl_U:
3916 ctrl_x_mode = CTRL_X_FUNCTION;
3917 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003918 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003919 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003920 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003921#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003922 case 's':
3923 case Ctrl_S:
3924 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003925#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003926 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003927 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003928 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003929#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 case Ctrl_RSB:
3932 ctrl_x_mode = CTRL_X_TAGS;
3933 break;
3934#ifdef FEAT_FIND_ID
3935 case Ctrl_I:
3936 case K_S_TAB:
3937 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3938 break;
3939 case Ctrl_D:
3940 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3941 break;
3942#endif
3943 case Ctrl_V:
3944 case Ctrl_Q:
3945 ctrl_x_mode = CTRL_X_CMDLINE;
3946 break;
3947 case Ctrl_P:
3948 case Ctrl_N:
3949 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3950 * just started ^X mode, or there were enough ^X's to cancel
3951 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3952 * do normal expansion when interrupting a different mode (say
3953 * ^X^F^X^P or ^P^X^X^P, see below)
3954 * nothing changes if interrupting mode 0, (eg, the flag
3955 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003956 if (!(compl_cont_status & CONT_INTRPT))
3957 compl_cont_status |= CONT_LOCAL;
3958 else if (compl_cont_mode != 0)
3959 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 /* FALLTHROUGH */
3961 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003962 /* If we have typed at least 2 ^X's... for modes != 0, we set
3963 * compl_cont_status = 0 (eg, as if we had just started ^X
3964 * mode).
3965 * For mode 0, we set "compl_cont_mode" to an impossible
3966 * value, in both cases ^X^X can be used to restart the same
3967 * mode (avoiding ADDING mode).
3968 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3969 * 'complete' and local ^P expansions respectively.
3970 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3971 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 if (c == Ctrl_X)
3973 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003974 if (compl_cont_mode != 0)
3975 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003977 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003979 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 edit_submode = NULL;
3981 showmode();
3982 break;
3983 }
3984 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003985 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
3987 /* We're already in CTRL-X mode, do we stay in it? */
3988 if (!vim_is_ctrl_x_key(c))
3989 {
3990 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003991 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 else
3993 ctrl_x_mode = CTRL_X_FINISHED;
3994 edit_submode = NULL;
3995 }
3996 showmode();
3997 }
3998
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003999 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
4001 /* Show error message from attempted keyword completion (probably
4002 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004003 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004005 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
4006 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 || ctrl_x_mode == CTRL_X_FINISHED)
4008 {
4009 /* Get here when we have finished typing a sequence of ^N and
4010 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004011 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004012 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 {
4014 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004015 * If any of the original typed text has been changed, eg when
4016 * ignorecase is set, we must add back-spaces to the redo
4017 * buffer. We add as few as necessary to delete just the part
4018 * of the original text that has changed.
4019 * When using the longest match, edited the match or used
4020 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004022 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4023 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004024 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004025 ptr = NULL;
4026 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028
4029#ifdef FEAT_CINDENT
4030 want_cindent = (can_cindent && cindent_on());
4031#endif
4032 /*
4033 * When completing whole lines: fix indent for 'cindent'.
4034 * Otherwise, break line if it's too long.
4035 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004036 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
4038#ifdef FEAT_CINDENT
4039 /* re-indent the current line */
4040 if (want_cindent)
4041 {
4042 do_c_expr_indent();
4043 want_cindent = FALSE; /* don't do it again */
4044 }
4045#endif
4046 }
4047 else
4048 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004049 int prev_col = curwin->w_cursor.col;
4050
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004052 if (prev_col > 0)
4053 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004054 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004055 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004057 if (prev_col > 0
4058 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4059 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004062 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004063 * the selection without inserting anything. When
4064 * compl_enter_selects is set the Enter key does the same. */
4065 if ((c == Ctrl_Y || (compl_enter_selects
4066 && (c == CAR || c == K_KENTER || c == NL)))
4067 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004068 retval = TRUE;
4069
Bram Moolenaar47247282016-08-02 22:36:02 +02004070 /* CTRL-E means completion is Ended, go back to the typed text.
4071 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004072 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004073 {
4074 ins_compl_delete();
4075 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004076 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004077 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004078 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004079 retval = TRUE;
4080 }
4081
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004082 auto_format(FALSE, TRUE);
4083
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004085 compl_started = FALSE;
4086 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004087 if (!shortmess(SHM_COMPLETIONMENU))
4088 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004089 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004090 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (edit_submode != NULL)
4092 {
4093 edit_submode = NULL;
4094 showmode();
4095 }
4096
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004097#ifdef FEAT_CMDWIN
4098 if (c == Ctrl_C && cmdwin_type != 0)
4099 /* Avoid the popup menu remains displayed when leaving the
4100 * command line window. */
4101 update_screen(0);
4102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103#ifdef FEAT_CINDENT
4104 /*
4105 * Indent now if a key was typed that is in 'cinkeys'.
4106 */
4107 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4108 do_c_expr_indent();
4109#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004110 /* Trigger the CompleteDone event to give scripts a chance to act
4111 * upon the completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004112 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004115 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4116 /* Trigger the CompleteDone event to give scripts a chance to act
4117 * upon the (possibly failed) completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004118 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
4120 /* reset continue_* if we left expansion-mode, if we stay they'll be
4121 * (re)set properly in ins_complete() */
4122 if (!vim_is_ctrl_x_key(c))
4123 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124 compl_cont_status = 0;
4125 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004127
4128 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129}
4130
4131/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004132 * Fix the redo buffer for the completion leader replacing some of the typed
4133 * text. This inserts backspaces and appends the changed text.
4134 * "ptr" is the known leader text or NUL.
4135 */
4136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004137ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004138{
4139 int len;
4140 char_u *p;
4141 char_u *ptr = ptr_arg;
4142
4143 if (ptr == NULL)
4144 {
4145 if (compl_leader != NULL)
4146 ptr = compl_leader;
4147 else
4148 return; /* nothing to do */
4149 }
4150 if (compl_orig_text != NULL)
4151 {
4152 p = compl_orig_text;
4153 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4154 ;
4155#ifdef FEAT_MBYTE
4156 if (len > 0)
4157 len -= (*mb_head_off)(p, p + len);
4158#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004159 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004160 AppendCharToRedobuff(K_BS);
4161 }
4162 else
4163 len = 0;
4164 if (ptr != NULL)
4165 AppendToRedobuffLit(ptr + len, -1);
4166}
4167
4168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4170 * (depending on flag) starting from buf and looking for a non-scanned
4171 * buffer (other than curbuf). curbuf is special, if it is called with
4172 * buf=curbuf then it has to be the first call for a given flag/expansion.
4173 *
4174 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4175 */
4176 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004177ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
4181 if (flag == 'w') /* just windows */
4182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 if (buf == curbuf) /* first call for this flag/expansion */
4184 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004185 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 && wp->w_buffer->b_scanned)
4187 ;
4188 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 else
4191 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4192 * (unlisted buffers)
4193 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004194 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 && ((flag == 'U'
4196 ? buf->b_p_bl
4197 : (!buf->b_p_bl
4198 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004199 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 ;
4201 return buf;
4202}
4203
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004204#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004205/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004206 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004207 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004208 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004210expand_by_function(
4211 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4212 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004213{
Bram Moolenaar82139082011-09-14 16:52:09 +02004214 list_T *matchlist = NULL;
4215 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004216 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004217 char_u *funcname;
4218 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004219 win_T *curwin_save;
4220 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004221 typval_T rettv;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004222 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004223
Bram Moolenaare344bea2005-09-01 20:46:49 +00004224 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4225 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004226 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004227
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004228 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004229 args[0].v_type = VAR_NUMBER;
4230 args[0].vval.v_number = 0;
4231 args[1].v_type = VAR_STRING;
4232 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4233 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004234
Bram Moolenaare344bea2005-09-01 20:46:49 +00004235 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004236 curwin_save = curwin;
4237 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004238
4239 /* Call a function, which returns a list or dict. */
Bram Moolenaarded27a12018-08-01 19:06:03 +02004240 if (call_vim_function(funcname, 2, args, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004241 {
4242 switch (rettv.v_type)
4243 {
4244 case VAR_LIST:
4245 matchlist = rettv.vval.v_list;
4246 break;
4247 case VAR_DICT:
4248 matchdict = rettv.vval.v_dict;
4249 break;
4250 default:
4251 /* TODO: Give error message? */
4252 clear_tv(&rettv);
4253 break;
4254 }
4255 }
4256
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004257 if (curwin_save != curwin || curbuf_save != curbuf)
4258 {
4259 EMSG(_(e_complwin));
4260 goto theend;
4261 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004262 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004263 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004264 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004265 {
4266 EMSG(_(e_compldel));
4267 goto theend;
4268 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004269
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004270 if (matchlist != NULL)
4271 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004272 else if (matchdict != NULL)
4273 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004274
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004275theend:
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004276 // Restore State, it might have been changed.
4277 State = save_State;
4278
Bram Moolenaar82139082011-09-14 16:52:09 +02004279 if (matchdict != NULL)
4280 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004281 if (matchlist != NULL)
4282 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004283}
4284#endif /* FEAT_COMPL_FUNC */
4285
Bram Moolenaar39f05632006-03-19 22:15:26 +00004286#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004287/*
4288 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004289 */
4290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004291ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004292{
4293 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004294 int dir = compl_direction;
4295
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004296 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004297 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004298 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004299 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4300 /* if dir was BACKWARD then honor it just once */
4301 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004302 else if (did_emsg)
4303 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004304 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004305}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004306
4307/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004308 * Add completions from a dict.
4309 */
4310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004311ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004312{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004313 dictitem_T *di_refresh;
4314 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004315
4316 /* Check for optional "refresh" item. */
4317 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004318 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4319 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004320 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004321 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004322
4323 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4324 compl_opt_refresh_always = TRUE;
4325 }
4326
4327 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004328 di_words = dict_find(dict, (char_u *)"words", 5);
4329 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4330 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004331}
4332
4333/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004334 * Add a match to the list of matches from a typeval_T.
4335 * If the given string is already in the list of completions, then return
4336 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4337 * maybe because alloc() returns NULL, then FAIL is returned.
4338 */
4339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004340ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004341{
4342 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004343 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004344 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004345 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004346 char_u *(cptext[CPT_COUNT]);
4347
4348 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4349 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004350 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4351 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004352 (char_u *)"abbr", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004353 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004354 (char_u *)"menu", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004355 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004356 (char_u *)"kind", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004357 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004358 (char_u *)"info", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004359 cptext[CPT_USER_DATA] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004360 (char_u *)"user_data", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004361 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4362 icase = dict_get_number(tv->vval.v_dict, (char_u *)"icase");
4363 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
4364 adup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
4365 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4366 aempty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004367 }
4368 else
4369 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004370 word = tv_get_string_chk(tv);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004371 vim_memset(cptext, 0, sizeof(cptext));
4372 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004373 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004374 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004375 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004376}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004377#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004378
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004379/*
4380 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004381 * The search starts at position "ini" in curbuf and in the direction
4382 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004383 * When "compl_started" is FALSE start at that position, otherwise continue
4384 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004385 * This may return before finding all the matches.
4386 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 */
4388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004389ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
4391 static pos_T first_match_pos;
4392 static pos_T last_match_pos;
4393 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004394 static int found_all = FALSE; /* Found all matches of a
4395 certain type. */
4396 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
Bram Moolenaar572cb562005-08-05 21:35:02 +00004398 pos_T *pos;
4399 char_u **matches;
4400 int save_p_scs;
4401 int save_p_ws;
4402 int save_p_ic;
4403 int i;
4404 int num_matches;
4405 int len;
4406 int found_new_match;
4407 int type = ctrl_x_mode;
4408 char_u *ptr;
4409 char_u *dict = NULL;
4410 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004411 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004415 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 ins_buf->b_scanned = 0;
4417 found_all = FALSE;
4418 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004419 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004420 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 last_match_pos = first_match_pos = *ini;
4422 }
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004423 else if (ins_buf != curbuf && !buf_valid(ins_buf))
4424 ins_buf = curbuf; // In case the buffer was wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
Bram Moolenaar4475b622017-05-01 20:46:52 +02004426 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004427 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004428
4429 /*
4430 * For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 for (;;)
4433 {
4434 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004435 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 * or if found_all says this entry is done. For ^X^L only use the
4439 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004440 if ((ctrl_x_mode == CTRL_X_NORMAL
4441 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
4444 found_all = FALSE;
4445 while (*e_cpt == ',' || *e_cpt == ' ')
4446 e_cpt++;
4447 if (*e_cpt == '.' && !curbuf->b_scanned)
4448 {
4449 ins_buf = curbuf;
4450 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004451 /* Move the cursor back one character so that ^N can match the
4452 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004453 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004454 {
4455 /* Move the cursor to after the last character in the
4456 * buffer, so that word at start of buffer is found
4457 * correctly. */
4458 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4459 first_match_pos.col =
4460 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 last_match_pos = first_match_pos;
4463 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004464
4465 /* Remember the first match so that the loop stops when we
4466 * wrap and come back there a second time. */
4467 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4470 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4471 {
4472 /* Scan a buffer, but not the current one. */
4473 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4474 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004475 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 first_match_pos.col = last_match_pos.col = 0;
4477 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4478 last_match_pos.lnum = 0;
4479 type = 0;
4480 }
4481 else /* unloaded buffer, scan like dictionary */
4482 {
4483 found_all = TRUE;
4484 if (ins_buf->b_fname == NULL)
4485 continue;
4486 type = CTRL_X_DICTIONARY;
4487 dict = ins_buf->b_fname;
4488 dict_f = DICT_EXACT;
4489 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004490 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 ins_buf->b_fname == NULL
4492 ? buf_spname(ins_buf)
4493 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004494 ? ins_buf->b_fname
4495 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004496 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498 else if (*e_cpt == NUL)
4499 break;
4500 else
4501 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004502 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 type = -1;
4504 else if (*e_cpt == 'k' || *e_cpt == 's')
4505 {
4506 if (*e_cpt == 'k')
4507 type = CTRL_X_DICTIONARY;
4508 else
4509 type = CTRL_X_THESAURUS;
4510 if (*++e_cpt != ',' && *e_cpt != NUL)
4511 {
4512 dict = e_cpt;
4513 dict_f = DICT_FIRST;
4514 }
4515 }
4516#ifdef FEAT_FIND_ID
4517 else if (*e_cpt == 'i')
4518 type = CTRL_X_PATH_PATTERNS;
4519 else if (*e_cpt == 'd')
4520 type = CTRL_X_PATH_DEFINES;
4521#endif
4522 else if (*e_cpt == ']' || *e_cpt == 't')
4523 {
4524 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004525 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004526 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528 else
4529 type = -1;
4530
4531 /* in any case e_cpt is advanced to the next entry */
4532 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4533
4534 found_all = TRUE;
4535 if (type == -1)
4536 continue;
4537 }
4538 }
4539
Bram Moolenaar4475b622017-05-01 20:46:52 +02004540 /* If complete() was called then compl_pattern has been reset. The
4541 * following won't work then, bail out. */
4542 if (compl_pattern == NULL)
4543 break;
4544
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 switch (type)
4546 {
4547 case -1:
4548 break;
4549#ifdef FEAT_FIND_ID
4550 case CTRL_X_PATH_PATTERNS:
4551 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004552 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004553 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4557 (linenr_T)1, (linenr_T)MAXLNUM);
4558 break;
4559#endif
4560
4561 case CTRL_X_DICTIONARY:
4562 case CTRL_X_THESAURUS:
4563 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004564 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 : (type == CTRL_X_THESAURUS
4566 ? (*curbuf->b_p_tsr == NUL
4567 ? p_tsr
4568 : curbuf->b_p_tsr)
4569 : (*curbuf->b_p_dict == NUL
4570 ? p_dict
4571 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004572 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004573 dict != NULL ? dict_f
4574 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 dict = NULL;
4576 break;
4577
4578 case CTRL_X_TAGS:
4579 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4580 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004581 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004583 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004584 * of matches is found when compl_pattern is empty */
4585 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004586 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4587 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4589 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004590 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592 p_ic = save_p_ic;
4593 break;
4594
4595 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4598 {
4599
4600 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004601 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004602 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
4604 break;
4605
4606 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004607 if (expand_cmdline(&compl_xp, compl_pattern,
4608 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004610 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 break;
4612
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004613#ifdef FEAT_COMPL_FUNC
4614 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004615 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004616 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004617 break;
4618#endif
4619
Bram Moolenaar488c6512005-08-11 20:09:58 +00004620 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004621#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004622 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004623 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004624 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004625 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004626#endif
4627 break;
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 default: /* normal ^P/^N and ^X^L */
4630 /*
4631 * If 'infercase' is set, don't use 'smartcase' here
4632 */
4633 save_p_scs = p_scs;
4634 if (ins_buf->b_p_inf)
4635 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004636
Bram Moolenaar361aa502014-01-23 22:45:58 +01004637 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 * end but never from the middle, thus setting nowrapscan in this
4639 * buffers is a good idea, on the other hand, we always set
4640 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4641 save_p_ws = p_ws;
4642 if (ins_buf != curbuf)
4643 p_ws = FALSE;
4644 else if (*e_cpt == '.')
4645 p_ws = TRUE;
4646 for (;;)
4647 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004648 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004650 ++msg_silent; /* Don't want messages for wrapscan. */
4651
Bram Moolenaare4214502015-03-05 18:08:43 +01004652 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4653 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004654 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004655 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004656 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004658 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 else
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004660 found_new_match = searchit(NULL, ins_buf, pos, NULL,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004661 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004662 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004663 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004664 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004665 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004667 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004668 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 first_match_pos = *pos;
4670 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004671 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004674 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 found_new_match = FAIL;
4676 if (found_new_match == FAIL)
4677 {
4678 if (ins_buf == curbuf)
4679 found_all = TRUE;
4680 break;
4681 }
4682
4683 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004684 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 && ini->lnum == pos->lnum
4686 && ini->col == pos->col)
4687 continue;
4688 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004689 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004691 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
4693 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4694 continue;
4695 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4696 if (!p_paste)
4697 ptr = skipwhite(ptr);
4698 }
4699 len = (int)STRLEN(ptr);
4700 }
4701 else
4702 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004703 char_u *tmp_ptr = ptr;
4704
4705 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004707 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 /* Skip if already inside a word. */
4709 if (vim_iswordp(tmp_ptr))
4710 continue;
4711 /* Find start of next word. */
4712 tmp_ptr = find_word_start(tmp_ptr);
4713 }
4714 /* Find end of this word. */
4715 tmp_ptr = find_word_end(tmp_ptr);
4716 len = (int)(tmp_ptr - ptr);
4717
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004718 if ((compl_cont_status & CONT_ADDING)
4719 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
4721 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4722 {
4723 /* Try next line, if any. the new word will be
4724 * "join" as if the normal command "J" was used.
4725 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004726 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 * works -- Acevedo */
4728 STRNCPY(IObuff, ptr, len);
4729 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4730 tmp_ptr = ptr = skipwhite(ptr);
4731 /* Find start of next word. */
4732 tmp_ptr = find_word_start(tmp_ptr);
4733 /* Find end of next word. */
4734 tmp_ptr = find_word_end(tmp_ptr);
4735 if (tmp_ptr > ptr)
4736 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004737 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004739 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 IObuff[len++] = ' ';
4741 /* IObuf =~ "\k.* ", thus len >= 2 */
4742 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004743 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 || (vim_strchr(p_cpo, CPO_JOINSP)
4745 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004746 && (IObuff[len - 2] == '?'
4747 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 IObuff[len++] = ' ';
4749 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004750 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 if (tmp_ptr - ptr >= IOSIZE - len)
4752 tmp_ptr = ptr + IOSIZE - len - 1;
4753 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4754 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004755 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 IObuff[len] = NUL;
4758 ptr = IObuff;
4759 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004760 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 continue;
4762 }
4763 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004764 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004765 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004766 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 {
4768 found_new_match = OK;
4769 break;
4770 }
4771 }
4772 p_scs = save_p_scs;
4773 p_ws = save_p_ws;
4774 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004775
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004776 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004777 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004778 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 found_new_match = OK;
4780
4781 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004782 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4783 * match */
4784 if ((ctrl_x_mode != CTRL_X_NORMAL
4785 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004786 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004787 {
4788 if (got_int)
4789 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004790 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004791 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004792 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004793
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004794 if ((ctrl_x_mode != CTRL_X_NORMAL
4795 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004796 || compl_interrupted)
4797 break;
4798 compl_started = TRUE;
4799 }
4800 else
4801 {
4802 /* Mark a buffer scanned when it has been scanned completely */
4803 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4804 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004806 compl_started = FALSE;
4807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004809 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004811 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 && *e_cpt == NUL) /* Got to end of 'complete' */
4813 found_new_match = FAIL;
4814
4815 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004816 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4817 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 i = ins_compl_make_cyclic();
4819
Bram Moolenaar4475b622017-05-01 20:46:52 +02004820 if (compl_old_match != NULL)
4821 {
4822 /* If several matches were added (FORWARD) or the search failed and has
4823 * just been made cyclic then we have to move compl_curr_match to the
4824 * next or previous entry (if any) -- Acevedo */
4825 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4826 : compl_old_match->cp_prev;
4827 if (compl_curr_match == NULL)
4828 compl_curr_match = compl_old_match;
4829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 return i;
4831}
4832
4833/* Delete the old text being completed. */
4834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004835ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004837 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
4839 /*
4840 * In insert mode: Delete the typed part.
4841 * In replace mode: Put the old characters back, if any.
4842 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004843 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4844 if ((int)curwin->w_cursor.col > col)
4845 {
4846 if (stop_arrow() == FAIL)
4847 return;
4848 backspace_until_column(col);
4849 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004850
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004851 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4852 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004854 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004855 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856}
4857
Bram Moolenaar472e8592016-10-15 17:06:47 +02004858/*
4859 * Insert the new text being completed.
4860 * "in_compl_func" is TRUE when called from complete_check().
4861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004863ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004865 dict_T *dict;
4866
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004867 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004868 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4869 compl_used_match = FALSE;
4870 else
4871 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004872
4873 /* Set completed item. */
4874 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004875 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004876 if (dict != NULL)
4877 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004878 dict_add_string(dict, "word", compl_shown_match->cp_str);
4879 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4880 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4881 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4882 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4883 dict_add_string(dict, "user_data",
4884 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004885 }
4886 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004887 if (!in_compl_func)
4888 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889}
4890
4891/*
4892 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004893 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4894 * get more completions. If it is FALSE, then we just do nothing when there
4895 * are no more completions in a given direction. The latter case is used when
4896 * we are still in the middle of finding completions, to allow browsing
4897 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 * Return the total number of matches, or -1 if still unknown -- webb.
4899 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004900 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4901 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 *
4903 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004904 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4905 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 */
4907 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004908ins_compl_next(
4909 int allow_get_expansion,
4910 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004911 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004912 int insert_match, /* Insert the newly selected match */
4913 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
4915 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004916 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004917 compl_T *found_compl = NULL;
4918 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004919 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004920 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004922 /* When user complete function return -1 for findstart which is next
4923 * time of 'always', compl_shown_match become NULL. */
4924 if (compl_shown_match == NULL)
4925 return -1;
4926
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004927 if (compl_leader != NULL
4928 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004930 /* Set "compl_shown_match" to the actually shown match, it may differ
4931 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004932 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004933 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004934 && compl_shown_match->cp_next != NULL
4935 && compl_shown_match->cp_next != compl_first_match)
4936 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004937
4938 /* If we didn't find it searching forward, and compl_shows_dir is
4939 * backward, find the last match. */
4940 if (compl_shows_dir == BACKWARD
4941 && !ins_compl_equal(compl_shown_match,
4942 compl_leader, (int)STRLEN(compl_leader))
4943 && (compl_shown_match->cp_next == NULL
4944 || compl_shown_match->cp_next == compl_first_match))
4945 {
4946 while (!ins_compl_equal(compl_shown_match,
4947 compl_leader, (int)STRLEN(compl_leader))
4948 && compl_shown_match->cp_prev != NULL
4949 && compl_shown_match->cp_prev != compl_first_match)
4950 compl_shown_match = compl_shown_match->cp_prev;
4951 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004952 }
4953
4954 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004955 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 /* Delete old text to be replaced */
4957 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004958
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004959 /* When finding the longest common text we stick at the original text,
4960 * don't let CTRL-N or CTRL-P move to the first match. */
4961 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4962
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004963 /* When restarting the search don't insert the first match either. */
4964 if (compl_restarting)
4965 {
4966 advance = FALSE;
4967 compl_restarting = FALSE;
4968 }
4969
Bram Moolenaare3226be2005-12-18 22:10:00 +00004970 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4971 * around. */
4972 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004974 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004976 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004977 found_end = (compl_first_match != NULL
4978 && (compl_shown_match->cp_next == compl_first_match
4979 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004980 }
4981 else if (compl_shows_dir == BACKWARD
4982 && compl_shown_match->cp_prev != NULL)
4983 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004984 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004985 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004986 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004989 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004990 if (!allow_get_expansion)
4991 {
4992 if (advance)
4993 {
4994 if (compl_shows_dir == BACKWARD)
4995 compl_pending -= todo + 1;
4996 else
4997 compl_pending += todo + 1;
4998 }
4999 return -1;
5000 }
5001
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005002 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005003 {
5004 if (compl_shows_dir == BACKWARD)
5005 --compl_pending;
5006 else
5007 ++compl_pending;
5008 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005009
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005010 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005011 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005012
5013 /* handle any pending completions */
5014 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005015 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005016 {
5017 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5018 {
5019 compl_shown_match = compl_shown_match->cp_next;
5020 --compl_pending;
5021 }
5022 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5023 {
5024 compl_shown_match = compl_shown_match->cp_prev;
5025 ++compl_pending;
5026 }
5027 else
5028 break;
5029 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005030 found_end = FALSE;
5031 }
5032 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5033 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005034 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005035 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005036 ++todo;
5037 else
5038 /* Remember a matching item. */
5039 found_compl = compl_shown_match;
5040
5041 /* Stop at the end of the list when we found a usable match. */
5042 if (found_end)
5043 {
5044 if (found_compl != NULL)
5045 {
5046 compl_shown_match = found_compl;
5047 break;
5048 }
5049 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005053 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005054 if (compl_no_insert && !started)
5055 {
5056 ins_bytes(compl_orig_text + ins_compl_len());
5057 compl_used_match = FALSE;
5058 }
5059 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005060 {
5061 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005062 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005063 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005064 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005065 }
5066 else
5067 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
5069 if (!allow_get_expansion)
5070 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005071 /* may undisplay the popup menu first */
5072 ins_compl_upd_pum();
5073
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005074 /* redraw to show the user what was inserted */
5075 update_screen(0);
5076
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005077 /* display the updated popup menu */
5078 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005079#ifdef FEAT_GUI
5080 if (gui.in_use)
5081 {
5082 /* Show the cursor after the match, not after the redrawn text. */
5083 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005084 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005085 }
5086#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 /* Delete old text to be replaced, since we're still searching and
5089 * don't want to match ourselves! */
5090 ins_compl_delete();
5091 }
5092
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005093 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005094 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005095 if (compl_no_insert && !started)
5096 compl_enter_selects = TRUE;
5097 else
5098 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005099
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 /*
5101 * Show the file name for the match (if any)
5102 * Truncate the file name to avoid a wait for return.
5103 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005104 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005106 char *lead = _("match in file");
5107 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5108 char_u *s;
5109 char_u *e;
5110
5111 if (space > 0)
5112 {
5113 /* We need the tail that fits. With double-byte encoding going
5114 * back from the end is very slow, thus go from the start and keep
5115 * the text that fits in "space" between "s" and "e". */
5116 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5117 {
5118 space -= ptr2cells(e);
5119 while (space < 0)
5120 {
5121 space += ptr2cells(s);
5122 MB_PTR_ADV(s);
5123 }
5124 }
5125 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5126 s > compl_shown_match->cp_fname ? "<" : "", s);
5127 msg(IObuff);
5128 redraw_cmdline = FALSE; /* don't overwrite! */
5129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131
5132 return num_matches;
5133}
5134
5135/*
5136 * Call this while finding completions, to check whether the user has hit a key
5137 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005138 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005140 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005141 * "in_compl_func" is TRUE when called from complete_check(), don't set
5142 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 */
5144 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005145ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146{
5147 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005148 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005150 /* Don't check when reading keys from a script, :normal or feedkeys().
5151 * That would break the test scripts. But do check for keys when called
5152 * from complete_check(). */
5153 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 return;
5155
5156 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005157 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 return;
5159 count = 0;
5160
Bram Moolenaara260a972006-06-23 19:36:29 +00005161 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5162 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 if (c != NUL)
5165 {
5166 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5167 {
5168 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005169 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005170 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005171 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005173 else
5174 {
5175 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005176 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005177 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005178 if (c != K_IGNORE)
5179 {
5180 /* Don't interrupt completion when the character wasn't typed,
5181 * e.g., when doing @q to replay keys. */
5182 if (c != Ctrl_R && KeyTyped)
5183 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005184
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005185 vungetc(c);
5186 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005189 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005190 {
5191 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5192
5193 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005194 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005195 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005196}
5197
5198/*
5199 * Decide the direction of Insert mode complete from the key typed.
5200 * Returns BACKWARD or FORWARD.
5201 */
5202 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005203ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005204{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005205 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005206 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005207 return BACKWARD;
5208 return FORWARD;
5209}
5210
5211/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005212 * Return TRUE for keys that are used for completion only when the popup menu
5213 * is visible.
5214 */
5215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005216ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005217{
5218 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005219 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5220 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005221}
5222
5223/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005224 * Decide the number of completions to move forward.
5225 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5226 */
5227 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005228ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005229{
5230 int h;
5231
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005232 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005233 {
5234 h = pum_get_height();
5235 if (h > 3)
5236 h -= 2; /* keep some context */
5237 return h;
5238 }
5239 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240}
5241
5242/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005243 * Return TRUE if completion with "c" should insert the match, FALSE if only
5244 * to change the currently selected completion.
5245 */
5246 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005247ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005248{
5249 switch (c)
5250 {
5251 case K_UP:
5252 case K_DOWN:
5253 case K_PAGEDOWN:
5254 case K_KPAGEDOWN:
5255 case K_S_DOWN:
5256 case K_PAGEUP:
5257 case K_KPAGEUP:
5258 case K_S_UP:
5259 return FALSE;
5260 }
5261 return TRUE;
5262}
5263
5264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 * Do Insert mode completion.
5266 * Called when character "c" was typed, which has a meaning for completion.
5267 * Returns OK if completion was done, FAIL if something failed (out of mem).
5268 */
5269 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005270ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005272 char_u *line;
5273 int startcol = 0; /* column where searched text starts */
5274 colnr_T curs_col; /* cursor column */
5275 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005276 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005277 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005278 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005279 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
Bram Moolenaare3226be2005-12-18 22:10:00 +00005281 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005282 insert_match = ins_compl_use_match(c);
5283
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005284 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 {
5286 /* First time we hit ^N or ^P (in a row, I mean) */
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 did_ai = FALSE;
5289#ifdef FEAT_SMARTINDENT
5290 did_si = FALSE;
5291 can_si = FALSE;
5292 can_si_back = FALSE;
5293#endif
5294 if (stop_arrow() == FAIL)
5295 return FAIL;
5296
5297 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005298 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005299 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005301 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005302 * "compl_startpos" to the cursor as a pattern to add a new word
5303 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005304 * "compl_startpos" is not in the same line as the cursor then fix it
5305 * (the line has been split because it was longer than 'tw'). if SOL
5306 * is set then skip the previous pattern, a word at the beginning of
5307 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005308 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5309 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 {
5311 /*
5312 * it is a continued search
5313 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005314 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005315 if (ctrl_x_mode == CTRL_X_NORMAL
5316 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5317 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005319 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005321 /* line (probably) wrapped, set compl_startpos to the
5322 * first non_blank in the line, if it is not a wordchar
5323 * include it to get a better pattern, but then we don't
5324 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005325 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005326 compl_startpos.col = compl_col;
5327 compl_startpos.lnum = curwin->w_cursor.lnum;
5328 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330 else
5331 {
5332 /* S_IPOS was set when we inserted a word that was at the
5333 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005334 * mode but first we need to redefine compl_startpos */
5335 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 compl_cont_status |= CONT_SOL;
5338 compl_startpos.col = (colnr_T)(skipwhite(
5339 line + compl_length
5340 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005344 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005345 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005346 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005350 compl_cont_status &= ~CONT_SOL;
5351 compl_length = (IOSIZE - MIN_SPACE);
5352 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005354 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5355 if (compl_length < 1)
5356 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005358 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005359 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005361 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005364 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005366 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005368 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005369 if (ctrl_x_mode != CTRL_X_NORMAL)
5370 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005371 compl_cont_status = 0;
5372 compl_cont_status |= CONT_N_ADDS;
5373 compl_startpos = curwin->w_cursor;
5374 startcol = (int)curs_col;
5375 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 }
5377
5378 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005379 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005381 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5383 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005384 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005386 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005388 compl_col += ++startcol;
5389 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005392 compl_pattern = str_foldcase(line + compl_col,
5393 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005395 compl_pattern = vim_strnsave(line + compl_col,
5396 compl_length);
5397 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 return FAIL;
5399 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005400 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 {
5402 char_u *prefix = (char_u *)"\\<";
5403
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005404 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005405 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005406 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005407 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005409 if (!vim_iswordp(line + compl_col)
5410 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 && (
5412#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005413 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005415 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416#endif
5417 )))
5418 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005419 STRCPY((char *)compl_pattern, prefix);
5420 (void)quote_meta(compl_pattern + STRLEN(prefix),
5421 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005423 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005425 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005427 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428#endif
5429 )
5430 {
5431 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005432 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5433 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005435 compl_col += curs_col;
5436 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 }
5438 else
5439 {
5440#ifdef FEAT_MBYTE
5441 /* Search the point of change class of multibyte character
5442 * or not a word single byte character backward. */
5443 if (has_mbyte)
5444 {
5445 int base_class;
5446 int head_off;
5447
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005448 startcol -= (*mb_head_off)(line, line + startcol);
5449 base_class = mb_get_class(line + startcol);
5450 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005452 head_off = (*mb_head_off)(line, line + startcol);
5453 if (base_class != mb_get_class(line + startcol
5454 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005456 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
5458 }
5459 else
5460#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005461 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005463 compl_col += ++startcol;
5464 compl_length = (int)curs_col - startcol;
5465 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467 /* Only match word with at least two chars -- webb
5468 * there's no need to call quote_meta,
5469 * alloc(7) is enough -- Acevedo
5470 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005471 compl_pattern = alloc(7);
5472 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005474 STRCPY((char *)compl_pattern, "\\<");
5475 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5476 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
5478 else
5479 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005480 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005481 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005482 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005484 STRCPY((char *)compl_pattern, "\\<");
5485 (void)quote_meta(compl_pattern + 2, line + compl_col,
5486 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488 }
5489 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005490 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005492 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005493 compl_length = (int)curs_col - (int)compl_col;
5494 if (compl_length < 0) /* cursor in indent: empty pattern */
5495 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005497 compl_pattern = str_foldcase(line + compl_col, compl_length,
5498 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005500 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5501 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 return FAIL;
5503 }
5504 else if (ctrl_x_mode == CTRL_X_FILES)
5505 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005506 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005507 if (startcol > 0)
5508 {
5509 char_u *p = line + startcol;
5510
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005511 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005512 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005513 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005514 if (p == line && vim_isfilec(PTR2CHAR(p)))
5515 startcol = 0;
5516 else
5517 startcol = (int)(p - line) + 1;
5518 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005519
Bram Moolenaar0300e462013-09-08 16:03:45 +02005520 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005521 compl_length = (int)curs_col - startcol;
5522 compl_pattern = addstar(line + compl_col, compl_length,
5523 EXPAND_FILES);
5524 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 return FAIL;
5526 }
5527 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5528 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005529 compl_pattern = vim_strnsave(line, curs_col);
5530 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005532 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005533 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005534 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5535 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005536 /* No completion possible, use an empty pattern to get a
5537 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005538 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005539 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005540 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5541 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005543 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005544 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005545#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005546 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005547 * Call user defined function 'completefunc' with "a:findstart"
5548 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005549 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005550 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005551 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005552 char_u *funcname;
5553 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005554 win_T *curwin_save;
5555 buf_T *curbuf_save;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005556 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005557
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005558 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005559 * string */
5560 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5561 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5562 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005563 {
5564 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5565 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005566 /* restore did_ai, so that adding comment leader works */
5567 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005568 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005569 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005570
Bram Moolenaarffa96842018-06-12 22:05:14 +02005571 args[0].v_type = VAR_NUMBER;
5572 args[0].vval.v_number = 1;
5573 args[1].v_type = VAR_STRING;
5574 args[1].vval.v_string = (char_u *)"";
5575 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005576 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005577 curwin_save = curwin;
5578 curbuf_save = curbuf;
Bram Moolenaarded27a12018-08-01 19:06:03 +02005579 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005580
5581 State = save_State;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005582 if (curwin_save != curwin || curbuf_save != curbuf)
5583 {
5584 EMSG(_(e_complwin));
5585 return FAIL;
5586 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005587 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005588 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005589 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005590 {
5591 EMSG(_(e_compldel));
5592 return FAIL;
5593 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005594
Bram Moolenaar6110a002012-01-26 18:58:38 +01005595 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005596 * cancel the complete without an error.
5597 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005598 if (col == -2)
5599 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005600 if (col == -3)
5601 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005602 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005603 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005604 if (!shortmess(SHM_COMPLETIONMENU))
5605 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005606 return FAIL;
5607 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005608
Bram Moolenaar82139082011-09-14 16:52:09 +02005609 /*
5610 * Reset extended parameters of completion, when start new
5611 * completion.
5612 */
5613 compl_opt_refresh_always = FALSE;
5614
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005615 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005616 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005617 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005618 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005619 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005620
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005621 /* Setup variables for completion. Need to obtain "line" again,
5622 * it may have become invalid. */
5623 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005624 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005625 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5626 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005627#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005628 return FAIL;
5629 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005630 else if (ctrl_x_mode == CTRL_X_SPELL)
5631 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005632#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005633 if (spell_bad_len > 0)
5634 compl_col = curs_col - spell_bad_len;
5635 else
5636 compl_col = spell_word_start(startcol);
5637 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005638 {
5639 compl_length = 0;
5640 compl_col = curs_col;
5641 }
5642 else
5643 {
5644 spell_expand_check_cap(compl_col);
5645 compl_length = (int)curs_col - compl_col;
5646 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005647 /* Need to obtain "line" again, it may have become invalid. */
5648 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005649 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5650 if (compl_pattern == NULL)
5651#endif
5652 return FAIL;
5653 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005654 else
5655 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005656 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005657 return FAIL;
5658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005660 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
5662 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005663 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 {
5665 /* Insert a new line, keep indentation but ignore 'comments' */
5666#ifdef FEAT_COMMENTS
5667 char_u *old = curbuf->b_p_com;
5668
5669 curbuf->b_p_com = (char_u *)"";
5670#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005671 compl_startpos.lnum = curwin->w_cursor.lnum;
5672 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 ins_eol('\r');
5674#ifdef FEAT_COMMENTS
5675 curbuf->b_p_com = old;
5676#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005677 compl_length = 0;
5678 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680 }
5681 else
5682 {
5683 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005684 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005687 if (compl_cont_status & CONT_LOCAL)
5688 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 else
5690 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5691
Bram Moolenaar62951b12011-09-21 18:23:05 +02005692 /* If any of the original typed text has been changed we need to fix
5693 * the redo buffer. */
5694 ins_compl_fixRedoBufForLeader(NULL);
5695
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005696 /* Always add completion for the original text. */
5697 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005698 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5699 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005700 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005702 VIM_CLEAR(compl_pattern);
5703 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 return FAIL;
5705 }
5706
5707 /* showmode might reset the internal line pointers, so it must
5708 * be called before line = ml_get(), or when this address is no
5709 * longer needed. -- Acevedo.
5710 */
5711 edit_submode_extra = (char_u *)_("-- Searching...");
5712 edit_submode_highl = HLF_COUNT;
5713 showmode();
5714 edit_submode_extra = NULL;
5715 out_flush();
5716 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005717 else if (insert_match && stop_arrow() == FAIL)
5718 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005720 compl_shown_match = compl_curr_match;
5721 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
5723 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005724 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005726 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005727 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005728 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005730 /* may undisplay the popup menu */
5731 ins_compl_upd_pum();
5732
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005733 if (n > 1) /* all matches have been found */
5734 compl_matches = n;
5735 compl_curr_match = compl_shown_match;
5736 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
Bram Moolenaard68071d2006-05-02 22:08:30 +00005738 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5739 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 if (got_int && !global_busy)
5741 {
5742 (void)vgetc();
5743 got_int = FALSE;
5744 }
5745
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005746 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005747 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005749 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5750 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5752 edit_submode_highl = HLF_E;
5753 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5754 * because we couldn't expand anything at first place, but if we used
5755 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5756 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005757 if ( compl_length > 1
5758 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005759 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5761 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005762 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 }
5764
Bram Moolenaar572cb562005-08-05 21:35:02 +00005765 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005766 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005768 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
5770 if (edit_submode_extra == NULL)
5771 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005772 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {
5774 edit_submode_extra = (char_u *)_("Back at original");
5775 edit_submode_highl = HLF_W;
5776 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005777 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 {
5779 edit_submode_extra = (char_u *)_("Word from other line");
5780 edit_submode_highl = HLF_COUNT;
5781 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005782 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 {
5784 edit_submode_extra = (char_u *)_("The only match");
5785 edit_submode_highl = HLF_COUNT;
5786 }
5787 else
5788 {
5789 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005790 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005792 int number = 0;
5793 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005795 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 {
5797 /* search backwards for the first valid (!= -1) number.
5798 * This should normally succeed already at the first loop
5799 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005800 for (match = compl_curr_match->cp_prev; match != NULL
5801 && match != compl_first_match;
5802 match = match->cp_prev)
5803 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005805 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 break;
5807 }
5808 if (match != NULL)
5809 /* go up and assign all numbers which are not assigned
5810 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005811 for (match = match->cp_next;
5812 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005813 match = match->cp_next)
5814 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
5816 else /* BACKWARD */
5817 {
5818 /* search forwards (upwards) for the first valid (!= -1)
5819 * number. This should normally succeed already at the
5820 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005821 for (match = compl_curr_match->cp_next; match != NULL
5822 && match != compl_first_match;
5823 match = match->cp_next)
5824 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005826 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 break;
5828 }
5829 if (match != NULL)
5830 /* go down and assign all numbers which are not
5831 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005832 for (match = match->cp_prev; match
5833 && match->cp_number == -1;
5834 match = match->cp_prev)
5835 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
5837 }
5838
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005839 /* The match should always have a sequence number now, this is
5840 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005841 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005843 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5844 * Translations may need more than twice that. */
5845 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005847 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005848 vim_snprintf((char *)match_ref, sizeof(match_ref),
5849 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005850 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005852 vim_snprintf((char *)match_ref, sizeof(match_ref),
5853 _("match %d"),
5854 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 edit_submode_extra = match_ref;
5856 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005857 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 curs_columns(FALSE);
5859 }
5860 }
5861 }
5862
5863 /* Show a message about what (completion) mode we're in. */
5864 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005865 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005867 if (edit_submode_extra != NULL)
5868 {
5869 if (!p_smd)
5870 msg_attr(edit_submode_extra,
5871 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005872 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005873 }
5874 else
5875 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
Bram Moolenaard68071d2006-05-02 22:08:30 +00005878 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005879 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005880 show_pum(save_w_wrow, save_w_leftcol);
5881
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005882 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005883 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005884
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 return OK;
5886}
5887
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005888 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005889show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005890{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005891 /* RedrawingDisabled may be set when invoked through complete(). */
5892 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005893
Bram Moolenaarcb036422017-03-01 12:29:10 +01005894 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005895
Bram Moolenaarcb036422017-03-01 12:29:10 +01005896 /* If the cursor moved or the display scrolled we need to remove the pum
5897 * first. */
5898 setcursor();
5899 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5900 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005901
Bram Moolenaarcb036422017-03-01 12:29:10 +01005902 ins_compl_show_pum();
5903 setcursor();
5904 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005905}
5906
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907/*
5908 * Looks in the first "len" chars. of "src" for search-metachars.
5909 * If dest is not NULL the chars. are copied there quoting (with
5910 * a backslash) the metachars, and dest would be NUL terminated.
5911 * Returns the length (needed) of dest
5912 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005913 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005914quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005916 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005918 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 {
5920 switch (*src)
5921 {
5922 case '.':
5923 case '*':
5924 case '[':
5925 if (ctrl_x_mode == CTRL_X_DICTIONARY
5926 || ctrl_x_mode == CTRL_X_THESAURUS)
5927 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005928 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 case '~':
5930 if (!p_magic) /* quote these only if magic is set */
5931 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005932 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 case '\\':
5934 if (ctrl_x_mode == CTRL_X_DICTIONARY
5935 || ctrl_x_mode == CTRL_X_THESAURUS)
5936 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005937 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 case '^': /* currently it's not needed. */
5939 case '$':
5940 m++;
5941 if (dest != NULL)
5942 *dest++ = '\\';
5943 break;
5944 }
5945 if (dest != NULL)
5946 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005947# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 /* Copy remaining bytes of a multibyte character. */
5949 if (has_mbyte)
5950 {
5951 int i, mb_len;
5952
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005953 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 if (mb_len > 0 && len >= mb_len)
5955 for (i = 0; i < mb_len; ++i)
5956 {
5957 --len;
5958 ++src;
5959 if (dest != NULL)
5960 *dest++ = *src;
5961 }
5962 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 }
5965 if (dest != NULL)
5966 *dest = NUL;
5967
5968 return m;
5969}
5970#endif /* FEAT_INS_EXPAND */
5971
5972/*
5973 * Next character is interpreted literally.
5974 * A one, two or three digit decimal number is interpreted as its byte value.
5975 * If one or two digits are entered, the next character is given to vungetc().
5976 * For Unicode a character > 255 may be returned.
5977 */
5978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005979get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
5981 int cc;
5982 int nc;
5983 int i;
5984 int hex = FALSE;
5985 int octal = FALSE;
5986#ifdef FEAT_MBYTE
5987 int unicode = 0;
5988#endif
5989
5990 if (got_int)
5991 return Ctrl_C;
5992
5993#ifdef FEAT_GUI
5994 /*
5995 * In GUI there is no point inserting the internal code for a special key.
5996 * It is more useful to insert the string "<KEY>" instead. This would
5997 * probably be useful in a text window too, but it would not be
5998 * vi-compatible (maybe there should be an option for it?) -- webb
5999 */
6000 if (gui.in_use)
6001 ++allow_keys;
6002#endif
6003#ifdef USE_ON_FLY_SCROLL
6004 dont_scroll = TRUE; /* disallow scrolling here */
6005#endif
6006 ++no_mapping; /* don't map the next key hits */
6007 cc = 0;
6008 i = 0;
6009 for (;;)
6010 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006011 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012#ifdef FEAT_CMDL_INFO
6013 if (!(State & CMDLINE)
6014# ifdef FEAT_MBYTE
6015 && MB_BYTE2LEN_CHECK(nc) == 1
6016# endif
6017 )
6018 add_to_showcmd(nc);
6019#endif
6020 if (nc == 'x' || nc == 'X')
6021 hex = TRUE;
6022 else if (nc == 'o' || nc == 'O')
6023 octal = TRUE;
6024#ifdef FEAT_MBYTE
6025 else if (nc == 'u' || nc == 'U')
6026 unicode = nc;
6027#endif
6028 else
6029 {
6030 if (hex
6031#ifdef FEAT_MBYTE
6032 || unicode != 0
6033#endif
6034 )
6035 {
6036 if (!vim_isxdigit(nc))
6037 break;
6038 cc = cc * 16 + hex2nr(nc);
6039 }
6040 else if (octal)
6041 {
6042 if (nc < '0' || nc > '7')
6043 break;
6044 cc = cc * 8 + nc - '0';
6045 }
6046 else
6047 {
6048 if (!VIM_ISDIGIT(nc))
6049 break;
6050 cc = cc * 10 + nc - '0';
6051 }
6052
6053 ++i;
6054 }
6055
6056 if (cc > 255
6057#ifdef FEAT_MBYTE
6058 && unicode == 0
6059#endif
6060 )
6061 cc = 255; /* limit range to 0-255 */
6062 nc = 0;
6063
6064 if (hex) /* hex: up to two chars */
6065 {
6066 if (i >= 2)
6067 break;
6068 }
6069#ifdef FEAT_MBYTE
6070 else if (unicode) /* Unicode: up to four or eight chars */
6071 {
6072 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6073 break;
6074 }
6075#endif
6076 else if (i >= 3) /* decimal or octal: up to three chars */
6077 break;
6078 }
6079 if (i == 0) /* no number entered */
6080 {
6081 if (nc == K_ZERO) /* NUL is stored as NL */
6082 {
6083 cc = '\n';
6084 nc = 0;
6085 }
6086 else
6087 {
6088 cc = nc;
6089 nc = 0;
6090 }
6091 }
6092
6093 if (cc == 0) /* NUL is stored as NL */
6094 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006095#ifdef FEAT_MBYTE
6096 if (enc_dbcs && (cc & 0xff) == 0)
6097 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6098 second byte will cause trouble! */
6099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
6101 --no_mapping;
6102#ifdef FEAT_GUI
6103 if (gui.in_use)
6104 --allow_keys;
6105#endif
6106 if (nc)
6107 vungetc(nc);
6108 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6109 return cc;
6110}
6111
6112/*
6113 * Insert character, taking care of special keys and mod_mask
6114 */
6115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116insert_special(
6117 int c,
6118 int allow_modmask,
6119 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120{
6121 char_u *p;
6122 int len;
6123
6124 /*
6125 * Special function key, translate into "<Key>". Up to the last '>' is
6126 * inserted with ins_str(), so as not to replace characters in replace
6127 * mode.
6128 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6129 * unless 'allow_modmask' is TRUE.
6130 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006131#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 /* Command-key never produces a normal key */
6133 if (mod_mask & MOD_MASK_CMD)
6134 allow_modmask = TRUE;
6135#endif
6136 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6137 {
6138 p = get_special_key_name(c, mod_mask);
6139 len = (int)STRLEN(p);
6140 c = p[len - 1];
6141 if (len > 2)
6142 {
6143 if (stop_arrow() == FAIL)
6144 return;
6145 p[len - 1] = NUL;
6146 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006147 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 ctrlv = FALSE;
6149 }
6150 }
6151 if (stop_arrow() == OK)
6152 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6153}
6154
6155/*
6156 * Special characters in this context are those that need processing other
6157 * than the simple insertion that can be performed here. This includes ESC
6158 * which terminates the insert, and CR/NL which need special processing to
6159 * open up a new line. This routine tries to optimize insertions performed by
6160 * the "redo", "undo" or "put" commands, so it needs to know when it should
6161 * stop and defer processing to the "normal" mechanism.
6162 * '0' and '^' are special, because they can be followed by CTRL-D.
6163 */
6164#ifdef EBCDIC
6165# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6166#else
6167# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6168#endif
6169
6170#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006171# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006173# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174#endif
6175
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006176/*
6177 * "flags": INSCHAR_FORMAT - force formatting
6178 * INSCHAR_CTRLV - char typed just after CTRL-V
6179 * INSCHAR_NO_FEX - don't use 'formatexpr'
6180 *
6181 * NOTE: passes the flags value straight through to internal_format() which,
6182 * beside INSCHAR_FORMAT (above), is also looking for these:
6183 * INSCHAR_DO_COM - format comments
6184 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006187insertchar(
6188 int c, /* character to insert or NUL */
6189 int flags, /* INSCHAR_FORMAT, etc. */
6190 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 int textwidth;
6193#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006197 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006199 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201
6202 /*
6203 * Try to break the line in two or more pieces when:
6204 * - Always do this if we have been called to do formatting only.
6205 * - Always do this when 'formatoptions' has the 'a' flag and the line
6206 * ends in white space.
6207 * - Otherwise:
6208 * - Don't do this if inserting a blank
6209 * - Don't do this if an existing character is being replaced, unless
6210 * we're in VREPLACE mode.
6211 * - Do this if the cursor is not on the line where insert started
6212 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6213 * before the insert.
6214 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6215 * before 'textwidth'
6216 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006217 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006218 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006219 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 && *ml_get_cursor() != NUL)
6223 && (curwin->w_cursor.lnum != Insstart.lnum
6224 || ((!has_format_option(FO_INS_LONG)
6225 || Insstart_textlen <= (colnr_T)textwidth)
6226 && (!fo_ins_blank
6227 || Insstart_blank_vcol <= (colnr_T)textwidth
6228 ))))))
6229 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006230 /* Format with 'formatexpr' when it's set. Use internal formatting
6231 * when 'formatexpr' isn't set or it returns non-zero. */
6232#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006233 int do_internal = TRUE;
6234 colnr_T virtcol = get_nolist_virtcol()
6235 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006236
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006237 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6238 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006239 {
6240 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6241 /* It may be required to save for undo again, e.g. when setline()
6242 * was called. */
6243 ins_need_undo = TRUE;
6244 }
6245 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006247 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006249
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 if (c == NUL) /* only formatting was wanted */
6251 return;
6252
6253#ifdef FEAT_COMMENTS
6254 /* Check whether this character should end a comment. */
6255 if (did_ai && (int)c == end_comment_pending)
6256 {
6257 char_u *line;
6258 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6259 int middle_len, end_len;
6260 int i;
6261
6262 /*
6263 * Need to remove existing (middle) comment leader and insert end
6264 * comment leader. First, check what comment leader we can find.
6265 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006266 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6268 {
6269 /* Skip middle-comment string */
6270 while (*p && p[-1] != ':') /* find end of middle flags */
6271 ++p;
6272 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6273 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006274 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 --middle_len;
6276
6277 /* Find the end-comment string */
6278 while (*p && p[-1] != ':') /* find end of end flags */
6279 ++p;
6280 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6281
6282 /* Skip white space before the cursor */
6283 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006284 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 ;
6286 i++;
6287
6288 /* Skip to before the middle leader */
6289 i -= middle_len;
6290
6291 /* Check some expected things before we go on */
6292 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6293 {
6294 /* Backspace over all the stuff we want to replace */
6295 backspace_until_column(i);
6296
6297 /*
6298 * Insert the end-comment string, except for the last
6299 * character, which will get inserted as normal later.
6300 */
6301 ins_bytes_len(lead_end, end_len - 1);
6302 }
6303 }
6304 }
6305 end_comment_pending = NUL;
6306#endif
6307
6308 did_ai = FALSE;
6309#ifdef FEAT_SMARTINDENT
6310 did_si = FALSE;
6311 can_si = FALSE;
6312 can_si_back = FALSE;
6313#endif
6314
6315 /*
6316 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6317 * This speeds up normal text input considerably.
6318 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6319 * need to re-indent at a ':', or any other character (but not what
6320 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006321 * Don't do this when there an InsertCharPre autocommand is defined,
6322 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006323 * Do the check for InsertCharPre before the call to vpeekc() because the
6324 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 */
6326#ifdef USE_ON_FLY_SCROLL
6327 dont_scroll = FALSE; /* allow scrolling here */
6328#endif
6329
6330 if ( !ISSPECIAL(c)
6331#ifdef FEAT_MBYTE
6332 && (!has_mbyte || (*mb_char2len)(c) == 1)
6333#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006334 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 && vpeekc() != NUL
6336 && !(State & REPLACE_FLAG)
6337#ifdef FEAT_CINDENT
6338 && !cindent_on()
6339#endif
6340#ifdef FEAT_RIGHTLEFT
6341 && !p_ri
6342#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006343 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 {
6345#define INPUT_BUFLEN 100
6346 char_u buf[INPUT_BUFLEN + 1];
6347 int i;
6348 colnr_T virtcol = 0;
6349
6350 buf[0] = c;
6351 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006352 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 virtcol = get_nolist_virtcol();
6354 /*
6355 * Stop the string when:
6356 * - no more chars available
6357 * - finding a special character (command key)
6358 * - buffer is full
6359 * - running into the 'textwidth' boundary
6360 * - need to check for abbreviation: A non-word char after a word-char
6361 */
6362 while ( (c = vpeekc()) != NUL
6363 && !ISSPECIAL(c)
6364#ifdef FEAT_MBYTE
6365 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6366#endif
6367 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006368# ifdef FEAT_FKMAP
6369 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 && (textwidth == 0
6372 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6373 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6374 {
6375#ifdef FEAT_RIGHTLEFT
6376 c = vgetc();
6377 if (p_hkmap && KeyTyped)
6378 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 buf[i++] = c;
6380#else
6381 buf[i++] = vgetc();
6382#endif
6383 }
6384
6385#ifdef FEAT_DIGRAPHS
6386 do_digraph(-1); /* clear digraphs */
6387 do_digraph(buf[i-1]); /* may be the start of a digraph */
6388#endif
6389 buf[i] = NUL;
6390 ins_str(buf);
6391 if (flags & INSCHAR_CTRLV)
6392 {
6393 redo_literal(*buf);
6394 i = 1;
6395 }
6396 else
6397 i = 0;
6398 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006399 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 }
6401 else
6402 {
6403#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006404 int cc;
6405
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6407 {
6408 char_u buf[MB_MAXBYTES + 1];
6409
6410 (*mb_char2bytes)(c, buf);
6411 buf[cc] = NUL;
6412 ins_char_bytes(buf, cc);
6413 AppendCharToRedobuff(c);
6414 }
6415 else
6416#endif
6417 {
6418 ins_char(c);
6419 if (flags & INSCHAR_CTRLV)
6420 redo_literal(c);
6421 else
6422 AppendCharToRedobuff(c);
6423 }
6424 }
6425}
6426
6427/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006428 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006429 *
6430 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6431 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006432 */
6433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006434internal_format(
6435 int textwidth,
6436 int second_indent,
6437 int flags,
6438 int format_only,
6439 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440{
6441 int cc;
6442 int save_char = NUL;
6443 int haveto_redraw = FALSE;
6444 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6445#ifdef FEAT_MBYTE
6446 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6447#endif
6448 int fo_white_par = has_format_option(FO_WHITE_PAR);
6449 int first_line = TRUE;
6450#ifdef FEAT_COMMENTS
6451 colnr_T leader_len;
6452 int no_leader = FALSE;
6453 int do_comments = (flags & INSCHAR_DO_COM);
6454#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006455#ifdef FEAT_LINEBREAK
6456 int has_lbr = curwin->w_p_lbr;
6457
6458 /* make sure win_lbr_chartabsize() counts correctly */
6459 curwin->w_p_lbr = FALSE;
6460#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006461
6462 /*
6463 * When 'ai' is off we don't want a space under the cursor to be
6464 * deleted. Replace it with an 'x' temporarily.
6465 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006466 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006467 {
6468 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006469 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006470 {
6471 save_char = cc;
6472 pchar_cursor('x');
6473 }
6474 }
6475
6476 /*
6477 * Repeat breaking lines, until the current line is not too long.
6478 */
6479 while (!got_int)
6480 {
6481 int startcol; /* Cursor column at entry */
6482 int wantcol; /* column at textwidth border */
6483 int foundcol; /* column for start of spaces */
6484 int end_foundcol = 0; /* column for start of word */
6485 colnr_T len;
6486 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006487 int orig_col = 0;
6488 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006489 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006490 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006491
Bram Moolenaar97b98102009-11-17 16:41:01 +00006492 virtcol = get_nolist_virtcol()
6493 + char2cells(c != NUL ? c : gchar_cursor());
6494 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006495 break;
6496
6497#ifdef FEAT_COMMENTS
6498 if (no_leader)
6499 do_comments = FALSE;
6500 else if (!(flags & INSCHAR_FORMAT)
6501 && has_format_option(FO_WRAP_COMS))
6502 do_comments = TRUE;
6503
6504 /* Don't break until after the comment leader */
6505 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006506 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006507 else
6508 leader_len = 0;
6509
6510 /* If the line doesn't start with a comment leader, then don't
6511 * start one in a following broken line. Avoids that a %word
6512 * moved to the start of the next line causes all following lines
6513 * to start with %. */
6514 if (leader_len == 0)
6515 no_leader = TRUE;
6516#endif
6517 if (!(flags & INSCHAR_FORMAT)
6518#ifdef FEAT_COMMENTS
6519 && leader_len == 0
6520#endif
6521 && !has_format_option(FO_WRAP))
6522
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006523 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006524 if ((startcol = curwin->w_cursor.col) == 0)
6525 break;
6526
6527 /* find column of textwidth border */
6528 coladvance((colnr_T)textwidth);
6529 wantcol = curwin->w_cursor.col;
6530
Bram Moolenaar97b98102009-11-17 16:41:01 +00006531 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006532 foundcol = 0;
6533
6534 /*
6535 * Find position to break at.
6536 * Stop at first entered white when 'formatoptions' has 'v'
6537 */
6538 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006539 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006540 || curwin->w_cursor.lnum != Insstart.lnum
6541 || curwin->w_cursor.col >= Insstart.col)
6542 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006543 if (curwin->w_cursor.col == startcol && c != NUL)
6544 cc = c;
6545 else
6546 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006547 if (WHITECHAR(cc))
6548 {
6549 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006550 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006551
6552 /* find start of sequence of blanks */
6553 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6554 {
6555 dec_cursor();
6556 cc = gchar_cursor();
6557 }
6558 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6559 break; /* only spaces in front of text */
6560#ifdef FEAT_COMMENTS
6561 /* Don't break until after the comment leader */
6562 if (curwin->w_cursor.col < leader_len)
6563 break;
6564#endif
6565 if (has_format_option(FO_ONE_LETTER))
6566 {
6567 /* do not break after one-letter words */
6568 if (curwin->w_cursor.col == 0)
6569 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006570#ifdef FEAT_COMMENTS
6571 /* do not break "#a b" when 'tw' is 2 */
6572 if (curwin->w_cursor.col <= leader_len)
6573 break;
6574#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006575 col = curwin->w_cursor.col;
6576 dec_cursor();
6577 cc = gchar_cursor();
6578
6579 if (WHITECHAR(cc))
6580 continue; /* one-letter, continue */
6581 curwin->w_cursor.col = col;
6582 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006583
6584 inc_cursor();
6585
6586 end_foundcol = end_col + 1;
6587 foundcol = curwin->w_cursor.col;
6588 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006589 break;
6590 }
6591#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006592 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006593 {
6594 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006595 if (curwin->w_cursor.col != startcol)
6596 {
6597#ifdef FEAT_COMMENTS
6598 /* Don't break until after the comment leader */
6599 if (curwin->w_cursor.col < leader_len)
6600 break;
6601#endif
6602 col = curwin->w_cursor.col;
6603 inc_cursor();
6604 /* Don't change end_foundcol if already set. */
6605 if (foundcol != curwin->w_cursor.col)
6606 {
6607 foundcol = curwin->w_cursor.col;
6608 end_foundcol = foundcol;
6609 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6610 break;
6611 }
6612 curwin->w_cursor.col = col;
6613 }
6614
6615 if (curwin->w_cursor.col == 0)
6616 break;
6617
6618 col = curwin->w_cursor.col;
6619
6620 dec_cursor();
6621 cc = gchar_cursor();
6622
6623 if (WHITECHAR(cc))
6624 continue; /* break with space */
6625#ifdef FEAT_COMMENTS
6626 /* Don't break until after the comment leader */
6627 if (curwin->w_cursor.col < leader_len)
6628 break;
6629#endif
6630
6631 curwin->w_cursor.col = col;
6632
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006633 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006634 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006635 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6636 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006637 }
6638#endif
6639 if (curwin->w_cursor.col == 0)
6640 break;
6641 dec_cursor();
6642 }
6643
6644 if (foundcol == 0) /* no spaces, cannot break line */
6645 {
6646 curwin->w_cursor.col = startcol;
6647 break;
6648 }
6649
6650 /* Going to break the line, remove any "$" now. */
6651 undisplay_dollar();
6652
6653 /*
6654 * Offset between cursor position and line break is used by replace
6655 * stack functions. VREPLACE does not use this, and backspaces
6656 * over the text instead.
6657 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006658 if (State & VREPLACE_FLAG)
6659 orig_col = startcol; /* Will start backspacing from here */
6660 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006661 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006662
6663 /*
6664 * adjust startcol for spaces that will be deleted and
6665 * characters that will remain on top line
6666 */
6667 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006668 while ((cc = gchar_cursor(), WHITECHAR(cc))
6669 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006670 inc_cursor();
6671 startcol -= curwin->w_cursor.col;
6672 if (startcol < 0)
6673 startcol = 0;
6674
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006675 if (State & VREPLACE_FLAG)
6676 {
6677 /*
6678 * In VREPLACE mode, we will backspace over the text to be
6679 * wrapped, so save a copy now to put on the next line.
6680 */
6681 saved_text = vim_strsave(ml_get_cursor());
6682 curwin->w_cursor.col = orig_col;
6683 if (saved_text == NULL)
6684 break; /* Can't do it, out of memory */
6685 saved_text[startcol] = NUL;
6686
6687 /* Backspace over characters that will move to the next line */
6688 if (!fo_white_par)
6689 backspace_until_column(foundcol);
6690 }
6691 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006692 {
6693 /* put cursor after pos. to break line */
6694 if (!fo_white_par)
6695 curwin->w_cursor.col = foundcol;
6696 }
6697
6698 /*
6699 * Split the line just before the margin.
6700 * Only insert/delete lines, but don't really redraw the window.
6701 */
6702 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6703 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6704#ifdef FEAT_COMMENTS
6705 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006706 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006707#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006708 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6709 if (!(flags & INSCHAR_COM_LIST))
6710 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006711
6712 replace_offset = 0;
6713 if (first_line)
6714 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006715 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006716 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006717 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006718 * This section is for auto-wrap of numeric lists. When not
6719 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6720 * flag will be set and open_line() will handle it (as seen
6721 * above). The code here (and in get_number_indent()) will
6722 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006723 */
6724 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006725 second_indent =
6726 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006727 if (second_indent >= 0)
6728 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006729 if (State & VREPLACE_FLAG)
6730 change_indent(INDENT_SET, second_indent,
6731 FALSE, NUL, TRUE);
6732 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006733#ifdef FEAT_COMMENTS
6734 if (leader_len > 0 && second_indent - leader_len > 0)
6735 {
6736 int i;
6737 int padding = second_indent - leader_len;
6738
6739 /* We started at the first_line of a numbered list
6740 * that has a comment. the open_line() function has
6741 * inserted the proper comment leader and positioned
6742 * the cursor at the end of the split line. Now we
6743 * add the additional whitespace needed after the
6744 * comment leader for the numbered list. */
6745 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006746 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006747 }
6748 else
6749 {
6750#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006751 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006752#ifdef FEAT_COMMENTS
6753 }
6754#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006755 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006756 }
6757 first_line = FALSE;
6758 }
6759
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006760 if (State & VREPLACE_FLAG)
6761 {
6762 /*
6763 * In VREPLACE mode we have backspaced over the text to be
6764 * moved, now we re-insert it into the new line.
6765 */
6766 ins_bytes(saved_text);
6767 vim_free(saved_text);
6768 }
6769 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006770 {
6771 /*
6772 * Check if cursor is not past the NUL off the line, cindent
6773 * may have added or removed indent.
6774 */
6775 curwin->w_cursor.col += startcol;
6776 len = (colnr_T)STRLEN(ml_get_curline());
6777 if (curwin->w_cursor.col > len)
6778 curwin->w_cursor.col = len;
6779 }
6780
6781 haveto_redraw = TRUE;
6782#ifdef FEAT_CINDENT
6783 can_cindent = TRUE;
6784#endif
6785 /* moved the cursor, don't autoindent or cindent now */
6786 did_ai = FALSE;
6787#ifdef FEAT_SMARTINDENT
6788 did_si = FALSE;
6789 can_si = FALSE;
6790 can_si_back = FALSE;
6791#endif
6792 line_breakcheck();
6793 }
6794
6795 if (save_char != NUL) /* put back space after cursor */
6796 pchar_cursor(save_char);
6797
Bram Moolenaar0026d472014-09-09 16:32:39 +02006798#ifdef FEAT_LINEBREAK
6799 curwin->w_p_lbr = has_lbr;
6800#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006801 if (!format_only && haveto_redraw)
6802 {
6803 update_topline();
6804 redraw_curbuf_later(VALID);
6805 }
6806}
6807
6808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 * Called after inserting or deleting text: When 'formatoptions' includes the
6810 * 'a' flag format from the current line until the end of the paragraph.
6811 * Keep the cursor at the same position relative to the text.
6812 * The caller must have saved the cursor line for undo, following ones will be
6813 * saved here.
6814 */
6815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006816auto_format(
6817 int trailblank, /* when TRUE also format with trailing blank */
6818 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819{
6820 pos_T pos;
6821 colnr_T len;
6822 char_u *old;
6823 char_u *new, *pnew;
6824 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006825 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826
6827 if (!has_format_option(FO_AUTO))
6828 return;
6829
6830 pos = curwin->w_cursor;
6831 old = ml_get_curline();
6832
6833 /* may remove added space */
6834 check_auto_format(FALSE);
6835
6836 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6837 * user might insert normal text next. Also skip formatting when "1" is
6838 * in 'formatoptions' and there is a single character before the cursor.
6839 * Otherwise the line would be broken and when typing another non-white
6840 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006841 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 if (*old != NUL && !trailblank && wasatend)
6843 {
6844 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006845 cc = gchar_cursor();
6846 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6847 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006849 cc = gchar_cursor();
6850 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {
6852 curwin->w_cursor = pos;
6853 return;
6854 }
6855 curwin->w_cursor = pos;
6856 }
6857
6858#ifdef FEAT_COMMENTS
6859 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6860 * comments. */
6861 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006862 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 return;
6864#endif
6865
6866 /*
6867 * May start formatting in a previous line, so that after "x" a word is
6868 * moved to the previous line if it fits there now. Only when this is not
6869 * the start of a paragraph.
6870 */
6871 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6872 {
6873 --curwin->w_cursor.lnum;
6874 if (u_save_cursor() == FAIL)
6875 return;
6876 }
6877
6878 /*
6879 * Do the formatting and restore the cursor position. "saved_cursor" will
6880 * be adjusted for the text formatting.
6881 */
6882 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006883 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 curwin->w_cursor = saved_cursor;
6885 saved_cursor.lnum = 0;
6886
6887 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6888 {
6889 /* "cannot happen" */
6890 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6891 coladvance((colnr_T)MAXCOL);
6892 }
6893 else
6894 check_cursor_col();
6895
6896 /* Insert mode: If the cursor is now after the end of the line while it
6897 * previously wasn't, the line was broken. Because of the rule above we
6898 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6899 * formatted. */
6900 if (!wasatend && has_format_option(FO_WHITE_PAR))
6901 {
6902 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006903 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (curwin->w_cursor.col == len)
6905 {
6906 pnew = vim_strnsave(new, len + 2);
6907 pnew[len] = ' ';
6908 pnew[len + 1] = NUL;
6909 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6910 /* remove the space later */
6911 did_add_space = TRUE;
6912 }
6913 else
6914 /* may remove added space */
6915 check_auto_format(FALSE);
6916 }
6917
6918 check_cursor();
6919}
6920
6921/*
6922 * When an extra space was added to continue a paragraph for auto-formatting,
6923 * delete it now. The space must be under the cursor, just after the insert
6924 * position.
6925 */
6926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006927check_auto_format(
6928 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
6930 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006931 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932
6933 if (did_add_space)
6934 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006935 cc = gchar_cursor();
6936 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 /* Somehow the space was removed already. */
6938 did_add_space = FALSE;
6939 else
6940 {
6941 if (!end_insert)
6942 {
6943 inc_cursor();
6944 c = gchar_cursor();
6945 dec_cursor();
6946 }
6947 if (c != NUL)
6948 {
6949 /* The space is no longer at the end of the line, delete it. */
6950 del_char(FALSE);
6951 did_add_space = FALSE;
6952 }
6953 }
6954 }
6955}
6956
6957/*
6958 * Find out textwidth to be used for formatting:
6959 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006960 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 * if invalid value, use 0.
6962 * Set default to window width (maximum 79) for "gq" operator.
6963 */
6964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006965comp_textwidth(
6966 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968 int textwidth;
6969
6970 textwidth = curbuf->b_p_tw;
6971 if (textwidth == 0 && curbuf->b_p_wm)
6972 {
6973 /* The width is the window width minus 'wrapmargin' minus all the
6974 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006975 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976#ifdef FEAT_CMDWIN
6977 if (cmdwin_type != 0)
6978 textwidth -= 1;
6979#endif
6980#ifdef FEAT_FOLDING
6981 textwidth -= curwin->w_p_fdc;
6982#endif
6983#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006984 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 textwidth -= 1;
6986#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006987 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 textwidth -= 8;
6989 }
6990 if (textwidth < 0)
6991 textwidth = 0;
6992 if (ff && textwidth == 0)
6993 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006994 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 if (textwidth > 79)
6996 textwidth = 79;
6997 }
6998 return textwidth;
6999}
7000
7001/*
7002 * Put a character in the redo buffer, for when just after a CTRL-V.
7003 */
7004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007005redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007 char_u buf[10];
7008
7009 /* Only digits need special treatment. Translate them into a string of
7010 * three digits. */
7011 if (VIM_ISDIGIT(c))
7012 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007013 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 AppendToRedobuff(buf);
7015 }
7016 else
7017 AppendCharToRedobuff(c);
7018}
7019
7020/*
7021 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007022 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 */
7024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007025start_arrow(
7026 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007028 start_arrow_common(end_insert_pos, TRUE);
7029}
7030
7031/*
7032 * Like start_arrow() but with end_change argument.
7033 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7034 */
7035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007036start_arrow_with_change(
7037 pos_T *end_insert_pos, /* can be NULL */
7038 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007039{
7040 start_arrow_common(end_insert_pos, end_change);
7041 if (!end_change)
7042 {
7043 AppendCharToRedobuff(Ctrl_G);
7044 AppendCharToRedobuff('U');
7045 }
7046}
7047
7048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007049start_arrow_common(
7050 pos_T *end_insert_pos, /* can be NULL */
7051 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007052{
7053 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 {
7055 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007056 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 arrow_used = TRUE; /* this means we stopped the current insert */
7058 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007059#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007060 check_spell_redraw();
7061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062}
7063
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007064#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007065/*
7066 * If we skipped highlighting word at cursor, do it now.
7067 * It may be skipped again, thus reset spell_redraw_lnum first.
7068 */
7069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007070check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007071{
7072 if (spell_redraw_lnum != 0)
7073 {
7074 linenr_T lnum = spell_redraw_lnum;
7075
7076 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01007077 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00007078 }
7079}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007080
7081/*
7082 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7083 * spelled word, if there is one.
7084 */
7085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007086spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007087{
7088 pos_T tpos = curwin->w_cursor;
7089
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007090 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007091 if (curwin->w_cursor.col != tpos.col)
7092 start_arrow(&tpos);
7093}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007094#endif
7095
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096/*
7097 * stop_arrow() is called before a change is made in insert mode.
7098 * If an arrow key has been used, start a new insertion.
7099 * Returns FAIL if undo is impossible, shouldn't insert then.
7100 */
7101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007102stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104 if (arrow_used)
7105 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007106 Insstart = curwin->w_cursor; /* new insertion starts here */
7107 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7108 /* Don't update the original insert position when moved to the
7109 * right, except when nothing was inserted yet. */
7110 update_Insstart_orig = FALSE;
7111 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7112
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 if (u_save_cursor() == OK)
7114 {
7115 arrow_used = FALSE;
7116 ins_need_undo = FALSE;
7117 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007118
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 if (State & VREPLACE_FLAG)
7121 {
7122 orig_line_count = curbuf->b_ml.ml_line_count;
7123 vr_lines_changed = 1;
7124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 ResetRedobuff();
7126 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007127 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 }
7129 else if (ins_need_undo)
7130 {
7131 if (u_save_cursor() == OK)
7132 ins_need_undo = FALSE;
7133 }
7134
7135#ifdef FEAT_FOLDING
7136 /* Always open fold at the cursor line when inserting something. */
7137 foldOpenCursor();
7138#endif
7139
7140 return (arrow_used || ins_need_undo ? FAIL : OK);
7141}
7142
7143/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007144 * Do a few things to stop inserting.
7145 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7146 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 */
7148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007149stop_insert(
7150 pos_T *end_insert_pos,
7151 int esc, /* called by ins_esc() */
7152 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007154 int cc;
7155 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156
7157 stop_redo_ins();
7158 replace_flush(); /* abandon replace stack */
7159
7160 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007161 * Save the inserted text for later redo with ^@ and CTRL-A.
7162 * Don't do it when "restart_edit" was set and nothing was inserted,
7163 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007165 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007166 if (did_restart_edit == 0 || (ptr != NULL
7167 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007168 {
7169 vim_free(last_insert);
7170 last_insert = ptr;
7171 last_insert_skip = new_insert_skip;
7172 }
7173 else
7174 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007176 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {
7178 /* Auto-format now. It may seem strange to do this when stopping an
7179 * insertion (or moving the cursor), but it's required when appending
7180 * a line and having it end in a space. But only do it when something
7181 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007182 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007184 pos_T tpos = curwin->w_cursor;
7185
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 /* When the cursor is at the end of the line after a space the
7187 * formatting will move it to the following word. Avoid that by
7188 * moving the cursor onto the space. */
7189 cc = 'x';
7190 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7191 {
7192 dec_cursor();
7193 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007194 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007195 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 }
7197
7198 auto_format(TRUE, FALSE);
7199
Bram Moolenaar1c465442017-03-12 20:10:05 +01007200 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007201 {
7202 if (gchar_cursor() != NUL)
7203 inc_cursor();
7204#ifdef FEAT_VIRTUALEDIT
7205 /* If the cursor is still at the same character, also keep
7206 * the "coladd". */
7207 if (gchar_cursor() == NUL
7208 && curwin->w_cursor.lnum == tpos.lnum
7209 && curwin->w_cursor.col == tpos.col)
7210 curwin->w_cursor.coladd = tpos.coladd;
7211#endif
7212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 }
7214
7215 /* If a space was inserted for auto-formatting, remove it now. */
7216 check_auto_format(TRUE);
7217
7218 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007219 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007220 * Do this when ESC was used or moving the cursor up/down.
7221 * Check for the old position still being valid, just in case the text
7222 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007223 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007224 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7225 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007227 pos_T tpos = curwin->w_cursor;
7228
7229 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007230 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007231 for (;;)
7232 {
7233 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7234 --curwin->w_cursor.col;
7235 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007236 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007237 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007238 if (del_char(TRUE) == FAIL)
7239 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007240 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007241 if (curwin->w_cursor.lnum != tpos.lnum)
7242 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007243 else
7244 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007245 /* reset tpos, could have been invalidated in the loop above */
7246 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007247 tpos.col++;
7248 if (cc != NUL && gchar_pos(&tpos) == NUL)
7249 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 /* <C-S-Right> may have started Visual mode, adjust the position for
7253 * deleted characters. */
7254 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7255 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007256 int len = (int)STRLEN(ml_get_curline());
7257
7258 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007260 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007261#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
7265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 }
7267 }
7268 did_ai = FALSE;
7269#ifdef FEAT_SMARTINDENT
7270 did_si = FALSE;
7271 can_si = FALSE;
7272 can_si_back = FALSE;
7273#endif
7274
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007275 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7276 * now in a different buffer. */
7277 if (end_insert_pos != NULL)
7278 {
7279 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007280 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007281 curbuf->b_op_end = *end_insert_pos;
7282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283}
7284
7285/*
7286 * Set the last inserted text to a single character.
7287 * Used for the replace command.
7288 */
7289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007290set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
7292 char_u *s;
7293
7294 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 if (last_insert != NULL)
7297 {
7298 s = last_insert;
7299 /* Use the CTRL-V only when entering a special char */
7300 if (c < ' ' || c == DEL)
7301 *s++ = Ctrl_V;
7302 s = add_char2buf(c, s);
7303 *s++ = ESC;
7304 *s++ = NUL;
7305 last_insert_skip = 0;
7306 }
7307}
7308
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007309#if defined(EXITFREE) || defined(PROTO)
7310 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007311free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007312{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007313 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007314# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007315 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007316# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007317}
7318#endif
7319
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320/*
7321 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7322 * and CSI. Handle multi-byte characters.
7323 * Returns a pointer to after the added bytes.
7324 */
7325 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007326add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327{
7328#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007329 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 int i;
7331 int len;
7332
7333 len = (*mb_char2bytes)(c, temp);
7334 for (i = 0; i < len; ++i)
7335 {
7336 c = temp[i];
7337#endif
7338 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7339 if (c == K_SPECIAL)
7340 {
7341 *s++ = K_SPECIAL;
7342 *s++ = KS_SPECIAL;
7343 *s++ = KE_FILLER;
7344 }
7345#ifdef FEAT_GUI
7346 else if (c == CSI)
7347 {
7348 *s++ = CSI;
7349 *s++ = KS_EXTRA;
7350 *s++ = (int)KE_CSI;
7351 }
7352#endif
7353 else
7354 *s++ = c;
7355#ifdef FEAT_MBYTE
7356 }
7357#endif
7358 return s;
7359}
7360
7361/*
7362 * move cursor to start of line
7363 * if flags & BL_WHITE move to first non-white
7364 * if flags & BL_SOL move to first non-white if startofline is set,
7365 * otherwise keep "curswant" column
7366 * if flags & BL_FIX don't leave the cursor on a NUL.
7367 */
7368 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007369beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370{
7371 if ((flags & BL_SOL) && !p_sol)
7372 coladvance(curwin->w_curswant);
7373 else
7374 {
7375 curwin->w_cursor.col = 0;
7376#ifdef FEAT_VIRTUALEDIT
7377 curwin->w_cursor.coladd = 0;
7378#endif
7379
7380 if (flags & (BL_WHITE | BL_SOL))
7381 {
7382 char_u *ptr;
7383
Bram Moolenaar1c465442017-03-12 20:10:05 +01007384 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7386 ++curwin->w_cursor.col;
7387 }
7388 curwin->w_set_curswant = TRUE;
7389 }
7390}
7391
7392/*
7393 * oneright oneleft cursor_down cursor_up
7394 *
7395 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007396 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 * Return OK when successful, FAIL when we hit a line of file boundary.
7398 */
7399
7400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007401oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
7406#ifdef FEAT_VIRTUALEDIT
7407 if (virtual_active())
7408 {
7409 pos_T prevpos = curwin->w_cursor;
7410
7411 /* Adjust for multi-wide char (excluding TAB) */
7412 ptr = ml_get_cursor();
7413 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007414# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007416# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 ))
7420 ? ptr2cells(ptr) : 1));
7421 curwin->w_set_curswant = TRUE;
7422 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7423 return (prevpos.col != curwin->w_cursor.col
7424 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7425 }
7426#endif
7427
7428 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007429 if (*ptr == NUL)
7430 return FAIL; /* already at the very end */
7431
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007433 if (has_mbyte)
7434 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 else
7436#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007437 l = 1;
7438
7439 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7440 * contains "onemore". */
7441 if (ptr[l] == NUL
7442#ifdef FEAT_VIRTUALEDIT
7443 && (ve_flags & VE_ONEMORE) == 0
7444#endif
7445 )
7446 return FAIL;
7447 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
7449 curwin->w_set_curswant = TRUE;
7450 return OK;
7451}
7452
7453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007454oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455{
7456#ifdef FEAT_VIRTUALEDIT
7457 if (virtual_active())
7458 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007459# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 int v = getviscol();
7463
7464 if (v == 0)
7465 return FAIL;
7466
7467# ifdef FEAT_LINEBREAK
7468 /* We might get stuck on 'showbreak', skip over it. */
7469 width = 1;
7470 for (;;)
7471 {
7472 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007473 /* getviscol() is slow, skip it when 'showbreak' is empty,
7474 * 'breakindent' is not set and there are no multi-byte
7475 * characters */
7476 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477# ifdef FEAT_MBYTE
7478 && !has_mbyte
7479# endif
7480 ) || getviscol() < v)
7481 break;
7482 ++width;
7483 }
7484# else
7485 coladvance(v - 1);
7486# endif
7487
7488 if (curwin->w_cursor.coladd == 1)
7489 {
7490 char_u *ptr;
7491
7492 /* Adjust for multi-wide char (not a TAB) */
7493 ptr = ml_get_cursor();
7494 if (*ptr != TAB && vim_isprintc(
7495# ifdef FEAT_MBYTE
7496 (*mb_ptr2char)(ptr)
7497# else
7498 *ptr
7499# endif
7500 ) && ptr2cells(ptr) > 1)
7501 curwin->w_cursor.coladd = 0;
7502 }
7503
7504 curwin->w_set_curswant = TRUE;
7505 return OK;
7506 }
7507#endif
7508
7509 if (curwin->w_cursor.col == 0)
7510 return FAIL;
7511
7512 curwin->w_set_curswant = TRUE;
7513 --curwin->w_cursor.col;
7514
7515#ifdef FEAT_MBYTE
7516 /* if the character on the left of the current cursor is a multi-byte
7517 * character, move to its first byte */
7518 if (has_mbyte)
7519 mb_adjust_cursor();
7520#endif
7521 return OK;
7522}
7523
7524 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007525cursor_up(
7526 long n,
7527 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528{
7529 linenr_T lnum;
7530
7531 if (n > 0)
7532 {
7533 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007534 /* This fails if the cursor is already in the first line or the count
7535 * is larger than the line number and '-' is in 'cpoptions' */
7536 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 return FAIL;
7538 if (n >= lnum)
7539 lnum = 1;
7540 else
7541#ifdef FEAT_FOLDING
7542 if (hasAnyFolding(curwin))
7543 {
7544 /*
7545 * Count each sequence of folded lines as one logical line.
7546 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007547 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 (void)hasFolding(lnum, &lnum, NULL);
7549
7550 while (n--)
7551 {
7552 /* move up one line */
7553 --lnum;
7554 if (lnum <= 1)
7555 break;
7556 /* If we entered a fold, move to the beginning, unless in
7557 * Insert mode or when 'foldopen' contains "all": it will open
7558 * in a moment. */
7559 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7560 (void)hasFolding(lnum, &lnum, NULL);
7561 }
7562 if (lnum < 1)
7563 lnum = 1;
7564 }
7565 else
7566#endif
7567 lnum -= n;
7568 curwin->w_cursor.lnum = lnum;
7569 }
7570
7571 /* try to advance to the column we want to be at */
7572 coladvance(curwin->w_curswant);
7573
7574 if (upd_topline)
7575 update_topline(); /* make sure curwin->w_topline is valid */
7576
7577 return OK;
7578}
7579
7580/*
7581 * Cursor down a number of logical lines.
7582 */
7583 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007584cursor_down(
7585 long n,
7586 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587{
7588 linenr_T lnum;
7589
7590 if (n > 0)
7591 {
7592 lnum = curwin->w_cursor.lnum;
7593#ifdef FEAT_FOLDING
7594 /* Move to last line of fold, will fail if it's the end-of-file. */
7595 (void)hasFolding(lnum, NULL, &lnum);
7596#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007597 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007598 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007599 if (lnum >= curbuf->b_ml.ml_line_count
7600 || (lnum + n > curbuf->b_ml.ml_line_count
7601 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 return FAIL;
7603 if (lnum + n >= curbuf->b_ml.ml_line_count)
7604 lnum = curbuf->b_ml.ml_line_count;
7605 else
7606#ifdef FEAT_FOLDING
7607 if (hasAnyFolding(curwin))
7608 {
7609 linenr_T last;
7610
7611 /* count each sequence of folded lines as one logical line */
7612 while (n--)
7613 {
7614 if (hasFolding(lnum, NULL, &last))
7615 lnum = last + 1;
7616 else
7617 ++lnum;
7618 if (lnum >= curbuf->b_ml.ml_line_count)
7619 break;
7620 }
7621 if (lnum > curbuf->b_ml.ml_line_count)
7622 lnum = curbuf->b_ml.ml_line_count;
7623 }
7624 else
7625#endif
7626 lnum += n;
7627 curwin->w_cursor.lnum = lnum;
7628 }
7629
7630 /* try to advance to the column we want to be at */
7631 coladvance(curwin->w_curswant);
7632
7633 if (upd_topline)
7634 update_topline(); /* make sure curwin->w_topline is valid */
7635
7636 return OK;
7637}
7638
7639/*
7640 * Stuff the last inserted text in the read buffer.
7641 * Last_insert actually is a copy of the redo buffer, so we
7642 * first have to remove the command.
7643 */
7644 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007645stuff_inserted(
7646 int c, /* Command character to be inserted */
7647 long count, /* Repeat this many times */
7648 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649{
7650 char_u *esc_ptr;
7651 char_u *ptr;
7652 char_u *last_ptr;
7653 char_u last = NUL;
7654
7655 ptr = get_last_insert();
7656 if (ptr == NULL)
7657 {
7658 EMSG(_(e_noinstext));
7659 return FAIL;
7660 }
7661
7662 /* may want to stuff the command character, to start Insert mode */
7663 if (c != NUL)
7664 stuffcharReadbuff(c);
7665 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7666 *esc_ptr = NUL; /* remove the ESC */
7667
7668 /* when the last char is either "0" or "^" it will be quoted if no ESC
7669 * comes after it OR if it will inserted more than once and "ptr"
7670 * starts with ^D. -- Acevedo
7671 */
7672 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7673 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7674 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7675 {
7676 last = *last_ptr;
7677 *last_ptr = NUL;
7678 }
7679
7680 do
7681 {
7682 stuffReadbuff(ptr);
7683 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7684 if (last)
7685 stuffReadbuff((char_u *)(last == '0'
7686 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7687 : IF_EB("\026^", CTRL_V_STR "^")));
7688 }
7689 while (--count > 0);
7690
7691 if (last)
7692 *last_ptr = last;
7693
7694 if (esc_ptr != NULL)
7695 *esc_ptr = ESC; /* put the ESC back */
7696
7697 /* may want to stuff a trailing ESC, to get out of Insert mode */
7698 if (!no_esc)
7699 stuffcharReadbuff(ESC);
7700
7701 return OK;
7702}
7703
7704 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007705get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
7707 if (last_insert == NULL)
7708 return NULL;
7709 return last_insert + last_insert_skip;
7710}
7711
7712/*
7713 * Get last inserted string, and remove trailing <Esc>.
7714 * Returns pointer to allocated memory (must be freed) or NULL.
7715 */
7716 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007717get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
7719 char_u *s;
7720 int len;
7721
7722 if (last_insert == NULL)
7723 return NULL;
7724 s = vim_strsave(last_insert + last_insert_skip);
7725 if (s != NULL)
7726 {
7727 len = (int)STRLEN(s);
7728 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7729 s[len - 1] = NUL;
7730 }
7731 return s;
7732}
7733
7734/*
7735 * Check the word in front of the cursor for an abbreviation.
7736 * Called when the non-id character "c" has been entered.
7737 * When an abbreviation is recognized it is removed from the text and
7738 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7739 */
7740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007741echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742{
7743 /* Don't check for abbreviation in paste mode, when disabled and just
7744 * after moving around with cursor keys. */
7745 if (p_paste || no_abbr || arrow_used)
7746 return FALSE;
7747
7748 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7749 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7750}
7751
7752/*
7753 * replace-stack functions
7754 *
7755 * When replacing characters, the replaced characters are remembered for each
7756 * new character. This is used to re-insert the old text when backspacing.
7757 *
7758 * There is a NUL headed list of characters for each character that is
7759 * currently in the file after the insertion point. When BS is used, one NUL
7760 * headed list is put back for the deleted character.
7761 *
7762 * For a newline, there are two NUL headed lists. One contains the characters
7763 * that the NL replaced. The extra one stores the characters after the cursor
7764 * that were deleted (always white space).
7765 *
7766 * Replace_offset is normally 0, in which case replace_push will add a new
7767 * character at the end of the stack. If replace_offset is not 0, that many
7768 * characters will be left on the stack above the newly inserted character.
7769 */
7770
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007771static char_u *replace_stack = NULL;
7772static long replace_stack_nr = 0; /* next entry in replace stack */
7773static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
7775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007776replace_push(
7777 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 char_u *p;
7780
7781 if (replace_stack_nr < replace_offset) /* nothing to do */
7782 return;
7783 if (replace_stack_len <= replace_stack_nr)
7784 {
7785 replace_stack_len += 50;
7786 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7787 if (p == NULL) /* out of memory */
7788 {
7789 replace_stack_len -= 50;
7790 return;
7791 }
7792 if (replace_stack != NULL)
7793 {
7794 mch_memmove(p, replace_stack,
7795 (size_t)(replace_stack_nr * sizeof(char_u)));
7796 vim_free(replace_stack);
7797 }
7798 replace_stack = p;
7799 }
7800 p = replace_stack + replace_stack_nr - replace_offset;
7801 if (replace_offset)
7802 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7803 *p = c;
7804 ++replace_stack_nr;
7805}
7806
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007807#if defined(FEAT_MBYTE) || defined(PROTO)
7808/*
7809 * Push a character onto the replace stack. Handles a multi-byte character in
7810 * reverse byte order, so that the first byte is popped off first.
7811 * Return the number of bytes done (includes composing characters).
7812 */
7813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007814replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007815{
7816 int l = (*mb_ptr2len)(p);
7817 int j;
7818
7819 for (j = l - 1; j >= 0; --j)
7820 replace_push(p[j]);
7821 return l;
7822}
7823#endif
7824
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825/*
7826 * Pop one item from the replace stack.
7827 * return -1 if stack empty
7828 * return replaced character or NUL otherwise
7829 */
7830 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007831replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832{
7833 if (replace_stack_nr == 0)
7834 return -1;
7835 return (int)replace_stack[--replace_stack_nr];
7836}
7837
7838/*
7839 * Join the top two items on the replace stack. This removes to "off"'th NUL
7840 * encountered.
7841 */
7842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007843replace_join(
7844 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846 int i;
7847
7848 for (i = replace_stack_nr; --i >= 0; )
7849 if (replace_stack[i] == NUL && off-- <= 0)
7850 {
7851 --replace_stack_nr;
7852 mch_memmove(replace_stack + i, replace_stack + i + 1,
7853 (size_t)(replace_stack_nr - i));
7854 return;
7855 }
7856}
7857
7858/*
7859 * Pop bytes from the replace stack until a NUL is found, and insert them
7860 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7861 */
7862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007863replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864{
7865 int cc;
7866 int oldState = State;
7867
7868 State = NORMAL; /* don't want REPLACE here */
7869 while ((cc = replace_pop()) > 0)
7870 {
7871#ifdef FEAT_MBYTE
7872 mb_replace_pop_ins(cc);
7873#else
7874 ins_char(cc);
7875#endif
7876 dec_cursor();
7877 }
7878 State = oldState;
7879}
7880
7881#ifdef FEAT_MBYTE
7882/*
7883 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7884 * indicates a multi-byte char, pop the other bytes too.
7885 */
7886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007887mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888{
7889 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007890 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 int i;
7892 int c;
7893
7894 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7895 {
7896 buf[0] = cc;
7897 for (i = 1; i < n; ++i)
7898 buf[i] = replace_pop();
7899 ins_bytes_len(buf, n);
7900 }
7901 else
7902 ins_char(cc);
7903
7904 if (enc_utf8)
7905 /* Handle composing chars. */
7906 for (;;)
7907 {
7908 c = replace_pop();
7909 if (c == -1) /* stack empty */
7910 break;
7911 if ((n = MB_BYTE2LEN(c)) == 1)
7912 {
7913 /* Not a multi-byte char, put it back. */
7914 replace_push(c);
7915 break;
7916 }
7917 else
7918 {
7919 buf[0] = c;
7920 for (i = 1; i < n; ++i)
7921 buf[i] = replace_pop();
7922 if (utf_iscomposing(utf_ptr2char(buf)))
7923 ins_bytes_len(buf, n);
7924 else
7925 {
7926 /* Not a composing char, put it back. */
7927 for (i = n - 1; i >= 0; --i)
7928 replace_push(buf[i]);
7929 break;
7930 }
7931 }
7932 }
7933}
7934#endif
7935
7936/*
7937 * make the replace stack empty
7938 * (called when exiting replace mode)
7939 */
7940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007941replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007943 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 replace_stack_len = 0;
7945 replace_stack_nr = 0;
7946}
7947
7948/*
7949 * Handle doing a BS for one character.
7950 * cc < 0: replace stack empty, just move cursor
7951 * cc == 0: character was inserted, delete it
7952 * cc > 0: character was replaced, put cc (first byte of original char) back
7953 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007954 * When "limit_col" is >= 0, don't delete before this column. Matters when
7955 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 */
7957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007958replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959{
7960 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 int orig_len = 0;
7962 int ins_len;
7963 int orig_vcols = 0;
7964 colnr_T start_vcol;
7965 char_u *p;
7966 int i;
7967 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968
7969 cc = replace_pop();
7970 if (cc > 0)
7971 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01007972#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01007973 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01007974
7975 if (curbuf->b_has_textprop)
7976 {
7977 // Do not adjust text properties for individual delete and insert
7978 // operations, do it afterwards on the resulting text.
7979 len_before = STRLEN(ml_get_curline());
7980 ++text_prop_frozen;
7981 }
7982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 if (State & VREPLACE_FLAG)
7984 {
7985 /* Get the number of screen cells used by the character we are
7986 * going to delete. */
7987 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7988 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990#ifdef FEAT_MBYTE
7991 if (has_mbyte)
7992 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007993 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007995 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 replace_push(cc);
7997 }
7998 else
7999#endif
8000 {
8001 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008003 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 }
8005 replace_pop_ins();
8006
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 if (State & VREPLACE_FLAG)
8008 {
8009 /* Get the number of screen cells used by the inserted characters */
8010 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008011 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 vcol = start_vcol;
8013 for (i = 0; i < ins_len; ++i)
8014 {
8015 vcol += chartabsize(p + i, vcol);
8016#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008017 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018#endif
8019 }
8020 vcol -= start_vcol;
8021
8022 /* Delete spaces that were inserted after the cursor to keep the
8023 * text aligned. */
8024 curwin->w_cursor.col += ins_len;
8025 while (vcol > orig_vcols && gchar_cursor() == ' ')
8026 {
8027 del_char(FALSE);
8028 ++orig_vcols;
8029 }
8030 curwin->w_cursor.col -= ins_len;
8031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032
Bram Moolenaar196d1572019-01-02 23:47:18 +01008033 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01008035
8036#ifdef FEAT_TEXT_PROP
8037 if (curbuf->b_has_textprop)
8038 {
8039 size_t len_now = STRLEN(ml_get_curline());
8040
8041 --text_prop_frozen;
8042 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
8043 (int)(len_now - len_before));
8044 }
8045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 }
8047 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008048 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049}
8050
8051#ifdef FEAT_CINDENT
8052/*
8053 * Return TRUE if C-indenting is on.
8054 */
8055 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008056cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
8058 return (!p_paste && (curbuf->b_p_cin
8059# ifdef FEAT_EVAL
8060 || *curbuf->b_p_inde != NUL
8061# endif
8062 ));
8063}
8064#endif
8065
8066#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8067/*
8068 * Re-indent the current line, based on the current contents of it and the
8069 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8070 * confused what all the part that handles Control-T is doing that I'm not.
8071 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8072 */
8073
8074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008075fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008077 int amount = get_the_indent();
8078
8079 if (amount >= 0)
8080 {
8081 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8082 if (linewhite(curwin->w_cursor.lnum))
8083 did_ai = TRUE; /* delete the indent if the line stays empty */
8084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085}
8086
8087 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008088fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089{
8090 if (p_paste)
8091 return;
8092# ifdef FEAT_LISP
8093 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8094 fixthisline(get_lisp_indent);
8095# endif
8096# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8097 else
8098# endif
8099# ifdef FEAT_CINDENT
8100 if (cindent_on())
8101 do_c_expr_indent();
8102# endif
8103}
8104
8105#endif
8106
8107#ifdef FEAT_CINDENT
8108/*
8109 * return TRUE if 'cinkeys' contains the key "keytyped",
8110 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008111 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8113 *
8114 * "keytyped" can have a few special values:
8115 * KEY_OPEN_FORW
8116 * KEY_OPEN_BACK
8117 * KEY_COMPLETE just finished completion.
8118 *
8119 * If line_is_empty is TRUE accept keys with '0' before them.
8120 */
8121 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008122in_cinkeys(
8123 int keytyped,
8124 int when,
8125 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126{
8127 char_u *look;
8128 int try_match;
8129 int try_match_word;
8130 char_u *p;
8131 char_u *line;
8132 int icase;
8133 int i;
8134
Bram Moolenaar30848942009-12-24 14:46:12 +00008135 if (keytyped == NUL)
8136 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8137 return FALSE;
8138
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139#ifdef FEAT_EVAL
8140 if (*curbuf->b_p_inde != NUL)
8141 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8142 else
8143#endif
8144 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8145 while (*look)
8146 {
8147 /*
8148 * Find out if we want to try a match with this key, depending on
8149 * 'when' and a '*' or '!' before the key.
8150 */
8151 switch (when)
8152 {
8153 case '*': try_match = (*look == '*'); break;
8154 case '!': try_match = (*look == '!'); break;
8155 default: try_match = (*look != '*'); break;
8156 }
8157 if (*look == '*' || *look == '!')
8158 ++look;
8159
8160 /*
8161 * If there is a '0', only accept a match if the line is empty.
8162 * But may still match when typing last char of a word.
8163 */
8164 if (*look == '0')
8165 {
8166 try_match_word = try_match;
8167 if (!line_is_empty)
8168 try_match = FALSE;
8169 ++look;
8170 }
8171 else
8172 try_match_word = FALSE;
8173
8174 /*
8175 * does it look like a control character?
8176 */
8177 if (*look == '^'
8178#ifdef EBCDIC
8179 && (Ctrl_chr(look[1]) != 0)
8180#else
8181 && look[1] >= '?' && look[1] <= '_'
8182#endif
8183 )
8184 {
8185 if (try_match && keytyped == Ctrl_chr(look[1]))
8186 return TRUE;
8187 look += 2;
8188 }
8189 /*
8190 * 'o' means "o" command, open forward.
8191 * 'O' means "O" command, open backward.
8192 */
8193 else if (*look == 'o')
8194 {
8195 if (try_match && keytyped == KEY_OPEN_FORW)
8196 return TRUE;
8197 ++look;
8198 }
8199 else if (*look == 'O')
8200 {
8201 if (try_match && keytyped == KEY_OPEN_BACK)
8202 return TRUE;
8203 ++look;
8204 }
8205
8206 /*
8207 * 'e' means to check for "else" at start of line and just before the
8208 * cursor.
8209 */
8210 else if (*look == 'e')
8211 {
8212 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8213 {
8214 p = ml_get_curline();
8215 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8216 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8217 return TRUE;
8218 }
8219 ++look;
8220 }
8221
8222 /*
8223 * ':' only causes an indent if it is at the end of a label or case
8224 * statement, or when it was before typing the ':' (to fix
8225 * class::method for C++).
8226 */
8227 else if (*look == ':')
8228 {
8229 if (try_match && keytyped == ':')
8230 {
8231 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008232 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008234 /* Need to get the line again after cin_islabel(). */
8235 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 if (curwin->w_cursor.col > 2
8237 && p[curwin->w_cursor.col - 1] == ':'
8238 && p[curwin->w_cursor.col - 2] == ':')
8239 {
8240 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008241 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008242 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 p = ml_get_curline();
8244 p[curwin->w_cursor.col - 1] = ':';
8245 if (i)
8246 return TRUE;
8247 }
8248 }
8249 ++look;
8250 }
8251
8252
8253 /*
8254 * Is it a key in <>, maybe?
8255 */
8256 else if (*look == '<')
8257 {
8258 if (try_match)
8259 {
8260 /*
8261 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8262 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8263 * >, *, : and ! keys if they really really want to.
8264 */
8265 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8266 && keytyped == look[1])
8267 return TRUE;
8268
8269 if (keytyped == get_special_key_code(look + 1))
8270 return TRUE;
8271 }
8272 while (*look && *look != '>')
8273 look++;
8274 while (*look == '>')
8275 look++;
8276 }
8277
8278 /*
8279 * Is it a word: "=word"?
8280 */
8281 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8282 {
8283 ++look;
8284 if (*look == '~')
8285 {
8286 icase = TRUE;
8287 ++look;
8288 }
8289 else
8290 icase = FALSE;
8291 p = vim_strchr(look, ',');
8292 if (p == NULL)
8293 p = look + STRLEN(look);
8294 if ((try_match || try_match_word)
8295 && curwin->w_cursor.col >= (colnr_T)(p - look))
8296 {
8297 int match = FALSE;
8298
8299#ifdef FEAT_INS_EXPAND
8300 if (keytyped == KEY_COMPLETE)
8301 {
8302 char_u *s;
8303
8304 /* Just completed a word, check if it starts with "look".
8305 * search back for the start of a word. */
8306 line = ml_get_curline();
8307# ifdef FEAT_MBYTE
8308 if (has_mbyte)
8309 {
8310 char_u *n;
8311
8312 for (s = line + curwin->w_cursor.col; s > line; s = n)
8313 {
8314 n = mb_prevptr(line, s);
8315 if (!vim_iswordp(n))
8316 break;
8317 }
8318 }
8319 else
8320# endif
8321 for (s = line + curwin->w_cursor.col; s > line; --s)
8322 if (!vim_iswordc(s[-1]))
8323 break;
8324 if (s + (p - look) <= line + curwin->w_cursor.col
8325 && (icase
8326 ? MB_STRNICMP(s, look, p - look)
8327 : STRNCMP(s, look, p - look)) == 0)
8328 match = TRUE;
8329 }
8330 else
8331#endif
8332 /* TODO: multi-byte */
8333 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8334 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8335 {
8336 line = ml_get_cursor();
8337 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8338 || !vim_iswordc(line[-(p - look) - 1]))
8339 && (icase
8340 ? MB_STRNICMP(line - (p - look), look, p - look)
8341 : STRNCMP(line - (p - look), look, p - look))
8342 == 0)
8343 match = TRUE;
8344 }
8345 if (match && try_match_word && !try_match)
8346 {
8347 /* "0=word": Check if there are only blanks before the
8348 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008349 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 (int)(curwin->w_cursor.col - (p - look)))
8351 match = FALSE;
8352 }
8353 if (match)
8354 return TRUE;
8355 }
8356 look = p;
8357 }
8358
8359 /*
8360 * ok, it's a boring generic character.
8361 */
8362 else
8363 {
8364 if (try_match && *look == keytyped)
8365 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008366 if (*look != NUL)
8367 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 }
8369
8370 /*
8371 * Skip over ", ".
8372 */
8373 look = skip_to_option_part(look);
8374 }
8375 return FALSE;
8376}
8377#endif /* FEAT_CINDENT */
8378
8379#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8380/*
8381 * Map Hebrew keyboard when in hkmap mode.
8382 */
8383 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008384hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385{
8386 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8387 {
8388 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8389 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8390 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8391 static char_u map[26] =
8392 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8393 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8394 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8395 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8396 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8397 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8398 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8399 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8400 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8401
8402 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8403 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8404 /* '-1'='sofit' */
8405 else if (c == 'x')
8406 return 'X';
8407 else if (c == 'q')
8408 return '\''; /* {geresh}={'} */
8409 else if (c == 246)
8410 return ' '; /* \"o --> ' ' for a german keyboard */
8411 else if (c == 228)
8412 return ' '; /* \"a --> ' ' -- / -- */
8413 else if (c == 252)
8414 return ' '; /* \"u --> ' ' -- / -- */
8415#ifdef EBCDIC
8416 else if (islower(c))
8417#else
8418 /* NOTE: islower() does not do the right thing for us on Linux so we
8419 * do this the same was as 5.7 and previous, so it works correctly on
8420 * all systems. Specifically, the e.g. Delete and Arrow keys are
8421 * munged and won't work if e.g. searching for Hebrew text.
8422 */
8423 else if (c >= 'a' && c <= 'z')
8424#endif
8425 return (int)(map[CharOrdLow(c)] + p_aleph);
8426 else
8427 return c;
8428 }
8429 else
8430 {
8431 switch (c)
8432 {
8433 case '`': return ';';
8434 case '/': return '.';
8435 case '\'': return ',';
8436 case 'q': return '/';
8437 case 'w': return '\'';
8438
8439 /* Hebrew letters - set offset from 'a' */
8440 case ',': c = '{'; break;
8441 case '.': c = 'v'; break;
8442 case ';': c = 't'; break;
8443 default: {
8444 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8445
8446#ifdef EBCDIC
8447 /* see note about islower() above */
8448 if (!islower(c))
8449#else
8450 if (c < 'a' || c > 'z')
8451#endif
8452 return c;
8453 c = str[CharOrdLow(c)];
8454 break;
8455 }
8456 }
8457
8458 return (int)(CharOrdLow(c) + p_aleph);
8459 }
8460}
8461#endif
8462
8463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008464ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465{
8466 int need_redraw = FALSE;
8467 int regname;
8468 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008469 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470
8471 /*
8472 * If we are going to wait for a character, show a '"'.
8473 */
8474 pc_status = PC_STATUS_UNSET;
8475 if (redrawing() && !char_avail())
8476 {
8477 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008478 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479
8480 edit_putchar('"', TRUE);
8481#ifdef FEAT_CMDL_INFO
8482 add_to_showcmd_c(Ctrl_R);
8483#endif
8484 }
8485
8486#ifdef USE_ON_FLY_SCROLL
8487 dont_scroll = TRUE; /* disallow scrolling here */
8488#endif
8489
8490 /*
8491 * Don't map the register name. This also prevents the mode message to be
8492 * deleted when ESC is hit.
8493 */
8494 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008495 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8498 {
8499 /* Get a third key for literal register insertion */
8500 literally = regname;
8501#ifdef FEAT_CMDL_INFO
8502 add_to_showcmd_c(literally);
8503#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008504 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 }
8507 --no_mapping;
8508
8509#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008510 /* Don't call u_sync() while typing the expression or giving an error
8511 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 ++no_u_sync;
8513 if (regname == '=')
8514 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008515# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008517# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008518 /* Sync undo when evaluating the expression calls setline() or
8519 * append(), so that it can be undone separately. */
8520 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008521
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008523# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 /* Restore the Input Method. */
8525 if (im_on)
8526 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008529 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8530 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008531 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 else
8535 {
8536#endif
8537 if (literally == Ctrl_O || literally == Ctrl_P)
8538 {
8539 /* Append the command to the redo buffer. */
8540 AppendCharToRedobuff(Ctrl_R);
8541 AppendCharToRedobuff(literally);
8542 AppendCharToRedobuff(regname);
8543
8544 do_put(regname, BACKWARD, 1L,
8545 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8546 }
8547 else if (insert_reg(regname, literally) == FAIL)
8548 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008549 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 need_redraw = TRUE; /* remove the '"' */
8551 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008552 else if (stop_insert_mode)
8553 /* When the '=' register was used and a function was invoked that
8554 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8555 * insert anything, need to remove the '"' */
8556 need_redraw = TRUE;
8557
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558#ifdef FEAT_EVAL
8559 }
8560 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008561 if (u_sync_once == 1)
8562 ins_need_undo = TRUE;
8563 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564#endif
8565#ifdef FEAT_CMDL_INFO
8566 clear_showcmd();
8567#endif
8568
8569 /* If the inserted register is empty, we need to remove the '"' */
8570 if (need_redraw || stuff_empty())
8571 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008572
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008573 /* Disallow starting Visual mode here, would get a weird mode. */
8574 if (!vis_active && VIsual_active)
8575 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576}
8577
8578/*
8579 * CTRL-G commands in Insert mode.
8580 */
8581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008582ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583{
8584 int c;
8585
8586#ifdef FEAT_INS_EXPAND
8587 /* Right after CTRL-X the cursor will be after the ruler. */
8588 setcursor();
8589#endif
8590
8591 /*
8592 * Don't map the second key. This also prevents the mode message to be
8593 * deleted when ESC is hit.
8594 */
8595 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008596 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 --no_mapping;
8598 switch (c)
8599 {
8600 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8601 case K_UP:
8602 case Ctrl_K:
8603 case 'k': ins_up(TRUE);
8604 break;
8605
8606 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8607 case K_DOWN:
8608 case Ctrl_J:
8609 case 'j': ins_down(TRUE);
8610 break;
8611
8612 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008613 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008615
8616 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008617 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008618 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008619 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 break;
8621
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008622 /* CTRL-G U: do not break undo with the next char */
8623 case 'U':
8624 /* Allow one left/right cursor movement with the next char,
8625 * without breaking undo. */
8626 dont_sync_undo = MAYBE;
8627 break;
8628
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008630 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 }
8632}
8633
8634/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008635 * CTRL-^ in Insert mode.
8636 */
8637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008638ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008639{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008640 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008641 {
8642 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8643 if (State & LANGMAP)
8644 {
8645 curbuf->b_p_iminsert = B_IMODE_NONE;
8646 State &= ~LANGMAP;
8647 }
8648 else
8649 {
8650 curbuf->b_p_iminsert = B_IMODE_LMAP;
8651 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008652#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008653 im_set_active(FALSE);
8654#endif
8655 }
8656 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008657#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008658 else
8659 {
8660 /* There are no ":lmap" mappings, toggle IM */
8661 if (im_get_status())
8662 {
8663 curbuf->b_p_iminsert = B_IMODE_NONE;
8664 im_set_active(FALSE);
8665 }
8666 else
8667 {
8668 curbuf->b_p_iminsert = B_IMODE_IM;
8669 State &= ~LANGMAP;
8670 im_set_active(TRUE);
8671 }
8672 }
8673#endif
8674 set_iminsert_global();
8675 showmode();
8676#ifdef FEAT_GUI
8677 /* may show different cursor shape or color */
8678 if (gui.in_use)
8679 gui_update_cursor(TRUE, FALSE);
8680#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008681#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008682 /* Show/unshow value of 'keymap' in status lines. */
8683 status_redraw_curbuf();
8684#endif
8685}
8686
8687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 * Handle ESC in insert mode.
8689 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8690 * insert.
8691 */
8692 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008693ins_esc(
8694 long *count,
8695 int cmdchar,
8696 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697{
8698 int temp;
8699 static int disabled_redraw = FALSE;
8700
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008701#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008702 check_spell_redraw();
8703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704#if defined(FEAT_HANGULIN)
8705# if defined(ESC_CHG_TO_ENG_MODE)
8706 hangul_input_state_set(0);
8707# endif
8708 if (composing_hangul)
8709 {
8710 push_raw_key(composing_hangul_buffer, 2);
8711 composing_hangul = 0;
8712 }
8713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714
8715 temp = curwin->w_cursor.col;
8716 if (disabled_redraw)
8717 {
8718 --RedrawingDisabled;
8719 disabled_redraw = FALSE;
8720 }
8721 if (!arrow_used)
8722 {
8723 /*
8724 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008725 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8726 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 */
8728 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008729 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730
8731 /*
8732 * Repeating insert may take a long time. Check for
8733 * interrupt now and then.
8734 */
8735 if (*count > 0)
8736 {
8737 line_breakcheck();
8738 if (got_int)
8739 *count = 0;
8740 }
8741
8742 if (--*count > 0) /* repeat what was typed */
8743 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008744 /* Vi repeats the insert without replacing characters. */
8745 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8746 State &= ~REPLACE_FLAG;
8747
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 (void)start_redo_ins();
8749 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008750 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 ++RedrawingDisabled;
8752 disabled_redraw = TRUE;
8753 return FALSE; /* repeat the insert */
8754 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008755 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 undisplay_dollar();
8757 }
8758
8759 /* When an autoindent was removed, curswant stays after the
8760 * indent */
8761 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8762 curwin->w_set_curswant = TRUE;
8763
8764 /* Remember the last Insert position in the '^ mark. */
8765 if (!cmdmod.keepjumps)
8766 curbuf->b_last_insert = curwin->w_cursor;
8767
8768 /*
8769 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008770 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008772 if (!nomove
8773 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774#ifdef FEAT_VIRTUALEDIT
8775 || curwin->w_cursor.coladd > 0
8776#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008777 )
8778 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008779 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780#ifdef FEAT_RIGHTLEFT
8781 && !revins_on
8782#endif
8783 )
8784 {
8785#ifdef FEAT_VIRTUALEDIT
8786 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8787 {
8788 oneleft();
8789 if (restart_edit != NUL)
8790 ++curwin->w_cursor.coladd;
8791 }
8792 else
8793#endif
8794 {
8795 --curwin->w_cursor.col;
8796#ifdef FEAT_MBYTE
8797 /* Correct cursor for multi-byte character. */
8798 if (has_mbyte)
8799 mb_adjust_cursor();
8800#endif
8801 }
8802 }
8803
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008804#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 /* Disable IM to allow typing English directly for Normal mode commands.
8806 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8807 * well). */
8808 if (!(State & LANGMAP))
8809 im_save_status(&curbuf->b_p_iminsert);
8810 im_set_active(FALSE);
8811#endif
8812
8813 State = NORMAL;
8814 /* need to position cursor again (e.g. when on a TAB ) */
8815 changed_cline_bef_curs();
8816
8817#ifdef FEAT_MOUSE
8818 setmouse();
8819#endif
8820#ifdef CURSOR_SHAPE
8821 ui_cursor_shape(); /* may show different cursor shape */
8822#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008823 if (!p_ek)
8824 /* Re-enable bracketed paste mode. */
8825 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826
8827 /*
8828 * When recording or for CTRL-O, need to display the new mode.
8829 * Otherwise remove the mode message.
8830 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008831 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 showmode();
8833 else if (p_smd)
8834 MSG("");
8835
8836 return TRUE; /* exit Insert mode */
8837}
8838
8839#ifdef FEAT_RIGHTLEFT
8840/*
8841 * Toggle language: hkmap and revins_on.
8842 * Move to end of reverse inserted text.
8843 */
8844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008845ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
8847 if (revins_on && revins_chars && revins_scol >= 0)
8848 {
8849 while (gchar_cursor() != NUL && revins_chars--)
8850 ++curwin->w_cursor.col;
8851 }
8852 p_ri = !p_ri;
8853 revins_on = (State == INSERT && p_ri);
8854 if (revins_on)
8855 {
8856 revins_scol = curwin->w_cursor.col;
8857 revins_legal++;
8858 revins_chars = 0;
8859 undisplay_dollar();
8860 }
8861 else
8862 revins_scol = -1;
8863#ifdef FEAT_FKMAP
8864 if (p_altkeymap)
8865 {
8866 /*
8867 * to be consistent also for redo command, using '.'
8868 * set arrow_used to true and stop it - causing to redo
8869 * characters entered in one mode (normal/reverse insert).
8870 */
8871 arrow_used = TRUE;
8872 (void)stop_arrow();
8873 p_fkmap = curwin->w_p_rl ^ p_ri;
8874 if (p_fkmap && p_ri)
8875 State = INSERT;
8876 }
8877 else
8878#endif
8879 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8880 showmode();
8881}
8882#endif
8883
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884/*
8885 * If 'keymodel' contains "startsel", may start selection.
8886 * Returns TRUE when a CTRL-O and other keys stuffed.
8887 */
8888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008889ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890{
8891 if (km_startsel)
8892 switch (c)
8893 {
8894 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 case K_PAGEUP:
8897 case K_KPAGEUP:
8898 case K_PAGEDOWN:
8899 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008900# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 case K_LEFT:
8902 case K_RIGHT:
8903 case K_UP:
8904 case K_DOWN:
8905 case K_END:
8906 case K_HOME:
8907# endif
8908 if (!(mod_mask & MOD_MASK_SHIFT))
8909 break;
8910 /* FALLTHROUGH */
8911 case K_S_LEFT:
8912 case K_S_RIGHT:
8913 case K_S_UP:
8914 case K_S_DOWN:
8915 case K_S_END:
8916 case K_S_HOME:
8917 /* Start selection right away, the cursor can move with
8918 * CTRL-O when beyond the end of the line. */
8919 start_selection();
8920
8921 /* Execute the key in (insert) Select mode. */
8922 stuffcharReadbuff(Ctrl_O);
8923 if (mod_mask)
8924 {
8925 char_u buf[4];
8926
8927 buf[0] = K_SPECIAL;
8928 buf[1] = KS_MODIFIER;
8929 buf[2] = mod_mask;
8930 buf[3] = NUL;
8931 stuffReadbuff(buf);
8932 }
8933 stuffcharReadbuff(c);
8934 return TRUE;
8935 }
8936 return FALSE;
8937}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938
8939/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008940 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008941 */
8942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008943ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008944{
8945#ifdef FEAT_FKMAP
8946 if (p_fkmap && p_ri)
8947 {
8948 beep_flush();
8949 EMSG(farsi_text_3); /* encoded in Farsi */
8950 return;
8951 }
8952#endif
8953
Bram Moolenaar1e015462005-09-25 22:16:38 +00008954# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008955 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008956 (char_u *)((State & REPLACE_FLAG) ? "i"
8957 : replaceState == VREPLACE ? "v"
8958 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008959# endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02008960 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008961 if (State & REPLACE_FLAG)
8962 State = INSERT | (State & LANGMAP);
8963 else
8964 State = replaceState | (State & LANGMAP);
8965 AppendCharToRedobuff(K_INS);
8966 showmode();
8967#ifdef CURSOR_SHAPE
8968 ui_cursor_shape(); /* may show different cursor shape */
8969#endif
8970}
8971
8972/*
8973 * Pressed CTRL-O in Insert mode.
8974 */
8975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008976ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008977{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008978 if (State & VREPLACE_FLAG)
8979 restart_edit = 'V';
8980 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008981 if (State & REPLACE_FLAG)
8982 restart_edit = 'R';
8983 else
8984 restart_edit = 'I';
8985#ifdef FEAT_VIRTUALEDIT
8986 if (virtual_active())
8987 ins_at_eol = FALSE; /* cursor always keeps its column */
8988 else
8989#endif
8990 ins_at_eol = (gchar_cursor() == NUL);
8991}
8992
8993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 * If the cursor is on an indent, ^T/^D insert/delete one
8995 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008996 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 * with vi. But vi only supports ^T and ^D after an
8998 * autoindent, we support it everywhere.
8999 */
9000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009001ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002{
9003 if (stop_arrow() == FAIL)
9004 return;
9005 AppendCharToRedobuff(c);
9006
9007 /*
9008 * 0^D and ^^D: remove all indent.
9009 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00009010 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
9011 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 {
9013 --curwin->w_cursor.col;
9014 (void)del_char(FALSE); /* delete the '^' or '0' */
9015 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9016 if (State & REPLACE_FLAG)
9017 replace_pop_ins();
9018 if (lastc == '^')
9019 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009020 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 }
9022 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009023 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024
9025 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9026 did_ai = FALSE;
9027#ifdef FEAT_SMARTINDENT
9028 did_si = FALSE;
9029 can_si = FALSE;
9030 can_si_back = FALSE;
9031#endif
9032#ifdef FEAT_CINDENT
9033 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9034#endif
9035}
9036
9037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009038ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
9040 int temp;
9041
9042 if (stop_arrow() == FAIL)
9043 return;
9044 if (gchar_cursor() == NUL) /* delete newline */
9045 {
9046 temp = curwin->w_cursor.col;
9047 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009048 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009049 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009051 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009053 /* Adjust orig_line_count in case more lines have been deleted than
9054 * have been added. That makes sure, that open_line() later
9055 * can access all buffer lines correctly */
9056 if (State & VREPLACE_FLAG &&
9057 orig_line_count > curbuf->b_ml.ml_line_count)
9058 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009061 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9062 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 did_ai = FALSE;
9064#ifdef FEAT_SMARTINDENT
9065 did_si = FALSE;
9066 can_si = FALSE;
9067 can_si_back = FALSE;
9068#endif
9069 AppendCharToRedobuff(K_DEL);
9070}
9071
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009072/*
9073 * Delete one character for ins_bs().
9074 */
9075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009076ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009077{
9078 dec_cursor();
9079 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9080 if (State & REPLACE_FLAG)
9081 {
9082 /* Don't delete characters before the insert point when in
9083 * Replace mode */
9084 if (curwin->w_cursor.lnum != Insstart.lnum
9085 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009086 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009087 }
9088 else
9089 (void)del_char(FALSE);
9090}
9091
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092/*
9093 * Handle Backspace, delete-word and delete-line in Insert mode.
9094 * Return TRUE when backspace was actually used.
9095 */
9096 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009097ins_bs(
9098 int c,
9099 int mode,
9100 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102 linenr_T lnum;
9103 int cc;
9104 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009105 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 colnr_T mincol;
9107 int did_backspace = FALSE;
9108 int in_indent;
9109 int oldState;
9110#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009111 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112#endif
9113
9114 /*
9115 * can't delete anything in an empty file
9116 * can't backup past first character in buffer
9117 * can't backup past starting point unless 'backspace' > 1
9118 * can backup to a previous line if 'backspace' == 0
9119 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009120 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 || (
9122#ifdef FEAT_RIGHTLEFT
9123 !revins_on &&
9124#endif
9125 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9126 || (!can_bs(BS_START)
9127 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009128 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9129 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9131 && curwin->w_cursor.col <= ai_col)
9132 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9133 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009134 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 return FALSE;
9136 }
9137
9138 if (stop_arrow() == FAIL)
9139 return FALSE;
9140 in_indent = inindent(0);
9141#ifdef FEAT_CINDENT
9142 if (in_indent)
9143 can_cindent = FALSE;
9144#endif
9145#ifdef FEAT_COMMENTS
9146 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9147#endif
9148#ifdef FEAT_RIGHTLEFT
9149 if (revins_on) /* put cursor after last inserted char */
9150 inc_cursor();
9151#endif
9152
9153#ifdef FEAT_VIRTUALEDIT
9154 /* Virtualedit:
9155 * BACKSPACE_CHAR eats a virtual space
9156 * BACKSPACE_WORD eats all coladd
9157 * BACKSPACE_LINE eats all coladd and keeps going
9158 */
9159 if (curwin->w_cursor.coladd > 0)
9160 {
9161 if (mode == BACKSPACE_CHAR)
9162 {
9163 --curwin->w_cursor.coladd;
9164 return TRUE;
9165 }
9166 if (mode == BACKSPACE_WORD)
9167 {
9168 curwin->w_cursor.coladd = 0;
9169 return TRUE;
9170 }
9171 curwin->w_cursor.coladd = 0;
9172 }
9173#endif
9174
9175 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009176 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 */
9178 if (curwin->w_cursor.col == 0)
9179 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009180 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009181 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182#ifdef FEAT_RIGHTLEFT
9183 || revins_on
9184#endif
9185 )
9186 {
9187 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9188 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9189 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009190 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009191 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 }
9193 /*
9194 * In replace mode:
9195 * cc < 0: NL was inserted, delete it
9196 * cc >= 0: NL was replaced, put original characters back
9197 */
9198 cc = -1;
9199 if (State & REPLACE_FLAG)
9200 cc = replace_pop(); /* returns -1 if NL was inserted */
9201 /*
9202 * In replace mode, in the line we started replacing, we only move the
9203 * cursor.
9204 */
9205 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9206 {
9207 dec_cursor();
9208 }
9209 else
9210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 if (!(State & VREPLACE_FLAG)
9212 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 {
9214 temp = gchar_cursor(); /* remember current char */
9215 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009216
9217 /* When "aw" is in 'formatoptions' we must delete the space at
9218 * the end of the line, otherwise the line will be broken
9219 * again when auto-formatting. */
9220 if (has_format_option(FO_AUTO)
9221 && has_format_option(FO_WHITE_PAR))
9222 {
9223 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9224 TRUE);
9225 int len;
9226
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009227 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009228 if (len > 0 && ptr[len - 1] == ' ')
9229 ptr[len - 1] = NUL;
9230 }
9231
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009232 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 if (temp == NUL && gchar_cursor() != NUL)
9234 inc_cursor();
9235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 else
9237 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238
9239 /*
9240 * In REPLACE mode we have to put back the text that was replaced
9241 * by the NL. On the replace stack is first a NUL-terminated
9242 * sequence of characters that were deleted and then the
9243 * characters that NL replaced.
9244 */
9245 if (State & REPLACE_FLAG)
9246 {
9247 /*
9248 * Do the next ins_char() in NORMAL state, to
9249 * prevent ins_char() from replacing characters and
9250 * avoiding showmatch().
9251 */
9252 oldState = State;
9253 State = NORMAL;
9254 /*
9255 * restore characters (blanks) deleted after cursor
9256 */
9257 while (cc > 0)
9258 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009259 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260#ifdef FEAT_MBYTE
9261 mb_replace_pop_ins(cc);
9262#else
9263 ins_char(cc);
9264#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009265 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 cc = replace_pop();
9267 }
9268 /* restore the characters that NL replaced */
9269 replace_pop_ins();
9270 State = oldState;
9271 }
9272 }
9273 did_ai = FALSE;
9274 }
9275 else
9276 {
9277 /*
9278 * Delete character(s) before the cursor.
9279 */
9280#ifdef FEAT_RIGHTLEFT
9281 if (revins_on) /* put cursor on last inserted char */
9282 dec_cursor();
9283#endif
9284 mincol = 0;
9285 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009286 if (mode == BACKSPACE_LINE
9287 && (curbuf->b_p_ai
9288#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009289 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009290#endif
9291 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292#ifdef FEAT_RIGHTLEFT
9293 && !revins_on
9294#endif
9295 )
9296 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009297 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009299 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009301 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 }
9303
9304 /*
9305 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9306 */
9307 if ( mode == BACKSPACE_CHAR
9308 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009309 || ((get_sts_value() != 0
9310#ifdef FEAT_VARTABS
9311 || tabstop_count(curbuf->b_p_vsts_array)
9312#endif
9313 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009314 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 && (*(ml_get_cursor() - 1) == TAB
9316 || (*(ml_get_cursor() - 1) == ' '
9317 && (!*inserted_space_p
9318 || arrow_used))))))
9319 {
9320 int ts;
9321 colnr_T vcol;
9322 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009323 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324
9325 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 /* Compute the virtual column where we want to be. Since
9327 * 'showbreak' may get in the way, need to get the last column of
9328 * the previous character. */
9329 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009330 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 dec_cursor();
9332 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9333 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009334#ifdef FEAT_VARTABS
9335 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009336 {
9337 ts = (int)get_sw_value(curbuf);
9338 want_vcol = (want_vcol / ts) * ts;
9339 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009340 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009341 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009342 curbuf->b_p_vsts_array);
9343#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009344 if (p_sta && in_indent)
9345 ts = (int)get_sw_value(curbuf);
9346 else
9347 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350
9351 /* delete characters until we are at or before want_vcol */
9352 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009353 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009354 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355
9356 /* insert extra spaces until we are at want_vcol */
9357 while (vcol < want_vcol)
9358 {
9359 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009360 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9361 && curwin->w_cursor.col < Insstart_orig.col)
9362 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 if (State & VREPLACE_FLAG)
9365 ins_char(' ');
9366 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 {
9368 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009369 if ((State & REPLACE_FLAG))
9370 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 }
9372 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9373 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009374
9375 /* If we are now back where we started delete one character. Can
9376 * happen when using 'sts' and 'linebreak'. */
9377 if (vcol >= start_vcol)
9378 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 }
9380
9381 /*
9382 * Delete upto starting point, start of line or previous word.
9383 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009384 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009386#ifdef FEAT_MBYTE
9387 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009389 if (has_mbyte)
9390 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009392 do
9393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009395 if (!revins_on) /* put cursor on char to be deleted */
9396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009398
9399 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009401 /* look multi-byte character class */
9402 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009404 prev_cclass = cclass;
9405 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009408
9409 /* start of word? */
9410 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9411 {
9412 mode = BACKSPACE_WORD_NOT_SPACE;
9413 temp = vim_iswordc(cc);
9414 }
9415 /* end of word? */
9416 else if (mode == BACKSPACE_WORD_NOT_SPACE
9417 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9418#ifdef FEAT_MBYTE
9419 || prev_cclass != cclass
9420#endif
9421 ))
9422 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009424 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009426 inc_cursor();
9427#ifdef FEAT_RIGHTLEFT
9428 else if (State & REPLACE_FLAG)
9429 dec_cursor();
9430#endif
9431 break;
9432 }
9433 if (State & REPLACE_FLAG)
9434 replace_do_bs(-1);
9435 else
9436 {
9437#ifdef FEAT_MBYTE
9438 if (enc_utf8 && p_deco)
9439 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9440#endif
9441 (void)del_char(FALSE);
9442#ifdef FEAT_MBYTE
9443 /*
9444 * If there are combining characters and 'delcombine' is set
9445 * move the cursor back. Don't back up before the base
9446 * character.
9447 */
9448 if (enc_utf8 && p_deco && cpc[0] != NUL)
9449 inc_cursor();
9450#endif
9451#ifdef FEAT_RIGHTLEFT
9452 if (revins_chars)
9453 {
9454 revins_chars--;
9455 revins_legal++;
9456 }
9457 if (revins_on && gchar_cursor() == NUL)
9458 break;
9459#endif
9460 }
9461 /* Just a single backspace?: */
9462 if (mode == BACKSPACE_CHAR)
9463 break;
9464 } while (
9465#ifdef FEAT_RIGHTLEFT
9466 revins_on ||
9467#endif
9468 (curwin->w_cursor.col > mincol
9469 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9470 || curwin->w_cursor.col != Insstart_orig.col)));
9471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 did_backspace = TRUE;
9473 }
9474#ifdef FEAT_SMARTINDENT
9475 did_si = FALSE;
9476 can_si = FALSE;
9477 can_si_back = FALSE;
9478#endif
9479 if (curwin->w_cursor.col <= 1)
9480 did_ai = FALSE;
9481 /*
9482 * It's a little strange to put backspaces into the redo
9483 * buffer, but it makes auto-indent a lot easier to deal
9484 * with.
9485 */
9486 AppendCharToRedobuff(c);
9487
9488 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009489 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009490 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009491 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492
9493 /* vi behaviour: the cursor moves backward but the character that
9494 * was there remains visible
9495 * Vim behaviour: the cursor moves backward and the character that
9496 * was there is erased from the screen.
9497 * We can emulate the vi behaviour by pretending there is a dollar
9498 * displayed even when there isn't.
9499 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009500 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 dollar_vcol = curwin->w_virtcol;
9502
Bram Moolenaarce3be472008-01-14 19:12:28 +00009503#ifdef FEAT_FOLDING
9504 /* When deleting a char the cursor line must never be in a closed fold.
9505 * E.g., when 'foldmethod' is indent and deleting the first non-white
9506 * char before a Tab. */
9507 if (did_backspace)
9508 foldOpenCursor();
9509#endif
9510
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 return did_backspace;
9512}
9513
9514#ifdef FEAT_MOUSE
9515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009516ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
9518 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009519 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520
9521# ifdef FEAT_GUI
9522 /* When GUI is active, also move/paste when 'mouse' is empty */
9523 if (!gui.in_use)
9524# endif
9525 if (!mouse_has(MOUSE_INSERT))
9526 return;
9527
9528 undisplay_dollar();
9529 tpos = curwin->w_cursor;
9530 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9531 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009532 win_T *new_curwin = curwin;
9533
9534 if (curwin != old_curwin && win_valid(old_curwin))
9535 {
9536 /* Mouse took us to another window. We need to go back to the
9537 * previous one to stop insert there properly. */
9538 curwin = old_curwin;
9539 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009540#ifdef FEAT_JOB_CHANNEL
9541 if (bt_prompt(curbuf))
9542 // Restart Insert mode when re-entering the prompt buffer.
9543 curbuf->b_prompt_insert = 'A';
9544#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009545 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009546 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009547 if (curwin != new_curwin && win_valid(new_curwin))
9548 {
9549 curwin = new_curwin;
9550 curbuf = curwin->w_buffer;
9551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552# ifdef FEAT_CINDENT
9553 can_cindent = TRUE;
9554# endif
9555 }
9556
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 /* redraw status lines (in case another window became active) */
9558 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559}
9560
9561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009562ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009565 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009566# ifdef FEAT_INS_EXPAND
9567 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568# endif
9569
9570 tpos = curwin->w_cursor;
9571
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009572 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 {
9574 int row, col;
9575
9576 row = mouse_row;
9577 col = mouse_col;
9578
9579 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009580 wp = mouse_find_win(&row, &col);
9581 if (wp == NULL)
9582 return;
9583 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 curbuf = curwin->w_buffer;
9585 }
9586 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 undisplay_dollar();
9588
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009589# ifdef FEAT_INS_EXPAND
9590 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009591 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009592# endif
9593 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009594 if (dir == MSCR_DOWN || dir == MSCR_UP)
9595 {
9596 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9597 scroll_redraw(dir,
9598 (long)(curwin->w_botline - curwin->w_topline));
9599 else
9600 scroll_redraw(dir, 3L);
9601 }
9602#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009603 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009604 {
9605 int val, step = 6;
9606
9607 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009608 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009609 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9610 if (val < 0)
9611 val = 0;
9612 gui_do_horiz_scroll(val, TRUE);
9613 }
9614#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009615# ifdef FEAT_INS_EXPAND
9616 did_scroll = TRUE;
9617# endif
9618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 curwin->w_redr_status = TRUE;
9621
9622 curwin = old_curwin;
9623 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009625# ifdef FEAT_INS_EXPAND
9626 /* The popup menu may overlay the window, need to redraw it.
9627 * TODO: Would be more efficient to only redraw the windows that are
9628 * overlapped by the popup menu. */
9629 if (pum_visible() && did_scroll)
9630 {
9631 redraw_all_later(NOT_VALID);
9632 ins_compl_show_pum();
9633 }
9634# endif
9635
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009636 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 {
9638 start_arrow(&tpos);
9639# ifdef FEAT_CINDENT
9640 can_cindent = TRUE;
9641# endif
9642 }
9643}
9644#endif
9645
Bram Moolenaarec2da362017-01-21 20:04:22 +01009646/*
9647 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9648 * P_PE literally.
9649 * When "drop" is TRUE then consume the text and drop it.
9650 */
9651 int
9652bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9653{
9654 int c;
9655 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9656 int idx = 0;
9657 char_u *end = find_termcode((char_u *)"PE");
9658 int ret_char = -1;
9659 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009660 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009661
9662 /* If the end code is too long we can't detect it, read everything. */
9663 if (STRLEN(end) >= NUMBUFLEN)
9664 end = NULL;
9665 ++no_mapping;
9666 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009667 if (!p_paste)
9668 // Also have the side effects of setting 'paste' to make it work much
9669 // faster.
9670 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009671
Bram Moolenaarec2da362017-01-21 20:04:22 +01009672 for (;;)
9673 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009674 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009675 if (end == NULL && vpeekc() == NUL)
9676 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009677 do
9678 {
9679 c = vgetc();
9680 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9681 if (c == NUL || got_int)
9682 // When CTRL-C was encountered the typeahead will be flushed and we
9683 // won't get the end sequence.
9684 break;
9685
Bram Moolenaarec2da362017-01-21 20:04:22 +01009686#ifdef FEAT_MBYTE
9687 if (has_mbyte)
9688 idx += (*mb_char2bytes)(c, buf + idx);
9689 else
9690#endif
9691 buf[idx++] = c;
9692 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009693 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009694 {
9695 if (end[idx] == NUL)
9696 break; /* Found the end of paste code. */
9697 continue;
9698 }
9699 if (!drop)
9700 {
9701 switch (mode)
9702 {
9703 case PASTE_CMDLINE:
9704 put_on_cmdline(buf, idx, TRUE);
9705 break;
9706
9707 case PASTE_EX:
9708 if (gap != NULL && ga_grow(gap, idx) == OK)
9709 {
9710 mch_memmove((char *)gap->ga_data + gap->ga_len,
9711 buf, (size_t)idx);
9712 gap->ga_len += idx;
9713 }
9714 break;
9715
9716 case PASTE_INSERT:
9717 if (stop_arrow() == OK)
9718 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009719 c = buf[0];
9720 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9721 ins_eol(c);
9722 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009723 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009724 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009725 AppendToRedobuffLit(buf, idx);
9726 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009727 }
9728 break;
9729
9730 case PASTE_ONE_CHAR:
9731 if (ret_char == -1)
9732 {
9733#ifdef FEAT_MBYTE
9734 if (has_mbyte)
9735 ret_char = (*mb_ptr2char)(buf);
9736 else
9737#endif
9738 ret_char = buf[0];
9739 }
9740 break;
9741 }
9742 }
9743 idx = 0;
9744 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009745
Bram Moolenaarec2da362017-01-21 20:04:22 +01009746 --no_mapping;
9747 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009748 if (!save_paste)
9749 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009750
9751 return ret_char;
9752}
9753
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009754#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009756ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009757{
9758 /* We will be leaving the current window, unless closing another tab. */
9759 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9760 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9761 {
9762 undisplay_dollar();
9763 start_arrow(&curwin->w_cursor);
9764# ifdef FEAT_CINDENT
9765 can_cindent = TRUE;
9766# endif
9767 }
9768
9769 if (c == K_TABLINE)
9770 goto_tabpage(current_tab);
9771 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009772 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009773 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009774 redraw_statuslines(); /* will redraw the tabline when needed */
9775 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009776}
9777#endif
9778
9779#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009781ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782{
9783 pos_T tpos;
9784
9785 undisplay_dollar();
9786 tpos = curwin->w_cursor;
9787 if (gui_do_scroll())
9788 {
9789 start_arrow(&tpos);
9790# ifdef FEAT_CINDENT
9791 can_cindent = TRUE;
9792# endif
9793 }
9794}
9795
9796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009797ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798{
9799 pos_T tpos;
9800
9801 undisplay_dollar();
9802 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009803 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 {
9805 start_arrow(&tpos);
9806# ifdef FEAT_CINDENT
9807 can_cindent = TRUE;
9808# endif
9809 }
9810}
9811#endif
9812
9813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009814ins_left(
9815 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816{
9817 pos_T tpos;
9818
9819#ifdef FEAT_FOLDING
9820 if ((fdo_flags & FDO_HOR) && KeyTyped)
9821 foldOpenCursor();
9822#endif
9823 undisplay_dollar();
9824 tpos = curwin->w_cursor;
9825 if (oneleft() == OK)
9826 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009827#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9828 /* Only call start_arrow() when not busy with preediting, it will
9829 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009830 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009831#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009832 {
9833 start_arrow_with_change(&tpos, end_change);
9834 if (!end_change)
9835 AppendCharToRedobuff(K_LEFT);
9836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837#ifdef FEAT_RIGHTLEFT
9838 /* If exit reversed string, position is fixed */
9839 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9840 revins_legal++;
9841 revins_chars++;
9842#endif
9843 }
9844
9845 /*
9846 * if 'whichwrap' set for cursor in insert mode may go to
9847 * previous line
9848 */
9849 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9850 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009851 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 start_arrow(&tpos);
9853 --(curwin->w_cursor.lnum);
9854 coladvance((colnr_T)MAXCOL);
9855 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9856 }
9857 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009858 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009859 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860}
9861
9862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009863ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864{
9865 pos_T tpos;
9866
9867#ifdef FEAT_FOLDING
9868 if ((fdo_flags & FDO_HOR) && KeyTyped)
9869 foldOpenCursor();
9870#endif
9871 undisplay_dollar();
9872 tpos = curwin->w_cursor;
9873 if (c == K_C_HOME)
9874 curwin->w_cursor.lnum = 1;
9875 curwin->w_cursor.col = 0;
9876#ifdef FEAT_VIRTUALEDIT
9877 curwin->w_cursor.coladd = 0;
9878#endif
9879 curwin->w_curswant = 0;
9880 start_arrow(&tpos);
9881}
9882
9883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009884ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
9886 pos_T tpos;
9887
9888#ifdef FEAT_FOLDING
9889 if ((fdo_flags & FDO_HOR) && KeyTyped)
9890 foldOpenCursor();
9891#endif
9892 undisplay_dollar();
9893 tpos = curwin->w_cursor;
9894 if (c == K_C_END)
9895 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9896 coladvance((colnr_T)MAXCOL);
9897 curwin->w_curswant = MAXCOL;
9898
9899 start_arrow(&tpos);
9900}
9901
9902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009903ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904{
9905#ifdef FEAT_FOLDING
9906 if ((fdo_flags & FDO_HOR) && KeyTyped)
9907 foldOpenCursor();
9908#endif
9909 undisplay_dollar();
9910 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9911 {
9912 start_arrow(&curwin->w_cursor);
9913 (void)bck_word(1L, FALSE, FALSE);
9914 curwin->w_set_curswant = TRUE;
9915 }
9916 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009917 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918}
9919
9920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009921ins_right(
9922 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923{
9924#ifdef FEAT_FOLDING
9925 if ((fdo_flags & FDO_HOR) && KeyTyped)
9926 foldOpenCursor();
9927#endif
9928 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009929 if (gchar_cursor() != NUL
9930#ifdef FEAT_VIRTUALEDIT
9931 || virtual_active()
9932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 )
9934 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009935 start_arrow_with_change(&curwin->w_cursor, end_change);
9936 if (!end_change)
9937 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 curwin->w_set_curswant = TRUE;
9939#ifdef FEAT_VIRTUALEDIT
9940 if (virtual_active())
9941 oneright();
9942 else
9943#endif
9944 {
9945#ifdef FEAT_MBYTE
9946 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009947 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 else
9949#endif
9950 ++curwin->w_cursor.col;
9951 }
9952
9953#ifdef FEAT_RIGHTLEFT
9954 revins_legal++;
9955 if (revins_chars)
9956 revins_chars--;
9957#endif
9958 }
9959 /* if 'whichwrap' set for cursor in insert mode, may move the
9960 * cursor to the next line */
9961 else if (vim_strchr(p_ww, ']') != NULL
9962 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9963 {
9964 start_arrow(&curwin->w_cursor);
9965 curwin->w_set_curswant = TRUE;
9966 ++curwin->w_cursor.lnum;
9967 curwin->w_cursor.col = 0;
9968 }
9969 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009970 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009971 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972}
9973
9974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009975ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976{
9977#ifdef FEAT_FOLDING
9978 if ((fdo_flags & FDO_HOR) && KeyTyped)
9979 foldOpenCursor();
9980#endif
9981 undisplay_dollar();
9982 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9983 || gchar_cursor() != NUL)
9984 {
9985 start_arrow(&curwin->w_cursor);
9986 (void)fwd_word(1L, FALSE, 0);
9987 curwin->w_set_curswant = TRUE;
9988 }
9989 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009990 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991}
9992
9993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009994ins_up(
9995 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996{
9997 pos_T tpos;
9998 linenr_T old_topline = curwin->w_topline;
9999#ifdef FEAT_DIFF
10000 int old_topfill = curwin->w_topfill;
10001#endif
10002
10003 undisplay_dollar();
10004 tpos = curwin->w_cursor;
10005 if (cursor_up(1L, TRUE) == OK)
10006 {
10007 if (startcol)
10008 coladvance(getvcol_nolist(&Insstart));
10009 if (old_topline != curwin->w_topline
10010#ifdef FEAT_DIFF
10011 || old_topfill != curwin->w_topfill
10012#endif
10013 )
10014 redraw_later(VALID);
10015 start_arrow(&tpos);
10016#ifdef FEAT_CINDENT
10017 can_cindent = TRUE;
10018#endif
10019 }
10020 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010021 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022}
10023
10024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010025ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
10027 pos_T tpos;
10028
10029 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010030
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010031 if (mod_mask & MOD_MASK_CTRL)
10032 {
10033 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010034 if (first_tabpage->tp_next != NULL)
10035 {
10036 start_arrow(&curwin->w_cursor);
10037 goto_tabpage(-1);
10038 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010039 return;
10040 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010041
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 tpos = curwin->w_cursor;
10043 if (onepage(BACKWARD, 1L) == OK)
10044 {
10045 start_arrow(&tpos);
10046#ifdef FEAT_CINDENT
10047 can_cindent = TRUE;
10048#endif
10049 }
10050 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010051 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052}
10053
10054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010055ins_down(
10056 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057{
10058 pos_T tpos;
10059 linenr_T old_topline = curwin->w_topline;
10060#ifdef FEAT_DIFF
10061 int old_topfill = curwin->w_topfill;
10062#endif
10063
10064 undisplay_dollar();
10065 tpos = curwin->w_cursor;
10066 if (cursor_down(1L, TRUE) == OK)
10067 {
10068 if (startcol)
10069 coladvance(getvcol_nolist(&Insstart));
10070 if (old_topline != curwin->w_topline
10071#ifdef FEAT_DIFF
10072 || old_topfill != curwin->w_topfill
10073#endif
10074 )
10075 redraw_later(VALID);
10076 start_arrow(&tpos);
10077#ifdef FEAT_CINDENT
10078 can_cindent = TRUE;
10079#endif
10080 }
10081 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010082 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083}
10084
10085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010086ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
10088 pos_T tpos;
10089
10090 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010091
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010092 if (mod_mask & MOD_MASK_CTRL)
10093 {
10094 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010095 if (first_tabpage->tp_next != NULL)
10096 {
10097 start_arrow(&curwin->w_cursor);
10098 goto_tabpage(0);
10099 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010100 return;
10101 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010102
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 tpos = curwin->w_cursor;
10104 if (onepage(FORWARD, 1L) == OK)
10105 {
10106 start_arrow(&tpos);
10107#ifdef FEAT_CINDENT
10108 can_cindent = TRUE;
10109#endif
10110 }
10111 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010112 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113}
10114
10115#ifdef FEAT_DND
10116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010117ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118{
10119 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10120}
10121#endif
10122
10123/*
10124 * Handle TAB in Insert or Replace mode.
10125 * Return TRUE when the TAB needs to be inserted like a normal character.
10126 */
10127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010128ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129{
10130 int ind;
10131 int i;
10132 int temp;
10133
10134 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10135 Insstart_blank_vcol = get_nolist_virtcol();
10136 if (echeck_abbr(TAB + ABBR_OFF))
10137 return FALSE;
10138
10139 ind = inindent(0);
10140#ifdef FEAT_CINDENT
10141 if (ind)
10142 can_cindent = FALSE;
10143#endif
10144
10145 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010146 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 */
10148 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010149#ifdef FEAT_VARTABS
10150 && !(p_sta && ind
10151 /* These five lines mean 'tabstop' != 'shiftwidth' */
10152 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
10153 || (tabstop_count(curbuf->b_p_vts_array) == 1
10154 && tabstop_first(curbuf->b_p_vts_array)
10155 != get_sw_value(curbuf))
10156 || (tabstop_count(curbuf->b_p_vts_array) == 0
10157 && curbuf->b_p_ts != get_sw_value(curbuf))))
10158 && tabstop_count(curbuf->b_p_vsts_array) == 0
10159#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010160 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010161#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010162 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 return TRUE;
10164
10165 if (stop_arrow() == FAIL)
10166 return TRUE;
10167
10168 did_ai = FALSE;
10169#ifdef FEAT_SMARTINDENT
10170 did_si = FALSE;
10171 can_si = FALSE;
10172 can_si_back = FALSE;
10173#endif
10174 AppendToRedobuff((char_u *)"\t");
10175
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010176#ifdef FEAT_VARTABS
10177 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10178 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010179 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010180 temp -= get_nolist_virtcol() % temp;
10181 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010182 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010183 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010184 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010185 curbuf->b_p_vsts_array);
10186 else /* otherwise use 'tabstop' */
10187 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10188 curbuf->b_p_vts_array);
10189#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010191 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010192 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10193 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 else /* otherwise use 'tabstop' */
10195 temp = (int)curbuf->b_p_ts;
10196 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198
10199 /*
10200 * Insert the first space with ins_char(). It will delete one char in
10201 * replace mode. Insert the rest with ins_str(); it will not delete any
10202 * chars. For VREPLACE mode, we use ins_char() for all characters.
10203 */
10204 ins_char(' ');
10205 while (--temp > 0)
10206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 if (State & VREPLACE_FLAG)
10208 ins_char(' ');
10209 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 {
10211 ins_str((char_u *)" ");
10212 if (State & REPLACE_FLAG) /* no char replaced */
10213 replace_push(NUL);
10214 }
10215 }
10216
10217 /*
10218 * When 'expandtab' not set: Replace spaces by TABs where possible.
10219 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010220#ifdef FEAT_VARTABS
10221 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10222 || get_sts_value() > 0
10223 || (p_sta && ind)))
10224#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010225 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 {
10228 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 char_u *saved_line = NULL; /* init for GCC */
10230 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 pos_T fpos;
10232 pos_T *cursor;
10233 colnr_T want_vcol, vcol;
10234 int change_col = -1;
10235 int save_list = curwin->w_p_list;
10236
10237 /*
10238 * Get the current line. For VREPLACE mode, don't make real changes
10239 * yet, just work on a copy of the line.
10240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 if (State & VREPLACE_FLAG)
10242 {
10243 pos = curwin->w_cursor;
10244 cursor = &pos;
10245 saved_line = vim_strsave(ml_get_curline());
10246 if (saved_line == NULL)
10247 return FALSE;
10248 ptr = saved_line + pos.col;
10249 }
10250 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 {
10252 ptr = ml_get_cursor();
10253 cursor = &curwin->w_cursor;
10254 }
10255
10256 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10257 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10258 curwin->w_p_list = FALSE;
10259
10260 /* Find first white before the cursor */
10261 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010262 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 {
10264 --fpos.col;
10265 --ptr;
10266 }
10267
10268 /* In Replace mode, don't change characters before the insert point. */
10269 if ((State & REPLACE_FLAG)
10270 && fpos.lnum == Insstart.lnum
10271 && fpos.col < Insstart.col)
10272 {
10273 ptr += Insstart.col - fpos.col;
10274 fpos.col = Insstart.col;
10275 }
10276
10277 /* compute virtual column numbers of first white and cursor */
10278 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10279 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10280
Bram Moolenaar597a4222014-06-25 14:39:50 +020010281 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10282 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010283 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010285 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 if (vcol + i > want_vcol)
10287 break;
10288 if (*ptr != TAB)
10289 {
10290 *ptr = TAB;
10291 if (change_col < 0)
10292 {
10293 change_col = fpos.col; /* Column of first change */
10294 /* May have to adjust Insstart */
10295 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10296 Insstart.col = fpos.col;
10297 }
10298 }
10299 ++fpos.col;
10300 ++ptr;
10301 vcol += i;
10302 }
10303
10304 if (change_col >= 0)
10305 {
10306 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010307 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308
10309 /* Skip over the spaces we need. */
10310 while (vcol < want_vcol && *ptr == ' ')
10311 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010312 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 ++ptr;
10314 ++repl_off;
10315 }
10316 if (vcol > want_vcol)
10317 {
10318 /* Must have a char with 'showbreak' just before it. */
10319 --ptr;
10320 --repl_off;
10321 }
10322 fpos.col += repl_off;
10323
10324 /* Delete following spaces. */
10325 i = cursor->col - fpos.col;
10326 if (i > 0)
10327 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010328 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010330 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 for (temp = i; --temp >= 0; )
10332 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010010333#ifdef FEAT_TEXT_PROP
10334 curbuf->b_ml.ml_line_len -= i;
10335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010337#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010338 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010339 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010340 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010341 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10342 (char_u *)"\t", 1);
10343 }
10344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 cursor->col -= i;
10346
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 /*
10348 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10349 * backspacing over the changed spacing and then inserting the new
10350 * spacing.
10351 */
10352 if (State & VREPLACE_FLAG)
10353 {
10354 /* Backspace from real cursor to change_col */
10355 backspace_until_column(change_col);
10356
10357 /* Insert each char in saved_line from changed_col to
10358 * ptr-cursor */
10359 ins_bytes_len(saved_line + change_col,
10360 cursor->col - change_col);
10361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 }
10363
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 if (State & VREPLACE_FLAG)
10365 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 curwin->w_p_list = save_list;
10367 }
10368
10369 return FALSE;
10370}
10371
10372/*
10373 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010374 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 */
10376 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010377ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378{
10379 int i;
10380
10381 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010382 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010384 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 undisplay_dollar();
10386
10387 /*
10388 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10389 * character under the cursor. Only push a NUL on the replace stack,
10390 * nothing to put back when the NL is deleted.
10391 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010392 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 replace_push(NUL);
10394
10395 /*
10396 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10397 * replacing the next line, so we push all of the characters left on the
10398 * line onto the replace stack. This is not done here though, it is done
10399 * in open_line().
10400 */
10401
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010402#ifdef FEAT_VIRTUALEDIT
10403 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10404 * CTRL-O). */
10405 if (virtual_active() && curwin->w_cursor.coladd > 0)
10406 coladvance(getviscol());
10407#endif
10408
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409#ifdef FEAT_RIGHTLEFT
10410# ifdef FEAT_FKMAP
10411 if (p_altkeymap && p_fkmap)
10412 fkmap(NL);
10413# endif
10414 /* NL in reverse insert will always start in the end of
10415 * current line. */
10416 if (revins_on)
10417 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10418#endif
10419
10420 AppendToRedobuff(NL_STR);
10421 i = open_line(FORWARD,
10422#ifdef FEAT_COMMENTS
10423 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10424#endif
10425 0, old_indent);
10426 old_indent = 0;
10427#ifdef FEAT_CINDENT
10428 can_cindent = TRUE;
10429#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010430#ifdef FEAT_FOLDING
10431 /* When inserting a line the cursor line must never be in a closed fold. */
10432 foldOpenCursor();
10433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010435 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436}
10437
10438#ifdef FEAT_DIGRAPHS
10439/*
10440 * Handle digraph in insert mode.
10441 * Returns character still to be inserted, or NUL when nothing remaining to be
10442 * done.
10443 */
10444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010445ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446{
10447 int c;
10448 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010449 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450
10451 pc_status = PC_STATUS_UNSET;
10452 if (redrawing() && !char_avail())
10453 {
10454 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010455 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456
10457 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010458 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459#ifdef FEAT_CMDL_INFO
10460 add_to_showcmd_c(Ctrl_K);
10461#endif
10462 }
10463
10464#ifdef USE_ON_FLY_SCROLL
10465 dont_scroll = TRUE; /* disallow scrolling here */
10466#endif
10467
10468 /* don't map the digraph chars. This also prevents the
10469 * mode message to be deleted when ESC is hit */
10470 ++no_mapping;
10471 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010472 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 --no_mapping;
10474 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010475 if (did_putchar)
10476 /* when the line fits in 'columns' the '?' is at the start of the next
10477 * line and will not be removed by the redraw */
10478 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010479
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 if (IS_SPECIAL(c) || mod_mask) /* special key */
10481 {
10482#ifdef FEAT_CMDL_INFO
10483 clear_showcmd();
10484#endif
10485 insert_special(c, TRUE, FALSE);
10486 return NUL;
10487 }
10488 if (c != ESC)
10489 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010490 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 if (redrawing() && !char_avail())
10492 {
10493 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010494 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495
10496 if (char2cells(c) == 1)
10497 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010498 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010500 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 }
10502#ifdef FEAT_CMDL_INFO
10503 add_to_showcmd_c(c);
10504#endif
10505 }
10506 ++no_mapping;
10507 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010508 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 --no_mapping;
10510 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010511 if (did_putchar)
10512 /* when the line fits in 'columns' the '?' is at the start of the
10513 * next line and will not be removed by a redraw */
10514 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 if (cc != ESC)
10516 {
10517 AppendToRedobuff((char_u *)CTRL_V_STR);
10518 c = getdigraph(c, cc, TRUE);
10519#ifdef FEAT_CMDL_INFO
10520 clear_showcmd();
10521#endif
10522 return c;
10523 }
10524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525#ifdef FEAT_CMDL_INFO
10526 clear_showcmd();
10527#endif
10528 return NUL;
10529}
10530#endif
10531
10532/*
10533 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10534 * Returns the char to be inserted, or NUL if none found.
10535 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010536 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010537ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538{
10539 int c;
10540 int temp;
10541 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010542 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543
10544 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10545 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010546 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 return NUL;
10548 }
10549
10550 /* try to advance to the cursor column */
10551 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010552 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 prev_ptr = ptr;
10554 validate_virtcol();
10555 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10556 {
10557 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010558 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 }
10560 if ((colnr_T)temp > curwin->w_virtcol)
10561 ptr = prev_ptr;
10562
10563#ifdef FEAT_MBYTE
10564 c = (*mb_ptr2char)(ptr);
10565#else
10566 c = *ptr;
10567#endif
10568 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010569 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 return c;
10571}
10572
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010573/*
10574 * CTRL-Y or CTRL-E typed in Insert mode.
10575 */
10576 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010577ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010578{
10579 int c = tc;
10580
10581#ifdef FEAT_INS_EXPAND
10582 if (ctrl_x_mode == CTRL_X_SCROLL)
10583 {
10584 if (c == Ctrl_Y)
10585 scrolldown_clamp();
10586 else
10587 scrollup_clamp();
10588 redraw_later(VALID);
10589 }
10590 else
10591#endif
10592 {
10593 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10594 if (c != NUL)
10595 {
10596 long tw_save;
10597
10598 /* The character must be taken literally, insert like it
10599 * was typed after a CTRL-V, and pretend 'textwidth'
10600 * wasn't set. Digits, 'o' and 'x' are special after a
10601 * CTRL-V, don't use it for these. */
10602 if (c < 256 && !isalnum(c))
10603 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10604 tw_save = curbuf->b_p_tw;
10605 curbuf->b_p_tw = -1;
10606 insert_special(c, TRUE, FALSE);
10607 curbuf->b_p_tw = tw_save;
10608#ifdef FEAT_RIGHTLEFT
10609 revins_chars++;
10610 revins_legal++;
10611#endif
10612 c = Ctrl_V; /* pretend CTRL-V is last character */
10613 auto_format(FALSE, TRUE);
10614 }
10615 }
10616 return c;
10617}
10618
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619#ifdef FEAT_SMARTINDENT
10620/*
10621 * Try to do some very smart auto-indenting.
10622 * Used when inserting a "normal" character.
10623 */
10624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010625ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626{
10627 pos_T *pos, old_pos;
10628 char_u *ptr;
10629 int i;
10630 int temp;
10631
10632 /*
10633 * do some very smart indenting when entering '{' or '}'
10634 */
10635 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10636 {
10637 /*
10638 * for '}' set indent equal to indent of line containing matching '{'
10639 */
10640 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10641 {
10642 old_pos = curwin->w_cursor;
10643 /*
10644 * If the matching '{' has a ')' immediately before it (ignoring
10645 * white-space), then line up with the start of the line
10646 * containing the matching '(' if there is one. This handles the
10647 * case where an "if (..\n..) {" statement continues over multiple
10648 * lines -- webb
10649 */
10650 ptr = ml_get(pos->lnum);
10651 i = pos->col;
10652 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010653 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 ;
10655 curwin->w_cursor.lnum = pos->lnum;
10656 curwin->w_cursor.col = i;
10657 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10658 curwin->w_cursor = *pos;
10659 i = get_indent();
10660 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010662 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 (void)set_indent(i, SIN_CHANGED);
10665 }
10666 else if (curwin->w_cursor.col > 0)
10667 {
10668 /*
10669 * when inserting '{' after "O" reduce indent, but not
10670 * more than indent of previous line
10671 */
10672 temp = TRUE;
10673 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10674 {
10675 old_pos = curwin->w_cursor;
10676 i = get_indent();
10677 while (curwin->w_cursor.lnum > 1)
10678 {
10679 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10680
10681 /* ignore empty lines and lines starting with '#'. */
10682 if (*ptr != '#' && *ptr != NUL)
10683 break;
10684 }
10685 if (get_indent() >= i)
10686 temp = FALSE;
10687 curwin->w_cursor = old_pos;
10688 }
10689 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010690 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 }
10692 }
10693
10694 /*
10695 * set indent of '#' always to 0
10696 */
10697 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10698 {
10699 /* remember current indent for next line */
10700 old_indent = get_indent();
10701 (void)set_indent(0, SIN_CHANGED);
10702 }
10703
10704 /* Adjust ai_col, the char at this position can be deleted. */
10705 if (ai_col > curwin->w_cursor.col)
10706 ai_col = curwin->w_cursor.col;
10707}
10708#endif
10709
10710/*
10711 * Get the value that w_virtcol would have when 'list' is off.
10712 * Unless 'cpo' contains the 'L' flag.
10713 */
Bram Moolenaarf9514162018-11-22 03:08:29 +010010714 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010715get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010717 // check validity of cursor in current buffer
10718 if (curwin->w_buffer == NULL
10719 || curwin->w_buffer->b_ml.ml_mfp == NULL
10720 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
10721 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10723 return getvcol_nolist(&curwin->w_cursor);
10724 validate_virtcol();
10725 return curwin->w_virtcol;
10726}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010727
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010728#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010729/*
10730 * Handle the InsertCharPre autocommand.
10731 * "c" is the character that was typed.
10732 * Return a pointer to allocated memory with the replacement string.
10733 * Return NULL to continue inserting "c".
10734 */
10735 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010736do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010737{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010738 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010739 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010740 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010741
10742 /* Return quickly when there is nothing to do. */
10743 if (!has_insertcharpre())
10744 return NULL;
10745
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010746# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010747 if (has_mbyte)
10748 buf[(*mb_char2bytes)(c, buf)] = NUL;
10749 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010750# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010751 {
10752 buf[0] = c;
10753 buf[1] = NUL;
10754 }
10755
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010756 /* Lock the text to avoid weird things from happening. */
10757 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010758 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010759
Bram Moolenaar704984a2012-06-01 14:57:51 +020010760 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010761 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010762 {
10763 /* Get the value of v:char. It may be empty or more than one
10764 * character. Only use it when changed, otherwise continue with the
10765 * original character to avoid breaking autoindent. */
10766 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10767 res = vim_strsave(get_vim_var_str(VV_CHAR));
10768 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010769
10770 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10771 --textlock;
10772
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010773 // Restore the State, it may have been changed.
10774 State = save_State;
10775
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010776 return res;
10777}
10778#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010779
10780/*
10781 * Trigger "event" and take care of fixing undo.
10782 */
10783 static int
10784ins_apply_autocmds(event_T event)
10785{
10786 varnumber_T tick = CHANGEDTICK(curbuf);
10787 int r;
10788
10789 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
10790
10791 // If u_savesub() was called then we are not prepared to start
10792 // a new line. Call u_save() with no contents to fix that.
10793 if (tick != CHANGEDTICK(curbuf))
10794 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
10795
10796 return r;
10797}