blob: 8ee4643570bad4a8fdcb466af4d142a3010e4db7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010020# define CTRL_X_WANT_IDENT 0x100
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010022# define CTRL_X_NORMAL 0 /* CTRL-N CTRL-P completion, default */
23# define CTRL_X_NOT_DEFINED_YET 1
24# define CTRL_X_SCROLL 2
25# define CTRL_X_WHOLE_LINE 3
26# define CTRL_X_FILES 4
27# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
28# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
30# define CTRL_X_FINISHED 8
31# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
32# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
33# define CTRL_X_CMDLINE 11
34# define CTRL_X_FUNCTION 12
35# define CTRL_X_OMNI 13
36# define CTRL_X_SPELL 14
37# define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
38# define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010040# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
41# define CTRL_X_MODE_LINE_OR_EVAL(m) ((m) == CTRL_X_WHOLE_LINE || (m) == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010043/* Message for CTRL-X mode, index is ctrl_x_mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static char *ctrl_x_msgs[] =
45{
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010046 N_(" Keyword completion (^N^P)"), /* CTRL_X_NORMAL, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000047 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010048 NULL, /* CTRL_X_SCROLL: depends on state */
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 N_(" Whole line completion (^L^N^P)"),
50 N_(" File name completion (^F^N^P)"),
51 N_(" Tag completion (^]^N^P)"),
52 N_(" Path pattern completion (^N^P)"),
53 N_(" Definition completion (^D^N^P)"),
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010054 NULL, /* CTRL_X_FINISHED */
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 N_(" Dictionary completion (^K^N^P)"),
56 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000057 N_(" Command-line completion (^V^N^P)"),
58 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000059 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000060 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000061 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010062 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000063};
64
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000065static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010066# ifdef FEAT_COMPL_FUNC
Bram Moolenaar37dd0182010-11-10 16:54:20 +010067static char e_complwin[] = N_("E839: Completion function changed window");
68static char e_compldel[] = N_("E840: Completion function deleted text");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71/*
72 * Structure used to store one match for insert completion.
73 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000074typedef struct compl_S compl_T;
75struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
Bram Moolenaar572cb562005-08-05 21:35:02 +000077 compl_T *cp_next;
78 compl_T *cp_prev;
79 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000080 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000081 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000082 char_u *cp_fname; /* file containing the match, allocated when
83 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000084 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
85 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086};
87
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010088# define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
89# define FREE_FNAME (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
91/*
92 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000093 * "compl_first_match" points to the start of the list.
94 * "compl_curr_match" points to the currently selected entry.
95 * "compl_shown_match" is different from compl_curr_match during
96 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000098static compl_T *compl_first_match = NULL;
99static compl_T *compl_curr_match = NULL;
100static compl_T *compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +0200101static compl_T *compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000103/* After using a cursor key <Enter> selects a match in the popup menu,
104 * otherwise it inserts a line break. */
105static int compl_enter_selects = FALSE;
106
Bram Moolenaara6557602006-02-04 22:43:20 +0000107/* When "compl_leader" is not NULL only matches that start with this string
108 * are used. */
109static char_u *compl_leader = NULL;
110
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000111static int compl_get_longest = FALSE; /* put longest common string
112 in compl_leader */
113
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200114static int compl_no_insert = FALSE; /* FALSE: select & insert
115 TRUE: noinsert */
116static int compl_no_select = FALSE; /* FALSE: select & insert
117 TRUE: noselect */
118
Bram Moolenaara6557602006-02-04 22:43:20 +0000119static int compl_used_match; /* Selected one of the matches. When
120 FALSE the match was edited or using
121 the longest common string. */
122
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000123static int compl_was_interrupted = FALSE; /* didn't finish finding
124 completions. */
125
126static int compl_restarting = FALSE; /* don't insert match */
127
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128/* When the first completion is done "compl_started" is set. When it's
129 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000130static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000131
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100132/* Which Ctrl-X mode are we in? */
133static int ctrl_x_mode = CTRL_X_NORMAL;
134
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000135/* Set when doing something for completion that may call edit() recursively,
136 * which is not allowed. */
137static int compl_busy = FALSE;
138
Bram Moolenaar572cb562005-08-05 21:35:02 +0000139static int compl_matches = 0;
140static char_u *compl_pattern = NULL;
141static int compl_direction = FORWARD;
142static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000143static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000144static pos_T compl_startpos;
145static colnr_T compl_col = 0; /* column where the text starts
146 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000147static char_u *compl_orig_text = NULL; /* text as it was before
148 * completion started */
149static int compl_cont_mode = 0;
150static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
Bram Moolenaar82139082011-09-14 16:52:09 +0200152static int compl_opt_refresh_always = FALSE;
153
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static void ins_ctrl_x(void);
155static int has_compl_option(int dict_opt);
156static int ins_compl_accept_char(int c);
157static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
158static int ins_compl_equal(compl_T *match, char_u *str, int len);
159static void ins_compl_longest_match(compl_T *match);
160static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
161static int ins_compl_make_cyclic(void);
162static void ins_compl_upd_pum(void);
163static void ins_compl_del_pum(void);
164static int pum_wanted(void);
165static int pum_enough_matches(void);
166static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
167static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
168static char_u *find_line_end(char_u *ptr);
169static void ins_compl_free(void);
170static void ins_compl_clear(void);
171static int ins_compl_bs(void);
172static int ins_compl_need_restart(void);
173static void ins_compl_new_leader(void);
174static void ins_compl_addleader(int c);
175static int ins_compl_len(void);
176static void ins_compl_restart(void);
177static void ins_compl_set_original_text(char_u *str);
178static void ins_compl_addfrommatch(void);
179static int ins_compl_prep(int c);
180static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
181static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100182# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100183static void ins_compl_add_list(list_T *list);
184static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100185# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static int ins_compl_get_exp(pos_T *ini);
187static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200188static void ins_compl_insert(int in_compl_func);
189static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static int ins_compl_key2dir(int c);
191static int ins_compl_pum_key(int c);
192static int ins_compl_key2count(int c);
193static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100194static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100195static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100196static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#endif /* FEAT_INS_EXPAND */
198
199#define BACKSPACE_CHAR 1
200#define BACKSPACE_WORD 2
201#define BACKSPACE_WORD_NOT_SPACE 3
202#define BACKSPACE_LINE 4
203
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ins_redraw(int ready);
205static void ins_ctrl_v(void);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200206#ifdef FEAT_JOB_CHANNEL
207static void init_prompt(int cmdchar_todo);
208#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void undisplay_dollar(void);
210static void insert_special(int, int, int);
211static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
212static void check_auto_format(int);
213static void redo_literal(int c);
214static void start_arrow(pos_T *end_insert_pos);
215static void start_arrow_with_change(pos_T *end_insert_pos, int change);
216static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000217#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100218static void check_spell_redraw(void);
219static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000220static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000221#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
223static int echeck_abbr(int);
224static int replace_pop(void);
225static void replace_join(int off);
226static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void replace_flush(void);
231static void replace_do_bs(int limit_col);
232static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100234static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ins_reg(void);
237static void ins_ctrl_g(void);
238static void ins_ctrl_hat(void);
239static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static int ins_start_select(int c);
244static void ins_insert(int replaceState);
245static void ins_ctrl_o(void);
246static void ins_shift(int c, int lastc);
247static void ins_del(void);
248static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ins_mouse(int c);
251static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000253#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100254static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000255#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static void ins_left(int end_change);
257static void ins_home(int c);
258static void ins_end(int c);
259static void ins_s_left(void);
260static void ins_right(int end_change);
261static void ins_s_right(void);
262static void ins_up(int startcol);
263static void ins_pageup(void);
264static void ins_down(int startcol);
265static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static int ins_tab(void);
270static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100272static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100274static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100276static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100278static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100279#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100280static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
283static colnr_T Insstart_textlen; /* length of line when insert started */
284static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100285static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286
287static char_u *last_insert = NULL; /* the text of the previous insert,
288 K_SPECIAL and CSI are escaped */
289static int last_insert_skip; /* nr of chars in front of previous insert */
290static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000291static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
293#ifdef FEAT_CINDENT
294static int can_cindent; /* may do cindenting on this line */
295#endif
296
297static int old_indent = 0; /* for ^^D command in insert mode */
298
299#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000300static int revins_on; /* reverse insert mode on */
301static int revins_chars; /* how much to skip after edit */
302static int revins_legal; /* was the last char 'legal'? */
303static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304#endif
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static int ins_need_undo; /* call u_save() before inserting a
307 char. Set when edit() is called.
308 after that arrow_used is used. */
309
310static int did_add_space = FALSE; /* auto_format() added an extra space
311 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200312static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
313 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315/*
316 * edit(): Start inserting text.
317 *
318 * "cmdchar" can be:
319 * 'i' normal insert command
320 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100321 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 * 'R' replace command
323 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
324 * but still only one <CR> is inserted. The <Esc> is not used for redo.
325 * 'g' "gI" command.
326 * 'V' "gR" command for Virtual Replace mode.
327 * 'v' "gr" command for single character Virtual Replace mode.
328 *
329 * This function is not called recursively. For CTRL-O commands, it returns
330 * and lets the caller handle the Normal-mode command.
331 *
332 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
333 */
334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100335edit(
336 int cmdchar,
337 int startln, /* if set, insert at start of line */
338 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 int c = 0;
341 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100342 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000343 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 int i;
346 int did_backspace = TRUE; /* previous char was backspace */
347#ifdef FEAT_CINDENT
348 int line_is_white = FALSE; /* line is empty before insert */
349#endif
350 linenr_T old_topline = 0; /* topline before insertion */
351#ifdef FEAT_DIFF
352 int old_topfill = -1;
353#endif
354 int inserted_space = FALSE; /* just inserted a space */
355 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000356 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200357#ifdef FEAT_JOB_CHANNEL
358 int cmdchar_todo = cmdchar;
359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000361 /* Remember whether editing was restarted after CTRL-O. */
362 did_restart_edit = restart_edit;
363
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 /* sleep before redrawing, needed for "CTRL-O :" that results in an
365 * error message */
366 check_for_delay(TRUE);
367
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100368 /* set Insstart_orig to Insstart */
369 update_Insstart_orig = TRUE;
370
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371#ifdef HAVE_SANDBOX
372 /* Don't allow inserting in the sandbox. */
373 if (sandbox != 0)
374 {
375 EMSG(_(e_sandbox));
376 return FALSE;
377 }
378#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000379 /* Don't allow changes in the buffer while editing the cmdline. The
380 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000381 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000382 {
383 EMSG(_(e_secure));
384 return FALSE;
385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
387#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000388 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000389 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000390 {
391 EMSG(_(e_secure));
392 return FALSE;
393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 ins_compl_clear(); /* clear stuff for CTRL-X mode */
395#endif
396
Bram Moolenaar843ee412004-06-30 16:16:41 +0000397 /*
398 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
399 */
400 if (cmdchar != 'r' && cmdchar != 'v')
401 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100402 pos_T save_cursor = curwin->w_cursor;
403
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100404#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000405 if (cmdchar == 'R')
406 ptr = (char_u *)"r";
407 else if (cmdchar == 'V')
408 ptr = (char_u *)"v";
409 else
410 ptr = (char_u *)"i";
411 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200412 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100413#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000414 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100415
Bram Moolenaar097c9922013-05-19 21:15:15 +0200416 /* Make sure the cursor didn't move. Do call check_cursor_col() in
417 * case the text was modified. Since Insert mode was not started yet
418 * a call to check_cursor_col() may move the cursor, especially with
419 * the "A" command, thus set State to avoid that. Also check that the
420 * line number is still valid (lines may have been deleted).
421 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100422 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100423#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200424 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100425#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200426 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100427 {
428 int save_state = State;
429
430 curwin->w_cursor = save_cursor;
431 State = INSERT;
432 check_cursor_col();
433 State = save_state;
434 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000435 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000436
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200437#ifdef FEAT_CONCEAL
438 /* Check if the cursor line needs redrawing before changing State. If
439 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200440 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200441#endif
442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#ifdef FEAT_MOUSE
444 /*
445 * When doing a paste with the middle mouse button, Insstart is set to
446 * where the paste started.
447 */
448 if (where_paste_started.lnum != 0)
449 Insstart = where_paste_started;
450 else
451#endif
452 {
453 Insstart = curwin->w_cursor;
454 if (startln)
455 Insstart.col = 0;
456 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000457 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 Insstart_blank_vcol = MAXCOL;
459 if (!did_ai)
460 ai_col = 0;
461
462 if (cmdchar != NUL && restart_edit == 0)
463 {
464 ResetRedobuff();
465 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 if (cmdchar == 'V' || cmdchar == 'v')
467 {
468 /* "gR" or "gr" command */
469 AppendCharToRedobuff('g');
470 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
471 }
472 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100474 if (cmdchar == K_PS)
475 AppendCharToRedobuff('a');
476 else
477 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 if (cmdchar == 'g') /* "gI" command */
479 AppendCharToRedobuff('I');
480 else if (cmdchar == 'r') /* "r<CR>" command */
481 count = 1; /* insert only one <CR> */
482 }
483 }
484
485 if (cmdchar == 'R')
486 {
487#ifdef FEAT_FKMAP
488 if (p_fkmap && p_ri)
489 {
490 beep_flush();
491 EMSG(farsi_text_3); /* encoded in Farsi */
492 State = INSERT;
493 }
494 else
495#endif
496 State = REPLACE;
497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 else if (cmdchar == 'V' || cmdchar == 'v')
499 {
500 State = VREPLACE;
501 replaceState = VREPLACE;
502 orig_line_count = curbuf->b_ml.ml_line_count;
503 vr_lines_changed = 1;
504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 else
506 State = INSERT;
507
508 stop_insert_mode = FALSE;
509
510 /*
511 * Need to recompute the cursor position, it might move when the cursor is
512 * on a TAB or special character.
513 */
514 curs_columns(TRUE);
515
516 /*
517 * Enable langmap or IME, indicated by 'iminsert'.
518 * Note that IME may enabled/disabled without us noticing here, thus the
519 * 'iminsert' value may not reflect what is actually used. It is updated
520 * when hitting <Esc>.
521 */
522 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
523 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100524#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
526#endif
527
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528#ifdef FEAT_MOUSE
529 setmouse();
530#endif
531#ifdef FEAT_CMDL_INFO
532 clear_showcmd();
533#endif
534#ifdef FEAT_RIGHTLEFT
535 /* there is no reverse replace mode */
536 revins_on = (State == INSERT && p_ri);
537 if (revins_on)
538 undisplay_dollar();
539 revins_chars = 0;
540 revins_legal = 0;
541 revins_scol = -1;
542#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100543 if (!p_ek)
544 /* Disable bracketed paste mode, we won't recognize the escape
545 * sequences. */
546 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
548 /*
549 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100550 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
551 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
553 if (restart_edit != 0 && stuff_empty())
554 {
555#ifdef FEAT_MOUSE
556 /*
557 * After a paste we consider text typed to be part of the insert for
558 * the pasted text. You can backspace over the pasted text too.
559 */
560 if (where_paste_started.lnum)
561 arrow_used = FALSE;
562 else
563#endif
564 arrow_used = TRUE;
565 restart_edit = 0;
566
567 /*
568 * If the cursor was after the end-of-line before the CTRL-O and it is
569 * now at the end-of-line, put it after the end-of-line (this is not
570 * correct in very rare cases).
571 * Also do this if curswant is greater than the current virtual
572 * column. Eg after "^O$" or "^O80|".
573 */
574 validate_virtcol();
575 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000576 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 || curwin->w_curswant > curwin->w_virtcol)
578 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
579 {
580 if (ptr[1] == NUL)
581 ++curwin->w_cursor.col;
582#ifdef FEAT_MBYTE
583 else if (has_mbyte)
584 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000585 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (ptr[i] == NUL)
587 curwin->w_cursor.col += i;
588 }
589#endif
590 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000591 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
593 else
594 arrow_used = FALSE;
595
596 /* we are in insert mode now, don't need to start it anymore */
597 need_start_insertmode = FALSE;
598
599 /* Need to save the line for undo before inserting the first char. */
600 ins_need_undo = TRUE;
601
602#ifdef FEAT_MOUSE
603 where_paste_started.lnum = 0;
604#endif
605#ifdef FEAT_CINDENT
606 can_cindent = TRUE;
607#endif
608#ifdef FEAT_FOLDING
609 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
610 * restarting. */
611 if (!p_im && did_restart_edit == 0)
612 foldOpenCursor();
613#endif
614
615 /*
616 * If 'showmode' is set, show the current (insert/replace/..) mode.
617 * A warning message for changing a readonly file is given here, before
618 * actually changing anything. It's put after the mode, if any.
619 */
620 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000621 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 i = showmode();
623
624 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000625 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627#ifdef CURSOR_SHAPE
628 ui_cursor_shape(); /* may show different cursor shape */
629#endif
630#ifdef FEAT_DIGRAPHS
631 do_digraph(-1); /* clear digraphs */
632#endif
633
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000634 /*
635 * Get the current length of the redo buffer, those characters have to be
636 * skipped if we want to get to the inserted characters.
637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ptr = get_inserted();
639 if (ptr == NULL)
640 new_insert_skip = 0;
641 else
642 {
643 new_insert_skip = (int)STRLEN(ptr);
644 vim_free(ptr);
645 }
646
647 old_indent = 0;
648
649 /*
650 * Main loop in Insert mode: repeat until Insert mode is left.
651 */
652 for (;;)
653 {
654#ifdef FEAT_RIGHTLEFT
655 if (!revins_legal)
656 revins_scol = -1; /* reset on illegal motions */
657 else
658 revins_legal = 0;
659#endif
660 if (arrow_used) /* don't repeat insert when arrow key used */
661 count = 0;
662
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100663 if (update_Insstart_orig)
664 Insstart_orig = Insstart;
665
Bram Moolenaar00672e12016-06-26 18:38:13 +0200666 if (stop_insert_mode
667#ifdef FEAT_INS_EXPAND
668 && !pum_visible()
669#endif
670 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
672 /* ":stopinsert" used or 'insertmode' reset */
673 count = 0;
674 goto doESCkey;
675 }
676
677 /* set curwin->w_curswant for next K_DOWN or K_UP */
678 if (!arrow_used)
679 curwin->w_set_curswant = TRUE;
680
681 /* If there is no typeahead may check for timestamps (e.g., for when a
682 * menu invoked a shell command). */
683 if (stuff_empty())
684 {
685 did_check_timestamps = FALSE;
686 if (need_check_timestamps)
687 check_timestamps(FALSE);
688 }
689
690 /*
691 * When emsg() was called msg_scroll will have been set.
692 */
693 msg_scroll = FALSE;
694
695#ifdef FEAT_GUI
696 /* When 'mousefocus' is set a mouse movement may have taken us to
697 * another window. "need_mouse_correct" may then be set because of an
698 * autocommand. */
699 if (need_mouse_correct)
700 gui_mouse_correct();
701#endif
702
703#ifdef FEAT_FOLDING
704 /* Open fold at the cursor line, according to 'foldopen'. */
705 if (fdo_flags & FDO_INSERT)
706 foldOpenCursor();
707 /* Close folds where the cursor isn't, according to 'foldclose' */
708 if (!char_avail())
709 foldCheckClose();
710#endif
711
Bram Moolenaarf2732452018-06-03 14:47:35 +0200712#ifdef FEAT_JOB_CHANNEL
713 if (bt_prompt(curbuf))
714 {
715 init_prompt(cmdchar_todo);
716 cmdchar_todo = NUL;
717 }
718#endif
719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 /*
721 * If we inserted a character at the last position of the last line in
722 * the window, scroll the window one line up. This avoids an extra
723 * redraw.
724 * This is detected when the cursor column is smaller after inserting
725 * something.
726 * Don't do this when the topline changed already, it has
727 * already been adjusted (by insertchar() calling open_line())).
728 */
729 if (curbuf->b_mod_set
730 && curwin->w_p_wrap
731 && !did_backspace
732 && curwin->w_topline == old_topline
733#ifdef FEAT_DIFF
734 && curwin->w_topfill == old_topfill
735#endif
736 )
737 {
738 mincol = curwin->w_wcol;
739 validate_cursor_col();
740
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200741 if (
742#ifdef FEAT_VARTABS
743 (int)curwin->w_wcol < mincol - tabstop_at(
744 get_nolist_virtcol(), curbuf->b_p_ts,
745 curbuf->b_p_vts_array)
746#else
747 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 && curwin->w_wrow == W_WINROW(curwin)
750 + curwin->w_height - 1 - p_so
751 && (curwin->w_cursor.lnum != curwin->w_topline
752#ifdef FEAT_DIFF
753 || curwin->w_topfill > 0
754#endif
755 ))
756 {
757#ifdef FEAT_DIFF
758 if (curwin->w_topfill > 0)
759 --curwin->w_topfill;
760 else
761#endif
762#ifdef FEAT_FOLDING
763 if (hasFolding(curwin->w_topline, NULL, &old_topline))
764 set_topline(curwin, old_topline + 1);
765 else
766#endif
767 set_topline(curwin, curwin->w_topline + 1);
768 }
769 }
770
771 /* May need to adjust w_topline to show the cursor. */
772 update_topline();
773
774 did_backspace = FALSE;
775
776 validate_cursor(); /* may set must_redraw */
777
778 /*
779 * Redraw the display when no characters are waiting.
780 * Also shows mode, ruler and positions cursor.
781 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000782 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (curwin->w_p_scb)
785 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
Bram Moolenaar860cae12010-06-05 23:22:07 +0200787 if (curwin->w_p_crb)
788 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 update_curswant();
790 old_topline = curwin->w_topline;
791#ifdef FEAT_DIFF
792 old_topfill = curwin->w_topfill;
793#endif
794
795#ifdef USE_ON_FLY_SCROLL
796 dont_scroll = FALSE; /* allow scrolling here */
797#endif
798
799 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100800 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100802 if (c != K_CURSORHOLD)
803 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200804
805 /* After using CTRL-G U the next cursor key will not break undo. */
806 if (dont_sync_undo == MAYBE)
807 dont_sync_undo = TRUE;
808 else
809 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100810 if (cmdchar == K_PS)
811 /* Got here from normal mode when bracketed paste started. */
812 c = K_PS;
813 else
814 do
815 {
816 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200817
818 if (stop_insert_mode)
819 {
820 // Insert mode ended, possibly from a callback.
821 count = 0;
822 nomove = TRUE;
823 goto doESCkey;
824 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100825 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000827 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
828 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#ifdef FEAT_RIGHTLEFT
831 if (p_hkmap && KeyTyped)
832 c = hkmap(c); /* Hebrew mode mapping */
833#endif
834#ifdef FEAT_FKMAP
835 if (p_fkmap && KeyTyped)
836 c = fkmap(c); /* Farsi mode mapping */
837#endif
838
839#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000840 /*
841 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000842 * and the cursor is still in the completed word. Only when there is
843 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000845 if (compl_started
846 && pum_wanted()
847 && curwin->w_cursor.col >= compl_col
848 && (compl_shown_match == NULL
849 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000850 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000851 /* BS: Delete one character from "compl_leader". */
852 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000853 && curwin->w_cursor.col > compl_col
854 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000855 continue;
856
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000857 /* When no match was selected or it was edited. */
858 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000859 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000860 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000861 * "compl_leader". Except when at the original match and
862 * there is nothing to add, CTRL-L works like CTRL-P then. */
863 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100864 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000865 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000866 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000867 {
868 ins_compl_addfrommatch();
869 continue;
870 }
871
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000872 /* A non-white character that fits in with the current
873 * completion: Add to "compl_leader". */
874 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000875 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100876#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100877 /* Trigger InsertCharPre. */
878 char_u *str = do_insert_char_pre(c);
879 char_u *p;
880
881 if (str != NULL)
882 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100883 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100884 ins_compl_addleader(PTR2CHAR(p));
885 vim_free(str);
886 }
887 else
888#endif
889 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000890 continue;
891 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000892
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000893 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000894 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200895 if ((c == Ctrl_Y || (compl_enter_selects
896 && (c == CAR || c == K_KENTER || c == NL)))
897 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000898 {
899 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200900 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000901 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000902 }
903 }
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
906 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000907 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000908 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000909 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#endif
911
Bram Moolenaar488c6512005-08-11 20:09:58 +0000912 /* CTRL-\ CTRL-N goes to Normal mode,
913 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
914 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (c == Ctrl_BSL)
916 {
917 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000918 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 ++no_mapping;
920 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000921 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 --no_mapping;
923 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000924 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000926 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 vungetc(c);
928 c = Ctrl_BSL;
929 }
930 else if (c == Ctrl_G && p_im)
931 continue;
932 else
933 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000934 if (c == Ctrl_O)
935 {
936 ins_ctrl_o();
937 ins_at_eol = FALSE; /* cursor keeps its column */
938 nomove = TRUE;
939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 count = 0;
941 goto doESCkey;
942 }
943 }
944
945#ifdef FEAT_DIGRAPHS
946 c = do_digraph(c);
947#endif
948
949#ifdef FEAT_INS_EXPAND
950 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
951 goto docomplete;
952#endif
953 if (c == Ctrl_V || c == Ctrl_Q)
954 {
955 ins_ctrl_v();
956 c = Ctrl_V; /* pretend CTRL-V is last typed character */
957 continue;
958 }
959
960#ifdef FEAT_CINDENT
961 if (cindent_on()
962# ifdef FEAT_INS_EXPAND
963 && ctrl_x_mode == 0
964# endif
965 )
966 {
967 /* A key name preceded by a bang means this key is not to be
968 * inserted. Skip ahead to the re-indenting below.
969 * A key name preceded by a star means that indenting has to be
970 * done before inserting the key. */
971 line_is_white = inindent(0);
972 if (in_cinkeys(c, '!', line_is_white))
973 goto force_cindent;
974 if (can_cindent && in_cinkeys(c, '*', line_is_white)
975 && stop_arrow() == OK)
976 do_c_expr_indent();
977 }
978#endif
979
980#ifdef FEAT_RIGHTLEFT
981 if (curwin->w_p_rl)
982 switch (c)
983 {
984 case K_LEFT: c = K_RIGHT; break;
985 case K_S_LEFT: c = K_S_RIGHT; break;
986 case K_C_LEFT: c = K_C_RIGHT; break;
987 case K_RIGHT: c = K_LEFT; break;
988 case K_S_RIGHT: c = K_S_LEFT; break;
989 case K_C_RIGHT: c = K_C_LEFT; break;
990 }
991#endif
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 /*
994 * If 'keymodel' contains "startsel", may start selection. If it
995 * does, a CTRL-O and c will be stuffed, we need to get these
996 * characters.
997 */
998 if (ins_start_select(c))
999 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001 /*
1002 * The big switch to handle a character in insert mode.
1003 */
1004 switch (c)
1005 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001006 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (echeck_abbr(ESC + ABBR_OFF))
1008 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001009 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001011 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#ifdef FEAT_CMDWIN
1013 if (c == Ctrl_C && cmdwin_type != 0)
1014 {
1015 /* Close the cmdline window. */
1016 cmdwin_result = K_IGNORE;
1017 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001018 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 goto doESCkey;
1020 }
1021#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001022#ifdef FEAT_JOB_CHANNEL
1023 if (c == Ctrl_C && bt_prompt(curbuf))
1024 {
1025 if (invoke_prompt_interrupt())
1026 {
1027 if (!bt_prompt(curbuf))
1028 // buffer changed to a non-prompt buffer, get out of
1029 // Insert mode
1030 goto doESCkey;
1031 break;
1032 }
1033 }
1034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036#ifdef UNIX
1037do_intr:
1038#endif
1039 /* when 'insertmode' set, and not halfway a mapping, don't leave
1040 * Insert mode */
1041 if (goto_im())
1042 {
1043 if (got_int)
1044 {
1045 (void)vgetc(); /* flush all buffers */
1046 got_int = FALSE;
1047 }
1048 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001049 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 break;
1051 }
1052doESCkey:
1053 /*
1054 * This is the ONLY return from edit()!
1055 */
1056 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1057 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001058 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 o_lnum = curwin->w_cursor.lnum;
1060
Bram Moolenaar488c6512005-08-11 20:09:58 +00001061 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001062 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00001063 if (cmdchar != 'r' && cmdchar != 'v')
1064 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1065 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001066 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 continue;
1070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 case Ctrl_Z: /* suspend when 'insertmode' set */
1072 if (!p_im)
1073 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001074 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001075#ifdef CURSOR_SHAPE
1076 ui_cursor_shape(); /* may need to update cursor shape */
1077#endif
1078 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001079
1080 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001081#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001082 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 goto docomplete;
1084#endif
1085 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1086 break;
1087 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001088
1089#ifdef FEAT_VIRTUALEDIT
1090 /* don't move the cursor left when 'virtualedit' has "onemore". */
1091 if (ve_flags & VE_ONEMORE)
1092 {
1093 ins_at_eol = FALSE;
1094 nomove = TRUE;
1095 }
1096#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001097 count = 0;
1098 goto doESCkey;
1099
Bram Moolenaar572cb562005-08-05 21:35:02 +00001100 case K_INS: /* toggle insert/replace mode */
1101 case K_KINS:
1102 ins_insert(replaceState);
1103 break;
1104
1105 case K_SELECT: /* end of Select mode mapping - ignore */
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case K_HELP: /* Help key works like <ESC> <Help> */
1109 case K_F1:
1110 case K_XF1:
1111 stuffcharReadbuff(K_HELP);
1112 if (p_im)
1113 need_start_insertmode = TRUE;
1114 goto doESCkey;
1115
1116#ifdef FEAT_NETBEANS_INTG
1117 case K_F21: /* NetBeans command */
1118 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001119 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001120 --no_mapping;
1121 netbeans_keycommand(i);
1122 break;
1123#endif
1124
1125 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 case NUL:
1127 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001128 /* For ^@ the trailing ESC will end the insert, unless there is an
1129 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1131 && c != Ctrl_A && !p_im)
1132 goto doESCkey; /* quit insert mode */
1133 inserted_space = FALSE;
1134 break;
1135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 ins_reg();
1138 auto_format(FALSE, TRUE);
1139 inserted_space = FALSE;
1140 break;
1141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 ins_ctrl_g();
1144 break;
1145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 case Ctrl_HAT: /* switch input mode and/or langmap */
1147 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 break;
1149
1150#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if (!p_ari)
1153 goto normalchar;
1154 ins_ctrl_();
1155 break;
1156#endif
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1160 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1161 goto docomplete;
1162#endif
1163 /* FALLTHROUGH */
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166# ifdef FEAT_INS_EXPAND
1167 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1168 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 if (has_compl_option(FALSE))
1170 goto docomplete;
1171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173# endif
1174 ins_shift(c, lastc);
1175 auto_format(FALSE, TRUE);
1176 inserted_space = FALSE;
1177 break;
1178
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 case K_KDEL:
1181 ins_del();
1182 auto_format(FALSE, TRUE);
1183 break;
1184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001185 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 case Ctrl_H:
1187 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1188 auto_format(FALSE, TRUE);
1189 break;
1190
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001191 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001192#ifdef FEAT_JOB_CHANNEL
1193 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1194 {
1195 // In a prompt window CTRL-W is used for window commands.
1196 // Use Shift-CTRL-W to delete a word.
1197 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001198 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001199 nomove = TRUE;
1200 count = 0;
1201 goto doESCkey;
1202 }
1203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1205 auto_format(FALSE, TRUE);
1206 break;
1207
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001208 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001209# ifdef FEAT_COMPL_FUNC
1210 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001211 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001212 goto docomplete;
1213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1215 auto_format(FALSE, TRUE);
1216 inserted_space = FALSE;
1217 break;
1218
1219#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001220 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 case K_LEFTMOUSE_NM:
1222 case K_LEFTDRAG:
1223 case K_LEFTRELEASE:
1224 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001225 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 case K_MIDDLEMOUSE:
1227 case K_MIDDLEDRAG:
1228 case K_MIDDLERELEASE:
1229 case K_RIGHTMOUSE:
1230 case K_RIGHTDRAG:
1231 case K_RIGHTRELEASE:
1232 case K_X1MOUSE:
1233 case K_X1DRAG:
1234 case K_X1RELEASE:
1235 case K_X2MOUSE:
1236 case K_X2DRAG:
1237 case K_X2RELEASE:
1238 ins_mouse(c);
1239 break;
1240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001241 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001242 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 break;
1244
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001245 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001246 ins_mousescroll(MSCR_UP);
1247 break;
1248
1249 case K_MOUSELEFT: /* Scroll wheel left */
1250 ins_mousescroll(MSCR_LEFT);
1251 break;
1252
1253 case K_MOUSERIGHT: /* Scroll wheel right */
1254 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 break;
1256#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001257 case K_PS:
1258 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1259 if (cmdchar == K_PS)
1260 /* invoked from normal mode, bail out */
1261 goto doESCkey;
1262 break;
1263 case K_PE:
1264 /* Got K_PE without K_PS, ignore. */
1265 break;
1266
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001267#ifdef FEAT_GUI_TABLINE
1268 case K_TABLINE:
1269 case K_TABMENU:
1270 ins_tabline(c);
1271 break;
1272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001274 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 break;
1276
Bram Moolenaar754b5602006-02-09 23:53:20 +00001277 case K_CURSORHOLD: /* Didn't type something for a while. */
1278 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1279 did_cursorhold = TRUE;
1280 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001281
Bram Moolenaar4770d092006-01-12 23:22:24 +00001282#ifdef FEAT_GUI_W32
1283 /* On Win32 ignore <M-F4>, we get it when closing the window was
1284 * cancelled. */
1285 case K_F4:
1286 if (mod_mask != MOD_MASK_ALT)
1287 goto normalchar;
1288 break;
1289#endif
1290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291#ifdef FEAT_GUI
1292 case K_VER_SCROLLBAR:
1293 ins_scroll();
1294 break;
1295
1296 case K_HOR_SCROLLBAR:
1297 ins_horscroll();
1298 break;
1299#endif
1300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001301 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 case K_S_HOME:
1304 case K_C_HOME:
1305 ins_home(c);
1306 break;
1307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 case K_S_END:
1311 case K_C_END:
1312 ins_end(c);
1313 break;
1314
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001315 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001316 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1317 ins_s_left();
1318 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001319 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 break;
1321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001322 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 case K_C_LEFT:
1324 ins_s_left();
1325 break;
1326
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001327 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001328 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1329 ins_s_right();
1330 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001331 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 break;
1333
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001334 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 case K_C_RIGHT:
1336 ins_s_right();
1337 break;
1338
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001340#ifdef FEAT_INS_EXPAND
1341 if (pum_visible())
1342 goto docomplete;
1343#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001344 if (mod_mask & MOD_MASK_SHIFT)
1345 ins_pageup();
1346 else
1347 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 break;
1349
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001350 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 case K_PAGEUP:
1352 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001353#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001354 if (pum_visible())
1355 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 ins_pageup();
1358 break;
1359
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001360 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001361#ifdef FEAT_INS_EXPAND
1362 if (pum_visible())
1363 goto docomplete;
1364#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001365 if (mod_mask & MOD_MASK_SHIFT)
1366 ins_pagedown();
1367 else
1368 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 break;
1370
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001371 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 case K_PAGEDOWN:
1373 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001374#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001375 if (pum_visible())
1376 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 ins_pagedown();
1379 break;
1380
1381#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001382 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 ins_drop();
1384 break;
1385#endif
1386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001387 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 c = TAB;
1389 /* FALLTHROUGH */
1390
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001391 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1393 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1394 goto docomplete;
1395#endif
1396 inserted_space = FALSE;
1397 if (ins_tab())
1398 goto normalchar; /* insert TAB as a normal char */
1399 auto_format(FALSE, TRUE);
1400 break;
1401
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001402 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 c = CAR;
1404 /* FALLTHROUGH */
1405 case CAR:
1406 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001407#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /* In a quickfix window a <CR> jumps to the error under the
1409 * cursor. */
1410 if (bt_quickfix(curbuf) && c == CAR)
1411 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001412 if (curwin->w_llist_ref == NULL) /* quickfix window */
1413 do_cmdline_cmd((char_u *)".cc");
1414 else /* location list window */
1415 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 break;
1417 }
1418#endif
1419#ifdef FEAT_CMDWIN
1420 if (cmdwin_type != 0)
1421 {
1422 /* Execute the command in the cmdline window. */
1423 cmdwin_result = CAR;
1424 goto doESCkey;
1425 }
1426#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001427#ifdef FEAT_JOB_CHANNEL
1428 if (bt_prompt(curbuf))
1429 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001430 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001431 if (!bt_prompt(curbuf))
1432 // buffer changed to a non-prompt buffer, get out of
1433 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001434 goto doESCkey;
1435 break;
1436 }
1437#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001438 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 goto doESCkey; /* out of memory */
1440 auto_format(FALSE, FALSE);
1441 inserted_space = FALSE;
1442 break;
1443
Bram Moolenaar860cae12010-06-05 23:22:07 +02001444#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001445 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446# ifdef FEAT_INS_EXPAND
1447 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1448 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001449 if (has_compl_option(TRUE))
1450 goto docomplete;
1451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
1453# endif
1454# ifdef FEAT_DIGRAPHS
1455 c = ins_digraph();
1456 if (c == NUL)
1457 break;
1458# endif
1459 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
1462#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001463 case Ctrl_X: /* Enter CTRL-X mode */
1464 ins_ctrl_x();
1465 break;
1466
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001467 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (ctrl_x_mode != CTRL_X_TAGS)
1469 goto normalchar;
1470 goto docomplete;
1471
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001472 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (ctrl_x_mode != CTRL_X_FILES)
1474 goto normalchar;
1475 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001476
1477 case 's': /* Spelling completion after ^X */
1478 case Ctrl_S:
1479 if (ctrl_x_mode != CTRL_X_SPELL)
1480 goto normalchar;
1481 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482#endif
1483
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001484 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485#ifdef FEAT_INS_EXPAND
1486 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1487#endif
1488 {
1489 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1490 if (p_im)
1491 {
1492 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1493 break;
1494 goto doESCkey;
1495 }
1496 goto normalchar;
1497 }
1498#ifdef FEAT_INS_EXPAND
1499 /* FALLTHROUGH */
1500
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001501 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 case Ctrl_N:
1503 /* if 'complete' is empty then plain ^P is no longer special,
1504 * but it is under other ^X modes */
1505 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001506 && (ctrl_x_mode == CTRL_X_NORMAL
1507 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001508 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 goto normalchar;
1510
1511docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001512 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001513#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001514 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001515#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001516 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001517 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001518#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001519 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001520#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001521 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 break;
1523#endif /* FEAT_INS_EXPAND */
1524
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001525 case Ctrl_Y: /* copy from previous line or scroll down */
1526 case Ctrl_E: /* copy from next line or scroll up */
1527 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 break;
1529
1530 default:
1531#ifdef UNIX
1532 if (c == intr_char) /* special interrupt char */
1533 goto do_intr;
1534#endif
1535
Bram Moolenaare659c952011-05-19 17:25:41 +02001536normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001538 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001540#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001541 if (!p_paste)
1542 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001543 /* Trigger InsertCharPre. */
1544 char_u *str = do_insert_char_pre(c);
1545 char_u *p;
1546
1547 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001548 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001549 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001550 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001551 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001552 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001553 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001554 c = PTR2CHAR(p);
1555 if (c == CAR || c == K_KENTER || c == NL)
1556 ins_eol(c);
1557 else
1558 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001559 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001560 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001561 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001562 vim_free(str);
1563 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001564 }
1565
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001566 /* If the new value is already inserted or an empty string
1567 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001568 if (c == NUL)
1569 break;
1570 }
1571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572#ifdef FEAT_SMARTINDENT
1573 /* Try to perform smart-indenting. */
1574 ins_try_si(c);
1575#endif
1576
1577 if (c == ' ')
1578 {
1579 inserted_space = TRUE;
1580#ifdef FEAT_CINDENT
1581 if (inindent(0))
1582 can_cindent = FALSE;
1583#endif
1584 if (Insstart_blank_vcol == MAXCOL
1585 && curwin->w_cursor.lnum == Insstart.lnum)
1586 Insstart_blank_vcol = get_nolist_virtcol();
1587 }
1588
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001589 /* Insert a normal character and check for abbreviations on a
1590 * special character. Let CTRL-] expand abbreviations without
1591 * inserting it. */
1592 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593#ifdef FEAT_MBYTE
1594 /* Add ABBR_OFF for characters above 0x100, this is
1595 * what check_abbr() expects. */
1596 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1597#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001598 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600 insert_special(c, FALSE, FALSE);
1601#ifdef FEAT_RIGHTLEFT
1602 revins_legal++;
1603 revins_chars++;
1604#endif
1605 }
1606
1607 auto_format(FALSE, TRUE);
1608
1609#ifdef FEAT_FOLDING
1610 /* When inserting a character the cursor line must never be in a
1611 * closed fold. */
1612 foldOpenCursor();
1613#endif
1614 break;
1615 } /* end of switch (c) */
1616
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001617 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001618 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001619#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001620 /* but not in CTRL-X mode, a script can't restore the state */
1621 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001622#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001623 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001624 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001625
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 /* If the cursor was moved we didn't just insert a space */
1627 if (arrow_used)
1628 inserted_space = FALSE;
1629
1630#ifdef FEAT_CINDENT
1631 if (can_cindent && cindent_on()
1632# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001633 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634# endif
1635 )
1636 {
1637force_cindent:
1638 /*
1639 * Indent now if a key was typed that is in 'cinkeys'.
1640 */
1641 if (in_cinkeys(c, ' ', line_is_white))
1642 {
1643 if (stop_arrow() == OK)
1644 /* re-indent the current line */
1645 do_c_expr_indent();
1646 }
1647 }
1648#endif /* FEAT_CINDENT */
1649
1650 } /* for (;;) */
1651 /* NOTREACHED */
1652}
1653
1654/*
1655 * Redraw for Insert mode.
1656 * This is postponed until getting the next character to make '$' in the 'cpo'
1657 * option work correctly.
1658 * Only redraw when there are no characters available. This speeds up
1659 * inserting sequences of characters (e.g., for CTRL-R).
1660 */
1661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001662ins_redraw(
1663 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001665#ifdef FEAT_CONCEAL
1666 linenr_T conceal_old_cursor_line = 0;
1667 linenr_T conceal_new_cursor_line = 0;
1668 int conceal_update_lines = FALSE;
1669#endif
1670
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001671 if (char_avail())
1672 return;
1673
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001675 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1676 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001677 if (ready && (has_cursormovedI()
1678# if defined(FEAT_CONCEAL)
1679 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001680# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001681 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001682 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001683# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001684 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001685# endif
1686 )
1687 {
1688# ifdef FEAT_SYN_HL
1689 /* Need to update the screen first, to make sure syntax
1690 * highlighting is correct after making a change (e.g., inserting
1691 * a "(". The autocommand may also require a redraw, so it's done
1692 * again below, unfortunately. */
1693 if (syntax_present(curwin) && must_redraw)
1694 update_screen(0);
1695# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001696 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001697 {
1698 /* Make sure curswant is correct, an autocommand may call
1699 * getcurpos(). */
1700 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001701 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001702 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001703# ifdef FEAT_CONCEAL
1704 if (curwin->w_p_cole > 0)
1705 {
1706 conceal_old_cursor_line = last_cursormoved.lnum;
1707 conceal_new_cursor_line = curwin->w_cursor.lnum;
1708 conceal_update_lines = TRUE;
1709 }
1710# endif
1711 last_cursormoved = curwin->w_cursor;
1712 }
1713#endif
1714
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001715 /* Trigger TextChangedI if b_changedtick differs. */
1716 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001717 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001718#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001719 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001720#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001721 )
1722 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001723 aco_save_T aco;
1724
Bram Moolenaaree380ae2018-08-07 19:32:52 +02001725#ifdef FEAT_EVAL
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001726 // Sync undo when the autocommand calls setline() or append(), so that
1727 // it can be undone separately.
1728 u_sync_once = 2;
Bram Moolenaaree380ae2018-08-07 19:32:52 +02001729#endif
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001730
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001731 // 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_TEXTCHANGEDI, 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 = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001736
Bram Moolenaaree380ae2018-08-07 19:32:52 +02001737#ifdef FEAT_EVAL
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001738 if (u_sync_once == 1)
1739 ins_need_undo = TRUE;
1740 u_sync_once = 0;
Bram Moolenaaree380ae2018-08-07 19:32:52 +02001741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001743
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001744#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001745 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1746 * TextChangedI will need to trigger for backwards compatibility, thus use
1747 * different b_last_changedtick* variables. */
1748 if (ready && has_textchangedP()
1749 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1750 && pum_visible())
1751 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001752 aco_save_T aco;
1753
1754 // save and restore curwin and curbuf, in case the autocmd changes them
1755 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001756 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001757 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001758 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1759 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001760#endif
1761
1762 if (must_redraw)
1763 update_screen(0);
1764 else if (clear_cmdline || redraw_cmdline)
1765 showmode(); /* clear cmdline and show mode */
1766# if defined(FEAT_CONCEAL)
1767 if ((conceal_update_lines
1768 && (conceal_old_cursor_line != conceal_new_cursor_line
1769 || conceal_cursor_line(curwin)))
1770 || need_cursor_line_redraw)
1771 {
1772 if (conceal_old_cursor_line != conceal_new_cursor_line)
1773 update_single_line(curwin, conceal_old_cursor_line);
1774 update_single_line(curwin, conceal_new_cursor_line == 0
1775 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1776 curwin->w_valid &= ~VALID_CROW;
1777 }
1778# endif
1779 showruler(FALSE);
1780 setcursor();
1781 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782}
1783
1784/*
1785 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1786 */
1787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001791 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
1793 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001794 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001797 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001799 did_putchar = TRUE;
1800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1802
1803#ifdef FEAT_CMDL_INFO
1804 add_to_showcmd_c(Ctrl_V);
1805#endif
1806
1807 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001808 if (did_putchar)
1809 /* when the line fits in 'columns' the '^' is at the start of the next
1810 * line and will not removed by the redraw */
1811 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812#ifdef FEAT_CMDL_INFO
1813 clear_showcmd();
1814#endif
1815 insert_special(c, FALSE, TRUE);
1816#ifdef FEAT_RIGHTLEFT
1817 revins_chars++;
1818 revins_legal++;
1819#endif
1820}
1821
1822/*
1823 * Put a character directly onto the screen. It's not stored in a buffer.
1824 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1825 */
1826static int pc_status;
1827#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1828#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1829#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1830#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832static int pc_attr;
1833static int pc_row;
1834static int pc_col;
1835
1836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838{
1839 int attr;
1840
1841 if (ScreenLines != NULL)
1842 {
1843 update_topline(); /* just in case w_topline isn't valid */
1844 validate_cursor();
1845 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001846 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 else
1848 attr = 0;
1849 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001850 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1852 pc_status = PC_STATUS_UNSET;
1853#endif
1854#ifdef FEAT_RIGHTLEFT
1855 if (curwin->w_p_rl)
1856 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001857 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858# ifdef FEAT_MBYTE
1859 if (has_mbyte)
1860 {
1861 int fix_col = mb_fix_col(pc_col, pc_row);
1862
1863 if (fix_col != pc_col)
1864 {
1865 screen_putchar(' ', pc_row, fix_col, attr);
1866 --curwin->w_wcol;
1867 pc_status = PC_STATUS_RIGHT;
1868 }
1869 }
1870# endif
1871 }
1872 else
1873#endif
1874 {
1875 pc_col += curwin->w_wcol;
1876#ifdef FEAT_MBYTE
1877 if (mb_lefthalve(pc_row, pc_col))
1878 pc_status = PC_STATUS_LEFT;
1879#endif
1880 }
1881
1882 /* save the character to be able to put it back */
1883#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1884 if (pc_status == PC_STATUS_UNSET)
1885#endif
1886 {
1887 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1888 pc_status = PC_STATUS_SET;
1889 }
1890 screen_putchar(c, pc_row, pc_col, attr);
1891 }
1892}
1893
Bram Moolenaarf2732452018-06-03 14:47:35 +02001894#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1895/*
1896 * Return the effective prompt for the current buffer.
1897 */
1898 char_u *
1899prompt_text(void)
1900{
1901 if (curbuf->b_prompt_text == NULL)
1902 return (char_u *)"% ";
1903 return curbuf->b_prompt_text;
1904}
1905
1906/*
1907 * Prepare for prompt mode: Make sure the last line has the prompt text.
1908 * Move the cursor to this line.
1909 */
1910 static void
1911init_prompt(int cmdchar_todo)
1912{
1913 char_u *prompt = prompt_text();
1914 char_u *text;
1915
1916 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1917 text = ml_get_curline();
1918 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1919 {
1920 // prompt is missing, insert it or append a line with it
1921 if (*text == NUL)
1922 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1923 else
1924 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1925 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1926 coladvance((colnr_T)MAXCOL);
1927 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1928 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001929
1930 // Insert always starts after the prompt, allow editing text after it.
1931 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1932 || Insstart_orig.col != (int)STRLEN(prompt))
1933 {
1934 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001935 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001936 Insstart_orig = Insstart;
1937 Insstart_textlen = Insstart.col;
1938 Insstart_blank_vcol = MAXCOL;
1939 arrow_used = FALSE;
1940 }
1941
Bram Moolenaarf2732452018-06-03 14:47:35 +02001942 if (cmdchar_todo == 'A')
1943 coladvance((colnr_T)MAXCOL);
1944 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001945 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001946 /* Make sure the cursor is in a valid position. */
1947 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001948}
1949
1950/*
1951 * Return TRUE if the cursor is in the editable position of the prompt line.
1952 */
1953 int
1954prompt_curpos_editable()
1955{
1956 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1957 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1958}
1959#endif
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961/*
1962 * Undo the previous edit_putchar().
1963 */
1964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001965edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1968 {
1969#if defined(FEAT_MBYTE)
1970 if (pc_status == PC_STATUS_RIGHT)
1971 ++curwin->w_wcol;
1972 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1973 redrawWinline(curwin->w_cursor.lnum, FALSE);
1974 else
1975#endif
1976 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1977 }
1978}
1979
1980/*
1981 * Called when p_dollar is set: display a '$' at the end of the changed text
1982 * Only works when cursor is in the line that changes.
1983 */
1984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001985display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
1987 colnr_T save_col;
1988
1989 if (!redrawing())
1990 return;
1991
1992 cursor_off();
1993 save_col = curwin->w_cursor.col;
1994 curwin->w_cursor.col = col;
1995#ifdef FEAT_MBYTE
1996 if (has_mbyte)
1997 {
1998 char_u *p;
1999
2000 /* If on the last byte of a multi-byte move to the first byte. */
2001 p = ml_get_curline();
2002 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
2003 }
2004#endif
2005 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02002006 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 edit_putchar('$', FALSE);
2009 dollar_vcol = curwin->w_virtcol;
2010 }
2011 curwin->w_cursor.col = save_col;
2012}
2013
2014/*
2015 * Call this function before moving the cursor from the normal insert position
2016 * in insert mode.
2017 */
2018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002019undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002021 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002023 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 redrawWinline(curwin->w_cursor.lnum, FALSE);
2025 }
2026}
2027
2028/*
2029 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
2030 * Keep the cursor on the same character.
2031 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2032 * type == INDENT_DEC decrease indent (for CTRL-D)
2033 * type == INDENT_SET set indent to "amount"
2034 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2035 */
2036 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002037change_indent(
2038 int type,
2039 int amount,
2040 int round,
2041 int replaced, /* replaced character, put on replace stack */
2042 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
2044 int vcol;
2045 int last_vcol;
2046 int insstart_less; /* reduction for Insstart.col */
2047 int new_cursor_col;
2048 int i;
2049 char_u *ptr;
2050 int save_p_list;
2051 int start_col;
2052 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 colnr_T orig_col = 0; /* init for GCC */
2054 char_u *new_line, *orig_line = NULL; /* init for GCC */
2055
2056 /* VREPLACE mode needs to know what the line was like before changing */
2057 if (State & VREPLACE_FLAG)
2058 {
2059 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2060 orig_col = curwin->w_cursor.col;
2061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
2063 /* for the following tricks we don't want list mode */
2064 save_p_list = curwin->w_p_list;
2065 curwin->w_p_list = FALSE;
2066 vc = getvcol_nolist(&curwin->w_cursor);
2067 vcol = vc;
2068
2069 /*
2070 * For Replace mode we need to fix the replace stack later, which is only
2071 * possible when the cursor is in the indent. Remember the number of
2072 * characters before the cursor if it's possible.
2073 */
2074 start_col = curwin->w_cursor.col;
2075
2076 /* determine offset from first non-blank */
2077 new_cursor_col = curwin->w_cursor.col;
2078 beginline(BL_WHITE);
2079 new_cursor_col -= curwin->w_cursor.col;
2080
2081 insstart_less = curwin->w_cursor.col;
2082
2083 /*
2084 * If the cursor is in the indent, compute how many screen columns the
2085 * cursor is to the left of the first non-blank.
2086 */
2087 if (new_cursor_col < 0)
2088 vcol = get_indent() - vcol;
2089
2090 if (new_cursor_col > 0) /* can't fix replace stack */
2091 start_col = -1;
2092
2093 /*
2094 * Set the new indent. The cursor will be put on the first non-blank.
2095 */
2096 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002097 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 else
2099 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 int save_State = State;
2101
2102 /* Avoid being called recursively. */
2103 if (State & VREPLACE_FLAG)
2104 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002105 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
2108 insstart_less -= curwin->w_cursor.col;
2109
2110 /*
2111 * Try to put cursor on same character.
2112 * If the cursor is at or after the first non-blank in the line,
2113 * compute the cursor column relative to the column of the first
2114 * non-blank character.
2115 * If we are not in insert mode, leave the cursor on the first non-blank.
2116 * If the cursor is before the first non-blank, position it relative
2117 * to the first non-blank, counted in screen columns.
2118 */
2119 if (new_cursor_col >= 0)
2120 {
2121 /*
2122 * When changing the indent while the cursor is touching it, reset
2123 * Insstart_col to 0.
2124 */
2125 if (new_cursor_col == 0)
2126 insstart_less = MAXCOL;
2127 new_cursor_col += curwin->w_cursor.col;
2128 }
2129 else if (!(State & INSERT))
2130 new_cursor_col = curwin->w_cursor.col;
2131 else
2132 {
2133 /*
2134 * Compute the screen column where the cursor should be.
2135 */
2136 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002137 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
2139 /*
2140 * Advance the cursor until we reach the right screen column.
2141 */
2142 vcol = last_vcol = 0;
2143 new_cursor_col = -1;
2144 ptr = ml_get_curline();
2145 while (vcol <= (int)curwin->w_virtcol)
2146 {
2147 last_vcol = vcol;
2148#ifdef FEAT_MBYTE
2149 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002150 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 else
2152#endif
2153 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002154 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 vcol = last_vcol;
2157
2158 /*
2159 * May need to insert spaces to be able to position the cursor on
2160 * the right screen column.
2161 */
2162 if (vcol != (int)curwin->w_virtcol)
2163 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002164 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002166 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 if (ptr != NULL)
2168 {
2169 new_cursor_col += i;
2170 ptr[i] = NUL;
2171 while (--i >= 0)
2172 ptr[i] = ' ';
2173 ins_str(ptr);
2174 vim_free(ptr);
2175 }
2176 }
2177
2178 /*
2179 * When changing the indent while the cursor is in it, reset
2180 * Insstart_col to 0.
2181 */
2182 insstart_less = MAXCOL;
2183 }
2184
2185 curwin->w_p_list = save_p_list;
2186
2187 if (new_cursor_col <= 0)
2188 curwin->w_cursor.col = 0;
2189 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002190 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 curwin->w_set_curswant = TRUE;
2192 changed_cline_bef_curs();
2193
2194 /*
2195 * May have to adjust the start of the insert.
2196 */
2197 if (State & INSERT)
2198 {
2199 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2200 {
2201 if ((int)Insstart.col <= insstart_less)
2202 Insstart.col = 0;
2203 else
2204 Insstart.col -= insstart_less;
2205 }
2206 if ((int)ai_col <= insstart_less)
2207 ai_col = 0;
2208 else
2209 ai_col -= insstart_less;
2210 }
2211
2212 /*
2213 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2214 * If the number of characters before the cursor decreased, need to pop a
2215 * few characters from the replace stack.
2216 * If the number of characters before the cursor increased, need to push a
2217 * few NULs onto the replace stack.
2218 */
2219 if (REPLACE_NORMAL(State) && start_col >= 0)
2220 {
2221 while (start_col > (int)curwin->w_cursor.col)
2222 {
2223 replace_join(0); /* remove a NUL from the replace stack */
2224 --start_col;
2225 }
2226 while (start_col < (int)curwin->w_cursor.col || replaced)
2227 {
2228 replace_push(NUL);
2229 if (replaced)
2230 {
2231 replace_push(replaced);
2232 replaced = NUL;
2233 }
2234 ++start_col;
2235 }
2236 }
2237
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 /*
2239 * For VREPLACE mode, we also have to fix the replace stack. In this case
2240 * it is always possible because we backspace over the whole line and then
2241 * put it back again the way we wanted it.
2242 */
2243 if (State & VREPLACE_FLAG)
2244 {
2245 /* If orig_line didn't allocate, just return. At least we did the job,
2246 * even if you can't backspace. */
2247 if (orig_line == NULL)
2248 return;
2249
2250 /* Save new line */
2251 new_line = vim_strsave(ml_get_curline());
2252 if (new_line == NULL)
2253 return;
2254
2255 /* We only put back the new line up to the cursor */
2256 new_line[curwin->w_cursor.col] = NUL;
2257
2258 /* Put back original line */
2259 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2260 curwin->w_cursor.col = orig_col;
2261
2262 /* Backspace from cursor to start of line */
2263 backspace_until_column(0);
2264
2265 /* Insert new stuff into line again */
2266 ins_bytes(new_line);
2267
2268 vim_free(new_line);
2269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270}
2271
2272/*
2273 * Truncate the space at the end of a line. This is to be used only in an
2274 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2275 * modes.
2276 */
2277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002278truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 int i;
2281
2282 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002283 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 if (State & REPLACE_FLAG)
2286 replace_join(0); /* remove a NUL from the replace stack */
2287 }
2288 line[i + 1] = NUL;
2289}
2290
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291/*
2292 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2293 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002294 * Will attempt not to go before "col" even when there is a composing
2295 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 */
2297 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002298backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299{
2300 while ((int)curwin->w_cursor.col > col)
2301 {
2302 curwin->w_cursor.col--;
2303 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002304 replace_do_bs(col);
2305 else if (!del_char_after_col(col))
2306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002310/*
2311 * Like del_char(), but make sure not to go before column "limit_col".
2312 * Only matters when there are composing characters.
2313 * Return TRUE when something was deleted.
2314 */
2315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002316del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002317{
2318#ifdef FEAT_MBYTE
2319 if (enc_utf8 && limit_col >= 0)
2320 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002321 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002322
2323 /* Make sure the cursor is at the start of a character, but
2324 * skip forward again when going too far back because of a
2325 * composing character. */
2326 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002327 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002328 {
2329 int l = utf_ptr2len(ml_get_cursor());
2330
2331 if (l == 0) /* end of line */
2332 break;
2333 curwin->w_cursor.col += l;
2334 }
2335 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2336 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002337 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002338 }
2339 else
2340#endif
2341 (void)del_char(FALSE);
2342 return TRUE;
2343}
2344
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2346/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002347 * CTRL-X pressed in Insert mode.
2348 */
2349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002350ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002351{
2352 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2353 * CTRL-V works like CTRL-N */
2354 if (ctrl_x_mode != CTRL_X_CMDLINE)
2355 {
2356 /* if the next ^X<> won't ADD nothing, then reset
2357 * compl_cont_status */
2358 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002359 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002360 else
2361 compl_cont_status = 0;
2362 /* We're not sure which CTRL-X mode it will be yet */
2363 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2364 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2365 edit_submode_pre = NULL;
2366 showmode();
2367 }
2368}
2369
2370/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002371 * Whether other than default completion has been selected.
2372 */
2373 int
2374ctrl_x_mode_not_default(void)
2375{
2376 return ctrl_x_mode != CTRL_X_NORMAL;
2377}
2378
2379/*
2380 * Whether CTRL-X was typed without a following character.
2381 */
2382 int
2383ctrl_x_mode_not_defined_yet(void)
2384{
2385 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2386}
2387
2388/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002389 * Return TRUE if the 'dict' or 'tsr' option can be used.
2390 */
2391 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002392has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002393{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002394 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002395# ifdef FEAT_SPELL
2396 && !curwin->w_p_spell
2397# endif
2398 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002399 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2400 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002401 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002402 edit_submode = NULL;
2403 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2404 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002405 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002406 if (emsg_silent == 0)
2407 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002408 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002409 setcursor();
2410 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002411#ifdef FEAT_EVAL
2412 if (!get_vim_var_nr(VV_TESTING))
2413#endif
2414 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002415 }
2416 return FALSE;
2417 }
2418 return TRUE;
2419}
2420
2421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2423 * This depends on the current mode.
2424 */
2425 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002426vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
2428 /* Always allow ^R - let it's results then be checked */
2429 if (c == Ctrl_R)
2430 return TRUE;
2431
Bram Moolenaare3226be2005-12-18 22:10:00 +00002432 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002433 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002434 return TRUE;
2435
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 switch (ctrl_x_mode)
2437 {
2438 case 0: /* Not in any CTRL-X mode */
2439 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2440 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002441 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2443 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2444 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002445 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002446 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 case CTRL_X_SCROLL:
2448 return (c == Ctrl_Y || c == Ctrl_E);
2449 case CTRL_X_WHOLE_LINE:
2450 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2451 case CTRL_X_FILES:
2452 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2453 case CTRL_X_DICTIONARY:
2454 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2455 case CTRL_X_THESAURUS:
2456 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2457 case CTRL_X_TAGS:
2458 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2459#ifdef FEAT_FIND_ID
2460 case CTRL_X_PATH_PATTERNS:
2461 return (c == Ctrl_P || c == Ctrl_N);
2462 case CTRL_X_PATH_DEFINES:
2463 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2464#endif
2465 case CTRL_X_CMDLINE:
2466 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2467 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002468#ifdef FEAT_COMPL_FUNC
2469 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002470 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002471 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002472 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002473#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002474 case CTRL_X_SPELL:
2475 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002476 case CTRL_X_EVAL:
2477 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002479 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 return FALSE;
2481}
2482
2483/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002484 * Return TRUE when character "c" is part of the item currently being
2485 * completed. Used to decide whether to abandon complete mode when the menu
2486 * is visible.
2487 */
2488 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002489ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002490{
2491 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2492 /* When expanding an identifier only accept identifier chars. */
2493 return vim_isIDc(c);
2494
2495 switch (ctrl_x_mode)
2496 {
2497 case CTRL_X_FILES:
2498 /* When expanding file name only accept file name chars. But not
2499 * path separators, so that "proto/<Tab>" expands files in
2500 * "proto", not "proto/" as a whole */
2501 return vim_isfilec(c) && !vim_ispathsep(c);
2502
2503 case CTRL_X_CMDLINE:
2504 case CTRL_X_OMNI:
2505 /* Command line and Omni completion can work with just about any
2506 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002507 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002508
2509 case CTRL_X_WHOLE_LINE:
2510 /* For while line completion a space can be part of the line. */
2511 return vim_isprintc(c);
2512 }
2513 return vim_iswordc(c);
2514}
2515
2516/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002517 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002519 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 * the rest of the word to be in -- webb
2521 */
2522 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002523ins_compl_add_infercase(
2524 char_u *str,
2525 int len,
2526 int icase,
2527 char_u *fname,
2528 int dir,
2529 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002531 char_u *p;
2532 int i, c;
2533 int actual_len; /* Take multi-byte characters */
2534 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002535 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002536 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 int has_lower = FALSE;
2538 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002540 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002542 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
Bram Moolenaara2993e12007-08-12 14:38:46 +00002544 /* Find actual length of completion. */
2545#ifdef FEAT_MBYTE
2546 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002548 p = str;
2549 actual_len = 0;
2550 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002552 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002553 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
2555 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002556 else
2557#endif
2558 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
Bram Moolenaara2993e12007-08-12 14:38:46 +00002560 /* Find actual length of original text. */
2561#ifdef FEAT_MBYTE
2562 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002564 p = compl_orig_text;
2565 actual_compl_length = 0;
2566 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002568 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002569 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002572 else
2573#endif
2574 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002576 /* "actual_len" may be smaller than "actual_compl_length" when using
2577 * thesaurus, only use the minimum when comparing. */
2578 min_len = actual_len < actual_compl_length
2579 ? actual_len : actual_compl_length;
2580
Bram Moolenaara2993e12007-08-12 14:38:46 +00002581 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002582 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002583 if (wca != NULL)
2584 {
2585 p = str;
2586 for (i = 0; i < actual_len; ++i)
2587#ifdef FEAT_MBYTE
2588 if (has_mbyte)
2589 wca[i] = mb_ptr2char_adv(&p);
2590 else
2591#endif
2592 wca[i] = *(p++);
2593
2594 /* Rule 1: Were any chars converted to lower? */
2595 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002596 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002597 {
2598#ifdef FEAT_MBYTE
2599 if (has_mbyte)
2600 c = mb_ptr2char_adv(&p);
2601 else
2602#endif
2603 c = *(p++);
2604 if (MB_ISLOWER(c))
2605 {
2606 has_lower = TRUE;
2607 if (MB_ISUPPER(wca[i]))
2608 {
2609 /* Rule 1 is satisfied. */
2610 for (i = actual_compl_length; i < actual_len; ++i)
2611 wca[i] = MB_TOLOWER(wca[i]);
2612 break;
2613 }
2614 }
2615 }
2616
2617 /*
2618 * Rule 2: No lower case, 2nd consecutive letter converted to
2619 * upper case.
2620 */
2621 if (!has_lower)
2622 {
2623 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002624 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002625 {
2626#ifdef FEAT_MBYTE
2627 if (has_mbyte)
2628 c = mb_ptr2char_adv(&p);
2629 else
2630#endif
2631 c = *(p++);
2632 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2633 {
2634 /* Rule 2 is satisfied. */
2635 for (i = actual_compl_length; i < actual_len; ++i)
2636 wca[i] = MB_TOUPPER(wca[i]);
2637 break;
2638 }
2639 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2640 }
2641 }
2642
2643 /* Copy the original case of the part we typed. */
2644 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002645 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002646 {
2647#ifdef FEAT_MBYTE
2648 if (has_mbyte)
2649 c = mb_ptr2char_adv(&p);
2650 else
2651#endif
2652 c = *(p++);
2653 if (MB_ISLOWER(c))
2654 wca[i] = MB_TOLOWER(wca[i]);
2655 else if (MB_ISUPPER(c))
2656 wca[i] = MB_TOUPPER(wca[i]);
2657 }
2658
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002659 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002660 * Generate encoding specific output from wide character array.
2661 * Multi-byte characters can occupy up to five bytes more than
2662 * ASCII characters, and we also need one byte for NUL, so stay
2663 * six bytes away from the edge of IObuff.
2664 */
2665 p = IObuff;
2666 i = 0;
2667 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2668#ifdef FEAT_MBYTE
2669 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002670 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002671 else
2672#endif
2673 *(p++) = wca[i++];
2674 *p = NUL;
2675
2676 vim_free(wca);
2677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002679 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2680 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002682 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683}
2684
2685/*
2686 * Add a match to the list of matches.
2687 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002688 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002689 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002691 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002692ins_compl_add(
2693 char_u *str,
2694 int len,
2695 int icase,
2696 char_u *fname,
2697 char_u **cptext, /* extra text for popup menu or NULL */
2698 int cdir,
2699 int flags,
2700 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002702 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002703 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 ui_breakcheck();
2706 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002707 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (len < 0)
2709 len = (int)STRLEN(str);
2710
2711 /*
2712 * If the same match is already present, don't add it.
2713 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002714 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002716 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 do
2718 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002719 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002720 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002721 && match->cp_str[len] == NUL)
2722 return NOTDONE;
2723 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002724 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 }
2726
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002727 /* Remove any popup menu before changing the list of matches. */
2728 ins_compl_del_pum();
2729
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 /*
2731 * Allocate a new match structure.
2732 * Copy the values to the new match structure.
2733 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002734 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002736 return FAIL;
2737 match->cp_number = -1;
2738 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002739 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002740 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
2742 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002743 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002745 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002748 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2750 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002751 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002752 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002753 && compl_curr_match->cp_fname != NULL
2754 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002755 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002756 else if (fname != NULL)
2757 {
2758 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002759 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002762 match->cp_fname = NULL;
2763 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002764
2765 if (cptext != NULL)
2766 {
2767 int i;
2768
2769 for (i = 0; i < CPT_COUNT; ++i)
2770 if (cptext[i] != NULL && *cptext[i] != NUL)
2771 match->cp_text[i] = vim_strsave(cptext[i]);
2772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
2774 /*
2775 * Link the new match structure in the list of matches.
2776 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002777 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002778 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 else if (dir == FORWARD)
2780 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002781 match->cp_next = compl_curr_match->cp_next;
2782 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 }
2784 else /* BACKWARD */
2785 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002786 match->cp_next = compl_curr_match;
2787 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002789 if (match->cp_next)
2790 match->cp_next->cp_prev = match;
2791 if (match->cp_prev)
2792 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002794 compl_first_match = match;
2795 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002797 /*
2798 * Find the longest common string if still doing that.
2799 */
2800 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2801 ins_compl_longest_match(match);
2802
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 return OK;
2804}
2805
2806/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002807 * Return TRUE if "str[len]" matches with match->cp_str, considering
2808 * match->cp_icase.
2809 */
2810 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002811ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002812{
2813 if (match->cp_icase)
2814 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2815 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2816}
2817
2818/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002819 * Reduce the longest common string for match "match".
2820 */
2821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002822ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002823{
2824 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002825 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002826 int had_match;
2827
2828 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002829 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002830 /* First match, use it as a whole. */
2831 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002832 if (compl_leader != NULL)
2833 {
2834 had_match = (curwin->w_cursor.col > compl_col);
2835 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002836 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002837 ins_redraw(FALSE);
2838
2839 /* When the match isn't there (to avoid matching itself) remove it
2840 * again after redrawing. */
2841 if (!had_match)
2842 ins_compl_delete();
2843 compl_used_match = FALSE;
2844 }
2845 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002846 else
2847 {
2848 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002849 p = compl_leader;
2850 s = match->cp_str;
2851 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002852 {
2853#ifdef FEAT_MBYTE
2854 if (has_mbyte)
2855 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002856 c1 = mb_ptr2char(p);
2857 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002858 }
2859 else
2860#endif
2861 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002862 c1 = *p;
2863 c2 = *s;
2864 }
2865 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2866 : (c1 != c2))
2867 break;
2868#ifdef FEAT_MBYTE
2869 if (has_mbyte)
2870 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002871 MB_PTR_ADV(p);
2872 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002873 }
2874 else
2875#endif
2876 {
2877 ++p;
2878 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002879 }
2880 }
2881
2882 if (*p != NUL)
2883 {
2884 /* Leader was shortened, need to change the inserted text. */
2885 *p = NUL;
2886 had_match = (curwin->w_cursor.col > compl_col);
2887 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002888 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002889 ins_redraw(FALSE);
2890
2891 /* When the match isn't there (to avoid matching itself) remove it
2892 * again after redrawing. */
2893 if (!had_match)
2894 ins_compl_delete();
2895 }
2896
2897 compl_used_match = FALSE;
2898 }
2899}
2900
2901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 * Add an array of matches to the list of matches.
2903 * Frees matches[].
2904 */
2905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002906ins_compl_add_matches(
2907 int num_matches,
2908 char_u **matches,
2909 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
2911 int i;
2912 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002913 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
Bram Moolenaar572cb562005-08-05 21:35:02 +00002915 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002916 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002917 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002919 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 FreeWild(num_matches, matches);
2921}
2922
2923/* Make the completion list cyclic.
2924 * Return the number of matches (excluding the original).
2925 */
2926 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002927ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002929 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 int count = 0;
2931
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002932 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 {
2934 /*
2935 * Find the end of the list.
2936 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002937 match = compl_first_match;
2938 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002939 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002941 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 ++count;
2943 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002944 match->cp_next = compl_first_match;
2945 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947 return count;
2948}
2949
Bram Moolenaara94bc432006-03-10 21:42:59 +00002950/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002951 * Set variables that store noselect and noinsert behavior from the
2952 * 'completeopt' value.
2953 */
2954 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002955completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002956{
2957 compl_no_insert = FALSE;
2958 compl_no_select = FALSE;
2959 if (strstr((char *)p_cot, "noselect") != NULL)
2960 compl_no_select = TRUE;
2961 if (strstr((char *)p_cot, "noinsert") != NULL)
2962 compl_no_insert = TRUE;
2963}
2964
2965/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002966 * Start completion for the complete() function.
2967 * "startcol" is where the matched text starts (1 is first column).
2968 * "list" is the list of matches.
2969 */
2970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002971set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002972{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002973 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002974 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002975
Bram Moolenaara94bc432006-03-10 21:42:59 +00002976 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002977 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002978 ins_compl_prep(' ');
2979 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002980 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002981
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002982 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002983 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002984 startcol = curwin->w_cursor.col;
2985 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002986 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002987 /* compl_pattern doesn't need to be set */
2988 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2989 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002990 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002991 return;
2992
Bram Moolenaare4214502015-03-05 18:08:43 +01002993 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002994
2995 ins_compl_add_list(list);
2996 compl_matches = ins_compl_make_cyclic();
2997 compl_started = TRUE;
2998 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002999 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003000
3001 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02003002 if (compl_no_insert || compl_no_select)
3003 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003004 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02003005 if (compl_no_select)
3006 /* Down/Up has no real effect. */
3007 ins_complete(K_UP, FALSE);
3008 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003009 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003010 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02003011 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003012
3013 /* Lazily show the popup menu, unless we got interrupted. */
3014 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01003015 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003016 out_flush();
3017}
3018
3019
Bram Moolenaar9372a112005-12-06 19:59:18 +00003020/* "compl_match_array" points the currently displayed list of entries in the
3021 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003022static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003023static int compl_match_arraysize;
3024
3025/*
3026 * Update the screen and when there is any scrolling remove the popup menu.
3027 */
3028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003029ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003030{
3031 int h;
3032
3033 if (compl_match_array != NULL)
3034 {
3035 h = curwin->w_cline_height;
3036 update_screen(0);
3037 if (h != curwin->w_cline_height)
3038 ins_compl_del_pum();
3039 }
3040}
3041
3042/*
3043 * Remove any popup menu.
3044 */
3045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003046ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003047{
3048 if (compl_match_array != NULL)
3049 {
3050 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003051 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003052 }
3053}
3054
3055/*
3056 * Return TRUE if the popup menu should be displayed.
3057 */
3058 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003059pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003060{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003061 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003062 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003063 return FALSE;
3064
3065 /* The display looks bad on a B&W display. */
3066 if (t_colors < 8
3067#ifdef FEAT_GUI
3068 && !gui.in_use
3069#endif
3070 )
3071 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003072 return TRUE;
3073}
3074
3075/*
3076 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003077 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003078 */
3079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003081{
3082 compl_T *compl;
3083 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003084
3085 /* Don't display the popup menu if there are no matches or there is only
3086 * one (ignoring the original text). */
3087 compl = compl_first_match;
3088 i = 0;
3089 do
3090 {
3091 if (compl == NULL
3092 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3093 break;
3094 compl = compl->cp_next;
3095 } while (compl != compl_first_match);
3096
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003097 if (strstr((char *)p_cot, "menuone") != NULL)
3098 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003099 return (i >= 2);
3100}
3101
3102/*
3103 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003104 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003105 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003107ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003108{
3109 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003110 compl_T *shown_compl = NULL;
3111 int did_find_shown_match = FALSE;
3112 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003113 int i;
3114 int cur = -1;
3115 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003116 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003117
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003118 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003119 return;
3120
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003121#if defined(FEAT_EVAL)
3122 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3123 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3124#endif
3125
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003126 /* Update the screen before drawing the popup menu over it. */
3127 update_screen(0);
3128
3129 if (compl_match_array == NULL)
3130 {
3131 /* Need to build the popup menu list. */
3132 compl_match_arraysize = 0;
3133 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003134 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003135 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003136 do
3137 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003138 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3139 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003140 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003141 ++compl_match_arraysize;
3142 compl = compl->cp_next;
3143 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003144 if (compl_match_arraysize == 0)
3145 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003146 compl_match_array = (pumitem_T *)alloc_clear(
3147 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003148 * compl_match_arraysize));
3149 if (compl_match_array != NULL)
3150 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003151 /* If the current match is the original text don't find the first
3152 * match after it, don't highlight anything. */
3153 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3154 shown_match_ok = TRUE;
3155
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003156 i = 0;
3157 compl = compl_first_match;
3158 do
3159 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003160 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3161 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003162 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003163 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003164 if (!shown_match_ok)
3165 {
3166 if (compl == compl_shown_match || did_find_shown_match)
3167 {
3168 /* This item is the shown match or this is the
3169 * first displayed item after the shown match. */
3170 compl_shown_match = compl;
3171 did_find_shown_match = TRUE;
3172 shown_match_ok = TRUE;
3173 }
3174 else
3175 /* Remember this displayed match for when the
3176 * shown match is just below it. */
3177 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003178 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003179 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003180
3181 if (compl->cp_text[CPT_ABBR] != NULL)
3182 compl_match_array[i].pum_text =
3183 compl->cp_text[CPT_ABBR];
3184 else
3185 compl_match_array[i].pum_text = compl->cp_str;
3186 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3187 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3188 if (compl->cp_text[CPT_MENU] != NULL)
3189 compl_match_array[i++].pum_extra =
3190 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003191 else
3192 compl_match_array[i++].pum_extra = compl->cp_fname;
3193 }
3194
3195 if (compl == compl_shown_match)
3196 {
3197 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003198
3199 /* When the original text is the shown match don't set
3200 * compl_shown_match. */
3201 if (compl->cp_flags & ORIGINAL_TEXT)
3202 shown_match_ok = TRUE;
3203
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003204 if (!shown_match_ok && shown_compl != NULL)
3205 {
3206 /* The shown match isn't displayed, set it to the
3207 * previously displayed match. */
3208 compl_shown_match = shown_compl;
3209 shown_match_ok = TRUE;
3210 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003211 }
3212 compl = compl->cp_next;
3213 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003214
3215 if (!shown_match_ok) /* no displayed match at all */
3216 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003217 }
3218 }
3219 else
3220 {
3221 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003222 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003223 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3224 || compl_match_array[i].pum_text
3225 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003226 {
3227 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003228 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003229 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003230 }
3231
3232 if (compl_match_array != NULL)
3233 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003234 /* In Replace mode when a $ is displayed at the end of the line only
3235 * part of the screen would be updated. We do need to redraw here. */
3236 dollar_vcol = -1;
3237
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003238 /* Compute the screen column of the start of the completed text.
3239 * Use the cursor to get all wrapping and other settings right. */
3240 col = curwin->w_cursor.col;
3241 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003242 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003243 curwin->w_cursor.col = col;
3244 }
3245}
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247#define DICT_FIRST (1) /* use just first element in "dict" */
3248#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003249
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003251 * Add any identifiers that match the given pattern in the list of dictionary
3252 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 */
3254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003255ins_compl_dictionaries(
3256 char_u *dict_start,
3257 char_u *pat,
3258 int flags, /* DICT_FIRST and/or DICT_EXACT */
3259 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003261 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 char_u *ptr;
3263 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 char_u **files;
3266 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003268 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaar0b238792006-03-02 22:49:12 +00003270 if (*dict == NUL)
3271 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003272#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003273 /* When 'dictionary' is empty and spell checking is enabled use
3274 * "spell". */
3275 if (!thesaurus && curwin->w_p_spell)
3276 dict = (char_u *)"spell";
3277 else
3278#endif
3279 return;
3280 }
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003283 if (buf == NULL)
3284 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003285 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 /* If 'infercase' is set, don't use 'smartcase' here */
3288 save_p_scs = p_scs;
3289 if (curbuf->b_p_inf)
3290 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003291
3292 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3293 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003294 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003295 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003296 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003297 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003298 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003299
3300 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003301 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003302 len = STRLEN(pat_esc) + 10;
3303 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003304 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003305 {
3306 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003307 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003308 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003309 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003310 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3311 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003312 vim_free(ptr);
3313 }
3314 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003315 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003316 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003317 if (regmatch.regprog == NULL)
3318 goto theend;
3319 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3322 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003323 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
3325 /* copy one dictionary file name into buf */
3326 if (flags == DICT_EXACT)
3327 {
3328 count = 1;
3329 files = &dict;
3330 }
3331 else
3332 {
3333 /* Expand wildcards in the dictionary name, but do not allow
3334 * backticks (for security, the 'dict' option may have been set in
3335 * a modeline). */
3336 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003337# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003338 if (!thesaurus && STRCMP(buf, "spell") == 0)
3339 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003340 else
3341# endif
3342 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 || expand_wildcards(1, &buf, &count, &files,
3344 EW_FILE|EW_SILENT) != OK)
3345 count = 0;
3346 }
3347
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003348# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003349 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003351 /* Complete from active spelling. Skip "\<" in the pattern, we
3352 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003353 if (pat[0] == '\\' && pat[1] == '<')
3354 ptr = pat + 2;
3355 else
3356 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003357 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003359 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003360# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003361 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003362 {
3363 ins_compl_files(count, files, thesaurus, flags,
3364 &regmatch, buf, &dir);
3365 if (flags != DICT_EXACT)
3366 FreeWild(count, files);
3367 }
3368 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 break;
3370 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003371
3372theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003374 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 vim_free(buf);
3376}
3377
Bram Moolenaar0b238792006-03-02 22:49:12 +00003378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003379ins_compl_files(
3380 int count,
3381 char_u **files,
3382 int thesaurus,
3383 int flags,
3384 regmatch_T *regmatch,
3385 char_u *buf,
3386 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003387{
3388 char_u *ptr;
3389 int i;
3390 FILE *fp;
3391 int add_r;
3392
3393 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3394 {
3395 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3396 if (flags != DICT_EXACT)
3397 {
3398 vim_snprintf((char *)IObuff, IOSIZE,
3399 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003400 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003401 }
3402
3403 if (fp != NULL)
3404 {
3405 /*
3406 * Read dictionary file line by line.
3407 * Check each line for a match.
3408 */
3409 while (!got_int && !compl_interrupted
3410 && !vim_fgets(buf, LSIZE, fp))
3411 {
3412 ptr = buf;
3413 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3414 {
3415 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003416 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003417 ptr = find_line_end(ptr);
3418 else
3419 ptr = find_word_end(ptr);
3420 add_r = ins_compl_add_infercase(regmatch->startp[0],
3421 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003422 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003423 if (thesaurus)
3424 {
3425 char_u *wstart;
3426
3427 /*
3428 * Add the other matches on the line
3429 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003430 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003431 while (!got_int)
3432 {
3433 /* Find start of the next word. Skip white
3434 * space and punctuation. */
3435 ptr = find_word_start(ptr);
3436 if (*ptr == NUL || *ptr == NL)
3437 break;
3438 wstart = ptr;
3439
Bram Moolenaara2993e12007-08-12 14:38:46 +00003440 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003441#ifdef FEAT_MBYTE
3442 if (has_mbyte)
3443 /* Japanese words may have characters in
3444 * different classes, only separate words
3445 * with single-byte non-word characters. */
3446 while (*ptr != NUL)
3447 {
3448 int l = (*mb_ptr2len)(ptr);
3449
3450 if (l < 2 && !vim_iswordc(*ptr))
3451 break;
3452 ptr += l;
3453 }
3454 else
3455#endif
3456 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003457
3458 /* Add the word. Skip the regexp match. */
3459 if (wstart != regmatch->startp[0])
3460 add_r = ins_compl_add_infercase(wstart,
3461 (int)(ptr - wstart),
3462 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003463 }
3464 }
3465 if (add_r == OK)
3466 /* if dir was BACKWARD then honor it just once */
3467 *dir = FORWARD;
3468 else if (add_r == FAIL)
3469 break;
3470 /* avoid expensive call to vim_regexec() when at end
3471 * of line */
3472 if (*ptr == '\n' || got_int)
3473 break;
3474 }
3475 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003476 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003477 }
3478 fclose(fp);
3479 }
3480 }
3481}
3482
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483/*
3484 * Find the start of the next word.
3485 * Returns a pointer to the first char of the word. Also stops at a NUL.
3486 */
3487 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003488find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489{
3490#ifdef FEAT_MBYTE
3491 if (has_mbyte)
3492 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003493 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 else
3495#endif
3496 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3497 ++ptr;
3498 return ptr;
3499}
3500
3501/*
3502 * Find the end of the word. Assumes it starts inside a word.
3503 * Returns a pointer to just after the word.
3504 */
3505 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003506find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
3508#ifdef FEAT_MBYTE
3509 int start_class;
3510
3511 if (has_mbyte)
3512 {
3513 start_class = mb_get_class(ptr);
3514 if (start_class > 1)
3515 while (*ptr != NUL)
3516 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003517 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if (mb_get_class(ptr) != start_class)
3519 break;
3520 }
3521 }
3522 else
3523#endif
3524 while (vim_iswordc(*ptr))
3525 ++ptr;
3526 return ptr;
3527}
3528
3529/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003530 * Find the end of the line, omitting CR and NL at the end.
3531 * Returns a pointer to just after the line.
3532 */
3533 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003534find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003535{
3536 char_u *s;
3537
3538 s = ptr + STRLEN(ptr);
3539 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3540 --s;
3541 return s;
3542}
3543
3544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 * Free the list of completions
3546 */
3547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003548ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003550 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003551 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552
Bram Moolenaard23a8232018-02-10 18:45:26 +01003553 VIM_CLEAR(compl_pattern);
3554 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003556 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003558
3559 ins_compl_del_pum();
3560 pum_clear();
3561
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003562 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 do
3564 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003565 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003566 compl_curr_match = compl_curr_match->cp_next;
3567 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003569 if (match->cp_flags & FREE_FNAME)
3570 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003571 for (i = 0; i < CPT_COUNT; ++i)
3572 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003574 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3575 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003576 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003577 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578}
3579
3580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003581ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003583 compl_cont_status = 0;
3584 compl_started = FALSE;
3585 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003586 VIM_CLEAR(compl_pattern);
3587 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003589 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003590 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003591 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003592 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593}
3594
3595/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003596 * Return TRUE when Insert completion is active.
3597 */
3598 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003599ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003600{
3601 return compl_started;
3602}
3603
3604/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003605 * Delete one character before the cursor and show the subset of the matches
3606 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003607 * Returns the character to be used, NUL if the work is done and another char
3608 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003609 */
3610 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003611ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003612{
3613 char_u *line;
3614 char_u *p;
3615
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003616 line = ml_get_curline();
3617 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003618 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003619
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003620 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003621 * allow the word to be deleted, we won't match everything.
3622 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003623 if ((int)(p - line) - (int)compl_col < 0
3624 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003625 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3626 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3627 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003628 return K_BS;
3629
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003630 /* Deleted more than what was used to find matches or didn't finish
3631 * finding all matches: need to look for matches all over again. */
3632 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003633 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003634 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003635
Bram Moolenaara6557602006-02-04 22:43:20 +00003636 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003637 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003638 if (compl_leader != NULL)
3639 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003640 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003641 if (compl_shown_match != NULL)
3642 /* Make sure current match is not a hidden item. */
3643 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003644 return NUL;
3645 }
3646 return K_BS;
3647}
Bram Moolenaara6557602006-02-04 22:43:20 +00003648
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003649/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003650 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3651 * be called.
3652 */
3653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003654ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003655{
3656 /* Return TRUE if we didn't complete finding matches or when the
3657 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3658 return compl_was_interrupted
3659 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3660 && compl_opt_refresh_always);
3661}
3662
3663/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003664 * Called after changing "compl_leader".
3665 * Show the popup menu with a different set of matches.
3666 * May also search for matches again if the previous search was interrupted.
3667 */
3668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003669ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003670{
3671 ins_compl_del_pum();
3672 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003673 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003674 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003675
3676 if (compl_started)
3677 ins_compl_set_original_text(compl_leader);
3678 else
3679 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003680#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003681 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003682#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003683 /*
3684 * Matches were cleared, need to search for them now. First display
3685 * the changed text before the cursor. Set "compl_restarting" to
3686 * avoid that the first match is inserted.
3687 */
3688 update_screen(0);
3689#ifdef FEAT_GUI
3690 if (gui.in_use)
3691 {
3692 /* Show the cursor after the match, not after the redrawn text. */
3693 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003694 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003695 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003696#endif
3697 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003698 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003699 compl_cont_status = 0;
3700 compl_restarting = FALSE;
3701 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003702
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003703 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003704
3705 /* Show the popup menu with a different set of matches. */
3706 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003707
3708 /* Don't let Enter select the original text when there is no popup menu. */
3709 if (compl_match_array == NULL)
3710 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003711}
3712
3713/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003714 * Return the length of the completion, from the completion start column to
3715 * the cursor column. Making sure it never goes below zero.
3716 */
3717 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003718ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003719{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003720 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003721
3722 if (off < 0)
3723 return 0;
3724 return off;
3725}
3726
3727/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003728 * Append one character to the match leader. May reduce the number of
3729 * matches.
3730 */
3731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003732ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003733{
3734#ifdef FEAT_MBYTE
3735 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003736#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003737
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003738 if (stop_arrow() == FAIL)
3739 return;
3740#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003741 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3742 {
3743 char_u buf[MB_MAXBYTES + 1];
3744
3745 (*mb_char2bytes)(c, buf);
3746 buf[cc] = NUL;
3747 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003748 if (compl_opt_refresh_always)
3749 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003750 }
3751 else
3752#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003753 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003754 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003755 if (compl_opt_refresh_always)
3756 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003757 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003758
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003759 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003760 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003761 ins_compl_restart();
3762
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003763 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003764 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003765 * break redo. */
3766 if (!compl_opt_refresh_always)
3767 {
3768 vim_free(compl_leader);
3769 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003770 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003771 if (compl_leader != NULL)
3772 ins_compl_new_leader();
3773 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003774}
3775
3776/*
3777 * Setup for finding completions again without leaving CTRL-X mode. Used when
3778 * BS or a key was typed while still searching for matches.
3779 */
3780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003781ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003782{
3783 ins_compl_free();
3784 compl_started = FALSE;
3785 compl_matches = 0;
3786 compl_cont_status = 0;
3787 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003788}
3789
3790/*
3791 * Set the first match, the original text.
3792 */
3793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003794ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003795{
3796 char_u *p;
3797
Bram Moolenaare87edf32018-04-17 22:14:32 +02003798 /* Replace the original text entry.
3799 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3800 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003801 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3802 {
3803 p = vim_strsave(str);
3804 if (p != NULL)
3805 {
3806 vim_free(compl_first_match->cp_str);
3807 compl_first_match->cp_str = p;
3808 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003809 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003810 else if (compl_first_match->cp_prev != NULL
3811 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3812 {
3813 p = vim_strsave(str);
3814 if (p != NULL)
3815 {
3816 vim_free(compl_first_match->cp_prev->cp_str);
3817 compl_first_match->cp_prev->cp_str = p;
3818 }
3819 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003820}
3821
3822/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003823 * Append one character to the match leader. May reduce the number of
3824 * matches.
3825 */
3826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003827ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003828{
3829 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003830 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003831 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003832 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003833
3834 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003835 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003836 {
3837 /* When still at the original match use the first entry that matches
3838 * the leader. */
3839 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3840 {
3841 p = NULL;
3842 for (cp = compl_shown_match->cp_next; cp != NULL
3843 && cp != compl_first_match; cp = cp->cp_next)
3844 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003845 if (compl_leader == NULL
3846 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003847 (int)STRLEN(compl_leader)))
3848 {
3849 p = cp->cp_str;
3850 break;
3851 }
3852 }
3853 if (p == NULL || (int)STRLEN(p) <= len)
3854 return;
3855 }
3856 else
3857 return;
3858 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003859 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003860 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003861 ins_compl_addleader(c);
3862}
3863
3864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003866 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003867 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003870ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871{
3872 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003874 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875
3876 /* Forget any previous 'special' messages if this is actually
3877 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3878 */
3879 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3880 edit_submode_extra = NULL;
3881
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003882 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003883 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3884 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003885 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003887 /* Set "compl_get_longest" when finding the first matches. */
3888 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003889 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003890 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003891 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003892 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003893
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003894 }
3895
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3897 {
3898 /*
3899 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3900 * it will be yet. Now we decide.
3901 */
3902 switch (c)
3903 {
3904 case Ctrl_E:
3905 case Ctrl_Y:
3906 ctrl_x_mode = CTRL_X_SCROLL;
3907 if (!(State & REPLACE_FLAG))
3908 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3909 else
3910 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3911 edit_submode_pre = NULL;
3912 showmode();
3913 break;
3914 case Ctrl_L:
3915 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3916 break;
3917 case Ctrl_F:
3918 ctrl_x_mode = CTRL_X_FILES;
3919 break;
3920 case Ctrl_K:
3921 ctrl_x_mode = CTRL_X_DICTIONARY;
3922 break;
3923 case Ctrl_R:
3924 /* Simply allow ^R to happen without affecting ^X mode */
3925 break;
3926 case Ctrl_T:
3927 ctrl_x_mode = CTRL_X_THESAURUS;
3928 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003929#ifdef FEAT_COMPL_FUNC
3930 case Ctrl_U:
3931 ctrl_x_mode = CTRL_X_FUNCTION;
3932 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003933 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003934 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003935 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003936#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003937 case 's':
3938 case Ctrl_S:
3939 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003940#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003941 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003942 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003943 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003944#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003945 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 case Ctrl_RSB:
3947 ctrl_x_mode = CTRL_X_TAGS;
3948 break;
3949#ifdef FEAT_FIND_ID
3950 case Ctrl_I:
3951 case K_S_TAB:
3952 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3953 break;
3954 case Ctrl_D:
3955 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3956 break;
3957#endif
3958 case Ctrl_V:
3959 case Ctrl_Q:
3960 ctrl_x_mode = CTRL_X_CMDLINE;
3961 break;
3962 case Ctrl_P:
3963 case Ctrl_N:
3964 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3965 * just started ^X mode, or there were enough ^X's to cancel
3966 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3967 * do normal expansion when interrupting a different mode (say
3968 * ^X^F^X^P or ^P^X^X^P, see below)
3969 * nothing changes if interrupting mode 0, (eg, the flag
3970 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003971 if (!(compl_cont_status & CONT_INTRPT))
3972 compl_cont_status |= CONT_LOCAL;
3973 else if (compl_cont_mode != 0)
3974 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 /* FALLTHROUGH */
3976 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003977 /* If we have typed at least 2 ^X's... for modes != 0, we set
3978 * compl_cont_status = 0 (eg, as if we had just started ^X
3979 * mode).
3980 * For mode 0, we set "compl_cont_mode" to an impossible
3981 * value, in both cases ^X^X can be used to restart the same
3982 * mode (avoiding ADDING mode).
3983 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3984 * 'complete' and local ^P expansions respectively.
3985 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3986 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 if (c == Ctrl_X)
3988 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003989 if (compl_cont_mode != 0)
3990 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003992 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003994 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 edit_submode = NULL;
3996 showmode();
3997 break;
3998 }
3999 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004000 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
4002 /* We're already in CTRL-X mode, do we stay in it? */
4003 if (!vim_is_ctrl_x_key(c))
4004 {
4005 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004006 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 else
4008 ctrl_x_mode = CTRL_X_FINISHED;
4009 edit_submode = NULL;
4010 }
4011 showmode();
4012 }
4013
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004014 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
4016 /* Show error message from attempted keyword completion (probably
4017 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004018 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004020 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
4021 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 || ctrl_x_mode == CTRL_X_FINISHED)
4023 {
4024 /* Get here when we have finished typing a sequence of ^N and
4025 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004026 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004027 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
4029 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004030 * If any of the original typed text has been changed, eg when
4031 * ignorecase is set, we must add back-spaces to the redo
4032 * buffer. We add as few as necessary to delete just the part
4033 * of the original text that has changed.
4034 * When using the longest match, edited the match or used
4035 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004037 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4038 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004039 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004040 ptr = NULL;
4041 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
4043
4044#ifdef FEAT_CINDENT
4045 want_cindent = (can_cindent && cindent_on());
4046#endif
4047 /*
4048 * When completing whole lines: fix indent for 'cindent'.
4049 * Otherwise, break line if it's too long.
4050 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004051 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
4053#ifdef FEAT_CINDENT
4054 /* re-indent the current line */
4055 if (want_cindent)
4056 {
4057 do_c_expr_indent();
4058 want_cindent = FALSE; /* don't do it again */
4059 }
4060#endif
4061 }
4062 else
4063 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004064 int prev_col = curwin->w_cursor.col;
4065
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004067 if (prev_col > 0)
4068 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004069 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004070 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004072 if (prev_col > 0
4073 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4074 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004077 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004078 * the selection without inserting anything. When
4079 * compl_enter_selects is set the Enter key does the same. */
4080 if ((c == Ctrl_Y || (compl_enter_selects
4081 && (c == CAR || c == K_KENTER || c == NL)))
4082 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004083 retval = TRUE;
4084
Bram Moolenaar47247282016-08-02 22:36:02 +02004085 /* CTRL-E means completion is Ended, go back to the typed text.
4086 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004087 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004088 {
4089 ins_compl_delete();
4090 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004091 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004092 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004093 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004094 retval = TRUE;
4095 }
4096
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004097 auto_format(FALSE, TRUE);
4098
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004100 compl_started = FALSE;
4101 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004102 if (!shortmess(SHM_COMPLETIONMENU))
4103 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004104 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004105 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 if (edit_submode != NULL)
4107 {
4108 edit_submode = NULL;
4109 showmode();
4110 }
4111
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004112#ifdef FEAT_CMDWIN
4113 if (c == Ctrl_C && cmdwin_type != 0)
4114 /* Avoid the popup menu remains displayed when leaving the
4115 * command line window. */
4116 update_screen(0);
4117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118#ifdef FEAT_CINDENT
4119 /*
4120 * Indent now if a key was typed that is in 'cinkeys'.
4121 */
4122 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4123 do_c_expr_indent();
4124#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004125 /* Trigger the CompleteDone event to give scripts a chance to act
4126 * upon the completion. */
4127 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004130 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4131 /* Trigger the CompleteDone event to give scripts a chance to act
4132 * upon the (possibly failed) completion. */
4133 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134
4135 /* reset continue_* if we left expansion-mode, if we stay they'll be
4136 * (re)set properly in ins_complete() */
4137 if (!vim_is_ctrl_x_key(c))
4138 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 compl_cont_status = 0;
4140 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004142
4143 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144}
4145
4146/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004147 * Fix the redo buffer for the completion leader replacing some of the typed
4148 * text. This inserts backspaces and appends the changed text.
4149 * "ptr" is the known leader text or NUL.
4150 */
4151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004152ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004153{
4154 int len;
4155 char_u *p;
4156 char_u *ptr = ptr_arg;
4157
4158 if (ptr == NULL)
4159 {
4160 if (compl_leader != NULL)
4161 ptr = compl_leader;
4162 else
4163 return; /* nothing to do */
4164 }
4165 if (compl_orig_text != NULL)
4166 {
4167 p = compl_orig_text;
4168 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4169 ;
4170#ifdef FEAT_MBYTE
4171 if (len > 0)
4172 len -= (*mb_head_off)(p, p + len);
4173#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004174 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004175 AppendCharToRedobuff(K_BS);
4176 }
4177 else
4178 len = 0;
4179 if (ptr != NULL)
4180 AppendToRedobuffLit(ptr + len, -1);
4181}
4182
4183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4185 * (depending on flag) starting from buf and looking for a non-scanned
4186 * buffer (other than curbuf). curbuf is special, if it is called with
4187 * buf=curbuf then it has to be the first call for a given flag/expansion.
4188 *
4189 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4190 */
4191 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004192ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
4196 if (flag == 'w') /* just windows */
4197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 if (buf == curbuf) /* first call for this flag/expansion */
4199 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004200 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 && wp->w_buffer->b_scanned)
4202 ;
4203 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 else
4206 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4207 * (unlisted buffers)
4208 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004209 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 && ((flag == 'U'
4211 ? buf->b_p_bl
4212 : (!buf->b_p_bl
4213 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004214 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 ;
4216 return buf;
4217}
4218
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004219#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004220/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004221 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004222 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004223 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004225expand_by_function(
4226 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4227 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004228{
Bram Moolenaar82139082011-09-14 16:52:09 +02004229 list_T *matchlist = NULL;
4230 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004231 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004232 char_u *funcname;
4233 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004234 win_T *curwin_save;
4235 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004236 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004237
Bram Moolenaare344bea2005-09-01 20:46:49 +00004238 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4239 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004240 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004241
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004242 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004243 args[0].v_type = VAR_NUMBER;
4244 args[0].vval.v_number = 0;
4245 args[1].v_type = VAR_STRING;
4246 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4247 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004248
Bram Moolenaare344bea2005-09-01 20:46:49 +00004249 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004250 curwin_save = curwin;
4251 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004252
4253 /* Call a function, which returns a list or dict. */
Bram Moolenaarded27a12018-08-01 19:06:03 +02004254 if (call_vim_function(funcname, 2, args, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004255 {
4256 switch (rettv.v_type)
4257 {
4258 case VAR_LIST:
4259 matchlist = rettv.vval.v_list;
4260 break;
4261 case VAR_DICT:
4262 matchdict = rettv.vval.v_dict;
4263 break;
4264 default:
4265 /* TODO: Give error message? */
4266 clear_tv(&rettv);
4267 break;
4268 }
4269 }
4270
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004271 if (curwin_save != curwin || curbuf_save != curbuf)
4272 {
4273 EMSG(_(e_complwin));
4274 goto theend;
4275 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004276 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004277 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004278 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004279 {
4280 EMSG(_(e_compldel));
4281 goto theend;
4282 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004283
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004284 if (matchlist != NULL)
4285 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004286 else if (matchdict != NULL)
4287 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004288
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004289theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004290 if (matchdict != NULL)
4291 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004292 if (matchlist != NULL)
4293 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004294}
4295#endif /* FEAT_COMPL_FUNC */
4296
Bram Moolenaar39f05632006-03-19 22:15:26 +00004297#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004298/*
4299 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004300 */
4301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004302ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004303{
4304 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004305 int dir = compl_direction;
4306
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004307 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004308 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004309 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004310 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4311 /* if dir was BACKWARD then honor it just once */
4312 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004313 else if (did_emsg)
4314 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004315 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004316}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004317
4318/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004319 * Add completions from a dict.
4320 */
4321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004322ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004323{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004324 dictitem_T *di_refresh;
4325 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004326
4327 /* Check for optional "refresh" item. */
4328 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004329 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4330 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004331 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004332 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004333
4334 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4335 compl_opt_refresh_always = TRUE;
4336 }
4337
4338 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004339 di_words = dict_find(dict, (char_u *)"words", 5);
4340 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4341 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004342}
4343
4344/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004345 * Add a match to the list of matches from a typeval_T.
4346 * If the given string is already in the list of completions, then return
4347 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4348 * maybe because alloc() returns NULL, then FAIL is returned.
4349 */
4350 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004351ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004352{
4353 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004354 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004355 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004356 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004357 char_u *(cptext[CPT_COUNT]);
4358
4359 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4360 {
4361 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4362 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4363 (char_u *)"abbr", FALSE);
4364 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4365 (char_u *)"menu", FALSE);
4366 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4367 (char_u *)"kind", FALSE);
4368 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4369 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004370 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4371 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004372 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4373 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004374 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004375 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004376 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4377 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004378 }
4379 else
4380 {
4381 word = get_tv_string_chk(tv);
4382 vim_memset(cptext, 0, sizeof(cptext));
4383 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004384 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004385 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004386 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004387}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004388#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004389
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390/*
4391 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004392 * The search starts at position "ini" in curbuf and in the direction
4393 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004394 * When "compl_started" is FALSE start at that position, otherwise continue
4395 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004396 * This may return before finding all the matches.
4397 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 */
4399 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004400ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
4402 static pos_T first_match_pos;
4403 static pos_T last_match_pos;
4404 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004405 static int found_all = FALSE; /* Found all matches of a
4406 certain type. */
4407 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
Bram Moolenaar572cb562005-08-05 21:35:02 +00004409 pos_T *pos;
4410 char_u **matches;
4411 int save_p_scs;
4412 int save_p_ws;
4413 int save_p_ic;
4414 int i;
4415 int num_matches;
4416 int len;
4417 int found_new_match;
4418 int type = ctrl_x_mode;
4419 char_u *ptr;
4420 char_u *dict = NULL;
4421 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004422 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004426 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 ins_buf->b_scanned = 0;
4428 found_all = FALSE;
4429 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004431 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 last_match_pos = first_match_pos = *ini;
4433 }
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004434 else if (ins_buf != curbuf && !buf_valid(ins_buf))
4435 ins_buf = curbuf; // In case the buffer was wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
Bram Moolenaar4475b622017-05-01 20:46:52 +02004437 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004438 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004439
4440 /*
4441 * For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 for (;;)
4444 {
4445 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004446 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004448 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 * or if found_all says this entry is done. For ^X^L only use the
4450 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004451 if ((ctrl_x_mode == CTRL_X_NORMAL
4452 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004453 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
4455 found_all = FALSE;
4456 while (*e_cpt == ',' || *e_cpt == ' ')
4457 e_cpt++;
4458 if (*e_cpt == '.' && !curbuf->b_scanned)
4459 {
4460 ins_buf = curbuf;
4461 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004462 /* Move the cursor back one character so that ^N can match the
4463 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004464 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004465 {
4466 /* Move the cursor to after the last character in the
4467 * buffer, so that word at start of buffer is found
4468 * correctly. */
4469 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4470 first_match_pos.col =
4471 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 last_match_pos = first_match_pos;
4474 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004475
4476 /* Remember the first match so that the loop stops when we
4477 * wrap and come back there a second time. */
4478 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
4480 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4481 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4482 {
4483 /* Scan a buffer, but not the current one. */
4484 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4485 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004486 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 first_match_pos.col = last_match_pos.col = 0;
4488 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4489 last_match_pos.lnum = 0;
4490 type = 0;
4491 }
4492 else /* unloaded buffer, scan like dictionary */
4493 {
4494 found_all = TRUE;
4495 if (ins_buf->b_fname == NULL)
4496 continue;
4497 type = CTRL_X_DICTIONARY;
4498 dict = ins_buf->b_fname;
4499 dict_f = DICT_EXACT;
4500 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004501 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 ins_buf->b_fname == NULL
4503 ? buf_spname(ins_buf)
4504 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004505 ? ins_buf->b_fname
4506 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004507 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509 else if (*e_cpt == NUL)
4510 break;
4511 else
4512 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004513 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 type = -1;
4515 else if (*e_cpt == 'k' || *e_cpt == 's')
4516 {
4517 if (*e_cpt == 'k')
4518 type = CTRL_X_DICTIONARY;
4519 else
4520 type = CTRL_X_THESAURUS;
4521 if (*++e_cpt != ',' && *e_cpt != NUL)
4522 {
4523 dict = e_cpt;
4524 dict_f = DICT_FIRST;
4525 }
4526 }
4527#ifdef FEAT_FIND_ID
4528 else if (*e_cpt == 'i')
4529 type = CTRL_X_PATH_PATTERNS;
4530 else if (*e_cpt == 'd')
4531 type = CTRL_X_PATH_DEFINES;
4532#endif
4533 else if (*e_cpt == ']' || *e_cpt == 't')
4534 {
4535 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004536 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004537 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 }
4539 else
4540 type = -1;
4541
4542 /* in any case e_cpt is advanced to the next entry */
4543 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4544
4545 found_all = TRUE;
4546 if (type == -1)
4547 continue;
4548 }
4549 }
4550
Bram Moolenaar4475b622017-05-01 20:46:52 +02004551 /* If complete() was called then compl_pattern has been reset. The
4552 * following won't work then, bail out. */
4553 if (compl_pattern == NULL)
4554 break;
4555
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 switch (type)
4557 {
4558 case -1:
4559 break;
4560#ifdef FEAT_FIND_ID
4561 case CTRL_X_PATH_PATTERNS:
4562 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004563 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004566 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4568 (linenr_T)1, (linenr_T)MAXLNUM);
4569 break;
4570#endif
4571
4572 case CTRL_X_DICTIONARY:
4573 case CTRL_X_THESAURUS:
4574 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004575 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 : (type == CTRL_X_THESAURUS
4577 ? (*curbuf->b_p_tsr == NUL
4578 ? p_tsr
4579 : curbuf->b_p_tsr)
4580 : (*curbuf->b_p_dict == NUL
4581 ? p_dict
4582 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004583 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004584 dict != NULL ? dict_f
4585 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 dict = NULL;
4587 break;
4588
4589 case CTRL_X_TAGS:
4590 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4591 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004592 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004594 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004595 * of matches is found when compl_pattern is empty */
4596 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004597 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4598 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4600 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004601 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603 p_ic = save_p_ic;
4604 break;
4605
4606 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004607 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4609 {
4610
4611 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004613 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615 break;
4616
4617 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004618 if (expand_cmdline(&compl_xp, compl_pattern,
4619 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004621 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 break;
4623
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004624#ifdef FEAT_COMPL_FUNC
4625 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004626 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004627 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004628 break;
4629#endif
4630
Bram Moolenaar488c6512005-08-11 20:09:58 +00004631 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004632#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004633 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004634 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004635 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004636 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004637#endif
4638 break;
4639
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 default: /* normal ^P/^N and ^X^L */
4641 /*
4642 * If 'infercase' is set, don't use 'smartcase' here
4643 */
4644 save_p_scs = p_scs;
4645 if (ins_buf->b_p_inf)
4646 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004647
Bram Moolenaar361aa502014-01-23 22:45:58 +01004648 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 * end but never from the middle, thus setting nowrapscan in this
4650 * buffers is a good idea, on the other hand, we always set
4651 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4652 save_p_ws = p_ws;
4653 if (ins_buf != curbuf)
4654 p_ws = FALSE;
4655 else if (*e_cpt == '.')
4656 p_ws = TRUE;
4657 for (;;)
4658 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004659 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004661 ++msg_silent; /* Don't want messages for wrapscan. */
4662
Bram Moolenaare4214502015-03-05 18:08:43 +01004663 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4664 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004665 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004666 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004667 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004669 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004671 found_new_match = searchit(NULL, ins_buf, pos,
4672 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004673 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004674 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004675 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004676 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004678 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004679 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 first_match_pos = *pos;
4681 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004682 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
4684 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004685 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 found_new_match = FAIL;
4687 if (found_new_match == FAIL)
4688 {
4689 if (ins_buf == curbuf)
4690 found_all = TRUE;
4691 break;
4692 }
4693
4694 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004695 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 && ini->lnum == pos->lnum
4697 && ini->col == pos->col)
4698 continue;
4699 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004700 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004702 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
4704 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4705 continue;
4706 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4707 if (!p_paste)
4708 ptr = skipwhite(ptr);
4709 }
4710 len = (int)STRLEN(ptr);
4711 }
4712 else
4713 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004714 char_u *tmp_ptr = ptr;
4715
4716 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004718 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 /* Skip if already inside a word. */
4720 if (vim_iswordp(tmp_ptr))
4721 continue;
4722 /* Find start of next word. */
4723 tmp_ptr = find_word_start(tmp_ptr);
4724 }
4725 /* Find end of this word. */
4726 tmp_ptr = find_word_end(tmp_ptr);
4727 len = (int)(tmp_ptr - ptr);
4728
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004729 if ((compl_cont_status & CONT_ADDING)
4730 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 {
4732 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4733 {
4734 /* Try next line, if any. the new word will be
4735 * "join" as if the normal command "J" was used.
4736 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004737 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 * works -- Acevedo */
4739 STRNCPY(IObuff, ptr, len);
4740 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4741 tmp_ptr = ptr = skipwhite(ptr);
4742 /* Find start of next word. */
4743 tmp_ptr = find_word_start(tmp_ptr);
4744 /* Find end of next word. */
4745 tmp_ptr = find_word_end(tmp_ptr);
4746 if (tmp_ptr > ptr)
4747 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004748 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004750 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 IObuff[len++] = ' ';
4752 /* IObuf =~ "\k.* ", thus len >= 2 */
4753 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004754 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 || (vim_strchr(p_cpo, CPO_JOINSP)
4756 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004757 && (IObuff[len - 2] == '?'
4758 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 IObuff[len++] = ' ';
4760 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004761 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if (tmp_ptr - ptr >= IOSIZE - len)
4763 tmp_ptr = ptr + IOSIZE - len - 1;
4764 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4765 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004766 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768 IObuff[len] = NUL;
4769 ptr = IObuff;
4770 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004771 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 continue;
4773 }
4774 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004775 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004776 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004777 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
4779 found_new_match = OK;
4780 break;
4781 }
4782 }
4783 p_scs = save_p_scs;
4784 p_ws = save_p_ws;
4785 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004786
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004787 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004788 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004789 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 found_new_match = OK;
4791
4792 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004793 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4794 * match */
4795 if ((ctrl_x_mode != CTRL_X_NORMAL
4796 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004797 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004798 {
4799 if (got_int)
4800 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004801 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004802 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004803 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004804
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004805 if ((ctrl_x_mode != CTRL_X_NORMAL
4806 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004807 || compl_interrupted)
4808 break;
4809 compl_started = TRUE;
4810 }
4811 else
4812 {
4813 /* Mark a buffer scanned when it has been scanned completely */
4814 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4815 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004817 compl_started = FALSE;
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004820 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004822 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 && *e_cpt == NUL) /* Got to end of 'complete' */
4824 found_new_match = FAIL;
4825
4826 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004827 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4828 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 i = ins_compl_make_cyclic();
4830
Bram Moolenaar4475b622017-05-01 20:46:52 +02004831 if (compl_old_match != NULL)
4832 {
4833 /* If several matches were added (FORWARD) or the search failed and has
4834 * just been made cyclic then we have to move compl_curr_match to the
4835 * next or previous entry (if any) -- Acevedo */
4836 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4837 : compl_old_match->cp_prev;
4838 if (compl_curr_match == NULL)
4839 compl_curr_match = compl_old_match;
4840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 return i;
4842}
4843
4844/* Delete the old text being completed. */
4845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004846ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004848 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
4850 /*
4851 * In insert mode: Delete the typed part.
4852 * In replace mode: Put the old characters back, if any.
4853 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004854 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4855 if ((int)curwin->w_cursor.col > col)
4856 {
4857 if (stop_arrow() == FAIL)
4858 return;
4859 backspace_until_column(col);
4860 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004861
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004862 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4863 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004865 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004866 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867}
4868
Bram Moolenaar472e8592016-10-15 17:06:47 +02004869/*
4870 * Insert the new text being completed.
4871 * "in_compl_func" is TRUE when called from complete_check().
4872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004874ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004876 dict_T *dict;
4877
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004878 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004879 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4880 compl_used_match = FALSE;
4881 else
4882 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004883
4884 /* Set completed item. */
4885 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004886 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004887 if (dict != NULL)
4888 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004889 dict_add_string(dict, "word", compl_shown_match->cp_str);
4890 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4891 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4892 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4893 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4894 dict_add_string(dict, "user_data",
4895 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004896 }
4897 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004898 if (!in_compl_func)
4899 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900}
4901
4902/*
4903 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004904 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4905 * get more completions. If it is FALSE, then we just do nothing when there
4906 * are no more completions in a given direction. The latter case is used when
4907 * we are still in the middle of finding completions, to allow browsing
4908 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 * Return the total number of matches, or -1 if still unknown -- webb.
4910 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004911 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4912 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 *
4914 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004915 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4916 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 */
4918 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004919ins_compl_next(
4920 int allow_get_expansion,
4921 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004922 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004923 int insert_match, /* Insert the newly selected match */
4924 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
4926 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004927 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004928 compl_T *found_compl = NULL;
4929 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004930 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004931 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004933 /* When user complete function return -1 for findstart which is next
4934 * time of 'always', compl_shown_match become NULL. */
4935 if (compl_shown_match == NULL)
4936 return -1;
4937
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004938 if (compl_leader != NULL
4939 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004941 /* Set "compl_shown_match" to the actually shown match, it may differ
4942 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004943 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004944 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004945 && compl_shown_match->cp_next != NULL
4946 && compl_shown_match->cp_next != compl_first_match)
4947 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004948
4949 /* If we didn't find it searching forward, and compl_shows_dir is
4950 * backward, find the last match. */
4951 if (compl_shows_dir == BACKWARD
4952 && !ins_compl_equal(compl_shown_match,
4953 compl_leader, (int)STRLEN(compl_leader))
4954 && (compl_shown_match->cp_next == NULL
4955 || compl_shown_match->cp_next == compl_first_match))
4956 {
4957 while (!ins_compl_equal(compl_shown_match,
4958 compl_leader, (int)STRLEN(compl_leader))
4959 && compl_shown_match->cp_prev != NULL
4960 && compl_shown_match->cp_prev != compl_first_match)
4961 compl_shown_match = compl_shown_match->cp_prev;
4962 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004963 }
4964
4965 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004966 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 /* Delete old text to be replaced */
4968 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004969
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004970 /* When finding the longest common text we stick at the original text,
4971 * don't let CTRL-N or CTRL-P move to the first match. */
4972 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4973
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004974 /* When restarting the search don't insert the first match either. */
4975 if (compl_restarting)
4976 {
4977 advance = FALSE;
4978 compl_restarting = FALSE;
4979 }
4980
Bram Moolenaare3226be2005-12-18 22:10:00 +00004981 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4982 * around. */
4983 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004985 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004987 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004988 found_end = (compl_first_match != NULL
4989 && (compl_shown_match->cp_next == compl_first_match
4990 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004991 }
4992 else if (compl_shows_dir == BACKWARD
4993 && compl_shown_match->cp_prev != NULL)
4994 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004995 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004996 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004997 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
4999 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00005000 {
Bram Moolenaara260a972006-06-23 19:36:29 +00005001 if (!allow_get_expansion)
5002 {
5003 if (advance)
5004 {
5005 if (compl_shows_dir == BACKWARD)
5006 compl_pending -= todo + 1;
5007 else
5008 compl_pending += todo + 1;
5009 }
5010 return -1;
5011 }
5012
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005013 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005014 {
5015 if (compl_shows_dir == BACKWARD)
5016 --compl_pending;
5017 else
5018 ++compl_pending;
5019 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005020
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005021 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005022 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005023
5024 /* handle any pending completions */
5025 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005026 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005027 {
5028 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5029 {
5030 compl_shown_match = compl_shown_match->cp_next;
5031 --compl_pending;
5032 }
5033 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5034 {
5035 compl_shown_match = compl_shown_match->cp_prev;
5036 ++compl_pending;
5037 }
5038 else
5039 break;
5040 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005041 found_end = FALSE;
5042 }
5043 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5044 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005045 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005046 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005047 ++todo;
5048 else
5049 /* Remember a matching item. */
5050 found_compl = compl_shown_match;
5051
5052 /* Stop at the end of the list when we found a usable match. */
5053 if (found_end)
5054 {
5055 if (found_compl != NULL)
5056 {
5057 compl_shown_match = found_compl;
5058 break;
5059 }
5060 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005064 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005065 if (compl_no_insert && !started)
5066 {
5067 ins_bytes(compl_orig_text + ins_compl_len());
5068 compl_used_match = FALSE;
5069 }
5070 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005071 {
5072 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005073 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005074 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005075 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005076 }
5077 else
5078 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079
5080 if (!allow_get_expansion)
5081 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005082 /* may undisplay the popup menu first */
5083 ins_compl_upd_pum();
5084
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005085 /* redraw to show the user what was inserted */
5086 update_screen(0);
5087
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005088 /* display the updated popup menu */
5089 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005090#ifdef FEAT_GUI
5091 if (gui.in_use)
5092 {
5093 /* Show the cursor after the match, not after the redrawn text. */
5094 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005095 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005096 }
5097#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 /* Delete old text to be replaced, since we're still searching and
5100 * don't want to match ourselves! */
5101 ins_compl_delete();
5102 }
5103
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005104 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005105 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005106 if (compl_no_insert && !started)
5107 compl_enter_selects = TRUE;
5108 else
5109 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 /*
5112 * Show the file name for the match (if any)
5113 * Truncate the file name to avoid a wait for return.
5114 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005115 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005117 char *lead = _("match in file");
5118 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5119 char_u *s;
5120 char_u *e;
5121
5122 if (space > 0)
5123 {
5124 /* We need the tail that fits. With double-byte encoding going
5125 * back from the end is very slow, thus go from the start and keep
5126 * the text that fits in "space" between "s" and "e". */
5127 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5128 {
5129 space -= ptr2cells(e);
5130 while (space < 0)
5131 {
5132 space += ptr2cells(s);
5133 MB_PTR_ADV(s);
5134 }
5135 }
5136 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5137 s > compl_shown_match->cp_fname ? "<" : "", s);
5138 msg(IObuff);
5139 redraw_cmdline = FALSE; /* don't overwrite! */
5140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142
5143 return num_matches;
5144}
5145
5146/*
5147 * Call this while finding completions, to check whether the user has hit a key
5148 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005149 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005151 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005152 * "in_compl_func" is TRUE when called from complete_check(), don't set
5153 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 */
5155 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005156ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157{
5158 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005159 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005161 /* Don't check when reading keys from a script, :normal or feedkeys().
5162 * That would break the test scripts. But do check for keys when called
5163 * from complete_check(). */
5164 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 return;
5166
5167 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005168 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 return;
5170 count = 0;
5171
Bram Moolenaara260a972006-06-23 19:36:29 +00005172 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5173 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (c != NUL)
5176 {
5177 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5178 {
5179 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005180 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005181 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005182 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005184 else
5185 {
5186 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005187 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005188 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005189 if (c != K_IGNORE)
5190 {
5191 /* Don't interrupt completion when the character wasn't typed,
5192 * e.g., when doing @q to replay keys. */
5193 if (c != Ctrl_R && KeyTyped)
5194 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005195
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005196 vungetc(c);
5197 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005200 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005201 {
5202 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5203
5204 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005205 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005206 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005207}
5208
5209/*
5210 * Decide the direction of Insert mode complete from the key typed.
5211 * Returns BACKWARD or FORWARD.
5212 */
5213 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005214ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005215{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005216 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005217 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005218 return BACKWARD;
5219 return FORWARD;
5220}
5221
5222/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005223 * Return TRUE for keys that are used for completion only when the popup menu
5224 * is visible.
5225 */
5226 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005227ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005228{
5229 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005230 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5231 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005232}
5233
5234/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005235 * Decide the number of completions to move forward.
5236 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5237 */
5238 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005239ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005240{
5241 int h;
5242
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005243 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005244 {
5245 h = pum_get_height();
5246 if (h > 3)
5247 h -= 2; /* keep some context */
5248 return h;
5249 }
5250 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251}
5252
5253/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005254 * Return TRUE if completion with "c" should insert the match, FALSE if only
5255 * to change the currently selected completion.
5256 */
5257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005258ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005259{
5260 switch (c)
5261 {
5262 case K_UP:
5263 case K_DOWN:
5264 case K_PAGEDOWN:
5265 case K_KPAGEDOWN:
5266 case K_S_DOWN:
5267 case K_PAGEUP:
5268 case K_KPAGEUP:
5269 case K_S_UP:
5270 return FALSE;
5271 }
5272 return TRUE;
5273}
5274
5275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 * Do Insert mode completion.
5277 * Called when character "c" was typed, which has a meaning for completion.
5278 * Returns OK if completion was done, FAIL if something failed (out of mem).
5279 */
5280 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005281ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005283 char_u *line;
5284 int startcol = 0; /* column where searched text starts */
5285 colnr_T curs_col; /* cursor column */
5286 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005287 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005288 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005289 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005290 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
Bram Moolenaare3226be2005-12-18 22:10:00 +00005292 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005293 insert_match = ins_compl_use_match(c);
5294
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005295 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 {
5297 /* First time we hit ^N or ^P (in a row, I mean) */
5298
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 did_ai = FALSE;
5300#ifdef FEAT_SMARTINDENT
5301 did_si = FALSE;
5302 can_si = FALSE;
5303 can_si_back = FALSE;
5304#endif
5305 if (stop_arrow() == FAIL)
5306 return FAIL;
5307
5308 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005309 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005310 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005312 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 * "compl_startpos" to the cursor as a pattern to add a new word
5314 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005315 * "compl_startpos" is not in the same line as the cursor then fix it
5316 * (the line has been split because it was longer than 'tw'). if SOL
5317 * is set then skip the previous pattern, a word at the beginning of
5318 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005319 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5320 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
5322 /*
5323 * it is a continued search
5324 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005325 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005326 if (ctrl_x_mode == CTRL_X_NORMAL
5327 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5328 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005330 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005332 /* line (probably) wrapped, set compl_startpos to the
5333 * first non_blank in the line, if it is not a wordchar
5334 * include it to get a better pattern, but then we don't
5335 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005336 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 compl_startpos.col = compl_col;
5338 compl_startpos.lnum = curwin->w_cursor.lnum;
5339 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
5341 else
5342 {
5343 /* S_IPOS was set when we inserted a word that was at the
5344 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005345 * mode but first we need to redefine compl_startpos */
5346 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 compl_cont_status |= CONT_SOL;
5349 compl_startpos.col = (colnr_T)(skipwhite(
5350 line + compl_length
5351 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005353 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005356 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005357 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005359 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005361 compl_cont_status &= ~CONT_SOL;
5362 compl_length = (IOSIZE - MIN_SPACE);
5363 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5366 if (compl_length < 1)
5367 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005369 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005370 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005372 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 }
5374 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005377 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005379 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005380 if (ctrl_x_mode != CTRL_X_NORMAL)
5381 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005382 compl_cont_status = 0;
5383 compl_cont_status |= CONT_N_ADDS;
5384 compl_startpos = curwin->w_cursor;
5385 startcol = (int)curs_col;
5386 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388
5389 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005390 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005392 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5394 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005395 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005397 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005399 compl_col += ++startcol;
5400 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
5402 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005403 compl_pattern = str_foldcase(line + compl_col,
5404 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005406 compl_pattern = vim_strnsave(line + compl_col,
5407 compl_length);
5408 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 return FAIL;
5410 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005411 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 {
5413 char_u *prefix = (char_u *)"\\<";
5414
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005415 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005416 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005417 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005418 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005420 if (!vim_iswordp(line + compl_col)
5421 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 && (
5423#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005424 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005426 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427#endif
5428 )))
5429 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005430 STRCPY((char *)compl_pattern, prefix);
5431 (void)quote_meta(compl_pattern + STRLEN(prefix),
5432 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005434 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005438 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439#endif
5440 )
5441 {
5442 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005443 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5444 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005446 compl_col += curs_col;
5447 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 }
5449 else
5450 {
5451#ifdef FEAT_MBYTE
5452 /* Search the point of change class of multibyte character
5453 * or not a word single byte character backward. */
5454 if (has_mbyte)
5455 {
5456 int base_class;
5457 int head_off;
5458
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005459 startcol -= (*mb_head_off)(line, line + startcol);
5460 base_class = mb_get_class(line + startcol);
5461 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005463 head_off = (*mb_head_off)(line, line + startcol);
5464 if (base_class != mb_get_class(line + startcol
5465 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005467 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
5469 }
5470 else
5471#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005472 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005474 compl_col += ++startcol;
5475 compl_length = (int)curs_col - startcol;
5476 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 {
5478 /* Only match word with at least two chars -- webb
5479 * there's no need to call quote_meta,
5480 * alloc(7) is enough -- Acevedo
5481 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005482 compl_pattern = alloc(7);
5483 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005485 STRCPY((char *)compl_pattern, "\\<");
5486 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5487 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 }
5489 else
5490 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005491 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005492 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005493 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005495 STRCPY((char *)compl_pattern, "\\<");
5496 (void)quote_meta(compl_pattern + 2, line + compl_col,
5497 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
5499 }
5500 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005501 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005503 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005504 compl_length = (int)curs_col - (int)compl_col;
5505 if (compl_length < 0) /* cursor in indent: empty pattern */
5506 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005508 compl_pattern = str_foldcase(line + compl_col, compl_length,
5509 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005511 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5512 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 return FAIL;
5514 }
5515 else if (ctrl_x_mode == CTRL_X_FILES)
5516 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005517 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005518 if (startcol > 0)
5519 {
5520 char_u *p = line + startcol;
5521
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005522 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005523 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005524 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005525 if (p == line && vim_isfilec(PTR2CHAR(p)))
5526 startcol = 0;
5527 else
5528 startcol = (int)(p - line) + 1;
5529 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005530
Bram Moolenaar0300e462013-09-08 16:03:45 +02005531 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005532 compl_length = (int)curs_col - startcol;
5533 compl_pattern = addstar(line + compl_col, compl_length,
5534 EXPAND_FILES);
5535 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 return FAIL;
5537 }
5538 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5539 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005540 compl_pattern = vim_strnsave(line, curs_col);
5541 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005543 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005544 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005545 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5546 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005547 /* No completion possible, use an empty pattern to get a
5548 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005549 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005550 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005551 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5552 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005554 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005555 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005556#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005557 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005558 * Call user defined function 'completefunc' with "a:findstart"
5559 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005560 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005561 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005562 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005563 char_u *funcname;
5564 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005565 win_T *curwin_save;
5566 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005567
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005568 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005569 * string */
5570 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5571 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5572 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005573 {
5574 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5575 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005576 /* restore did_ai, so that adding comment leader works */
5577 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005578 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005579 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005580
Bram Moolenaarffa96842018-06-12 22:05:14 +02005581 args[0].v_type = VAR_NUMBER;
5582 args[0].vval.v_number = 1;
5583 args[1].v_type = VAR_STRING;
5584 args[1].vval.v_string = (char_u *)"";
5585 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005586 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005587 curwin_save = curwin;
5588 curbuf_save = curbuf;
Bram Moolenaarded27a12018-08-01 19:06:03 +02005589 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005590 if (curwin_save != curwin || curbuf_save != curbuf)
5591 {
5592 EMSG(_(e_complwin));
5593 return FAIL;
5594 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005595 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005596 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005597 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005598 {
5599 EMSG(_(e_compldel));
5600 return FAIL;
5601 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005602
Bram Moolenaar6110a002012-01-26 18:58:38 +01005603 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005604 * cancel the complete without an error.
5605 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005606 if (col == -2)
5607 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005608 if (col == -3)
5609 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005610 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005611 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005612 if (!shortmess(SHM_COMPLETIONMENU))
5613 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005614 return FAIL;
5615 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005616
Bram Moolenaar82139082011-09-14 16:52:09 +02005617 /*
5618 * Reset extended parameters of completion, when start new
5619 * completion.
5620 */
5621 compl_opt_refresh_always = FALSE;
5622
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005623 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005624 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005625 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005626 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005627 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005628
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005629 /* Setup variables for completion. Need to obtain "line" again,
5630 * it may have become invalid. */
5631 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005632 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005633 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5634 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005635#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005636 return FAIL;
5637 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005638 else if (ctrl_x_mode == CTRL_X_SPELL)
5639 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005640#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005641 if (spell_bad_len > 0)
5642 compl_col = curs_col - spell_bad_len;
5643 else
5644 compl_col = spell_word_start(startcol);
5645 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005646 {
5647 compl_length = 0;
5648 compl_col = curs_col;
5649 }
5650 else
5651 {
5652 spell_expand_check_cap(compl_col);
5653 compl_length = (int)curs_col - compl_col;
5654 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005655 /* Need to obtain "line" again, it may have become invalid. */
5656 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005657 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5658 if (compl_pattern == NULL)
5659#endif
5660 return FAIL;
5661 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005662 else
5663 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005664 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005665 return FAIL;
5666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005668 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 {
5670 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005671 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
5673 /* Insert a new line, keep indentation but ignore 'comments' */
5674#ifdef FEAT_COMMENTS
5675 char_u *old = curbuf->b_p_com;
5676
5677 curbuf->b_p_com = (char_u *)"";
5678#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005679 compl_startpos.lnum = curwin->w_cursor.lnum;
5680 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 ins_eol('\r');
5682#ifdef FEAT_COMMENTS
5683 curbuf->b_p_com = old;
5684#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005685 compl_length = 0;
5686 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 }
5689 else
5690 {
5691 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005692 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
5694
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005695 if (compl_cont_status & CONT_LOCAL)
5696 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 else
5698 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5699
Bram Moolenaar62951b12011-09-21 18:23:05 +02005700 /* If any of the original typed text has been changed we need to fix
5701 * the redo buffer. */
5702 ins_compl_fixRedoBufForLeader(NULL);
5703
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005704 /* Always add completion for the original text. */
5705 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005706 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5707 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005708 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005710 VIM_CLEAR(compl_pattern);
5711 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 return FAIL;
5713 }
5714
5715 /* showmode might reset the internal line pointers, so it must
5716 * be called before line = ml_get(), or when this address is no
5717 * longer needed. -- Acevedo.
5718 */
5719 edit_submode_extra = (char_u *)_("-- Searching...");
5720 edit_submode_highl = HLF_COUNT;
5721 showmode();
5722 edit_submode_extra = NULL;
5723 out_flush();
5724 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005725 else if (insert_match && stop_arrow() == FAIL)
5726 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005728 compl_shown_match = compl_curr_match;
5729 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730
5731 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005732 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005734 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005735 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005736 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005738 /* may undisplay the popup menu */
5739 ins_compl_upd_pum();
5740
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005741 if (n > 1) /* all matches have been found */
5742 compl_matches = n;
5743 compl_curr_match = compl_shown_match;
5744 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
Bram Moolenaard68071d2006-05-02 22:08:30 +00005746 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5747 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 if (got_int && !global_busy)
5749 {
5750 (void)vgetc();
5751 got_int = FALSE;
5752 }
5753
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005754 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005755 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005757 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5758 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5760 edit_submode_highl = HLF_E;
5761 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5762 * because we couldn't expand anything at first place, but if we used
5763 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5764 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005765 if ( compl_length > 1
5766 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005767 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5769 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005770 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 }
5772
Bram Moolenaar572cb562005-08-05 21:35:02 +00005773 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005774 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005776 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777
5778 if (edit_submode_extra == NULL)
5779 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005780 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 {
5782 edit_submode_extra = (char_u *)_("Back at original");
5783 edit_submode_highl = HLF_W;
5784 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005785 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 {
5787 edit_submode_extra = (char_u *)_("Word from other line");
5788 edit_submode_highl = HLF_COUNT;
5789 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005790 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 {
5792 edit_submode_extra = (char_u *)_("The only match");
5793 edit_submode_highl = HLF_COUNT;
5794 }
5795 else
5796 {
5797 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005798 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005800 int number = 0;
5801 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005803 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {
5805 /* search backwards for the first valid (!= -1) number.
5806 * This should normally succeed already at the first loop
5807 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005808 for (match = compl_curr_match->cp_prev; match != NULL
5809 && match != compl_first_match;
5810 match = match->cp_prev)
5811 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005813 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 break;
5815 }
5816 if (match != NULL)
5817 /* go up and assign all numbers which are not assigned
5818 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005819 for (match = match->cp_next;
5820 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005821 match = match->cp_next)
5822 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 }
5824 else /* BACKWARD */
5825 {
5826 /* search forwards (upwards) for the first valid (!= -1)
5827 * number. This should normally succeed already at the
5828 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005829 for (match = compl_curr_match->cp_next; match != NULL
5830 && match != compl_first_match;
5831 match = match->cp_next)
5832 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005834 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 break;
5836 }
5837 if (match != NULL)
5838 /* go down and assign all numbers which are not
5839 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005840 for (match = match->cp_prev; match
5841 && match->cp_number == -1;
5842 match = match->cp_prev)
5843 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 }
5845 }
5846
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005847 /* The match should always have a sequence number now, this is
5848 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005849 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005851 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5852 * Translations may need more than twice that. */
5853 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005855 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005856 vim_snprintf((char *)match_ref, sizeof(match_ref),
5857 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005858 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005860 vim_snprintf((char *)match_ref, sizeof(match_ref),
5861 _("match %d"),
5862 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 edit_submode_extra = match_ref;
5864 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005865 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 curs_columns(FALSE);
5867 }
5868 }
5869 }
5870
5871 /* Show a message about what (completion) mode we're in. */
5872 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005873 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005875 if (edit_submode_extra != NULL)
5876 {
5877 if (!p_smd)
5878 msg_attr(edit_submode_extra,
5879 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005880 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005881 }
5882 else
5883 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885
Bram Moolenaard68071d2006-05-02 22:08:30 +00005886 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005887 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005888 show_pum(save_w_wrow, save_w_leftcol);
5889
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005890 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005891 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005892
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 return OK;
5894}
5895
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005896 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005897show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005898{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005899 /* RedrawingDisabled may be set when invoked through complete(). */
5900 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005901
Bram Moolenaarcb036422017-03-01 12:29:10 +01005902 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005903
Bram Moolenaarcb036422017-03-01 12:29:10 +01005904 /* If the cursor moved or the display scrolled we need to remove the pum
5905 * first. */
5906 setcursor();
5907 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5908 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005909
Bram Moolenaarcb036422017-03-01 12:29:10 +01005910 ins_compl_show_pum();
5911 setcursor();
5912 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005913}
5914
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915/*
5916 * Looks in the first "len" chars. of "src" for search-metachars.
5917 * If dest is not NULL the chars. are copied there quoting (with
5918 * a backslash) the metachars, and dest would be NUL terminated.
5919 * Returns the length (needed) of dest
5920 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005921 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005922quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005924 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005926 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 {
5928 switch (*src)
5929 {
5930 case '.':
5931 case '*':
5932 case '[':
5933 if (ctrl_x_mode == CTRL_X_DICTIONARY
5934 || ctrl_x_mode == CTRL_X_THESAURUS)
5935 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005936 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 case '~':
5938 if (!p_magic) /* quote these only if magic is set */
5939 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005940 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 case '\\':
5942 if (ctrl_x_mode == CTRL_X_DICTIONARY
5943 || ctrl_x_mode == CTRL_X_THESAURUS)
5944 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005945 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 case '^': /* currently it's not needed. */
5947 case '$':
5948 m++;
5949 if (dest != NULL)
5950 *dest++ = '\\';
5951 break;
5952 }
5953 if (dest != NULL)
5954 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005955# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 /* Copy remaining bytes of a multibyte character. */
5957 if (has_mbyte)
5958 {
5959 int i, mb_len;
5960
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005961 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 if (mb_len > 0 && len >= mb_len)
5963 for (i = 0; i < mb_len; ++i)
5964 {
5965 --len;
5966 ++src;
5967 if (dest != NULL)
5968 *dest++ = *src;
5969 }
5970 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 }
5973 if (dest != NULL)
5974 *dest = NUL;
5975
5976 return m;
5977}
5978#endif /* FEAT_INS_EXPAND */
5979
5980/*
5981 * Next character is interpreted literally.
5982 * A one, two or three digit decimal number is interpreted as its byte value.
5983 * If one or two digits are entered, the next character is given to vungetc().
5984 * For Unicode a character > 255 may be returned.
5985 */
5986 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005987get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 int cc;
5990 int nc;
5991 int i;
5992 int hex = FALSE;
5993 int octal = FALSE;
5994#ifdef FEAT_MBYTE
5995 int unicode = 0;
5996#endif
5997
5998 if (got_int)
5999 return Ctrl_C;
6000
6001#ifdef FEAT_GUI
6002 /*
6003 * In GUI there is no point inserting the internal code for a special key.
6004 * It is more useful to insert the string "<KEY>" instead. This would
6005 * probably be useful in a text window too, but it would not be
6006 * vi-compatible (maybe there should be an option for it?) -- webb
6007 */
6008 if (gui.in_use)
6009 ++allow_keys;
6010#endif
6011#ifdef USE_ON_FLY_SCROLL
6012 dont_scroll = TRUE; /* disallow scrolling here */
6013#endif
6014 ++no_mapping; /* don't map the next key hits */
6015 cc = 0;
6016 i = 0;
6017 for (;;)
6018 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006019 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020#ifdef FEAT_CMDL_INFO
6021 if (!(State & CMDLINE)
6022# ifdef FEAT_MBYTE
6023 && MB_BYTE2LEN_CHECK(nc) == 1
6024# endif
6025 )
6026 add_to_showcmd(nc);
6027#endif
6028 if (nc == 'x' || nc == 'X')
6029 hex = TRUE;
6030 else if (nc == 'o' || nc == 'O')
6031 octal = TRUE;
6032#ifdef FEAT_MBYTE
6033 else if (nc == 'u' || nc == 'U')
6034 unicode = nc;
6035#endif
6036 else
6037 {
6038 if (hex
6039#ifdef FEAT_MBYTE
6040 || unicode != 0
6041#endif
6042 )
6043 {
6044 if (!vim_isxdigit(nc))
6045 break;
6046 cc = cc * 16 + hex2nr(nc);
6047 }
6048 else if (octal)
6049 {
6050 if (nc < '0' || nc > '7')
6051 break;
6052 cc = cc * 8 + nc - '0';
6053 }
6054 else
6055 {
6056 if (!VIM_ISDIGIT(nc))
6057 break;
6058 cc = cc * 10 + nc - '0';
6059 }
6060
6061 ++i;
6062 }
6063
6064 if (cc > 255
6065#ifdef FEAT_MBYTE
6066 && unicode == 0
6067#endif
6068 )
6069 cc = 255; /* limit range to 0-255 */
6070 nc = 0;
6071
6072 if (hex) /* hex: up to two chars */
6073 {
6074 if (i >= 2)
6075 break;
6076 }
6077#ifdef FEAT_MBYTE
6078 else if (unicode) /* Unicode: up to four or eight chars */
6079 {
6080 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6081 break;
6082 }
6083#endif
6084 else if (i >= 3) /* decimal or octal: up to three chars */
6085 break;
6086 }
6087 if (i == 0) /* no number entered */
6088 {
6089 if (nc == K_ZERO) /* NUL is stored as NL */
6090 {
6091 cc = '\n';
6092 nc = 0;
6093 }
6094 else
6095 {
6096 cc = nc;
6097 nc = 0;
6098 }
6099 }
6100
6101 if (cc == 0) /* NUL is stored as NL */
6102 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006103#ifdef FEAT_MBYTE
6104 if (enc_dbcs && (cc & 0xff) == 0)
6105 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6106 second byte will cause trouble! */
6107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
6109 --no_mapping;
6110#ifdef FEAT_GUI
6111 if (gui.in_use)
6112 --allow_keys;
6113#endif
6114 if (nc)
6115 vungetc(nc);
6116 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6117 return cc;
6118}
6119
6120/*
6121 * Insert character, taking care of special keys and mod_mask
6122 */
6123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006124insert_special(
6125 int c,
6126 int allow_modmask,
6127 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128{
6129 char_u *p;
6130 int len;
6131
6132 /*
6133 * Special function key, translate into "<Key>". Up to the last '>' is
6134 * inserted with ins_str(), so as not to replace characters in replace
6135 * mode.
6136 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6137 * unless 'allow_modmask' is TRUE.
6138 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006139#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 /* Command-key never produces a normal key */
6141 if (mod_mask & MOD_MASK_CMD)
6142 allow_modmask = TRUE;
6143#endif
6144 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6145 {
6146 p = get_special_key_name(c, mod_mask);
6147 len = (int)STRLEN(p);
6148 c = p[len - 1];
6149 if (len > 2)
6150 {
6151 if (stop_arrow() == FAIL)
6152 return;
6153 p[len - 1] = NUL;
6154 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006155 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 ctrlv = FALSE;
6157 }
6158 }
6159 if (stop_arrow() == OK)
6160 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6161}
6162
6163/*
6164 * Special characters in this context are those that need processing other
6165 * than the simple insertion that can be performed here. This includes ESC
6166 * which terminates the insert, and CR/NL which need special processing to
6167 * open up a new line. This routine tries to optimize insertions performed by
6168 * the "redo", "undo" or "put" commands, so it needs to know when it should
6169 * stop and defer processing to the "normal" mechanism.
6170 * '0' and '^' are special, because they can be followed by CTRL-D.
6171 */
6172#ifdef EBCDIC
6173# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6174#else
6175# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6176#endif
6177
6178#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006179# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006181# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182#endif
6183
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006184/*
6185 * "flags": INSCHAR_FORMAT - force formatting
6186 * INSCHAR_CTRLV - char typed just after CTRL-V
6187 * INSCHAR_NO_FEX - don't use 'formatexpr'
6188 *
6189 * NOTE: passes the flags value straight through to internal_format() which,
6190 * beside INSCHAR_FORMAT (above), is also looking for these:
6191 * INSCHAR_DO_COM - format comments
6192 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006195insertchar(
6196 int c, /* character to insert or NUL */
6197 int flags, /* INSCHAR_FORMAT, etc. */
6198 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 int textwidth;
6201#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006205 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006207 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
6210 /*
6211 * Try to break the line in two or more pieces when:
6212 * - Always do this if we have been called to do formatting only.
6213 * - Always do this when 'formatoptions' has the 'a' flag and the line
6214 * ends in white space.
6215 * - Otherwise:
6216 * - Don't do this if inserting a blank
6217 * - Don't do this if an existing character is being replaced, unless
6218 * we're in VREPLACE mode.
6219 * - Do this if the cursor is not on the line where insert started
6220 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6221 * before the insert.
6222 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6223 * before 'textwidth'
6224 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006225 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006226 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006227 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 && *ml_get_cursor() != NUL)
6231 && (curwin->w_cursor.lnum != Insstart.lnum
6232 || ((!has_format_option(FO_INS_LONG)
6233 || Insstart_textlen <= (colnr_T)textwidth)
6234 && (!fo_ins_blank
6235 || Insstart_blank_vcol <= (colnr_T)textwidth
6236 ))))))
6237 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006238 /* Format with 'formatexpr' when it's set. Use internal formatting
6239 * when 'formatexpr' isn't set or it returns non-zero. */
6240#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006241 int do_internal = TRUE;
6242 colnr_T virtcol = get_nolist_virtcol()
6243 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006244
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006245 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6246 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006247 {
6248 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6249 /* It may be required to save for undo again, e.g. when setline()
6250 * was called. */
6251 ins_need_undo = TRUE;
6252 }
6253 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006255 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006257
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (c == NUL) /* only formatting was wanted */
6259 return;
6260
6261#ifdef FEAT_COMMENTS
6262 /* Check whether this character should end a comment. */
6263 if (did_ai && (int)c == end_comment_pending)
6264 {
6265 char_u *line;
6266 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6267 int middle_len, end_len;
6268 int i;
6269
6270 /*
6271 * Need to remove existing (middle) comment leader and insert end
6272 * comment leader. First, check what comment leader we can find.
6273 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006274 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6276 {
6277 /* Skip middle-comment string */
6278 while (*p && p[-1] != ':') /* find end of middle flags */
6279 ++p;
6280 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6281 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006282 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 --middle_len;
6284
6285 /* Find the end-comment string */
6286 while (*p && p[-1] != ':') /* find end of end flags */
6287 ++p;
6288 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6289
6290 /* Skip white space before the cursor */
6291 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006292 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 ;
6294 i++;
6295
6296 /* Skip to before the middle leader */
6297 i -= middle_len;
6298
6299 /* Check some expected things before we go on */
6300 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6301 {
6302 /* Backspace over all the stuff we want to replace */
6303 backspace_until_column(i);
6304
6305 /*
6306 * Insert the end-comment string, except for the last
6307 * character, which will get inserted as normal later.
6308 */
6309 ins_bytes_len(lead_end, end_len - 1);
6310 }
6311 }
6312 }
6313 end_comment_pending = NUL;
6314#endif
6315
6316 did_ai = FALSE;
6317#ifdef FEAT_SMARTINDENT
6318 did_si = FALSE;
6319 can_si = FALSE;
6320 can_si_back = FALSE;
6321#endif
6322
6323 /*
6324 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6325 * This speeds up normal text input considerably.
6326 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6327 * need to re-indent at a ':', or any other character (but not what
6328 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006329 * Don't do this when there an InsertCharPre autocommand is defined,
6330 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006331 * Do the check for InsertCharPre before the call to vpeekc() because the
6332 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 */
6334#ifdef USE_ON_FLY_SCROLL
6335 dont_scroll = FALSE; /* allow scrolling here */
6336#endif
6337
6338 if ( !ISSPECIAL(c)
6339#ifdef FEAT_MBYTE
6340 && (!has_mbyte || (*mb_char2len)(c) == 1)
6341#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006342 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 && vpeekc() != NUL
6344 && !(State & REPLACE_FLAG)
6345#ifdef FEAT_CINDENT
6346 && !cindent_on()
6347#endif
6348#ifdef FEAT_RIGHTLEFT
6349 && !p_ri
6350#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006351 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 {
6353#define INPUT_BUFLEN 100
6354 char_u buf[INPUT_BUFLEN + 1];
6355 int i;
6356 colnr_T virtcol = 0;
6357
6358 buf[0] = c;
6359 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006360 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 virtcol = get_nolist_virtcol();
6362 /*
6363 * Stop the string when:
6364 * - no more chars available
6365 * - finding a special character (command key)
6366 * - buffer is full
6367 * - running into the 'textwidth' boundary
6368 * - need to check for abbreviation: A non-word char after a word-char
6369 */
6370 while ( (c = vpeekc()) != NUL
6371 && !ISSPECIAL(c)
6372#ifdef FEAT_MBYTE
6373 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6374#endif
6375 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006376# ifdef FEAT_FKMAP
6377 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 && (textwidth == 0
6380 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6381 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6382 {
6383#ifdef FEAT_RIGHTLEFT
6384 c = vgetc();
6385 if (p_hkmap && KeyTyped)
6386 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 buf[i++] = c;
6388#else
6389 buf[i++] = vgetc();
6390#endif
6391 }
6392
6393#ifdef FEAT_DIGRAPHS
6394 do_digraph(-1); /* clear digraphs */
6395 do_digraph(buf[i-1]); /* may be the start of a digraph */
6396#endif
6397 buf[i] = NUL;
6398 ins_str(buf);
6399 if (flags & INSCHAR_CTRLV)
6400 {
6401 redo_literal(*buf);
6402 i = 1;
6403 }
6404 else
6405 i = 0;
6406 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006407 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 }
6409 else
6410 {
6411#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006412 int cc;
6413
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6415 {
6416 char_u buf[MB_MAXBYTES + 1];
6417
6418 (*mb_char2bytes)(c, buf);
6419 buf[cc] = NUL;
6420 ins_char_bytes(buf, cc);
6421 AppendCharToRedobuff(c);
6422 }
6423 else
6424#endif
6425 {
6426 ins_char(c);
6427 if (flags & INSCHAR_CTRLV)
6428 redo_literal(c);
6429 else
6430 AppendCharToRedobuff(c);
6431 }
6432 }
6433}
6434
6435/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006436 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006437 *
6438 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6439 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440 */
6441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006442internal_format(
6443 int textwidth,
6444 int second_indent,
6445 int flags,
6446 int format_only,
6447 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006448{
6449 int cc;
6450 int save_char = NUL;
6451 int haveto_redraw = FALSE;
6452 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6453#ifdef FEAT_MBYTE
6454 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6455#endif
6456 int fo_white_par = has_format_option(FO_WHITE_PAR);
6457 int first_line = TRUE;
6458#ifdef FEAT_COMMENTS
6459 colnr_T leader_len;
6460 int no_leader = FALSE;
6461 int do_comments = (flags & INSCHAR_DO_COM);
6462#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006463#ifdef FEAT_LINEBREAK
6464 int has_lbr = curwin->w_p_lbr;
6465
6466 /* make sure win_lbr_chartabsize() counts correctly */
6467 curwin->w_p_lbr = FALSE;
6468#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006469
6470 /*
6471 * When 'ai' is off we don't want a space under the cursor to be
6472 * deleted. Replace it with an 'x' temporarily.
6473 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006474 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006475 {
6476 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006477 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006478 {
6479 save_char = cc;
6480 pchar_cursor('x');
6481 }
6482 }
6483
6484 /*
6485 * Repeat breaking lines, until the current line is not too long.
6486 */
6487 while (!got_int)
6488 {
6489 int startcol; /* Cursor column at entry */
6490 int wantcol; /* column at textwidth border */
6491 int foundcol; /* column for start of spaces */
6492 int end_foundcol = 0; /* column for start of word */
6493 colnr_T len;
6494 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006495 int orig_col = 0;
6496 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006497 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006498 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006499
Bram Moolenaar97b98102009-11-17 16:41:01 +00006500 virtcol = get_nolist_virtcol()
6501 + char2cells(c != NUL ? c : gchar_cursor());
6502 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006503 break;
6504
6505#ifdef FEAT_COMMENTS
6506 if (no_leader)
6507 do_comments = FALSE;
6508 else if (!(flags & INSCHAR_FORMAT)
6509 && has_format_option(FO_WRAP_COMS))
6510 do_comments = TRUE;
6511
6512 /* Don't break until after the comment leader */
6513 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006514 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006515 else
6516 leader_len = 0;
6517
6518 /* If the line doesn't start with a comment leader, then don't
6519 * start one in a following broken line. Avoids that a %word
6520 * moved to the start of the next line causes all following lines
6521 * to start with %. */
6522 if (leader_len == 0)
6523 no_leader = TRUE;
6524#endif
6525 if (!(flags & INSCHAR_FORMAT)
6526#ifdef FEAT_COMMENTS
6527 && leader_len == 0
6528#endif
6529 && !has_format_option(FO_WRAP))
6530
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006531 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006532 if ((startcol = curwin->w_cursor.col) == 0)
6533 break;
6534
6535 /* find column of textwidth border */
6536 coladvance((colnr_T)textwidth);
6537 wantcol = curwin->w_cursor.col;
6538
Bram Moolenaar97b98102009-11-17 16:41:01 +00006539 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006540 foundcol = 0;
6541
6542 /*
6543 * Find position to break at.
6544 * Stop at first entered white when 'formatoptions' has 'v'
6545 */
6546 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006547 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006548 || curwin->w_cursor.lnum != Insstart.lnum
6549 || curwin->w_cursor.col >= Insstart.col)
6550 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006551 if (curwin->w_cursor.col == startcol && c != NUL)
6552 cc = c;
6553 else
6554 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006555 if (WHITECHAR(cc))
6556 {
6557 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006558 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006559
6560 /* find start of sequence of blanks */
6561 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6562 {
6563 dec_cursor();
6564 cc = gchar_cursor();
6565 }
6566 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6567 break; /* only spaces in front of text */
6568#ifdef FEAT_COMMENTS
6569 /* Don't break until after the comment leader */
6570 if (curwin->w_cursor.col < leader_len)
6571 break;
6572#endif
6573 if (has_format_option(FO_ONE_LETTER))
6574 {
6575 /* do not break after one-letter words */
6576 if (curwin->w_cursor.col == 0)
6577 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006578#ifdef FEAT_COMMENTS
6579 /* do not break "#a b" when 'tw' is 2 */
6580 if (curwin->w_cursor.col <= leader_len)
6581 break;
6582#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006583 col = curwin->w_cursor.col;
6584 dec_cursor();
6585 cc = gchar_cursor();
6586
6587 if (WHITECHAR(cc))
6588 continue; /* one-letter, continue */
6589 curwin->w_cursor.col = col;
6590 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006591
6592 inc_cursor();
6593
6594 end_foundcol = end_col + 1;
6595 foundcol = curwin->w_cursor.col;
6596 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006597 break;
6598 }
6599#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006600 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006601 {
6602 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006603 if (curwin->w_cursor.col != startcol)
6604 {
6605#ifdef FEAT_COMMENTS
6606 /* Don't break until after the comment leader */
6607 if (curwin->w_cursor.col < leader_len)
6608 break;
6609#endif
6610 col = curwin->w_cursor.col;
6611 inc_cursor();
6612 /* Don't change end_foundcol if already set. */
6613 if (foundcol != curwin->w_cursor.col)
6614 {
6615 foundcol = curwin->w_cursor.col;
6616 end_foundcol = foundcol;
6617 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6618 break;
6619 }
6620 curwin->w_cursor.col = col;
6621 }
6622
6623 if (curwin->w_cursor.col == 0)
6624 break;
6625
6626 col = curwin->w_cursor.col;
6627
6628 dec_cursor();
6629 cc = gchar_cursor();
6630
6631 if (WHITECHAR(cc))
6632 continue; /* break with space */
6633#ifdef FEAT_COMMENTS
6634 /* Don't break until after the comment leader */
6635 if (curwin->w_cursor.col < leader_len)
6636 break;
6637#endif
6638
6639 curwin->w_cursor.col = col;
6640
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006641 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006642 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006643 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6644 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006645 }
6646#endif
6647 if (curwin->w_cursor.col == 0)
6648 break;
6649 dec_cursor();
6650 }
6651
6652 if (foundcol == 0) /* no spaces, cannot break line */
6653 {
6654 curwin->w_cursor.col = startcol;
6655 break;
6656 }
6657
6658 /* Going to break the line, remove any "$" now. */
6659 undisplay_dollar();
6660
6661 /*
6662 * Offset between cursor position and line break is used by replace
6663 * stack functions. VREPLACE does not use this, and backspaces
6664 * over the text instead.
6665 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006666 if (State & VREPLACE_FLAG)
6667 orig_col = startcol; /* Will start backspacing from here */
6668 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006669 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006670
6671 /*
6672 * adjust startcol for spaces that will be deleted and
6673 * characters that will remain on top line
6674 */
6675 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006676 while ((cc = gchar_cursor(), WHITECHAR(cc))
6677 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006678 inc_cursor();
6679 startcol -= curwin->w_cursor.col;
6680 if (startcol < 0)
6681 startcol = 0;
6682
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006683 if (State & VREPLACE_FLAG)
6684 {
6685 /*
6686 * In VREPLACE mode, we will backspace over the text to be
6687 * wrapped, so save a copy now to put on the next line.
6688 */
6689 saved_text = vim_strsave(ml_get_cursor());
6690 curwin->w_cursor.col = orig_col;
6691 if (saved_text == NULL)
6692 break; /* Can't do it, out of memory */
6693 saved_text[startcol] = NUL;
6694
6695 /* Backspace over characters that will move to the next line */
6696 if (!fo_white_par)
6697 backspace_until_column(foundcol);
6698 }
6699 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006700 {
6701 /* put cursor after pos. to break line */
6702 if (!fo_white_par)
6703 curwin->w_cursor.col = foundcol;
6704 }
6705
6706 /*
6707 * Split the line just before the margin.
6708 * Only insert/delete lines, but don't really redraw the window.
6709 */
6710 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6711 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6712#ifdef FEAT_COMMENTS
6713 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006714 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006715#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006716 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6717 if (!(flags & INSCHAR_COM_LIST))
6718 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006719
6720 replace_offset = 0;
6721 if (first_line)
6722 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006723 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006724 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006725 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006726 * This section is for auto-wrap of numeric lists. When not
6727 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6728 * flag will be set and open_line() will handle it (as seen
6729 * above). The code here (and in get_number_indent()) will
6730 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006731 */
6732 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006733 second_indent =
6734 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006735 if (second_indent >= 0)
6736 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006737 if (State & VREPLACE_FLAG)
6738 change_indent(INDENT_SET, second_indent,
6739 FALSE, NUL, TRUE);
6740 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006741#ifdef FEAT_COMMENTS
6742 if (leader_len > 0 && second_indent - leader_len > 0)
6743 {
6744 int i;
6745 int padding = second_indent - leader_len;
6746
6747 /* We started at the first_line of a numbered list
6748 * that has a comment. the open_line() function has
6749 * inserted the proper comment leader and positioned
6750 * the cursor at the end of the split line. Now we
6751 * add the additional whitespace needed after the
6752 * comment leader for the numbered list. */
6753 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006754 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006755 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006756 }
6757 else
6758 {
6759#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006760 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006761#ifdef FEAT_COMMENTS
6762 }
6763#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006764 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006765 }
6766 first_line = FALSE;
6767 }
6768
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006769 if (State & VREPLACE_FLAG)
6770 {
6771 /*
6772 * In VREPLACE mode we have backspaced over the text to be
6773 * moved, now we re-insert it into the new line.
6774 */
6775 ins_bytes(saved_text);
6776 vim_free(saved_text);
6777 }
6778 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006779 {
6780 /*
6781 * Check if cursor is not past the NUL off the line, cindent
6782 * may have added or removed indent.
6783 */
6784 curwin->w_cursor.col += startcol;
6785 len = (colnr_T)STRLEN(ml_get_curline());
6786 if (curwin->w_cursor.col > len)
6787 curwin->w_cursor.col = len;
6788 }
6789
6790 haveto_redraw = TRUE;
6791#ifdef FEAT_CINDENT
6792 can_cindent = TRUE;
6793#endif
6794 /* moved the cursor, don't autoindent or cindent now */
6795 did_ai = FALSE;
6796#ifdef FEAT_SMARTINDENT
6797 did_si = FALSE;
6798 can_si = FALSE;
6799 can_si_back = FALSE;
6800#endif
6801 line_breakcheck();
6802 }
6803
6804 if (save_char != NUL) /* put back space after cursor */
6805 pchar_cursor(save_char);
6806
Bram Moolenaar0026d472014-09-09 16:32:39 +02006807#ifdef FEAT_LINEBREAK
6808 curwin->w_p_lbr = has_lbr;
6809#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006810 if (!format_only && haveto_redraw)
6811 {
6812 update_topline();
6813 redraw_curbuf_later(VALID);
6814 }
6815}
6816
6817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 * Called after inserting or deleting text: When 'formatoptions' includes the
6819 * 'a' flag format from the current line until the end of the paragraph.
6820 * Keep the cursor at the same position relative to the text.
6821 * The caller must have saved the cursor line for undo, following ones will be
6822 * saved here.
6823 */
6824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006825auto_format(
6826 int trailblank, /* when TRUE also format with trailing blank */
6827 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828{
6829 pos_T pos;
6830 colnr_T len;
6831 char_u *old;
6832 char_u *new, *pnew;
6833 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006834 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
6836 if (!has_format_option(FO_AUTO))
6837 return;
6838
6839 pos = curwin->w_cursor;
6840 old = ml_get_curline();
6841
6842 /* may remove added space */
6843 check_auto_format(FALSE);
6844
6845 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6846 * user might insert normal text next. Also skip formatting when "1" is
6847 * in 'formatoptions' and there is a single character before the cursor.
6848 * Otherwise the line would be broken and when typing another non-white
6849 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006850 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 if (*old != NUL && !trailblank && wasatend)
6852 {
6853 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006854 cc = gchar_cursor();
6855 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6856 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006858 cc = gchar_cursor();
6859 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {
6861 curwin->w_cursor = pos;
6862 return;
6863 }
6864 curwin->w_cursor = pos;
6865 }
6866
6867#ifdef FEAT_COMMENTS
6868 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6869 * comments. */
6870 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006871 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 return;
6873#endif
6874
6875 /*
6876 * May start formatting in a previous line, so that after "x" a word is
6877 * moved to the previous line if it fits there now. Only when this is not
6878 * the start of a paragraph.
6879 */
6880 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6881 {
6882 --curwin->w_cursor.lnum;
6883 if (u_save_cursor() == FAIL)
6884 return;
6885 }
6886
6887 /*
6888 * Do the formatting and restore the cursor position. "saved_cursor" will
6889 * be adjusted for the text formatting.
6890 */
6891 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006892 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 curwin->w_cursor = saved_cursor;
6894 saved_cursor.lnum = 0;
6895
6896 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6897 {
6898 /* "cannot happen" */
6899 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6900 coladvance((colnr_T)MAXCOL);
6901 }
6902 else
6903 check_cursor_col();
6904
6905 /* Insert mode: If the cursor is now after the end of the line while it
6906 * previously wasn't, the line was broken. Because of the rule above we
6907 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6908 * formatted. */
6909 if (!wasatend && has_format_option(FO_WHITE_PAR))
6910 {
6911 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006912 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 if (curwin->w_cursor.col == len)
6914 {
6915 pnew = vim_strnsave(new, len + 2);
6916 pnew[len] = ' ';
6917 pnew[len + 1] = NUL;
6918 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6919 /* remove the space later */
6920 did_add_space = TRUE;
6921 }
6922 else
6923 /* may remove added space */
6924 check_auto_format(FALSE);
6925 }
6926
6927 check_cursor();
6928}
6929
6930/*
6931 * When an extra space was added to continue a paragraph for auto-formatting,
6932 * delete it now. The space must be under the cursor, just after the insert
6933 * position.
6934 */
6935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006936check_auto_format(
6937 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938{
6939 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006940 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
6942 if (did_add_space)
6943 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006944 cc = gchar_cursor();
6945 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 /* Somehow the space was removed already. */
6947 did_add_space = FALSE;
6948 else
6949 {
6950 if (!end_insert)
6951 {
6952 inc_cursor();
6953 c = gchar_cursor();
6954 dec_cursor();
6955 }
6956 if (c != NUL)
6957 {
6958 /* The space is no longer at the end of the line, delete it. */
6959 del_char(FALSE);
6960 did_add_space = FALSE;
6961 }
6962 }
6963 }
6964}
6965
6966/*
6967 * Find out textwidth to be used for formatting:
6968 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006969 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 * if invalid value, use 0.
6971 * Set default to window width (maximum 79) for "gq" operator.
6972 */
6973 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006974comp_textwidth(
6975 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
6977 int textwidth;
6978
6979 textwidth = curbuf->b_p_tw;
6980 if (textwidth == 0 && curbuf->b_p_wm)
6981 {
6982 /* The width is the window width minus 'wrapmargin' minus all the
6983 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006984 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985#ifdef FEAT_CMDWIN
6986 if (cmdwin_type != 0)
6987 textwidth -= 1;
6988#endif
6989#ifdef FEAT_FOLDING
6990 textwidth -= curwin->w_p_fdc;
6991#endif
6992#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006993 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 textwidth -= 1;
6995#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006996 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 textwidth -= 8;
6998 }
6999 if (textwidth < 0)
7000 textwidth = 0;
7001 if (ff && textwidth == 0)
7002 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007003 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 if (textwidth > 79)
7005 textwidth = 79;
7006 }
7007 return textwidth;
7008}
7009
7010/*
7011 * Put a character in the redo buffer, for when just after a CTRL-V.
7012 */
7013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007014redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015{
7016 char_u buf[10];
7017
7018 /* Only digits need special treatment. Translate them into a string of
7019 * three digits. */
7020 if (VIM_ISDIGIT(c))
7021 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007022 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 AppendToRedobuff(buf);
7024 }
7025 else
7026 AppendCharToRedobuff(c);
7027}
7028
7029/*
7030 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007031 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 */
7033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007034start_arrow(
7035 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007037 start_arrow_common(end_insert_pos, TRUE);
7038}
7039
7040/*
7041 * Like start_arrow() but with end_change argument.
7042 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7043 */
7044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007045start_arrow_with_change(
7046 pos_T *end_insert_pos, /* can be NULL */
7047 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007048{
7049 start_arrow_common(end_insert_pos, end_change);
7050 if (!end_change)
7051 {
7052 AppendCharToRedobuff(Ctrl_G);
7053 AppendCharToRedobuff('U');
7054 }
7055}
7056
7057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007058start_arrow_common(
7059 pos_T *end_insert_pos, /* can be NULL */
7060 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007061{
7062 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {
7064 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007065 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 arrow_used = TRUE; /* this means we stopped the current insert */
7067 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007068#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007069 check_spell_redraw();
7070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071}
7072
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007073#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007074/*
7075 * If we skipped highlighting word at cursor, do it now.
7076 * It may be skipped again, thus reset spell_redraw_lnum first.
7077 */
7078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007079check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007080{
7081 if (spell_redraw_lnum != 0)
7082 {
7083 linenr_T lnum = spell_redraw_lnum;
7084
7085 spell_redraw_lnum = 0;
7086 redrawWinline(lnum, FALSE);
7087 }
7088}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007089
7090/*
7091 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7092 * spelled word, if there is one.
7093 */
7094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007095spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007096{
7097 pos_T tpos = curwin->w_cursor;
7098
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007099 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007100 if (curwin->w_cursor.col != tpos.col)
7101 start_arrow(&tpos);
7102}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007103#endif
7104
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105/*
7106 * stop_arrow() is called before a change is made in insert mode.
7107 * If an arrow key has been used, start a new insertion.
7108 * Returns FAIL if undo is impossible, shouldn't insert then.
7109 */
7110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007111stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112{
7113 if (arrow_used)
7114 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007115 Insstart = curwin->w_cursor; /* new insertion starts here */
7116 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7117 /* Don't update the original insert position when moved to the
7118 * right, except when nothing was inserted yet. */
7119 update_Insstart_orig = FALSE;
7120 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7121
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 if (u_save_cursor() == OK)
7123 {
7124 arrow_used = FALSE;
7125 ins_need_undo = FALSE;
7126 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007127
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 if (State & VREPLACE_FLAG)
7130 {
7131 orig_line_count = curbuf->b_ml.ml_line_count;
7132 vr_lines_changed = 1;
7133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 ResetRedobuff();
7135 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007136 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 }
7138 else if (ins_need_undo)
7139 {
7140 if (u_save_cursor() == OK)
7141 ins_need_undo = FALSE;
7142 }
7143
7144#ifdef FEAT_FOLDING
7145 /* Always open fold at the cursor line when inserting something. */
7146 foldOpenCursor();
7147#endif
7148
7149 return (arrow_used || ins_need_undo ? FAIL : OK);
7150}
7151
7152/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007153 * Do a few things to stop inserting.
7154 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7155 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 */
7157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007158stop_insert(
7159 pos_T *end_insert_pos,
7160 int esc, /* called by ins_esc() */
7161 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007163 int cc;
7164 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165
7166 stop_redo_ins();
7167 replace_flush(); /* abandon replace stack */
7168
7169 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007170 * Save the inserted text for later redo with ^@ and CTRL-A.
7171 * Don't do it when "restart_edit" was set and nothing was inserted,
7172 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007174 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007175 if (did_restart_edit == 0 || (ptr != NULL
7176 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007177 {
7178 vim_free(last_insert);
7179 last_insert = ptr;
7180 last_insert_skip = new_insert_skip;
7181 }
7182 else
7183 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007185 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {
7187 /* Auto-format now. It may seem strange to do this when stopping an
7188 * insertion (or moving the cursor), but it's required when appending
7189 * a line and having it end in a space. But only do it when something
7190 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007191 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007193 pos_T tpos = curwin->w_cursor;
7194
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 /* When the cursor is at the end of the line after a space the
7196 * formatting will move it to the following word. Avoid that by
7197 * moving the cursor onto the space. */
7198 cc = 'x';
7199 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7200 {
7201 dec_cursor();
7202 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007203 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007204 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 }
7206
7207 auto_format(TRUE, FALSE);
7208
Bram Moolenaar1c465442017-03-12 20:10:05 +01007209 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007210 {
7211 if (gchar_cursor() != NUL)
7212 inc_cursor();
7213#ifdef FEAT_VIRTUALEDIT
7214 /* If the cursor is still at the same character, also keep
7215 * the "coladd". */
7216 if (gchar_cursor() == NUL
7217 && curwin->w_cursor.lnum == tpos.lnum
7218 && curwin->w_cursor.col == tpos.col)
7219 curwin->w_cursor.coladd = tpos.coladd;
7220#endif
7221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
7223
7224 /* If a space was inserted for auto-formatting, remove it now. */
7225 check_auto_format(TRUE);
7226
7227 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007228 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007229 * Do this when ESC was used or moving the cursor up/down.
7230 * Check for the old position still being valid, just in case the text
7231 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007232 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007233 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7234 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007236 pos_T tpos = curwin->w_cursor;
7237
7238 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007239 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007240 for (;;)
7241 {
7242 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7243 --curwin->w_cursor.col;
7244 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007245 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007246 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007247 if (del_char(TRUE) == FAIL)
7248 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007249 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007250 if (curwin->w_cursor.lnum != tpos.lnum)
7251 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007252 else
7253 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007254 /* reset tpos, could have been invalidated in the loop above */
7255 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007256 tpos.col++;
7257 if (cc != NUL && gchar_pos(&tpos) == NUL)
7258 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 /* <C-S-Right> may have started Visual mode, adjust the position for
7262 * deleted characters. */
7263 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7264 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007265 int len = (int)STRLEN(ml_get_curline());
7266
7267 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007269 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007270#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 }
7274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 }
7276 }
7277 did_ai = FALSE;
7278#ifdef FEAT_SMARTINDENT
7279 did_si = FALSE;
7280 can_si = FALSE;
7281 can_si_back = FALSE;
7282#endif
7283
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007284 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7285 * now in a different buffer. */
7286 if (end_insert_pos != NULL)
7287 {
7288 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007289 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007290 curbuf->b_op_end = *end_insert_pos;
7291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292}
7293
7294/*
7295 * Set the last inserted text to a single character.
7296 * Used for the replace command.
7297 */
7298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007299set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300{
7301 char_u *s;
7302
7303 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 if (last_insert != NULL)
7306 {
7307 s = last_insert;
7308 /* Use the CTRL-V only when entering a special char */
7309 if (c < ' ' || c == DEL)
7310 *s++ = Ctrl_V;
7311 s = add_char2buf(c, s);
7312 *s++ = ESC;
7313 *s++ = NUL;
7314 last_insert_skip = 0;
7315 }
7316}
7317
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007318#if defined(EXITFREE) || defined(PROTO)
7319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007320free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007321{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007322 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007323# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007324 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007325# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007326}
7327#endif
7328
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329/*
7330 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7331 * and CSI. Handle multi-byte characters.
7332 * Returns a pointer to after the added bytes.
7333 */
7334 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007335add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
7337#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007338 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 int i;
7340 int len;
7341
7342 len = (*mb_char2bytes)(c, temp);
7343 for (i = 0; i < len; ++i)
7344 {
7345 c = temp[i];
7346#endif
7347 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7348 if (c == K_SPECIAL)
7349 {
7350 *s++ = K_SPECIAL;
7351 *s++ = KS_SPECIAL;
7352 *s++ = KE_FILLER;
7353 }
7354#ifdef FEAT_GUI
7355 else if (c == CSI)
7356 {
7357 *s++ = CSI;
7358 *s++ = KS_EXTRA;
7359 *s++ = (int)KE_CSI;
7360 }
7361#endif
7362 else
7363 *s++ = c;
7364#ifdef FEAT_MBYTE
7365 }
7366#endif
7367 return s;
7368}
7369
7370/*
7371 * move cursor to start of line
7372 * if flags & BL_WHITE move to first non-white
7373 * if flags & BL_SOL move to first non-white if startofline is set,
7374 * otherwise keep "curswant" column
7375 * if flags & BL_FIX don't leave the cursor on a NUL.
7376 */
7377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007378beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
7380 if ((flags & BL_SOL) && !p_sol)
7381 coladvance(curwin->w_curswant);
7382 else
7383 {
7384 curwin->w_cursor.col = 0;
7385#ifdef FEAT_VIRTUALEDIT
7386 curwin->w_cursor.coladd = 0;
7387#endif
7388
7389 if (flags & (BL_WHITE | BL_SOL))
7390 {
7391 char_u *ptr;
7392
Bram Moolenaar1c465442017-03-12 20:10:05 +01007393 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7395 ++curwin->w_cursor.col;
7396 }
7397 curwin->w_set_curswant = TRUE;
7398 }
7399}
7400
7401/*
7402 * oneright oneleft cursor_down cursor_up
7403 *
7404 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007405 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 * Return OK when successful, FAIL when we hit a line of file boundary.
7407 */
7408
7409 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007410oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411{
7412 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
7415#ifdef FEAT_VIRTUALEDIT
7416 if (virtual_active())
7417 {
7418 pos_T prevpos = curwin->w_cursor;
7419
7420 /* Adjust for multi-wide char (excluding TAB) */
7421 ptr = ml_get_cursor();
7422 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007423# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007425# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 ))
7429 ? ptr2cells(ptr) : 1));
7430 curwin->w_set_curswant = TRUE;
7431 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7432 return (prevpos.col != curwin->w_cursor.col
7433 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7434 }
7435#endif
7436
7437 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007438 if (*ptr == NUL)
7439 return FAIL; /* already at the very end */
7440
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007442 if (has_mbyte)
7443 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 else
7445#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007446 l = 1;
7447
7448 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7449 * contains "onemore". */
7450 if (ptr[l] == NUL
7451#ifdef FEAT_VIRTUALEDIT
7452 && (ve_flags & VE_ONEMORE) == 0
7453#endif
7454 )
7455 return FAIL;
7456 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
7458 curwin->w_set_curswant = TRUE;
7459 return OK;
7460}
7461
7462 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007463oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464{
7465#ifdef FEAT_VIRTUALEDIT
7466 if (virtual_active())
7467 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007468# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007470# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 int v = getviscol();
7472
7473 if (v == 0)
7474 return FAIL;
7475
7476# ifdef FEAT_LINEBREAK
7477 /* We might get stuck on 'showbreak', skip over it. */
7478 width = 1;
7479 for (;;)
7480 {
7481 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007482 /* getviscol() is slow, skip it when 'showbreak' is empty,
7483 * 'breakindent' is not set and there are no multi-byte
7484 * characters */
7485 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486# ifdef FEAT_MBYTE
7487 && !has_mbyte
7488# endif
7489 ) || getviscol() < v)
7490 break;
7491 ++width;
7492 }
7493# else
7494 coladvance(v - 1);
7495# endif
7496
7497 if (curwin->w_cursor.coladd == 1)
7498 {
7499 char_u *ptr;
7500
7501 /* Adjust for multi-wide char (not a TAB) */
7502 ptr = ml_get_cursor();
7503 if (*ptr != TAB && vim_isprintc(
7504# ifdef FEAT_MBYTE
7505 (*mb_ptr2char)(ptr)
7506# else
7507 *ptr
7508# endif
7509 ) && ptr2cells(ptr) > 1)
7510 curwin->w_cursor.coladd = 0;
7511 }
7512
7513 curwin->w_set_curswant = TRUE;
7514 return OK;
7515 }
7516#endif
7517
7518 if (curwin->w_cursor.col == 0)
7519 return FAIL;
7520
7521 curwin->w_set_curswant = TRUE;
7522 --curwin->w_cursor.col;
7523
7524#ifdef FEAT_MBYTE
7525 /* if the character on the left of the current cursor is a multi-byte
7526 * character, move to its first byte */
7527 if (has_mbyte)
7528 mb_adjust_cursor();
7529#endif
7530 return OK;
7531}
7532
7533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007534cursor_up(
7535 long n,
7536 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 linenr_T lnum;
7539
7540 if (n > 0)
7541 {
7542 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007543 /* This fails if the cursor is already in the first line or the count
7544 * is larger than the line number and '-' is in 'cpoptions' */
7545 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 return FAIL;
7547 if (n >= lnum)
7548 lnum = 1;
7549 else
7550#ifdef FEAT_FOLDING
7551 if (hasAnyFolding(curwin))
7552 {
7553 /*
7554 * Count each sequence of folded lines as one logical line.
7555 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007556 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 (void)hasFolding(lnum, &lnum, NULL);
7558
7559 while (n--)
7560 {
7561 /* move up one line */
7562 --lnum;
7563 if (lnum <= 1)
7564 break;
7565 /* If we entered a fold, move to the beginning, unless in
7566 * Insert mode or when 'foldopen' contains "all": it will open
7567 * in a moment. */
7568 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7569 (void)hasFolding(lnum, &lnum, NULL);
7570 }
7571 if (lnum < 1)
7572 lnum = 1;
7573 }
7574 else
7575#endif
7576 lnum -= n;
7577 curwin->w_cursor.lnum = lnum;
7578 }
7579
7580 /* try to advance to the column we want to be at */
7581 coladvance(curwin->w_curswant);
7582
7583 if (upd_topline)
7584 update_topline(); /* make sure curwin->w_topline is valid */
7585
7586 return OK;
7587}
7588
7589/*
7590 * Cursor down a number of logical lines.
7591 */
7592 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007593cursor_down(
7594 long n,
7595 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 linenr_T lnum;
7598
7599 if (n > 0)
7600 {
7601 lnum = curwin->w_cursor.lnum;
7602#ifdef FEAT_FOLDING
7603 /* Move to last line of fold, will fail if it's the end-of-file. */
7604 (void)hasFolding(lnum, NULL, &lnum);
7605#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007606 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007607 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007608 if (lnum >= curbuf->b_ml.ml_line_count
7609 || (lnum + n > curbuf->b_ml.ml_line_count
7610 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 return FAIL;
7612 if (lnum + n >= curbuf->b_ml.ml_line_count)
7613 lnum = curbuf->b_ml.ml_line_count;
7614 else
7615#ifdef FEAT_FOLDING
7616 if (hasAnyFolding(curwin))
7617 {
7618 linenr_T last;
7619
7620 /* count each sequence of folded lines as one logical line */
7621 while (n--)
7622 {
7623 if (hasFolding(lnum, NULL, &last))
7624 lnum = last + 1;
7625 else
7626 ++lnum;
7627 if (lnum >= curbuf->b_ml.ml_line_count)
7628 break;
7629 }
7630 if (lnum > curbuf->b_ml.ml_line_count)
7631 lnum = curbuf->b_ml.ml_line_count;
7632 }
7633 else
7634#endif
7635 lnum += n;
7636 curwin->w_cursor.lnum = lnum;
7637 }
7638
7639 /* try to advance to the column we want to be at */
7640 coladvance(curwin->w_curswant);
7641
7642 if (upd_topline)
7643 update_topline(); /* make sure curwin->w_topline is valid */
7644
7645 return OK;
7646}
7647
7648/*
7649 * Stuff the last inserted text in the read buffer.
7650 * Last_insert actually is a copy of the redo buffer, so we
7651 * first have to remove the command.
7652 */
7653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654stuff_inserted(
7655 int c, /* Command character to be inserted */
7656 long count, /* Repeat this many times */
7657 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658{
7659 char_u *esc_ptr;
7660 char_u *ptr;
7661 char_u *last_ptr;
7662 char_u last = NUL;
7663
7664 ptr = get_last_insert();
7665 if (ptr == NULL)
7666 {
7667 EMSG(_(e_noinstext));
7668 return FAIL;
7669 }
7670
7671 /* may want to stuff the command character, to start Insert mode */
7672 if (c != NUL)
7673 stuffcharReadbuff(c);
7674 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7675 *esc_ptr = NUL; /* remove the ESC */
7676
7677 /* when the last char is either "0" or "^" it will be quoted if no ESC
7678 * comes after it OR if it will inserted more than once and "ptr"
7679 * starts with ^D. -- Acevedo
7680 */
7681 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7682 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7683 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7684 {
7685 last = *last_ptr;
7686 *last_ptr = NUL;
7687 }
7688
7689 do
7690 {
7691 stuffReadbuff(ptr);
7692 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7693 if (last)
7694 stuffReadbuff((char_u *)(last == '0'
7695 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7696 : IF_EB("\026^", CTRL_V_STR "^")));
7697 }
7698 while (--count > 0);
7699
7700 if (last)
7701 *last_ptr = last;
7702
7703 if (esc_ptr != NULL)
7704 *esc_ptr = ESC; /* put the ESC back */
7705
7706 /* may want to stuff a trailing ESC, to get out of Insert mode */
7707 if (!no_esc)
7708 stuffcharReadbuff(ESC);
7709
7710 return OK;
7711}
7712
7713 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007714get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
7716 if (last_insert == NULL)
7717 return NULL;
7718 return last_insert + last_insert_skip;
7719}
7720
7721/*
7722 * Get last inserted string, and remove trailing <Esc>.
7723 * Returns pointer to allocated memory (must be freed) or NULL.
7724 */
7725 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007726get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 char_u *s;
7729 int len;
7730
7731 if (last_insert == NULL)
7732 return NULL;
7733 s = vim_strsave(last_insert + last_insert_skip);
7734 if (s != NULL)
7735 {
7736 len = (int)STRLEN(s);
7737 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7738 s[len - 1] = NUL;
7739 }
7740 return s;
7741}
7742
7743/*
7744 * Check the word in front of the cursor for an abbreviation.
7745 * Called when the non-id character "c" has been entered.
7746 * When an abbreviation is recognized it is removed from the text and
7747 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7748 */
7749 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007750echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751{
7752 /* Don't check for abbreviation in paste mode, when disabled and just
7753 * after moving around with cursor keys. */
7754 if (p_paste || no_abbr || arrow_used)
7755 return FALSE;
7756
7757 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7758 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7759}
7760
7761/*
7762 * replace-stack functions
7763 *
7764 * When replacing characters, the replaced characters are remembered for each
7765 * new character. This is used to re-insert the old text when backspacing.
7766 *
7767 * There is a NUL headed list of characters for each character that is
7768 * currently in the file after the insertion point. When BS is used, one NUL
7769 * headed list is put back for the deleted character.
7770 *
7771 * For a newline, there are two NUL headed lists. One contains the characters
7772 * that the NL replaced. The extra one stores the characters after the cursor
7773 * that were deleted (always white space).
7774 *
7775 * Replace_offset is normally 0, in which case replace_push will add a new
7776 * character at the end of the stack. If replace_offset is not 0, that many
7777 * characters will be left on the stack above the newly inserted character.
7778 */
7779
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007780static char_u *replace_stack = NULL;
7781static long replace_stack_nr = 0; /* next entry in replace stack */
7782static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783
7784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007785replace_push(
7786 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
7788 char_u *p;
7789
7790 if (replace_stack_nr < replace_offset) /* nothing to do */
7791 return;
7792 if (replace_stack_len <= replace_stack_nr)
7793 {
7794 replace_stack_len += 50;
7795 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7796 if (p == NULL) /* out of memory */
7797 {
7798 replace_stack_len -= 50;
7799 return;
7800 }
7801 if (replace_stack != NULL)
7802 {
7803 mch_memmove(p, replace_stack,
7804 (size_t)(replace_stack_nr * sizeof(char_u)));
7805 vim_free(replace_stack);
7806 }
7807 replace_stack = p;
7808 }
7809 p = replace_stack + replace_stack_nr - replace_offset;
7810 if (replace_offset)
7811 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7812 *p = c;
7813 ++replace_stack_nr;
7814}
7815
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007816#if defined(FEAT_MBYTE) || defined(PROTO)
7817/*
7818 * Push a character onto the replace stack. Handles a multi-byte character in
7819 * reverse byte order, so that the first byte is popped off first.
7820 * Return the number of bytes done (includes composing characters).
7821 */
7822 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007823replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007824{
7825 int l = (*mb_ptr2len)(p);
7826 int j;
7827
7828 for (j = l - 1; j >= 0; --j)
7829 replace_push(p[j]);
7830 return l;
7831}
7832#endif
7833
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834/*
7835 * Pop one item from the replace stack.
7836 * return -1 if stack empty
7837 * return replaced character or NUL otherwise
7838 */
7839 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007840replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
7842 if (replace_stack_nr == 0)
7843 return -1;
7844 return (int)replace_stack[--replace_stack_nr];
7845}
7846
7847/*
7848 * Join the top two items on the replace stack. This removes to "off"'th NUL
7849 * encountered.
7850 */
7851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007852replace_join(
7853 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854{
7855 int i;
7856
7857 for (i = replace_stack_nr; --i >= 0; )
7858 if (replace_stack[i] == NUL && off-- <= 0)
7859 {
7860 --replace_stack_nr;
7861 mch_memmove(replace_stack + i, replace_stack + i + 1,
7862 (size_t)(replace_stack_nr - i));
7863 return;
7864 }
7865}
7866
7867/*
7868 * Pop bytes from the replace stack until a NUL is found, and insert them
7869 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7870 */
7871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007872replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 int cc;
7875 int oldState = State;
7876
7877 State = NORMAL; /* don't want REPLACE here */
7878 while ((cc = replace_pop()) > 0)
7879 {
7880#ifdef FEAT_MBYTE
7881 mb_replace_pop_ins(cc);
7882#else
7883 ins_char(cc);
7884#endif
7885 dec_cursor();
7886 }
7887 State = oldState;
7888}
7889
7890#ifdef FEAT_MBYTE
7891/*
7892 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7893 * indicates a multi-byte char, pop the other bytes too.
7894 */
7895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007896mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007899 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 int i;
7901 int c;
7902
7903 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7904 {
7905 buf[0] = cc;
7906 for (i = 1; i < n; ++i)
7907 buf[i] = replace_pop();
7908 ins_bytes_len(buf, n);
7909 }
7910 else
7911 ins_char(cc);
7912
7913 if (enc_utf8)
7914 /* Handle composing chars. */
7915 for (;;)
7916 {
7917 c = replace_pop();
7918 if (c == -1) /* stack empty */
7919 break;
7920 if ((n = MB_BYTE2LEN(c)) == 1)
7921 {
7922 /* Not a multi-byte char, put it back. */
7923 replace_push(c);
7924 break;
7925 }
7926 else
7927 {
7928 buf[0] = c;
7929 for (i = 1; i < n; ++i)
7930 buf[i] = replace_pop();
7931 if (utf_iscomposing(utf_ptr2char(buf)))
7932 ins_bytes_len(buf, n);
7933 else
7934 {
7935 /* Not a composing char, put it back. */
7936 for (i = n - 1; i >= 0; --i)
7937 replace_push(buf[i]);
7938 break;
7939 }
7940 }
7941 }
7942}
7943#endif
7944
7945/*
7946 * make the replace stack empty
7947 * (called when exiting replace mode)
7948 */
7949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007950replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007952 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 replace_stack_len = 0;
7954 replace_stack_nr = 0;
7955}
7956
7957/*
7958 * Handle doing a BS for one character.
7959 * cc < 0: replace stack empty, just move cursor
7960 * cc == 0: character was inserted, delete it
7961 * cc > 0: character was replaced, put cc (first byte of original char) back
7962 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007963 * When "limit_col" is >= 0, don't delete before this column. Matters when
7964 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 */
7966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007967replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968{
7969 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 int orig_len = 0;
7971 int ins_len;
7972 int orig_vcols = 0;
7973 colnr_T start_vcol;
7974 char_u *p;
7975 int i;
7976 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977
7978 cc = replace_pop();
7979 if (cc > 0)
7980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 if (State & VREPLACE_FLAG)
7982 {
7983 /* Get the number of screen cells used by the character we are
7984 * going to delete. */
7985 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7986 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988#ifdef FEAT_MBYTE
7989 if (has_mbyte)
7990 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007991 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007993 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 replace_push(cc);
7995 }
7996 else
7997#endif
7998 {
7999 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008001 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 }
8003 replace_pop_ins();
8004
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 if (State & VREPLACE_FLAG)
8006 {
8007 /* Get the number of screen cells used by the inserted characters */
8008 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008009 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 vcol = start_vcol;
8011 for (i = 0; i < ins_len; ++i)
8012 {
8013 vcol += chartabsize(p + i, vcol);
8014#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008015 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016#endif
8017 }
8018 vcol -= start_vcol;
8019
8020 /* Delete spaces that were inserted after the cursor to keep the
8021 * text aligned. */
8022 curwin->w_cursor.col += ins_len;
8023 while (vcol > orig_vcols && gchar_cursor() == ' ')
8024 {
8025 del_char(FALSE);
8026 ++orig_vcols;
8027 }
8028 curwin->w_cursor.col -= ins_len;
8029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030
8031 /* mark the buffer as changed and prepare for displaying */
8032 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
8033 }
8034 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008035 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036}
8037
8038#ifdef FEAT_CINDENT
8039/*
8040 * Return TRUE if C-indenting is on.
8041 */
8042 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008043cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
8045 return (!p_paste && (curbuf->b_p_cin
8046# ifdef FEAT_EVAL
8047 || *curbuf->b_p_inde != NUL
8048# endif
8049 ));
8050}
8051#endif
8052
8053#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8054/*
8055 * Re-indent the current line, based on the current contents of it and the
8056 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8057 * confused what all the part that handles Control-T is doing that I'm not.
8058 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8059 */
8060
8061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008062fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008064 int amount = get_the_indent();
8065
8066 if (amount >= 0)
8067 {
8068 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8069 if (linewhite(curwin->w_cursor.lnum))
8070 did_ai = TRUE; /* delete the indent if the line stays empty */
8071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072}
8073
8074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008075fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
8077 if (p_paste)
8078 return;
8079# ifdef FEAT_LISP
8080 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8081 fixthisline(get_lisp_indent);
8082# endif
8083# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8084 else
8085# endif
8086# ifdef FEAT_CINDENT
8087 if (cindent_on())
8088 do_c_expr_indent();
8089# endif
8090}
8091
8092#endif
8093
8094#ifdef FEAT_CINDENT
8095/*
8096 * return TRUE if 'cinkeys' contains the key "keytyped",
8097 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008098 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8100 *
8101 * "keytyped" can have a few special values:
8102 * KEY_OPEN_FORW
8103 * KEY_OPEN_BACK
8104 * KEY_COMPLETE just finished completion.
8105 *
8106 * If line_is_empty is TRUE accept keys with '0' before them.
8107 */
8108 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008109in_cinkeys(
8110 int keytyped,
8111 int when,
8112 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113{
8114 char_u *look;
8115 int try_match;
8116 int try_match_word;
8117 char_u *p;
8118 char_u *line;
8119 int icase;
8120 int i;
8121
Bram Moolenaar30848942009-12-24 14:46:12 +00008122 if (keytyped == NUL)
8123 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8124 return FALSE;
8125
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126#ifdef FEAT_EVAL
8127 if (*curbuf->b_p_inde != NUL)
8128 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8129 else
8130#endif
8131 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8132 while (*look)
8133 {
8134 /*
8135 * Find out if we want to try a match with this key, depending on
8136 * 'when' and a '*' or '!' before the key.
8137 */
8138 switch (when)
8139 {
8140 case '*': try_match = (*look == '*'); break;
8141 case '!': try_match = (*look == '!'); break;
8142 default: try_match = (*look != '*'); break;
8143 }
8144 if (*look == '*' || *look == '!')
8145 ++look;
8146
8147 /*
8148 * If there is a '0', only accept a match if the line is empty.
8149 * But may still match when typing last char of a word.
8150 */
8151 if (*look == '0')
8152 {
8153 try_match_word = try_match;
8154 if (!line_is_empty)
8155 try_match = FALSE;
8156 ++look;
8157 }
8158 else
8159 try_match_word = FALSE;
8160
8161 /*
8162 * does it look like a control character?
8163 */
8164 if (*look == '^'
8165#ifdef EBCDIC
8166 && (Ctrl_chr(look[1]) != 0)
8167#else
8168 && look[1] >= '?' && look[1] <= '_'
8169#endif
8170 )
8171 {
8172 if (try_match && keytyped == Ctrl_chr(look[1]))
8173 return TRUE;
8174 look += 2;
8175 }
8176 /*
8177 * 'o' means "o" command, open forward.
8178 * 'O' means "O" command, open backward.
8179 */
8180 else if (*look == 'o')
8181 {
8182 if (try_match && keytyped == KEY_OPEN_FORW)
8183 return TRUE;
8184 ++look;
8185 }
8186 else if (*look == 'O')
8187 {
8188 if (try_match && keytyped == KEY_OPEN_BACK)
8189 return TRUE;
8190 ++look;
8191 }
8192
8193 /*
8194 * 'e' means to check for "else" at start of line and just before the
8195 * cursor.
8196 */
8197 else if (*look == 'e')
8198 {
8199 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8200 {
8201 p = ml_get_curline();
8202 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8203 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8204 return TRUE;
8205 }
8206 ++look;
8207 }
8208
8209 /*
8210 * ':' only causes an indent if it is at the end of a label or case
8211 * statement, or when it was before typing the ':' (to fix
8212 * class::method for C++).
8213 */
8214 else if (*look == ':')
8215 {
8216 if (try_match && keytyped == ':')
8217 {
8218 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008219 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008221 /* Need to get the line again after cin_islabel(). */
8222 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 if (curwin->w_cursor.col > 2
8224 && p[curwin->w_cursor.col - 1] == ':'
8225 && p[curwin->w_cursor.col - 2] == ':')
8226 {
8227 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008228 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008229 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 p = ml_get_curline();
8231 p[curwin->w_cursor.col - 1] = ':';
8232 if (i)
8233 return TRUE;
8234 }
8235 }
8236 ++look;
8237 }
8238
8239
8240 /*
8241 * Is it a key in <>, maybe?
8242 */
8243 else if (*look == '<')
8244 {
8245 if (try_match)
8246 {
8247 /*
8248 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8249 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8250 * >, *, : and ! keys if they really really want to.
8251 */
8252 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8253 && keytyped == look[1])
8254 return TRUE;
8255
8256 if (keytyped == get_special_key_code(look + 1))
8257 return TRUE;
8258 }
8259 while (*look && *look != '>')
8260 look++;
8261 while (*look == '>')
8262 look++;
8263 }
8264
8265 /*
8266 * Is it a word: "=word"?
8267 */
8268 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8269 {
8270 ++look;
8271 if (*look == '~')
8272 {
8273 icase = TRUE;
8274 ++look;
8275 }
8276 else
8277 icase = FALSE;
8278 p = vim_strchr(look, ',');
8279 if (p == NULL)
8280 p = look + STRLEN(look);
8281 if ((try_match || try_match_word)
8282 && curwin->w_cursor.col >= (colnr_T)(p - look))
8283 {
8284 int match = FALSE;
8285
8286#ifdef FEAT_INS_EXPAND
8287 if (keytyped == KEY_COMPLETE)
8288 {
8289 char_u *s;
8290
8291 /* Just completed a word, check if it starts with "look".
8292 * search back for the start of a word. */
8293 line = ml_get_curline();
8294# ifdef FEAT_MBYTE
8295 if (has_mbyte)
8296 {
8297 char_u *n;
8298
8299 for (s = line + curwin->w_cursor.col; s > line; s = n)
8300 {
8301 n = mb_prevptr(line, s);
8302 if (!vim_iswordp(n))
8303 break;
8304 }
8305 }
8306 else
8307# endif
8308 for (s = line + curwin->w_cursor.col; s > line; --s)
8309 if (!vim_iswordc(s[-1]))
8310 break;
8311 if (s + (p - look) <= line + curwin->w_cursor.col
8312 && (icase
8313 ? MB_STRNICMP(s, look, p - look)
8314 : STRNCMP(s, look, p - look)) == 0)
8315 match = TRUE;
8316 }
8317 else
8318#endif
8319 /* TODO: multi-byte */
8320 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8321 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8322 {
8323 line = ml_get_cursor();
8324 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8325 || !vim_iswordc(line[-(p - look) - 1]))
8326 && (icase
8327 ? MB_STRNICMP(line - (p - look), look, p - look)
8328 : STRNCMP(line - (p - look), look, p - look))
8329 == 0)
8330 match = TRUE;
8331 }
8332 if (match && try_match_word && !try_match)
8333 {
8334 /* "0=word": Check if there are only blanks before the
8335 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008336 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 (int)(curwin->w_cursor.col - (p - look)))
8338 match = FALSE;
8339 }
8340 if (match)
8341 return TRUE;
8342 }
8343 look = p;
8344 }
8345
8346 /*
8347 * ok, it's a boring generic character.
8348 */
8349 else
8350 {
8351 if (try_match && *look == keytyped)
8352 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008353 if (*look != NUL)
8354 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 }
8356
8357 /*
8358 * Skip over ", ".
8359 */
8360 look = skip_to_option_part(look);
8361 }
8362 return FALSE;
8363}
8364#endif /* FEAT_CINDENT */
8365
8366#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8367/*
8368 * Map Hebrew keyboard when in hkmap mode.
8369 */
8370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008371hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372{
8373 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8374 {
8375 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8376 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8377 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8378 static char_u map[26] =
8379 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8380 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8381 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8382 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8383 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8384 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8385 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8386 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8387 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8388
8389 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8390 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8391 /* '-1'='sofit' */
8392 else if (c == 'x')
8393 return 'X';
8394 else if (c == 'q')
8395 return '\''; /* {geresh}={'} */
8396 else if (c == 246)
8397 return ' '; /* \"o --> ' ' for a german keyboard */
8398 else if (c == 228)
8399 return ' '; /* \"a --> ' ' -- / -- */
8400 else if (c == 252)
8401 return ' '; /* \"u --> ' ' -- / -- */
8402#ifdef EBCDIC
8403 else if (islower(c))
8404#else
8405 /* NOTE: islower() does not do the right thing for us on Linux so we
8406 * do this the same was as 5.7 and previous, so it works correctly on
8407 * all systems. Specifically, the e.g. Delete and Arrow keys are
8408 * munged and won't work if e.g. searching for Hebrew text.
8409 */
8410 else if (c >= 'a' && c <= 'z')
8411#endif
8412 return (int)(map[CharOrdLow(c)] + p_aleph);
8413 else
8414 return c;
8415 }
8416 else
8417 {
8418 switch (c)
8419 {
8420 case '`': return ';';
8421 case '/': return '.';
8422 case '\'': return ',';
8423 case 'q': return '/';
8424 case 'w': return '\'';
8425
8426 /* Hebrew letters - set offset from 'a' */
8427 case ',': c = '{'; break;
8428 case '.': c = 'v'; break;
8429 case ';': c = 't'; break;
8430 default: {
8431 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8432
8433#ifdef EBCDIC
8434 /* see note about islower() above */
8435 if (!islower(c))
8436#else
8437 if (c < 'a' || c > 'z')
8438#endif
8439 return c;
8440 c = str[CharOrdLow(c)];
8441 break;
8442 }
8443 }
8444
8445 return (int)(CharOrdLow(c) + p_aleph);
8446 }
8447}
8448#endif
8449
8450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008451ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
8453 int need_redraw = FALSE;
8454 int regname;
8455 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008456 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457
8458 /*
8459 * If we are going to wait for a character, show a '"'.
8460 */
8461 pc_status = PC_STATUS_UNSET;
8462 if (redrawing() && !char_avail())
8463 {
8464 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008465 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466
8467 edit_putchar('"', TRUE);
8468#ifdef FEAT_CMDL_INFO
8469 add_to_showcmd_c(Ctrl_R);
8470#endif
8471 }
8472
8473#ifdef USE_ON_FLY_SCROLL
8474 dont_scroll = TRUE; /* disallow scrolling here */
8475#endif
8476
8477 /*
8478 * Don't map the register name. This also prevents the mode message to be
8479 * deleted when ESC is hit.
8480 */
8481 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008482 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8485 {
8486 /* Get a third key for literal register insertion */
8487 literally = regname;
8488#ifdef FEAT_CMDL_INFO
8489 add_to_showcmd_c(literally);
8490#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008491 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 }
8494 --no_mapping;
8495
8496#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008497 /* Don't call u_sync() while typing the expression or giving an error
8498 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 ++no_u_sync;
8500 if (regname == '=')
8501 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008502# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008504# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008505 /* Sync undo when evaluating the expression calls setline() or
8506 * append(), so that it can be undone separately. */
8507 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008508
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008510# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 /* Restore the Input Method. */
8512 if (im_on)
8513 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008514# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008516 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8517 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008518 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 else
8522 {
8523#endif
8524 if (literally == Ctrl_O || literally == Ctrl_P)
8525 {
8526 /* Append the command to the redo buffer. */
8527 AppendCharToRedobuff(Ctrl_R);
8528 AppendCharToRedobuff(literally);
8529 AppendCharToRedobuff(regname);
8530
8531 do_put(regname, BACKWARD, 1L,
8532 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8533 }
8534 else if (insert_reg(regname, literally) == FAIL)
8535 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008536 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 need_redraw = TRUE; /* remove the '"' */
8538 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008539 else if (stop_insert_mode)
8540 /* When the '=' register was used and a function was invoked that
8541 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8542 * insert anything, need to remove the '"' */
8543 need_redraw = TRUE;
8544
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545#ifdef FEAT_EVAL
8546 }
8547 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008548 if (u_sync_once == 1)
8549 ins_need_undo = TRUE;
8550 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551#endif
8552#ifdef FEAT_CMDL_INFO
8553 clear_showcmd();
8554#endif
8555
8556 /* If the inserted register is empty, we need to remove the '"' */
8557 if (need_redraw || stuff_empty())
8558 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008559
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008560 /* Disallow starting Visual mode here, would get a weird mode. */
8561 if (!vis_active && VIsual_active)
8562 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563}
8564
8565/*
8566 * CTRL-G commands in Insert mode.
8567 */
8568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008569ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570{
8571 int c;
8572
8573#ifdef FEAT_INS_EXPAND
8574 /* Right after CTRL-X the cursor will be after the ruler. */
8575 setcursor();
8576#endif
8577
8578 /*
8579 * Don't map the second key. This also prevents the mode message to be
8580 * deleted when ESC is hit.
8581 */
8582 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008583 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 --no_mapping;
8585 switch (c)
8586 {
8587 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8588 case K_UP:
8589 case Ctrl_K:
8590 case 'k': ins_up(TRUE);
8591 break;
8592
8593 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8594 case K_DOWN:
8595 case Ctrl_J:
8596 case 'j': ins_down(TRUE);
8597 break;
8598
8599 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008600 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008602
8603 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008604 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008605 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008606 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 break;
8608
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008609 /* CTRL-G U: do not break undo with the next char */
8610 case 'U':
8611 /* Allow one left/right cursor movement with the next char,
8612 * without breaking undo. */
8613 dont_sync_undo = MAYBE;
8614 break;
8615
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008617 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 }
8619}
8620
8621/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008622 * CTRL-^ in Insert mode.
8623 */
8624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008625ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008626{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008627 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008628 {
8629 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8630 if (State & LANGMAP)
8631 {
8632 curbuf->b_p_iminsert = B_IMODE_NONE;
8633 State &= ~LANGMAP;
8634 }
8635 else
8636 {
8637 curbuf->b_p_iminsert = B_IMODE_LMAP;
8638 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008639#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008640 im_set_active(FALSE);
8641#endif
8642 }
8643 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008644#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008645 else
8646 {
8647 /* There are no ":lmap" mappings, toggle IM */
8648 if (im_get_status())
8649 {
8650 curbuf->b_p_iminsert = B_IMODE_NONE;
8651 im_set_active(FALSE);
8652 }
8653 else
8654 {
8655 curbuf->b_p_iminsert = B_IMODE_IM;
8656 State &= ~LANGMAP;
8657 im_set_active(TRUE);
8658 }
8659 }
8660#endif
8661 set_iminsert_global();
8662 showmode();
8663#ifdef FEAT_GUI
8664 /* may show different cursor shape or color */
8665 if (gui.in_use)
8666 gui_update_cursor(TRUE, FALSE);
8667#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008668#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008669 /* Show/unshow value of 'keymap' in status lines. */
8670 status_redraw_curbuf();
8671#endif
8672}
8673
8674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 * Handle ESC in insert mode.
8676 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8677 * insert.
8678 */
8679 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008680ins_esc(
8681 long *count,
8682 int cmdchar,
8683 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
8685 int temp;
8686 static int disabled_redraw = FALSE;
8687
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008688#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008689 check_spell_redraw();
8690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691#if defined(FEAT_HANGULIN)
8692# if defined(ESC_CHG_TO_ENG_MODE)
8693 hangul_input_state_set(0);
8694# endif
8695 if (composing_hangul)
8696 {
8697 push_raw_key(composing_hangul_buffer, 2);
8698 composing_hangul = 0;
8699 }
8700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701
8702 temp = curwin->w_cursor.col;
8703 if (disabled_redraw)
8704 {
8705 --RedrawingDisabled;
8706 disabled_redraw = FALSE;
8707 }
8708 if (!arrow_used)
8709 {
8710 /*
8711 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008712 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8713 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 */
8715 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008716 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717
8718 /*
8719 * Repeating insert may take a long time. Check for
8720 * interrupt now and then.
8721 */
8722 if (*count > 0)
8723 {
8724 line_breakcheck();
8725 if (got_int)
8726 *count = 0;
8727 }
8728
8729 if (--*count > 0) /* repeat what was typed */
8730 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008731 /* Vi repeats the insert without replacing characters. */
8732 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8733 State &= ~REPLACE_FLAG;
8734
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 (void)start_redo_ins();
8736 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008737 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 ++RedrawingDisabled;
8739 disabled_redraw = TRUE;
8740 return FALSE; /* repeat the insert */
8741 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008742 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 undisplay_dollar();
8744 }
8745
8746 /* When an autoindent was removed, curswant stays after the
8747 * indent */
8748 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8749 curwin->w_set_curswant = TRUE;
8750
8751 /* Remember the last Insert position in the '^ mark. */
8752 if (!cmdmod.keepjumps)
8753 curbuf->b_last_insert = curwin->w_cursor;
8754
8755 /*
8756 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008757 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008759 if (!nomove
8760 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761#ifdef FEAT_VIRTUALEDIT
8762 || curwin->w_cursor.coladd > 0
8763#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008764 )
8765 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008766 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767#ifdef FEAT_RIGHTLEFT
8768 && !revins_on
8769#endif
8770 )
8771 {
8772#ifdef FEAT_VIRTUALEDIT
8773 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8774 {
8775 oneleft();
8776 if (restart_edit != NUL)
8777 ++curwin->w_cursor.coladd;
8778 }
8779 else
8780#endif
8781 {
8782 --curwin->w_cursor.col;
8783#ifdef FEAT_MBYTE
8784 /* Correct cursor for multi-byte character. */
8785 if (has_mbyte)
8786 mb_adjust_cursor();
8787#endif
8788 }
8789 }
8790
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008791#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 /* Disable IM to allow typing English directly for Normal mode commands.
8793 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8794 * well). */
8795 if (!(State & LANGMAP))
8796 im_save_status(&curbuf->b_p_iminsert);
8797 im_set_active(FALSE);
8798#endif
8799
8800 State = NORMAL;
8801 /* need to position cursor again (e.g. when on a TAB ) */
8802 changed_cline_bef_curs();
8803
8804#ifdef FEAT_MOUSE
8805 setmouse();
8806#endif
8807#ifdef CURSOR_SHAPE
8808 ui_cursor_shape(); /* may show different cursor shape */
8809#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008810 if (!p_ek)
8811 /* Re-enable bracketed paste mode. */
8812 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813
8814 /*
8815 * When recording or for CTRL-O, need to display the new mode.
8816 * Otherwise remove the mode message.
8817 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008818 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 showmode();
8820 else if (p_smd)
8821 MSG("");
8822
8823 return TRUE; /* exit Insert mode */
8824}
8825
8826#ifdef FEAT_RIGHTLEFT
8827/*
8828 * Toggle language: hkmap and revins_on.
8829 * Move to end of reverse inserted text.
8830 */
8831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008832ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833{
8834 if (revins_on && revins_chars && revins_scol >= 0)
8835 {
8836 while (gchar_cursor() != NUL && revins_chars--)
8837 ++curwin->w_cursor.col;
8838 }
8839 p_ri = !p_ri;
8840 revins_on = (State == INSERT && p_ri);
8841 if (revins_on)
8842 {
8843 revins_scol = curwin->w_cursor.col;
8844 revins_legal++;
8845 revins_chars = 0;
8846 undisplay_dollar();
8847 }
8848 else
8849 revins_scol = -1;
8850#ifdef FEAT_FKMAP
8851 if (p_altkeymap)
8852 {
8853 /*
8854 * to be consistent also for redo command, using '.'
8855 * set arrow_used to true and stop it - causing to redo
8856 * characters entered in one mode (normal/reverse insert).
8857 */
8858 arrow_used = TRUE;
8859 (void)stop_arrow();
8860 p_fkmap = curwin->w_p_rl ^ p_ri;
8861 if (p_fkmap && p_ri)
8862 State = INSERT;
8863 }
8864 else
8865#endif
8866 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8867 showmode();
8868}
8869#endif
8870
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871/*
8872 * If 'keymodel' contains "startsel", may start selection.
8873 * Returns TRUE when a CTRL-O and other keys stuffed.
8874 */
8875 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008876ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
8878 if (km_startsel)
8879 switch (c)
8880 {
8881 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 case K_PAGEUP:
8884 case K_KPAGEUP:
8885 case K_PAGEDOWN:
8886 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008887# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 case K_LEFT:
8889 case K_RIGHT:
8890 case K_UP:
8891 case K_DOWN:
8892 case K_END:
8893 case K_HOME:
8894# endif
8895 if (!(mod_mask & MOD_MASK_SHIFT))
8896 break;
8897 /* FALLTHROUGH */
8898 case K_S_LEFT:
8899 case K_S_RIGHT:
8900 case K_S_UP:
8901 case K_S_DOWN:
8902 case K_S_END:
8903 case K_S_HOME:
8904 /* Start selection right away, the cursor can move with
8905 * CTRL-O when beyond the end of the line. */
8906 start_selection();
8907
8908 /* Execute the key in (insert) Select mode. */
8909 stuffcharReadbuff(Ctrl_O);
8910 if (mod_mask)
8911 {
8912 char_u buf[4];
8913
8914 buf[0] = K_SPECIAL;
8915 buf[1] = KS_MODIFIER;
8916 buf[2] = mod_mask;
8917 buf[3] = NUL;
8918 stuffReadbuff(buf);
8919 }
8920 stuffcharReadbuff(c);
8921 return TRUE;
8922 }
8923 return FALSE;
8924}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925
8926/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008927 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008928 */
8929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008930ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008931{
8932#ifdef FEAT_FKMAP
8933 if (p_fkmap && p_ri)
8934 {
8935 beep_flush();
8936 EMSG(farsi_text_3); /* encoded in Farsi */
8937 return;
8938 }
8939#endif
8940
Bram Moolenaar1e015462005-09-25 22:16:38 +00008941# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008942 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008943 (char_u *)((State & REPLACE_FLAG) ? "i"
8944 : replaceState == VREPLACE ? "v"
8945 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008946# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008947 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008948 if (State & REPLACE_FLAG)
8949 State = INSERT | (State & LANGMAP);
8950 else
8951 State = replaceState | (State & LANGMAP);
8952 AppendCharToRedobuff(K_INS);
8953 showmode();
8954#ifdef CURSOR_SHAPE
8955 ui_cursor_shape(); /* may show different cursor shape */
8956#endif
8957}
8958
8959/*
8960 * Pressed CTRL-O in Insert mode.
8961 */
8962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008963ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008964{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008965 if (State & VREPLACE_FLAG)
8966 restart_edit = 'V';
8967 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008968 if (State & REPLACE_FLAG)
8969 restart_edit = 'R';
8970 else
8971 restart_edit = 'I';
8972#ifdef FEAT_VIRTUALEDIT
8973 if (virtual_active())
8974 ins_at_eol = FALSE; /* cursor always keeps its column */
8975 else
8976#endif
8977 ins_at_eol = (gchar_cursor() == NUL);
8978}
8979
8980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 * If the cursor is on an indent, ^T/^D insert/delete one
8982 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008983 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 * with vi. But vi only supports ^T and ^D after an
8985 * autoindent, we support it everywhere.
8986 */
8987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008988ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
8990 if (stop_arrow() == FAIL)
8991 return;
8992 AppendCharToRedobuff(c);
8993
8994 /*
8995 * 0^D and ^^D: remove all indent.
8996 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008997 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8998 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 {
9000 --curwin->w_cursor.col;
9001 (void)del_char(FALSE); /* delete the '^' or '0' */
9002 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9003 if (State & REPLACE_FLAG)
9004 replace_pop_ins();
9005 if (lastc == '^')
9006 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009007 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 }
9009 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009010 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011
9012 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9013 did_ai = FALSE;
9014#ifdef FEAT_SMARTINDENT
9015 did_si = FALSE;
9016 can_si = FALSE;
9017 can_si_back = FALSE;
9018#endif
9019#ifdef FEAT_CINDENT
9020 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9021#endif
9022}
9023
9024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009025ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
9027 int temp;
9028
9029 if (stop_arrow() == FAIL)
9030 return;
9031 if (gchar_cursor() == NUL) /* delete newline */
9032 {
9033 temp = curwin->w_cursor.col;
9034 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009035 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009036 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009040 /* Adjust orig_line_count in case more lines have been deleted than
9041 * have been added. That makes sure, that open_line() later
9042 * can access all buffer lines correctly */
9043 if (State & VREPLACE_FLAG &&
9044 orig_line_count > curbuf->b_ml.ml_line_count)
9045 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009048 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9049 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 did_ai = FALSE;
9051#ifdef FEAT_SMARTINDENT
9052 did_si = FALSE;
9053 can_si = FALSE;
9054 can_si_back = FALSE;
9055#endif
9056 AppendCharToRedobuff(K_DEL);
9057}
9058
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009059static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009060
9061/*
9062 * Delete one character for ins_bs().
9063 */
9064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009065ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009066{
9067 dec_cursor();
9068 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9069 if (State & REPLACE_FLAG)
9070 {
9071 /* Don't delete characters before the insert point when in
9072 * Replace mode */
9073 if (curwin->w_cursor.lnum != Insstart.lnum
9074 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009075 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009076 }
9077 else
9078 (void)del_char(FALSE);
9079}
9080
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081/*
9082 * Handle Backspace, delete-word and delete-line in Insert mode.
9083 * Return TRUE when backspace was actually used.
9084 */
9085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009086ins_bs(
9087 int c,
9088 int mode,
9089 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090{
9091 linenr_T lnum;
9092 int cc;
9093 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009094 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 colnr_T mincol;
9096 int did_backspace = FALSE;
9097 int in_indent;
9098 int oldState;
9099#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009100 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101#endif
9102
9103 /*
9104 * can't delete anything in an empty file
9105 * can't backup past first character in buffer
9106 * can't backup past starting point unless 'backspace' > 1
9107 * can backup to a previous line if 'backspace' == 0
9108 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009109 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 || (
9111#ifdef FEAT_RIGHTLEFT
9112 !revins_on &&
9113#endif
9114 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9115 || (!can_bs(BS_START)
9116 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009117 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9118 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9120 && curwin->w_cursor.col <= ai_col)
9121 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9122 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009123 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 return FALSE;
9125 }
9126
9127 if (stop_arrow() == FAIL)
9128 return FALSE;
9129 in_indent = inindent(0);
9130#ifdef FEAT_CINDENT
9131 if (in_indent)
9132 can_cindent = FALSE;
9133#endif
9134#ifdef FEAT_COMMENTS
9135 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9136#endif
9137#ifdef FEAT_RIGHTLEFT
9138 if (revins_on) /* put cursor after last inserted char */
9139 inc_cursor();
9140#endif
9141
9142#ifdef FEAT_VIRTUALEDIT
9143 /* Virtualedit:
9144 * BACKSPACE_CHAR eats a virtual space
9145 * BACKSPACE_WORD eats all coladd
9146 * BACKSPACE_LINE eats all coladd and keeps going
9147 */
9148 if (curwin->w_cursor.coladd > 0)
9149 {
9150 if (mode == BACKSPACE_CHAR)
9151 {
9152 --curwin->w_cursor.coladd;
9153 return TRUE;
9154 }
9155 if (mode == BACKSPACE_WORD)
9156 {
9157 curwin->w_cursor.coladd = 0;
9158 return TRUE;
9159 }
9160 curwin->w_cursor.coladd = 0;
9161 }
9162#endif
9163
9164 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009165 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 */
9167 if (curwin->w_cursor.col == 0)
9168 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009169 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009170 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171#ifdef FEAT_RIGHTLEFT
9172 || revins_on
9173#endif
9174 )
9175 {
9176 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9177 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9178 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009179 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009180 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 }
9182 /*
9183 * In replace mode:
9184 * cc < 0: NL was inserted, delete it
9185 * cc >= 0: NL was replaced, put original characters back
9186 */
9187 cc = -1;
9188 if (State & REPLACE_FLAG)
9189 cc = replace_pop(); /* returns -1 if NL was inserted */
9190 /*
9191 * In replace mode, in the line we started replacing, we only move the
9192 * cursor.
9193 */
9194 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9195 {
9196 dec_cursor();
9197 }
9198 else
9199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 if (!(State & VREPLACE_FLAG)
9201 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 {
9203 temp = gchar_cursor(); /* remember current char */
9204 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009205
9206 /* When "aw" is in 'formatoptions' we must delete the space at
9207 * the end of the line, otherwise the line will be broken
9208 * again when auto-formatting. */
9209 if (has_format_option(FO_AUTO)
9210 && has_format_option(FO_WHITE_PAR))
9211 {
9212 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9213 TRUE);
9214 int len;
9215
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009216 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009217 if (len > 0 && ptr[len - 1] == ' ')
9218 ptr[len - 1] = NUL;
9219 }
9220
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009221 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 if (temp == NUL && gchar_cursor() != NUL)
9223 inc_cursor();
9224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 else
9226 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227
9228 /*
9229 * In REPLACE mode we have to put back the text that was replaced
9230 * by the NL. On the replace stack is first a NUL-terminated
9231 * sequence of characters that were deleted and then the
9232 * characters that NL replaced.
9233 */
9234 if (State & REPLACE_FLAG)
9235 {
9236 /*
9237 * Do the next ins_char() in NORMAL state, to
9238 * prevent ins_char() from replacing characters and
9239 * avoiding showmatch().
9240 */
9241 oldState = State;
9242 State = NORMAL;
9243 /*
9244 * restore characters (blanks) deleted after cursor
9245 */
9246 while (cc > 0)
9247 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009248 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249#ifdef FEAT_MBYTE
9250 mb_replace_pop_ins(cc);
9251#else
9252 ins_char(cc);
9253#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009254 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 cc = replace_pop();
9256 }
9257 /* restore the characters that NL replaced */
9258 replace_pop_ins();
9259 State = oldState;
9260 }
9261 }
9262 did_ai = FALSE;
9263 }
9264 else
9265 {
9266 /*
9267 * Delete character(s) before the cursor.
9268 */
9269#ifdef FEAT_RIGHTLEFT
9270 if (revins_on) /* put cursor on last inserted char */
9271 dec_cursor();
9272#endif
9273 mincol = 0;
9274 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009275 if (mode == BACKSPACE_LINE
9276 && (curbuf->b_p_ai
9277#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009278 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009279#endif
9280 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281#ifdef FEAT_RIGHTLEFT
9282 && !revins_on
9283#endif
9284 )
9285 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009286 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009288 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009290 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 }
9292
9293 /*
9294 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9295 */
9296 if ( mode == BACKSPACE_CHAR
9297 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009298 || ((get_sts_value() != 0
9299#ifdef FEAT_VARTABS
9300 || tabstop_count(curbuf->b_p_vsts_array)
9301#endif
9302 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009303 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 && (*(ml_get_cursor() - 1) == TAB
9305 || (*(ml_get_cursor() - 1) == ' '
9306 && (!*inserted_space_p
9307 || arrow_used))))))
9308 {
9309 int ts;
9310 colnr_T vcol;
9311 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009312 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313
9314 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 /* Compute the virtual column where we want to be. Since
9316 * 'showbreak' may get in the way, need to get the last column of
9317 * the previous character. */
9318 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009319 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 dec_cursor();
9321 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9322 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009323#ifdef FEAT_VARTABS
9324 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009325 {
9326 ts = (int)get_sw_value(curbuf);
9327 want_vcol = (want_vcol / ts) * ts;
9328 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009329 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009330 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009331 curbuf->b_p_vsts_array);
9332#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009333 if (p_sta && in_indent)
9334 ts = (int)get_sw_value(curbuf);
9335 else
9336 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339
9340 /* delete characters until we are at or before want_vcol */
9341 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009342 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009343 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344
9345 /* insert extra spaces until we are at want_vcol */
9346 while (vcol < want_vcol)
9347 {
9348 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009349 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9350 && curwin->w_cursor.col < Insstart_orig.col)
9351 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 if (State & VREPLACE_FLAG)
9354 ins_char(' ');
9355 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 {
9357 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009358 if ((State & REPLACE_FLAG))
9359 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 }
9361 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9362 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009363
9364 /* If we are now back where we started delete one character. Can
9365 * happen when using 'sts' and 'linebreak'. */
9366 if (vcol >= start_vcol)
9367 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 }
9369
9370 /*
9371 * Delete upto starting point, start of line or previous word.
9372 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009373 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009375#ifdef FEAT_MBYTE
9376 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009378 if (has_mbyte)
9379 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009381 do
9382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009384 if (!revins_on) /* put cursor on char to be deleted */
9385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009387
9388 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009390 /* look multi-byte character class */
9391 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009393 prev_cclass = cclass;
9394 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009397
9398 /* start of word? */
9399 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9400 {
9401 mode = BACKSPACE_WORD_NOT_SPACE;
9402 temp = vim_iswordc(cc);
9403 }
9404 /* end of word? */
9405 else if (mode == BACKSPACE_WORD_NOT_SPACE
9406 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9407#ifdef FEAT_MBYTE
9408 || prev_cclass != cclass
9409#endif
9410 ))
9411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009413 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009415 inc_cursor();
9416#ifdef FEAT_RIGHTLEFT
9417 else if (State & REPLACE_FLAG)
9418 dec_cursor();
9419#endif
9420 break;
9421 }
9422 if (State & REPLACE_FLAG)
9423 replace_do_bs(-1);
9424 else
9425 {
9426#ifdef FEAT_MBYTE
9427 if (enc_utf8 && p_deco)
9428 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9429#endif
9430 (void)del_char(FALSE);
9431#ifdef FEAT_MBYTE
9432 /*
9433 * If there are combining characters and 'delcombine' is set
9434 * move the cursor back. Don't back up before the base
9435 * character.
9436 */
9437 if (enc_utf8 && p_deco && cpc[0] != NUL)
9438 inc_cursor();
9439#endif
9440#ifdef FEAT_RIGHTLEFT
9441 if (revins_chars)
9442 {
9443 revins_chars--;
9444 revins_legal++;
9445 }
9446 if (revins_on && gchar_cursor() == NUL)
9447 break;
9448#endif
9449 }
9450 /* Just a single backspace?: */
9451 if (mode == BACKSPACE_CHAR)
9452 break;
9453 } while (
9454#ifdef FEAT_RIGHTLEFT
9455 revins_on ||
9456#endif
9457 (curwin->w_cursor.col > mincol
9458 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9459 || curwin->w_cursor.col != Insstart_orig.col)));
9460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 did_backspace = TRUE;
9462 }
9463#ifdef FEAT_SMARTINDENT
9464 did_si = FALSE;
9465 can_si = FALSE;
9466 can_si_back = FALSE;
9467#endif
9468 if (curwin->w_cursor.col <= 1)
9469 did_ai = FALSE;
9470 /*
9471 * It's a little strange to put backspaces into the redo
9472 * buffer, but it makes auto-indent a lot easier to deal
9473 * with.
9474 */
9475 AppendCharToRedobuff(c);
9476
9477 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009478 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009479 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009480 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
9482 /* vi behaviour: the cursor moves backward but the character that
9483 * was there remains visible
9484 * Vim behaviour: the cursor moves backward and the character that
9485 * was there is erased from the screen.
9486 * We can emulate the vi behaviour by pretending there is a dollar
9487 * displayed even when there isn't.
9488 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009489 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 dollar_vcol = curwin->w_virtcol;
9491
Bram Moolenaarce3be472008-01-14 19:12:28 +00009492#ifdef FEAT_FOLDING
9493 /* When deleting a char the cursor line must never be in a closed fold.
9494 * E.g., when 'foldmethod' is indent and deleting the first non-white
9495 * char before a Tab. */
9496 if (did_backspace)
9497 foldOpenCursor();
9498#endif
9499
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 return did_backspace;
9501}
9502
9503#ifdef FEAT_MOUSE
9504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009505ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506{
9507 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009508 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509
9510# ifdef FEAT_GUI
9511 /* When GUI is active, also move/paste when 'mouse' is empty */
9512 if (!gui.in_use)
9513# endif
9514 if (!mouse_has(MOUSE_INSERT))
9515 return;
9516
9517 undisplay_dollar();
9518 tpos = curwin->w_cursor;
9519 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9520 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009521 win_T *new_curwin = curwin;
9522
9523 if (curwin != old_curwin && win_valid(old_curwin))
9524 {
9525 /* Mouse took us to another window. We need to go back to the
9526 * previous one to stop insert there properly. */
9527 curwin = old_curwin;
9528 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009529#ifdef FEAT_JOB_CHANNEL
9530 if (bt_prompt(curbuf))
9531 // Restart Insert mode when re-entering the prompt buffer.
9532 curbuf->b_prompt_insert = 'A';
9533#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009534 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009535 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009536 if (curwin != new_curwin && win_valid(new_curwin))
9537 {
9538 curwin = new_curwin;
9539 curbuf = curwin->w_buffer;
9540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541# ifdef FEAT_CINDENT
9542 can_cindent = TRUE;
9543# endif
9544 }
9545
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 /* redraw status lines (in case another window became active) */
9547 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548}
9549
9550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009551ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
9553 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009554 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009555# ifdef FEAT_INS_EXPAND
9556 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557# endif
9558
9559 tpos = curwin->w_cursor;
9560
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009561 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 {
9563 int row, col;
9564
9565 row = mouse_row;
9566 col = mouse_col;
9567
9568 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009569 wp = mouse_find_win(&row, &col);
9570 if (wp == NULL)
9571 return;
9572 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 curbuf = curwin->w_buffer;
9574 }
9575 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 undisplay_dollar();
9577
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009578# ifdef FEAT_INS_EXPAND
9579 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009580 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009581# endif
9582 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009583 if (dir == MSCR_DOWN || dir == MSCR_UP)
9584 {
9585 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9586 scroll_redraw(dir,
9587 (long)(curwin->w_botline - curwin->w_topline));
9588 else
9589 scroll_redraw(dir, 3L);
9590 }
9591#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009592 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009593 {
9594 int val, step = 6;
9595
9596 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009597 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009598 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9599 if (val < 0)
9600 val = 0;
9601 gui_do_horiz_scroll(val, TRUE);
9602 }
9603#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009604# ifdef FEAT_INS_EXPAND
9605 did_scroll = TRUE;
9606# endif
9607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 curwin->w_redr_status = TRUE;
9610
9611 curwin = old_curwin;
9612 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009614# ifdef FEAT_INS_EXPAND
9615 /* The popup menu may overlay the window, need to redraw it.
9616 * TODO: Would be more efficient to only redraw the windows that are
9617 * overlapped by the popup menu. */
9618 if (pum_visible() && did_scroll)
9619 {
9620 redraw_all_later(NOT_VALID);
9621 ins_compl_show_pum();
9622 }
9623# endif
9624
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009625 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 {
9627 start_arrow(&tpos);
9628# ifdef FEAT_CINDENT
9629 can_cindent = TRUE;
9630# endif
9631 }
9632}
9633#endif
9634
Bram Moolenaarec2da362017-01-21 20:04:22 +01009635/*
9636 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9637 * P_PE literally.
9638 * When "drop" is TRUE then consume the text and drop it.
9639 */
9640 int
9641bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9642{
9643 int c;
9644 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9645 int idx = 0;
9646 char_u *end = find_termcode((char_u *)"PE");
9647 int ret_char = -1;
9648 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009649 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009650
9651 /* If the end code is too long we can't detect it, read everything. */
9652 if (STRLEN(end) >= NUMBUFLEN)
9653 end = NULL;
9654 ++no_mapping;
9655 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009656 if (!p_paste)
9657 // Also have the side effects of setting 'paste' to make it work much
9658 // faster.
9659 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009660
Bram Moolenaarec2da362017-01-21 20:04:22 +01009661 for (;;)
9662 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009663 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009664 if (end == NULL && vpeekc() == NUL)
9665 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009666 do
9667 {
9668 c = vgetc();
9669 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9670 if (c == NUL || got_int)
9671 // When CTRL-C was encountered the typeahead will be flushed and we
9672 // won't get the end sequence.
9673 break;
9674
Bram Moolenaarec2da362017-01-21 20:04:22 +01009675#ifdef FEAT_MBYTE
9676 if (has_mbyte)
9677 idx += (*mb_char2bytes)(c, buf + idx);
9678 else
9679#endif
9680 buf[idx++] = c;
9681 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009682 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009683 {
9684 if (end[idx] == NUL)
9685 break; /* Found the end of paste code. */
9686 continue;
9687 }
9688 if (!drop)
9689 {
9690 switch (mode)
9691 {
9692 case PASTE_CMDLINE:
9693 put_on_cmdline(buf, idx, TRUE);
9694 break;
9695
9696 case PASTE_EX:
9697 if (gap != NULL && ga_grow(gap, idx) == OK)
9698 {
9699 mch_memmove((char *)gap->ga_data + gap->ga_len,
9700 buf, (size_t)idx);
9701 gap->ga_len += idx;
9702 }
9703 break;
9704
9705 case PASTE_INSERT:
9706 if (stop_arrow() == OK)
9707 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009708 c = buf[0];
9709 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9710 ins_eol(c);
9711 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009712 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009713 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009714 AppendToRedobuffLit(buf, idx);
9715 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009716 }
9717 break;
9718
9719 case PASTE_ONE_CHAR:
9720 if (ret_char == -1)
9721 {
9722#ifdef FEAT_MBYTE
9723 if (has_mbyte)
9724 ret_char = (*mb_ptr2char)(buf);
9725 else
9726#endif
9727 ret_char = buf[0];
9728 }
9729 break;
9730 }
9731 }
9732 idx = 0;
9733 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009734
Bram Moolenaarec2da362017-01-21 20:04:22 +01009735 --no_mapping;
9736 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009737 if (!save_paste)
9738 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009739
9740 return ret_char;
9741}
9742
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009743#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009745ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009746{
9747 /* We will be leaving the current window, unless closing another tab. */
9748 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9749 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9750 {
9751 undisplay_dollar();
9752 start_arrow(&curwin->w_cursor);
9753# ifdef FEAT_CINDENT
9754 can_cindent = TRUE;
9755# endif
9756 }
9757
9758 if (c == K_TABLINE)
9759 goto_tabpage(current_tab);
9760 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009761 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009762 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009763 redraw_statuslines(); /* will redraw the tabline when needed */
9764 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009765}
9766#endif
9767
9768#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009770ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771{
9772 pos_T tpos;
9773
9774 undisplay_dollar();
9775 tpos = curwin->w_cursor;
9776 if (gui_do_scroll())
9777 {
9778 start_arrow(&tpos);
9779# ifdef FEAT_CINDENT
9780 can_cindent = TRUE;
9781# endif
9782 }
9783}
9784
9785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009786ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787{
9788 pos_T tpos;
9789
9790 undisplay_dollar();
9791 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009792 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 {
9794 start_arrow(&tpos);
9795# ifdef FEAT_CINDENT
9796 can_cindent = TRUE;
9797# endif
9798 }
9799}
9800#endif
9801
9802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009803ins_left(
9804 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805{
9806 pos_T tpos;
9807
9808#ifdef FEAT_FOLDING
9809 if ((fdo_flags & FDO_HOR) && KeyTyped)
9810 foldOpenCursor();
9811#endif
9812 undisplay_dollar();
9813 tpos = curwin->w_cursor;
9814 if (oneleft() == OK)
9815 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009816#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9817 /* Only call start_arrow() when not busy with preediting, it will
9818 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009819 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009820#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009821 {
9822 start_arrow_with_change(&tpos, end_change);
9823 if (!end_change)
9824 AppendCharToRedobuff(K_LEFT);
9825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826#ifdef FEAT_RIGHTLEFT
9827 /* If exit reversed string, position is fixed */
9828 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9829 revins_legal++;
9830 revins_chars++;
9831#endif
9832 }
9833
9834 /*
9835 * if 'whichwrap' set for cursor in insert mode may go to
9836 * previous line
9837 */
9838 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9839 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009840 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 start_arrow(&tpos);
9842 --(curwin->w_cursor.lnum);
9843 coladvance((colnr_T)MAXCOL);
9844 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9845 }
9846 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009847 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009848 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849}
9850
9851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009852ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853{
9854 pos_T tpos;
9855
9856#ifdef FEAT_FOLDING
9857 if ((fdo_flags & FDO_HOR) && KeyTyped)
9858 foldOpenCursor();
9859#endif
9860 undisplay_dollar();
9861 tpos = curwin->w_cursor;
9862 if (c == K_C_HOME)
9863 curwin->w_cursor.lnum = 1;
9864 curwin->w_cursor.col = 0;
9865#ifdef FEAT_VIRTUALEDIT
9866 curwin->w_cursor.coladd = 0;
9867#endif
9868 curwin->w_curswant = 0;
9869 start_arrow(&tpos);
9870}
9871
9872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009873ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874{
9875 pos_T tpos;
9876
9877#ifdef FEAT_FOLDING
9878 if ((fdo_flags & FDO_HOR) && KeyTyped)
9879 foldOpenCursor();
9880#endif
9881 undisplay_dollar();
9882 tpos = curwin->w_cursor;
9883 if (c == K_C_END)
9884 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9885 coladvance((colnr_T)MAXCOL);
9886 curwin->w_curswant = MAXCOL;
9887
9888 start_arrow(&tpos);
9889}
9890
9891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009892ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893{
9894#ifdef FEAT_FOLDING
9895 if ((fdo_flags & FDO_HOR) && KeyTyped)
9896 foldOpenCursor();
9897#endif
9898 undisplay_dollar();
9899 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9900 {
9901 start_arrow(&curwin->w_cursor);
9902 (void)bck_word(1L, FALSE, FALSE);
9903 curwin->w_set_curswant = TRUE;
9904 }
9905 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009906 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907}
9908
9909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009910ins_right(
9911 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912{
9913#ifdef FEAT_FOLDING
9914 if ((fdo_flags & FDO_HOR) && KeyTyped)
9915 foldOpenCursor();
9916#endif
9917 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009918 if (gchar_cursor() != NUL
9919#ifdef FEAT_VIRTUALEDIT
9920 || virtual_active()
9921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 )
9923 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009924 start_arrow_with_change(&curwin->w_cursor, end_change);
9925 if (!end_change)
9926 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 curwin->w_set_curswant = TRUE;
9928#ifdef FEAT_VIRTUALEDIT
9929 if (virtual_active())
9930 oneright();
9931 else
9932#endif
9933 {
9934#ifdef FEAT_MBYTE
9935 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009936 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 else
9938#endif
9939 ++curwin->w_cursor.col;
9940 }
9941
9942#ifdef FEAT_RIGHTLEFT
9943 revins_legal++;
9944 if (revins_chars)
9945 revins_chars--;
9946#endif
9947 }
9948 /* if 'whichwrap' set for cursor in insert mode, may move the
9949 * cursor to the next line */
9950 else if (vim_strchr(p_ww, ']') != NULL
9951 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9952 {
9953 start_arrow(&curwin->w_cursor);
9954 curwin->w_set_curswant = TRUE;
9955 ++curwin->w_cursor.lnum;
9956 curwin->w_cursor.col = 0;
9957 }
9958 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009959 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009960 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961}
9962
9963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009964ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965{
9966#ifdef FEAT_FOLDING
9967 if ((fdo_flags & FDO_HOR) && KeyTyped)
9968 foldOpenCursor();
9969#endif
9970 undisplay_dollar();
9971 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9972 || gchar_cursor() != NUL)
9973 {
9974 start_arrow(&curwin->w_cursor);
9975 (void)fwd_word(1L, FALSE, 0);
9976 curwin->w_set_curswant = TRUE;
9977 }
9978 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009979 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980}
9981
9982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009983ins_up(
9984 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985{
9986 pos_T tpos;
9987 linenr_T old_topline = curwin->w_topline;
9988#ifdef FEAT_DIFF
9989 int old_topfill = curwin->w_topfill;
9990#endif
9991
9992 undisplay_dollar();
9993 tpos = curwin->w_cursor;
9994 if (cursor_up(1L, TRUE) == OK)
9995 {
9996 if (startcol)
9997 coladvance(getvcol_nolist(&Insstart));
9998 if (old_topline != curwin->w_topline
9999#ifdef FEAT_DIFF
10000 || old_topfill != curwin->w_topfill
10001#endif
10002 )
10003 redraw_later(VALID);
10004 start_arrow(&tpos);
10005#ifdef FEAT_CINDENT
10006 can_cindent = TRUE;
10007#endif
10008 }
10009 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010010 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011}
10012
10013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010014ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015{
10016 pos_T tpos;
10017
10018 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010019
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010020 if (mod_mask & MOD_MASK_CTRL)
10021 {
10022 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010023 if (first_tabpage->tp_next != NULL)
10024 {
10025 start_arrow(&curwin->w_cursor);
10026 goto_tabpage(-1);
10027 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010028 return;
10029 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010030
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 tpos = curwin->w_cursor;
10032 if (onepage(BACKWARD, 1L) == OK)
10033 {
10034 start_arrow(&tpos);
10035#ifdef FEAT_CINDENT
10036 can_cindent = TRUE;
10037#endif
10038 }
10039 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010040 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041}
10042
10043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010044ins_down(
10045 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046{
10047 pos_T tpos;
10048 linenr_T old_topline = curwin->w_topline;
10049#ifdef FEAT_DIFF
10050 int old_topfill = curwin->w_topfill;
10051#endif
10052
10053 undisplay_dollar();
10054 tpos = curwin->w_cursor;
10055 if (cursor_down(1L, TRUE) == OK)
10056 {
10057 if (startcol)
10058 coladvance(getvcol_nolist(&Insstart));
10059 if (old_topline != curwin->w_topline
10060#ifdef FEAT_DIFF
10061 || old_topfill != curwin->w_topfill
10062#endif
10063 )
10064 redraw_later(VALID);
10065 start_arrow(&tpos);
10066#ifdef FEAT_CINDENT
10067 can_cindent = TRUE;
10068#endif
10069 }
10070 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010071 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072}
10073
10074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010075ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076{
10077 pos_T tpos;
10078
10079 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010080
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010081 if (mod_mask & MOD_MASK_CTRL)
10082 {
10083 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010084 if (first_tabpage->tp_next != NULL)
10085 {
10086 start_arrow(&curwin->w_cursor);
10087 goto_tabpage(0);
10088 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010089 return;
10090 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010091
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 tpos = curwin->w_cursor;
10093 if (onepage(FORWARD, 1L) == OK)
10094 {
10095 start_arrow(&tpos);
10096#ifdef FEAT_CINDENT
10097 can_cindent = TRUE;
10098#endif
10099 }
10100 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010101 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102}
10103
10104#ifdef FEAT_DND
10105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010106ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107{
10108 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10109}
10110#endif
10111
10112/*
10113 * Handle TAB in Insert or Replace mode.
10114 * Return TRUE when the TAB needs to be inserted like a normal character.
10115 */
10116 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010117ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118{
10119 int ind;
10120 int i;
10121 int temp;
10122
10123 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10124 Insstart_blank_vcol = get_nolist_virtcol();
10125 if (echeck_abbr(TAB + ABBR_OFF))
10126 return FALSE;
10127
10128 ind = inindent(0);
10129#ifdef FEAT_CINDENT
10130 if (ind)
10131 can_cindent = FALSE;
10132#endif
10133
10134 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010135 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 */
10137 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010138#ifdef FEAT_VARTABS
10139 && !(p_sta && ind
10140 /* These five lines mean 'tabstop' != 'shiftwidth' */
10141 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
10142 || (tabstop_count(curbuf->b_p_vts_array) == 1
10143 && tabstop_first(curbuf->b_p_vts_array)
10144 != get_sw_value(curbuf))
10145 || (tabstop_count(curbuf->b_p_vts_array) == 0
10146 && curbuf->b_p_ts != get_sw_value(curbuf))))
10147 && tabstop_count(curbuf->b_p_vsts_array) == 0
10148#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010149 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010150#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010151 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 return TRUE;
10153
10154 if (stop_arrow() == FAIL)
10155 return TRUE;
10156
10157 did_ai = FALSE;
10158#ifdef FEAT_SMARTINDENT
10159 did_si = FALSE;
10160 can_si = FALSE;
10161 can_si_back = FALSE;
10162#endif
10163 AppendToRedobuff((char_u *)"\t");
10164
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010165#ifdef FEAT_VARTABS
10166 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10167 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010168 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010169 temp -= get_nolist_virtcol() % temp;
10170 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010171 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010172 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010173 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010174 curbuf->b_p_vsts_array);
10175 else /* otherwise use 'tabstop' */
10176 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10177 curbuf->b_p_vts_array);
10178#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010180 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010181 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10182 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 else /* otherwise use 'tabstop' */
10184 temp = (int)curbuf->b_p_ts;
10185 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187
10188 /*
10189 * Insert the first space with ins_char(). It will delete one char in
10190 * replace mode. Insert the rest with ins_str(); it will not delete any
10191 * chars. For VREPLACE mode, we use ins_char() for all characters.
10192 */
10193 ins_char(' ');
10194 while (--temp > 0)
10195 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 if (State & VREPLACE_FLAG)
10197 ins_char(' ');
10198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 {
10200 ins_str((char_u *)" ");
10201 if (State & REPLACE_FLAG) /* no char replaced */
10202 replace_push(NUL);
10203 }
10204 }
10205
10206 /*
10207 * When 'expandtab' not set: Replace spaces by TABs where possible.
10208 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010209#ifdef FEAT_VARTABS
10210 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10211 || get_sts_value() > 0
10212 || (p_sta && ind)))
10213#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010214 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 {
10217 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 char_u *saved_line = NULL; /* init for GCC */
10219 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 pos_T fpos;
10221 pos_T *cursor;
10222 colnr_T want_vcol, vcol;
10223 int change_col = -1;
10224 int save_list = curwin->w_p_list;
10225
10226 /*
10227 * Get the current line. For VREPLACE mode, don't make real changes
10228 * yet, just work on a copy of the line.
10229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 if (State & VREPLACE_FLAG)
10231 {
10232 pos = curwin->w_cursor;
10233 cursor = &pos;
10234 saved_line = vim_strsave(ml_get_curline());
10235 if (saved_line == NULL)
10236 return FALSE;
10237 ptr = saved_line + pos.col;
10238 }
10239 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 {
10241 ptr = ml_get_cursor();
10242 cursor = &curwin->w_cursor;
10243 }
10244
10245 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10246 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10247 curwin->w_p_list = FALSE;
10248
10249 /* Find first white before the cursor */
10250 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010251 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 {
10253 --fpos.col;
10254 --ptr;
10255 }
10256
10257 /* In Replace mode, don't change characters before the insert point. */
10258 if ((State & REPLACE_FLAG)
10259 && fpos.lnum == Insstart.lnum
10260 && fpos.col < Insstart.col)
10261 {
10262 ptr += Insstart.col - fpos.col;
10263 fpos.col = Insstart.col;
10264 }
10265
10266 /* compute virtual column numbers of first white and cursor */
10267 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10268 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10269
Bram Moolenaar597a4222014-06-25 14:39:50 +020010270 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10271 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010272 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010274 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 if (vcol + i > want_vcol)
10276 break;
10277 if (*ptr != TAB)
10278 {
10279 *ptr = TAB;
10280 if (change_col < 0)
10281 {
10282 change_col = fpos.col; /* Column of first change */
10283 /* May have to adjust Insstart */
10284 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10285 Insstart.col = fpos.col;
10286 }
10287 }
10288 ++fpos.col;
10289 ++ptr;
10290 vcol += i;
10291 }
10292
10293 if (change_col >= 0)
10294 {
10295 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010296 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297
10298 /* Skip over the spaces we need. */
10299 while (vcol < want_vcol && *ptr == ' ')
10300 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010301 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 ++ptr;
10303 ++repl_off;
10304 }
10305 if (vcol > want_vcol)
10306 {
10307 /* Must have a char with 'showbreak' just before it. */
10308 --ptr;
10309 --repl_off;
10310 }
10311 fpos.col += repl_off;
10312
10313 /* Delete following spaces. */
10314 i = cursor->col - fpos.col;
10315 if (i > 0)
10316 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010317 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010319 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 for (temp = i; --temp >= 0; )
10321 replace_join(repl_off);
10322 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010323#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010324 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010325 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010326 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010327 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10328 (char_u *)"\t", 1);
10329 }
10330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 cursor->col -= i;
10332
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 /*
10334 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10335 * backspacing over the changed spacing and then inserting the new
10336 * spacing.
10337 */
10338 if (State & VREPLACE_FLAG)
10339 {
10340 /* Backspace from real cursor to change_col */
10341 backspace_until_column(change_col);
10342
10343 /* Insert each char in saved_line from changed_col to
10344 * ptr-cursor */
10345 ins_bytes_len(saved_line + change_col,
10346 cursor->col - change_col);
10347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 }
10349
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 if (State & VREPLACE_FLAG)
10351 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 curwin->w_p_list = save_list;
10353 }
10354
10355 return FALSE;
10356}
10357
10358/*
10359 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010360 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 */
10362 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010363ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364{
10365 int i;
10366
10367 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010368 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010370 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 undisplay_dollar();
10372
10373 /*
10374 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10375 * character under the cursor. Only push a NUL on the replace stack,
10376 * nothing to put back when the NL is deleted.
10377 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010378 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 replace_push(NUL);
10380
10381 /*
10382 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10383 * replacing the next line, so we push all of the characters left on the
10384 * line onto the replace stack. This is not done here though, it is done
10385 * in open_line().
10386 */
10387
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010388#ifdef FEAT_VIRTUALEDIT
10389 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10390 * CTRL-O). */
10391 if (virtual_active() && curwin->w_cursor.coladd > 0)
10392 coladvance(getviscol());
10393#endif
10394
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395#ifdef FEAT_RIGHTLEFT
10396# ifdef FEAT_FKMAP
10397 if (p_altkeymap && p_fkmap)
10398 fkmap(NL);
10399# endif
10400 /* NL in reverse insert will always start in the end of
10401 * current line. */
10402 if (revins_on)
10403 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10404#endif
10405
10406 AppendToRedobuff(NL_STR);
10407 i = open_line(FORWARD,
10408#ifdef FEAT_COMMENTS
10409 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10410#endif
10411 0, old_indent);
10412 old_indent = 0;
10413#ifdef FEAT_CINDENT
10414 can_cindent = TRUE;
10415#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010416#ifdef FEAT_FOLDING
10417 /* When inserting a line the cursor line must never be in a closed fold. */
10418 foldOpenCursor();
10419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010421 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422}
10423
10424#ifdef FEAT_DIGRAPHS
10425/*
10426 * Handle digraph in insert mode.
10427 * Returns character still to be inserted, or NUL when nothing remaining to be
10428 * done.
10429 */
10430 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010431ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
10433 int c;
10434 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010435 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436
10437 pc_status = PC_STATUS_UNSET;
10438 if (redrawing() && !char_avail())
10439 {
10440 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010441 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442
10443 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010444 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445#ifdef FEAT_CMDL_INFO
10446 add_to_showcmd_c(Ctrl_K);
10447#endif
10448 }
10449
10450#ifdef USE_ON_FLY_SCROLL
10451 dont_scroll = TRUE; /* disallow scrolling here */
10452#endif
10453
10454 /* don't map the digraph chars. This also prevents the
10455 * mode message to be deleted when ESC is hit */
10456 ++no_mapping;
10457 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010458 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 --no_mapping;
10460 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010461 if (did_putchar)
10462 /* when the line fits in 'columns' the '?' is at the start of the next
10463 * line and will not be removed by the redraw */
10464 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010465
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 if (IS_SPECIAL(c) || mod_mask) /* special key */
10467 {
10468#ifdef FEAT_CMDL_INFO
10469 clear_showcmd();
10470#endif
10471 insert_special(c, TRUE, FALSE);
10472 return NUL;
10473 }
10474 if (c != ESC)
10475 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010476 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 if (redrawing() && !char_avail())
10478 {
10479 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010480 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481
10482 if (char2cells(c) == 1)
10483 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010484 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010486 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 }
10488#ifdef FEAT_CMDL_INFO
10489 add_to_showcmd_c(c);
10490#endif
10491 }
10492 ++no_mapping;
10493 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010494 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 --no_mapping;
10496 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010497 if (did_putchar)
10498 /* when the line fits in 'columns' the '?' is at the start of the
10499 * next line and will not be removed by a redraw */
10500 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 if (cc != ESC)
10502 {
10503 AppendToRedobuff((char_u *)CTRL_V_STR);
10504 c = getdigraph(c, cc, TRUE);
10505#ifdef FEAT_CMDL_INFO
10506 clear_showcmd();
10507#endif
10508 return c;
10509 }
10510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511#ifdef FEAT_CMDL_INFO
10512 clear_showcmd();
10513#endif
10514 return NUL;
10515}
10516#endif
10517
10518/*
10519 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10520 * Returns the char to be inserted, or NUL if none found.
10521 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010522 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010523ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524{
10525 int c;
10526 int temp;
10527 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010528 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529
10530 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10531 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010532 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 return NUL;
10534 }
10535
10536 /* try to advance to the cursor column */
10537 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010538 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 prev_ptr = ptr;
10540 validate_virtcol();
10541 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10542 {
10543 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010544 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 }
10546 if ((colnr_T)temp > curwin->w_virtcol)
10547 ptr = prev_ptr;
10548
10549#ifdef FEAT_MBYTE
10550 c = (*mb_ptr2char)(ptr);
10551#else
10552 c = *ptr;
10553#endif
10554 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010555 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 return c;
10557}
10558
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010559/*
10560 * CTRL-Y or CTRL-E typed in Insert mode.
10561 */
10562 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010563ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010564{
10565 int c = tc;
10566
10567#ifdef FEAT_INS_EXPAND
10568 if (ctrl_x_mode == CTRL_X_SCROLL)
10569 {
10570 if (c == Ctrl_Y)
10571 scrolldown_clamp();
10572 else
10573 scrollup_clamp();
10574 redraw_later(VALID);
10575 }
10576 else
10577#endif
10578 {
10579 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10580 if (c != NUL)
10581 {
10582 long tw_save;
10583
10584 /* The character must be taken literally, insert like it
10585 * was typed after a CTRL-V, and pretend 'textwidth'
10586 * wasn't set. Digits, 'o' and 'x' are special after a
10587 * CTRL-V, don't use it for these. */
10588 if (c < 256 && !isalnum(c))
10589 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10590 tw_save = curbuf->b_p_tw;
10591 curbuf->b_p_tw = -1;
10592 insert_special(c, TRUE, FALSE);
10593 curbuf->b_p_tw = tw_save;
10594#ifdef FEAT_RIGHTLEFT
10595 revins_chars++;
10596 revins_legal++;
10597#endif
10598 c = Ctrl_V; /* pretend CTRL-V is last character */
10599 auto_format(FALSE, TRUE);
10600 }
10601 }
10602 return c;
10603}
10604
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605#ifdef FEAT_SMARTINDENT
10606/*
10607 * Try to do some very smart auto-indenting.
10608 * Used when inserting a "normal" character.
10609 */
10610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010611ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612{
10613 pos_T *pos, old_pos;
10614 char_u *ptr;
10615 int i;
10616 int temp;
10617
10618 /*
10619 * do some very smart indenting when entering '{' or '}'
10620 */
10621 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10622 {
10623 /*
10624 * for '}' set indent equal to indent of line containing matching '{'
10625 */
10626 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10627 {
10628 old_pos = curwin->w_cursor;
10629 /*
10630 * If the matching '{' has a ')' immediately before it (ignoring
10631 * white-space), then line up with the start of the line
10632 * containing the matching '(' if there is one. This handles the
10633 * case where an "if (..\n..) {" statement continues over multiple
10634 * lines -- webb
10635 */
10636 ptr = ml_get(pos->lnum);
10637 i = pos->col;
10638 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010639 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 ;
10641 curwin->w_cursor.lnum = pos->lnum;
10642 curwin->w_cursor.col = i;
10643 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10644 curwin->w_cursor = *pos;
10645 i = get_indent();
10646 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010648 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 (void)set_indent(i, SIN_CHANGED);
10651 }
10652 else if (curwin->w_cursor.col > 0)
10653 {
10654 /*
10655 * when inserting '{' after "O" reduce indent, but not
10656 * more than indent of previous line
10657 */
10658 temp = TRUE;
10659 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10660 {
10661 old_pos = curwin->w_cursor;
10662 i = get_indent();
10663 while (curwin->w_cursor.lnum > 1)
10664 {
10665 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10666
10667 /* ignore empty lines and lines starting with '#'. */
10668 if (*ptr != '#' && *ptr != NUL)
10669 break;
10670 }
10671 if (get_indent() >= i)
10672 temp = FALSE;
10673 curwin->w_cursor = old_pos;
10674 }
10675 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010676 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 }
10678 }
10679
10680 /*
10681 * set indent of '#' always to 0
10682 */
10683 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10684 {
10685 /* remember current indent for next line */
10686 old_indent = get_indent();
10687 (void)set_indent(0, SIN_CHANGED);
10688 }
10689
10690 /* Adjust ai_col, the char at this position can be deleted. */
10691 if (ai_col > curwin->w_cursor.col)
10692 ai_col = curwin->w_cursor.col;
10693}
10694#endif
10695
10696/*
10697 * Get the value that w_virtcol would have when 'list' is off.
10698 * Unless 'cpo' contains the 'L' flag.
10699 */
10700 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010701get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702{
10703 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10704 return getvcol_nolist(&curwin->w_cursor);
10705 validate_virtcol();
10706 return curwin->w_virtcol;
10707}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010708
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010709#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010710/*
10711 * Handle the InsertCharPre autocommand.
10712 * "c" is the character that was typed.
10713 * Return a pointer to allocated memory with the replacement string.
10714 * Return NULL to continue inserting "c".
10715 */
10716 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010717do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010718{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010719 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010720 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010721
10722 /* Return quickly when there is nothing to do. */
10723 if (!has_insertcharpre())
10724 return NULL;
10725
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010726# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010727 if (has_mbyte)
10728 buf[(*mb_char2bytes)(c, buf)] = NUL;
10729 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010730# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010731 {
10732 buf[0] = c;
10733 buf[1] = NUL;
10734 }
10735
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010736 /* Lock the text to avoid weird things from happening. */
10737 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010738 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010739
Bram Moolenaar704984a2012-06-01 14:57:51 +020010740 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010741 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010742 {
10743 /* Get the value of v:char. It may be empty or more than one
10744 * character. Only use it when changed, otherwise continue with the
10745 * original character to avoid breaking autoindent. */
10746 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10747 res = vim_strsave(get_vim_var_str(VV_CHAR));
10748 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010749
10750 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10751 --textlock;
10752
10753 return res;
10754}
10755#endif