blob: 92dbf4cb5111ec3524048cebe3abba3958ea3b82 [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;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100153static int compl_opt_suppress_empty = FALSE;
Bram Moolenaar82139082011-09-14 16:52:09 +0200154
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100155static void ins_ctrl_x(void);
156static int has_compl_option(int dict_opt);
157static int ins_compl_accept_char(int c);
158static 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 +0100159static void ins_compl_longest_match(compl_T *match);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100160static void ins_compl_del_pum(void);
161static int pum_wanted(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100162static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
163static char_u *find_line_end(char_u *ptr);
164static void ins_compl_free(void);
165static void ins_compl_clear(void);
166static int ins_compl_bs(void);
167static int ins_compl_need_restart(void);
168static void ins_compl_new_leader(void);
169static void ins_compl_addleader(int c);
170static int ins_compl_len(void);
171static void ins_compl_restart(void);
172static void ins_compl_set_original_text(char_u *str);
173static void ins_compl_addfrommatch(void);
174static int ins_compl_prep(int c);
175static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100176# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100177static void ins_compl_add_list(list_T *list);
178static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100179# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200181static void ins_compl_insert(int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100182static int ins_compl_key2dir(int c);
183static int ins_compl_pum_key(int c);
184static int ins_compl_key2count(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100185static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100186static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#endif /* FEAT_INS_EXPAND */
189
190#define BACKSPACE_CHAR 1
191#define BACKSPACE_WORD 2
192#define BACKSPACE_WORD_NOT_SPACE 3
193#define BACKSPACE_LINE 4
194
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100195static void ins_redraw(int ready);
196static void ins_ctrl_v(void);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200197#ifdef FEAT_JOB_CHANNEL
198static void init_prompt(int cmdchar_todo);
199#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100200static void undisplay_dollar(void);
201static void insert_special(int, int, int);
202static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
203static void check_auto_format(int);
204static void redo_literal(int c);
205static void start_arrow(pos_T *end_insert_pos);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100206static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000207#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100208static void check_spell_redraw(void);
209static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000210static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000211#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100212static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
213static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100214static void replace_join(int off);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void mb_replace_pop_ins(int cc);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100216static void replace_flush(void);
217static void replace_do_bs(int limit_col);
218static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100220static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ins_reg(void);
223static void ins_ctrl_g(void);
224static void ins_ctrl_hat(void);
225static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static int ins_start_select(int c);
230static void ins_insert(int replaceState);
231static void ins_ctrl_o(void);
232static void ins_shift(int c, int lastc);
233static void ins_del(void);
234static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ins_mouse(int c);
237static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000239#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ins_left(int end_change);
243static void ins_home(int c);
244static void ins_end(int c);
245static void ins_s_left(void);
246static void ins_right(int end_change);
247static void ins_s_right(void);
248static void ins_up(int startcol);
249static void ins_pageup(void);
250static void ins_down(int startcol);
251static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100255static int ins_tab(void);
256static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100264#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100266#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200267static int ins_apply_autocmds(event_T event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
269static colnr_T Insstart_textlen; /* length of line when insert started */
270static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100271static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
273static char_u *last_insert = NULL; /* the text of the previous insert,
274 K_SPECIAL and CSI are escaped */
275static int last_insert_skip; /* nr of chars in front of previous insert */
276static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000277static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
279#ifdef FEAT_CINDENT
280static int can_cindent; /* may do cindenting on this line */
281#endif
282
283static int old_indent = 0; /* for ^^D command in insert mode */
284
285#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000286static int revins_on; /* reverse insert mode on */
287static int revins_chars; /* how much to skip after edit */
288static int revins_legal; /* was the last char 'legal'? */
289static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290#endif
291
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292static int ins_need_undo; /* call u_save() before inserting a
293 char. Set when edit() is called.
294 after that arrow_used is used. */
295
296static int did_add_space = FALSE; /* auto_format() added an extra space
297 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200298static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
299 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300
301/*
302 * edit(): Start inserting text.
303 *
304 * "cmdchar" can be:
305 * 'i' normal insert command
306 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100307 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 * 'R' replace command
309 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
310 * but still only one <CR> is inserted. The <Esc> is not used for redo.
311 * 'g' "gI" command.
312 * 'V' "gR" command for Virtual Replace mode.
313 * 'v' "gr" command for single character Virtual Replace mode.
314 *
315 * This function is not called recursively. For CTRL-O commands, it returns
316 * and lets the caller handle the Normal-mode command.
317 *
318 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
319 */
320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100321edit(
322 int cmdchar,
323 int startln, /* if set, insert at start of line */
324 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325{
326 int c = 0;
327 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100328 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000329 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 int i;
332 int did_backspace = TRUE; /* previous char was backspace */
333#ifdef FEAT_CINDENT
334 int line_is_white = FALSE; /* line is empty before insert */
335#endif
336 linenr_T old_topline = 0; /* topline before insertion */
337#ifdef FEAT_DIFF
338 int old_topfill = -1;
339#endif
340 int inserted_space = FALSE; /* just inserted a space */
341 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000342 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200343#ifdef FEAT_JOB_CHANNEL
344 int cmdchar_todo = cmdchar;
345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000347 /* Remember whether editing was restarted after CTRL-O. */
348 did_restart_edit = restart_edit;
349
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 /* sleep before redrawing, needed for "CTRL-O :" that results in an
351 * error message */
352 check_for_delay(TRUE);
353
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100354 /* set Insstart_orig to Insstart */
355 update_Insstart_orig = TRUE;
356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357#ifdef HAVE_SANDBOX
358 /* Don't allow inserting in the sandbox. */
359 if (sandbox != 0)
360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100361 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 return FALSE;
363 }
364#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000365 /* Don't allow changes in the buffer while editing the cmdline. The
366 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000367 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100369 emsg(_(e_secure));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000370 return FALSE;
371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000374 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000375 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000376 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100377 emsg(_(e_secure));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000378 return FALSE;
379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 ins_compl_clear(); /* clear stuff for CTRL-X mode */
381#endif
382
Bram Moolenaar843ee412004-06-30 16:16:41 +0000383 /*
384 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
385 */
386 if (cmdchar != 'r' && cmdchar != 'v')
387 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100388 pos_T save_cursor = curwin->w_cursor;
389
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100390#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000391 if (cmdchar == 'R')
392 ptr = (char_u *)"r";
393 else if (cmdchar == 'V')
394 ptr = (char_u *)"v";
395 else
396 ptr = (char_u *)"i";
397 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200398 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100399#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200400 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100401
Bram Moolenaar097c9922013-05-19 21:15:15 +0200402 /* Make sure the cursor didn't move. Do call check_cursor_col() in
403 * case the text was modified. Since Insert mode was not started yet
404 * a call to check_cursor_col() may move the cursor, especially with
405 * the "A" command, thus set State to avoid that. Also check that the
406 * line number is still valid (lines may have been deleted).
407 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100408 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100409#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200410 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100411#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200412 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100413 {
414 int save_state = State;
415
416 curwin->w_cursor = save_cursor;
417 State = INSERT;
418 check_cursor_col();
419 State = save_state;
420 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000421 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000422
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200423#ifdef FEAT_CONCEAL
424 /* Check if the cursor line needs redrawing before changing State. If
425 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200426 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200427#endif
428
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429#ifdef FEAT_MOUSE
430 /*
431 * When doing a paste with the middle mouse button, Insstart is set to
432 * where the paste started.
433 */
434 if (where_paste_started.lnum != 0)
435 Insstart = where_paste_started;
436 else
437#endif
438 {
439 Insstart = curwin->w_cursor;
440 if (startln)
441 Insstart.col = 0;
442 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000443 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 Insstart_blank_vcol = MAXCOL;
445 if (!did_ai)
446 ai_col = 0;
447
448 if (cmdchar != NUL && restart_edit == 0)
449 {
450 ResetRedobuff();
451 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 if (cmdchar == 'V' || cmdchar == 'v')
453 {
454 /* "gR" or "gr" command */
455 AppendCharToRedobuff('g');
456 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
457 }
458 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100460 if (cmdchar == K_PS)
461 AppendCharToRedobuff('a');
462 else
463 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 if (cmdchar == 'g') /* "gI" command */
465 AppendCharToRedobuff('I');
466 else if (cmdchar == 'r') /* "r<CR>" command */
467 count = 1; /* insert only one <CR> */
468 }
469 }
470
471 if (cmdchar == 'R')
472 {
473#ifdef FEAT_FKMAP
474 if (p_fkmap && p_ri)
475 {
476 beep_flush();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100477 emsg(farsi_text_3); /* encoded in Farsi */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 State = INSERT;
479 }
480 else
481#endif
482 State = REPLACE;
483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 else if (cmdchar == 'V' || cmdchar == 'v')
485 {
486 State = VREPLACE;
487 replaceState = VREPLACE;
488 orig_line_count = curbuf->b_ml.ml_line_count;
489 vr_lines_changed = 1;
490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 else
492 State = INSERT;
493
494 stop_insert_mode = FALSE;
495
496 /*
497 * Need to recompute the cursor position, it might move when the cursor is
498 * on a TAB or special character.
499 */
500 curs_columns(TRUE);
501
502 /*
503 * Enable langmap or IME, indicated by 'iminsert'.
504 * Note that IME may enabled/disabled without us noticing here, thus the
505 * 'iminsert' value may not reflect what is actually used. It is updated
506 * when hitting <Esc>.
507 */
508 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
509 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100510#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
512#endif
513
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514#ifdef FEAT_MOUSE
515 setmouse();
516#endif
517#ifdef FEAT_CMDL_INFO
518 clear_showcmd();
519#endif
520#ifdef FEAT_RIGHTLEFT
521 /* there is no reverse replace mode */
522 revins_on = (State == INSERT && p_ri);
523 if (revins_on)
524 undisplay_dollar();
525 revins_chars = 0;
526 revins_legal = 0;
527 revins_scol = -1;
528#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100529 if (!p_ek)
530 /* Disable bracketed paste mode, we won't recognize the escape
531 * sequences. */
532 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533
534 /*
535 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100536 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
537 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 */
539 if (restart_edit != 0 && stuff_empty())
540 {
541#ifdef FEAT_MOUSE
542 /*
543 * After a paste we consider text typed to be part of the insert for
544 * the pasted text. You can backspace over the pasted text too.
545 */
546 if (where_paste_started.lnum)
547 arrow_used = FALSE;
548 else
549#endif
550 arrow_used = TRUE;
551 restart_edit = 0;
552
553 /*
554 * If the cursor was after the end-of-line before the CTRL-O and it is
555 * now at the end-of-line, put it after the end-of-line (this is not
556 * correct in very rare cases).
557 * Also do this if curswant is greater than the current virtual
558 * column. Eg after "^O$" or "^O80|".
559 */
560 validate_virtcol();
561 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000562 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 || curwin->w_curswant > curwin->w_virtcol)
564 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
565 {
566 if (ptr[1] == NUL)
567 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 else if (has_mbyte)
569 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000570 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (ptr[i] == NUL)
572 curwin->w_cursor.col += i;
573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000575 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 }
577 else
578 arrow_used = FALSE;
579
580 /* we are in insert mode now, don't need to start it anymore */
581 need_start_insertmode = FALSE;
582
583 /* Need to save the line for undo before inserting the first char. */
584 ins_need_undo = TRUE;
585
586#ifdef FEAT_MOUSE
587 where_paste_started.lnum = 0;
588#endif
589#ifdef FEAT_CINDENT
590 can_cindent = TRUE;
591#endif
592#ifdef FEAT_FOLDING
593 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
594 * restarting. */
595 if (!p_im && did_restart_edit == 0)
596 foldOpenCursor();
597#endif
598
599 /*
600 * If 'showmode' is set, show the current (insert/replace/..) mode.
601 * A warning message for changing a readonly file is given here, before
602 * actually changing anything. It's put after the mode, if any.
603 */
604 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000605 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 i = showmode();
607
608 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000609 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611#ifdef CURSOR_SHAPE
612 ui_cursor_shape(); /* may show different cursor shape */
613#endif
614#ifdef FEAT_DIGRAPHS
615 do_digraph(-1); /* clear digraphs */
616#endif
617
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000618 /*
619 * Get the current length of the redo buffer, those characters have to be
620 * skipped if we want to get to the inserted characters.
621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 ptr = get_inserted();
623 if (ptr == NULL)
624 new_insert_skip = 0;
625 else
626 {
627 new_insert_skip = (int)STRLEN(ptr);
628 vim_free(ptr);
629 }
630
631 old_indent = 0;
632
633 /*
634 * Main loop in Insert mode: repeat until Insert mode is left.
635 */
636 for (;;)
637 {
638#ifdef FEAT_RIGHTLEFT
639 if (!revins_legal)
640 revins_scol = -1; /* reset on illegal motions */
641 else
642 revins_legal = 0;
643#endif
644 if (arrow_used) /* don't repeat insert when arrow key used */
645 count = 0;
646
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100647 if (update_Insstart_orig)
648 Insstart_orig = Insstart;
649
Bram Moolenaar00672e12016-06-26 18:38:13 +0200650 if (stop_insert_mode
651#ifdef FEAT_INS_EXPAND
652 && !pum_visible()
653#endif
654 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {
656 /* ":stopinsert" used or 'insertmode' reset */
657 count = 0;
658 goto doESCkey;
659 }
660
661 /* set curwin->w_curswant for next K_DOWN or K_UP */
662 if (!arrow_used)
663 curwin->w_set_curswant = TRUE;
664
665 /* If there is no typeahead may check for timestamps (e.g., for when a
666 * menu invoked a shell command). */
667 if (stuff_empty())
668 {
669 did_check_timestamps = FALSE;
670 if (need_check_timestamps)
671 check_timestamps(FALSE);
672 }
673
674 /*
675 * When emsg() was called msg_scroll will have been set.
676 */
677 msg_scroll = FALSE;
678
679#ifdef FEAT_GUI
680 /* When 'mousefocus' is set a mouse movement may have taken us to
681 * another window. "need_mouse_correct" may then be set because of an
682 * autocommand. */
683 if (need_mouse_correct)
684 gui_mouse_correct();
685#endif
686
687#ifdef FEAT_FOLDING
688 /* Open fold at the cursor line, according to 'foldopen'. */
689 if (fdo_flags & FDO_INSERT)
690 foldOpenCursor();
691 /* Close folds where the cursor isn't, according to 'foldclose' */
692 if (!char_avail())
693 foldCheckClose();
694#endif
695
Bram Moolenaarf2732452018-06-03 14:47:35 +0200696#ifdef FEAT_JOB_CHANNEL
697 if (bt_prompt(curbuf))
698 {
699 init_prompt(cmdchar_todo);
700 cmdchar_todo = NUL;
701 }
702#endif
703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 /*
705 * If we inserted a character at the last position of the last line in
706 * the window, scroll the window one line up. This avoids an extra
707 * redraw.
708 * This is detected when the cursor column is smaller after inserting
709 * something.
710 * Don't do this when the topline changed already, it has
711 * already been adjusted (by insertchar() calling open_line())).
712 */
713 if (curbuf->b_mod_set
714 && curwin->w_p_wrap
715 && !did_backspace
716 && curwin->w_topline == old_topline
717#ifdef FEAT_DIFF
718 && curwin->w_topfill == old_topfill
719#endif
720 )
721 {
722 mincol = curwin->w_wcol;
723 validate_cursor_col();
724
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200725 if (
726#ifdef FEAT_VARTABS
727 (int)curwin->w_wcol < mincol - tabstop_at(
728 get_nolist_virtcol(), curbuf->b_p_ts,
729 curbuf->b_p_vts_array)
730#else
731 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 && curwin->w_wrow == W_WINROW(curwin)
734 + curwin->w_height - 1 - p_so
735 && (curwin->w_cursor.lnum != curwin->w_topline
736#ifdef FEAT_DIFF
737 || curwin->w_topfill > 0
738#endif
739 ))
740 {
741#ifdef FEAT_DIFF
742 if (curwin->w_topfill > 0)
743 --curwin->w_topfill;
744 else
745#endif
746#ifdef FEAT_FOLDING
747 if (hasFolding(curwin->w_topline, NULL, &old_topline))
748 set_topline(curwin, old_topline + 1);
749 else
750#endif
751 set_topline(curwin, curwin->w_topline + 1);
752 }
753 }
754
755 /* May need to adjust w_topline to show the cursor. */
756 update_topline();
757
758 did_backspace = FALSE;
759
760 validate_cursor(); /* may set must_redraw */
761
762 /*
763 * Redraw the display when no characters are waiting.
764 * Also shows mode, ruler and positions cursor.
765 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000766 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 if (curwin->w_p_scb)
769 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
Bram Moolenaar860cae12010-06-05 23:22:07 +0200771 if (curwin->w_p_crb)
772 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 update_curswant();
774 old_topline = curwin->w_topline;
775#ifdef FEAT_DIFF
776 old_topfill = curwin->w_topfill;
777#endif
778
779#ifdef USE_ON_FLY_SCROLL
780 dont_scroll = FALSE; /* allow scrolling here */
781#endif
782
783 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100784 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100786 if (c != K_CURSORHOLD)
787 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200788
789 /* After using CTRL-G U the next cursor key will not break undo. */
790 if (dont_sync_undo == MAYBE)
791 dont_sync_undo = TRUE;
792 else
793 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100794 if (cmdchar == K_PS)
795 /* Got here from normal mode when bracketed paste started. */
796 c = K_PS;
797 else
798 do
799 {
800 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200801
802 if (stop_insert_mode)
803 {
804 // Insert mode ended, possibly from a callback.
805 count = 0;
806 nomove = TRUE;
807 goto doESCkey;
808 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100809 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000811 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
812 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814#ifdef FEAT_RIGHTLEFT
815 if (p_hkmap && KeyTyped)
816 c = hkmap(c); /* Hebrew mode mapping */
817#endif
818#ifdef FEAT_FKMAP
819 if (p_fkmap && KeyTyped)
820 c = fkmap(c); /* Farsi mode mapping */
821#endif
822
823#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000824 /*
825 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000826 * and the cursor is still in the completed word. Only when there is
827 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000828 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000829 if (compl_started
830 && pum_wanted()
831 && curwin->w_cursor.col >= compl_col
832 && (compl_shown_match == NULL
833 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000834 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000835 /* BS: Delete one character from "compl_leader". */
836 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000837 && curwin->w_cursor.col > compl_col
838 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000839 continue;
840
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000841 /* When no match was selected or it was edited. */
842 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000843 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000845 * "compl_leader". Except when at the original match and
846 * there is nothing to add, CTRL-L works like CTRL-P then. */
847 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100848 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000849 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000850 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000851 {
852 ins_compl_addfrommatch();
853 continue;
854 }
855
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000856 /* A non-white character that fits in with the current
857 * completion: Add to "compl_leader". */
858 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000859 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100860#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100861 /* Trigger InsertCharPre. */
862 char_u *str = do_insert_char_pre(c);
863 char_u *p;
864
865 if (str != NULL)
866 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100867 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100868 ins_compl_addleader(PTR2CHAR(p));
869 vim_free(str);
870 }
871 else
872#endif
873 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000874 continue;
875 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000876
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000877 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000878 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200879 if ((c == Ctrl_Y || (compl_enter_selects
880 && (c == CAR || c == K_KENTER || c == NL)))
881 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000882 {
883 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200884 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000885 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000886 }
887 }
888
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
890 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000891 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000892 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000893 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#endif
895
Bram Moolenaar488c6512005-08-11 20:09:58 +0000896 /* CTRL-\ CTRL-N goes to Normal mode,
897 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
898 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (c == Ctrl_BSL)
900 {
901 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000902 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 ++no_mapping;
904 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000905 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 --no_mapping;
907 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000908 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000910 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 vungetc(c);
912 c = Ctrl_BSL;
913 }
914 else if (c == Ctrl_G && p_im)
915 continue;
916 else
917 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000918 if (c == Ctrl_O)
919 {
920 ins_ctrl_o();
921 ins_at_eol = FALSE; /* cursor keeps its column */
922 nomove = TRUE;
923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 count = 0;
925 goto doESCkey;
926 }
927 }
928
929#ifdef FEAT_DIGRAPHS
930 c = do_digraph(c);
931#endif
932
933#ifdef FEAT_INS_EXPAND
934 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
935 goto docomplete;
936#endif
937 if (c == Ctrl_V || c == Ctrl_Q)
938 {
939 ins_ctrl_v();
940 c = Ctrl_V; /* pretend CTRL-V is last typed character */
941 continue;
942 }
943
944#ifdef FEAT_CINDENT
945 if (cindent_on()
946# ifdef FEAT_INS_EXPAND
947 && ctrl_x_mode == 0
948# endif
949 )
950 {
951 /* A key name preceded by a bang means this key is not to be
952 * inserted. Skip ahead to the re-indenting below.
953 * A key name preceded by a star means that indenting has to be
954 * done before inserting the key. */
955 line_is_white = inindent(0);
956 if (in_cinkeys(c, '!', line_is_white))
957 goto force_cindent;
958 if (can_cindent && in_cinkeys(c, '*', line_is_white)
959 && stop_arrow() == OK)
960 do_c_expr_indent();
961 }
962#endif
963
964#ifdef FEAT_RIGHTLEFT
965 if (curwin->w_p_rl)
966 switch (c)
967 {
968 case K_LEFT: c = K_RIGHT; break;
969 case K_S_LEFT: c = K_S_RIGHT; break;
970 case K_C_LEFT: c = K_C_RIGHT; break;
971 case K_RIGHT: c = K_LEFT; break;
972 case K_S_RIGHT: c = K_S_LEFT; break;
973 case K_C_RIGHT: c = K_C_LEFT; break;
974 }
975#endif
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 /*
978 * If 'keymodel' contains "startsel", may start selection. If it
979 * does, a CTRL-O and c will be stuffed, we need to get these
980 * characters.
981 */
982 if (ins_start_select(c))
983 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
985 /*
986 * The big switch to handle a character in insert mode.
987 */
988 switch (c)
989 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000990 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (echeck_abbr(ESC + ABBR_OFF))
992 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200993 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000995 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996#ifdef FEAT_CMDWIN
997 if (c == Ctrl_C && cmdwin_type != 0)
998 {
999 /* Close the cmdline window. */
1000 cmdwin_result = K_IGNORE;
1001 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001002 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 goto doESCkey;
1004 }
1005#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001006#ifdef FEAT_JOB_CHANNEL
1007 if (c == Ctrl_C && bt_prompt(curbuf))
1008 {
1009 if (invoke_prompt_interrupt())
1010 {
1011 if (!bt_prompt(curbuf))
1012 // buffer changed to a non-prompt buffer, get out of
1013 // Insert mode
1014 goto doESCkey;
1015 break;
1016 }
1017 }
1018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
1020#ifdef UNIX
1021do_intr:
1022#endif
1023 /* when 'insertmode' set, and not halfway a mapping, don't leave
1024 * Insert mode */
1025 if (goto_im())
1026 {
1027 if (got_int)
1028 {
1029 (void)vgetc(); /* flush all buffers */
1030 got_int = FALSE;
1031 }
1032 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001033 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 break;
1035 }
1036doESCkey:
1037 /*
1038 * This is the ONLY return from edit()!
1039 */
1040 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1041 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001042 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 o_lnum = curwin->w_cursor.lnum;
1044
Bram Moolenaar488c6512005-08-11 20:09:58 +00001045 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001046 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001047 // When CTRL-C was typed got_int will be set, with the result
1048 // that the autocommands won't be executed. When mapped got_int
1049 // is not set, but let's keep the behavior the same.
1050 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001051 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001052 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 continue;
1056
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001057 case Ctrl_Z: /* suspend when 'insertmode' set */
1058 if (!p_im)
1059 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001060 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001061#ifdef CURSOR_SHAPE
1062 ui_cursor_shape(); /* may need to update cursor shape */
1063#endif
1064 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065
1066 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001067#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001068 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001069 goto docomplete;
1070#endif
1071 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1072 break;
1073 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001074
1075#ifdef FEAT_VIRTUALEDIT
1076 /* don't move the cursor left when 'virtualedit' has "onemore". */
1077 if (ve_flags & VE_ONEMORE)
1078 {
1079 ins_at_eol = FALSE;
1080 nomove = TRUE;
1081 }
1082#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 count = 0;
1084 goto doESCkey;
1085
Bram Moolenaar572cb562005-08-05 21:35:02 +00001086 case K_INS: /* toggle insert/replace mode */
1087 case K_KINS:
1088 ins_insert(replaceState);
1089 break;
1090
1091 case K_SELECT: /* end of Select mode mapping - ignore */
1092 break;
1093
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 case K_HELP: /* Help key works like <ESC> <Help> */
1095 case K_F1:
1096 case K_XF1:
1097 stuffcharReadbuff(K_HELP);
1098 if (p_im)
1099 need_start_insertmode = TRUE;
1100 goto doESCkey;
1101
1102#ifdef FEAT_NETBEANS_INTG
1103 case K_F21: /* NetBeans command */
1104 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001105 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001106 --no_mapping;
1107 netbeans_keycommand(i);
1108 break;
1109#endif
1110
1111 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 case NUL:
1113 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001114 /* For ^@ the trailing ESC will end the insert, unless there is an
1115 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1117 && c != Ctrl_A && !p_im)
1118 goto doESCkey; /* quit insert mode */
1119 inserted_space = FALSE;
1120 break;
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 ins_reg();
1124 auto_format(FALSE, TRUE);
1125 inserted_space = FALSE;
1126 break;
1127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001128 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 ins_ctrl_g();
1130 break;
1131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 case Ctrl_HAT: /* switch input mode and/or langmap */
1133 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 break;
1135
1136#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if (!p_ari)
1139 goto normalchar;
1140 ins_ctrl_();
1141 break;
1142#endif
1143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001144 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1146 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1147 goto docomplete;
1148#endif
1149 /* FALLTHROUGH */
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152# ifdef FEAT_INS_EXPAND
1153 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1154 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 if (has_compl_option(FALSE))
1156 goto docomplete;
1157 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 }
1159# endif
1160 ins_shift(c, lastc);
1161 auto_format(FALSE, TRUE);
1162 inserted_space = FALSE;
1163 break;
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 case K_KDEL:
1167 ins_del();
1168 auto_format(FALSE, TRUE);
1169 break;
1170
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001171 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 case Ctrl_H:
1173 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1174 auto_format(FALSE, TRUE);
1175 break;
1176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001177 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001178#ifdef FEAT_JOB_CHANNEL
1179 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1180 {
1181 // In a prompt window CTRL-W is used for window commands.
1182 // Use Shift-CTRL-W to delete a word.
1183 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001184 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001185 nomove = TRUE;
1186 count = 0;
1187 goto doESCkey;
1188 }
1189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1191 auto_format(FALSE, TRUE);
1192 break;
1193
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001194 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001195# ifdef FEAT_COMPL_FUNC
1196 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001197 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001198 goto docomplete;
1199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1201 auto_format(FALSE, TRUE);
1202 inserted_space = FALSE;
1203 break;
1204
1205#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001206 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 case K_LEFTMOUSE_NM:
1208 case K_LEFTDRAG:
1209 case K_LEFTRELEASE:
1210 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001211 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 case K_MIDDLEMOUSE:
1213 case K_MIDDLEDRAG:
1214 case K_MIDDLERELEASE:
1215 case K_RIGHTMOUSE:
1216 case K_RIGHTDRAG:
1217 case K_RIGHTRELEASE:
1218 case K_X1MOUSE:
1219 case K_X1DRAG:
1220 case K_X1RELEASE:
1221 case K_X2MOUSE:
1222 case K_X2DRAG:
1223 case K_X2RELEASE:
1224 ins_mouse(c);
1225 break;
1226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001227 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001228 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 break;
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001232 ins_mousescroll(MSCR_UP);
1233 break;
1234
1235 case K_MOUSELEFT: /* Scroll wheel left */
1236 ins_mousescroll(MSCR_LEFT);
1237 break;
1238
1239 case K_MOUSERIGHT: /* Scroll wheel right */
1240 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 break;
1242#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001243 case K_PS:
1244 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1245 if (cmdchar == K_PS)
1246 /* invoked from normal mode, bail out */
1247 goto doESCkey;
1248 break;
1249 case K_PE:
1250 /* Got K_PE without K_PS, ignore. */
1251 break;
1252
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001253#ifdef FEAT_GUI_TABLINE
1254 case K_TABLINE:
1255 case K_TABMENU:
1256 ins_tabline(c);
1257 break;
1258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001260 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 break;
1262
Bram Moolenaar754b5602006-02-09 23:53:20 +00001263 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001264 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001265 did_cursorhold = TRUE;
1266 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001267
Bram Moolenaar4770d092006-01-12 23:22:24 +00001268#ifdef FEAT_GUI_W32
1269 /* On Win32 ignore <M-F4>, we get it when closing the window was
1270 * cancelled. */
1271 case K_F4:
1272 if (mod_mask != MOD_MASK_ALT)
1273 goto normalchar;
1274 break;
1275#endif
1276
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277#ifdef FEAT_GUI
1278 case K_VER_SCROLLBAR:
1279 ins_scroll();
1280 break;
1281
1282 case K_HOR_SCROLLBAR:
1283 ins_horscroll();
1284 break;
1285#endif
1286
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001287 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 case K_S_HOME:
1290 case K_C_HOME:
1291 ins_home(c);
1292 break;
1293
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001294 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 case K_S_END:
1297 case K_C_END:
1298 ins_end(c);
1299 break;
1300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001301 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001302 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1303 ins_s_left();
1304 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001305 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 break;
1307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 case K_C_LEFT:
1310 ins_s_left();
1311 break;
1312
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001313 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001314 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1315 ins_s_right();
1316 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001317 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 break;
1319
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001320 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 case K_C_RIGHT:
1322 ins_s_right();
1323 break;
1324
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001325 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001326#ifdef FEAT_INS_EXPAND
1327 if (pum_visible())
1328 goto docomplete;
1329#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001330 if (mod_mask & MOD_MASK_SHIFT)
1331 ins_pageup();
1332 else
1333 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 break;
1335
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001336 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 case K_PAGEUP:
1338 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001339#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001340 if (pum_visible())
1341 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 ins_pageup();
1344 break;
1345
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001346 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001347#ifdef FEAT_INS_EXPAND
1348 if (pum_visible())
1349 goto docomplete;
1350#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001351 if (mod_mask & MOD_MASK_SHIFT)
1352 ins_pagedown();
1353 else
1354 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 break;
1356
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001357 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 case K_PAGEDOWN:
1359 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001360#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001361 if (pum_visible())
1362 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 ins_pagedown();
1365 break;
1366
1367#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001368 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 ins_drop();
1370 break;
1371#endif
1372
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001373 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 c = TAB;
1375 /* FALLTHROUGH */
1376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001377 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1379 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1380 goto docomplete;
1381#endif
1382 inserted_space = FALSE;
1383 if (ins_tab())
1384 goto normalchar; /* insert TAB as a normal char */
1385 auto_format(FALSE, TRUE);
1386 break;
1387
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001388 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 c = CAR;
1390 /* FALLTHROUGH */
1391 case CAR:
1392 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001393#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 /* In a quickfix window a <CR> jumps to the error under the
1395 * cursor. */
1396 if (bt_quickfix(curbuf) && c == CAR)
1397 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001398 if (curwin->w_llist_ref == NULL) /* quickfix window */
1399 do_cmdline_cmd((char_u *)".cc");
1400 else /* location list window */
1401 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 break;
1403 }
1404#endif
1405#ifdef FEAT_CMDWIN
1406 if (cmdwin_type != 0)
1407 {
1408 /* Execute the command in the cmdline window. */
1409 cmdwin_result = CAR;
1410 goto doESCkey;
1411 }
1412#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001413#ifdef FEAT_JOB_CHANNEL
1414 if (bt_prompt(curbuf))
1415 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001416 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001417 if (!bt_prompt(curbuf))
1418 // buffer changed to a non-prompt buffer, get out of
1419 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001420 goto doESCkey;
1421 break;
1422 }
1423#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001424 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 goto doESCkey; /* out of memory */
1426 auto_format(FALSE, FALSE);
1427 inserted_space = FALSE;
1428 break;
1429
Bram Moolenaar860cae12010-06-05 23:22:07 +02001430#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001431 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432# ifdef FEAT_INS_EXPAND
1433 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1434 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001435 if (has_compl_option(TRUE))
1436 goto docomplete;
1437 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439# endif
1440# ifdef FEAT_DIGRAPHS
1441 c = ins_digraph();
1442 if (c == NUL)
1443 break;
1444# endif
1445 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
1448#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001449 case Ctrl_X: /* Enter CTRL-X mode */
1450 ins_ctrl_x();
1451 break;
1452
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001453 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 if (ctrl_x_mode != CTRL_X_TAGS)
1455 goto normalchar;
1456 goto docomplete;
1457
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001458 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 if (ctrl_x_mode != CTRL_X_FILES)
1460 goto normalchar;
1461 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001462
1463 case 's': /* Spelling completion after ^X */
1464 case Ctrl_S:
1465 if (ctrl_x_mode != CTRL_X_SPELL)
1466 goto normalchar;
1467 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468#endif
1469
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001470 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471#ifdef FEAT_INS_EXPAND
1472 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1473#endif
1474 {
1475 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1476 if (p_im)
1477 {
1478 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1479 break;
1480 goto doESCkey;
1481 }
1482 goto normalchar;
1483 }
1484#ifdef FEAT_INS_EXPAND
1485 /* FALLTHROUGH */
1486
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001487 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 case Ctrl_N:
1489 /* if 'complete' is empty then plain ^P is no longer special,
1490 * but it is under other ^X modes */
1491 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001492 && (ctrl_x_mode == CTRL_X_NORMAL
1493 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001494 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 goto normalchar;
1496
1497docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001498 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001499#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001500 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001501#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001502 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001503 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001504#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001505 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001506#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001507 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 break;
1509#endif /* FEAT_INS_EXPAND */
1510
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001511 case Ctrl_Y: /* copy from previous line or scroll down */
1512 case Ctrl_E: /* copy from next line or scroll up */
1513 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 break;
1515
1516 default:
1517#ifdef UNIX
1518 if (c == intr_char) /* special interrupt char */
1519 goto do_intr;
1520#endif
1521
Bram Moolenaare659c952011-05-19 17:25:41 +02001522normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001524 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001526#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001527 if (!p_paste)
1528 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001529 /* Trigger InsertCharPre. */
1530 char_u *str = do_insert_char_pre(c);
1531 char_u *p;
1532
1533 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001534 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001535 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001536 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001537 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001538 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001539 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001540 c = PTR2CHAR(p);
1541 if (c == CAR || c == K_KENTER || c == NL)
1542 ins_eol(c);
1543 else
1544 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001545 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001546 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001547 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001548 vim_free(str);
1549 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001550 }
1551
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001552 /* If the new value is already inserted or an empty string
1553 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001554 if (c == NUL)
1555 break;
1556 }
1557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558#ifdef FEAT_SMARTINDENT
1559 /* Try to perform smart-indenting. */
1560 ins_try_si(c);
1561#endif
1562
1563 if (c == ' ')
1564 {
1565 inserted_space = TRUE;
1566#ifdef FEAT_CINDENT
1567 if (inindent(0))
1568 can_cindent = FALSE;
1569#endif
1570 if (Insstart_blank_vcol == MAXCOL
1571 && curwin->w_cursor.lnum == Insstart.lnum)
1572 Insstart_blank_vcol = get_nolist_virtcol();
1573 }
1574
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001575 /* Insert a normal character and check for abbreviations on a
1576 * special character. Let CTRL-] expand abbreviations without
1577 * inserting it. */
1578 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001579 // Add ABBR_OFF for characters above 0x100, this is
1580 // what check_abbr() expects.
1581 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1582 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
1584 insert_special(c, FALSE, FALSE);
1585#ifdef FEAT_RIGHTLEFT
1586 revins_legal++;
1587 revins_chars++;
1588#endif
1589 }
1590
1591 auto_format(FALSE, TRUE);
1592
1593#ifdef FEAT_FOLDING
1594 /* When inserting a character the cursor line must never be in a
1595 * closed fold. */
1596 foldOpenCursor();
1597#endif
1598 break;
1599 } /* end of switch (c) */
1600
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001601 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001602 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001603#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001604 /* but not in CTRL-X mode, a script can't restore the state */
1605 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001606#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001607 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001608 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 /* If the cursor was moved we didn't just insert a space */
1611 if (arrow_used)
1612 inserted_space = FALSE;
1613
1614#ifdef FEAT_CINDENT
1615 if (can_cindent && cindent_on()
1616# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001617 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618# endif
1619 )
1620 {
1621force_cindent:
1622 /*
1623 * Indent now if a key was typed that is in 'cinkeys'.
1624 */
1625 if (in_cinkeys(c, ' ', line_is_white))
1626 {
1627 if (stop_arrow() == OK)
1628 /* re-indent the current line */
1629 do_c_expr_indent();
1630 }
1631 }
1632#endif /* FEAT_CINDENT */
1633
1634 } /* for (;;) */
1635 /* NOTREACHED */
1636}
1637
1638/*
1639 * Redraw for Insert mode.
1640 * This is postponed until getting the next character to make '$' in the 'cpo'
1641 * option work correctly.
1642 * Only redraw when there are no characters available. This speeds up
1643 * inserting sequences of characters (e.g., for CTRL-R).
1644 */
1645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646ins_redraw(
1647 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001649#ifdef FEAT_CONCEAL
1650 linenr_T conceal_old_cursor_line = 0;
1651 linenr_T conceal_new_cursor_line = 0;
1652 int conceal_update_lines = FALSE;
1653#endif
1654
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001655 if (char_avail())
1656 return;
1657
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001658#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001659 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1660 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001661 if (ready && (has_cursormovedI()
1662# if defined(FEAT_CONCEAL)
1663 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001664# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001666 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001667# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001668 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001669# endif
1670 )
1671 {
1672# ifdef FEAT_SYN_HL
1673 /* Need to update the screen first, to make sure syntax
1674 * highlighting is correct after making a change (e.g., inserting
1675 * a "(". The autocommand may also require a redraw, so it's done
1676 * again below, unfortunately. */
1677 if (syntax_present(curwin) && must_redraw)
1678 update_screen(0);
1679# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001680 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001681 {
1682 /* Make sure curswant is correct, an autocommand may call
1683 * getcurpos(). */
1684 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001685 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001686 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001687# ifdef FEAT_CONCEAL
1688 if (curwin->w_p_cole > 0)
1689 {
1690 conceal_old_cursor_line = last_cursormoved.lnum;
1691 conceal_new_cursor_line = curwin->w_cursor.lnum;
1692 conceal_update_lines = TRUE;
1693 }
1694# endif
1695 last_cursormoved = curwin->w_cursor;
1696 }
1697#endif
1698
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001699 /* Trigger TextChangedI if b_changedtick differs. */
1700 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001701 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001702#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001703 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001704#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001705 )
1706 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001707 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001708 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001709
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001710 // save and restore curwin and curbuf, in case the autocmd changes them
1711 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001712 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001713 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001714 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001715 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1716 u_save(curwin->w_cursor.lnum,
1717 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001719
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001720#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001721 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1722 * TextChangedI will need to trigger for backwards compatibility, thus use
1723 * different b_last_changedtick* variables. */
1724 if (ready && has_textchangedP()
1725 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1726 && pum_visible())
1727 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001728 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001729 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001730
1731 // save and restore curwin and curbuf, in case the autocmd changes them
1732 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001733 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001734 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001735 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001736 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1737 u_save(curwin->w_cursor.lnum,
1738 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001739 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001740#endif
1741
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001742#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001743 if ((conceal_update_lines
1744 && (conceal_old_cursor_line != conceal_new_cursor_line
1745 || conceal_cursor_line(curwin)))
1746 || need_cursor_line_redraw)
1747 {
1748 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001749 redrawWinline(curwin, conceal_old_cursor_line);
1750 redrawWinline(curwin, conceal_new_cursor_line == 0
1751 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001752 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001753 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001754 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001755#endif
1756 if (must_redraw)
1757 update_screen(0);
1758 else if (clear_cmdline || redraw_cmdline)
1759 showmode(); /* clear cmdline and show mode */
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001760 showruler(FALSE);
1761 setcursor();
1762 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763}
1764
1765/*
1766 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1767 */
1768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001769ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770{
1771 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001772 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
1774 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001775 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001780 did_putchar = TRUE;
1781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1783
1784#ifdef FEAT_CMDL_INFO
1785 add_to_showcmd_c(Ctrl_V);
1786#endif
1787
1788 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001789 if (did_putchar)
1790 /* when the line fits in 'columns' the '^' is at the start of the next
1791 * line and will not removed by the redraw */
1792 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_CMDL_INFO
1794 clear_showcmd();
1795#endif
1796 insert_special(c, FALSE, TRUE);
1797#ifdef FEAT_RIGHTLEFT
1798 revins_chars++;
1799 revins_legal++;
1800#endif
1801}
1802
1803/*
1804 * Put a character directly onto the screen. It's not stored in a buffer.
1805 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1806 */
1807static int pc_status;
1808#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1809#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1810#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1811#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813static int pc_attr;
1814static int pc_row;
1815static int pc_col;
1816
1817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819{
1820 int attr;
1821
1822 if (ScreenLines != NULL)
1823 {
1824 update_topline(); /* just in case w_topline isn't valid */
1825 validate_cursor();
1826 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001827 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 else
1829 attr = 0;
1830 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001831 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833#ifdef FEAT_RIGHTLEFT
1834 if (curwin->w_p_rl)
1835 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001836 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (has_mbyte)
1838 {
1839 int fix_col = mb_fix_col(pc_col, pc_row);
1840
1841 if (fix_col != pc_col)
1842 {
1843 screen_putchar(' ', pc_row, fix_col, attr);
1844 --curwin->w_wcol;
1845 pc_status = PC_STATUS_RIGHT;
1846 }
1847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849 else
1850#endif
1851 {
1852 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (mb_lefthalve(pc_row, pc_col))
1854 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
1856
1857 /* save the character to be able to put it back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 {
1860 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1861 pc_status = PC_STATUS_SET;
1862 }
1863 screen_putchar(c, pc_row, pc_col, attr);
1864 }
1865}
1866
Bram Moolenaarf2732452018-06-03 14:47:35 +02001867#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1868/*
1869 * Return the effective prompt for the current buffer.
1870 */
1871 char_u *
1872prompt_text(void)
1873{
1874 if (curbuf->b_prompt_text == NULL)
1875 return (char_u *)"% ";
1876 return curbuf->b_prompt_text;
1877}
1878
1879/*
1880 * Prepare for prompt mode: Make sure the last line has the prompt text.
1881 * Move the cursor to this line.
1882 */
1883 static void
1884init_prompt(int cmdchar_todo)
1885{
1886 char_u *prompt = prompt_text();
1887 char_u *text;
1888
1889 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1890 text = ml_get_curline();
1891 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1892 {
1893 // prompt is missing, insert it or append a line with it
1894 if (*text == NUL)
1895 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1896 else
1897 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1898 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1899 coladvance((colnr_T)MAXCOL);
1900 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1901 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001902
1903 // Insert always starts after the prompt, allow editing text after it.
1904 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1905 || Insstart_orig.col != (int)STRLEN(prompt))
1906 {
1907 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001908 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001909 Insstart_orig = Insstart;
1910 Insstart_textlen = Insstart.col;
1911 Insstart_blank_vcol = MAXCOL;
1912 arrow_used = FALSE;
1913 }
1914
Bram Moolenaarf2732452018-06-03 14:47:35 +02001915 if (cmdchar_todo == 'A')
1916 coladvance((colnr_T)MAXCOL);
1917 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001918 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001919 /* Make sure the cursor is in a valid position. */
1920 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001921}
1922
1923/*
1924 * Return TRUE if the cursor is in the editable position of the prompt line.
1925 */
1926 int
1927prompt_curpos_editable()
1928{
1929 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1930 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1931}
1932#endif
1933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934/*
1935 * Undo the previous edit_putchar().
1936 */
1937 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001938edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939{
1940 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 if (pc_status == PC_STATUS_RIGHT)
1943 ++curwin->w_wcol;
1944 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001945 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1948 }
1949}
1950
1951/*
1952 * Called when p_dollar is set: display a '$' at the end of the changed text
1953 * Only works when cursor is in the line that changes.
1954 */
1955 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001956display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
1958 colnr_T save_col;
1959
1960 if (!redrawing())
1961 return;
1962
1963 cursor_off();
1964 save_col = curwin->w_cursor.col;
1965 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 if (has_mbyte)
1967 {
1968 char_u *p;
1969
1970 /* If on the last byte of a multi-byte move to the first byte. */
1971 p = ml_get_curline();
1972 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001975 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
1977 edit_putchar('$', FALSE);
1978 dollar_vcol = curwin->w_virtcol;
1979 }
1980 curwin->w_cursor.col = save_col;
1981}
1982
1983/*
1984 * Call this function before moving the cursor from the normal insert position
1985 * in insert mode.
1986 */
1987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001988undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001990 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001992 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001993 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 }
1995}
1996
1997/*
1998 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1999 * Keep the cursor on the same character.
2000 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2001 * type == INDENT_DEC decrease indent (for CTRL-D)
2002 * type == INDENT_SET set indent to "amount"
2003 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2004 */
2005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002006change_indent(
2007 int type,
2008 int amount,
2009 int round,
2010 int replaced, /* replaced character, put on replace stack */
2011 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
2013 int vcol;
2014 int last_vcol;
2015 int insstart_less; /* reduction for Insstart.col */
2016 int new_cursor_col;
2017 int i;
2018 char_u *ptr;
2019 int save_p_list;
2020 int start_col;
2021 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 colnr_T orig_col = 0; /* init for GCC */
2023 char_u *new_line, *orig_line = NULL; /* init for GCC */
2024
2025 /* VREPLACE mode needs to know what the line was like before changing */
2026 if (State & VREPLACE_FLAG)
2027 {
2028 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2029 orig_col = curwin->w_cursor.col;
2030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
2032 /* for the following tricks we don't want list mode */
2033 save_p_list = curwin->w_p_list;
2034 curwin->w_p_list = FALSE;
2035 vc = getvcol_nolist(&curwin->w_cursor);
2036 vcol = vc;
2037
2038 /*
2039 * For Replace mode we need to fix the replace stack later, which is only
2040 * possible when the cursor is in the indent. Remember the number of
2041 * characters before the cursor if it's possible.
2042 */
2043 start_col = curwin->w_cursor.col;
2044
2045 /* determine offset from first non-blank */
2046 new_cursor_col = curwin->w_cursor.col;
2047 beginline(BL_WHITE);
2048 new_cursor_col -= curwin->w_cursor.col;
2049
2050 insstart_less = curwin->w_cursor.col;
2051
2052 /*
2053 * If the cursor is in the indent, compute how many screen columns the
2054 * cursor is to the left of the first non-blank.
2055 */
2056 if (new_cursor_col < 0)
2057 vcol = get_indent() - vcol;
2058
2059 if (new_cursor_col > 0) /* can't fix replace stack */
2060 start_col = -1;
2061
2062 /*
2063 * Set the new indent. The cursor will be put on the first non-blank.
2064 */
2065 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002066 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 else
2068 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 int save_State = State;
2070
2071 /* Avoid being called recursively. */
2072 if (State & VREPLACE_FLAG)
2073 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002074 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 }
2077 insstart_less -= curwin->w_cursor.col;
2078
2079 /*
2080 * Try to put cursor on same character.
2081 * If the cursor is at or after the first non-blank in the line,
2082 * compute the cursor column relative to the column of the first
2083 * non-blank character.
2084 * If we are not in insert mode, leave the cursor on the first non-blank.
2085 * If the cursor is before the first non-blank, position it relative
2086 * to the first non-blank, counted in screen columns.
2087 */
2088 if (new_cursor_col >= 0)
2089 {
2090 /*
2091 * When changing the indent while the cursor is touching it, reset
2092 * Insstart_col to 0.
2093 */
2094 if (new_cursor_col == 0)
2095 insstart_less = MAXCOL;
2096 new_cursor_col += curwin->w_cursor.col;
2097 }
2098 else if (!(State & INSERT))
2099 new_cursor_col = curwin->w_cursor.col;
2100 else
2101 {
2102 /*
2103 * Compute the screen column where the cursor should be.
2104 */
2105 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002106 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
2108 /*
2109 * Advance the cursor until we reach the right screen column.
2110 */
2111 vcol = last_vcol = 0;
2112 new_cursor_col = -1;
2113 ptr = ml_get_curline();
2114 while (vcol <= (int)curwin->w_virtcol)
2115 {
2116 last_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002118 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002121 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
2123 vcol = last_vcol;
2124
2125 /*
2126 * May need to insert spaces to be able to position the cursor on
2127 * the right screen column.
2128 */
2129 if (vcol != (int)curwin->w_virtcol)
2130 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002131 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002133 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 if (ptr != NULL)
2135 {
2136 new_cursor_col += i;
2137 ptr[i] = NUL;
2138 while (--i >= 0)
2139 ptr[i] = ' ';
2140 ins_str(ptr);
2141 vim_free(ptr);
2142 }
2143 }
2144
2145 /*
2146 * When changing the indent while the cursor is in it, reset
2147 * Insstart_col to 0.
2148 */
2149 insstart_less = MAXCOL;
2150 }
2151
2152 curwin->w_p_list = save_p_list;
2153
2154 if (new_cursor_col <= 0)
2155 curwin->w_cursor.col = 0;
2156 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002157 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 curwin->w_set_curswant = TRUE;
2159 changed_cline_bef_curs();
2160
2161 /*
2162 * May have to adjust the start of the insert.
2163 */
2164 if (State & INSERT)
2165 {
2166 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2167 {
2168 if ((int)Insstart.col <= insstart_less)
2169 Insstart.col = 0;
2170 else
2171 Insstart.col -= insstart_less;
2172 }
2173 if ((int)ai_col <= insstart_less)
2174 ai_col = 0;
2175 else
2176 ai_col -= insstart_less;
2177 }
2178
2179 /*
2180 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2181 * If the number of characters before the cursor decreased, need to pop a
2182 * few characters from the replace stack.
2183 * If the number of characters before the cursor increased, need to push a
2184 * few NULs onto the replace stack.
2185 */
2186 if (REPLACE_NORMAL(State) && start_col >= 0)
2187 {
2188 while (start_col > (int)curwin->w_cursor.col)
2189 {
2190 replace_join(0); /* remove a NUL from the replace stack */
2191 --start_col;
2192 }
2193 while (start_col < (int)curwin->w_cursor.col || replaced)
2194 {
2195 replace_push(NUL);
2196 if (replaced)
2197 {
2198 replace_push(replaced);
2199 replaced = NUL;
2200 }
2201 ++start_col;
2202 }
2203 }
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 /*
2206 * For VREPLACE mode, we also have to fix the replace stack. In this case
2207 * it is always possible because we backspace over the whole line and then
2208 * put it back again the way we wanted it.
2209 */
2210 if (State & VREPLACE_FLAG)
2211 {
2212 /* If orig_line didn't allocate, just return. At least we did the job,
2213 * even if you can't backspace. */
2214 if (orig_line == NULL)
2215 return;
2216
2217 /* Save new line */
2218 new_line = vim_strsave(ml_get_curline());
2219 if (new_line == NULL)
2220 return;
2221
2222 /* We only put back the new line up to the cursor */
2223 new_line[curwin->w_cursor.col] = NUL;
2224
2225 /* Put back original line */
2226 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2227 curwin->w_cursor.col = orig_col;
2228
2229 /* Backspace from cursor to start of line */
2230 backspace_until_column(0);
2231
2232 /* Insert new stuff into line again */
2233 ins_bytes(new_line);
2234
2235 vim_free(new_line);
2236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237}
2238
2239/*
2240 * Truncate the space at the end of a line. This is to be used only in an
2241 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2242 * modes.
2243 */
2244 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002245truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246{
2247 int i;
2248
2249 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002250 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
2252 if (State & REPLACE_FLAG)
2253 replace_join(0); /* remove a NUL from the replace stack */
2254 }
2255 line[i + 1] = NUL;
2256}
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258/*
2259 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2260 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002261 * Will attempt not to go before "col" even when there is a composing
2262 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 */
2264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002265backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 while ((int)curwin->w_cursor.col > col)
2268 {
2269 curwin->w_cursor.col--;
2270 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002271 replace_do_bs(col);
2272 else if (!del_char_after_col(col))
2273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002277/*
2278 * Like del_char(), but make sure not to go before column "limit_col".
2279 * Only matters when there are composing characters.
2280 * Return TRUE when something was deleted.
2281 */
2282 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002283del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002284{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002285 if (enc_utf8 && limit_col >= 0)
2286 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002287 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002288
2289 /* Make sure the cursor is at the start of a character, but
2290 * skip forward again when going too far back because of a
2291 * composing character. */
2292 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002293 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002294 {
2295 int l = utf_ptr2len(ml_get_cursor());
2296
2297 if (l == 0) /* end of line */
2298 break;
2299 curwin->w_cursor.col += l;
2300 }
2301 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2302 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002303 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002304 }
2305 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002306 (void)del_char(FALSE);
2307 return TRUE;
2308}
2309
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2311/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002312 * CTRL-X pressed in Insert mode.
2313 */
2314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002315ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002316{
2317 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2318 * CTRL-V works like CTRL-N */
2319 if (ctrl_x_mode != CTRL_X_CMDLINE)
2320 {
2321 /* if the next ^X<> won't ADD nothing, then reset
2322 * compl_cont_status */
2323 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002324 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002325 else
2326 compl_cont_status = 0;
2327 /* We're not sure which CTRL-X mode it will be yet */
2328 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2329 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2330 edit_submode_pre = NULL;
2331 showmode();
2332 }
2333}
2334
2335/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002336 * Whether other than default completion has been selected.
2337 */
2338 int
2339ctrl_x_mode_not_default(void)
2340{
2341 return ctrl_x_mode != CTRL_X_NORMAL;
2342}
2343
2344/*
2345 * Whether CTRL-X was typed without a following character.
2346 */
2347 int
2348ctrl_x_mode_not_defined_yet(void)
2349{
2350 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2351}
2352
2353/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002354 * Return TRUE if the 'dict' or 'tsr' option can be used.
2355 */
2356 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002357has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002358{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002359 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002360# ifdef FEAT_SPELL
2361 && !curwin->w_p_spell
2362# endif
2363 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002364 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2365 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002366 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002367 edit_submode = NULL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002368 msg_attr(dict_opt ? _("'dictionary' option is empty")
2369 : _("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002370 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002371 if (emsg_silent == 0)
2372 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002373 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002374 setcursor();
2375 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002376#ifdef FEAT_EVAL
2377 if (!get_vim_var_nr(VV_TESTING))
2378#endif
2379 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002380 }
2381 return FALSE;
2382 }
2383 return TRUE;
2384}
2385
2386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2388 * This depends on the current mode.
2389 */
2390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002391vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392{
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01002393 // Always allow ^R - let its results then be checked
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (c == Ctrl_R)
2395 return TRUE;
2396
Bram Moolenaare3226be2005-12-18 22:10:00 +00002397 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002398 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002399 return TRUE;
2400
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 switch (ctrl_x_mode)
2402 {
2403 case 0: /* Not in any CTRL-X mode */
2404 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2405 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002406 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2408 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2409 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002410 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002411 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 case CTRL_X_SCROLL:
2413 return (c == Ctrl_Y || c == Ctrl_E);
2414 case CTRL_X_WHOLE_LINE:
2415 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2416 case CTRL_X_FILES:
2417 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2418 case CTRL_X_DICTIONARY:
2419 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2420 case CTRL_X_THESAURUS:
2421 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2422 case CTRL_X_TAGS:
2423 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2424#ifdef FEAT_FIND_ID
2425 case CTRL_X_PATH_PATTERNS:
2426 return (c == Ctrl_P || c == Ctrl_N);
2427 case CTRL_X_PATH_DEFINES:
2428 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2429#endif
2430 case CTRL_X_CMDLINE:
2431 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2432 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002433#ifdef FEAT_COMPL_FUNC
2434 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002435 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002436 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002437 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002438#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002439 case CTRL_X_SPELL:
2440 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002441 case CTRL_X_EVAL:
2442 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002444 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 return FALSE;
2446}
2447
2448/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002449 * Return TRUE when character "c" is part of the item currently being
2450 * completed. Used to decide whether to abandon complete mode when the menu
2451 * is visible.
2452 */
2453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002454ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002455{
2456 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2457 /* When expanding an identifier only accept identifier chars. */
2458 return vim_isIDc(c);
2459
2460 switch (ctrl_x_mode)
2461 {
2462 case CTRL_X_FILES:
2463 /* When expanding file name only accept file name chars. But not
2464 * path separators, so that "proto/<Tab>" expands files in
2465 * "proto", not "proto/" as a whole */
2466 return vim_isfilec(c) && !vim_ispathsep(c);
2467
2468 case CTRL_X_CMDLINE:
2469 case CTRL_X_OMNI:
2470 /* Command line and Omni completion can work with just about any
2471 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002472 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002473
2474 case CTRL_X_WHOLE_LINE:
2475 /* For while line completion a space can be part of the line. */
2476 return vim_isprintc(c);
2477 }
2478 return vim_iswordc(c);
2479}
2480
2481/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002482 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002484 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 * the rest of the word to be in -- webb
2486 */
2487 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002488ins_compl_add_infercase(
2489 char_u *str,
2490 int len,
2491 int icase,
2492 char_u *fname,
2493 int dir,
2494 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002496 char_u *p;
2497 int i, c;
2498 int actual_len; /* Take multi-byte characters */
2499 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002500 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002501 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 int has_lower = FALSE;
2503 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002505 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002507 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
Bram Moolenaara2993e12007-08-12 14:38:46 +00002509 /* Find actual length of completion. */
Bram Moolenaara2993e12007-08-12 14:38:46 +00002510 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002512 p = str;
2513 actual_len = 0;
2514 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002516 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002517 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002520 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002521 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
Bram Moolenaara2993e12007-08-12 14:38:46 +00002523 /* Find actual length of original text. */
Bram Moolenaara2993e12007-08-12 14:38:46 +00002524 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002526 p = compl_orig_text;
2527 actual_compl_length = 0;
2528 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002530 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002531 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
2533 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002534 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002535 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002537 /* "actual_len" may be smaller than "actual_compl_length" when using
2538 * thesaurus, only use the minimum when comparing. */
2539 min_len = actual_len < actual_compl_length
2540 ? actual_len : actual_compl_length;
2541
Bram Moolenaara2993e12007-08-12 14:38:46 +00002542 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002543 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002544 if (wca != NULL)
2545 {
2546 p = str;
2547 for (i = 0; i < actual_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002548 if (has_mbyte)
2549 wca[i] = mb_ptr2char_adv(&p);
2550 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002551 wca[i] = *(p++);
2552
2553 /* Rule 1: Were any chars converted to lower? */
2554 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002555 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002556 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002557 if (has_mbyte)
2558 c = mb_ptr2char_adv(&p);
2559 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002560 c = *(p++);
2561 if (MB_ISLOWER(c))
2562 {
2563 has_lower = TRUE;
2564 if (MB_ISUPPER(wca[i]))
2565 {
2566 /* Rule 1 is satisfied. */
2567 for (i = actual_compl_length; i < actual_len; ++i)
2568 wca[i] = MB_TOLOWER(wca[i]);
2569 break;
2570 }
2571 }
2572 }
2573
2574 /*
2575 * Rule 2: No lower case, 2nd consecutive letter converted to
2576 * upper case.
2577 */
2578 if (!has_lower)
2579 {
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 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002583 if (has_mbyte)
2584 c = mb_ptr2char_adv(&p);
2585 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002586 c = *(p++);
2587 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2588 {
2589 /* Rule 2 is satisfied. */
2590 for (i = actual_compl_length; i < actual_len; ++i)
2591 wca[i] = MB_TOUPPER(wca[i]);
2592 break;
2593 }
2594 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2595 }
2596 }
2597
2598 /* Copy the original case of the part we typed. */
2599 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002600 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002601 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002602 if (has_mbyte)
2603 c = mb_ptr2char_adv(&p);
2604 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002605 c = *(p++);
2606 if (MB_ISLOWER(c))
2607 wca[i] = MB_TOLOWER(wca[i]);
2608 else if (MB_ISUPPER(c))
2609 wca[i] = MB_TOUPPER(wca[i]);
2610 }
2611
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002612 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002613 * Generate encoding specific output from wide character array.
2614 * Multi-byte characters can occupy up to five bytes more than
2615 * ASCII characters, and we also need one byte for NUL, so stay
2616 * six bytes away from the edge of IObuff.
2617 */
2618 p = IObuff;
2619 i = 0;
2620 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002621 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002622 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002623 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002624 *(p++) = wca[i++];
2625 *p = NUL;
2626
2627 vim_free(wca);
2628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002630 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2631 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002633 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634}
2635
2636/*
2637 * Add a match to the list of matches.
2638 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002639 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002640 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002642 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002643ins_compl_add(
2644 char_u *str,
2645 int len,
2646 int icase,
2647 char_u *fname,
2648 char_u **cptext, /* extra text for popup menu or NULL */
2649 int cdir,
2650 int flags,
2651 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002653 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002654 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
2656 ui_breakcheck();
2657 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002658 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (len < 0)
2660 len = (int)STRLEN(str);
2661
2662 /*
2663 * If the same match is already present, don't add it.
2664 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002665 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002667 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 do
2669 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002670 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002671 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002672 && match->cp_str[len] == NUL)
2673 return NOTDONE;
2674 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002675 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
2677
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002678 /* Remove any popup menu before changing the list of matches. */
2679 ins_compl_del_pum();
2680
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 /*
2682 * Allocate a new match structure.
2683 * Copy the values to the new match structure.
2684 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002685 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002687 return FAIL;
2688 match->cp_number = -1;
2689 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002690 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002691 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
2693 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002694 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002696 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002697
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002699 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2701 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002702 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002703 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002704 && compl_curr_match->cp_fname != NULL
2705 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002706 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002707 else if (fname != NULL)
2708 {
2709 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002710 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002713 match->cp_fname = NULL;
2714 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002715
2716 if (cptext != NULL)
2717 {
2718 int i;
2719
2720 for (i = 0; i < CPT_COUNT; ++i)
2721 if (cptext[i] != NULL && *cptext[i] != NUL)
2722 match->cp_text[i] = vim_strsave(cptext[i]);
2723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 /*
2726 * Link the new match structure in the list of matches.
2727 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002728 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002729 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 else if (dir == FORWARD)
2731 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002732 match->cp_next = compl_curr_match->cp_next;
2733 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 }
2735 else /* BACKWARD */
2736 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002737 match->cp_next = compl_curr_match;
2738 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002740 if (match->cp_next)
2741 match->cp_next->cp_prev = match;
2742 if (match->cp_prev)
2743 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002745 compl_first_match = match;
2746 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002748 /*
2749 * Find the longest common string if still doing that.
2750 */
2751 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2752 ins_compl_longest_match(match);
2753
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 return OK;
2755}
2756
2757/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002758 * Return TRUE if "str[len]" matches with match->cp_str, considering
2759 * match->cp_icase.
2760 */
2761 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002762ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002763{
2764 if (match->cp_icase)
2765 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2766 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2767}
2768
2769/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002770 * Reduce the longest common string for match "match".
2771 */
2772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002773ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002774{
2775 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002776 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002777 int had_match;
2778
2779 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002780 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002781 /* First match, use it as a whole. */
2782 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002783 if (compl_leader != NULL)
2784 {
2785 had_match = (curwin->w_cursor.col > compl_col);
2786 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002787 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002788 ins_redraw(FALSE);
2789
2790 /* When the match isn't there (to avoid matching itself) remove it
2791 * again after redrawing. */
2792 if (!had_match)
2793 ins_compl_delete();
2794 compl_used_match = FALSE;
2795 }
2796 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002797 else
2798 {
2799 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002800 p = compl_leader;
2801 s = match->cp_str;
2802 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002803 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002804 if (has_mbyte)
2805 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002806 c1 = mb_ptr2char(p);
2807 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002808 }
2809 else
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002810 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002811 c1 = *p;
2812 c2 = *s;
2813 }
2814 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2815 : (c1 != c2))
2816 break;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002817 if (has_mbyte)
2818 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002819 MB_PTR_ADV(p);
2820 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002821 }
2822 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002823 {
2824 ++p;
2825 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002826 }
2827 }
2828
2829 if (*p != NUL)
2830 {
2831 /* Leader was shortened, need to change the inserted text. */
2832 *p = NUL;
2833 had_match = (curwin->w_cursor.col > compl_col);
2834 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002835 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002836 ins_redraw(FALSE);
2837
2838 /* When the match isn't there (to avoid matching itself) remove it
2839 * again after redrawing. */
2840 if (!had_match)
2841 ins_compl_delete();
2842 }
2843
2844 compl_used_match = FALSE;
2845 }
2846}
2847
2848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 * Add an array of matches to the list of matches.
2850 * Frees matches[].
2851 */
2852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002853ins_compl_add_matches(
2854 int num_matches,
2855 char_u **matches,
2856 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858 int i;
2859 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002860 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
Bram Moolenaar572cb562005-08-05 21:35:02 +00002862 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002863 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002864 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002866 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 FreeWild(num_matches, matches);
2868}
2869
2870/* Make the completion list cyclic.
2871 * Return the number of matches (excluding the original).
2872 */
2873 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002874ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002876 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 int count = 0;
2878
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002879 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 {
2881 /*
2882 * Find the end of the list.
2883 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002884 match = compl_first_match;
2885 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002886 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002888 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 ++count;
2890 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002891 match->cp_next = compl_first_match;
2892 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
2894 return count;
2895}
2896
Bram Moolenaara94bc432006-03-10 21:42:59 +00002897/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002898 * Set variables that store noselect and noinsert behavior from the
2899 * 'completeopt' value.
2900 */
2901 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002902completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002903{
2904 compl_no_insert = FALSE;
2905 compl_no_select = FALSE;
2906 if (strstr((char *)p_cot, "noselect") != NULL)
2907 compl_no_select = TRUE;
2908 if (strstr((char *)p_cot, "noinsert") != NULL)
2909 compl_no_insert = TRUE;
2910}
2911
2912/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002913 * Start completion for the complete() function.
2914 * "startcol" is where the matched text starts (1 is first column).
2915 * "list" is the list of matches.
2916 */
2917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002918set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002919{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002920 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002921 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002922
Bram Moolenaara94bc432006-03-10 21:42:59 +00002923 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002924 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002925 ins_compl_prep(' ');
2926 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002927 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002928
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002929 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002930 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002931 startcol = curwin->w_cursor.col;
2932 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002933 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002934 /* compl_pattern doesn't need to be set */
2935 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2936 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002937 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002938 return;
2939
Bram Moolenaare4214502015-03-05 18:08:43 +01002940 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002941
2942 ins_compl_add_list(list);
2943 compl_matches = ins_compl_make_cyclic();
2944 compl_started = TRUE;
2945 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002946 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002947
2948 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002949 if (compl_no_insert || compl_no_select)
2950 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002951 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002952 if (compl_no_select)
2953 /* Down/Up has no real effect. */
2954 ins_complete(K_UP, FALSE);
2955 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002956 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002957 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002958 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002959
2960 /* Lazily show the popup menu, unless we got interrupted. */
2961 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002962 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002963 out_flush();
2964}
2965
2966
Bram Moolenaar9372a112005-12-06 19:59:18 +00002967/* "compl_match_array" points the currently displayed list of entries in the
2968 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002969static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002970static int compl_match_arraysize;
2971
2972/*
2973 * Update the screen and when there is any scrolling remove the popup menu.
2974 */
2975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002976ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002977{
2978 int h;
2979
2980 if (compl_match_array != NULL)
2981 {
2982 h = curwin->w_cline_height;
Bram Moolenaarae654382019-01-17 21:09:05 +01002983 // Update the screen later, before drawing the popup menu over it.
2984 pum_call_update_screen();
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002985 if (h != curwin->w_cline_height)
2986 ins_compl_del_pum();
2987 }
2988}
2989
2990/*
2991 * Remove any popup menu.
2992 */
2993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002994ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002995{
2996 if (compl_match_array != NULL)
2997 {
2998 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01002999 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003000 }
3001}
3002
3003/*
3004 * Return TRUE if the popup menu should be displayed.
3005 */
3006 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003007pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003008{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003009 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003010 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003011 return FALSE;
3012
3013 /* The display looks bad on a B&W display. */
3014 if (t_colors < 8
3015#ifdef FEAT_GUI
3016 && !gui.in_use
3017#endif
3018 )
3019 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003020 return TRUE;
3021}
3022
3023/*
3024 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003025 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003026 */
3027 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003028pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003029{
3030 compl_T *compl;
3031 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003032
3033 /* Don't display the popup menu if there are no matches or there is only
3034 * one (ignoring the original text). */
3035 compl = compl_first_match;
3036 i = 0;
3037 do
3038 {
3039 if (compl == NULL
3040 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3041 break;
3042 compl = compl->cp_next;
3043 } while (compl != compl_first_match);
3044
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003045 if (strstr((char *)p_cot, "menuone") != NULL)
3046 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003047 return (i >= 2);
3048}
3049
3050/*
3051 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003052 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003053 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003054 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003055ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003056{
3057 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003058 compl_T *shown_compl = NULL;
3059 int did_find_shown_match = FALSE;
3060 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003061 int i;
3062 int cur = -1;
3063 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003064 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003065
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003066 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003067 return;
3068
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003069#if defined(FEAT_EVAL)
3070 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3071 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3072#endif
3073
Bram Moolenaarae654382019-01-17 21:09:05 +01003074 // Update the screen later, before drawing the popup menu over it.
3075 pum_call_update_screen();
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003076
3077 if (compl_match_array == NULL)
3078 {
3079 /* Need to build the popup menu list. */
3080 compl_match_arraysize = 0;
3081 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003082 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003083 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003084 do
3085 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003086 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3087 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003088 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003089 ++compl_match_arraysize;
3090 compl = compl->cp_next;
3091 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003092 if (compl_match_arraysize == 0)
3093 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003094 compl_match_array = (pumitem_T *)alloc_clear(
3095 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003096 * compl_match_arraysize));
3097 if (compl_match_array != NULL)
3098 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003099 /* If the current match is the original text don't find the first
3100 * match after it, don't highlight anything. */
3101 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3102 shown_match_ok = TRUE;
3103
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003104 i = 0;
3105 compl = compl_first_match;
3106 do
3107 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003108 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3109 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003110 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003111 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003112 if (!shown_match_ok)
3113 {
3114 if (compl == compl_shown_match || did_find_shown_match)
3115 {
3116 /* This item is the shown match or this is the
3117 * first displayed item after the shown match. */
3118 compl_shown_match = compl;
3119 did_find_shown_match = TRUE;
3120 shown_match_ok = TRUE;
3121 }
3122 else
3123 /* Remember this displayed match for when the
3124 * shown match is just below it. */
3125 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003126 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003127 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003128
3129 if (compl->cp_text[CPT_ABBR] != NULL)
3130 compl_match_array[i].pum_text =
3131 compl->cp_text[CPT_ABBR];
3132 else
3133 compl_match_array[i].pum_text = compl->cp_str;
3134 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3135 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3136 if (compl->cp_text[CPT_MENU] != NULL)
3137 compl_match_array[i++].pum_extra =
3138 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003139 else
3140 compl_match_array[i++].pum_extra = compl->cp_fname;
3141 }
3142
3143 if (compl == compl_shown_match)
3144 {
3145 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003146
3147 /* When the original text is the shown match don't set
3148 * compl_shown_match. */
3149 if (compl->cp_flags & ORIGINAL_TEXT)
3150 shown_match_ok = TRUE;
3151
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003152 if (!shown_match_ok && shown_compl != NULL)
3153 {
3154 /* The shown match isn't displayed, set it to the
3155 * previously displayed match. */
3156 compl_shown_match = shown_compl;
3157 shown_match_ok = TRUE;
3158 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003159 }
3160 compl = compl->cp_next;
3161 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003162
3163 if (!shown_match_ok) /* no displayed match at all */
3164 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003165 }
3166 }
3167 else
3168 {
3169 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003170 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003171 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3172 || compl_match_array[i].pum_text
3173 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003174 {
3175 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003176 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003177 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003178 }
3179
3180 if (compl_match_array != NULL)
3181 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003182 /* In Replace mode when a $ is displayed at the end of the line only
3183 * part of the screen would be updated. We do need to redraw here. */
3184 dollar_vcol = -1;
3185
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003186 /* Compute the screen column of the start of the completed text.
3187 * Use the cursor to get all wrapping and other settings right. */
3188 col = curwin->w_cursor.col;
3189 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003190 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003191 curwin->w_cursor.col = col;
3192 }
3193}
3194
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#define DICT_FIRST (1) /* use just first element in "dict" */
3196#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003197
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003199 * Add any identifiers that match the given pattern in the list of dictionary
3200 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 */
3202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003203ins_compl_dictionaries(
3204 char_u *dict_start,
3205 char_u *pat,
3206 int flags, /* DICT_FIRST and/or DICT_EXACT */
3207 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003209 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 char_u *ptr;
3211 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 char_u **files;
3214 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003216 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
Bram Moolenaar0b238792006-03-02 22:49:12 +00003218 if (*dict == NUL)
3219 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003220#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003221 /* When 'dictionary' is empty and spell checking is enabled use
3222 * "spell". */
3223 if (!thesaurus && curwin->w_p_spell)
3224 dict = (char_u *)"spell";
3225 else
3226#endif
3227 return;
3228 }
3229
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003231 if (buf == NULL)
3232 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003233 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 /* If 'infercase' is set, don't use 'smartcase' here */
3236 save_p_scs = p_scs;
3237 if (curbuf->b_p_inf)
3238 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003239
3240 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3241 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003242 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003243 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003244 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003245 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003246 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003247
3248 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003249 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003250 len = STRLEN(pat_esc) + 10;
3251 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003252 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003253 {
3254 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003255 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003256 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003257 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003258 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3259 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003260 vim_free(ptr);
3261 }
3262 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003263 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003264 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003265 if (regmatch.regprog == NULL)
3266 goto theend;
3267 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003268
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3270 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003271 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
3273 /* copy one dictionary file name into buf */
3274 if (flags == DICT_EXACT)
3275 {
3276 count = 1;
3277 files = &dict;
3278 }
3279 else
3280 {
3281 /* Expand wildcards in the dictionary name, but do not allow
3282 * backticks (for security, the 'dict' option may have been set in
3283 * a modeline). */
3284 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003285# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003286 if (!thesaurus && STRCMP(buf, "spell") == 0)
3287 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003288 else
3289# endif
3290 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 || expand_wildcards(1, &buf, &count, &files,
3292 EW_FILE|EW_SILENT) != OK)
3293 count = 0;
3294 }
3295
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003296# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003297 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003299 /* Complete from active spelling. Skip "\<" in the pattern, we
3300 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003301 if (pat[0] == '\\' && pat[1] == '<')
3302 ptr = pat + 2;
3303 else
3304 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003305 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003307 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003308# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003309 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003310 {
3311 ins_compl_files(count, files, thesaurus, flags,
3312 &regmatch, buf, &dir);
3313 if (flags != DICT_EXACT)
3314 FreeWild(count, files);
3315 }
3316 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 break;
3318 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003319
3320theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003322 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 vim_free(buf);
3324}
3325
Bram Moolenaar0b238792006-03-02 22:49:12 +00003326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003327ins_compl_files(
3328 int count,
3329 char_u **files,
3330 int thesaurus,
3331 int flags,
3332 regmatch_T *regmatch,
3333 char_u *buf,
3334 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003335{
3336 char_u *ptr;
3337 int i;
3338 FILE *fp;
3339 int add_r;
3340
3341 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3342 {
3343 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3344 if (flags != DICT_EXACT)
3345 {
3346 vim_snprintf((char *)IObuff, IOSIZE,
3347 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003348 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003349 }
3350
3351 if (fp != NULL)
3352 {
3353 /*
3354 * Read dictionary file line by line.
3355 * Check each line for a match.
3356 */
3357 while (!got_int && !compl_interrupted
3358 && !vim_fgets(buf, LSIZE, fp))
3359 {
3360 ptr = buf;
3361 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3362 {
3363 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003364 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003365 ptr = find_line_end(ptr);
3366 else
3367 ptr = find_word_end(ptr);
3368 add_r = ins_compl_add_infercase(regmatch->startp[0],
3369 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003370 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003371 if (thesaurus)
3372 {
3373 char_u *wstart;
3374
3375 /*
3376 * Add the other matches on the line
3377 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003378 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003379 while (!got_int)
3380 {
3381 /* Find start of the next word. Skip white
3382 * space and punctuation. */
3383 ptr = find_word_start(ptr);
3384 if (*ptr == NUL || *ptr == NL)
3385 break;
3386 wstart = ptr;
3387
Bram Moolenaara2993e12007-08-12 14:38:46 +00003388 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003389 if (has_mbyte)
3390 /* Japanese words may have characters in
3391 * different classes, only separate words
3392 * with single-byte non-word characters. */
3393 while (*ptr != NUL)
3394 {
3395 int l = (*mb_ptr2len)(ptr);
3396
3397 if (l < 2 && !vim_iswordc(*ptr))
3398 break;
3399 ptr += l;
3400 }
3401 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003402 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003403
3404 /* Add the word. Skip the regexp match. */
3405 if (wstart != regmatch->startp[0])
3406 add_r = ins_compl_add_infercase(wstart,
3407 (int)(ptr - wstart),
3408 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003409 }
3410 }
3411 if (add_r == OK)
3412 /* if dir was BACKWARD then honor it just once */
3413 *dir = FORWARD;
3414 else if (add_r == FAIL)
3415 break;
3416 /* avoid expensive call to vim_regexec() when at end
3417 * of line */
3418 if (*ptr == '\n' || got_int)
3419 break;
3420 }
3421 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003422 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003423 }
3424 fclose(fp);
3425 }
3426 }
3427}
3428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429/*
3430 * Find the start of the next word.
3431 * Returns a pointer to the first char of the word. Also stops at a NUL.
3432 */
3433 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003434find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 if (has_mbyte)
3437 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003438 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3441 ++ptr;
3442 return ptr;
3443}
3444
3445/*
3446 * Find the end of the word. Assumes it starts inside a word.
3447 * Returns a pointer to just after the word.
3448 */
3449 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003450find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 int start_class;
3453
3454 if (has_mbyte)
3455 {
3456 start_class = mb_get_class(ptr);
3457 if (start_class > 1)
3458 while (*ptr != NUL)
3459 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003460 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (mb_get_class(ptr) != start_class)
3462 break;
3463 }
3464 }
3465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 while (vim_iswordc(*ptr))
3467 ++ptr;
3468 return ptr;
3469}
3470
3471/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003472 * Find the end of the line, omitting CR and NL at the end.
3473 * Returns a pointer to just after the line.
3474 */
3475 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003476find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003477{
3478 char_u *s;
3479
3480 s = ptr + STRLEN(ptr);
3481 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3482 --s;
3483 return s;
3484}
3485
3486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 * Free the list of completions
3488 */
3489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003490ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003492 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003493 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
Bram Moolenaard23a8232018-02-10 18:45:26 +01003495 VIM_CLEAR(compl_pattern);
3496 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003498 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003500
3501 ins_compl_del_pum();
3502 pum_clear();
3503
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003504 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 do
3506 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003507 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003508 compl_curr_match = compl_curr_match->cp_next;
3509 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003511 if (match->cp_flags & FREE_FNAME)
3512 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003513 for (i = 0; i < CPT_COUNT; ++i)
3514 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003516 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3517 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003518 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003519 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520}
3521
3522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003523ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003525 compl_cont_status = 0;
3526 compl_started = FALSE;
3527 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003528 VIM_CLEAR(compl_pattern);
3529 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003531 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003532 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003533 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003534 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535}
3536
3537/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003538 * Return TRUE when Insert completion is active.
3539 */
3540 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003541ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003542{
3543 return compl_started;
3544}
3545
3546/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003547 * Delete one character before the cursor and show the subset of the matches
3548 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003549 * Returns the character to be used, NUL if the work is done and another char
3550 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003551 */
3552 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003553ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003554{
3555 char_u *line;
3556 char_u *p;
3557
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003558 line = ml_get_curline();
3559 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003560 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003561
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003562 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003563 * allow the word to be deleted, we won't match everything.
3564 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003565 if ((int)(p - line) - (int)compl_col < 0
3566 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003567 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3568 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3569 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003570 return K_BS;
3571
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003572 /* Deleted more than what was used to find matches or didn't finish
3573 * finding all matches: need to look for matches all over again. */
3574 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003575 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003576 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003577
Bram Moolenaara6557602006-02-04 22:43:20 +00003578 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003579 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003580 if (compl_leader != NULL)
3581 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003582 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003583 if (compl_shown_match != NULL)
3584 /* Make sure current match is not a hidden item. */
3585 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003586 return NUL;
3587 }
3588 return K_BS;
3589}
Bram Moolenaara6557602006-02-04 22:43:20 +00003590
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003591/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003592 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3593 * be called.
3594 */
3595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003596ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003597{
3598 /* Return TRUE if we didn't complete finding matches or when the
3599 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3600 return compl_was_interrupted
3601 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3602 && compl_opt_refresh_always);
3603}
3604
3605/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003606 * Called after changing "compl_leader".
3607 * Show the popup menu with a different set of matches.
3608 * May also search for matches again if the previous search was interrupted.
3609 */
3610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003611ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003612{
3613 ins_compl_del_pum();
3614 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003615 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003616 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003617
3618 if (compl_started)
3619 ins_compl_set_original_text(compl_leader);
3620 else
3621 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003622#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003623 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003624#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003625 /*
Bram Moolenaarae654382019-01-17 21:09:05 +01003626 * Matches were cleared, need to search for them now. Befor drawing
3627 * the popup menu display the changed text before the cursor. Set
3628 * "compl_restarting" to avoid that the first match is inserted.
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003629 */
Bram Moolenaarae654382019-01-17 21:09:05 +01003630 pum_call_update_screen();
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003631#ifdef FEAT_GUI
3632 if (gui.in_use)
3633 {
3634 /* Show the cursor after the match, not after the redrawn text. */
3635 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003636 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003637 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003638#endif
3639 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003640 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003641 compl_cont_status = 0;
3642 compl_restarting = FALSE;
3643 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003644
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003645 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003646
3647 /* Show the popup menu with a different set of matches. */
3648 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003649
3650 /* Don't let Enter select the original text when there is no popup menu. */
3651 if (compl_match_array == NULL)
3652 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003653}
3654
3655/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003656 * Return the length of the completion, from the completion start column to
3657 * the cursor column. Making sure it never goes below zero.
3658 */
3659 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003660ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003661{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003662 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003663
3664 if (off < 0)
3665 return 0;
3666 return off;
3667}
3668
3669/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003670 * Append one character to the match leader. May reduce the number of
3671 * matches.
3672 */
3673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003674ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003675{
Bram Moolenaara6557602006-02-04 22:43:20 +00003676 int cc;
3677
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003678 if (stop_arrow() == FAIL)
3679 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003680 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3681 {
3682 char_u buf[MB_MAXBYTES + 1];
3683
3684 (*mb_char2bytes)(c, buf);
3685 buf[cc] = NUL;
3686 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003687 if (compl_opt_refresh_always)
3688 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003689 }
3690 else
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003691 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003692 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003693 if (compl_opt_refresh_always)
3694 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003695 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003696
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003697 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003698 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003699 ins_compl_restart();
3700
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003701 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003702 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003703 * break redo. */
3704 if (!compl_opt_refresh_always)
3705 {
3706 vim_free(compl_leader);
3707 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003708 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003709 if (compl_leader != NULL)
3710 ins_compl_new_leader();
3711 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003712}
3713
3714/*
3715 * Setup for finding completions again without leaving CTRL-X mode. Used when
3716 * BS or a key was typed while still searching for matches.
3717 */
3718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003719ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003720{
3721 ins_compl_free();
3722 compl_started = FALSE;
3723 compl_matches = 0;
3724 compl_cont_status = 0;
3725 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003726}
3727
3728/*
3729 * Set the first match, the original text.
3730 */
3731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003732ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003733{
3734 char_u *p;
3735
Bram Moolenaare87edf32018-04-17 22:14:32 +02003736 /* Replace the original text entry.
3737 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3738 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003739 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3740 {
3741 p = vim_strsave(str);
3742 if (p != NULL)
3743 {
3744 vim_free(compl_first_match->cp_str);
3745 compl_first_match->cp_str = p;
3746 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003747 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003748 else if (compl_first_match->cp_prev != NULL
3749 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3750 {
3751 p = vim_strsave(str);
3752 if (p != NULL)
3753 {
3754 vim_free(compl_first_match->cp_prev->cp_str);
3755 compl_first_match->cp_prev->cp_str = p;
3756 }
3757 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003758}
3759
3760/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003761 * Append one character to the match leader. May reduce the number of
3762 * matches.
3763 */
3764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003765ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003766{
3767 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003768 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003769 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003770 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003771
3772 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003773 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003774 {
3775 /* When still at the original match use the first entry that matches
3776 * the leader. */
3777 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3778 {
3779 p = NULL;
3780 for (cp = compl_shown_match->cp_next; cp != NULL
3781 && cp != compl_first_match; cp = cp->cp_next)
3782 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003783 if (compl_leader == NULL
3784 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003785 (int)STRLEN(compl_leader)))
3786 {
3787 p = cp->cp_str;
3788 break;
3789 }
3790 }
3791 if (p == NULL || (int)STRLEN(p) <= len)
3792 return;
3793 }
3794 else
3795 return;
3796 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003797 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003798 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003799 ins_compl_addleader(c);
3800}
3801
3802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003804 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003805 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003807 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003808ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809{
3810 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003812 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
3814 /* Forget any previous 'special' messages if this is actually
3815 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3816 */
3817 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3818 edit_submode_extra = NULL;
3819
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003820 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003821 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3822 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003823 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003825 /* Set "compl_get_longest" when finding the first matches. */
3826 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003827 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003828 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003829 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003830 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003831
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003832 }
3833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3835 {
3836 /*
3837 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3838 * it will be yet. Now we decide.
3839 */
3840 switch (c)
3841 {
3842 case Ctrl_E:
3843 case Ctrl_Y:
3844 ctrl_x_mode = CTRL_X_SCROLL;
3845 if (!(State & REPLACE_FLAG))
3846 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3847 else
3848 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3849 edit_submode_pre = NULL;
3850 showmode();
3851 break;
3852 case Ctrl_L:
3853 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3854 break;
3855 case Ctrl_F:
3856 ctrl_x_mode = CTRL_X_FILES;
3857 break;
3858 case Ctrl_K:
3859 ctrl_x_mode = CTRL_X_DICTIONARY;
3860 break;
3861 case Ctrl_R:
3862 /* Simply allow ^R to happen without affecting ^X mode */
3863 break;
3864 case Ctrl_T:
3865 ctrl_x_mode = CTRL_X_THESAURUS;
3866 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003867#ifdef FEAT_COMPL_FUNC
3868 case Ctrl_U:
3869 ctrl_x_mode = CTRL_X_FUNCTION;
3870 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003872 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003873 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003874#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003875 case 's':
3876 case Ctrl_S:
3877 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003878#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003879 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003880 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003881 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003882#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003883 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 case Ctrl_RSB:
3885 ctrl_x_mode = CTRL_X_TAGS;
3886 break;
3887#ifdef FEAT_FIND_ID
3888 case Ctrl_I:
3889 case K_S_TAB:
3890 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3891 break;
3892 case Ctrl_D:
3893 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3894 break;
3895#endif
3896 case Ctrl_V:
3897 case Ctrl_Q:
3898 ctrl_x_mode = CTRL_X_CMDLINE;
3899 break;
3900 case Ctrl_P:
3901 case Ctrl_N:
3902 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3903 * just started ^X mode, or there were enough ^X's to cancel
3904 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3905 * do normal expansion when interrupting a different mode (say
3906 * ^X^F^X^P or ^P^X^X^P, see below)
3907 * nothing changes if interrupting mode 0, (eg, the flag
3908 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003909 if (!(compl_cont_status & CONT_INTRPT))
3910 compl_cont_status |= CONT_LOCAL;
3911 else if (compl_cont_mode != 0)
3912 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 /* FALLTHROUGH */
3914 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003915 /* If we have typed at least 2 ^X's... for modes != 0, we set
3916 * compl_cont_status = 0 (eg, as if we had just started ^X
3917 * mode).
3918 * For mode 0, we set "compl_cont_mode" to an impossible
3919 * value, in both cases ^X^X can be used to restart the same
3920 * mode (avoiding ADDING mode).
3921 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3922 * 'complete' and local ^P expansions respectively.
3923 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3924 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 if (c == Ctrl_X)
3926 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003927 if (compl_cont_mode != 0)
3928 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003930 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003932 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 edit_submode = NULL;
3934 showmode();
3935 break;
3936 }
3937 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003938 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
3940 /* We're already in CTRL-X mode, do we stay in it? */
3941 if (!vim_is_ctrl_x_key(c))
3942 {
3943 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003944 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 else
3946 ctrl_x_mode = CTRL_X_FINISHED;
3947 edit_submode = NULL;
3948 }
3949 showmode();
3950 }
3951
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003952 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
3954 /* Show error message from attempted keyword completion (probably
3955 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003956 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003958 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3959 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 || ctrl_x_mode == CTRL_X_FINISHED)
3961 {
3962 /* Get here when we have finished typing a sequence of ^N and
3963 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003964 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003965 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 {
3967 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003968 * If any of the original typed text has been changed, eg when
3969 * ignorecase is set, we must add back-spaces to the redo
3970 * buffer. We add as few as necessary to delete just the part
3971 * of the original text that has changed.
3972 * When using the longest match, edited the match or used
3973 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003975 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3976 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003977 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003978 ptr = NULL;
3979 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981
3982#ifdef FEAT_CINDENT
3983 want_cindent = (can_cindent && cindent_on());
3984#endif
3985 /*
3986 * When completing whole lines: fix indent for 'cindent'.
3987 * Otherwise, break line if it's too long.
3988 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003989 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 {
3991#ifdef FEAT_CINDENT
3992 /* re-indent the current line */
3993 if (want_cindent)
3994 {
3995 do_c_expr_indent();
3996 want_cindent = FALSE; /* don't do it again */
3997 }
3998#endif
3999 }
4000 else
4001 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004002 int prev_col = curwin->w_cursor.col;
4003
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004005 if (prev_col > 0)
4006 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004007 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004008 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004010 if (prev_col > 0
4011 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4012 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004015 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004016 * the selection without inserting anything. When
4017 * compl_enter_selects is set the Enter key does the same. */
4018 if ((c == Ctrl_Y || (compl_enter_selects
4019 && (c == CAR || c == K_KENTER || c == NL)))
4020 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004021 retval = TRUE;
4022
Bram Moolenaar47247282016-08-02 22:36:02 +02004023 /* CTRL-E means completion is Ended, go back to the typed text.
4024 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004025 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004026 {
4027 ins_compl_delete();
4028 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004029 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004030 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004031 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004032 retval = TRUE;
4033 }
4034
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004035 auto_format(FALSE, TRUE);
4036
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004038 compl_started = FALSE;
4039 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004040 if (!shortmess(SHM_COMPLETIONMENU))
4041 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004042 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004043 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if (edit_submode != NULL)
4045 {
4046 edit_submode = NULL;
4047 showmode();
4048 }
4049
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004050#ifdef FEAT_CMDWIN
4051 if (c == Ctrl_C && cmdwin_type != 0)
4052 /* Avoid the popup menu remains displayed when leaving the
4053 * command line window. */
4054 update_screen(0);
4055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056#ifdef FEAT_CINDENT
4057 /*
4058 * Indent now if a key was typed that is in 'cinkeys'.
4059 */
4060 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4061 do_c_expr_indent();
4062#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004063 /* Trigger the CompleteDone event to give scripts a chance to act
4064 * upon the completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004065 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004068 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4069 /* Trigger the CompleteDone event to give scripts a chance to act
4070 * upon the (possibly failed) completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004071 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 /* reset continue_* if we left expansion-mode, if we stay they'll be
4074 * (re)set properly in ins_complete() */
4075 if (!vim_is_ctrl_x_key(c))
4076 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004077 compl_cont_status = 0;
4078 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004080
4081 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082}
4083
4084/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004085 * Fix the redo buffer for the completion leader replacing some of the typed
4086 * text. This inserts backspaces and appends the changed text.
4087 * "ptr" is the known leader text or NUL.
4088 */
4089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004090ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004091{
4092 int len;
4093 char_u *p;
4094 char_u *ptr = ptr_arg;
4095
4096 if (ptr == NULL)
4097 {
4098 if (compl_leader != NULL)
4099 ptr = compl_leader;
4100 else
4101 return; /* nothing to do */
4102 }
4103 if (compl_orig_text != NULL)
4104 {
4105 p = compl_orig_text;
4106 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4107 ;
Bram Moolenaar62951b12011-09-21 18:23:05 +02004108 if (len > 0)
4109 len -= (*mb_head_off)(p, p + len);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004110 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004111 AppendCharToRedobuff(K_BS);
4112 }
4113 else
4114 len = 0;
4115 if (ptr != NULL)
4116 AppendToRedobuffLit(ptr + len, -1);
4117}
4118
4119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4121 * (depending on flag) starting from buf and looking for a non-scanned
4122 * buffer (other than curbuf). curbuf is special, if it is called with
4123 * buf=curbuf then it has to be the first call for a given flag/expansion.
4124 *
4125 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4126 */
4127 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004128ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
4132 if (flag == 'w') /* just windows */
4133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 if (buf == curbuf) /* first call for this flag/expansion */
4135 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004136 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 && wp->w_buffer->b_scanned)
4138 ;
4139 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 else
4142 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4143 * (unlisted buffers)
4144 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004145 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 && ((flag == 'U'
4147 ? buf->b_p_bl
4148 : (!buf->b_p_bl
4149 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004150 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 ;
4152 return buf;
4153}
4154
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004155#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004156/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004157 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004158 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004159 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004161expand_by_function(
4162 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4163 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004164{
Bram Moolenaar82139082011-09-14 16:52:09 +02004165 list_T *matchlist = NULL;
4166 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004167 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004168 char_u *funcname;
4169 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004170 win_T *curwin_save;
4171 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004172 typval_T rettv;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004173 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004174
Bram Moolenaare344bea2005-09-01 20:46:49 +00004175 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4176 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004177 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004178
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004179 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004180 args[0].v_type = VAR_NUMBER;
4181 args[0].vval.v_number = 0;
4182 args[1].v_type = VAR_STRING;
4183 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4184 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004185
Bram Moolenaare344bea2005-09-01 20:46:49 +00004186 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004187 curwin_save = curwin;
4188 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004189
4190 /* Call a function, which returns a list or dict. */
Bram Moolenaarded27a12018-08-01 19:06:03 +02004191 if (call_vim_function(funcname, 2, args, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004192 {
4193 switch (rettv.v_type)
4194 {
4195 case VAR_LIST:
4196 matchlist = rettv.vval.v_list;
4197 break;
4198 case VAR_DICT:
4199 matchdict = rettv.vval.v_dict;
4200 break;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004201 case VAR_SPECIAL:
4202 if (rettv.vval.v_number == VVAL_NONE)
4203 compl_opt_suppress_empty = TRUE;
4204 // FALLTHROUGH
Bram Moolenaar82139082011-09-14 16:52:09 +02004205 default:
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004206 // TODO: Give error message?
Bram Moolenaar82139082011-09-14 16:52:09 +02004207 clear_tv(&rettv);
4208 break;
4209 }
4210 }
4211
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004212 if (curwin_save != curwin || curbuf_save != curbuf)
4213 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004214 emsg(_(e_complwin));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004215 goto theend;
4216 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004217 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004218 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004219 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004221 emsg(_(e_compldel));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004222 goto theend;
4223 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004224
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004225 if (matchlist != NULL)
4226 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004227 else if (matchdict != NULL)
4228 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004229
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004230theend:
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004231 // Restore State, it might have been changed.
4232 State = save_State;
4233
Bram Moolenaar82139082011-09-14 16:52:09 +02004234 if (matchdict != NULL)
4235 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004236 if (matchlist != NULL)
4237 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004238}
4239#endif /* FEAT_COMPL_FUNC */
4240
Bram Moolenaar39f05632006-03-19 22:15:26 +00004241#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004242/*
4243 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004244 */
4245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004246ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004247{
4248 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004249 int dir = compl_direction;
4250
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004251 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004252 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004253 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004254 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4255 /* if dir was BACKWARD then honor it just once */
4256 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004257 else if (did_emsg)
4258 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004259 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004260}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004261
4262/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004263 * Add completions from a dict.
4264 */
4265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004266ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004267{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004268 dictitem_T *di_refresh;
4269 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004270
4271 /* Check for optional "refresh" item. */
4272 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004273 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4274 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004275 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004276 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004277
4278 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4279 compl_opt_refresh_always = TRUE;
4280 }
4281
4282 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004283 di_words = dict_find(dict, (char_u *)"words", 5);
4284 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4285 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004286}
4287
4288/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004289 * Add a match to the list of matches from a typeval_T.
4290 * If the given string is already in the list of completions, then return
4291 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4292 * maybe because alloc() returns NULL, then FAIL is returned.
4293 */
4294 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004295ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004296{
4297 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004298 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004299 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004300 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004301 char_u *(cptext[CPT_COUNT]);
4302
4303 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4304 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004305 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4306 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004307 (char_u *)"abbr", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004308 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004309 (char_u *)"menu", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004310 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004311 (char_u *)"kind", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004312 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004313 (char_u *)"info", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004314 cptext[CPT_USER_DATA] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004315 (char_u *)"user_data", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004316 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4317 icase = dict_get_number(tv->vval.v_dict, (char_u *)"icase");
4318 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
4319 adup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
4320 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4321 aempty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004322 }
4323 else
4324 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004325 word = tv_get_string_chk(tv);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004326 vim_memset(cptext, 0, sizeof(cptext));
4327 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004328 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004329 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004330 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004331}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004332#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004333
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004334/*
4335 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004336 * The search starts at position "ini" in curbuf and in the direction
4337 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004338 * When "compl_started" is FALSE start at that position, otherwise continue
4339 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004340 * This may return before finding all the matches.
4341 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 */
4343 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004344ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345{
4346 static pos_T first_match_pos;
4347 static pos_T last_match_pos;
4348 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004349 static int found_all = FALSE; /* Found all matches of a
4350 certain type. */
4351 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
Bram Moolenaar572cb562005-08-05 21:35:02 +00004353 pos_T *pos;
4354 char_u **matches;
4355 int save_p_scs;
4356 int save_p_ws;
4357 int save_p_ic;
4358 int i;
4359 int num_matches;
4360 int len;
4361 int found_new_match;
4362 int type = ctrl_x_mode;
4363 char_u *ptr;
4364 char_u *dict = NULL;
4365 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004366 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004368 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004370 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 ins_buf->b_scanned = 0;
4372 found_all = FALSE;
4373 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004374 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004375 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 last_match_pos = first_match_pos = *ini;
4377 }
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004378 else if (ins_buf != curbuf && !buf_valid(ins_buf))
4379 ins_buf = curbuf; // In case the buffer was wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
Bram Moolenaar4475b622017-05-01 20:46:52 +02004381 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004382 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004383
4384 /*
4385 * For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 for (;;)
4388 {
4389 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004390 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 * or if found_all says this entry is done. For ^X^L only use the
4394 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004395 if ((ctrl_x_mode == CTRL_X_NORMAL
4396 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004397 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 {
4399 found_all = FALSE;
4400 while (*e_cpt == ',' || *e_cpt == ' ')
4401 e_cpt++;
4402 if (*e_cpt == '.' && !curbuf->b_scanned)
4403 {
4404 ins_buf = curbuf;
4405 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004406 /* Move the cursor back one character so that ^N can match the
4407 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004408 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004409 {
4410 /* Move the cursor to after the last character in the
4411 * buffer, so that word at start of buffer is found
4412 * correctly. */
4413 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4414 first_match_pos.col =
4415 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 last_match_pos = first_match_pos;
4418 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004419
4420 /* Remember the first match so that the loop stops when we
4421 * wrap and come back there a second time. */
4422 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4425 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4426 {
4427 /* Scan a buffer, but not the current one. */
4428 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4429 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 first_match_pos.col = last_match_pos.col = 0;
4432 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4433 last_match_pos.lnum = 0;
4434 type = 0;
4435 }
4436 else /* unloaded buffer, scan like dictionary */
4437 {
4438 found_all = TRUE;
4439 if (ins_buf->b_fname == NULL)
4440 continue;
4441 type = CTRL_X_DICTIONARY;
4442 dict = ins_buf->b_fname;
4443 dict_f = DICT_EXACT;
4444 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004445 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 ins_buf->b_fname == NULL
4447 ? buf_spname(ins_buf)
4448 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004449 ? ins_buf->b_fname
4450 : ins_buf->b_sfname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004451 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453 else if (*e_cpt == NUL)
4454 break;
4455 else
4456 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004457 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 type = -1;
4459 else if (*e_cpt == 'k' || *e_cpt == 's')
4460 {
4461 if (*e_cpt == 'k')
4462 type = CTRL_X_DICTIONARY;
4463 else
4464 type = CTRL_X_THESAURUS;
4465 if (*++e_cpt != ',' && *e_cpt != NUL)
4466 {
4467 dict = e_cpt;
4468 dict_f = DICT_FIRST;
4469 }
4470 }
4471#ifdef FEAT_FIND_ID
4472 else if (*e_cpt == 'i')
4473 type = CTRL_X_PATH_PATTERNS;
4474 else if (*e_cpt == 'd')
4475 type = CTRL_X_PATH_DEFINES;
4476#endif
4477 else if (*e_cpt == ']' || *e_cpt == 't')
4478 {
4479 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004480 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar32526b32019-01-19 17:43:09 +01004481 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483 else
4484 type = -1;
4485
4486 /* in any case e_cpt is advanced to the next entry */
4487 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4488
4489 found_all = TRUE;
4490 if (type == -1)
4491 continue;
4492 }
4493 }
4494
Bram Moolenaar4475b622017-05-01 20:46:52 +02004495 /* If complete() was called then compl_pattern has been reset. The
4496 * following won't work then, bail out. */
4497 if (compl_pattern == NULL)
4498 break;
4499
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 switch (type)
4501 {
4502 case -1:
4503 break;
4504#ifdef FEAT_FIND_ID
4505 case CTRL_X_PATH_PATTERNS:
4506 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004507 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004508 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004510 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4512 (linenr_T)1, (linenr_T)MAXLNUM);
4513 break;
4514#endif
4515
4516 case CTRL_X_DICTIONARY:
4517 case CTRL_X_THESAURUS:
4518 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004519 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 : (type == CTRL_X_THESAURUS
4521 ? (*curbuf->b_p_tsr == NUL
4522 ? p_tsr
4523 : curbuf->b_p_tsr)
4524 : (*curbuf->b_p_dict == NUL
4525 ? p_dict
4526 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004527 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004528 dict != NULL ? dict_f
4529 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 dict = NULL;
4531 break;
4532
4533 case CTRL_X_TAGS:
4534 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4535 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004538 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539 * of matches is found when compl_pattern is empty */
4540 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004541 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4542 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4544 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004545 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
4547 p_ic = save_p_ic;
4548 break;
4549
4550 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4553 {
4554
4555 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004556 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004557 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
4559 break;
4560
4561 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 if (expand_cmdline(&compl_xp, compl_pattern,
4563 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004565 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 break;
4567
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004568#ifdef FEAT_COMPL_FUNC
4569 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004570 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004571 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004572 break;
4573#endif
4574
Bram Moolenaar488c6512005-08-11 20:09:58 +00004575 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004576#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004577 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004578 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004579 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004580 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004581#endif
4582 break;
4583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 default: /* normal ^P/^N and ^X^L */
4585 /*
4586 * If 'infercase' is set, don't use 'smartcase' here
4587 */
4588 save_p_scs = p_scs;
4589 if (ins_buf->b_p_inf)
4590 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004591
Bram Moolenaar361aa502014-01-23 22:45:58 +01004592 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 * end but never from the middle, thus setting nowrapscan in this
4594 * buffers is a good idea, on the other hand, we always set
4595 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4596 save_p_ws = p_ws;
4597 if (ins_buf != curbuf)
4598 p_ws = FALSE;
4599 else if (*e_cpt == '.')
4600 p_ws = TRUE;
4601 for (;;)
4602 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004603 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004605 ++msg_silent; /* Don't want messages for wrapscan. */
4606
Bram Moolenaare4214502015-03-05 18:08:43 +01004607 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4608 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004609 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004610 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004611 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004613 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 else
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004615 found_new_match = searchit(NULL, ins_buf, pos, NULL,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004616 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004617 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004618 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004619 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004620 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004622 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004623 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 first_match_pos = *pos;
4625 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004626 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
4628 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004629 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 found_new_match = FAIL;
4631 if (found_new_match == FAIL)
4632 {
4633 if (ins_buf == curbuf)
4634 found_all = TRUE;
4635 break;
4636 }
4637
4638 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004639 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 && ini->lnum == pos->lnum
4641 && ini->col == pos->col)
4642 continue;
4643 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004644 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004646 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
4648 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4649 continue;
4650 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4651 if (!p_paste)
4652 ptr = skipwhite(ptr);
4653 }
4654 len = (int)STRLEN(ptr);
4655 }
4656 else
4657 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004658 char_u *tmp_ptr = ptr;
4659
4660 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004662 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 /* Skip if already inside a word. */
4664 if (vim_iswordp(tmp_ptr))
4665 continue;
4666 /* Find start of next word. */
4667 tmp_ptr = find_word_start(tmp_ptr);
4668 }
4669 /* Find end of this word. */
4670 tmp_ptr = find_word_end(tmp_ptr);
4671 len = (int)(tmp_ptr - ptr);
4672
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004673 if ((compl_cont_status & CONT_ADDING)
4674 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
4676 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4677 {
4678 /* Try next line, if any. the new word will be
4679 * "join" as if the normal command "J" was used.
4680 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004681 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 * works -- Acevedo */
4683 STRNCPY(IObuff, ptr, len);
4684 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4685 tmp_ptr = ptr = skipwhite(ptr);
4686 /* Find start of next word. */
4687 tmp_ptr = find_word_start(tmp_ptr);
4688 /* Find end of next word. */
4689 tmp_ptr = find_word_end(tmp_ptr);
4690 if (tmp_ptr > ptr)
4691 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004692 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004694 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 IObuff[len++] = ' ';
4696 /* IObuf =~ "\k.* ", thus len >= 2 */
4697 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004698 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 || (vim_strchr(p_cpo, CPO_JOINSP)
4700 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004701 && (IObuff[len - 2] == '?'
4702 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 IObuff[len++] = ' ';
4704 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004705 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 if (tmp_ptr - ptr >= IOSIZE - len)
4707 tmp_ptr = ptr + IOSIZE - len - 1;
4708 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4709 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004710 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
4712 IObuff[len] = NUL;
4713 ptr = IObuff;
4714 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004715 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 continue;
4717 }
4718 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004719 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004720 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004721 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
4723 found_new_match = OK;
4724 break;
4725 }
4726 }
4727 p_scs = save_p_scs;
4728 p_ws = save_p_ws;
4729 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004730
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004731 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004732 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004733 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 found_new_match = OK;
4735
4736 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004737 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4738 * match */
4739 if ((ctrl_x_mode != CTRL_X_NORMAL
4740 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004741 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004742 {
4743 if (got_int)
4744 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004745 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004746 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004747 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004748
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004749 if ((ctrl_x_mode != CTRL_X_NORMAL
4750 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004751 || compl_interrupted)
4752 break;
4753 compl_started = TRUE;
4754 }
4755 else
4756 {
4757 /* Mark a buffer scanned when it has been scanned completely */
4758 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4759 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004761 compl_started = FALSE;
4762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004764 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004766 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 && *e_cpt == NUL) /* Got to end of 'complete' */
4768 found_new_match = FAIL;
4769
4770 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004771 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4772 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 i = ins_compl_make_cyclic();
4774
Bram Moolenaar4475b622017-05-01 20:46:52 +02004775 if (compl_old_match != NULL)
4776 {
4777 /* If several matches were added (FORWARD) or the search failed and has
4778 * just been made cyclic then we have to move compl_curr_match to the
4779 * next or previous entry (if any) -- Acevedo */
4780 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4781 : compl_old_match->cp_prev;
4782 if (compl_curr_match == NULL)
4783 compl_curr_match = compl_old_match;
4784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 return i;
4786}
4787
4788/* Delete the old text being completed. */
4789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004790ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004792 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
4794 /*
4795 * In insert mode: Delete the typed part.
4796 * In replace mode: Put the old characters back, if any.
4797 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004798 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4799 if ((int)curwin->w_cursor.col > col)
4800 {
4801 if (stop_arrow() == FAIL)
4802 return;
4803 backspace_until_column(col);
4804 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004805
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004806 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4807 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004809 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004810 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811}
4812
Bram Moolenaar472e8592016-10-15 17:06:47 +02004813/*
4814 * Insert the new text being completed.
4815 * "in_compl_func" is TRUE when called from complete_check().
4816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004818ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004820 dict_T *dict;
4821
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004822 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004823 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4824 compl_used_match = FALSE;
4825 else
4826 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004827
4828 /* Set completed item. */
4829 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004830 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004831 if (dict != NULL)
4832 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004833 dict_add_string(dict, "word", compl_shown_match->cp_str);
4834 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4835 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4836 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4837 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4838 dict_add_string(dict, "user_data",
4839 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004840 }
4841 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004842 if (!in_compl_func)
4843 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844}
4845
4846/*
4847 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004848 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4849 * get more completions. If it is FALSE, then we just do nothing when there
4850 * are no more completions in a given direction. The latter case is used when
4851 * we are still in the middle of finding completions, to allow browsing
4852 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 * Return the total number of matches, or -1 if still unknown -- webb.
4854 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004855 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4856 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 *
4858 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004859 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4860 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 */
4862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004863ins_compl_next(
4864 int allow_get_expansion,
4865 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004866 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004867 int insert_match, /* Insert the newly selected match */
4868 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869{
4870 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004871 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004872 compl_T *found_compl = NULL;
4873 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004874 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004875 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004877 /* When user complete function return -1 for findstart which is next
4878 * time of 'always', compl_shown_match become NULL. */
4879 if (compl_shown_match == NULL)
4880 return -1;
4881
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004882 if (compl_leader != NULL
4883 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004885 /* Set "compl_shown_match" to the actually shown match, it may differ
4886 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004887 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004888 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004889 && compl_shown_match->cp_next != NULL
4890 && compl_shown_match->cp_next != compl_first_match)
4891 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004892
4893 /* If we didn't find it searching forward, and compl_shows_dir is
4894 * backward, find the last match. */
4895 if (compl_shows_dir == BACKWARD
4896 && !ins_compl_equal(compl_shown_match,
4897 compl_leader, (int)STRLEN(compl_leader))
4898 && (compl_shown_match->cp_next == NULL
4899 || compl_shown_match->cp_next == compl_first_match))
4900 {
4901 while (!ins_compl_equal(compl_shown_match,
4902 compl_leader, (int)STRLEN(compl_leader))
4903 && compl_shown_match->cp_prev != NULL
4904 && compl_shown_match->cp_prev != compl_first_match)
4905 compl_shown_match = compl_shown_match->cp_prev;
4906 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004907 }
4908
4909 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004910 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 /* Delete old text to be replaced */
4912 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004913
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004914 /* When finding the longest common text we stick at the original text,
4915 * don't let CTRL-N or CTRL-P move to the first match. */
4916 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4917
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004918 /* When restarting the search don't insert the first match either. */
4919 if (compl_restarting)
4920 {
4921 advance = FALSE;
4922 compl_restarting = FALSE;
4923 }
4924
Bram Moolenaare3226be2005-12-18 22:10:00 +00004925 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4926 * around. */
4927 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004929 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004931 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004932 found_end = (compl_first_match != NULL
4933 && (compl_shown_match->cp_next == compl_first_match
4934 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004935 }
4936 else if (compl_shows_dir == BACKWARD
4937 && compl_shown_match->cp_prev != NULL)
4938 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004939 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004940 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004941 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004944 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004945 if (!allow_get_expansion)
4946 {
4947 if (advance)
4948 {
4949 if (compl_shows_dir == BACKWARD)
4950 compl_pending -= todo + 1;
4951 else
4952 compl_pending += todo + 1;
4953 }
4954 return -1;
4955 }
4956
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004957 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004958 {
4959 if (compl_shows_dir == BACKWARD)
4960 --compl_pending;
4961 else
4962 ++compl_pending;
4963 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004964
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004965 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004966 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004967
4968 /* handle any pending completions */
4969 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004970 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004971 {
4972 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4973 {
4974 compl_shown_match = compl_shown_match->cp_next;
4975 --compl_pending;
4976 }
4977 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4978 {
4979 compl_shown_match = compl_shown_match->cp_prev;
4980 ++compl_pending;
4981 }
4982 else
4983 break;
4984 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004985 found_end = FALSE;
4986 }
4987 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4988 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004989 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004990 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004991 ++todo;
4992 else
4993 /* Remember a matching item. */
4994 found_compl = compl_shown_match;
4995
4996 /* Stop at the end of the list when we found a usable match. */
4997 if (found_end)
4998 {
4999 if (found_compl != NULL)
5000 {
5001 compl_shown_match = found_compl;
5002 break;
5003 }
5004 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
5007
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005008 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005009 if (compl_no_insert && !started)
5010 {
5011 ins_bytes(compl_orig_text + ins_compl_len());
5012 compl_used_match = FALSE;
5013 }
5014 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005015 {
5016 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005017 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005018 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005019 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005020 }
5021 else
5022 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023
5024 if (!allow_get_expansion)
5025 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005026 /* may undisplay the popup menu first */
5027 ins_compl_upd_pum();
5028
Bram Moolenaarae654382019-01-17 21:09:05 +01005029 // Redraw before showing the popup menu to show the user what was
5030 // inserted.
5031 pum_call_update_screen();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005032
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005033 /* display the updated popup menu */
5034 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005035#ifdef FEAT_GUI
5036 if (gui.in_use)
5037 {
5038 /* Show the cursor after the match, not after the redrawn text. */
5039 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005040 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005041 }
5042#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 /* Delete old text to be replaced, since we're still searching and
5045 * don't want to match ourselves! */
5046 ins_compl_delete();
5047 }
5048
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005049 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005050 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005051 if (compl_no_insert && !started)
5052 compl_enter_selects = TRUE;
5053 else
5054 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005055
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 /*
5057 * Show the file name for the match (if any)
5058 * Truncate the file name to avoid a wait for return.
5059 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005060 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005062 char *lead = _("match in file");
5063 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5064 char_u *s;
5065 char_u *e;
5066
5067 if (space > 0)
5068 {
5069 /* We need the tail that fits. With double-byte encoding going
5070 * back from the end is very slow, thus go from the start and keep
5071 * the text that fits in "space" between "s" and "e". */
5072 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5073 {
5074 space -= ptr2cells(e);
5075 while (space < 0)
5076 {
5077 space += ptr2cells(s);
5078 MB_PTR_ADV(s);
5079 }
5080 }
5081 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5082 s > compl_shown_match->cp_fname ? "<" : "", s);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005083 msg((char *)IObuff);
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005084 redraw_cmdline = FALSE; /* don't overwrite! */
5085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087
5088 return num_matches;
5089}
5090
5091/*
5092 * Call this while finding completions, to check whether the user has hit a key
5093 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005094 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005096 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005097 * "in_compl_func" is TRUE when called from complete_check(), don't set
5098 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 */
5100 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005101ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102{
5103 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005104 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005106 /* Don't check when reading keys from a script, :normal or feedkeys().
5107 * That would break the test scripts. But do check for keys when called
5108 * from complete_check(). */
5109 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 return;
5111
5112 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005113 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 return;
5115 count = 0;
5116
Bram Moolenaara260a972006-06-23 19:36:29 +00005117 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5118 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 if (c != NUL)
5121 {
5122 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5123 {
5124 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005125 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005126 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005127 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005129 else
5130 {
5131 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005132 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005133 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005134 if (c != K_IGNORE)
5135 {
5136 /* Don't interrupt completion when the character wasn't typed,
5137 * e.g., when doing @q to replay keys. */
5138 if (c != Ctrl_R && KeyTyped)
5139 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005140
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005141 vungetc(c);
5142 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005145 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005146 {
5147 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5148
5149 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005150 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005151 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005152}
5153
5154/*
5155 * Decide the direction of Insert mode complete from the key typed.
5156 * Returns BACKWARD or FORWARD.
5157 */
5158 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005159ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005160{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005161 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005162 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005163 return BACKWARD;
5164 return FORWARD;
5165}
5166
5167/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005168 * Return TRUE for keys that are used for completion only when the popup menu
5169 * is visible.
5170 */
5171 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005172ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005173{
5174 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005175 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5176 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005177}
5178
5179/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005180 * Decide the number of completions to move forward.
5181 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5182 */
5183 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005184ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005185{
5186 int h;
5187
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005188 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005189 {
5190 h = pum_get_height();
5191 if (h > 3)
5192 h -= 2; /* keep some context */
5193 return h;
5194 }
5195 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196}
5197
5198/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005199 * Return TRUE if completion with "c" should insert the match, FALSE if only
5200 * to change the currently selected completion.
5201 */
5202 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005203ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005204{
5205 switch (c)
5206 {
5207 case K_UP:
5208 case K_DOWN:
5209 case K_PAGEDOWN:
5210 case K_KPAGEDOWN:
5211 case K_S_DOWN:
5212 case K_PAGEUP:
5213 case K_KPAGEUP:
5214 case K_S_UP:
5215 return FALSE;
5216 }
5217 return TRUE;
5218}
5219
5220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 * Do Insert mode completion.
5222 * Called when character "c" was typed, which has a meaning for completion.
5223 * Returns OK if completion was done, FAIL if something failed (out of mem).
5224 */
5225 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005226ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 char_u *line;
5229 int startcol = 0; /* column where searched text starts */
5230 colnr_T curs_col; /* cursor column */
5231 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005232 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005233 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005234 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005235 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
Bram Moolenaare3226be2005-12-18 22:10:00 +00005237 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005238 insert_match = ins_compl_use_match(c);
5239
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005240 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
5242 /* First time we hit ^N or ^P (in a row, I mean) */
5243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 did_ai = FALSE;
5245#ifdef FEAT_SMARTINDENT
5246 did_si = FALSE;
5247 can_si = FALSE;
5248 can_si_back = FALSE;
5249#endif
5250 if (stop_arrow() == FAIL)
5251 return FAIL;
5252
5253 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005254 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005255 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005257 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005258 * "compl_startpos" to the cursor as a pattern to add a new word
5259 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005260 * "compl_startpos" is not in the same line as the cursor then fix it
5261 * (the line has been split because it was longer than 'tw'). if SOL
5262 * is set then skip the previous pattern, a word at the beginning of
5263 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005264 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5265 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 {
5267 /*
5268 * it is a continued search
5269 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005270 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005271 if (ctrl_x_mode == CTRL_X_NORMAL
5272 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5273 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005275 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005277 /* line (probably) wrapped, set compl_startpos to the
5278 * first non_blank in the line, if it is not a wordchar
5279 * include it to get a better pattern, but then we don't
5280 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005281 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005282 compl_startpos.col = compl_col;
5283 compl_startpos.lnum = curwin->w_cursor.lnum;
5284 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286 else
5287 {
5288 /* S_IPOS was set when we inserted a word that was at the
5289 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005290 * mode but first we need to redefine compl_startpos */
5291 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005293 compl_cont_status |= CONT_SOL;
5294 compl_startpos.col = (colnr_T)(skipwhite(
5295 line + compl_length
5296 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005298 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005300 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005301 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005302 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005304 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005306 compl_cont_status &= ~CONT_SOL;
5307 compl_length = (IOSIZE - MIN_SPACE);
5308 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005310 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5311 if (compl_length < 1)
5312 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005314 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005317 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
5319 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005320 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005322 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005324 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005325 if (ctrl_x_mode != CTRL_X_NORMAL)
5326 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005327 compl_cont_status = 0;
5328 compl_cont_status |= CONT_N_ADDS;
5329 compl_startpos = curwin->w_cursor;
5330 startcol = (int)curs_col;
5331 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333
5334 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005335 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5339 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005340 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005344 compl_col += ++startcol;
5345 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
5347 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 compl_pattern = str_foldcase(line + compl_col,
5349 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005351 compl_pattern = vim_strnsave(line + compl_col,
5352 compl_length);
5353 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 return FAIL;
5355 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005356 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {
5358 char_u *prefix = (char_u *)"\\<";
5359
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005360 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005361 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005362 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005363 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 if (!vim_iswordp(line + compl_col)
5366 || (compl_col > 0
Bram Moolenaar13505972019-01-24 15:04:48 +01005367 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005369 STRCPY((char *)compl_pattern, prefix);
5370 (void)quote_meta(compl_pattern + STRLEN(prefix),
5371 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
Bram Moolenaar13505972019-01-24 15:04:48 +01005373 else if (--startcol < 0
5374 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {
5376 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005377 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5378 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 compl_col += curs_col;
5381 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 }
5383 else
5384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 /* Search the point of change class of multibyte character
5386 * or not a word single byte character backward. */
5387 if (has_mbyte)
5388 {
5389 int base_class;
5390 int head_off;
5391
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005392 startcol -= (*mb_head_off)(line, line + startcol);
5393 base_class = mb_get_class(line + startcol);
5394 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005396 head_off = (*mb_head_off)(line, line + startcol);
5397 if (base_class != mb_get_class(line + startcol
5398 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005400 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
5402 }
5403 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005404 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005406 compl_col += ++startcol;
5407 compl_length = (int)curs_col - startcol;
5408 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {
5410 /* Only match word with at least two chars -- webb
5411 * there's no need to call quote_meta,
5412 * alloc(7) is enough -- Acevedo
5413 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005414 compl_pattern = alloc(7);
5415 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005417 STRCPY((char *)compl_pattern, "\\<");
5418 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5419 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 }
5421 else
5422 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005423 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005424 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005425 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005427 STRCPY((char *)compl_pattern, "\\<");
5428 (void)quote_meta(compl_pattern + 2, line + compl_col,
5429 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431 }
5432 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005433 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005435 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 compl_length = (int)curs_col - (int)compl_col;
5437 if (compl_length < 0) /* cursor in indent: empty pattern */
5438 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005440 compl_pattern = str_foldcase(line + compl_col, compl_length,
5441 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005443 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5444 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 return FAIL;
5446 }
5447 else if (ctrl_x_mode == CTRL_X_FILES)
5448 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005449 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005450 if (startcol > 0)
5451 {
5452 char_u *p = line + startcol;
5453
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005454 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005455 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005456 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005457 if (p == line && vim_isfilec(PTR2CHAR(p)))
5458 startcol = 0;
5459 else
5460 startcol = (int)(p - line) + 1;
5461 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005462
Bram Moolenaar0300e462013-09-08 16:03:45 +02005463 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005464 compl_length = (int)curs_col - startcol;
5465 compl_pattern = addstar(line + compl_col, compl_length,
5466 EXPAND_FILES);
5467 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 return FAIL;
5469 }
5470 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5471 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005472 compl_pattern = vim_strnsave(line, curs_col);
5473 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005475 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005476 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005477 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5478 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005479 /* No completion possible, use an empty pattern to get a
5480 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005481 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005482 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005483 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5484 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005486 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005487 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005488#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005489 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005490 * Call user defined function 'completefunc' with "a:findstart"
5491 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005492 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005493 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005494 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005495 char_u *funcname;
5496 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005497 win_T *curwin_save;
5498 buf_T *curbuf_save;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005499 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005500
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005501 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005502 * string */
5503 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5504 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5505 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005507 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005508 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005509 /* restore did_ai, so that adding comment leader works */
5510 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005511 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005512 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005513
Bram Moolenaarffa96842018-06-12 22:05:14 +02005514 args[0].v_type = VAR_NUMBER;
5515 args[0].vval.v_number = 1;
5516 args[1].v_type = VAR_STRING;
5517 args[1].vval.v_string = (char_u *)"";
5518 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005519 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005520 curwin_save = curwin;
5521 curbuf_save = curbuf;
Bram Moolenaarded27a12018-08-01 19:06:03 +02005522 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005523
5524 State = save_State;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005525 if (curwin_save != curwin || curbuf_save != curbuf)
5526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005527 emsg(_(e_complwin));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005528 return FAIL;
5529 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005530 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005531 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005532 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005533 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005534 emsg(_(e_compldel));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005535 return FAIL;
5536 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005537
Bram Moolenaar6110a002012-01-26 18:58:38 +01005538 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005539 * cancel the complete without an error.
5540 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005541 if (col == -2)
5542 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005543 if (col == -3)
5544 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005545 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005546 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005547 if (!shortmess(SHM_COMPLETIONMENU))
5548 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005549 return FAIL;
5550 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005551
Bram Moolenaar82139082011-09-14 16:52:09 +02005552 /*
5553 * Reset extended parameters of completion, when start new
5554 * completion.
5555 */
5556 compl_opt_refresh_always = FALSE;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005557 compl_opt_suppress_empty = FALSE;
Bram Moolenaar82139082011-09-14 16:52:09 +02005558
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005559 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005560 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005561 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005562 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005563 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005564
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005565 /* Setup variables for completion. Need to obtain "line" again,
5566 * it may have become invalid. */
5567 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005568 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005569 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5570 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005571#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005572 return FAIL;
5573 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005574 else if (ctrl_x_mode == CTRL_X_SPELL)
5575 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005576#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005577 if (spell_bad_len > 0)
5578 compl_col = curs_col - spell_bad_len;
5579 else
5580 compl_col = spell_word_start(startcol);
5581 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005582 {
5583 compl_length = 0;
5584 compl_col = curs_col;
5585 }
5586 else
5587 {
5588 spell_expand_check_cap(compl_col);
5589 compl_length = (int)curs_col - compl_col;
5590 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005591 /* Need to obtain "line" again, it may have become invalid. */
5592 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005593 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5594 if (compl_pattern == NULL)
5595#endif
5596 return FAIL;
5597 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005598 else
5599 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005600 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005601 return FAIL;
5602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005604 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 {
5606 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005607 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
5609 /* Insert a new line, keep indentation but ignore 'comments' */
5610#ifdef FEAT_COMMENTS
5611 char_u *old = curbuf->b_p_com;
5612
5613 curbuf->b_p_com = (char_u *)"";
5614#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005615 compl_startpos.lnum = curwin->w_cursor.lnum;
5616 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 ins_eol('\r');
5618#ifdef FEAT_COMMENTS
5619 curbuf->b_p_com = old;
5620#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005621 compl_length = 0;
5622 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
5624 }
5625 else
5626 {
5627 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005628 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005631 if (compl_cont_status & CONT_LOCAL)
5632 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 else
5634 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5635
Bram Moolenaar62951b12011-09-21 18:23:05 +02005636 /* If any of the original typed text has been changed we need to fix
5637 * the redo buffer. */
5638 ins_compl_fixRedoBufForLeader(NULL);
5639
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005640 /* Always add completion for the original text. */
5641 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005642 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5643 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005644 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005646 VIM_CLEAR(compl_pattern);
5647 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 return FAIL;
5649 }
5650
5651 /* showmode might reset the internal line pointers, so it must
5652 * be called before line = ml_get(), or when this address is no
5653 * longer needed. -- Acevedo.
5654 */
5655 edit_submode_extra = (char_u *)_("-- Searching...");
5656 edit_submode_highl = HLF_COUNT;
5657 showmode();
5658 edit_submode_extra = NULL;
5659 out_flush();
5660 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005661 else if (insert_match && stop_arrow() == FAIL)
5662 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005664 compl_shown_match = compl_curr_match;
5665 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
5667 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005668 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005670 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005671 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005672 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005674 /* may undisplay the popup menu */
5675 ins_compl_upd_pum();
5676
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005677 if (n > 1) /* all matches have been found */
5678 compl_matches = n;
5679 compl_curr_match = compl_shown_match;
5680 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
Bram Moolenaard68071d2006-05-02 22:08:30 +00005682 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5683 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 if (got_int && !global_busy)
5685 {
5686 (void)vgetc();
5687 got_int = FALSE;
5688 }
5689
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005690 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005691 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005693 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5694 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5696 edit_submode_highl = HLF_E;
5697 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5698 * because we couldn't expand anything at first place, but if we used
5699 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5700 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005701 if ( compl_length > 1
5702 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005703 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5705 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005706 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708
Bram Moolenaar572cb562005-08-05 21:35:02 +00005709 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005710 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005712 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713
5714 if (edit_submode_extra == NULL)
5715 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005716 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
5718 edit_submode_extra = (char_u *)_("Back at original");
5719 edit_submode_highl = HLF_W;
5720 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005721 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723 edit_submode_extra = (char_u *)_("Word from other line");
5724 edit_submode_highl = HLF_COUNT;
5725 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005726 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
5728 edit_submode_extra = (char_u *)_("The only match");
5729 edit_submode_highl = HLF_COUNT;
5730 }
5731 else
5732 {
5733 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005734 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005736 int number = 0;
5737 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005739 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 {
5741 /* search backwards for the first valid (!= -1) number.
5742 * This should normally succeed already at the first loop
5743 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005744 for (match = compl_curr_match->cp_prev; match != NULL
5745 && match != compl_first_match;
5746 match = match->cp_prev)
5747 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005749 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 break;
5751 }
5752 if (match != NULL)
5753 /* go up and assign all numbers which are not assigned
5754 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005755 for (match = match->cp_next;
5756 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005757 match = match->cp_next)
5758 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
5760 else /* BACKWARD */
5761 {
5762 /* search forwards (upwards) for the first valid (!= -1)
5763 * number. This should normally succeed already at the
5764 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005765 for (match = compl_curr_match->cp_next; match != NULL
5766 && match != compl_first_match;
5767 match = match->cp_next)
5768 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005770 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 break;
5772 }
5773 if (match != NULL)
5774 /* go down and assign all numbers which are not
5775 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005776 for (match = match->cp_prev; match
5777 && match->cp_number == -1;
5778 match = match->cp_prev)
5779 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
5781 }
5782
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005783 /* The match should always have a sequence number now, this is
5784 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005785 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005787 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5788 * Translations may need more than twice that. */
5789 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005791 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005792 vim_snprintf((char *)match_ref, sizeof(match_ref),
5793 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005794 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005796 vim_snprintf((char *)match_ref, sizeof(match_ref),
5797 _("match %d"),
5798 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 edit_submode_extra = match_ref;
5800 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005801 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 curs_columns(FALSE);
5803 }
5804 }
5805 }
5806
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005807 // Show a message about what (completion) mode we're in.
5808 if (!compl_opt_suppress_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005810 showmode();
5811 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaarea389e92014-05-28 21:40:52 +02005812 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005813 if (edit_submode_extra != NULL)
5814 {
5815 if (!p_smd)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005816 msg_attr((char *)edit_submode_extra,
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005817 edit_submode_highl < HLF_COUNT
5818 ? HL_ATTR(edit_submode_highl) : 0);
5819 }
5820 else
5821 msg_clr_cmdline(); // necessary for "noshowmode"
Bram Moolenaarea389e92014-05-28 21:40:52 +02005822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
Bram Moolenaard68071d2006-05-02 22:08:30 +00005825 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005826 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005827 show_pum(save_w_wrow, save_w_leftcol);
5828
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005829 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005830 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005831
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 return OK;
5833}
5834
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005835 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005836show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005837{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005838 /* RedrawingDisabled may be set when invoked through complete(). */
5839 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005840
Bram Moolenaarcb036422017-03-01 12:29:10 +01005841 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005842
Bram Moolenaarcb036422017-03-01 12:29:10 +01005843 /* If the cursor moved or the display scrolled we need to remove the pum
5844 * first. */
5845 setcursor();
5846 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5847 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005848
Bram Moolenaarcb036422017-03-01 12:29:10 +01005849 ins_compl_show_pum();
5850 setcursor();
5851 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005852}
5853
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854/*
5855 * Looks in the first "len" chars. of "src" for search-metachars.
5856 * If dest is not NULL the chars. are copied there quoting (with
5857 * a backslash) the metachars, and dest would be NUL terminated.
5858 * Returns the length (needed) of dest
5859 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005860 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005861quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005863 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005865 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 {
5867 switch (*src)
5868 {
5869 case '.':
5870 case '*':
5871 case '[':
5872 if (ctrl_x_mode == CTRL_X_DICTIONARY
5873 || ctrl_x_mode == CTRL_X_THESAURUS)
5874 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005875 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 case '~':
5877 if (!p_magic) /* quote these only if magic is set */
5878 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005879 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 case '\\':
5881 if (ctrl_x_mode == CTRL_X_DICTIONARY
5882 || ctrl_x_mode == CTRL_X_THESAURUS)
5883 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005884 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 case '^': /* currently it's not needed. */
5886 case '$':
5887 m++;
5888 if (dest != NULL)
5889 *dest++ = '\\';
5890 break;
5891 }
5892 if (dest != NULL)
5893 *dest++ = *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 /* Copy remaining bytes of a multibyte character. */
5895 if (has_mbyte)
5896 {
5897 int i, mb_len;
5898
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005899 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 if (mb_len > 0 && len >= mb_len)
5901 for (i = 0; i < mb_len; ++i)
5902 {
5903 --len;
5904 ++src;
5905 if (dest != NULL)
5906 *dest++ = *src;
5907 }
5908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 }
5910 if (dest != NULL)
5911 *dest = NUL;
5912
5913 return m;
5914}
5915#endif /* FEAT_INS_EXPAND */
5916
5917/*
5918 * Next character is interpreted literally.
5919 * A one, two or three digit decimal number is interpreted as its byte value.
5920 * If one or two digits are entered, the next character is given to vungetc().
5921 * For Unicode a character > 255 may be returned.
5922 */
5923 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005924get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925{
5926 int cc;
5927 int nc;
5928 int i;
5929 int hex = FALSE;
5930 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
5933 if (got_int)
5934 return Ctrl_C;
5935
5936#ifdef FEAT_GUI
5937 /*
5938 * In GUI there is no point inserting the internal code for a special key.
5939 * It is more useful to insert the string "<KEY>" instead. This would
5940 * probably be useful in a text window too, but it would not be
5941 * vi-compatible (maybe there should be an option for it?) -- webb
5942 */
5943 if (gui.in_use)
5944 ++allow_keys;
5945#endif
5946#ifdef USE_ON_FLY_SCROLL
5947 dont_scroll = TRUE; /* disallow scrolling here */
5948#endif
5949 ++no_mapping; /* don't map the next key hits */
5950 cc = 0;
5951 i = 0;
5952 for (;;)
5953 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005954 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01005956 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 add_to_showcmd(nc);
5958#endif
5959 if (nc == 'x' || nc == 'X')
5960 hex = TRUE;
5961 else if (nc == 'o' || nc == 'O')
5962 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 else if (nc == 'u' || nc == 'U')
5964 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 else
5966 {
Bram Moolenaar13505972019-01-24 15:04:48 +01005967 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 {
5969 if (!vim_isxdigit(nc))
5970 break;
5971 cc = cc * 16 + hex2nr(nc);
5972 }
5973 else if (octal)
5974 {
5975 if (nc < '0' || nc > '7')
5976 break;
5977 cc = cc * 8 + nc - '0';
5978 }
5979 else
5980 {
5981 if (!VIM_ISDIGIT(nc))
5982 break;
5983 cc = cc * 10 + nc - '0';
5984 }
5985
5986 ++i;
5987 }
5988
Bram Moolenaar13505972019-01-24 15:04:48 +01005989 if (cc > 255 && unicode == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 cc = 255; /* limit range to 0-255 */
5991 nc = 0;
5992
5993 if (hex) /* hex: up to two chars */
5994 {
5995 if (i >= 2)
5996 break;
5997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 else if (unicode) /* Unicode: up to four or eight chars */
5999 {
6000 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6001 break;
6002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 else if (i >= 3) /* decimal or octal: up to three chars */
6004 break;
6005 }
6006 if (i == 0) /* no number entered */
6007 {
6008 if (nc == K_ZERO) /* NUL is stored as NL */
6009 {
6010 cc = '\n';
6011 nc = 0;
6012 }
6013 else
6014 {
6015 cc = nc;
6016 nc = 0;
6017 }
6018 }
6019
6020 if (cc == 0) /* NUL is stored as NL */
6021 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006022 if (enc_dbcs && (cc & 0xff) == 0)
6023 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6024 second byte will cause trouble! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025
6026 --no_mapping;
6027#ifdef FEAT_GUI
6028 if (gui.in_use)
6029 --allow_keys;
6030#endif
6031 if (nc)
6032 vungetc(nc);
6033 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6034 return cc;
6035}
6036
6037/*
6038 * Insert character, taking care of special keys and mod_mask
6039 */
6040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006041insert_special(
6042 int c,
6043 int allow_modmask,
6044 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 char_u *p;
6047 int len;
6048
6049 /*
6050 * Special function key, translate into "<Key>". Up to the last '>' is
6051 * inserted with ins_str(), so as not to replace characters in replace
6052 * mode.
6053 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6054 * unless 'allow_modmask' is TRUE.
6055 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006056#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 /* Command-key never produces a normal key */
6058 if (mod_mask & MOD_MASK_CMD)
6059 allow_modmask = TRUE;
6060#endif
6061 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6062 {
6063 p = get_special_key_name(c, mod_mask);
6064 len = (int)STRLEN(p);
6065 c = p[len - 1];
6066 if (len > 2)
6067 {
6068 if (stop_arrow() == FAIL)
6069 return;
6070 p[len - 1] = NUL;
6071 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006072 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 ctrlv = FALSE;
6074 }
6075 }
6076 if (stop_arrow() == OK)
6077 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6078}
6079
6080/*
6081 * Special characters in this context are those that need processing other
6082 * than the simple insertion that can be performed here. This includes ESC
6083 * which terminates the insert, and CR/NL which need special processing to
6084 * open up a new line. This routine tries to optimize insertions performed by
6085 * the "redo", "undo" or "put" commands, so it needs to know when it should
6086 * stop and defer processing to the "normal" mechanism.
6087 * '0' and '^' are special, because they can be followed by CTRL-D.
6088 */
6089#ifdef EBCDIC
6090# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6091#else
6092# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6093#endif
6094
Bram Moolenaar13505972019-01-24 15:04:48 +01006095#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006097/*
6098 * "flags": INSCHAR_FORMAT - force formatting
6099 * INSCHAR_CTRLV - char typed just after CTRL-V
6100 * INSCHAR_NO_FEX - don't use 'formatexpr'
6101 *
6102 * NOTE: passes the flags value straight through to internal_format() which,
6103 * beside INSCHAR_FORMAT (above), is also looking for these:
6104 * INSCHAR_DO_COM - format comments
6105 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006108insertchar(
6109 int c, /* character to insert or NUL */
6110 int flags, /* INSCHAR_FORMAT, etc. */
6111 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 int textwidth;
6114#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006118 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006120 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122
6123 /*
6124 * Try to break the line in two or more pieces when:
6125 * - Always do this if we have been called to do formatting only.
6126 * - Always do this when 'formatoptions' has the 'a' flag and the line
6127 * ends in white space.
6128 * - Otherwise:
6129 * - Don't do this if inserting a blank
6130 * - Don't do this if an existing character is being replaced, unless
6131 * we're in VREPLACE mode.
6132 * - Do this if the cursor is not on the line where insert started
6133 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6134 * before the insert.
6135 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6136 * before 'textwidth'
6137 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006138 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006139 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006140 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 && *ml_get_cursor() != NUL)
6144 && (curwin->w_cursor.lnum != Insstart.lnum
6145 || ((!has_format_option(FO_INS_LONG)
6146 || Insstart_textlen <= (colnr_T)textwidth)
6147 && (!fo_ins_blank
6148 || Insstart_blank_vcol <= (colnr_T)textwidth
6149 ))))))
6150 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006151 /* Format with 'formatexpr' when it's set. Use internal formatting
6152 * when 'formatexpr' isn't set or it returns non-zero. */
6153#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006154 int do_internal = TRUE;
6155 colnr_T virtcol = get_nolist_virtcol()
6156 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006157
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006158 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6159 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006160 {
6161 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6162 /* It may be required to save for undo again, e.g. when setline()
6163 * was called. */
6164 ins_need_undo = TRUE;
6165 }
6166 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006168 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006170
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 if (c == NUL) /* only formatting was wanted */
6172 return;
6173
6174#ifdef FEAT_COMMENTS
6175 /* Check whether this character should end a comment. */
6176 if (did_ai && (int)c == end_comment_pending)
6177 {
6178 char_u *line;
6179 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6180 int middle_len, end_len;
6181 int i;
6182
6183 /*
6184 * Need to remove existing (middle) comment leader and insert end
6185 * comment leader. First, check what comment leader we can find.
6186 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006187 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6189 {
6190 /* Skip middle-comment string */
6191 while (*p && p[-1] != ':') /* find end of middle flags */
6192 ++p;
6193 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6194 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006195 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 --middle_len;
6197
6198 /* Find the end-comment string */
6199 while (*p && p[-1] != ':') /* find end of end flags */
6200 ++p;
6201 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6202
6203 /* Skip white space before the cursor */
6204 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006205 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 ;
6207 i++;
6208
6209 /* Skip to before the middle leader */
6210 i -= middle_len;
6211
6212 /* Check some expected things before we go on */
6213 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6214 {
6215 /* Backspace over all the stuff we want to replace */
6216 backspace_until_column(i);
6217
6218 /*
6219 * Insert the end-comment string, except for the last
6220 * character, which will get inserted as normal later.
6221 */
6222 ins_bytes_len(lead_end, end_len - 1);
6223 }
6224 }
6225 }
6226 end_comment_pending = NUL;
6227#endif
6228
6229 did_ai = FALSE;
6230#ifdef FEAT_SMARTINDENT
6231 did_si = FALSE;
6232 can_si = FALSE;
6233 can_si_back = FALSE;
6234#endif
6235
6236 /*
6237 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6238 * This speeds up normal text input considerably.
6239 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6240 * need to re-indent at a ':', or any other character (but not what
6241 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006242 * Don't do this when there an InsertCharPre autocommand is defined,
6243 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006244 * Do the check for InsertCharPre before the call to vpeekc() because the
6245 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 */
6247#ifdef USE_ON_FLY_SCROLL
6248 dont_scroll = FALSE; /* allow scrolling here */
6249#endif
6250
6251 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02006253 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 && vpeekc() != NUL
6255 && !(State & REPLACE_FLAG)
6256#ifdef FEAT_CINDENT
6257 && !cindent_on()
6258#endif
6259#ifdef FEAT_RIGHTLEFT
6260 && !p_ri
6261#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006262 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {
6264#define INPUT_BUFLEN 100
6265 char_u buf[INPUT_BUFLEN + 1];
6266 int i;
6267 colnr_T virtcol = 0;
6268
6269 buf[0] = c;
6270 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006271 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 virtcol = get_nolist_virtcol();
6273 /*
6274 * Stop the string when:
6275 * - no more chars available
6276 * - finding a special character (command key)
6277 * - buffer is full
6278 * - running into the 'textwidth' boundary
6279 * - need to check for abbreviation: A non-word char after a word-char
6280 */
6281 while ( (c = vpeekc()) != NUL
6282 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006285# ifdef FEAT_FKMAP
6286 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 && (textwidth == 0
6289 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6290 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6291 {
6292#ifdef FEAT_RIGHTLEFT
6293 c = vgetc();
6294 if (p_hkmap && KeyTyped)
6295 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 buf[i++] = c;
6297#else
6298 buf[i++] = vgetc();
6299#endif
6300 }
6301
6302#ifdef FEAT_DIGRAPHS
6303 do_digraph(-1); /* clear digraphs */
6304 do_digraph(buf[i-1]); /* may be the start of a digraph */
6305#endif
6306 buf[i] = NUL;
6307 ins_str(buf);
6308 if (flags & INSCHAR_CTRLV)
6309 {
6310 redo_literal(*buf);
6311 i = 1;
6312 }
6313 else
6314 i = 0;
6315 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006316 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 }
6318 else
6319 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006320 int cc;
6321
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6323 {
6324 char_u buf[MB_MAXBYTES + 1];
6325
6326 (*mb_char2bytes)(c, buf);
6327 buf[cc] = NUL;
6328 ins_char_bytes(buf, cc);
6329 AppendCharToRedobuff(c);
6330 }
6331 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 {
6333 ins_char(c);
6334 if (flags & INSCHAR_CTRLV)
6335 redo_literal(c);
6336 else
6337 AppendCharToRedobuff(c);
6338 }
6339 }
6340}
6341
6342/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006343 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006344 *
6345 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6346 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006347 */
6348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006349internal_format(
6350 int textwidth,
6351 int second_indent,
6352 int flags,
6353 int format_only,
6354 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006355{
6356 int cc;
6357 int save_char = NUL;
6358 int haveto_redraw = FALSE;
6359 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006360 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006361 int fo_white_par = has_format_option(FO_WHITE_PAR);
6362 int first_line = TRUE;
6363#ifdef FEAT_COMMENTS
6364 colnr_T leader_len;
6365 int no_leader = FALSE;
6366 int do_comments = (flags & INSCHAR_DO_COM);
6367#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006368#ifdef FEAT_LINEBREAK
6369 int has_lbr = curwin->w_p_lbr;
6370
6371 /* make sure win_lbr_chartabsize() counts correctly */
6372 curwin->w_p_lbr = FALSE;
6373#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006374
6375 /*
6376 * When 'ai' is off we don't want a space under the cursor to be
6377 * deleted. Replace it with an 'x' temporarily.
6378 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006379 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006380 {
6381 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006382 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006383 {
6384 save_char = cc;
6385 pchar_cursor('x');
6386 }
6387 }
6388
6389 /*
6390 * Repeat breaking lines, until the current line is not too long.
6391 */
6392 while (!got_int)
6393 {
6394 int startcol; /* Cursor column at entry */
6395 int wantcol; /* column at textwidth border */
6396 int foundcol; /* column for start of spaces */
6397 int end_foundcol = 0; /* column for start of word */
6398 colnr_T len;
6399 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006400 int orig_col = 0;
6401 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006402 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006403 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006404 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006405
Bram Moolenaar97b98102009-11-17 16:41:01 +00006406 virtcol = get_nolist_virtcol()
6407 + char2cells(c != NUL ? c : gchar_cursor());
6408 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006409 break;
6410
6411#ifdef FEAT_COMMENTS
6412 if (no_leader)
6413 do_comments = FALSE;
6414 else if (!(flags & INSCHAR_FORMAT)
6415 && has_format_option(FO_WRAP_COMS))
6416 do_comments = TRUE;
6417
6418 /* Don't break until after the comment leader */
6419 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006420 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006421 else
6422 leader_len = 0;
6423
6424 /* If the line doesn't start with a comment leader, then don't
6425 * start one in a following broken line. Avoids that a %word
6426 * moved to the start of the next line causes all following lines
6427 * to start with %. */
6428 if (leader_len == 0)
6429 no_leader = TRUE;
6430#endif
6431 if (!(flags & INSCHAR_FORMAT)
6432#ifdef FEAT_COMMENTS
6433 && leader_len == 0
6434#endif
6435 && !has_format_option(FO_WRAP))
6436
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006437 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006438 if ((startcol = curwin->w_cursor.col) == 0)
6439 break;
6440
6441 /* find column of textwidth border */
6442 coladvance((colnr_T)textwidth);
6443 wantcol = curwin->w_cursor.col;
6444
Bram Moolenaar97b98102009-11-17 16:41:01 +00006445 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006446 foundcol = 0;
6447
6448 /*
6449 * Find position to break at.
6450 * Stop at first entered white when 'formatoptions' has 'v'
6451 */
6452 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006453 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006454 || curwin->w_cursor.lnum != Insstart.lnum
6455 || curwin->w_cursor.col >= Insstart.col)
6456 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006457 if (curwin->w_cursor.col == startcol && c != NUL)
6458 cc = c;
6459 else
6460 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006461 if (WHITECHAR(cc))
6462 {
6463 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006464 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006465
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006466 // find start of sequence of blanks
6467 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006468 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6469 {
6470 dec_cursor();
6471 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006472
6473 // Increment count of how many whitespace chars in this
6474 // group; we only need to know if it's more than one.
6475 if (wcc < 2)
6476 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006477 }
6478 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6479 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006480
6481 // Don't break after a period when 'formatoptions' has 'p' and
6482 // there are less than two spaces.
6483 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
6484 continue;
6485
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006486#ifdef FEAT_COMMENTS
6487 /* Don't break until after the comment leader */
6488 if (curwin->w_cursor.col < leader_len)
6489 break;
6490#endif
6491 if (has_format_option(FO_ONE_LETTER))
6492 {
6493 /* do not break after one-letter words */
6494 if (curwin->w_cursor.col == 0)
6495 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006496#ifdef FEAT_COMMENTS
6497 /* do not break "#a b" when 'tw' is 2 */
6498 if (curwin->w_cursor.col <= leader_len)
6499 break;
6500#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006501 col = curwin->w_cursor.col;
6502 dec_cursor();
6503 cc = gchar_cursor();
6504
6505 if (WHITECHAR(cc))
6506 continue; /* one-letter, continue */
6507 curwin->w_cursor.col = col;
6508 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006509
6510 inc_cursor();
6511
6512 end_foundcol = end_col + 1;
6513 foundcol = curwin->w_cursor.col;
6514 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006515 break;
6516 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006517 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006518 {
6519 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006520 if (curwin->w_cursor.col != startcol)
6521 {
6522#ifdef FEAT_COMMENTS
6523 /* Don't break until after the comment leader */
6524 if (curwin->w_cursor.col < leader_len)
6525 break;
6526#endif
6527 col = curwin->w_cursor.col;
6528 inc_cursor();
6529 /* Don't change end_foundcol if already set. */
6530 if (foundcol != curwin->w_cursor.col)
6531 {
6532 foundcol = curwin->w_cursor.col;
6533 end_foundcol = foundcol;
6534 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6535 break;
6536 }
6537 curwin->w_cursor.col = col;
6538 }
6539
6540 if (curwin->w_cursor.col == 0)
6541 break;
6542
6543 col = curwin->w_cursor.col;
6544
6545 dec_cursor();
6546 cc = gchar_cursor();
6547
6548 if (WHITECHAR(cc))
6549 continue; /* break with space */
6550#ifdef FEAT_COMMENTS
6551 /* Don't break until after the comment leader */
6552 if (curwin->w_cursor.col < leader_len)
6553 break;
6554#endif
6555
6556 curwin->w_cursor.col = col;
6557
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006558 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006559 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006560 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6561 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006562 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006563 if (curwin->w_cursor.col == 0)
6564 break;
6565 dec_cursor();
6566 }
6567
6568 if (foundcol == 0) /* no spaces, cannot break line */
6569 {
6570 curwin->w_cursor.col = startcol;
6571 break;
6572 }
6573
6574 /* Going to break the line, remove any "$" now. */
6575 undisplay_dollar();
6576
6577 /*
6578 * Offset between cursor position and line break is used by replace
6579 * stack functions. VREPLACE does not use this, and backspaces
6580 * over the text instead.
6581 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006582 if (State & VREPLACE_FLAG)
6583 orig_col = startcol; /* Will start backspacing from here */
6584 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006585 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006586
6587 /*
6588 * adjust startcol for spaces that will be deleted and
6589 * characters that will remain on top line
6590 */
6591 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006592 while ((cc = gchar_cursor(), WHITECHAR(cc))
6593 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006594 inc_cursor();
6595 startcol -= curwin->w_cursor.col;
6596 if (startcol < 0)
6597 startcol = 0;
6598
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006599 if (State & VREPLACE_FLAG)
6600 {
6601 /*
6602 * In VREPLACE mode, we will backspace over the text to be
6603 * wrapped, so save a copy now to put on the next line.
6604 */
6605 saved_text = vim_strsave(ml_get_cursor());
6606 curwin->w_cursor.col = orig_col;
6607 if (saved_text == NULL)
6608 break; /* Can't do it, out of memory */
6609 saved_text[startcol] = NUL;
6610
6611 /* Backspace over characters that will move to the next line */
6612 if (!fo_white_par)
6613 backspace_until_column(foundcol);
6614 }
6615 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006616 {
6617 /* put cursor after pos. to break line */
6618 if (!fo_white_par)
6619 curwin->w_cursor.col = foundcol;
6620 }
6621
6622 /*
6623 * Split the line just before the margin.
6624 * Only insert/delete lines, but don't really redraw the window.
6625 */
6626 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6627 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6628#ifdef FEAT_COMMENTS
6629 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006630 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006631#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006632 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6633 if (!(flags & INSCHAR_COM_LIST))
6634 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006635
6636 replace_offset = 0;
6637 if (first_line)
6638 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006639 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006640 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006641 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006642 * This section is for auto-wrap of numeric lists. When not
6643 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6644 * flag will be set and open_line() will handle it (as seen
6645 * above). The code here (and in get_number_indent()) will
6646 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006647 */
6648 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006649 second_indent =
6650 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006651 if (second_indent >= 0)
6652 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006653 if (State & VREPLACE_FLAG)
6654 change_indent(INDENT_SET, second_indent,
6655 FALSE, NUL, TRUE);
6656 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006657#ifdef FEAT_COMMENTS
6658 if (leader_len > 0 && second_indent - leader_len > 0)
6659 {
6660 int i;
6661 int padding = second_indent - leader_len;
6662
6663 /* We started at the first_line of a numbered list
6664 * that has a comment. the open_line() function has
6665 * inserted the proper comment leader and positioned
6666 * the cursor at the end of the split line. Now we
6667 * add the additional whitespace needed after the
6668 * comment leader for the numbered list. */
6669 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006670 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006671 }
6672 else
6673 {
6674#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006675 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006676#ifdef FEAT_COMMENTS
6677 }
6678#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006679 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006680 }
6681 first_line = FALSE;
6682 }
6683
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006684 if (State & VREPLACE_FLAG)
6685 {
6686 /*
6687 * In VREPLACE mode we have backspaced over the text to be
6688 * moved, now we re-insert it into the new line.
6689 */
6690 ins_bytes(saved_text);
6691 vim_free(saved_text);
6692 }
6693 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006694 {
6695 /*
6696 * Check if cursor is not past the NUL off the line, cindent
6697 * may have added or removed indent.
6698 */
6699 curwin->w_cursor.col += startcol;
6700 len = (colnr_T)STRLEN(ml_get_curline());
6701 if (curwin->w_cursor.col > len)
6702 curwin->w_cursor.col = len;
6703 }
6704
6705 haveto_redraw = TRUE;
6706#ifdef FEAT_CINDENT
6707 can_cindent = TRUE;
6708#endif
6709 /* moved the cursor, don't autoindent or cindent now */
6710 did_ai = FALSE;
6711#ifdef FEAT_SMARTINDENT
6712 did_si = FALSE;
6713 can_si = FALSE;
6714 can_si_back = FALSE;
6715#endif
6716 line_breakcheck();
6717 }
6718
6719 if (save_char != NUL) /* put back space after cursor */
6720 pchar_cursor(save_char);
6721
Bram Moolenaar0026d472014-09-09 16:32:39 +02006722#ifdef FEAT_LINEBREAK
6723 curwin->w_p_lbr = has_lbr;
6724#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006725 if (!format_only && haveto_redraw)
6726 {
6727 update_topline();
6728 redraw_curbuf_later(VALID);
6729 }
6730}
6731
6732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 * Called after inserting or deleting text: When 'formatoptions' includes the
6734 * 'a' flag format from the current line until the end of the paragraph.
6735 * Keep the cursor at the same position relative to the text.
6736 * The caller must have saved the cursor line for undo, following ones will be
6737 * saved here.
6738 */
6739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006740auto_format(
6741 int trailblank, /* when TRUE also format with trailing blank */
6742 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743{
6744 pos_T pos;
6745 colnr_T len;
6746 char_u *old;
6747 char_u *new, *pnew;
6748 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006749 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
6751 if (!has_format_option(FO_AUTO))
6752 return;
6753
6754 pos = curwin->w_cursor;
6755 old = ml_get_curline();
6756
6757 /* may remove added space */
6758 check_auto_format(FALSE);
6759
6760 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6761 * user might insert normal text next. Also skip formatting when "1" is
6762 * in 'formatoptions' and there is a single character before the cursor.
6763 * Otherwise the line would be broken and when typing another non-white
6764 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006765 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (*old != NUL && !trailblank && wasatend)
6767 {
6768 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006769 cc = gchar_cursor();
6770 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6771 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006773 cc = gchar_cursor();
6774 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 {
6776 curwin->w_cursor = pos;
6777 return;
6778 }
6779 curwin->w_cursor = pos;
6780 }
6781
6782#ifdef FEAT_COMMENTS
6783 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6784 * comments. */
6785 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006786 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 return;
6788#endif
6789
6790 /*
6791 * May start formatting in a previous line, so that after "x" a word is
6792 * moved to the previous line if it fits there now. Only when this is not
6793 * the start of a paragraph.
6794 */
6795 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6796 {
6797 --curwin->w_cursor.lnum;
6798 if (u_save_cursor() == FAIL)
6799 return;
6800 }
6801
6802 /*
6803 * Do the formatting and restore the cursor position. "saved_cursor" will
6804 * be adjusted for the text formatting.
6805 */
6806 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006807 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 curwin->w_cursor = saved_cursor;
6809 saved_cursor.lnum = 0;
6810
6811 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6812 {
6813 /* "cannot happen" */
6814 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6815 coladvance((colnr_T)MAXCOL);
6816 }
6817 else
6818 check_cursor_col();
6819
6820 /* Insert mode: If the cursor is now after the end of the line while it
6821 * previously wasn't, the line was broken. Because of the rule above we
6822 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6823 * formatted. */
6824 if (!wasatend && has_format_option(FO_WHITE_PAR))
6825 {
6826 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006827 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 if (curwin->w_cursor.col == len)
6829 {
6830 pnew = vim_strnsave(new, len + 2);
6831 pnew[len] = ' ';
6832 pnew[len + 1] = NUL;
6833 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6834 /* remove the space later */
6835 did_add_space = TRUE;
6836 }
6837 else
6838 /* may remove added space */
6839 check_auto_format(FALSE);
6840 }
6841
6842 check_cursor();
6843}
6844
6845/*
6846 * When an extra space was added to continue a paragraph for auto-formatting,
6847 * delete it now. The space must be under the cursor, just after the insert
6848 * position.
6849 */
6850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006851check_auto_format(
6852 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853{
6854 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006855 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856
6857 if (did_add_space)
6858 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006859 cc = gchar_cursor();
6860 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 /* Somehow the space was removed already. */
6862 did_add_space = FALSE;
6863 else
6864 {
6865 if (!end_insert)
6866 {
6867 inc_cursor();
6868 c = gchar_cursor();
6869 dec_cursor();
6870 }
6871 if (c != NUL)
6872 {
6873 /* The space is no longer at the end of the line, delete it. */
6874 del_char(FALSE);
6875 did_add_space = FALSE;
6876 }
6877 }
6878 }
6879}
6880
6881/*
6882 * Find out textwidth to be used for formatting:
6883 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006884 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 * if invalid value, use 0.
6886 * Set default to window width (maximum 79) for "gq" operator.
6887 */
6888 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006889comp_textwidth(
6890 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 int textwidth;
6893
6894 textwidth = curbuf->b_p_tw;
6895 if (textwidth == 0 && curbuf->b_p_wm)
6896 {
6897 /* The width is the window width minus 'wrapmargin' minus all the
6898 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006899 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900#ifdef FEAT_CMDWIN
6901 if (cmdwin_type != 0)
6902 textwidth -= 1;
6903#endif
6904#ifdef FEAT_FOLDING
6905 textwidth -= curwin->w_p_fdc;
6906#endif
6907#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006908 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 textwidth -= 1;
6910#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006911 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 textwidth -= 8;
6913 }
6914 if (textwidth < 0)
6915 textwidth = 0;
6916 if (ff && textwidth == 0)
6917 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006918 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 if (textwidth > 79)
6920 textwidth = 79;
6921 }
6922 return textwidth;
6923}
6924
6925/*
6926 * Put a character in the redo buffer, for when just after a CTRL-V.
6927 */
6928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006929redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930{
6931 char_u buf[10];
6932
6933 /* Only digits need special treatment. Translate them into a string of
6934 * three digits. */
6935 if (VIM_ISDIGIT(c))
6936 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006937 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 AppendToRedobuff(buf);
6939 }
6940 else
6941 AppendCharToRedobuff(c);
6942}
6943
6944/*
6945 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006946 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 */
6948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006949start_arrow(
6950 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006952 start_arrow_common(end_insert_pos, TRUE);
6953}
6954
6955/*
6956 * Like start_arrow() but with end_change argument.
6957 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6958 */
6959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006960start_arrow_with_change(
6961 pos_T *end_insert_pos, /* can be NULL */
6962 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006963{
6964 start_arrow_common(end_insert_pos, end_change);
6965 if (!end_change)
6966 {
6967 AppendCharToRedobuff(Ctrl_G);
6968 AppendCharToRedobuff('U');
6969 }
6970}
6971
6972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006973start_arrow_common(
6974 pos_T *end_insert_pos, /* can be NULL */
6975 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006976{
6977 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 {
6979 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006980 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 arrow_used = TRUE; /* this means we stopped the current insert */
6982 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006983#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006984 check_spell_redraw();
6985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986}
6987
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006988#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006989/*
6990 * If we skipped highlighting word at cursor, do it now.
6991 * It may be skipped again, thus reset spell_redraw_lnum first.
6992 */
6993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006994check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006995{
6996 if (spell_redraw_lnum != 0)
6997 {
6998 linenr_T lnum = spell_redraw_lnum;
6999
7000 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01007001 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00007002 }
7003}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007004
7005/*
7006 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7007 * spelled word, if there is one.
7008 */
7009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007010spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007011{
7012 pos_T tpos = curwin->w_cursor;
7013
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007014 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007015 if (curwin->w_cursor.col != tpos.col)
7016 start_arrow(&tpos);
7017}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007018#endif
7019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020/*
7021 * stop_arrow() is called before a change is made in insert mode.
7022 * If an arrow key has been used, start a new insertion.
7023 * Returns FAIL if undo is impossible, shouldn't insert then.
7024 */
7025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007026stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 if (arrow_used)
7029 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007030 Insstart = curwin->w_cursor; /* new insertion starts here */
7031 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7032 /* Don't update the original insert position when moved to the
7033 * right, except when nothing was inserted yet. */
7034 update_Insstart_orig = FALSE;
7035 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7036
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 if (u_save_cursor() == OK)
7038 {
7039 arrow_used = FALSE;
7040 ins_need_undo = FALSE;
7041 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007042
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 if (State & VREPLACE_FLAG)
7045 {
7046 orig_line_count = curbuf->b_ml.ml_line_count;
7047 vr_lines_changed = 1;
7048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 ResetRedobuff();
7050 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007051 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 }
7053 else if (ins_need_undo)
7054 {
7055 if (u_save_cursor() == OK)
7056 ins_need_undo = FALSE;
7057 }
7058
7059#ifdef FEAT_FOLDING
7060 /* Always open fold at the cursor line when inserting something. */
7061 foldOpenCursor();
7062#endif
7063
7064 return (arrow_used || ins_need_undo ? FAIL : OK);
7065}
7066
7067/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007068 * Do a few things to stop inserting.
7069 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7070 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 */
7072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007073stop_insert(
7074 pos_T *end_insert_pos,
7075 int esc, /* called by ins_esc() */
7076 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007078 int cc;
7079 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080
7081 stop_redo_ins();
7082 replace_flush(); /* abandon replace stack */
7083
7084 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007085 * Save the inserted text for later redo with ^@ and CTRL-A.
7086 * Don't do it when "restart_edit" was set and nothing was inserted,
7087 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007089 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007090 if (did_restart_edit == 0 || (ptr != NULL
7091 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007092 {
7093 vim_free(last_insert);
7094 last_insert = ptr;
7095 last_insert_skip = new_insert_skip;
7096 }
7097 else
7098 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007100 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
7102 /* Auto-format now. It may seem strange to do this when stopping an
7103 * insertion (or moving the cursor), but it's required when appending
7104 * a line and having it end in a space. But only do it when something
7105 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007106 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007108 pos_T tpos = curwin->w_cursor;
7109
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 /* When the cursor is at the end of the line after a space the
7111 * formatting will move it to the following word. Avoid that by
7112 * moving the cursor onto the space. */
7113 cc = 'x';
7114 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7115 {
7116 dec_cursor();
7117 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007118 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007119 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 }
7121
7122 auto_format(TRUE, FALSE);
7123
Bram Moolenaar1c465442017-03-12 20:10:05 +01007124 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007125 {
7126 if (gchar_cursor() != NUL)
7127 inc_cursor();
7128#ifdef FEAT_VIRTUALEDIT
7129 /* If the cursor is still at the same character, also keep
7130 * the "coladd". */
7131 if (gchar_cursor() == NUL
7132 && curwin->w_cursor.lnum == tpos.lnum
7133 && curwin->w_cursor.col == tpos.col)
7134 curwin->w_cursor.coladd = tpos.coladd;
7135#endif
7136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 }
7138
7139 /* If a space was inserted for auto-formatting, remove it now. */
7140 check_auto_format(TRUE);
7141
7142 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007143 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007144 * Do this when ESC was used or moving the cursor up/down.
7145 * Check for the old position still being valid, just in case the text
7146 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007147 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007148 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7149 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007151 pos_T tpos = curwin->w_cursor;
7152
7153 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007154 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007155 for (;;)
7156 {
7157 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7158 --curwin->w_cursor.col;
7159 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007160 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007161 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007162 if (del_char(TRUE) == FAIL)
7163 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007164 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007165 if (curwin->w_cursor.lnum != tpos.lnum)
7166 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007167 else
7168 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007169 /* reset tpos, could have been invalidated in the loop above */
7170 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007171 tpos.col++;
7172 if (cc != NUL && gchar_pos(&tpos) == NUL)
7173 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 /* <C-S-Right> may have started Visual mode, adjust the position for
7177 * deleted characters. */
7178 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7179 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007180 int len = (int)STRLEN(ml_get_curline());
7181
7182 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007184 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007185#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
7189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 }
7191 }
7192 did_ai = FALSE;
7193#ifdef FEAT_SMARTINDENT
7194 did_si = FALSE;
7195 can_si = FALSE;
7196 can_si_back = FALSE;
7197#endif
7198
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007199 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7200 * now in a different buffer. */
7201 if (end_insert_pos != NULL)
7202 {
7203 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007204 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007205 curbuf->b_op_end = *end_insert_pos;
7206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207}
7208
7209/*
7210 * Set the last inserted text to a single character.
7211 * Used for the replace command.
7212 */
7213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007214set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
7216 char_u *s;
7217
7218 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 if (last_insert != NULL)
7221 {
7222 s = last_insert;
7223 /* Use the CTRL-V only when entering a special char */
7224 if (c < ' ' || c == DEL)
7225 *s++ = Ctrl_V;
7226 s = add_char2buf(c, s);
7227 *s++ = ESC;
7228 *s++ = NUL;
7229 last_insert_skip = 0;
7230 }
7231}
7232
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007233#if defined(EXITFREE) || defined(PROTO)
7234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007235free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007236{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007237 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007238# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007239 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007240# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007241}
7242#endif
7243
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244/*
7245 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7246 * and CSI. Handle multi-byte characters.
7247 * Returns a pointer to after the added bytes.
7248 */
7249 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007250add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007252 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 int i;
7254 int len;
7255
7256 len = (*mb_char2bytes)(c, temp);
7257 for (i = 0; i < len; ++i)
7258 {
7259 c = temp[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7261 if (c == K_SPECIAL)
7262 {
7263 *s++ = K_SPECIAL;
7264 *s++ = KS_SPECIAL;
7265 *s++ = KE_FILLER;
7266 }
7267#ifdef FEAT_GUI
7268 else if (c == CSI)
7269 {
7270 *s++ = CSI;
7271 *s++ = KS_EXTRA;
7272 *s++ = (int)KE_CSI;
7273 }
7274#endif
7275 else
7276 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 return s;
7279}
7280
7281/*
7282 * move cursor to start of line
7283 * if flags & BL_WHITE move to first non-white
7284 * if flags & BL_SOL move to first non-white if startofline is set,
7285 * otherwise keep "curswant" column
7286 * if flags & BL_FIX don't leave the cursor on a NUL.
7287 */
7288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007289beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290{
7291 if ((flags & BL_SOL) && !p_sol)
7292 coladvance(curwin->w_curswant);
7293 else
7294 {
7295 curwin->w_cursor.col = 0;
7296#ifdef FEAT_VIRTUALEDIT
7297 curwin->w_cursor.coladd = 0;
7298#endif
7299
7300 if (flags & (BL_WHITE | BL_SOL))
7301 {
7302 char_u *ptr;
7303
Bram Moolenaar1c465442017-03-12 20:10:05 +01007304 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7306 ++curwin->w_cursor.col;
7307 }
7308 curwin->w_set_curswant = TRUE;
7309 }
7310}
7311
7312/*
7313 * oneright oneleft cursor_down cursor_up
7314 *
7315 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007316 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 * Return OK when successful, FAIL when we hit a line of file boundary.
7318 */
7319
7320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007321oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322{
7323 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325
7326#ifdef FEAT_VIRTUALEDIT
7327 if (virtual_active())
7328 {
7329 pos_T prevpos = curwin->w_cursor;
7330
7331 /* Adjust for multi-wide char (excluding TAB) */
7332 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01007333 coladvance(getviscol() + ((*ptr != TAB
7334 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 ? ptr2cells(ptr) : 1));
7336 curwin->w_set_curswant = TRUE;
7337 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7338 return (prevpos.col != curwin->w_cursor.col
7339 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7340 }
7341#endif
7342
7343 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007344 if (*ptr == NUL)
7345 return FAIL; /* already at the very end */
7346
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007347 if (has_mbyte)
7348 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007350 l = 1;
7351
7352 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7353 * contains "onemore". */
7354 if (ptr[l] == NUL
7355#ifdef FEAT_VIRTUALEDIT
7356 && (ve_flags & VE_ONEMORE) == 0
7357#endif
7358 )
7359 return FAIL;
7360 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361
7362 curwin->w_set_curswant = TRUE;
7363 return OK;
7364}
7365
7366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007367oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
7369#ifdef FEAT_VIRTUALEDIT
7370 if (virtual_active())
7371 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007372# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 int v = getviscol();
7376
7377 if (v == 0)
7378 return FAIL;
7379
7380# ifdef FEAT_LINEBREAK
7381 /* We might get stuck on 'showbreak', skip over it. */
7382 width = 1;
7383 for (;;)
7384 {
7385 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007386 /* getviscol() is slow, skip it when 'showbreak' is empty,
7387 * 'breakindent' is not set and there are no multi-byte
7388 * characters */
7389 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01007390 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 break;
7392 ++width;
7393 }
7394# else
7395 coladvance(v - 1);
7396# endif
7397
7398 if (curwin->w_cursor.coladd == 1)
7399 {
7400 char_u *ptr;
7401
7402 /* Adjust for multi-wide char (not a TAB) */
7403 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01007404 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
7405 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 curwin->w_cursor.coladd = 0;
7407 }
7408
7409 curwin->w_set_curswant = TRUE;
7410 return OK;
7411 }
7412#endif
7413
7414 if (curwin->w_cursor.col == 0)
7415 return FAIL;
7416
7417 curwin->w_set_curswant = TRUE;
7418 --curwin->w_cursor.col;
7419
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 /* if the character on the left of the current cursor is a multi-byte
7421 * character, move to its first byte */
7422 if (has_mbyte)
7423 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 return OK;
7425}
7426
7427 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007428cursor_up(
7429 long n,
7430 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
7432 linenr_T lnum;
7433
7434 if (n > 0)
7435 {
7436 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007437 /* This fails if the cursor is already in the first line or the count
7438 * is larger than the line number and '-' is in 'cpoptions' */
7439 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 return FAIL;
7441 if (n >= lnum)
7442 lnum = 1;
7443 else
7444#ifdef FEAT_FOLDING
7445 if (hasAnyFolding(curwin))
7446 {
7447 /*
7448 * Count each sequence of folded lines as one logical line.
7449 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007450 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 (void)hasFolding(lnum, &lnum, NULL);
7452
7453 while (n--)
7454 {
7455 /* move up one line */
7456 --lnum;
7457 if (lnum <= 1)
7458 break;
7459 /* If we entered a fold, move to the beginning, unless in
7460 * Insert mode or when 'foldopen' contains "all": it will open
7461 * in a moment. */
7462 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7463 (void)hasFolding(lnum, &lnum, NULL);
7464 }
7465 if (lnum < 1)
7466 lnum = 1;
7467 }
7468 else
7469#endif
7470 lnum -= n;
7471 curwin->w_cursor.lnum = lnum;
7472 }
7473
7474 /* try to advance to the column we want to be at */
7475 coladvance(curwin->w_curswant);
7476
7477 if (upd_topline)
7478 update_topline(); /* make sure curwin->w_topline is valid */
7479
7480 return OK;
7481}
7482
7483/*
7484 * Cursor down a number of logical lines.
7485 */
7486 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007487cursor_down(
7488 long n,
7489 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490{
7491 linenr_T lnum;
7492
7493 if (n > 0)
7494 {
7495 lnum = curwin->w_cursor.lnum;
7496#ifdef FEAT_FOLDING
7497 /* Move to last line of fold, will fail if it's the end-of-file. */
7498 (void)hasFolding(lnum, NULL, &lnum);
7499#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007500 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007501 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007502 if (lnum >= curbuf->b_ml.ml_line_count
7503 || (lnum + n > curbuf->b_ml.ml_line_count
7504 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 return FAIL;
7506 if (lnum + n >= curbuf->b_ml.ml_line_count)
7507 lnum = curbuf->b_ml.ml_line_count;
7508 else
7509#ifdef FEAT_FOLDING
7510 if (hasAnyFolding(curwin))
7511 {
7512 linenr_T last;
7513
7514 /* count each sequence of folded lines as one logical line */
7515 while (n--)
7516 {
7517 if (hasFolding(lnum, NULL, &last))
7518 lnum = last + 1;
7519 else
7520 ++lnum;
7521 if (lnum >= curbuf->b_ml.ml_line_count)
7522 break;
7523 }
7524 if (lnum > curbuf->b_ml.ml_line_count)
7525 lnum = curbuf->b_ml.ml_line_count;
7526 }
7527 else
7528#endif
7529 lnum += n;
7530 curwin->w_cursor.lnum = lnum;
7531 }
7532
7533 /* try to advance to the column we want to be at */
7534 coladvance(curwin->w_curswant);
7535
7536 if (upd_topline)
7537 update_topline(); /* make sure curwin->w_topline is valid */
7538
7539 return OK;
7540}
7541
7542/*
7543 * Stuff the last inserted text in the read buffer.
7544 * Last_insert actually is a copy of the redo buffer, so we
7545 * first have to remove the command.
7546 */
7547 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007548stuff_inserted(
7549 int c, /* Command character to be inserted */
7550 long count, /* Repeat this many times */
7551 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
7553 char_u *esc_ptr;
7554 char_u *ptr;
7555 char_u *last_ptr;
7556 char_u last = NUL;
7557
7558 ptr = get_last_insert();
7559 if (ptr == NULL)
7560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007561 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 return FAIL;
7563 }
7564
7565 /* may want to stuff the command character, to start Insert mode */
7566 if (c != NUL)
7567 stuffcharReadbuff(c);
7568 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7569 *esc_ptr = NUL; /* remove the ESC */
7570
7571 /* when the last char is either "0" or "^" it will be quoted if no ESC
7572 * comes after it OR if it will inserted more than once and "ptr"
7573 * starts with ^D. -- Acevedo
7574 */
7575 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7576 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7577 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7578 {
7579 last = *last_ptr;
7580 *last_ptr = NUL;
7581 }
7582
7583 do
7584 {
7585 stuffReadbuff(ptr);
7586 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7587 if (last)
7588 stuffReadbuff((char_u *)(last == '0'
7589 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7590 : IF_EB("\026^", CTRL_V_STR "^")));
7591 }
7592 while (--count > 0);
7593
7594 if (last)
7595 *last_ptr = last;
7596
7597 if (esc_ptr != NULL)
7598 *esc_ptr = ESC; /* put the ESC back */
7599
7600 /* may want to stuff a trailing ESC, to get out of Insert mode */
7601 if (!no_esc)
7602 stuffcharReadbuff(ESC);
7603
7604 return OK;
7605}
7606
7607 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007608get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610 if (last_insert == NULL)
7611 return NULL;
7612 return last_insert + last_insert_skip;
7613}
7614
7615/*
7616 * Get last inserted string, and remove trailing <Esc>.
7617 * Returns pointer to allocated memory (must be freed) or NULL.
7618 */
7619 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007620get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621{
7622 char_u *s;
7623 int len;
7624
7625 if (last_insert == NULL)
7626 return NULL;
7627 s = vim_strsave(last_insert + last_insert_skip);
7628 if (s != NULL)
7629 {
7630 len = (int)STRLEN(s);
7631 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7632 s[len - 1] = NUL;
7633 }
7634 return s;
7635}
7636
7637/*
7638 * Check the word in front of the cursor for an abbreviation.
7639 * Called when the non-id character "c" has been entered.
7640 * When an abbreviation is recognized it is removed from the text and
7641 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7642 */
7643 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007644echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
7646 /* Don't check for abbreviation in paste mode, when disabled and just
7647 * after moving around with cursor keys. */
7648 if (p_paste || no_abbr || arrow_used)
7649 return FALSE;
7650
7651 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7652 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7653}
7654
7655/*
7656 * replace-stack functions
7657 *
7658 * When replacing characters, the replaced characters are remembered for each
7659 * new character. This is used to re-insert the old text when backspacing.
7660 *
7661 * There is a NUL headed list of characters for each character that is
7662 * currently in the file after the insertion point. When BS is used, one NUL
7663 * headed list is put back for the deleted character.
7664 *
7665 * For a newline, there are two NUL headed lists. One contains the characters
7666 * that the NL replaced. The extra one stores the characters after the cursor
7667 * that were deleted (always white space).
7668 *
7669 * Replace_offset is normally 0, in which case replace_push will add a new
7670 * character at the end of the stack. If replace_offset is not 0, that many
7671 * characters will be left on the stack above the newly inserted character.
7672 */
7673
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007674static char_u *replace_stack = NULL;
7675static long replace_stack_nr = 0; /* next entry in replace stack */
7676static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007679replace_push(
7680 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
7682 char_u *p;
7683
7684 if (replace_stack_nr < replace_offset) /* nothing to do */
7685 return;
7686 if (replace_stack_len <= replace_stack_nr)
7687 {
7688 replace_stack_len += 50;
7689 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7690 if (p == NULL) /* out of memory */
7691 {
7692 replace_stack_len -= 50;
7693 return;
7694 }
7695 if (replace_stack != NULL)
7696 {
7697 mch_memmove(p, replace_stack,
7698 (size_t)(replace_stack_nr * sizeof(char_u)));
7699 vim_free(replace_stack);
7700 }
7701 replace_stack = p;
7702 }
7703 p = replace_stack + replace_stack_nr - replace_offset;
7704 if (replace_offset)
7705 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7706 *p = c;
7707 ++replace_stack_nr;
7708}
7709
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007710/*
7711 * Push a character onto the replace stack. Handles a multi-byte character in
7712 * reverse byte order, so that the first byte is popped off first.
7713 * Return the number of bytes done (includes composing characters).
7714 */
7715 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007716replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007717{
7718 int l = (*mb_ptr2len)(p);
7719 int j;
7720
7721 for (j = l - 1; j >= 0; --j)
7722 replace_push(p[j]);
7723 return l;
7724}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007725
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726/*
7727 * Pop one item from the replace stack.
7728 * return -1 if stack empty
7729 * return replaced character or NUL otherwise
7730 */
7731 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007732replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734 if (replace_stack_nr == 0)
7735 return -1;
7736 return (int)replace_stack[--replace_stack_nr];
7737}
7738
7739/*
7740 * Join the top two items on the replace stack. This removes to "off"'th NUL
7741 * encountered.
7742 */
7743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007744replace_join(
7745 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
7747 int i;
7748
7749 for (i = replace_stack_nr; --i >= 0; )
7750 if (replace_stack[i] == NUL && off-- <= 0)
7751 {
7752 --replace_stack_nr;
7753 mch_memmove(replace_stack + i, replace_stack + i + 1,
7754 (size_t)(replace_stack_nr - i));
7755 return;
7756 }
7757}
7758
7759/*
7760 * Pop bytes from the replace stack until a NUL is found, and insert them
7761 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7762 */
7763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007764replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
7766 int cc;
7767 int oldState = State;
7768
7769 State = NORMAL; /* don't want REPLACE here */
7770 while ((cc = replace_pop()) > 0)
7771 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 dec_cursor();
7774 }
7775 State = oldState;
7776}
7777
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778/*
7779 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7780 * indicates a multi-byte char, pop the other bytes too.
7781 */
7782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007783mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007786 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 int i;
7788 int c;
7789
7790 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7791 {
7792 buf[0] = cc;
7793 for (i = 1; i < n; ++i)
7794 buf[i] = replace_pop();
7795 ins_bytes_len(buf, n);
7796 }
7797 else
7798 ins_char(cc);
7799
7800 if (enc_utf8)
7801 /* Handle composing chars. */
7802 for (;;)
7803 {
7804 c = replace_pop();
7805 if (c == -1) /* stack empty */
7806 break;
7807 if ((n = MB_BYTE2LEN(c)) == 1)
7808 {
7809 /* Not a multi-byte char, put it back. */
7810 replace_push(c);
7811 break;
7812 }
7813 else
7814 {
7815 buf[0] = c;
7816 for (i = 1; i < n; ++i)
7817 buf[i] = replace_pop();
7818 if (utf_iscomposing(utf_ptr2char(buf)))
7819 ins_bytes_len(buf, n);
7820 else
7821 {
7822 /* Not a composing char, put it back. */
7823 for (i = n - 1; i >= 0; --i)
7824 replace_push(buf[i]);
7825 break;
7826 }
7827 }
7828 }
7829}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830
7831/*
7832 * make the replace stack empty
7833 * (called when exiting replace mode)
7834 */
7835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007836replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007838 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 replace_stack_len = 0;
7840 replace_stack_nr = 0;
7841}
7842
7843/*
7844 * Handle doing a BS for one character.
7845 * cc < 0: replace stack empty, just move cursor
7846 * cc == 0: character was inserted, delete it
7847 * cc > 0: character was replaced, put cc (first byte of original char) back
7848 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007849 * When "limit_col" is >= 0, don't delete before this column. Matters when
7850 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 */
7852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007853replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854{
7855 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 int orig_len = 0;
7857 int ins_len;
7858 int orig_vcols = 0;
7859 colnr_T start_vcol;
7860 char_u *p;
7861 int i;
7862 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863
7864 cc = replace_pop();
7865 if (cc > 0)
7866 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01007867#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01007868 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01007869
7870 if (curbuf->b_has_textprop)
7871 {
7872 // Do not adjust text properties for individual delete and insert
7873 // operations, do it afterwards on the resulting text.
7874 len_before = STRLEN(ml_get_curline());
7875 ++text_prop_frozen;
7876 }
7877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 if (State & VREPLACE_FLAG)
7879 {
7880 /* Get the number of screen cells used by the character we are
7881 * going to delete. */
7882 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7883 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 if (has_mbyte)
7886 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007887 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007889 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 replace_push(cc);
7891 }
7892 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {
7894 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007896 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 }
7898 replace_pop_ins();
7899
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 if (State & VREPLACE_FLAG)
7901 {
7902 /* Get the number of screen cells used by the inserted characters */
7903 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007904 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 vcol = start_vcol;
7906 for (i = 0; i < ins_len; ++i)
7907 {
7908 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007909 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 }
7911 vcol -= start_vcol;
7912
7913 /* Delete spaces that were inserted after the cursor to keep the
7914 * text aligned. */
7915 curwin->w_cursor.col += ins_len;
7916 while (vcol > orig_vcols && gchar_cursor() == ' ')
7917 {
7918 del_char(FALSE);
7919 ++orig_vcols;
7920 }
7921 curwin->w_cursor.col -= ins_len;
7922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
Bram Moolenaar196d1572019-01-02 23:47:18 +01007924 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01007926
7927#ifdef FEAT_TEXT_PROP
7928 if (curbuf->b_has_textprop)
7929 {
7930 size_t len_now = STRLEN(ml_get_curline());
7931
7932 --text_prop_frozen;
7933 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
7934 (int)(len_now - len_before));
7935 }
7936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 }
7938 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007939 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940}
7941
7942#ifdef FEAT_CINDENT
7943/*
7944 * Return TRUE if C-indenting is on.
7945 */
7946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007947cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948{
7949 return (!p_paste && (curbuf->b_p_cin
7950# ifdef FEAT_EVAL
7951 || *curbuf->b_p_inde != NUL
7952# endif
7953 ));
7954}
7955#endif
7956
7957#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7958/*
7959 * Re-indent the current line, based on the current contents of it and the
7960 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7961 * confused what all the part that handles Control-T is doing that I'm not.
7962 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7963 */
7964
7965 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007966fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007968 int amount = get_the_indent();
7969
7970 if (amount >= 0)
7971 {
7972 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7973 if (linewhite(curwin->w_cursor.lnum))
7974 did_ai = TRUE; /* delete the indent if the line stays empty */
7975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976}
7977
7978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007979fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
7981 if (p_paste)
7982 return;
7983# ifdef FEAT_LISP
7984 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7985 fixthisline(get_lisp_indent);
7986# endif
7987# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7988 else
7989# endif
7990# ifdef FEAT_CINDENT
7991 if (cindent_on())
7992 do_c_expr_indent();
7993# endif
7994}
7995
7996#endif
7997
7998#ifdef FEAT_CINDENT
7999/*
8000 * return TRUE if 'cinkeys' contains the key "keytyped",
8001 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008002 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8004 *
8005 * "keytyped" can have a few special values:
8006 * KEY_OPEN_FORW
8007 * KEY_OPEN_BACK
8008 * KEY_COMPLETE just finished completion.
8009 *
8010 * If line_is_empty is TRUE accept keys with '0' before them.
8011 */
8012 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008013in_cinkeys(
8014 int keytyped,
8015 int when,
8016 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017{
8018 char_u *look;
8019 int try_match;
8020 int try_match_word;
8021 char_u *p;
8022 char_u *line;
8023 int icase;
8024 int i;
8025
Bram Moolenaar30848942009-12-24 14:46:12 +00008026 if (keytyped == NUL)
8027 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8028 return FALSE;
8029
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030#ifdef FEAT_EVAL
8031 if (*curbuf->b_p_inde != NUL)
8032 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8033 else
8034#endif
8035 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8036 while (*look)
8037 {
8038 /*
8039 * Find out if we want to try a match with this key, depending on
8040 * 'when' and a '*' or '!' before the key.
8041 */
8042 switch (when)
8043 {
8044 case '*': try_match = (*look == '*'); break;
8045 case '!': try_match = (*look == '!'); break;
8046 default: try_match = (*look != '*'); break;
8047 }
8048 if (*look == '*' || *look == '!')
8049 ++look;
8050
8051 /*
8052 * If there is a '0', only accept a match if the line is empty.
8053 * But may still match when typing last char of a word.
8054 */
8055 if (*look == '0')
8056 {
8057 try_match_word = try_match;
8058 if (!line_is_empty)
8059 try_match = FALSE;
8060 ++look;
8061 }
8062 else
8063 try_match_word = FALSE;
8064
8065 /*
8066 * does it look like a control character?
8067 */
8068 if (*look == '^'
8069#ifdef EBCDIC
8070 && (Ctrl_chr(look[1]) != 0)
8071#else
8072 && look[1] >= '?' && look[1] <= '_'
8073#endif
8074 )
8075 {
8076 if (try_match && keytyped == Ctrl_chr(look[1]))
8077 return TRUE;
8078 look += 2;
8079 }
8080 /*
8081 * 'o' means "o" command, open forward.
8082 * 'O' means "O" command, open backward.
8083 */
8084 else if (*look == 'o')
8085 {
8086 if (try_match && keytyped == KEY_OPEN_FORW)
8087 return TRUE;
8088 ++look;
8089 }
8090 else if (*look == 'O')
8091 {
8092 if (try_match && keytyped == KEY_OPEN_BACK)
8093 return TRUE;
8094 ++look;
8095 }
8096
8097 /*
8098 * 'e' means to check for "else" at start of line and just before the
8099 * cursor.
8100 */
8101 else if (*look == 'e')
8102 {
8103 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8104 {
8105 p = ml_get_curline();
8106 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8107 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8108 return TRUE;
8109 }
8110 ++look;
8111 }
8112
8113 /*
8114 * ':' only causes an indent if it is at the end of a label or case
8115 * statement, or when it was before typing the ':' (to fix
8116 * class::method for C++).
8117 */
8118 else if (*look == ':')
8119 {
8120 if (try_match && keytyped == ':')
8121 {
8122 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008123 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008125 /* Need to get the line again after cin_islabel(). */
8126 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 if (curwin->w_cursor.col > 2
8128 && p[curwin->w_cursor.col - 1] == ':'
8129 && p[curwin->w_cursor.col - 2] == ':')
8130 {
8131 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008132 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008133 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 p = ml_get_curline();
8135 p[curwin->w_cursor.col - 1] = ':';
8136 if (i)
8137 return TRUE;
8138 }
8139 }
8140 ++look;
8141 }
8142
8143
8144 /*
8145 * Is it a key in <>, maybe?
8146 */
8147 else if (*look == '<')
8148 {
8149 if (try_match)
8150 {
8151 /*
8152 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8153 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8154 * >, *, : and ! keys if they really really want to.
8155 */
8156 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8157 && keytyped == look[1])
8158 return TRUE;
8159
8160 if (keytyped == get_special_key_code(look + 1))
8161 return TRUE;
8162 }
8163 while (*look && *look != '>')
8164 look++;
8165 while (*look == '>')
8166 look++;
8167 }
8168
8169 /*
8170 * Is it a word: "=word"?
8171 */
8172 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8173 {
8174 ++look;
8175 if (*look == '~')
8176 {
8177 icase = TRUE;
8178 ++look;
8179 }
8180 else
8181 icase = FALSE;
8182 p = vim_strchr(look, ',');
8183 if (p == NULL)
8184 p = look + STRLEN(look);
8185 if ((try_match || try_match_word)
8186 && curwin->w_cursor.col >= (colnr_T)(p - look))
8187 {
8188 int match = FALSE;
8189
8190#ifdef FEAT_INS_EXPAND
8191 if (keytyped == KEY_COMPLETE)
8192 {
8193 char_u *s;
8194
8195 /* Just completed a word, check if it starts with "look".
8196 * search back for the start of a word. */
8197 line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 if (has_mbyte)
8199 {
8200 char_u *n;
8201
8202 for (s = line + curwin->w_cursor.col; s > line; s = n)
8203 {
8204 n = mb_prevptr(line, s);
8205 if (!vim_iswordp(n))
8206 break;
8207 }
8208 }
8209 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 for (s = line + curwin->w_cursor.col; s > line; --s)
8211 if (!vim_iswordc(s[-1]))
8212 break;
8213 if (s + (p - look) <= line + curwin->w_cursor.col
8214 && (icase
8215 ? MB_STRNICMP(s, look, p - look)
8216 : STRNCMP(s, look, p - look)) == 0)
8217 match = TRUE;
8218 }
8219 else
8220#endif
8221 /* TODO: multi-byte */
8222 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8223 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8224 {
8225 line = ml_get_cursor();
8226 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8227 || !vim_iswordc(line[-(p - look) - 1]))
8228 && (icase
8229 ? MB_STRNICMP(line - (p - look), look, p - look)
8230 : STRNCMP(line - (p - look), look, p - look))
8231 == 0)
8232 match = TRUE;
8233 }
8234 if (match && try_match_word && !try_match)
8235 {
8236 /* "0=word": Check if there are only blanks before the
8237 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008238 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 (int)(curwin->w_cursor.col - (p - look)))
8240 match = FALSE;
8241 }
8242 if (match)
8243 return TRUE;
8244 }
8245 look = p;
8246 }
8247
8248 /*
8249 * ok, it's a boring generic character.
8250 */
8251 else
8252 {
8253 if (try_match && *look == keytyped)
8254 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008255 if (*look != NUL)
8256 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 }
8258
8259 /*
8260 * Skip over ", ".
8261 */
8262 look = skip_to_option_part(look);
8263 }
8264 return FALSE;
8265}
8266#endif /* FEAT_CINDENT */
8267
8268#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8269/*
8270 * Map Hebrew keyboard when in hkmap mode.
8271 */
8272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008273hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
8275 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8276 {
8277 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8278 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8279 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8280 static char_u map[26] =
8281 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8282 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8283 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8284 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8285 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8286 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8287 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8288 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8289 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8290
8291 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8292 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8293 /* '-1'='sofit' */
8294 else if (c == 'x')
8295 return 'X';
8296 else if (c == 'q')
8297 return '\''; /* {geresh}={'} */
8298 else if (c == 246)
8299 return ' '; /* \"o --> ' ' for a german keyboard */
8300 else if (c == 228)
8301 return ' '; /* \"a --> ' ' -- / -- */
8302 else if (c == 252)
8303 return ' '; /* \"u --> ' ' -- / -- */
8304#ifdef EBCDIC
8305 else if (islower(c))
8306#else
8307 /* NOTE: islower() does not do the right thing for us on Linux so we
8308 * do this the same was as 5.7 and previous, so it works correctly on
8309 * all systems. Specifically, the e.g. Delete and Arrow keys are
8310 * munged and won't work if e.g. searching for Hebrew text.
8311 */
8312 else if (c >= 'a' && c <= 'z')
8313#endif
8314 return (int)(map[CharOrdLow(c)] + p_aleph);
8315 else
8316 return c;
8317 }
8318 else
8319 {
8320 switch (c)
8321 {
8322 case '`': return ';';
8323 case '/': return '.';
8324 case '\'': return ',';
8325 case 'q': return '/';
8326 case 'w': return '\'';
8327
8328 /* Hebrew letters - set offset from 'a' */
8329 case ',': c = '{'; break;
8330 case '.': c = 'v'; break;
8331 case ';': c = 't'; break;
8332 default: {
8333 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8334
8335#ifdef EBCDIC
8336 /* see note about islower() above */
8337 if (!islower(c))
8338#else
8339 if (c < 'a' || c > 'z')
8340#endif
8341 return c;
8342 c = str[CharOrdLow(c)];
8343 break;
8344 }
8345 }
8346
8347 return (int)(CharOrdLow(c) + p_aleph);
8348 }
8349}
8350#endif
8351
8352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008353ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354{
8355 int need_redraw = FALSE;
8356 int regname;
8357 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008358 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359
8360 /*
8361 * If we are going to wait for a character, show a '"'.
8362 */
8363 pc_status = PC_STATUS_UNSET;
8364 if (redrawing() && !char_avail())
8365 {
8366 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008367 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368
8369 edit_putchar('"', TRUE);
8370#ifdef FEAT_CMDL_INFO
8371 add_to_showcmd_c(Ctrl_R);
8372#endif
8373 }
8374
8375#ifdef USE_ON_FLY_SCROLL
8376 dont_scroll = TRUE; /* disallow scrolling here */
8377#endif
8378
8379 /*
8380 * Don't map the register name. This also prevents the mode message to be
8381 * deleted when ESC is hit.
8382 */
8383 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008384 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8387 {
8388 /* Get a third key for literal register insertion */
8389 literally = regname;
8390#ifdef FEAT_CMDL_INFO
8391 add_to_showcmd_c(literally);
8392#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008393 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 }
8396 --no_mapping;
8397
8398#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008399 /* Don't call u_sync() while typing the expression or giving an error
8400 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 ++no_u_sync;
8402 if (regname == '=')
8403 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008404 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008405# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008407# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008408 /* Sync undo when evaluating the expression calls setline() or
8409 * append(), so that it can be undone separately. */
8410 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008411
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008413
8414 // Cursor may be moved back a column.
8415 curwin->w_cursor = curpos;
8416 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008417# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008418 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 if (im_on)
8420 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008423 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8424 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008425 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 else
8429 {
8430#endif
8431 if (literally == Ctrl_O || literally == Ctrl_P)
8432 {
8433 /* Append the command to the redo buffer. */
8434 AppendCharToRedobuff(Ctrl_R);
8435 AppendCharToRedobuff(literally);
8436 AppendCharToRedobuff(regname);
8437
8438 do_put(regname, BACKWARD, 1L,
8439 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8440 }
8441 else if (insert_reg(regname, literally) == FAIL)
8442 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008443 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 need_redraw = TRUE; /* remove the '"' */
8445 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008446 else if (stop_insert_mode)
8447 /* When the '=' register was used and a function was invoked that
8448 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8449 * insert anything, need to remove the '"' */
8450 need_redraw = TRUE;
8451
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452#ifdef FEAT_EVAL
8453 }
8454 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008455 if (u_sync_once == 1)
8456 ins_need_undo = TRUE;
8457 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458#endif
8459#ifdef FEAT_CMDL_INFO
8460 clear_showcmd();
8461#endif
8462
8463 /* If the inserted register is empty, we need to remove the '"' */
8464 if (need_redraw || stuff_empty())
8465 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008466
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008467 /* Disallow starting Visual mode here, would get a weird mode. */
8468 if (!vis_active && VIsual_active)
8469 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470}
8471
8472/*
8473 * CTRL-G commands in Insert mode.
8474 */
8475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008476ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
8478 int c;
8479
8480#ifdef FEAT_INS_EXPAND
8481 /* Right after CTRL-X the cursor will be after the ruler. */
8482 setcursor();
8483#endif
8484
8485 /*
8486 * Don't map the second key. This also prevents the mode message to be
8487 * deleted when ESC is hit.
8488 */
8489 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008490 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 --no_mapping;
8492 switch (c)
8493 {
8494 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8495 case K_UP:
8496 case Ctrl_K:
8497 case 'k': ins_up(TRUE);
8498 break;
8499
8500 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8501 case K_DOWN:
8502 case Ctrl_J:
8503 case 'j': ins_down(TRUE);
8504 break;
8505
8506 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008507 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008509
8510 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008511 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008512 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008513 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 break;
8515
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008516 /* CTRL-G U: do not break undo with the next char */
8517 case 'U':
8518 /* Allow one left/right cursor movement with the next char,
8519 * without breaking undo. */
8520 dont_sync_undo = MAYBE;
8521 break;
8522
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008524 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 }
8526}
8527
8528/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008529 * CTRL-^ in Insert mode.
8530 */
8531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008532ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008533{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008534 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008535 {
8536 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8537 if (State & LANGMAP)
8538 {
8539 curbuf->b_p_iminsert = B_IMODE_NONE;
8540 State &= ~LANGMAP;
8541 }
8542 else
8543 {
8544 curbuf->b_p_iminsert = B_IMODE_LMAP;
8545 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008546#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008547 im_set_active(FALSE);
8548#endif
8549 }
8550 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008551#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008552 else
8553 {
8554 /* There are no ":lmap" mappings, toggle IM */
8555 if (im_get_status())
8556 {
8557 curbuf->b_p_iminsert = B_IMODE_NONE;
8558 im_set_active(FALSE);
8559 }
8560 else
8561 {
8562 curbuf->b_p_iminsert = B_IMODE_IM;
8563 State &= ~LANGMAP;
8564 im_set_active(TRUE);
8565 }
8566 }
8567#endif
8568 set_iminsert_global();
8569 showmode();
8570#ifdef FEAT_GUI
8571 /* may show different cursor shape or color */
8572 if (gui.in_use)
8573 gui_update_cursor(TRUE, FALSE);
8574#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008575#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008576 /* Show/unshow value of 'keymap' in status lines. */
8577 status_redraw_curbuf();
8578#endif
8579}
8580
8581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 * Handle ESC in insert mode.
8583 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8584 * insert.
8585 */
8586 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008587ins_esc(
8588 long *count,
8589 int cmdchar,
8590 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591{
8592 int temp;
8593 static int disabled_redraw = FALSE;
8594
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008595#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008596 check_spell_redraw();
8597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598#if defined(FEAT_HANGULIN)
8599# if defined(ESC_CHG_TO_ENG_MODE)
8600 hangul_input_state_set(0);
8601# endif
8602 if (composing_hangul)
8603 {
8604 push_raw_key(composing_hangul_buffer, 2);
8605 composing_hangul = 0;
8606 }
8607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
8609 temp = curwin->w_cursor.col;
8610 if (disabled_redraw)
8611 {
8612 --RedrawingDisabled;
8613 disabled_redraw = FALSE;
8614 }
8615 if (!arrow_used)
8616 {
8617 /*
8618 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008619 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8620 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 */
8622 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008623 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624
8625 /*
8626 * Repeating insert may take a long time. Check for
8627 * interrupt now and then.
8628 */
8629 if (*count > 0)
8630 {
8631 line_breakcheck();
8632 if (got_int)
8633 *count = 0;
8634 }
8635
8636 if (--*count > 0) /* repeat what was typed */
8637 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008638 /* Vi repeats the insert without replacing characters. */
8639 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8640 State &= ~REPLACE_FLAG;
8641
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 (void)start_redo_ins();
8643 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008644 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 ++RedrawingDisabled;
8646 disabled_redraw = TRUE;
8647 return FALSE; /* repeat the insert */
8648 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008649 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 undisplay_dollar();
8651 }
8652
8653 /* When an autoindent was removed, curswant stays after the
8654 * indent */
8655 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8656 curwin->w_set_curswant = TRUE;
8657
8658 /* Remember the last Insert position in the '^ mark. */
8659 if (!cmdmod.keepjumps)
8660 curbuf->b_last_insert = curwin->w_cursor;
8661
8662 /*
8663 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008664 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008666 if (!nomove
8667 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668#ifdef FEAT_VIRTUALEDIT
8669 || curwin->w_cursor.coladd > 0
8670#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008671 )
8672 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008673 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674#ifdef FEAT_RIGHTLEFT
8675 && !revins_on
8676#endif
8677 )
8678 {
8679#ifdef FEAT_VIRTUALEDIT
8680 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8681 {
8682 oneleft();
8683 if (restart_edit != NUL)
8684 ++curwin->w_cursor.coladd;
8685 }
8686 else
8687#endif
8688 {
8689 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 /* Correct cursor for multi-byte character. */
8691 if (has_mbyte)
8692 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 }
8694 }
8695
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008696#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 /* Disable IM to allow typing English directly for Normal mode commands.
8698 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8699 * well). */
8700 if (!(State & LANGMAP))
8701 im_save_status(&curbuf->b_p_iminsert);
8702 im_set_active(FALSE);
8703#endif
8704
8705 State = NORMAL;
8706 /* need to position cursor again (e.g. when on a TAB ) */
8707 changed_cline_bef_curs();
8708
8709#ifdef FEAT_MOUSE
8710 setmouse();
8711#endif
8712#ifdef CURSOR_SHAPE
8713 ui_cursor_shape(); /* may show different cursor shape */
8714#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008715 if (!p_ek)
8716 /* Re-enable bracketed paste mode. */
8717 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718
8719 /*
8720 * When recording or for CTRL-O, need to display the new mode.
8721 * Otherwise remove the mode message.
8722 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008723 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 showmode();
8725 else if (p_smd)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008726 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
8728 return TRUE; /* exit Insert mode */
8729}
8730
8731#ifdef FEAT_RIGHTLEFT
8732/*
8733 * Toggle language: hkmap and revins_on.
8734 * Move to end of reverse inserted text.
8735 */
8736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008737ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738{
8739 if (revins_on && revins_chars && revins_scol >= 0)
8740 {
8741 while (gchar_cursor() != NUL && revins_chars--)
8742 ++curwin->w_cursor.col;
8743 }
8744 p_ri = !p_ri;
8745 revins_on = (State == INSERT && p_ri);
8746 if (revins_on)
8747 {
8748 revins_scol = curwin->w_cursor.col;
8749 revins_legal++;
8750 revins_chars = 0;
8751 undisplay_dollar();
8752 }
8753 else
8754 revins_scol = -1;
8755#ifdef FEAT_FKMAP
8756 if (p_altkeymap)
8757 {
8758 /*
8759 * to be consistent also for redo command, using '.'
8760 * set arrow_used to true and stop it - causing to redo
8761 * characters entered in one mode (normal/reverse insert).
8762 */
8763 arrow_used = TRUE;
8764 (void)stop_arrow();
8765 p_fkmap = curwin->w_p_rl ^ p_ri;
8766 if (p_fkmap && p_ri)
8767 State = INSERT;
8768 }
8769 else
8770#endif
8771 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8772 showmode();
8773}
8774#endif
8775
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776/*
8777 * If 'keymodel' contains "startsel", may start selection.
8778 * Returns TRUE when a CTRL-O and other keys stuffed.
8779 */
8780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008781ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
8783 if (km_startsel)
8784 switch (c)
8785 {
8786 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 case K_PAGEUP:
8789 case K_KPAGEUP:
8790 case K_PAGEDOWN:
8791 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008792# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 case K_LEFT:
8794 case K_RIGHT:
8795 case K_UP:
8796 case K_DOWN:
8797 case K_END:
8798 case K_HOME:
8799# endif
8800 if (!(mod_mask & MOD_MASK_SHIFT))
8801 break;
8802 /* FALLTHROUGH */
8803 case K_S_LEFT:
8804 case K_S_RIGHT:
8805 case K_S_UP:
8806 case K_S_DOWN:
8807 case K_S_END:
8808 case K_S_HOME:
8809 /* Start selection right away, the cursor can move with
8810 * CTRL-O when beyond the end of the line. */
8811 start_selection();
8812
8813 /* Execute the key in (insert) Select mode. */
8814 stuffcharReadbuff(Ctrl_O);
8815 if (mod_mask)
8816 {
8817 char_u buf[4];
8818
8819 buf[0] = K_SPECIAL;
8820 buf[1] = KS_MODIFIER;
8821 buf[2] = mod_mask;
8822 buf[3] = NUL;
8823 stuffReadbuff(buf);
8824 }
8825 stuffcharReadbuff(c);
8826 return TRUE;
8827 }
8828 return FALSE;
8829}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830
8831/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008832 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008833 */
8834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008835ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008836{
8837#ifdef FEAT_FKMAP
8838 if (p_fkmap && p_ri)
8839 {
8840 beep_flush();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008841 emsg(farsi_text_3); /* encoded in Farsi */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008842 return;
8843 }
8844#endif
8845
Bram Moolenaar1e015462005-09-25 22:16:38 +00008846# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008847 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008848 (char_u *)((State & REPLACE_FLAG) ? "i"
8849 : replaceState == VREPLACE ? "v"
8850 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008851# endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02008852 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008853 if (State & REPLACE_FLAG)
8854 State = INSERT | (State & LANGMAP);
8855 else
8856 State = replaceState | (State & LANGMAP);
8857 AppendCharToRedobuff(K_INS);
8858 showmode();
8859#ifdef CURSOR_SHAPE
8860 ui_cursor_shape(); /* may show different cursor shape */
8861#endif
8862}
8863
8864/*
8865 * Pressed CTRL-O in Insert mode.
8866 */
8867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008868ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008869{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008870 if (State & VREPLACE_FLAG)
8871 restart_edit = 'V';
8872 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008873 if (State & REPLACE_FLAG)
8874 restart_edit = 'R';
8875 else
8876 restart_edit = 'I';
8877#ifdef FEAT_VIRTUALEDIT
8878 if (virtual_active())
8879 ins_at_eol = FALSE; /* cursor always keeps its column */
8880 else
8881#endif
8882 ins_at_eol = (gchar_cursor() == NUL);
8883}
8884
8885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 * If the cursor is on an indent, ^T/^D insert/delete one
8887 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008888 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 * with vi. But vi only supports ^T and ^D after an
8890 * autoindent, we support it everywhere.
8891 */
8892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008893ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894{
8895 if (stop_arrow() == FAIL)
8896 return;
8897 AppendCharToRedobuff(c);
8898
8899 /*
8900 * 0^D and ^^D: remove all indent.
8901 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008902 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8903 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 {
8905 --curwin->w_cursor.col;
8906 (void)del_char(FALSE); /* delete the '^' or '0' */
8907 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8908 if (State & REPLACE_FLAG)
8909 replace_pop_ins();
8910 if (lastc == '^')
8911 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008912 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 }
8914 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008915 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
8917 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8918 did_ai = FALSE;
8919#ifdef FEAT_SMARTINDENT
8920 did_si = FALSE;
8921 can_si = FALSE;
8922 can_si_back = FALSE;
8923#endif
8924#ifdef FEAT_CINDENT
8925 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8926#endif
8927}
8928
8929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008930ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
8932 int temp;
8933
8934 if (stop_arrow() == FAIL)
8935 return;
8936 if (gchar_cursor() == NUL) /* delete newline */
8937 {
8938 temp = curwin->w_cursor.col;
8939 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008940 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008941 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008943 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008945 /* Adjust orig_line_count in case more lines have been deleted than
8946 * have been added. That makes sure, that open_line() later
8947 * can access all buffer lines correctly */
8948 if (State & VREPLACE_FLAG &&
8949 orig_line_count > curbuf->b_ml.ml_line_count)
8950 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008953 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8954 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 did_ai = FALSE;
8956#ifdef FEAT_SMARTINDENT
8957 did_si = FALSE;
8958 can_si = FALSE;
8959 can_si_back = FALSE;
8960#endif
8961 AppendCharToRedobuff(K_DEL);
8962}
8963
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008964/*
8965 * Delete one character for ins_bs().
8966 */
8967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008968ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008969{
8970 dec_cursor();
8971 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8972 if (State & REPLACE_FLAG)
8973 {
8974 /* Don't delete characters before the insert point when in
8975 * Replace mode */
8976 if (curwin->w_cursor.lnum != Insstart.lnum
8977 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008978 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008979 }
8980 else
8981 (void)del_char(FALSE);
8982}
8983
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984/*
8985 * Handle Backspace, delete-word and delete-line in Insert mode.
8986 * Return TRUE when backspace was actually used.
8987 */
8988 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008989ins_bs(
8990 int c,
8991 int mode,
8992 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
8994 linenr_T lnum;
8995 int cc;
8996 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008997 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 colnr_T mincol;
8999 int did_backspace = FALSE;
9000 int in_indent;
9001 int oldState;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009002 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003
9004 /*
9005 * can't delete anything in an empty file
9006 * can't backup past first character in buffer
9007 * can't backup past starting point unless 'backspace' > 1
9008 * can backup to a previous line if 'backspace' == 0
9009 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009010 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 || (
9012#ifdef FEAT_RIGHTLEFT
9013 !revins_on &&
9014#endif
9015 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9016 || (!can_bs(BS_START)
9017 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009018 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9019 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9021 && curwin->w_cursor.col <= ai_col)
9022 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9023 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009024 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 return FALSE;
9026 }
9027
9028 if (stop_arrow() == FAIL)
9029 return FALSE;
9030 in_indent = inindent(0);
9031#ifdef FEAT_CINDENT
9032 if (in_indent)
9033 can_cindent = FALSE;
9034#endif
9035#ifdef FEAT_COMMENTS
9036 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9037#endif
9038#ifdef FEAT_RIGHTLEFT
9039 if (revins_on) /* put cursor after last inserted char */
9040 inc_cursor();
9041#endif
9042
9043#ifdef FEAT_VIRTUALEDIT
9044 /* Virtualedit:
9045 * BACKSPACE_CHAR eats a virtual space
9046 * BACKSPACE_WORD eats all coladd
9047 * BACKSPACE_LINE eats all coladd and keeps going
9048 */
9049 if (curwin->w_cursor.coladd > 0)
9050 {
9051 if (mode == BACKSPACE_CHAR)
9052 {
9053 --curwin->w_cursor.coladd;
9054 return TRUE;
9055 }
9056 if (mode == BACKSPACE_WORD)
9057 {
9058 curwin->w_cursor.coladd = 0;
9059 return TRUE;
9060 }
9061 curwin->w_cursor.coladd = 0;
9062 }
9063#endif
9064
9065 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009066 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 */
9068 if (curwin->w_cursor.col == 0)
9069 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009070 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009071 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072#ifdef FEAT_RIGHTLEFT
9073 || revins_on
9074#endif
9075 )
9076 {
9077 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9078 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9079 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009080 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009081 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 }
9083 /*
9084 * In replace mode:
9085 * cc < 0: NL was inserted, delete it
9086 * cc >= 0: NL was replaced, put original characters back
9087 */
9088 cc = -1;
9089 if (State & REPLACE_FLAG)
9090 cc = replace_pop(); /* returns -1 if NL was inserted */
9091 /*
9092 * In replace mode, in the line we started replacing, we only move the
9093 * cursor.
9094 */
9095 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9096 {
9097 dec_cursor();
9098 }
9099 else
9100 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 if (!(State & VREPLACE_FLAG)
9102 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 {
9104 temp = gchar_cursor(); /* remember current char */
9105 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009106
9107 /* When "aw" is in 'formatoptions' we must delete the space at
9108 * the end of the line, otherwise the line will be broken
9109 * again when auto-formatting. */
9110 if (has_format_option(FO_AUTO)
9111 && has_format_option(FO_WHITE_PAR))
9112 {
9113 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9114 TRUE);
9115 int len;
9116
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009117 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009118 if (len > 0 && ptr[len - 1] == ' ')
9119 ptr[len - 1] = NUL;
9120 }
9121
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009122 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 if (temp == NUL && gchar_cursor() != NUL)
9124 inc_cursor();
9125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 else
9127 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128
9129 /*
9130 * In REPLACE mode we have to put back the text that was replaced
9131 * by the NL. On the replace stack is first a NUL-terminated
9132 * sequence of characters that were deleted and then the
9133 * characters that NL replaced.
9134 */
9135 if (State & REPLACE_FLAG)
9136 {
9137 /*
9138 * Do the next ins_char() in NORMAL state, to
9139 * prevent ins_char() from replacing characters and
9140 * avoiding showmatch().
9141 */
9142 oldState = State;
9143 State = NORMAL;
9144 /*
9145 * restore characters (blanks) deleted after cursor
9146 */
9147 while (cc > 0)
9148 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009149 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009151 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 cc = replace_pop();
9153 }
9154 /* restore the characters that NL replaced */
9155 replace_pop_ins();
9156 State = oldState;
9157 }
9158 }
9159 did_ai = FALSE;
9160 }
9161 else
9162 {
9163 /*
9164 * Delete character(s) before the cursor.
9165 */
9166#ifdef FEAT_RIGHTLEFT
9167 if (revins_on) /* put cursor on last inserted char */
9168 dec_cursor();
9169#endif
9170 mincol = 0;
9171 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009172 if (mode == BACKSPACE_LINE
9173 && (curbuf->b_p_ai
9174#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009175 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009176#endif
9177 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178#ifdef FEAT_RIGHTLEFT
9179 && !revins_on
9180#endif
9181 )
9182 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009183 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009185 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009187 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 }
9189
9190 /*
9191 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9192 */
9193 if ( mode == BACKSPACE_CHAR
9194 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009195 || ((get_sts_value() != 0
9196#ifdef FEAT_VARTABS
9197 || tabstop_count(curbuf->b_p_vsts_array)
9198#endif
9199 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009200 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 && (*(ml_get_cursor() - 1) == TAB
9202 || (*(ml_get_cursor() - 1) == ' '
9203 && (!*inserted_space_p
9204 || arrow_used))))))
9205 {
9206 int ts;
9207 colnr_T vcol;
9208 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009209 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210
9211 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 /* Compute the virtual column where we want to be. Since
9213 * 'showbreak' may get in the way, need to get the last column of
9214 * the previous character. */
9215 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009216 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 dec_cursor();
9218 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9219 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009220#ifdef FEAT_VARTABS
9221 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009222 {
9223 ts = (int)get_sw_value(curbuf);
9224 want_vcol = (want_vcol / ts) * ts;
9225 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009226 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009227 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009228 curbuf->b_p_vsts_array);
9229#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009230 if (p_sta && in_indent)
9231 ts = (int)get_sw_value(curbuf);
9232 else
9233 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
9237 /* delete characters until we are at or before want_vcol */
9238 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009239 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009240 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241
9242 /* insert extra spaces until we are at want_vcol */
9243 while (vcol < want_vcol)
9244 {
9245 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009246 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9247 && curwin->w_cursor.col < Insstart_orig.col)
9248 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 if (State & VREPLACE_FLAG)
9251 ins_char(' ');
9252 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 {
9254 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009255 if ((State & REPLACE_FLAG))
9256 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 }
9258 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9259 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009260
9261 /* If we are now back where we started delete one character. Can
9262 * happen when using 'sts' and 'linebreak'. */
9263 if (vcol >= start_vcol)
9264 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 }
9266
9267 /*
9268 * Delete upto starting point, start of line or previous word.
9269 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009270 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009272 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009274 if (has_mbyte)
9275 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009276 do
9277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009279 if (!revins_on) /* put cursor on char to be deleted */
9280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009282
9283 cc = gchar_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009284 /* look multi-byte character class */
9285 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009287 prev_cclass = cclass;
9288 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009290
9291 /* start of word? */
9292 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9293 {
9294 mode = BACKSPACE_WORD_NOT_SPACE;
9295 temp = vim_iswordc(cc);
9296 }
9297 /* end of word? */
9298 else if (mode == BACKSPACE_WORD_NOT_SPACE
9299 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01009300 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009303 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009305 inc_cursor();
9306#ifdef FEAT_RIGHTLEFT
9307 else if (State & REPLACE_FLAG)
9308 dec_cursor();
9309#endif
9310 break;
9311 }
9312 if (State & REPLACE_FLAG)
9313 replace_do_bs(-1);
9314 else
9315 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009316 if (enc_utf8 && p_deco)
9317 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009318 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009319 /*
9320 * If there are combining characters and 'delcombine' is set
9321 * move the cursor back. Don't back up before the base
9322 * character.
9323 */
9324 if (enc_utf8 && p_deco && cpc[0] != NUL)
9325 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009326#ifdef FEAT_RIGHTLEFT
9327 if (revins_chars)
9328 {
9329 revins_chars--;
9330 revins_legal++;
9331 }
9332 if (revins_on && gchar_cursor() == NUL)
9333 break;
9334#endif
9335 }
9336 /* Just a single backspace?: */
9337 if (mode == BACKSPACE_CHAR)
9338 break;
9339 } while (
9340#ifdef FEAT_RIGHTLEFT
9341 revins_on ||
9342#endif
9343 (curwin->w_cursor.col > mincol
9344 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9345 || curwin->w_cursor.col != Insstart_orig.col)));
9346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 did_backspace = TRUE;
9348 }
9349#ifdef FEAT_SMARTINDENT
9350 did_si = FALSE;
9351 can_si = FALSE;
9352 can_si_back = FALSE;
9353#endif
9354 if (curwin->w_cursor.col <= 1)
9355 did_ai = FALSE;
9356 /*
9357 * It's a little strange to put backspaces into the redo
9358 * buffer, but it makes auto-indent a lot easier to deal
9359 * with.
9360 */
9361 AppendCharToRedobuff(c);
9362
9363 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009364 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009365 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009366 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367
9368 /* vi behaviour: the cursor moves backward but the character that
9369 * was there remains visible
9370 * Vim behaviour: the cursor moves backward and the character that
9371 * was there is erased from the screen.
9372 * We can emulate the vi behaviour by pretending there is a dollar
9373 * displayed even when there isn't.
9374 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009375 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 dollar_vcol = curwin->w_virtcol;
9377
Bram Moolenaarce3be472008-01-14 19:12:28 +00009378#ifdef FEAT_FOLDING
9379 /* When deleting a char the cursor line must never be in a closed fold.
9380 * E.g., when 'foldmethod' is indent and deleting the first non-white
9381 * char before a Tab. */
9382 if (did_backspace)
9383 foldOpenCursor();
9384#endif
9385
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 return did_backspace;
9387}
9388
9389#ifdef FEAT_MOUSE
9390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009391ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392{
9393 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009394 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395
9396# ifdef FEAT_GUI
9397 /* When GUI is active, also move/paste when 'mouse' is empty */
9398 if (!gui.in_use)
9399# endif
9400 if (!mouse_has(MOUSE_INSERT))
9401 return;
9402
9403 undisplay_dollar();
9404 tpos = curwin->w_cursor;
9405 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9406 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009407 win_T *new_curwin = curwin;
9408
9409 if (curwin != old_curwin && win_valid(old_curwin))
9410 {
9411 /* Mouse took us to another window. We need to go back to the
9412 * previous one to stop insert there properly. */
9413 curwin = old_curwin;
9414 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009415#ifdef FEAT_JOB_CHANNEL
9416 if (bt_prompt(curbuf))
9417 // Restart Insert mode when re-entering the prompt buffer.
9418 curbuf->b_prompt_insert = 'A';
9419#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009420 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009421 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009422 if (curwin != new_curwin && win_valid(new_curwin))
9423 {
9424 curwin = new_curwin;
9425 curbuf = curwin->w_buffer;
9426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427# ifdef FEAT_CINDENT
9428 can_cindent = TRUE;
9429# endif
9430 }
9431
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 /* redraw status lines (in case another window became active) */
9433 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434}
9435
9436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009437ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009440 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009441# ifdef FEAT_INS_EXPAND
9442 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443# endif
9444
9445 tpos = curwin->w_cursor;
9446
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009447 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 {
9449 int row, col;
9450
9451 row = mouse_row;
9452 col = mouse_col;
9453
9454 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009455 wp = mouse_find_win(&row, &col);
9456 if (wp == NULL)
9457 return;
9458 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 curbuf = curwin->w_buffer;
9460 }
9461 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 undisplay_dollar();
9463
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009464# ifdef FEAT_INS_EXPAND
9465 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009466 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009467# endif
9468 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009469 if (dir == MSCR_DOWN || dir == MSCR_UP)
9470 {
9471 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9472 scroll_redraw(dir,
9473 (long)(curwin->w_botline - curwin->w_topline));
9474 else
9475 scroll_redraw(dir, 3L);
9476 }
9477#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009478 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009479 {
9480 int val, step = 6;
9481
9482 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009483 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009484 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9485 if (val < 0)
9486 val = 0;
9487 gui_do_horiz_scroll(val, TRUE);
9488 }
9489#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009490# ifdef FEAT_INS_EXPAND
9491 did_scroll = TRUE;
9492# endif
9493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 curwin->w_redr_status = TRUE;
9496
9497 curwin = old_curwin;
9498 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009500# ifdef FEAT_INS_EXPAND
9501 /* The popup menu may overlay the window, need to redraw it.
9502 * TODO: Would be more efficient to only redraw the windows that are
9503 * overlapped by the popup menu. */
9504 if (pum_visible() && did_scroll)
9505 {
9506 redraw_all_later(NOT_VALID);
9507 ins_compl_show_pum();
9508 }
9509# endif
9510
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009511 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 {
9513 start_arrow(&tpos);
9514# ifdef FEAT_CINDENT
9515 can_cindent = TRUE;
9516# endif
9517 }
9518}
9519#endif
9520
Bram Moolenaarec2da362017-01-21 20:04:22 +01009521/*
9522 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9523 * P_PE literally.
9524 * When "drop" is TRUE then consume the text and drop it.
9525 */
9526 int
9527bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9528{
9529 int c;
9530 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9531 int idx = 0;
9532 char_u *end = find_termcode((char_u *)"PE");
9533 int ret_char = -1;
9534 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009535 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009536
9537 /* If the end code is too long we can't detect it, read everything. */
9538 if (STRLEN(end) >= NUMBUFLEN)
9539 end = NULL;
9540 ++no_mapping;
9541 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009542 if (!p_paste)
9543 // Also have the side effects of setting 'paste' to make it work much
9544 // faster.
9545 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009546
Bram Moolenaarec2da362017-01-21 20:04:22 +01009547 for (;;)
9548 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009549 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009550 if (end == NULL && vpeekc() == NUL)
9551 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009552 do
9553 {
9554 c = vgetc();
9555 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9556 if (c == NUL || got_int)
9557 // When CTRL-C was encountered the typeahead will be flushed and we
9558 // won't get the end sequence.
9559 break;
9560
Bram Moolenaarec2da362017-01-21 20:04:22 +01009561 if (has_mbyte)
9562 idx += (*mb_char2bytes)(c, buf + idx);
9563 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01009564 buf[idx++] = c;
9565 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009566 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009567 {
9568 if (end[idx] == NUL)
9569 break; /* Found the end of paste code. */
9570 continue;
9571 }
9572 if (!drop)
9573 {
9574 switch (mode)
9575 {
9576 case PASTE_CMDLINE:
9577 put_on_cmdline(buf, idx, TRUE);
9578 break;
9579
9580 case PASTE_EX:
9581 if (gap != NULL && ga_grow(gap, idx) == OK)
9582 {
9583 mch_memmove((char *)gap->ga_data + gap->ga_len,
9584 buf, (size_t)idx);
9585 gap->ga_len += idx;
9586 }
9587 break;
9588
9589 case PASTE_INSERT:
9590 if (stop_arrow() == OK)
9591 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009592 c = buf[0];
9593 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9594 ins_eol(c);
9595 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009596 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009597 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009598 AppendToRedobuffLit(buf, idx);
9599 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009600 }
9601 break;
9602
9603 case PASTE_ONE_CHAR:
9604 if (ret_char == -1)
9605 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01009606 if (has_mbyte)
9607 ret_char = (*mb_ptr2char)(buf);
9608 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01009609 ret_char = buf[0];
9610 }
9611 break;
9612 }
9613 }
9614 idx = 0;
9615 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009616
Bram Moolenaarec2da362017-01-21 20:04:22 +01009617 --no_mapping;
9618 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009619 if (!save_paste)
9620 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009621
9622 return ret_char;
9623}
9624
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009625#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009627ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009628{
9629 /* We will be leaving the current window, unless closing another tab. */
9630 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9631 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9632 {
9633 undisplay_dollar();
9634 start_arrow(&curwin->w_cursor);
9635# ifdef FEAT_CINDENT
9636 can_cindent = TRUE;
9637# endif
9638 }
9639
9640 if (c == K_TABLINE)
9641 goto_tabpage(current_tab);
9642 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009643 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009644 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009645 redraw_statuslines(); /* will redraw the tabline when needed */
9646 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009647}
9648#endif
9649
9650#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009652ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653{
9654 pos_T tpos;
9655
9656 undisplay_dollar();
9657 tpos = curwin->w_cursor;
9658 if (gui_do_scroll())
9659 {
9660 start_arrow(&tpos);
9661# ifdef FEAT_CINDENT
9662 can_cindent = TRUE;
9663# endif
9664 }
9665}
9666
9667 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009668ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
9670 pos_T tpos;
9671
9672 undisplay_dollar();
9673 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009674 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 {
9676 start_arrow(&tpos);
9677# ifdef FEAT_CINDENT
9678 can_cindent = TRUE;
9679# endif
9680 }
9681}
9682#endif
9683
9684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009685ins_left(
9686 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
9688 pos_T tpos;
9689
9690#ifdef FEAT_FOLDING
9691 if ((fdo_flags & FDO_HOR) && KeyTyped)
9692 foldOpenCursor();
9693#endif
9694 undisplay_dollar();
9695 tpos = curwin->w_cursor;
9696 if (oneleft() == OK)
9697 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009698#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9699 /* Only call start_arrow() when not busy with preediting, it will
9700 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009701 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009702#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009703 {
9704 start_arrow_with_change(&tpos, end_change);
9705 if (!end_change)
9706 AppendCharToRedobuff(K_LEFT);
9707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708#ifdef FEAT_RIGHTLEFT
9709 /* If exit reversed string, position is fixed */
9710 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9711 revins_legal++;
9712 revins_chars++;
9713#endif
9714 }
9715
9716 /*
9717 * if 'whichwrap' set for cursor in insert mode may go to
9718 * previous line
9719 */
9720 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9721 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009722 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 start_arrow(&tpos);
9724 --(curwin->w_cursor.lnum);
9725 coladvance((colnr_T)MAXCOL);
9726 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9727 }
9728 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009729 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009730 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732
9733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009734ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735{
9736 pos_T tpos;
9737
9738#ifdef FEAT_FOLDING
9739 if ((fdo_flags & FDO_HOR) && KeyTyped)
9740 foldOpenCursor();
9741#endif
9742 undisplay_dollar();
9743 tpos = curwin->w_cursor;
9744 if (c == K_C_HOME)
9745 curwin->w_cursor.lnum = 1;
9746 curwin->w_cursor.col = 0;
9747#ifdef FEAT_VIRTUALEDIT
9748 curwin->w_cursor.coladd = 0;
9749#endif
9750 curwin->w_curswant = 0;
9751 start_arrow(&tpos);
9752}
9753
9754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009755ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756{
9757 pos_T tpos;
9758
9759#ifdef FEAT_FOLDING
9760 if ((fdo_flags & FDO_HOR) && KeyTyped)
9761 foldOpenCursor();
9762#endif
9763 undisplay_dollar();
9764 tpos = curwin->w_cursor;
9765 if (c == K_C_END)
9766 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9767 coladvance((colnr_T)MAXCOL);
9768 curwin->w_curswant = MAXCOL;
9769
9770 start_arrow(&tpos);
9771}
9772
9773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009774ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775{
9776#ifdef FEAT_FOLDING
9777 if ((fdo_flags & FDO_HOR) && KeyTyped)
9778 foldOpenCursor();
9779#endif
9780 undisplay_dollar();
9781 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9782 {
9783 start_arrow(&curwin->w_cursor);
9784 (void)bck_word(1L, FALSE, FALSE);
9785 curwin->w_set_curswant = TRUE;
9786 }
9787 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009788 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789}
9790
9791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009792ins_right(
9793 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794{
9795#ifdef FEAT_FOLDING
9796 if ((fdo_flags & FDO_HOR) && KeyTyped)
9797 foldOpenCursor();
9798#endif
9799 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009800 if (gchar_cursor() != NUL
9801#ifdef FEAT_VIRTUALEDIT
9802 || virtual_active()
9803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 )
9805 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009806 start_arrow_with_change(&curwin->w_cursor, end_change);
9807 if (!end_change)
9808 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 curwin->w_set_curswant = TRUE;
9810#ifdef FEAT_VIRTUALEDIT
9811 if (virtual_active())
9812 oneright();
9813 else
9814#endif
9815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009817 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 ++curwin->w_cursor.col;
9820 }
9821
9822#ifdef FEAT_RIGHTLEFT
9823 revins_legal++;
9824 if (revins_chars)
9825 revins_chars--;
9826#endif
9827 }
9828 /* if 'whichwrap' set for cursor in insert mode, may move the
9829 * cursor to the next line */
9830 else if (vim_strchr(p_ww, ']') != NULL
9831 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9832 {
9833 start_arrow(&curwin->w_cursor);
9834 curwin->w_set_curswant = TRUE;
9835 ++curwin->w_cursor.lnum;
9836 curwin->w_cursor.col = 0;
9837 }
9838 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009839 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009840 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841}
9842
9843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009844ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
9846#ifdef FEAT_FOLDING
9847 if ((fdo_flags & FDO_HOR) && KeyTyped)
9848 foldOpenCursor();
9849#endif
9850 undisplay_dollar();
9851 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9852 || gchar_cursor() != NUL)
9853 {
9854 start_arrow(&curwin->w_cursor);
9855 (void)fwd_word(1L, FALSE, 0);
9856 curwin->w_set_curswant = TRUE;
9857 }
9858 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009859 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860}
9861
9862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009863ins_up(
9864 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865{
9866 pos_T tpos;
9867 linenr_T old_topline = curwin->w_topline;
9868#ifdef FEAT_DIFF
9869 int old_topfill = curwin->w_topfill;
9870#endif
9871
9872 undisplay_dollar();
9873 tpos = curwin->w_cursor;
9874 if (cursor_up(1L, TRUE) == OK)
9875 {
9876 if (startcol)
9877 coladvance(getvcol_nolist(&Insstart));
9878 if (old_topline != curwin->w_topline
9879#ifdef FEAT_DIFF
9880 || old_topfill != curwin->w_topfill
9881#endif
9882 )
9883 redraw_later(VALID);
9884 start_arrow(&tpos);
9885#ifdef FEAT_CINDENT
9886 can_cindent = TRUE;
9887#endif
9888 }
9889 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009890 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891}
9892
9893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009894ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895{
9896 pos_T tpos;
9897
9898 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009899
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009900 if (mod_mask & MOD_MASK_CTRL)
9901 {
9902 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009903 if (first_tabpage->tp_next != NULL)
9904 {
9905 start_arrow(&curwin->w_cursor);
9906 goto_tabpage(-1);
9907 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009908 return;
9909 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009910
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 tpos = curwin->w_cursor;
9912 if (onepage(BACKWARD, 1L) == OK)
9913 {
9914 start_arrow(&tpos);
9915#ifdef FEAT_CINDENT
9916 can_cindent = TRUE;
9917#endif
9918 }
9919 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009920 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921}
9922
9923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009924ins_down(
9925 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926{
9927 pos_T tpos;
9928 linenr_T old_topline = curwin->w_topline;
9929#ifdef FEAT_DIFF
9930 int old_topfill = curwin->w_topfill;
9931#endif
9932
9933 undisplay_dollar();
9934 tpos = curwin->w_cursor;
9935 if (cursor_down(1L, TRUE) == OK)
9936 {
9937 if (startcol)
9938 coladvance(getvcol_nolist(&Insstart));
9939 if (old_topline != curwin->w_topline
9940#ifdef FEAT_DIFF
9941 || old_topfill != curwin->w_topfill
9942#endif
9943 )
9944 redraw_later(VALID);
9945 start_arrow(&tpos);
9946#ifdef FEAT_CINDENT
9947 can_cindent = TRUE;
9948#endif
9949 }
9950 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009951 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952}
9953
9954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009955ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956{
9957 pos_T tpos;
9958
9959 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009960
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009961 if (mod_mask & MOD_MASK_CTRL)
9962 {
9963 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009964 if (first_tabpage->tp_next != NULL)
9965 {
9966 start_arrow(&curwin->w_cursor);
9967 goto_tabpage(0);
9968 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009969 return;
9970 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009971
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 tpos = curwin->w_cursor;
9973 if (onepage(FORWARD, 1L) == OK)
9974 {
9975 start_arrow(&tpos);
9976#ifdef FEAT_CINDENT
9977 can_cindent = TRUE;
9978#endif
9979 }
9980 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009981 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982}
9983
9984#ifdef FEAT_DND
9985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009986ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987{
9988 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9989}
9990#endif
9991
9992/*
9993 * Handle TAB in Insert or Replace mode.
9994 * Return TRUE when the TAB needs to be inserted like a normal character.
9995 */
9996 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009997ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998{
9999 int ind;
10000 int i;
10001 int temp;
10002
10003 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10004 Insstart_blank_vcol = get_nolist_virtcol();
10005 if (echeck_abbr(TAB + ABBR_OFF))
10006 return FALSE;
10007
10008 ind = inindent(0);
10009#ifdef FEAT_CINDENT
10010 if (ind)
10011 can_cindent = FALSE;
10012#endif
10013
10014 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010015 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 */
10017 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010018#ifdef FEAT_VARTABS
10019 && !(p_sta && ind
10020 /* These five lines mean 'tabstop' != 'shiftwidth' */
10021 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
10022 || (tabstop_count(curbuf->b_p_vts_array) == 1
10023 && tabstop_first(curbuf->b_p_vts_array)
10024 != get_sw_value(curbuf))
10025 || (tabstop_count(curbuf->b_p_vts_array) == 0
10026 && curbuf->b_p_ts != get_sw_value(curbuf))))
10027 && tabstop_count(curbuf->b_p_vsts_array) == 0
10028#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010029 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010030#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010031 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 return TRUE;
10033
10034 if (stop_arrow() == FAIL)
10035 return TRUE;
10036
10037 did_ai = FALSE;
10038#ifdef FEAT_SMARTINDENT
10039 did_si = FALSE;
10040 can_si = FALSE;
10041 can_si_back = FALSE;
10042#endif
10043 AppendToRedobuff((char_u *)"\t");
10044
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010045#ifdef FEAT_VARTABS
10046 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10047 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010048 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010049 temp -= get_nolist_virtcol() % temp;
10050 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010051 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010052 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010053 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010054 curbuf->b_p_vsts_array);
10055 else /* otherwise use 'tabstop' */
10056 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10057 curbuf->b_p_vts_array);
10058#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010060 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010061 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10062 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 else /* otherwise use 'tabstop' */
10064 temp = (int)curbuf->b_p_ts;
10065 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067
10068 /*
10069 * Insert the first space with ins_char(). It will delete one char in
10070 * replace mode. Insert the rest with ins_str(); it will not delete any
10071 * chars. For VREPLACE mode, we use ins_char() for all characters.
10072 */
10073 ins_char(' ');
10074 while (--temp > 0)
10075 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 if (State & VREPLACE_FLAG)
10077 ins_char(' ');
10078 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 {
10080 ins_str((char_u *)" ");
10081 if (State & REPLACE_FLAG) /* no char replaced */
10082 replace_push(NUL);
10083 }
10084 }
10085
10086 /*
10087 * When 'expandtab' not set: Replace spaces by TABs where possible.
10088 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010089#ifdef FEAT_VARTABS
10090 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10091 || get_sts_value() > 0
10092 || (p_sta && ind)))
10093#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010094 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 {
10097 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 char_u *saved_line = NULL; /* init for GCC */
10099 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 pos_T fpos;
10101 pos_T *cursor;
10102 colnr_T want_vcol, vcol;
10103 int change_col = -1;
10104 int save_list = curwin->w_p_list;
10105
10106 /*
10107 * Get the current line. For VREPLACE mode, don't make real changes
10108 * yet, just work on a copy of the line.
10109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 if (State & VREPLACE_FLAG)
10111 {
10112 pos = curwin->w_cursor;
10113 cursor = &pos;
10114 saved_line = vim_strsave(ml_get_curline());
10115 if (saved_line == NULL)
10116 return FALSE;
10117 ptr = saved_line + pos.col;
10118 }
10119 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 {
10121 ptr = ml_get_cursor();
10122 cursor = &curwin->w_cursor;
10123 }
10124
10125 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10126 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10127 curwin->w_p_list = FALSE;
10128
10129 /* Find first white before the cursor */
10130 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010131 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 {
10133 --fpos.col;
10134 --ptr;
10135 }
10136
10137 /* In Replace mode, don't change characters before the insert point. */
10138 if ((State & REPLACE_FLAG)
10139 && fpos.lnum == Insstart.lnum
10140 && fpos.col < Insstart.col)
10141 {
10142 ptr += Insstart.col - fpos.col;
10143 fpos.col = Insstart.col;
10144 }
10145
10146 /* compute virtual column numbers of first white and cursor */
10147 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10148 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10149
Bram Moolenaar597a4222014-06-25 14:39:50 +020010150 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10151 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010152 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010154 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 if (vcol + i > want_vcol)
10156 break;
10157 if (*ptr != TAB)
10158 {
10159 *ptr = TAB;
10160 if (change_col < 0)
10161 {
10162 change_col = fpos.col; /* Column of first change */
10163 /* May have to adjust Insstart */
10164 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10165 Insstart.col = fpos.col;
10166 }
10167 }
10168 ++fpos.col;
10169 ++ptr;
10170 vcol += i;
10171 }
10172
10173 if (change_col >= 0)
10174 {
10175 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010176 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177
10178 /* Skip over the spaces we need. */
10179 while (vcol < want_vcol && *ptr == ' ')
10180 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010181 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 ++ptr;
10183 ++repl_off;
10184 }
10185 if (vcol > want_vcol)
10186 {
10187 /* Must have a char with 'showbreak' just before it. */
10188 --ptr;
10189 --repl_off;
10190 }
10191 fpos.col += repl_off;
10192
10193 /* Delete following spaces. */
10194 i = cursor->col - fpos.col;
10195 if (i > 0)
10196 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010197 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010199 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 for (temp = i; --temp >= 0; )
10201 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010010202#ifdef FEAT_TEXT_PROP
10203 curbuf->b_ml.ml_line_len -= i;
10204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010206#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010207 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010208 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010209 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010210 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10211 (char_u *)"\t", 1);
10212 }
10213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 cursor->col -= i;
10215
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 /*
10217 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10218 * backspacing over the changed spacing and then inserting the new
10219 * spacing.
10220 */
10221 if (State & VREPLACE_FLAG)
10222 {
10223 /* Backspace from real cursor to change_col */
10224 backspace_until_column(change_col);
10225
10226 /* Insert each char in saved_line from changed_col to
10227 * ptr-cursor */
10228 ins_bytes_len(saved_line + change_col,
10229 cursor->col - change_col);
10230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 }
10232
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 if (State & VREPLACE_FLAG)
10234 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 curwin->w_p_list = save_list;
10236 }
10237
10238 return FALSE;
10239}
10240
10241/*
10242 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010243 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 */
10245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010246ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247{
10248 int i;
10249
10250 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010251 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010253 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 undisplay_dollar();
10255
10256 /*
10257 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10258 * character under the cursor. Only push a NUL on the replace stack,
10259 * nothing to put back when the NL is deleted.
10260 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010261 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 replace_push(NUL);
10263
10264 /*
10265 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10266 * replacing the next line, so we push all of the characters left on the
10267 * line onto the replace stack. This is not done here though, it is done
10268 * in open_line().
10269 */
10270
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010271#ifdef FEAT_VIRTUALEDIT
10272 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10273 * CTRL-O). */
10274 if (virtual_active() && curwin->w_cursor.coladd > 0)
10275 coladvance(getviscol());
10276#endif
10277
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278#ifdef FEAT_RIGHTLEFT
10279# ifdef FEAT_FKMAP
10280 if (p_altkeymap && p_fkmap)
10281 fkmap(NL);
10282# endif
10283 /* NL in reverse insert will always start in the end of
10284 * current line. */
10285 if (revins_on)
10286 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10287#endif
10288
10289 AppendToRedobuff(NL_STR);
10290 i = open_line(FORWARD,
10291#ifdef FEAT_COMMENTS
10292 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10293#endif
10294 0, old_indent);
10295 old_indent = 0;
10296#ifdef FEAT_CINDENT
10297 can_cindent = TRUE;
10298#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010299#ifdef FEAT_FOLDING
10300 /* When inserting a line the cursor line must never be in a closed fold. */
10301 foldOpenCursor();
10302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010304 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305}
10306
10307#ifdef FEAT_DIGRAPHS
10308/*
10309 * Handle digraph in insert mode.
10310 * Returns character still to be inserted, or NUL when nothing remaining to be
10311 * done.
10312 */
10313 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010314ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315{
10316 int c;
10317 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010318 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319
10320 pc_status = PC_STATUS_UNSET;
10321 if (redrawing() && !char_avail())
10322 {
10323 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010324 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325
10326 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010327 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328#ifdef FEAT_CMDL_INFO
10329 add_to_showcmd_c(Ctrl_K);
10330#endif
10331 }
10332
10333#ifdef USE_ON_FLY_SCROLL
10334 dont_scroll = TRUE; /* disallow scrolling here */
10335#endif
10336
10337 /* don't map the digraph chars. This also prevents the
10338 * mode message to be deleted when ESC is hit */
10339 ++no_mapping;
10340 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010341 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 --no_mapping;
10343 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010344 if (did_putchar)
10345 /* when the line fits in 'columns' the '?' is at the start of the next
10346 * line and will not be removed by the redraw */
10347 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010348
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 if (IS_SPECIAL(c) || mod_mask) /* special key */
10350 {
10351#ifdef FEAT_CMDL_INFO
10352 clear_showcmd();
10353#endif
10354 insert_special(c, TRUE, FALSE);
10355 return NUL;
10356 }
10357 if (c != ESC)
10358 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010359 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 if (redrawing() && !char_avail())
10361 {
10362 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010363 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364
10365 if (char2cells(c) == 1)
10366 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010367 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010369 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 }
10371#ifdef FEAT_CMDL_INFO
10372 add_to_showcmd_c(c);
10373#endif
10374 }
10375 ++no_mapping;
10376 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010377 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 --no_mapping;
10379 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010380 if (did_putchar)
10381 /* when the line fits in 'columns' the '?' is at the start of the
10382 * next line and will not be removed by a redraw */
10383 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 if (cc != ESC)
10385 {
10386 AppendToRedobuff((char_u *)CTRL_V_STR);
10387 c = getdigraph(c, cc, TRUE);
10388#ifdef FEAT_CMDL_INFO
10389 clear_showcmd();
10390#endif
10391 return c;
10392 }
10393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394#ifdef FEAT_CMDL_INFO
10395 clear_showcmd();
10396#endif
10397 return NUL;
10398}
10399#endif
10400
10401/*
10402 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10403 * Returns the char to be inserted, or NUL if none found.
10404 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010405 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010406ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407{
10408 int c;
10409 int temp;
10410 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010411 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412
10413 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10414 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010415 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 return NUL;
10417 }
10418
10419 /* try to advance to the cursor column */
10420 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010421 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 prev_ptr = ptr;
10423 validate_virtcol();
10424 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10425 {
10426 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010427 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 }
10429 if ((colnr_T)temp > curwin->w_virtcol)
10430 ptr = prev_ptr;
10431
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010434 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 return c;
10436}
10437
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010438/*
10439 * CTRL-Y or CTRL-E typed in Insert mode.
10440 */
10441 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010442ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010443{
10444 int c = tc;
10445
10446#ifdef FEAT_INS_EXPAND
10447 if (ctrl_x_mode == CTRL_X_SCROLL)
10448 {
10449 if (c == Ctrl_Y)
10450 scrolldown_clamp();
10451 else
10452 scrollup_clamp();
10453 redraw_later(VALID);
10454 }
10455 else
10456#endif
10457 {
10458 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10459 if (c != NUL)
10460 {
10461 long tw_save;
10462
10463 /* The character must be taken literally, insert like it
10464 * was typed after a CTRL-V, and pretend 'textwidth'
10465 * wasn't set. Digits, 'o' and 'x' are special after a
10466 * CTRL-V, don't use it for these. */
10467 if (c < 256 && !isalnum(c))
10468 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10469 tw_save = curbuf->b_p_tw;
10470 curbuf->b_p_tw = -1;
10471 insert_special(c, TRUE, FALSE);
10472 curbuf->b_p_tw = tw_save;
10473#ifdef FEAT_RIGHTLEFT
10474 revins_chars++;
10475 revins_legal++;
10476#endif
10477 c = Ctrl_V; /* pretend CTRL-V is last character */
10478 auto_format(FALSE, TRUE);
10479 }
10480 }
10481 return c;
10482}
10483
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484#ifdef FEAT_SMARTINDENT
10485/*
10486 * Try to do some very smart auto-indenting.
10487 * Used when inserting a "normal" character.
10488 */
10489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010490ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491{
10492 pos_T *pos, old_pos;
10493 char_u *ptr;
10494 int i;
10495 int temp;
10496
10497 /*
10498 * do some very smart indenting when entering '{' or '}'
10499 */
10500 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10501 {
10502 /*
10503 * for '}' set indent equal to indent of line containing matching '{'
10504 */
10505 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10506 {
10507 old_pos = curwin->w_cursor;
10508 /*
10509 * If the matching '{' has a ')' immediately before it (ignoring
10510 * white-space), then line up with the start of the line
10511 * containing the matching '(' if there is one. This handles the
10512 * case where an "if (..\n..) {" statement continues over multiple
10513 * lines -- webb
10514 */
10515 ptr = ml_get(pos->lnum);
10516 i = pos->col;
10517 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010518 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 ;
10520 curwin->w_cursor.lnum = pos->lnum;
10521 curwin->w_cursor.col = i;
10522 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10523 curwin->w_cursor = *pos;
10524 i = get_indent();
10525 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010527 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 (void)set_indent(i, SIN_CHANGED);
10530 }
10531 else if (curwin->w_cursor.col > 0)
10532 {
10533 /*
10534 * when inserting '{' after "O" reduce indent, but not
10535 * more than indent of previous line
10536 */
10537 temp = TRUE;
10538 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10539 {
10540 old_pos = curwin->w_cursor;
10541 i = get_indent();
10542 while (curwin->w_cursor.lnum > 1)
10543 {
10544 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10545
10546 /* ignore empty lines and lines starting with '#'. */
10547 if (*ptr != '#' && *ptr != NUL)
10548 break;
10549 }
10550 if (get_indent() >= i)
10551 temp = FALSE;
10552 curwin->w_cursor = old_pos;
10553 }
10554 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010555 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 }
10557 }
10558
10559 /*
10560 * set indent of '#' always to 0
10561 */
10562 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10563 {
10564 /* remember current indent for next line */
10565 old_indent = get_indent();
10566 (void)set_indent(0, SIN_CHANGED);
10567 }
10568
10569 /* Adjust ai_col, the char at this position can be deleted. */
10570 if (ai_col > curwin->w_cursor.col)
10571 ai_col = curwin->w_cursor.col;
10572}
10573#endif
10574
10575/*
10576 * Get the value that w_virtcol would have when 'list' is off.
10577 * Unless 'cpo' contains the 'L' flag.
10578 */
Bram Moolenaarf9514162018-11-22 03:08:29 +010010579 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010580get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010582 // check validity of cursor in current buffer
10583 if (curwin->w_buffer == NULL
10584 || curwin->w_buffer->b_ml.ml_mfp == NULL
10585 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
10586 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10588 return getvcol_nolist(&curwin->w_cursor);
10589 validate_virtcol();
10590 return curwin->w_virtcol;
10591}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010592
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010593#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010594/*
10595 * Handle the InsertCharPre autocommand.
10596 * "c" is the character that was typed.
10597 * Return a pointer to allocated memory with the replacement string.
10598 * Return NULL to continue inserting "c".
10599 */
10600 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010601do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010602{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010603 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010604 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010605 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010606
10607 /* Return quickly when there is nothing to do. */
10608 if (!has_insertcharpre())
10609 return NULL;
10610
Bram Moolenaar704984a2012-06-01 14:57:51 +020010611 if (has_mbyte)
10612 buf[(*mb_char2bytes)(c, buf)] = NUL;
10613 else
Bram Moolenaar704984a2012-06-01 14:57:51 +020010614 {
10615 buf[0] = c;
10616 buf[1] = NUL;
10617 }
10618
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010619 /* Lock the text to avoid weird things from happening. */
10620 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010621 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010622
Bram Moolenaar704984a2012-06-01 14:57:51 +020010623 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010624 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010625 {
10626 /* Get the value of v:char. It may be empty or more than one
10627 * character. Only use it when changed, otherwise continue with the
10628 * original character to avoid breaking autoindent. */
10629 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10630 res = vim_strsave(get_vim_var_str(VV_CHAR));
10631 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010632
10633 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10634 --textlock;
10635
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010636 // Restore the State, it may have been changed.
10637 State = save_State;
10638
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010639 return res;
10640}
10641#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010642
10643/*
10644 * Trigger "event" and take care of fixing undo.
10645 */
10646 static int
10647ins_apply_autocmds(event_T event)
10648{
10649 varnumber_T tick = CHANGEDTICK(curbuf);
10650 int r;
10651
10652 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
10653
10654 // If u_savesub() was called then we are not prepared to start
10655 // a new line. Call u_save() with no contents to fix that.
10656 if (tick != CHANGEDTICK(curbuf))
10657 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
10658
10659 return r;
10660}