blob: dcead23f54929a529ad882f157aad9b271cd9e69 [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/*
Bram Moolenaarfd133322019-03-29 12:20:27 +010018 * Definitions used for CTRL-X submode.
19 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
20 * ctrl_x_mode_names[].
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010022# define CTRL_X_WANT_IDENT 0x100
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010024# define CTRL_X_NORMAL 0 /* CTRL-N CTRL-P completion, default */
25# define CTRL_X_NOT_DEFINED_YET 1
26# define CTRL_X_SCROLL 2
27# define CTRL_X_WHOLE_LINE 3
28# define CTRL_X_FILES 4
29# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
31# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
32# define CTRL_X_FINISHED 8
33# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
34# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
35# define CTRL_X_CMDLINE 11
36# define CTRL_X_FUNCTION 12
37# define CTRL_X_OMNI 13
38# define CTRL_X_SPELL 14
39# define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
40# define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010042# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43# define CTRL_X_MODE_LINE_OR_EVAL(m) ((m) == CTRL_X_WHOLE_LINE || (m) == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
Bram Moolenaarfd133322019-03-29 12:20:27 +010045// Message for CTRL-X mode, index is ctrl_x_mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000046static char *ctrl_x_msgs[] =
47{
Bram Moolenaarfd133322019-03-29 12:20:27 +010048 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
Bram Moolenaar910f66f2006-04-05 20:41:53 +000049 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaarfd133322019-03-29 12:20:27 +010050 NULL, // CTRL_X_SCROLL: depends on state
Bram Moolenaar071d4272004-06-13 20:20:40 +000051 N_(" Whole line completion (^L^N^P)"),
52 N_(" File name completion (^F^N^P)"),
53 N_(" Tag completion (^]^N^P)"),
54 N_(" Path pattern completion (^N^P)"),
55 N_(" Definition completion (^D^N^P)"),
Bram Moolenaarfd133322019-03-29 12:20:27 +010056 NULL, // CTRL_X_FINISHED
Bram Moolenaar071d4272004-06-13 20:20:40 +000057 N_(" Dictionary completion (^K^N^P)"),
58 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000059 N_(" Command-line completion (^V^N^P)"),
60 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000061 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000062 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000063 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaarfd133322019-03-29 12:20:27 +010064 NULL, // CTRL_X_EVAL doesn't use msg.
Bram Moolenaar071d4272004-06-13 20:20:40 +000065};
66
Bram Moolenaarfd133322019-03-29 12:20:27 +010067static char *ctrl_x_mode_names[] = {
68 "keyword",
69 "ctrl_x",
70 "unknown", // CTRL_X_SCROLL
71 "whole_line",
72 "files",
73 "tags",
74 "path_patterns",
75 "path_defines",
76 "unknown", // CTRL_X_FINISHED
77 "dictionary",
78 "thesaurus",
79 "cmdline",
80 "function",
81 "omni",
82 "spell",
83 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
84 "eval"
85 };
86
87
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000088static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010089# ifdef FEAT_COMPL_FUNC
Bram Moolenaar37dd0182010-11-10 16:54:20 +010090static char e_complwin[] = N_("E839: Completion function changed window");
91static char e_compldel[] = N_("E840: Completion function deleted text");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
94/*
95 * Structure used to store one match for insert completion.
96 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000097typedef struct compl_S compl_T;
98struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000099{
Bram Moolenaar572cb562005-08-05 21:35:02 +0000100 compl_T *cp_next;
101 compl_T *cp_prev;
102 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000103 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +0000104 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000105 char_u *cp_fname; /* file containing the match, allocated when
106 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000107 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
108 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109};
110
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100111# define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
112# define FREE_FNAME (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113
114/*
115 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000116 * "compl_first_match" points to the start of the list.
117 * "compl_curr_match" points to the currently selected entry.
118 * "compl_shown_match" is different from compl_curr_match during
119 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000121static compl_T *compl_first_match = NULL;
122static compl_T *compl_curr_match = NULL;
123static compl_T *compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +0200124static compl_T *compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000126/* After using a cursor key <Enter> selects a match in the popup menu,
127 * otherwise it inserts a line break. */
128static int compl_enter_selects = FALSE;
129
Bram Moolenaara6557602006-02-04 22:43:20 +0000130/* When "compl_leader" is not NULL only matches that start with this string
131 * are used. */
132static char_u *compl_leader = NULL;
133
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000134static int compl_get_longest = FALSE; /* put longest common string
135 in compl_leader */
136
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200137static int compl_no_insert = FALSE; /* FALSE: select & insert
138 TRUE: noinsert */
139static int compl_no_select = FALSE; /* FALSE: select & insert
140 TRUE: noselect */
141
Bram Moolenaara6557602006-02-04 22:43:20 +0000142static int compl_used_match; /* Selected one of the matches. When
143 FALSE the match was edited or using
144 the longest common string. */
145
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000146static int compl_was_interrupted = FALSE; /* didn't finish finding
147 completions. */
148
149static int compl_restarting = FALSE; /* don't insert match */
150
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000151/* When the first completion is done "compl_started" is set. When it's
152 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000153static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000154
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100155/* Which Ctrl-X mode are we in? */
156static int ctrl_x_mode = CTRL_X_NORMAL;
157
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000158/* Set when doing something for completion that may call edit() recursively,
159 * which is not allowed. */
160static int compl_busy = FALSE;
161
Bram Moolenaar572cb562005-08-05 21:35:02 +0000162static int compl_matches = 0;
163static char_u *compl_pattern = NULL;
164static int compl_direction = FORWARD;
165static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000166static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000167static pos_T compl_startpos;
168static colnr_T compl_col = 0; /* column where the text starts
169 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000170static char_u *compl_orig_text = NULL; /* text as it was before
171 * completion started */
172static int compl_cont_mode = 0;
173static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174
Bram Moolenaar82139082011-09-14 16:52:09 +0200175static int compl_opt_refresh_always = FALSE;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100176static int compl_opt_suppress_empty = FALSE;
Bram Moolenaar82139082011-09-14 16:52:09 +0200177
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100178static void ins_ctrl_x(void);
179static int has_compl_option(int dict_opt);
180static int ins_compl_accept_char(int c);
181static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100182static void ins_compl_longest_match(compl_T *match);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100183static void ins_compl_del_pum(void);
184static int pum_wanted(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100185static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
186static char_u *find_line_end(char_u *ptr);
187static void ins_compl_free(void);
188static void ins_compl_clear(void);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100189static char_u *ins_compl_mode(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static int ins_compl_bs(void);
191static int ins_compl_need_restart(void);
192static void ins_compl_new_leader(void);
193static void ins_compl_addleader(int c);
194static int ins_compl_len(void);
195static void ins_compl_restart(void);
196static void ins_compl_set_original_text(char_u *str);
197static void ins_compl_addfrommatch(void);
198static int ins_compl_prep(int c);
199static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100200# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100201static void ins_compl_add_list(list_T *list);
202static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100203# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200205static void ins_compl_insert(int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100206static int ins_compl_key2dir(int c);
207static int ins_compl_pum_key(int c);
208static int ins_compl_key2count(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100209static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100210static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100211static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#endif /* FEAT_INS_EXPAND */
213
214#define BACKSPACE_CHAR 1
215#define BACKSPACE_WORD 2
216#define BACKSPACE_WORD_NOT_SPACE 3
217#define BACKSPACE_LINE 4
218
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void ins_redraw(int ready);
220static void ins_ctrl_v(void);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200221#ifdef FEAT_JOB_CHANNEL
222static void init_prompt(int cmdchar_todo);
223#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void undisplay_dollar(void);
225static void insert_special(int, int, int);
226static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
227static void check_auto_format(int);
228static void redo_literal(int c);
229static void start_arrow(pos_T *end_insert_pos);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000231#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void check_spell_redraw(void);
233static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000234static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000235#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
237static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void replace_join(int off);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void mb_replace_pop_ins(int cc);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static void replace_flush(void);
241static void replace_do_bs(int limit_col);
242static int del_char_after_col(int limit_col);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static void ins_reg(void);
244static void ins_ctrl_g(void);
245static void ins_ctrl_hat(void);
246static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static int ins_start_select(int c);
251static void ins_insert(int replaceState);
252static void ins_ctrl_o(void);
253static void ins_shift(int c, int lastc);
254static void ins_del(void);
255static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static void ins_mouse(int c);
258static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000260#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000262#endif
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100263static void ins_left(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static void ins_home(int c);
265static void ins_end(int c);
266static void ins_s_left(void);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100267static void ins_right(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100268static void ins_s_right(void);
269static void ins_up(int startcol);
270static void ins_pageup(void);
271static void ins_down(int startcol);
272static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100274static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100276static int ins_tab(void);
277static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100279static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100281static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100283static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100285#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100286static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100287#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200288static int ins_apply_autocmds(event_T event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289
290static colnr_T Insstart_textlen; /* length of line when insert started */
291static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100292static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293
294static char_u *last_insert = NULL; /* the text of the previous insert,
295 K_SPECIAL and CSI are escaped */
296static int last_insert_skip; /* nr of chars in front of previous insert */
297static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000298static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299
300#ifdef FEAT_CINDENT
301static int can_cindent; /* may do cindenting on this line */
302#endif
303
304static int old_indent = 0; /* for ^^D command in insert mode */
305
306#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000307static int revins_on; /* reverse insert mode on */
308static int revins_chars; /* how much to skip after edit */
309static int revins_legal; /* was the last char 'legal'? */
310static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
312
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313static int ins_need_undo; /* call u_save() before inserting a
314 char. Set when edit() is called.
315 after that arrow_used is used. */
316
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100317static int did_add_space = FALSE; // auto_format() added an extra space
318 // under the cursor
319static int dont_sync_undo = FALSE; // CTRL-G U prevents syncing undo for
320 // the next left/right cursor key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
322/*
323 * edit(): Start inserting text.
324 *
325 * "cmdchar" can be:
326 * 'i' normal insert command
327 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100328 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 * 'R' replace command
330 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
331 * but still only one <CR> is inserted. The <Esc> is not used for redo.
332 * 'g' "gI" command.
333 * 'V' "gR" command for Virtual Replace mode.
334 * 'v' "gr" command for single character Virtual Replace mode.
335 *
336 * This function is not called recursively. For CTRL-O commands, it returns
337 * and lets the caller handle the Normal-mode command.
338 *
339 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
340 */
341 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100342edit(
343 int cmdchar,
344 int startln, /* if set, insert at start of line */
345 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346{
347 int c = 0;
348 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100349 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000350 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 int i;
353 int did_backspace = TRUE; /* previous char was backspace */
354#ifdef FEAT_CINDENT
355 int line_is_white = FALSE; /* line is empty before insert */
356#endif
357 linenr_T old_topline = 0; /* topline before insertion */
358#ifdef FEAT_DIFF
359 int old_topfill = -1;
360#endif
361 int inserted_space = FALSE; /* just inserted a space */
362 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000363 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200364#ifdef FEAT_JOB_CHANNEL
365 int cmdchar_todo = cmdchar;
366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000368 /* Remember whether editing was restarted after CTRL-O. */
369 did_restart_edit = restart_edit;
370
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 /* sleep before redrawing, needed for "CTRL-O :" that results in an
372 * error message */
373 check_for_delay(TRUE);
374
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100375 /* set Insstart_orig to Insstart */
376 update_Insstart_orig = TRUE;
377
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378#ifdef HAVE_SANDBOX
379 /* Don't allow inserting in the sandbox. */
380 if (sandbox != 0)
381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100382 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 return FALSE;
384 }
385#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000386 /* Don't allow changes in the buffer while editing the cmdline. The
387 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000388 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100390 emsg(_(e_secure));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000391 return FALSE;
392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393
394#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000395 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000396 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000397 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100398 emsg(_(e_secure));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000399 return FALSE;
400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 ins_compl_clear(); /* clear stuff for CTRL-X mode */
402#endif
403
Bram Moolenaar843ee412004-06-30 16:16:41 +0000404 /*
405 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
406 */
407 if (cmdchar != 'r' && cmdchar != 'v')
408 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100409 pos_T save_cursor = curwin->w_cursor;
410
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100411#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000412 if (cmdchar == 'R')
413 ptr = (char_u *)"r";
414 else if (cmdchar == 'V')
415 ptr = (char_u *)"v";
416 else
417 ptr = (char_u *)"i";
418 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200419 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100420#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200421 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100422
Bram Moolenaar097c9922013-05-19 21:15:15 +0200423 /* Make sure the cursor didn't move. Do call check_cursor_col() in
424 * case the text was modified. Since Insert mode was not started yet
425 * a call to check_cursor_col() may move the cursor, especially with
426 * the "A" command, thus set State to avoid that. Also check that the
427 * line number is still valid (lines may have been deleted).
428 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100429 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100430#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200431 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100432#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200433 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100434 {
435 int save_state = State;
436
437 curwin->w_cursor = save_cursor;
438 State = INSERT;
439 check_cursor_col();
440 State = save_state;
441 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000442 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000443
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200444#ifdef FEAT_CONCEAL
445 /* Check if the cursor line needs redrawing before changing State. If
446 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200447 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200448#endif
449
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450#ifdef FEAT_MOUSE
451 /*
452 * When doing a paste with the middle mouse button, Insstart is set to
453 * where the paste started.
454 */
455 if (where_paste_started.lnum != 0)
456 Insstart = where_paste_started;
457 else
458#endif
459 {
460 Insstart = curwin->w_cursor;
461 if (startln)
462 Insstart.col = 0;
463 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000464 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 Insstart_blank_vcol = MAXCOL;
466 if (!did_ai)
467 ai_col = 0;
468
469 if (cmdchar != NUL && restart_edit == 0)
470 {
471 ResetRedobuff();
472 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 if (cmdchar == 'V' || cmdchar == 'v')
474 {
475 /* "gR" or "gr" command */
476 AppendCharToRedobuff('g');
477 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
478 }
479 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100481 if (cmdchar == K_PS)
482 AppendCharToRedobuff('a');
483 else
484 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 if (cmdchar == 'g') /* "gI" command */
486 AppendCharToRedobuff('I');
487 else if (cmdchar == 'r') /* "r<CR>" command */
488 count = 1; /* insert only one <CR> */
489 }
490 }
491
492 if (cmdchar == 'R')
493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 State = REPLACE;
495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 else if (cmdchar == 'V' || cmdchar == 'v')
497 {
498 State = VREPLACE;
499 replaceState = VREPLACE;
500 orig_line_count = curbuf->b_ml.ml_line_count;
501 vr_lines_changed = 1;
502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 else
504 State = INSERT;
505
506 stop_insert_mode = FALSE;
507
508 /*
509 * Need to recompute the cursor position, it might move when the cursor is
510 * on a TAB or special character.
511 */
512 curs_columns(TRUE);
513
514 /*
515 * Enable langmap or IME, indicated by 'iminsert'.
516 * Note that IME may enabled/disabled without us noticing here, thus the
517 * 'iminsert' value may not reflect what is actually used. It is updated
518 * when hitting <Esc>.
519 */
520 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
521 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100522#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
524#endif
525
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526#ifdef FEAT_MOUSE
527 setmouse();
528#endif
529#ifdef FEAT_CMDL_INFO
530 clear_showcmd();
531#endif
532#ifdef FEAT_RIGHTLEFT
533 /* there is no reverse replace mode */
534 revins_on = (State == INSERT && p_ri);
535 if (revins_on)
536 undisplay_dollar();
537 revins_chars = 0;
538 revins_legal = 0;
539 revins_scol = -1;
540#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100541 if (!p_ek)
542 /* Disable bracketed paste mode, we won't recognize the escape
543 * sequences. */
544 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545
546 /*
547 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100548 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
549 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 */
551 if (restart_edit != 0 && stuff_empty())
552 {
553#ifdef FEAT_MOUSE
554 /*
555 * After a paste we consider text typed to be part of the insert for
556 * the pasted text. You can backspace over the pasted text too.
557 */
558 if (where_paste_started.lnum)
559 arrow_used = FALSE;
560 else
561#endif
562 arrow_used = TRUE;
563 restart_edit = 0;
564
565 /*
566 * If the cursor was after the end-of-line before the CTRL-O and it is
567 * now at the end-of-line, put it after the end-of-line (this is not
568 * correct in very rare cases).
569 * Also do this if curswant is greater than the current virtual
570 * column. Eg after "^O$" or "^O80|".
571 */
572 validate_virtcol();
573 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000574 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 || curwin->w_curswant > curwin->w_virtcol)
576 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
577 {
578 if (ptr[1] == NUL)
579 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 else if (has_mbyte)
581 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000582 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (ptr[i] == NUL)
584 curwin->w_cursor.col += i;
585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000587 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
589 else
590 arrow_used = FALSE;
591
592 /* we are in insert mode now, don't need to start it anymore */
593 need_start_insertmode = FALSE;
594
595 /* Need to save the line for undo before inserting the first char. */
596 ins_need_undo = TRUE;
597
598#ifdef FEAT_MOUSE
599 where_paste_started.lnum = 0;
600#endif
601#ifdef FEAT_CINDENT
602 can_cindent = TRUE;
603#endif
604#ifdef FEAT_FOLDING
605 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
606 * restarting. */
607 if (!p_im && did_restart_edit == 0)
608 foldOpenCursor();
609#endif
610
611 /*
612 * If 'showmode' is set, show the current (insert/replace/..) mode.
613 * A warning message for changing a readonly file is given here, before
614 * actually changing anything. It's put after the mode, if any.
615 */
616 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000617 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 i = showmode();
619
620 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000621 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
623#ifdef CURSOR_SHAPE
624 ui_cursor_shape(); /* may show different cursor shape */
625#endif
626#ifdef FEAT_DIGRAPHS
627 do_digraph(-1); /* clear digraphs */
628#endif
629
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000630 /*
631 * Get the current length of the redo buffer, those characters have to be
632 * skipped if we want to get to the inserted characters.
633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 ptr = get_inserted();
635 if (ptr == NULL)
636 new_insert_skip = 0;
637 else
638 {
639 new_insert_skip = (int)STRLEN(ptr);
640 vim_free(ptr);
641 }
642
643 old_indent = 0;
644
645 /*
646 * Main loop in Insert mode: repeat until Insert mode is left.
647 */
648 for (;;)
649 {
650#ifdef FEAT_RIGHTLEFT
651 if (!revins_legal)
652 revins_scol = -1; /* reset on illegal motions */
653 else
654 revins_legal = 0;
655#endif
656 if (arrow_used) /* don't repeat insert when arrow key used */
657 count = 0;
658
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100659 if (update_Insstart_orig)
660 Insstart_orig = Insstart;
661
Bram Moolenaar00672e12016-06-26 18:38:13 +0200662 if (stop_insert_mode
663#ifdef FEAT_INS_EXPAND
664 && !pum_visible()
665#endif
666 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {
668 /* ":stopinsert" used or 'insertmode' reset */
669 count = 0;
670 goto doESCkey;
671 }
672
673 /* set curwin->w_curswant for next K_DOWN or K_UP */
674 if (!arrow_used)
675 curwin->w_set_curswant = TRUE;
676
677 /* If there is no typeahead may check for timestamps (e.g., for when a
678 * menu invoked a shell command). */
679 if (stuff_empty())
680 {
681 did_check_timestamps = FALSE;
682 if (need_check_timestamps)
683 check_timestamps(FALSE);
684 }
685
686 /*
687 * When emsg() was called msg_scroll will have been set.
688 */
689 msg_scroll = FALSE;
690
691#ifdef FEAT_GUI
692 /* When 'mousefocus' is set a mouse movement may have taken us to
693 * another window. "need_mouse_correct" may then be set because of an
694 * autocommand. */
695 if (need_mouse_correct)
696 gui_mouse_correct();
697#endif
698
699#ifdef FEAT_FOLDING
700 /* Open fold at the cursor line, according to 'foldopen'. */
701 if (fdo_flags & FDO_INSERT)
702 foldOpenCursor();
703 /* Close folds where the cursor isn't, according to 'foldclose' */
704 if (!char_avail())
705 foldCheckClose();
706#endif
707
Bram Moolenaarf2732452018-06-03 14:47:35 +0200708#ifdef FEAT_JOB_CHANNEL
709 if (bt_prompt(curbuf))
710 {
711 init_prompt(cmdchar_todo);
712 cmdchar_todo = NUL;
713 }
714#endif
715
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 /*
717 * If we inserted a character at the last position of the last line in
718 * the window, scroll the window one line up. This avoids an extra
719 * redraw.
720 * This is detected when the cursor column is smaller after inserting
721 * something.
722 * Don't do this when the topline changed already, it has
723 * already been adjusted (by insertchar() calling open_line())).
724 */
725 if (curbuf->b_mod_set
726 && curwin->w_p_wrap
727 && !did_backspace
728 && curwin->w_topline == old_topline
729#ifdef FEAT_DIFF
730 && curwin->w_topfill == old_topfill
731#endif
732 )
733 {
734 mincol = curwin->w_wcol;
735 validate_cursor_col();
736
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200737 if (
738#ifdef FEAT_VARTABS
739 (int)curwin->w_wcol < mincol - tabstop_at(
740 get_nolist_virtcol(), curbuf->b_p_ts,
741 curbuf->b_p_vts_array)
742#else
743 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 && curwin->w_wrow == W_WINROW(curwin)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100746 + curwin->w_height - 1 - get_scrolloff_value()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 && (curwin->w_cursor.lnum != curwin->w_topline
748#ifdef FEAT_DIFF
749 || curwin->w_topfill > 0
750#endif
751 ))
752 {
753#ifdef FEAT_DIFF
754 if (curwin->w_topfill > 0)
755 --curwin->w_topfill;
756 else
757#endif
758#ifdef FEAT_FOLDING
759 if (hasFolding(curwin->w_topline, NULL, &old_topline))
760 set_topline(curwin, old_topline + 1);
761 else
762#endif
763 set_topline(curwin, curwin->w_topline + 1);
764 }
765 }
766
767 /* May need to adjust w_topline to show the cursor. */
768 update_topline();
769
770 did_backspace = FALSE;
771
772 validate_cursor(); /* may set must_redraw */
773
774 /*
775 * Redraw the display when no characters are waiting.
776 * Also shows mode, ruler and positions cursor.
777 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000778 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (curwin->w_p_scb)
781 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782
Bram Moolenaar860cae12010-06-05 23:22:07 +0200783 if (curwin->w_p_crb)
784 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 update_curswant();
786 old_topline = curwin->w_topline;
787#ifdef FEAT_DIFF
788 old_topfill = curwin->w_topfill;
789#endif
790
791#ifdef USE_ON_FLY_SCROLL
792 dont_scroll = FALSE; /* allow scrolling here */
793#endif
794
795 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100796 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100798 if (c != K_CURSORHOLD)
799 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200800
801 /* After using CTRL-G U the next cursor key will not break undo. */
802 if (dont_sync_undo == MAYBE)
803 dont_sync_undo = TRUE;
804 else
805 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100806 if (cmdchar == K_PS)
807 /* Got here from normal mode when bracketed paste started. */
808 c = K_PS;
809 else
810 do
811 {
812 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200813
814 if (stop_insert_mode)
815 {
816 // Insert mode ended, possibly from a callback.
817 count = 0;
818 nomove = TRUE;
819 goto doESCkey;
820 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100821 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000823 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
824 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#ifdef FEAT_RIGHTLEFT
827 if (p_hkmap && KeyTyped)
828 c = hkmap(c); /* Hebrew mode mapping */
829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000832 /*
833 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000834 * and the cursor is still in the completed word. Only when there is
835 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000836 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000837 if (compl_started
838 && pum_wanted()
839 && curwin->w_cursor.col >= compl_col
840 && (compl_shown_match == NULL
841 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000842 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000843 /* BS: Delete one character from "compl_leader". */
844 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000845 && curwin->w_cursor.col > compl_col
846 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000847 continue;
848
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000849 /* When no match was selected or it was edited. */
850 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000851 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000852 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000853 * "compl_leader". Except when at the original match and
854 * there is nothing to add, CTRL-L works like CTRL-P then. */
855 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100856 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000857 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000858 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000859 {
860 ins_compl_addfrommatch();
861 continue;
862 }
863
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000864 /* A non-white character that fits in with the current
865 * completion: Add to "compl_leader". */
866 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000867 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100868#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100869 /* Trigger InsertCharPre. */
870 char_u *str = do_insert_char_pre(c);
871 char_u *p;
872
873 if (str != NULL)
874 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100875 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100876 ins_compl_addleader(PTR2CHAR(p));
877 vim_free(str);
878 }
879 else
880#endif
881 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000882 continue;
883 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000884
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000885 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000886 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200887 if ((c == Ctrl_Y || (compl_enter_selects
888 && (c == CAR || c == K_KENTER || c == NL)))
889 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000890 {
891 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200892 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000893 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000894 }
895 }
896
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
898 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000899 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000900 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000901 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902#endif
903
Bram Moolenaar488c6512005-08-11 20:09:58 +0000904 /* CTRL-\ CTRL-N goes to Normal mode,
905 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
906 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 if (c == Ctrl_BSL)
908 {
909 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000910 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 ++no_mapping;
912 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000913 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 --no_mapping;
915 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000916 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000918 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 vungetc(c);
920 c = Ctrl_BSL;
921 }
922 else if (c == Ctrl_G && p_im)
923 continue;
924 else
925 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000926 if (c == Ctrl_O)
927 {
928 ins_ctrl_o();
929 ins_at_eol = FALSE; /* cursor keeps its column */
930 nomove = TRUE;
931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 count = 0;
933 goto doESCkey;
934 }
935 }
936
937#ifdef FEAT_DIGRAPHS
938 c = do_digraph(c);
939#endif
940
941#ifdef FEAT_INS_EXPAND
942 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
943 goto docomplete;
944#endif
945 if (c == Ctrl_V || c == Ctrl_Q)
946 {
947 ins_ctrl_v();
948 c = Ctrl_V; /* pretend CTRL-V is last typed character */
949 continue;
950 }
951
952#ifdef FEAT_CINDENT
953 if (cindent_on()
954# ifdef FEAT_INS_EXPAND
955 && ctrl_x_mode == 0
956# endif
957 )
958 {
959 /* A key name preceded by a bang means this key is not to be
960 * inserted. Skip ahead to the re-indenting below.
961 * A key name preceded by a star means that indenting has to be
962 * done before inserting the key. */
963 line_is_white = inindent(0);
964 if (in_cinkeys(c, '!', line_is_white))
965 goto force_cindent;
966 if (can_cindent && in_cinkeys(c, '*', line_is_white)
967 && stop_arrow() == OK)
968 do_c_expr_indent();
969 }
970#endif
971
972#ifdef FEAT_RIGHTLEFT
973 if (curwin->w_p_rl)
974 switch (c)
975 {
976 case K_LEFT: c = K_RIGHT; break;
977 case K_S_LEFT: c = K_S_RIGHT; break;
978 case K_C_LEFT: c = K_C_RIGHT; break;
979 case K_RIGHT: c = K_LEFT; break;
980 case K_S_RIGHT: c = K_S_LEFT; break;
981 case K_C_RIGHT: c = K_C_LEFT; break;
982 }
983#endif
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 /*
986 * If 'keymodel' contains "startsel", may start selection. If it
987 * does, a CTRL-O and c will be stuffed, we need to get these
988 * characters.
989 */
990 if (ins_start_select(c))
991 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
993 /*
994 * The big switch to handle a character in insert mode.
995 */
996 switch (c)
997 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000998 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (echeck_abbr(ESC + ABBR_OFF))
1000 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001001 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001003 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#ifdef FEAT_CMDWIN
1005 if (c == Ctrl_C && cmdwin_type != 0)
1006 {
1007 /* Close the cmdline window. */
1008 cmdwin_result = K_IGNORE;
1009 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001010 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 goto doESCkey;
1012 }
1013#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001014#ifdef FEAT_JOB_CHANNEL
1015 if (c == Ctrl_C && bt_prompt(curbuf))
1016 {
1017 if (invoke_prompt_interrupt())
1018 {
1019 if (!bt_prompt(curbuf))
1020 // buffer changed to a non-prompt buffer, get out of
1021 // Insert mode
1022 goto doESCkey;
1023 break;
1024 }
1025 }
1026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027
1028#ifdef UNIX
1029do_intr:
1030#endif
1031 /* when 'insertmode' set, and not halfway a mapping, don't leave
1032 * Insert mode */
1033 if (goto_im())
1034 {
1035 if (got_int)
1036 {
1037 (void)vgetc(); /* flush all buffers */
1038 got_int = FALSE;
1039 }
1040 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001041 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 break;
1043 }
1044doESCkey:
1045 /*
1046 * This is the ONLY return from edit()!
1047 */
1048 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1049 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001050 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 o_lnum = curwin->w_cursor.lnum;
1052
Bram Moolenaar488c6512005-08-11 20:09:58 +00001053 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001054 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001055 // When CTRL-C was typed got_int will be set, with the result
1056 // that the autocommands won't be executed. When mapped got_int
1057 // is not set, but let's keep the behavior the same.
1058 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001059 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001060 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 continue;
1064
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065 case Ctrl_Z: /* suspend when 'insertmode' set */
1066 if (!p_im)
1067 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001068 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001069#ifdef CURSOR_SHAPE
1070 ui_cursor_shape(); /* may need to update cursor shape */
1071#endif
1072 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073
1074 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001075#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001076 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001077 goto docomplete;
1078#endif
1079 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1080 break;
1081 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001082
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001083 /* don't move the cursor left when 'virtualedit' has "onemore". */
1084 if (ve_flags & VE_ONEMORE)
1085 {
1086 ins_at_eol = FALSE;
1087 nomove = TRUE;
1088 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001089 count = 0;
1090 goto doESCkey;
1091
Bram Moolenaar572cb562005-08-05 21:35:02 +00001092 case K_INS: /* toggle insert/replace mode */
1093 case K_KINS:
1094 ins_insert(replaceState);
1095 break;
1096
1097 case K_SELECT: /* end of Select mode mapping - ignore */
1098 break;
1099
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 case K_HELP: /* Help key works like <ESC> <Help> */
1101 case K_F1:
1102 case K_XF1:
1103 stuffcharReadbuff(K_HELP);
1104 if (p_im)
1105 need_start_insertmode = TRUE;
1106 goto doESCkey;
1107
1108#ifdef FEAT_NETBEANS_INTG
1109 case K_F21: /* NetBeans command */
1110 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001111 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 --no_mapping;
1113 netbeans_keycommand(i);
1114 break;
1115#endif
1116
1117 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 case NUL:
1119 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001120 /* For ^@ the trailing ESC will end the insert, unless there is an
1121 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1123 && c != Ctrl_A && !p_im)
1124 goto doESCkey; /* quit insert mode */
1125 inserted_space = FALSE;
1126 break;
1127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001128 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 ins_reg();
1130 auto_format(FALSE, TRUE);
1131 inserted_space = FALSE;
1132 break;
1133
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001134 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 ins_ctrl_g();
1136 break;
1137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case Ctrl_HAT: /* switch input mode and/or langmap */
1139 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 break;
1141
1142#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (!p_ari)
1145 goto normalchar;
1146 ins_ctrl_();
1147 break;
1148#endif
1149
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001150 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1152 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1153 goto docomplete;
1154#endif
1155 /* FALLTHROUGH */
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158# ifdef FEAT_INS_EXPAND
1159 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1160 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001161 if (has_compl_option(FALSE))
1162 goto docomplete;
1163 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165# endif
1166 ins_shift(c, lastc);
1167 auto_format(FALSE, TRUE);
1168 inserted_space = FALSE;
1169 break;
1170
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001171 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 case K_KDEL:
1173 ins_del();
1174 auto_format(FALSE, TRUE);
1175 break;
1176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001177 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 case Ctrl_H:
1179 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1180 auto_format(FALSE, TRUE);
1181 break;
1182
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001183 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001184#ifdef FEAT_JOB_CHANNEL
1185 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1186 {
1187 // In a prompt window CTRL-W is used for window commands.
1188 // Use Shift-CTRL-W to delete a word.
1189 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001190 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001191 nomove = TRUE;
1192 count = 0;
1193 goto doESCkey;
1194 }
1195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1197 auto_format(FALSE, TRUE);
1198 break;
1199
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001200 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001201# ifdef FEAT_COMPL_FUNC
1202 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001203 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001204 goto docomplete;
1205# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1207 auto_format(FALSE, TRUE);
1208 inserted_space = FALSE;
1209 break;
1210
1211#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001212 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 case K_LEFTMOUSE_NM:
1214 case K_LEFTDRAG:
1215 case K_LEFTRELEASE:
1216 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001217 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 case K_MIDDLEMOUSE:
1219 case K_MIDDLEDRAG:
1220 case K_MIDDLERELEASE:
1221 case K_RIGHTMOUSE:
1222 case K_RIGHTDRAG:
1223 case K_RIGHTRELEASE:
1224 case K_X1MOUSE:
1225 case K_X1DRAG:
1226 case K_X1RELEASE:
1227 case K_X2MOUSE:
1228 case K_X2DRAG:
1229 case K_X2RELEASE:
1230 ins_mouse(c);
1231 break;
1232
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001233 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001234 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 break;
1236
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001237 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001238 ins_mousescroll(MSCR_UP);
1239 break;
1240
1241 case K_MOUSELEFT: /* Scroll wheel left */
1242 ins_mousescroll(MSCR_LEFT);
1243 break;
1244
1245 case K_MOUSERIGHT: /* Scroll wheel right */
1246 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 break;
1248#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001249 case K_PS:
1250 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1251 if (cmdchar == K_PS)
1252 /* invoked from normal mode, bail out */
1253 goto doESCkey;
1254 break;
1255 case K_PE:
1256 /* Got K_PE without K_PS, ignore. */
1257 break;
1258
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001259#ifdef FEAT_GUI_TABLINE
1260 case K_TABLINE:
1261 case K_TABMENU:
1262 ins_tabline(c);
1263 break;
1264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001266 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 break;
1268
Bram Moolenaar754b5602006-02-09 23:53:20 +00001269 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001270 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001271 did_cursorhold = TRUE;
1272 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001273
Bram Moolenaar4f974752019-02-17 17:44:42 +01001274#ifdef FEAT_GUI_MSWIN
1275 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1276 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001277 case K_F4:
1278 if (mod_mask != MOD_MASK_ALT)
1279 goto normalchar;
1280 break;
1281#endif
1282
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283#ifdef FEAT_GUI
1284 case K_VER_SCROLLBAR:
1285 ins_scroll();
1286 break;
1287
1288 case K_HOR_SCROLLBAR:
1289 ins_horscroll();
1290 break;
1291#endif
1292
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001293 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 case K_S_HOME:
1296 case K_C_HOME:
1297 ins_home(c);
1298 break;
1299
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001300 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 case K_S_END:
1303 case K_C_END:
1304 ins_end(c);
1305 break;
1306
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001307 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001308 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1309 ins_s_left();
1310 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001311 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 break;
1313
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001314 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 case K_C_LEFT:
1316 ins_s_left();
1317 break;
1318
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001319 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001320 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1321 ins_s_right();
1322 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001323 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 break;
1325
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001326 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 case K_C_RIGHT:
1328 ins_s_right();
1329 break;
1330
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001331 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001332#ifdef FEAT_INS_EXPAND
1333 if (pum_visible())
1334 goto docomplete;
1335#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001336 if (mod_mask & MOD_MASK_SHIFT)
1337 ins_pageup();
1338 else
1339 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 break;
1341
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001342 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 case K_PAGEUP:
1344 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001345#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001346 if (pum_visible())
1347 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 ins_pageup();
1350 break;
1351
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001352 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001353#ifdef FEAT_INS_EXPAND
1354 if (pum_visible())
1355 goto docomplete;
1356#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001357 if (mod_mask & MOD_MASK_SHIFT)
1358 ins_pagedown();
1359 else
1360 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 break;
1362
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001363 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 case K_PAGEDOWN:
1365 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001366#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001367 if (pum_visible())
1368 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 ins_pagedown();
1371 break;
1372
1373#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001374 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 ins_drop();
1376 break;
1377#endif
1378
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001379 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 c = TAB;
1381 /* FALLTHROUGH */
1382
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001383 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1385 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1386 goto docomplete;
1387#endif
1388 inserted_space = FALSE;
1389 if (ins_tab())
1390 goto normalchar; /* insert TAB as a normal char */
1391 auto_format(FALSE, TRUE);
1392 break;
1393
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001394 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 c = CAR;
1396 /* FALLTHROUGH */
1397 case CAR:
1398 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001399#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 /* In a quickfix window a <CR> jumps to the error under the
1401 * cursor. */
1402 if (bt_quickfix(curbuf) && c == CAR)
1403 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001404 if (curwin->w_llist_ref == NULL) /* quickfix window */
1405 do_cmdline_cmd((char_u *)".cc");
1406 else /* location list window */
1407 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 break;
1409 }
1410#endif
1411#ifdef FEAT_CMDWIN
1412 if (cmdwin_type != 0)
1413 {
1414 /* Execute the command in the cmdline window. */
1415 cmdwin_result = CAR;
1416 goto doESCkey;
1417 }
1418#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001419#ifdef FEAT_JOB_CHANNEL
1420 if (bt_prompt(curbuf))
1421 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001422 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001423 if (!bt_prompt(curbuf))
1424 // buffer changed to a non-prompt buffer, get out of
1425 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001426 goto doESCkey;
1427 break;
1428 }
1429#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001430 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 goto doESCkey; /* out of memory */
1432 auto_format(FALSE, FALSE);
1433 inserted_space = FALSE;
1434 break;
1435
Bram Moolenaar860cae12010-06-05 23:22:07 +02001436#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001437 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438# ifdef FEAT_INS_EXPAND
1439 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1440 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001441 if (has_compl_option(TRUE))
1442 goto docomplete;
1443 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445# endif
1446# ifdef FEAT_DIGRAPHS
1447 c = ins_digraph();
1448 if (c == NUL)
1449 break;
1450# endif
1451 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453
1454#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001455 case Ctrl_X: /* Enter CTRL-X mode */
1456 ins_ctrl_x();
1457 break;
1458
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001459 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 if (ctrl_x_mode != CTRL_X_TAGS)
1461 goto normalchar;
1462 goto docomplete;
1463
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001464 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 if (ctrl_x_mode != CTRL_X_FILES)
1466 goto normalchar;
1467 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001468
1469 case 's': /* Spelling completion after ^X */
1470 case Ctrl_S:
1471 if (ctrl_x_mode != CTRL_X_SPELL)
1472 goto normalchar;
1473 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474#endif
1475
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001476 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477#ifdef FEAT_INS_EXPAND
1478 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1479#endif
1480 {
1481 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1482 if (p_im)
1483 {
1484 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1485 break;
1486 goto doESCkey;
1487 }
1488 goto normalchar;
1489 }
1490#ifdef FEAT_INS_EXPAND
1491 /* FALLTHROUGH */
1492
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001493 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 case Ctrl_N:
1495 /* if 'complete' is empty then plain ^P is no longer special,
1496 * but it is under other ^X modes */
1497 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001498 && (ctrl_x_mode == CTRL_X_NORMAL
1499 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001500 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 goto normalchar;
1502
1503docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001504 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001505#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001506 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001507#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001508 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001509 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001510#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001511 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001512#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001513 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 break;
1515#endif /* FEAT_INS_EXPAND */
1516
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001517 case Ctrl_Y: /* copy from previous line or scroll down */
1518 case Ctrl_E: /* copy from next line or scroll up */
1519 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 break;
1521
1522 default:
1523#ifdef UNIX
1524 if (c == intr_char) /* special interrupt char */
1525 goto do_intr;
1526#endif
1527
Bram Moolenaare659c952011-05-19 17:25:41 +02001528normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001530 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001532#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001533 if (!p_paste)
1534 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001535 /* Trigger InsertCharPre. */
1536 char_u *str = do_insert_char_pre(c);
1537 char_u *p;
1538
1539 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001540 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001541 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001542 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001543 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001544 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001545 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001546 c = PTR2CHAR(p);
1547 if (c == CAR || c == K_KENTER || c == NL)
1548 ins_eol(c);
1549 else
1550 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001551 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001552 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001553 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001554 vim_free(str);
1555 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001556 }
1557
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001558 /* If the new value is already inserted or an empty string
1559 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001560 if (c == NUL)
1561 break;
1562 }
1563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564#ifdef FEAT_SMARTINDENT
1565 /* Try to perform smart-indenting. */
1566 ins_try_si(c);
1567#endif
1568
1569 if (c == ' ')
1570 {
1571 inserted_space = TRUE;
1572#ifdef FEAT_CINDENT
1573 if (inindent(0))
1574 can_cindent = FALSE;
1575#endif
1576 if (Insstart_blank_vcol == MAXCOL
1577 && curwin->w_cursor.lnum == Insstart.lnum)
1578 Insstart_blank_vcol = get_nolist_virtcol();
1579 }
1580
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001581 /* Insert a normal character and check for abbreviations on a
1582 * special character. Let CTRL-] expand abbreviations without
1583 * inserting it. */
1584 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001585 // Add ABBR_OFF for characters above 0x100, this is
1586 // what check_abbr() expects.
1587 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1588 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
1590 insert_special(c, FALSE, FALSE);
1591#ifdef FEAT_RIGHTLEFT
1592 revins_legal++;
1593 revins_chars++;
1594#endif
1595 }
1596
1597 auto_format(FALSE, TRUE);
1598
1599#ifdef FEAT_FOLDING
1600 /* When inserting a character the cursor line must never be in a
1601 * closed fold. */
1602 foldOpenCursor();
1603#endif
1604 break;
1605 } /* end of switch (c) */
1606
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001607 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001608 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001609#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001610 /* but not in CTRL-X mode, a script can't restore the state */
1611 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001612#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001613 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001614 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 /* If the cursor was moved we didn't just insert a space */
1617 if (arrow_used)
1618 inserted_space = FALSE;
1619
1620#ifdef FEAT_CINDENT
1621 if (can_cindent && cindent_on()
1622# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001623 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624# endif
1625 )
1626 {
1627force_cindent:
1628 /*
1629 * Indent now if a key was typed that is in 'cinkeys'.
1630 */
1631 if (in_cinkeys(c, ' ', line_is_white))
1632 {
1633 if (stop_arrow() == OK)
1634 /* re-indent the current line */
1635 do_c_expr_indent();
1636 }
1637 }
1638#endif /* FEAT_CINDENT */
1639
1640 } /* for (;;) */
1641 /* NOTREACHED */
1642}
1643
1644/*
1645 * Redraw for Insert mode.
1646 * This is postponed until getting the next character to make '$' in the 'cpo'
1647 * option work correctly.
1648 * Only redraw when there are no characters available. This speeds up
1649 * inserting sequences of characters (e.g., for CTRL-R).
1650 */
1651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001652ins_redraw(
1653 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001655#ifdef FEAT_CONCEAL
1656 linenr_T conceal_old_cursor_line = 0;
1657 linenr_T conceal_new_cursor_line = 0;
1658 int conceal_update_lines = FALSE;
1659#endif
1660
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001661 if (char_avail())
1662 return;
1663
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001664#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1666 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001667 if (ready && (has_cursormovedI()
1668# if defined(FEAT_CONCEAL)
1669 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001670# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001671 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001672 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001673# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001675# endif
1676 )
1677 {
1678# ifdef FEAT_SYN_HL
1679 /* Need to update the screen first, to make sure syntax
1680 * highlighting is correct after making a change (e.g., inserting
1681 * a "(". The autocommand may also require a redraw, so it's done
1682 * again below, unfortunately. */
1683 if (syntax_present(curwin) && must_redraw)
1684 update_screen(0);
1685# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001686 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001687 {
1688 /* Make sure curswant is correct, an autocommand may call
1689 * getcurpos(). */
1690 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001691 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001692 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001693# ifdef FEAT_CONCEAL
1694 if (curwin->w_p_cole > 0)
1695 {
1696 conceal_old_cursor_line = last_cursormoved.lnum;
1697 conceal_new_cursor_line = curwin->w_cursor.lnum;
1698 conceal_update_lines = TRUE;
1699 }
1700# endif
1701 last_cursormoved = curwin->w_cursor;
1702 }
1703#endif
1704
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001705 /* Trigger TextChangedI if b_changedtick differs. */
1706 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001707 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001708#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001709 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001710#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001711 )
1712 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001713 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001714 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001715
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001716 // save and restore curwin and curbuf, in case the autocmd changes them
1717 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001718 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001719 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001720 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001721 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1722 u_save(curwin->w_cursor.lnum,
1723 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001725
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001726#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001727 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1728 * TextChangedI will need to trigger for backwards compatibility, thus use
1729 * different b_last_changedtick* variables. */
1730 if (ready && has_textchangedP()
1731 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1732 && pum_visible())
1733 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001734 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001735 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001736
1737 // save and restore curwin and curbuf, in case the autocmd changes them
1738 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001739 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001740 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001741 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001742 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1743 u_save(curwin->w_cursor.lnum,
1744 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001745 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001746#endif
1747
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001748#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001749 if ((conceal_update_lines
1750 && (conceal_old_cursor_line != conceal_new_cursor_line
1751 || conceal_cursor_line(curwin)))
1752 || need_cursor_line_redraw)
1753 {
1754 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001755 redrawWinline(curwin, conceal_old_cursor_line);
1756 redrawWinline(curwin, conceal_new_cursor_line == 0
1757 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001758 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001759 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001760 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001761#endif
1762 if (must_redraw)
1763 update_screen(0);
1764 else if (clear_cmdline || redraw_cmdline)
1765 showmode(); /* clear cmdline and show mode */
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001766 showruler(FALSE);
1767 setcursor();
1768 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769}
1770
1771/*
1772 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1773 */
1774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001778 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001781 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001786 did_putchar = TRUE;
1787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1789
1790#ifdef FEAT_CMDL_INFO
1791 add_to_showcmd_c(Ctrl_V);
1792#endif
1793
1794 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001795 if (did_putchar)
1796 /* when the line fits in 'columns' the '^' is at the start of the next
1797 * line and will not removed by the redraw */
1798 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799#ifdef FEAT_CMDL_INFO
1800 clear_showcmd();
1801#endif
1802 insert_special(c, FALSE, TRUE);
1803#ifdef FEAT_RIGHTLEFT
1804 revins_chars++;
1805 revins_legal++;
1806#endif
1807}
1808
1809/*
1810 * Put a character directly onto the screen. It's not stored in a buffer.
1811 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1812 */
1813static int pc_status;
1814#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1815#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1816#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1817#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819static int pc_attr;
1820static int pc_row;
1821static int pc_col;
1822
1823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001824edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 int attr;
1827
1828 if (ScreenLines != NULL)
1829 {
1830 update_topline(); /* just in case w_topline isn't valid */
1831 validate_cursor();
1832 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001833 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 else
1835 attr = 0;
1836 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001837 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839#ifdef FEAT_RIGHTLEFT
1840 if (curwin->w_p_rl)
1841 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001842 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (has_mbyte)
1844 {
1845 int fix_col = mb_fix_col(pc_col, pc_row);
1846
1847 if (fix_col != pc_col)
1848 {
1849 screen_putchar(' ', pc_row, fix_col, attr);
1850 --curwin->w_wcol;
1851 pc_status = PC_STATUS_RIGHT;
1852 }
1853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
1855 else
1856#endif
1857 {
1858 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (mb_lefthalve(pc_row, pc_col))
1860 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862
1863 /* save the character to be able to put it back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
1866 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1867 pc_status = PC_STATUS_SET;
1868 }
1869 screen_putchar(c, pc_row, pc_col, attr);
1870 }
1871}
1872
Bram Moolenaarf2732452018-06-03 14:47:35 +02001873#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1874/*
1875 * Return the effective prompt for the current buffer.
1876 */
1877 char_u *
1878prompt_text(void)
1879{
1880 if (curbuf->b_prompt_text == NULL)
1881 return (char_u *)"% ";
1882 return curbuf->b_prompt_text;
1883}
1884
1885/*
1886 * Prepare for prompt mode: Make sure the last line has the prompt text.
1887 * Move the cursor to this line.
1888 */
1889 static void
1890init_prompt(int cmdchar_todo)
1891{
1892 char_u *prompt = prompt_text();
1893 char_u *text;
1894
1895 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1896 text = ml_get_curline();
1897 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1898 {
1899 // prompt is missing, insert it or append a line with it
1900 if (*text == NUL)
1901 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1902 else
1903 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1904 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1905 coladvance((colnr_T)MAXCOL);
1906 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1907 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001908
1909 // Insert always starts after the prompt, allow editing text after it.
1910 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1911 || Insstart_orig.col != (int)STRLEN(prompt))
1912 {
1913 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001914 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001915 Insstart_orig = Insstart;
1916 Insstart_textlen = Insstart.col;
1917 Insstart_blank_vcol = MAXCOL;
1918 arrow_used = FALSE;
1919 }
1920
Bram Moolenaarf2732452018-06-03 14:47:35 +02001921 if (cmdchar_todo == 'A')
1922 coladvance((colnr_T)MAXCOL);
1923 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001924 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001925 /* Make sure the cursor is in a valid position. */
1926 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001927}
1928
1929/*
1930 * Return TRUE if the cursor is in the editable position of the prompt line.
1931 */
1932 int
1933prompt_curpos_editable()
1934{
1935 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1936 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1937}
1938#endif
1939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940/*
1941 * Undo the previous edit_putchar().
1942 */
1943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001944edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945{
1946 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (pc_status == PC_STATUS_RIGHT)
1949 ++curwin->w_wcol;
1950 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001951 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1954 }
1955}
1956
1957/*
1958 * Called when p_dollar is set: display a '$' at the end of the changed text
1959 * Only works when cursor is in the line that changes.
1960 */
1961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001962display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
1964 colnr_T save_col;
1965
1966 if (!redrawing())
1967 return;
1968
1969 cursor_off();
1970 save_col = curwin->w_cursor.col;
1971 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (has_mbyte)
1973 {
1974 char_u *p;
1975
1976 /* If on the last byte of a multi-byte move to the first byte. */
1977 p = ml_get_curline();
1978 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001981 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
1983 edit_putchar('$', FALSE);
1984 dollar_vcol = curwin->w_virtcol;
1985 }
1986 curwin->w_cursor.col = save_col;
1987}
1988
1989/*
1990 * Call this function before moving the cursor from the normal insert position
1991 * in insert mode.
1992 */
1993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001994undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001996 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001998 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001999 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 }
2001}
2002
2003/*
2004 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
2005 * Keep the cursor on the same character.
2006 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2007 * type == INDENT_DEC decrease indent (for CTRL-D)
2008 * type == INDENT_SET set indent to "amount"
2009 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2010 */
2011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002012change_indent(
2013 int type,
2014 int amount,
2015 int round,
2016 int replaced, /* replaced character, put on replace stack */
2017 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018{
2019 int vcol;
2020 int last_vcol;
2021 int insstart_less; /* reduction for Insstart.col */
2022 int new_cursor_col;
2023 int i;
2024 char_u *ptr;
2025 int save_p_list;
2026 int start_col;
2027 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 colnr_T orig_col = 0; /* init for GCC */
2029 char_u *new_line, *orig_line = NULL; /* init for GCC */
2030
2031 /* VREPLACE mode needs to know what the line was like before changing */
2032 if (State & VREPLACE_FLAG)
2033 {
2034 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2035 orig_col = curwin->w_cursor.col;
2036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038 /* for the following tricks we don't want list mode */
2039 save_p_list = curwin->w_p_list;
2040 curwin->w_p_list = FALSE;
2041 vc = getvcol_nolist(&curwin->w_cursor);
2042 vcol = vc;
2043
2044 /*
2045 * For Replace mode we need to fix the replace stack later, which is only
2046 * possible when the cursor is in the indent. Remember the number of
2047 * characters before the cursor if it's possible.
2048 */
2049 start_col = curwin->w_cursor.col;
2050
2051 /* determine offset from first non-blank */
2052 new_cursor_col = curwin->w_cursor.col;
2053 beginline(BL_WHITE);
2054 new_cursor_col -= curwin->w_cursor.col;
2055
2056 insstart_less = curwin->w_cursor.col;
2057
2058 /*
2059 * If the cursor is in the indent, compute how many screen columns the
2060 * cursor is to the left of the first non-blank.
2061 */
2062 if (new_cursor_col < 0)
2063 vcol = get_indent() - vcol;
2064
2065 if (new_cursor_col > 0) /* can't fix replace stack */
2066 start_col = -1;
2067
2068 /*
2069 * Set the new indent. The cursor will be put on the first non-blank.
2070 */
2071 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002072 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 else
2074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 int save_State = State;
2076
2077 /* Avoid being called recursively. */
2078 if (State & VREPLACE_FLAG)
2079 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002080 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083 insstart_less -= curwin->w_cursor.col;
2084
2085 /*
2086 * Try to put cursor on same character.
2087 * If the cursor is at or after the first non-blank in the line,
2088 * compute the cursor column relative to the column of the first
2089 * non-blank character.
2090 * If we are not in insert mode, leave the cursor on the first non-blank.
2091 * If the cursor is before the first non-blank, position it relative
2092 * to the first non-blank, counted in screen columns.
2093 */
2094 if (new_cursor_col >= 0)
2095 {
2096 /*
2097 * When changing the indent while the cursor is touching it, reset
2098 * Insstart_col to 0.
2099 */
2100 if (new_cursor_col == 0)
2101 insstart_less = MAXCOL;
2102 new_cursor_col += curwin->w_cursor.col;
2103 }
2104 else if (!(State & INSERT))
2105 new_cursor_col = curwin->w_cursor.col;
2106 else
2107 {
2108 /*
2109 * Compute the screen column where the cursor should be.
2110 */
2111 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002112 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
2114 /*
2115 * Advance the cursor until we reach the right screen column.
2116 */
2117 vcol = last_vcol = 0;
2118 new_cursor_col = -1;
2119 ptr = ml_get_curline();
2120 while (vcol <= (int)curwin->w_virtcol)
2121 {
2122 last_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002124 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002127 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 }
2129 vcol = last_vcol;
2130
2131 /*
2132 * May need to insert spaces to be able to position the cursor on
2133 * the right screen column.
2134 */
2135 if (vcol != (int)curwin->w_virtcol)
2136 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002137 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002139 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 if (ptr != NULL)
2141 {
2142 new_cursor_col += i;
2143 ptr[i] = NUL;
2144 while (--i >= 0)
2145 ptr[i] = ' ';
2146 ins_str(ptr);
2147 vim_free(ptr);
2148 }
2149 }
2150
2151 /*
2152 * When changing the indent while the cursor is in it, reset
2153 * Insstart_col to 0.
2154 */
2155 insstart_less = MAXCOL;
2156 }
2157
2158 curwin->w_p_list = save_p_list;
2159
2160 if (new_cursor_col <= 0)
2161 curwin->w_cursor.col = 0;
2162 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002163 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 curwin->w_set_curswant = TRUE;
2165 changed_cline_bef_curs();
2166
2167 /*
2168 * May have to adjust the start of the insert.
2169 */
2170 if (State & INSERT)
2171 {
2172 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2173 {
2174 if ((int)Insstart.col <= insstart_less)
2175 Insstart.col = 0;
2176 else
2177 Insstart.col -= insstart_less;
2178 }
2179 if ((int)ai_col <= insstart_less)
2180 ai_col = 0;
2181 else
2182 ai_col -= insstart_less;
2183 }
2184
2185 /*
2186 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2187 * If the number of characters before the cursor decreased, need to pop a
2188 * few characters from the replace stack.
2189 * If the number of characters before the cursor increased, need to push a
2190 * few NULs onto the replace stack.
2191 */
2192 if (REPLACE_NORMAL(State) && start_col >= 0)
2193 {
2194 while (start_col > (int)curwin->w_cursor.col)
2195 {
2196 replace_join(0); /* remove a NUL from the replace stack */
2197 --start_col;
2198 }
2199 while (start_col < (int)curwin->w_cursor.col || replaced)
2200 {
2201 replace_push(NUL);
2202 if (replaced)
2203 {
2204 replace_push(replaced);
2205 replaced = NUL;
2206 }
2207 ++start_col;
2208 }
2209 }
2210
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 /*
2212 * For VREPLACE mode, we also have to fix the replace stack. In this case
2213 * it is always possible because we backspace over the whole line and then
2214 * put it back again the way we wanted it.
2215 */
2216 if (State & VREPLACE_FLAG)
2217 {
2218 /* If orig_line didn't allocate, just return. At least we did the job,
2219 * even if you can't backspace. */
2220 if (orig_line == NULL)
2221 return;
2222
2223 /* Save new line */
2224 new_line = vim_strsave(ml_get_curline());
2225 if (new_line == NULL)
2226 return;
2227
2228 /* We only put back the new line up to the cursor */
2229 new_line[curwin->w_cursor.col] = NUL;
2230
2231 /* Put back original line */
2232 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2233 curwin->w_cursor.col = orig_col;
2234
2235 /* Backspace from cursor to start of line */
2236 backspace_until_column(0);
2237
2238 /* Insert new stuff into line again */
2239 ins_bytes(new_line);
2240
2241 vim_free(new_line);
2242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243}
2244
2245/*
2246 * Truncate the space at the end of a line. This is to be used only in an
2247 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2248 * modes.
2249 */
2250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002251truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252{
2253 int i;
2254
2255 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002256 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
2258 if (State & REPLACE_FLAG)
2259 replace_join(0); /* remove a NUL from the replace stack */
2260 }
2261 line[i + 1] = NUL;
2262}
2263
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264/*
2265 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2266 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002267 * Will attempt not to go before "col" even when there is a composing
2268 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 */
2270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002271backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
2273 while ((int)curwin->w_cursor.col > col)
2274 {
2275 curwin->w_cursor.col--;
2276 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002277 replace_do_bs(col);
2278 else if (!del_char_after_col(col))
2279 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002283/*
2284 * Like del_char(), but make sure not to go before column "limit_col".
2285 * Only matters when there are composing characters.
2286 * Return TRUE when something was deleted.
2287 */
2288 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002289del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002290{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002291 if (enc_utf8 && limit_col >= 0)
2292 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002293 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002294
2295 /* Make sure the cursor is at the start of a character, but
2296 * skip forward again when going too far back because of a
2297 * composing character. */
2298 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002299 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002300 {
2301 int l = utf_ptr2len(ml_get_cursor());
2302
2303 if (l == 0) /* end of line */
2304 break;
2305 curwin->w_cursor.col += l;
2306 }
2307 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2308 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002309 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002310 }
2311 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002312 (void)del_char(FALSE);
2313 return TRUE;
2314}
2315
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2317/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002318 * CTRL-X pressed in Insert mode.
2319 */
2320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002321ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002322{
2323 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2324 * CTRL-V works like CTRL-N */
2325 if (ctrl_x_mode != CTRL_X_CMDLINE)
2326 {
2327 /* if the next ^X<> won't ADD nothing, then reset
2328 * compl_cont_status */
2329 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002330 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002331 else
2332 compl_cont_status = 0;
2333 /* We're not sure which CTRL-X mode it will be yet */
2334 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2335 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2336 edit_submode_pre = NULL;
2337 showmode();
2338 }
2339}
2340
2341/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002342 * Whether other than default completion has been selected.
2343 */
2344 int
2345ctrl_x_mode_not_default(void)
2346{
2347 return ctrl_x_mode != CTRL_X_NORMAL;
2348}
2349
2350/*
2351 * Whether CTRL-X was typed without a following character.
2352 */
2353 int
2354ctrl_x_mode_not_defined_yet(void)
2355{
2356 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2357}
2358
2359/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002360 * Return TRUE if the 'dict' or 'tsr' option can be used.
2361 */
2362 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002363has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002364{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002365 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002366# ifdef FEAT_SPELL
2367 && !curwin->w_p_spell
2368# endif
2369 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002370 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2371 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002372 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002373 edit_submode = NULL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002374 msg_attr(dict_opt ? _("'dictionary' option is empty")
2375 : _("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002376 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002377 if (emsg_silent == 0)
2378 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002379 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002380 setcursor();
2381 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002382#ifdef FEAT_EVAL
2383 if (!get_vim_var_nr(VV_TESTING))
2384#endif
2385 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002386 }
2387 return FALSE;
2388 }
2389 return TRUE;
2390}
2391
2392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2394 * This depends on the current mode.
2395 */
2396 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002397vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01002399 // Always allow ^R - let its results then be checked
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (c == Ctrl_R)
2401 return TRUE;
2402
Bram Moolenaare3226be2005-12-18 22:10:00 +00002403 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002404 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002405 return TRUE;
2406
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 switch (ctrl_x_mode)
2408 {
2409 case 0: /* Not in any CTRL-X mode */
2410 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2411 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002412 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2414 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2415 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002416 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002417 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 case CTRL_X_SCROLL:
2419 return (c == Ctrl_Y || c == Ctrl_E);
2420 case CTRL_X_WHOLE_LINE:
2421 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2422 case CTRL_X_FILES:
2423 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2424 case CTRL_X_DICTIONARY:
2425 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2426 case CTRL_X_THESAURUS:
2427 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2428 case CTRL_X_TAGS:
2429 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2430#ifdef FEAT_FIND_ID
2431 case CTRL_X_PATH_PATTERNS:
2432 return (c == Ctrl_P || c == Ctrl_N);
2433 case CTRL_X_PATH_DEFINES:
2434 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2435#endif
2436 case CTRL_X_CMDLINE:
2437 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2438 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002439#ifdef FEAT_COMPL_FUNC
2440 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002441 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002442 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002443 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002444#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002445 case CTRL_X_SPELL:
2446 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002447 case CTRL_X_EVAL:
2448 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002450 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 return FALSE;
2452}
2453
2454/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002455 * Return TRUE when character "c" is part of the item currently being
2456 * completed. Used to decide whether to abandon complete mode when the menu
2457 * is visible.
2458 */
2459 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002460ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002461{
2462 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2463 /* When expanding an identifier only accept identifier chars. */
2464 return vim_isIDc(c);
2465
2466 switch (ctrl_x_mode)
2467 {
2468 case CTRL_X_FILES:
2469 /* When expanding file name only accept file name chars. But not
2470 * path separators, so that "proto/<Tab>" expands files in
2471 * "proto", not "proto/" as a whole */
2472 return vim_isfilec(c) && !vim_ispathsep(c);
2473
2474 case CTRL_X_CMDLINE:
2475 case CTRL_X_OMNI:
2476 /* Command line and Omni completion can work with just about any
2477 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002478 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002479
2480 case CTRL_X_WHOLE_LINE:
2481 /* For while line completion a space can be part of the line. */
2482 return vim_isprintc(c);
2483 }
2484 return vim_iswordc(c);
2485}
2486
2487/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002488 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002490 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 * the rest of the word to be in -- webb
2492 */
2493 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002494ins_compl_add_infercase(
2495 char_u *str,
2496 int len,
2497 int icase,
2498 char_u *fname,
2499 int dir,
2500 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002502 char_u *p;
2503 int i, c;
2504 int actual_len; /* Take multi-byte characters */
2505 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002506 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002507 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 int has_lower = FALSE;
2509 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002511 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002513 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
Bram Moolenaara2993e12007-08-12 14:38:46 +00002515 /* Find actual length of completion. */
Bram Moolenaara2993e12007-08-12 14:38:46 +00002516 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002518 p = str;
2519 actual_len = 0;
2520 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002522 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002523 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
2525 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002526 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002527 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaara2993e12007-08-12 14:38:46 +00002529 /* Find actual length of original text. */
Bram Moolenaara2993e12007-08-12 14:38:46 +00002530 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002532 p = compl_orig_text;
2533 actual_compl_length = 0;
2534 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002536 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002537 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002540 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002541 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002543 /* "actual_len" may be smaller than "actual_compl_length" when using
2544 * thesaurus, only use the minimum when comparing. */
2545 min_len = actual_len < actual_compl_length
2546 ? actual_len : actual_compl_length;
2547
Bram Moolenaara2993e12007-08-12 14:38:46 +00002548 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002549 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002550 if (wca != NULL)
2551 {
2552 p = str;
2553 for (i = 0; i < actual_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002554 if (has_mbyte)
2555 wca[i] = mb_ptr2char_adv(&p);
2556 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002557 wca[i] = *(p++);
2558
2559 /* Rule 1: Were any chars converted to lower? */
2560 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002561 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002562 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002563 if (has_mbyte)
2564 c = mb_ptr2char_adv(&p);
2565 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002566 c = *(p++);
2567 if (MB_ISLOWER(c))
2568 {
2569 has_lower = TRUE;
2570 if (MB_ISUPPER(wca[i]))
2571 {
2572 /* Rule 1 is satisfied. */
2573 for (i = actual_compl_length; i < actual_len; ++i)
2574 wca[i] = MB_TOLOWER(wca[i]);
2575 break;
2576 }
2577 }
2578 }
2579
2580 /*
2581 * Rule 2: No lower case, 2nd consecutive letter converted to
2582 * upper case.
2583 */
2584 if (!has_lower)
2585 {
2586 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002587 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002588 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002589 if (has_mbyte)
2590 c = mb_ptr2char_adv(&p);
2591 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002592 c = *(p++);
2593 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2594 {
2595 /* Rule 2 is satisfied. */
2596 for (i = actual_compl_length; i < actual_len; ++i)
2597 wca[i] = MB_TOUPPER(wca[i]);
2598 break;
2599 }
2600 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2601 }
2602 }
2603
2604 /* Copy the original case of the part we typed. */
2605 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002606 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002607 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002608 if (has_mbyte)
2609 c = mb_ptr2char_adv(&p);
2610 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002611 c = *(p++);
2612 if (MB_ISLOWER(c))
2613 wca[i] = MB_TOLOWER(wca[i]);
2614 else if (MB_ISUPPER(c))
2615 wca[i] = MB_TOUPPER(wca[i]);
2616 }
2617
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002618 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002619 * Generate encoding specific output from wide character array.
2620 * Multi-byte characters can occupy up to five bytes more than
2621 * ASCII characters, and we also need one byte for NUL, so stay
2622 * six bytes away from the edge of IObuff.
2623 */
2624 p = IObuff;
2625 i = 0;
2626 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002627 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002628 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002629 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002630 *(p++) = wca[i++];
2631 *p = NUL;
2632
2633 vim_free(wca);
2634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002636 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2637 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002639 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640}
2641
2642/*
2643 * Add a match to the list of matches.
2644 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002645 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002646 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002648 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002649ins_compl_add(
2650 char_u *str,
2651 int len,
2652 int icase,
2653 char_u *fname,
2654 char_u **cptext, /* extra text for popup menu or NULL */
2655 int cdir,
2656 int flags,
2657 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002659 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002660 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
2662 ui_breakcheck();
2663 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002664 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (len < 0)
2666 len = (int)STRLEN(str);
2667
2668 /*
2669 * If the same match is already present, don't add it.
2670 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002671 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002673 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 do
2675 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002676 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002677 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002678 && match->cp_str[len] == NUL)
2679 return NOTDONE;
2680 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002681 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 }
2683
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002684 /* Remove any popup menu before changing the list of matches. */
2685 ins_compl_del_pum();
2686
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 /*
2688 * Allocate a new match structure.
2689 * Copy the values to the new match structure.
2690 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002691 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002693 return FAIL;
2694 match->cp_number = -1;
2695 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002696 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002697 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
2699 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002700 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002702 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002705 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2707 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002708 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002709 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002710 && compl_curr_match->cp_fname != NULL
2711 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002712 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002713 else if (fname != NULL)
2714 {
2715 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002716 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002719 match->cp_fname = NULL;
2720 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002721
2722 if (cptext != NULL)
2723 {
2724 int i;
2725
2726 for (i = 0; i < CPT_COUNT; ++i)
2727 if (cptext[i] != NULL && *cptext[i] != NUL)
2728 match->cp_text[i] = vim_strsave(cptext[i]);
2729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731 /*
2732 * Link the new match structure in the list of matches.
2733 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002734 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002735 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 else if (dir == FORWARD)
2737 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002738 match->cp_next = compl_curr_match->cp_next;
2739 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
2741 else /* BACKWARD */
2742 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002743 match->cp_next = compl_curr_match;
2744 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002746 if (match->cp_next)
2747 match->cp_next->cp_prev = match;
2748 if (match->cp_prev)
2749 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002751 compl_first_match = match;
2752 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002754 /*
2755 * Find the longest common string if still doing that.
2756 */
2757 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2758 ins_compl_longest_match(match);
2759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 return OK;
2761}
2762
2763/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002764 * Return TRUE if "str[len]" matches with match->cp_str, considering
2765 * match->cp_icase.
2766 */
2767 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002768ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002769{
2770 if (match->cp_icase)
2771 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2772 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2773}
2774
2775/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002776 * Reduce the longest common string for match "match".
2777 */
2778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002779ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002780{
2781 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002782 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002783 int had_match;
2784
2785 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002786 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002787 /* First match, use it as a whole. */
2788 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002789 if (compl_leader != NULL)
2790 {
2791 had_match = (curwin->w_cursor.col > compl_col);
2792 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002793 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002794 ins_redraw(FALSE);
2795
2796 /* When the match isn't there (to avoid matching itself) remove it
2797 * again after redrawing. */
2798 if (!had_match)
2799 ins_compl_delete();
2800 compl_used_match = FALSE;
2801 }
2802 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002803 else
2804 {
2805 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002806 p = compl_leader;
2807 s = match->cp_str;
2808 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002809 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002810 if (has_mbyte)
2811 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002812 c1 = mb_ptr2char(p);
2813 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002814 }
2815 else
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002816 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002817 c1 = *p;
2818 c2 = *s;
2819 }
2820 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2821 : (c1 != c2))
2822 break;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002823 if (has_mbyte)
2824 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002825 MB_PTR_ADV(p);
2826 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002827 }
2828 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002829 {
2830 ++p;
2831 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002832 }
2833 }
2834
2835 if (*p != NUL)
2836 {
2837 /* Leader was shortened, need to change the inserted text. */
2838 *p = NUL;
2839 had_match = (curwin->w_cursor.col > compl_col);
2840 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002841 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002842 ins_redraw(FALSE);
2843
2844 /* When the match isn't there (to avoid matching itself) remove it
2845 * again after redrawing. */
2846 if (!had_match)
2847 ins_compl_delete();
2848 }
2849
2850 compl_used_match = FALSE;
2851 }
2852}
2853
2854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 * Add an array of matches to the list of matches.
2856 * Frees matches[].
2857 */
2858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002859ins_compl_add_matches(
2860 int num_matches,
2861 char_u **matches,
2862 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
2864 int i;
2865 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002866 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
Bram Moolenaar572cb562005-08-05 21:35:02 +00002868 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002869 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002870 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002872 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 FreeWild(num_matches, matches);
2874}
2875
2876/* Make the completion list cyclic.
2877 * Return the number of matches (excluding the original).
2878 */
2879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002880ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002882 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 int count = 0;
2884
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002885 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
2887 /*
2888 * Find the end of the list.
2889 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002890 match = compl_first_match;
2891 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002892 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002894 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 ++count;
2896 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002897 match->cp_next = compl_first_match;
2898 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
2900 return count;
2901}
2902
Bram Moolenaara94bc432006-03-10 21:42:59 +00002903/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002904 * Set variables that store noselect and noinsert behavior from the
2905 * 'completeopt' value.
2906 */
2907 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002908completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002909{
2910 compl_no_insert = FALSE;
2911 compl_no_select = FALSE;
2912 if (strstr((char *)p_cot, "noselect") != NULL)
2913 compl_no_select = TRUE;
2914 if (strstr((char *)p_cot, "noinsert") != NULL)
2915 compl_no_insert = TRUE;
2916}
2917
2918/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002919 * Start completion for the complete() function.
2920 * "startcol" is where the matched text starts (1 is first column).
2921 * "list" is the list of matches.
2922 */
2923 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002924set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002925{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002926 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002927 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002928
Bram Moolenaara94bc432006-03-10 21:42:59 +00002929 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002930 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002931 ins_compl_prep(' ');
2932 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002933 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002934
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002935 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002936 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002937 startcol = curwin->w_cursor.col;
2938 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002939 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002940 /* compl_pattern doesn't need to be set */
2941 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2942 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002943 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002944 return;
2945
Bram Moolenaare4214502015-03-05 18:08:43 +01002946 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002947
2948 ins_compl_add_list(list);
2949 compl_matches = ins_compl_make_cyclic();
2950 compl_started = TRUE;
2951 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002952 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002953
2954 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002955 if (compl_no_insert || compl_no_select)
2956 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002957 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002958 if (compl_no_select)
2959 /* Down/Up has no real effect. */
2960 ins_complete(K_UP, FALSE);
2961 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002962 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002963 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002964 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002965
2966 /* Lazily show the popup menu, unless we got interrupted. */
2967 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002968 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002969 out_flush();
2970}
2971
2972
Bram Moolenaar9372a112005-12-06 19:59:18 +00002973/* "compl_match_array" points the currently displayed list of entries in the
2974 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002975static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002976static int compl_match_arraysize;
2977
2978/*
2979 * Update the screen and when there is any scrolling remove the popup menu.
2980 */
2981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002982ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002983{
2984 int h;
2985
2986 if (compl_match_array != NULL)
2987 {
2988 h = curwin->w_cline_height;
Bram Moolenaarae654382019-01-17 21:09:05 +01002989 // Update the screen later, before drawing the popup menu over it.
2990 pum_call_update_screen();
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002991 if (h != curwin->w_cline_height)
2992 ins_compl_del_pum();
2993 }
2994}
2995
2996/*
2997 * Remove any popup menu.
2998 */
2999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003000ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003001{
3002 if (compl_match_array != NULL)
3003 {
3004 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003005 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003006 }
3007}
3008
3009/*
3010 * Return TRUE if the popup menu should be displayed.
3011 */
3012 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003013pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003014{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003015 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003016 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003017 return FALSE;
3018
3019 /* The display looks bad on a B&W display. */
3020 if (t_colors < 8
3021#ifdef FEAT_GUI
3022 && !gui.in_use
3023#endif
3024 )
3025 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003026 return TRUE;
3027}
3028
3029/*
3030 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003031 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003032 */
3033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003034pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003035{
3036 compl_T *compl;
3037 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003038
3039 /* Don't display the popup menu if there are no matches or there is only
3040 * one (ignoring the original text). */
3041 compl = compl_first_match;
3042 i = 0;
3043 do
3044 {
3045 if (compl == NULL
3046 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3047 break;
3048 compl = compl->cp_next;
3049 } while (compl != compl_first_match);
3050
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003051 if (strstr((char *)p_cot, "menuone") != NULL)
3052 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003053 return (i >= 2);
3054}
3055
3056/*
3057 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003058 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003059 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003061ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003062{
3063 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003064 compl_T *shown_compl = NULL;
3065 int did_find_shown_match = FALSE;
3066 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003067 int i;
3068 int cur = -1;
3069 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003070 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003071
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003072 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003073 return;
3074
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003075#if defined(FEAT_EVAL)
3076 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3077 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3078#endif
3079
Bram Moolenaarae654382019-01-17 21:09:05 +01003080 // Update the screen later, before drawing the popup menu over it.
3081 pum_call_update_screen();
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003082
3083 if (compl_match_array == NULL)
3084 {
3085 /* Need to build the popup menu list. */
3086 compl_match_arraysize = 0;
3087 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003088 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003089 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003090 do
3091 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003092 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3093 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003094 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003095 ++compl_match_arraysize;
3096 compl = compl->cp_next;
3097 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003098 if (compl_match_arraysize == 0)
3099 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003100 compl_match_array = (pumitem_T *)alloc_clear(
3101 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003102 * compl_match_arraysize));
3103 if (compl_match_array != NULL)
3104 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003105 /* If the current match is the original text don't find the first
3106 * match after it, don't highlight anything. */
3107 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3108 shown_match_ok = TRUE;
3109
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003110 i = 0;
3111 compl = compl_first_match;
3112 do
3113 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003114 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3115 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003116 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003117 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003118 if (!shown_match_ok)
3119 {
3120 if (compl == compl_shown_match || did_find_shown_match)
3121 {
3122 /* This item is the shown match or this is the
3123 * first displayed item after the shown match. */
3124 compl_shown_match = compl;
3125 did_find_shown_match = TRUE;
3126 shown_match_ok = TRUE;
3127 }
3128 else
3129 /* Remember this displayed match for when the
3130 * shown match is just below it. */
3131 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003132 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003133 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003134
3135 if (compl->cp_text[CPT_ABBR] != NULL)
3136 compl_match_array[i].pum_text =
3137 compl->cp_text[CPT_ABBR];
3138 else
3139 compl_match_array[i].pum_text = compl->cp_str;
3140 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3141 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3142 if (compl->cp_text[CPT_MENU] != NULL)
3143 compl_match_array[i++].pum_extra =
3144 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003145 else
3146 compl_match_array[i++].pum_extra = compl->cp_fname;
3147 }
3148
3149 if (compl == compl_shown_match)
3150 {
3151 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003152
3153 /* When the original text is the shown match don't set
3154 * compl_shown_match. */
3155 if (compl->cp_flags & ORIGINAL_TEXT)
3156 shown_match_ok = TRUE;
3157
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003158 if (!shown_match_ok && shown_compl != NULL)
3159 {
3160 /* The shown match isn't displayed, set it to the
3161 * previously displayed match. */
3162 compl_shown_match = shown_compl;
3163 shown_match_ok = TRUE;
3164 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003165 }
3166 compl = compl->cp_next;
3167 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003168
3169 if (!shown_match_ok) /* no displayed match at all */
3170 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003171 }
3172 }
3173 else
3174 {
3175 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003176 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003177 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3178 || compl_match_array[i].pum_text
3179 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003180 {
3181 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003182 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003183 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003184 }
3185
3186 if (compl_match_array != NULL)
3187 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003188 /* In Replace mode when a $ is displayed at the end of the line only
3189 * part of the screen would be updated. We do need to redraw here. */
3190 dollar_vcol = -1;
3191
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003192 /* Compute the screen column of the start of the completed text.
3193 * Use the cursor to get all wrapping and other settings right. */
3194 col = curwin->w_cursor.col;
3195 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003196 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003197 curwin->w_cursor.col = col;
3198 }
3199}
3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201#define DICT_FIRST (1) /* use just first element in "dict" */
3202#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003203
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003205 * Add any identifiers that match the given pattern in the list of dictionary
3206 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 */
3208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003209ins_compl_dictionaries(
3210 char_u *dict_start,
3211 char_u *pat,
3212 int flags, /* DICT_FIRST and/or DICT_EXACT */
3213 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003215 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 char_u *ptr;
3217 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 char_u **files;
3220 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003222 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
Bram Moolenaar0b238792006-03-02 22:49:12 +00003224 if (*dict == NUL)
3225 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003226#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003227 /* When 'dictionary' is empty and spell checking is enabled use
3228 * "spell". */
3229 if (!thesaurus && curwin->w_p_spell)
3230 dict = (char_u *)"spell";
3231 else
3232#endif
3233 return;
3234 }
3235
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003237 if (buf == NULL)
3238 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003239 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003240
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 /* If 'infercase' is set, don't use 'smartcase' here */
3242 save_p_scs = p_scs;
3243 if (curbuf->b_p_inf)
3244 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003245
3246 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3247 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003248 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003249 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003250 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003251 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003252 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003253
3254 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003255 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003256 len = STRLEN(pat_esc) + 10;
3257 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003258 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003259 {
3260 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003261 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003262 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003263 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003264 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3265 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003266 vim_free(ptr);
3267 }
3268 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003269 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003270 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003271 if (regmatch.regprog == NULL)
3272 goto theend;
3273 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3276 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003277 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
3279 /* copy one dictionary file name into buf */
3280 if (flags == DICT_EXACT)
3281 {
3282 count = 1;
3283 files = &dict;
3284 }
3285 else
3286 {
3287 /* Expand wildcards in the dictionary name, but do not allow
3288 * backticks (for security, the 'dict' option may have been set in
3289 * a modeline). */
3290 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003291# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003292 if (!thesaurus && STRCMP(buf, "spell") == 0)
3293 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003294 else
3295# endif
3296 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 || expand_wildcards(1, &buf, &count, &files,
3298 EW_FILE|EW_SILENT) != OK)
3299 count = 0;
3300 }
3301
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003302# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003303 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003305 /* Complete from active spelling. Skip "\<" in the pattern, we
3306 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003307 if (pat[0] == '\\' && pat[1] == '<')
3308 ptr = pat + 2;
3309 else
3310 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003311 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003313 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003314# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003315 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003316 {
3317 ins_compl_files(count, files, thesaurus, flags,
3318 &regmatch, buf, &dir);
3319 if (flags != DICT_EXACT)
3320 FreeWild(count, files);
3321 }
3322 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 break;
3324 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003325
3326theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003328 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 vim_free(buf);
3330}
3331
Bram Moolenaar0b238792006-03-02 22:49:12 +00003332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003333ins_compl_files(
3334 int count,
3335 char_u **files,
3336 int thesaurus,
3337 int flags,
3338 regmatch_T *regmatch,
3339 char_u *buf,
3340 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003341{
3342 char_u *ptr;
3343 int i;
3344 FILE *fp;
3345 int add_r;
3346
3347 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3348 {
3349 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3350 if (flags != DICT_EXACT)
3351 {
3352 vim_snprintf((char *)IObuff, IOSIZE,
3353 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003354 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003355 }
3356
3357 if (fp != NULL)
3358 {
3359 /*
3360 * Read dictionary file line by line.
3361 * Check each line for a match.
3362 */
3363 while (!got_int && !compl_interrupted
3364 && !vim_fgets(buf, LSIZE, fp))
3365 {
3366 ptr = buf;
3367 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3368 {
3369 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003370 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003371 ptr = find_line_end(ptr);
3372 else
3373 ptr = find_word_end(ptr);
3374 add_r = ins_compl_add_infercase(regmatch->startp[0],
3375 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003376 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003377 if (thesaurus)
3378 {
3379 char_u *wstart;
3380
3381 /*
3382 * Add the other matches on the line
3383 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003384 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003385 while (!got_int)
3386 {
3387 /* Find start of the next word. Skip white
3388 * space and punctuation. */
3389 ptr = find_word_start(ptr);
3390 if (*ptr == NUL || *ptr == NL)
3391 break;
3392 wstart = ptr;
3393
Bram Moolenaara2993e12007-08-12 14:38:46 +00003394 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003395 if (has_mbyte)
3396 /* Japanese words may have characters in
3397 * different classes, only separate words
3398 * with single-byte non-word characters. */
3399 while (*ptr != NUL)
3400 {
3401 int l = (*mb_ptr2len)(ptr);
3402
3403 if (l < 2 && !vim_iswordc(*ptr))
3404 break;
3405 ptr += l;
3406 }
3407 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003408 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003409
3410 /* Add the word. Skip the regexp match. */
3411 if (wstart != regmatch->startp[0])
3412 add_r = ins_compl_add_infercase(wstart,
3413 (int)(ptr - wstart),
3414 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003415 }
3416 }
3417 if (add_r == OK)
3418 /* if dir was BACKWARD then honor it just once */
3419 *dir = FORWARD;
3420 else if (add_r == FAIL)
3421 break;
3422 /* avoid expensive call to vim_regexec() when at end
3423 * of line */
3424 if (*ptr == '\n' || got_int)
3425 break;
3426 }
3427 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003428 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003429 }
3430 fclose(fp);
3431 }
3432 }
3433}
3434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435/*
3436 * Find the start of the next word.
3437 * Returns a pointer to the first char of the word. Also stops at a NUL.
3438 */
3439 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003440find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (has_mbyte)
3443 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003444 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3447 ++ptr;
3448 return ptr;
3449}
3450
3451/*
3452 * Find the end of the word. Assumes it starts inside a word.
3453 * Returns a pointer to just after the word.
3454 */
3455 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003456find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 int start_class;
3459
3460 if (has_mbyte)
3461 {
3462 start_class = mb_get_class(ptr);
3463 if (start_class > 1)
3464 while (*ptr != NUL)
3465 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003466 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (mb_get_class(ptr) != start_class)
3468 break;
3469 }
3470 }
3471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 while (vim_iswordc(*ptr))
3473 ++ptr;
3474 return ptr;
3475}
3476
3477/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003478 * Find the end of the line, omitting CR and NL at the end.
3479 * Returns a pointer to just after the line.
3480 */
3481 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003482find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003483{
3484 char_u *s;
3485
3486 s = ptr + STRLEN(ptr);
3487 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3488 --s;
3489 return s;
3490}
3491
3492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 * Free the list of completions
3494 */
3495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003496ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003498 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003499 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
Bram Moolenaard23a8232018-02-10 18:45:26 +01003501 VIM_CLEAR(compl_pattern);
3502 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003504 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003506
3507 ins_compl_del_pum();
3508 pum_clear();
3509
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003510 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 do
3512 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003513 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003514 compl_curr_match = compl_curr_match->cp_next;
3515 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003517 if (match->cp_flags & FREE_FNAME)
3518 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003519 for (i = 0; i < CPT_COUNT; ++i)
3520 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003522 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3523 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003524 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003525 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526}
3527
3528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003529ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003531 compl_cont_status = 0;
3532 compl_started = FALSE;
3533 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003534 VIM_CLEAR(compl_pattern);
3535 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003537 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003538 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003539 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003540 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541}
3542
3543/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003544 * Return TRUE when Insert completion is active.
3545 */
3546 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003547ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003548{
3549 return compl_started;
3550}
3551
Bram Moolenaarfd133322019-03-29 12:20:27 +01003552
3553/*
3554 * Get complete information
3555 */
3556 void
3557get_complete_info(list_T *what_list, dict_T *retdict)
3558{
3559 int ret = OK;
3560 listitem_T *item;
3561#define CI_WHAT_MODE 0x01
3562#define CI_WHAT_PUM_VISIBLE 0x02
3563#define CI_WHAT_ITEMS 0x04
3564#define CI_WHAT_SELECTED 0x08
3565#define CI_WHAT_INSERTED 0x10
3566#define CI_WHAT_ALL 0xff
3567 int what_flag;
3568
3569 if (what_list == NULL)
3570 what_flag = CI_WHAT_ALL;
3571 else
3572 {
3573 what_flag = 0;
3574 for (item = what_list->lv_first; item != NULL; item = item->li_next)
3575 {
3576 char_u *what = tv_get_string(&item->li_tv);
3577
3578 if (STRCMP(what, "mode") == 0)
3579 what_flag |= CI_WHAT_MODE;
3580 else if (STRCMP(what, "pum_visible") == 0)
3581 what_flag |= CI_WHAT_PUM_VISIBLE;
3582 else if (STRCMP(what, "items") == 0)
3583 what_flag |= CI_WHAT_ITEMS;
3584 else if (STRCMP(what, "selected") == 0)
3585 what_flag |= CI_WHAT_SELECTED;
3586 else if (STRCMP(what, "inserted") == 0)
3587 what_flag |= CI_WHAT_INSERTED;
3588 }
3589 }
3590
3591 if (ret == OK && (what_flag & CI_WHAT_MODE))
3592 ret = dict_add_string(retdict, "mode", ins_compl_mode());
3593
3594 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
3595 ret = dict_add_number(retdict, "pum_visible", pum_visible());
3596
3597 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
3598 {
3599 list_T *li;
3600 dict_T *di;
3601 compl_T *match;
3602
3603 li = list_alloc();
3604 if (li == NULL)
3605 return;
3606 ret = dict_add_list(retdict, "items", li);
3607 if (ret == OK && compl_first_match != NULL)
3608 {
3609 match = compl_first_match;
3610 do
3611 {
3612 if (!(match->cp_flags & ORIGINAL_TEXT))
3613 {
3614 di = dict_alloc();
3615 if (di == NULL)
3616 return;
3617 ret = list_append_dict(li, di);
3618 if (ret != OK)
3619 return;
3620 dict_add_string(di, "word", match->cp_str);
3621 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
3622 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
3623 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
3624 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3625 dict_add_string(di, "user_data",
3626 match->cp_text[CPT_USER_DATA]);
3627 }
3628 match = match->cp_next;
3629 }
3630 while (match != NULL && match != compl_first_match);
3631 }
3632 }
3633
3634 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
3635 ret = dict_add_number(retdict, "selected", (compl_curr_match != NULL) ?
3636 compl_curr_match->cp_number - 1 : -1);
3637
3638 // TODO
3639 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
3640}
3641
3642/*
3643 * Return Insert completion mode name string
3644 */
3645 static char_u *
3646ins_compl_mode(void)
3647{
3648 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET || compl_started)
3649 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
3650
3651 return (char_u *)"";
3652}
3653
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003654/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003655 * Delete one character before the cursor and show the subset of the matches
3656 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003657 * Returns the character to be used, NUL if the work is done and another char
3658 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003659 */
3660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003661ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003662{
3663 char_u *line;
3664 char_u *p;
3665
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003666 line = ml_get_curline();
3667 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003668 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003669
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003670 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003671 * allow the word to be deleted, we won't match everything.
3672 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003673 if ((int)(p - line) - (int)compl_col < 0
3674 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003675 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3676 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3677 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003678 return K_BS;
3679
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003680 /* Deleted more than what was used to find matches or didn't finish
3681 * finding all matches: need to look for matches all over again. */
3682 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003683 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003684 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003685
Bram Moolenaara6557602006-02-04 22:43:20 +00003686 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003687 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003688 if (compl_leader != NULL)
3689 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003690 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003691 if (compl_shown_match != NULL)
3692 /* Make sure current match is not a hidden item. */
3693 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003694 return NUL;
3695 }
3696 return K_BS;
3697}
Bram Moolenaara6557602006-02-04 22:43:20 +00003698
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003699/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003700 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3701 * be called.
3702 */
3703 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003704ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003705{
3706 /* Return TRUE if we didn't complete finding matches or when the
3707 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3708 return compl_was_interrupted
3709 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3710 && compl_opt_refresh_always);
3711}
3712
3713/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003714 * Called after changing "compl_leader".
3715 * Show the popup menu with a different set of matches.
3716 * May also search for matches again if the previous search was interrupted.
3717 */
3718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003719ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003720{
3721 ins_compl_del_pum();
3722 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003723 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003724 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003725
3726 if (compl_started)
3727 ins_compl_set_original_text(compl_leader);
3728 else
3729 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003730#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003731 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003732#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003733 /*
Bram Moolenaarae654382019-01-17 21:09:05 +01003734 * Matches were cleared, need to search for them now. Befor drawing
3735 * the popup menu display the changed text before the cursor. Set
3736 * "compl_restarting" to avoid that the first match is inserted.
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003737 */
Bram Moolenaarae654382019-01-17 21:09:05 +01003738 pum_call_update_screen();
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003739#ifdef FEAT_GUI
3740 if (gui.in_use)
3741 {
3742 /* Show the cursor after the match, not after the redrawn text. */
3743 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003744 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003745 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003746#endif
3747 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003748 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003749 compl_cont_status = 0;
3750 compl_restarting = FALSE;
3751 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003752
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003753 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003754
3755 /* Show the popup menu with a different set of matches. */
3756 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003757
3758 /* Don't let Enter select the original text when there is no popup menu. */
3759 if (compl_match_array == NULL)
3760 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003761}
3762
3763/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003764 * Return the length of the completion, from the completion start column to
3765 * the cursor column. Making sure it never goes below zero.
3766 */
3767 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003768ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003769{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003770 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003771
3772 if (off < 0)
3773 return 0;
3774 return off;
3775}
3776
3777/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003778 * Append one character to the match leader. May reduce the number of
3779 * matches.
3780 */
3781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003782ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003783{
Bram Moolenaara6557602006-02-04 22:43:20 +00003784 int cc;
3785
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003786 if (stop_arrow() == FAIL)
3787 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003788 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3789 {
3790 char_u buf[MB_MAXBYTES + 1];
3791
3792 (*mb_char2bytes)(c, buf);
3793 buf[cc] = NUL;
3794 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003795 if (compl_opt_refresh_always)
3796 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003797 }
3798 else
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003799 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003800 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003801 if (compl_opt_refresh_always)
3802 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003803 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003804
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003805 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003806 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003807 ins_compl_restart();
3808
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003809 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003810 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003811 * break redo. */
3812 if (!compl_opt_refresh_always)
3813 {
3814 vim_free(compl_leader);
3815 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003816 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003817 if (compl_leader != NULL)
3818 ins_compl_new_leader();
3819 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003820}
3821
3822/*
3823 * Setup for finding completions again without leaving CTRL-X mode. Used when
3824 * BS or a key was typed while still searching for matches.
3825 */
3826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003827ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003828{
3829 ins_compl_free();
3830 compl_started = FALSE;
3831 compl_matches = 0;
3832 compl_cont_status = 0;
3833 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003834}
3835
3836/*
3837 * Set the first match, the original text.
3838 */
3839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003840ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003841{
3842 char_u *p;
3843
Bram Moolenaare87edf32018-04-17 22:14:32 +02003844 /* Replace the original text entry.
3845 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3846 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003847 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3848 {
3849 p = vim_strsave(str);
3850 if (p != NULL)
3851 {
3852 vim_free(compl_first_match->cp_str);
3853 compl_first_match->cp_str = p;
3854 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003855 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003856 else if (compl_first_match->cp_prev != NULL
3857 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3858 {
3859 p = vim_strsave(str);
3860 if (p != NULL)
3861 {
3862 vim_free(compl_first_match->cp_prev->cp_str);
3863 compl_first_match->cp_prev->cp_str = p;
3864 }
3865 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003866}
3867
3868/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003869 * Append one character to the match leader. May reduce the number of
3870 * matches.
3871 */
3872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003873ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003874{
3875 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003876 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003877 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003878 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003879
3880 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003881 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003882 {
3883 /* When still at the original match use the first entry that matches
3884 * the leader. */
3885 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3886 {
3887 p = NULL;
3888 for (cp = compl_shown_match->cp_next; cp != NULL
3889 && cp != compl_first_match; cp = cp->cp_next)
3890 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003891 if (compl_leader == NULL
3892 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003893 (int)STRLEN(compl_leader)))
3894 {
3895 p = cp->cp_str;
3896 break;
3897 }
3898 }
3899 if (p == NULL || (int)STRLEN(p) <= len)
3900 return;
3901 }
3902 else
3903 return;
3904 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003905 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003906 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003907 ins_compl_addleader(c);
3908}
3909
3910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003912 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003913 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003915 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003916ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917{
3918 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003920 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
3922 /* Forget any previous 'special' messages if this is actually
3923 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3924 */
3925 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3926 edit_submode_extra = NULL;
3927
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003928 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003929 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3930 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003931 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003933 /* Set "compl_get_longest" when finding the first matches. */
3934 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003935 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003936 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003937 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003938 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003939
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003940 }
3941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3943 {
3944 /*
3945 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3946 * it will be yet. Now we decide.
3947 */
3948 switch (c)
3949 {
3950 case Ctrl_E:
3951 case Ctrl_Y:
3952 ctrl_x_mode = CTRL_X_SCROLL;
3953 if (!(State & REPLACE_FLAG))
3954 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3955 else
3956 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3957 edit_submode_pre = NULL;
3958 showmode();
3959 break;
3960 case Ctrl_L:
3961 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3962 break;
3963 case Ctrl_F:
3964 ctrl_x_mode = CTRL_X_FILES;
3965 break;
3966 case Ctrl_K:
3967 ctrl_x_mode = CTRL_X_DICTIONARY;
3968 break;
3969 case Ctrl_R:
3970 /* Simply allow ^R to happen without affecting ^X mode */
3971 break;
3972 case Ctrl_T:
3973 ctrl_x_mode = CTRL_X_THESAURUS;
3974 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003975#ifdef FEAT_COMPL_FUNC
3976 case Ctrl_U:
3977 ctrl_x_mode = CTRL_X_FUNCTION;
3978 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003979 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003980 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003981 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003982#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003983 case 's':
3984 case Ctrl_S:
3985 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003986#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003987 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003988 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003989 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003990#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003991 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 case Ctrl_RSB:
3993 ctrl_x_mode = CTRL_X_TAGS;
3994 break;
3995#ifdef FEAT_FIND_ID
3996 case Ctrl_I:
3997 case K_S_TAB:
3998 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3999 break;
4000 case Ctrl_D:
4001 ctrl_x_mode = CTRL_X_PATH_DEFINES;
4002 break;
4003#endif
4004 case Ctrl_V:
4005 case Ctrl_Q:
4006 ctrl_x_mode = CTRL_X_CMDLINE;
4007 break;
4008 case Ctrl_P:
4009 case Ctrl_N:
4010 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
4011 * just started ^X mode, or there were enough ^X's to cancel
4012 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
4013 * do normal expansion when interrupting a different mode (say
4014 * ^X^F^X^P or ^P^X^X^P, see below)
4015 * nothing changes if interrupting mode 0, (eg, the flag
4016 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004017 if (!(compl_cont_status & CONT_INTRPT))
4018 compl_cont_status |= CONT_LOCAL;
4019 else if (compl_cont_mode != 0)
4020 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 /* FALLTHROUGH */
4022 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004023 /* If we have typed at least 2 ^X's... for modes != 0, we set
4024 * compl_cont_status = 0 (eg, as if we had just started ^X
4025 * mode).
4026 * For mode 0, we set "compl_cont_mode" to an impossible
4027 * value, in both cases ^X^X can be used to restart the same
4028 * mode (avoiding ADDING mode).
4029 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
4030 * 'complete' and local ^P expansions respectively.
4031 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
4032 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 if (c == Ctrl_X)
4034 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004035 if (compl_cont_mode != 0)
4036 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004038 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004040 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 edit_submode = NULL;
4042 showmode();
4043 break;
4044 }
4045 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004046 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
4048 /* We're already in CTRL-X mode, do we stay in it? */
4049 if (!vim_is_ctrl_x_key(c))
4050 {
4051 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004052 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 else
4054 ctrl_x_mode = CTRL_X_FINISHED;
4055 edit_submode = NULL;
4056 }
4057 showmode();
4058 }
4059
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004060 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
4062 /* Show error message from attempted keyword completion (probably
4063 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004064 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004066 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
4067 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 || ctrl_x_mode == CTRL_X_FINISHED)
4069 {
4070 /* Get here when we have finished typing a sequence of ^N and
4071 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004072 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004073 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
4075 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004076 * If any of the original typed text has been changed, eg when
4077 * ignorecase is set, we must add back-spaces to the redo
4078 * buffer. We add as few as necessary to delete just the part
4079 * of the original text that has changed.
4080 * When using the longest match, edited the match or used
4081 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004083 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4084 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004085 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004086 ptr = NULL;
4087 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089
4090#ifdef FEAT_CINDENT
4091 want_cindent = (can_cindent && cindent_on());
4092#endif
4093 /*
4094 * When completing whole lines: fix indent for 'cindent'.
4095 * Otherwise, break line if it's too long.
4096 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004097 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 {
4099#ifdef FEAT_CINDENT
4100 /* re-indent the current line */
4101 if (want_cindent)
4102 {
4103 do_c_expr_indent();
4104 want_cindent = FALSE; /* don't do it again */
4105 }
4106#endif
4107 }
4108 else
4109 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004110 int prev_col = curwin->w_cursor.col;
4111
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004113 if (prev_col > 0)
4114 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004115 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004116 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004118 if (prev_col > 0
4119 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4120 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004123 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004124 * the selection without inserting anything. When
4125 * compl_enter_selects is set the Enter key does the same. */
4126 if ((c == Ctrl_Y || (compl_enter_selects
4127 && (c == CAR || c == K_KENTER || c == NL)))
4128 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004129 retval = TRUE;
4130
Bram Moolenaar47247282016-08-02 22:36:02 +02004131 /* CTRL-E means completion is Ended, go back to the typed text.
4132 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004133 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004134 {
4135 ins_compl_delete();
4136 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004137 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004138 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004139 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004140 retval = TRUE;
4141 }
4142
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004143 auto_format(FALSE, TRUE);
4144
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004146 compl_started = FALSE;
4147 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004148 if (!shortmess(SHM_COMPLETIONMENU))
4149 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004150 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004151 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 if (edit_submode != NULL)
4153 {
4154 edit_submode = NULL;
4155 showmode();
4156 }
4157
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004158#ifdef FEAT_CMDWIN
4159 if (c == Ctrl_C && cmdwin_type != 0)
4160 /* Avoid the popup menu remains displayed when leaving the
4161 * command line window. */
4162 update_screen(0);
4163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164#ifdef FEAT_CINDENT
4165 /*
4166 * Indent now if a key was typed that is in 'cinkeys'.
4167 */
4168 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4169 do_c_expr_indent();
4170#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004171 /* Trigger the CompleteDone event to give scripts a chance to act
4172 * upon the completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004173 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 }
4175 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004176 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4177 /* Trigger the CompleteDone event to give scripts a chance to act
4178 * upon the (possibly failed) completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004179 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
4181 /* reset continue_* if we left expansion-mode, if we stay they'll be
4182 * (re)set properly in ins_complete() */
4183 if (!vim_is_ctrl_x_key(c))
4184 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004185 compl_cont_status = 0;
4186 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004188
4189 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190}
4191
4192/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004193 * Fix the redo buffer for the completion leader replacing some of the typed
4194 * text. This inserts backspaces and appends the changed text.
4195 * "ptr" is the known leader text or NUL.
4196 */
4197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004198ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004199{
4200 int len;
4201 char_u *p;
4202 char_u *ptr = ptr_arg;
4203
4204 if (ptr == NULL)
4205 {
4206 if (compl_leader != NULL)
4207 ptr = compl_leader;
4208 else
4209 return; /* nothing to do */
4210 }
4211 if (compl_orig_text != NULL)
4212 {
4213 p = compl_orig_text;
4214 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4215 ;
Bram Moolenaar62951b12011-09-21 18:23:05 +02004216 if (len > 0)
4217 len -= (*mb_head_off)(p, p + len);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004218 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004219 AppendCharToRedobuff(K_BS);
4220 }
4221 else
4222 len = 0;
4223 if (ptr != NULL)
4224 AppendToRedobuffLit(ptr + len, -1);
4225}
4226
4227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4229 * (depending on flag) starting from buf and looking for a non-scanned
4230 * buffer (other than curbuf). curbuf is special, if it is called with
4231 * buf=curbuf then it has to be the first call for a given flag/expansion.
4232 *
4233 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4234 */
4235 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004236ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004238 static win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004240 if (flag == 'w') // just windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 {
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004242 if (buf == curbuf || wp == NULL) // first call for this flag/expansion
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004244 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 && wp->w_buffer->b_scanned)
4246 ;
4247 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249 else
4250 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4251 * (unlisted buffers)
4252 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004253 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 && ((flag == 'U'
4255 ? buf->b_p_bl
4256 : (!buf->b_p_bl
4257 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004258 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 ;
4260 return buf;
4261}
4262
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004263#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004264/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004265 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004266 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004267 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004269expand_by_function(
4270 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4271 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004272{
Bram Moolenaar82139082011-09-14 16:52:09 +02004273 list_T *matchlist = NULL;
4274 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004275 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004276 char_u *funcname;
4277 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004278 win_T *curwin_save;
4279 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004280 typval_T rettv;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004281 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004282
Bram Moolenaare344bea2005-09-01 20:46:49 +00004283 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4284 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004285 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004286
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004287 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004288 args[0].v_type = VAR_NUMBER;
4289 args[0].vval.v_number = 0;
4290 args[1].v_type = VAR_STRING;
4291 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4292 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004293
Bram Moolenaare344bea2005-09-01 20:46:49 +00004294 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004295 curwin_save = curwin;
4296 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004297
4298 /* Call a function, which returns a list or dict. */
Bram Moolenaarded27a12018-08-01 19:06:03 +02004299 if (call_vim_function(funcname, 2, args, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004300 {
4301 switch (rettv.v_type)
4302 {
4303 case VAR_LIST:
4304 matchlist = rettv.vval.v_list;
4305 break;
4306 case VAR_DICT:
4307 matchdict = rettv.vval.v_dict;
4308 break;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004309 case VAR_SPECIAL:
4310 if (rettv.vval.v_number == VVAL_NONE)
4311 compl_opt_suppress_empty = TRUE;
4312 // FALLTHROUGH
Bram Moolenaar82139082011-09-14 16:52:09 +02004313 default:
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004314 // TODO: Give error message?
Bram Moolenaar82139082011-09-14 16:52:09 +02004315 clear_tv(&rettv);
4316 break;
4317 }
4318 }
4319
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004320 if (curwin_save != curwin || curbuf_save != curbuf)
4321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004322 emsg(_(e_complwin));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004323 goto theend;
4324 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004325 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004326 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004327 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004329 emsg(_(e_compldel));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004330 goto theend;
4331 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004332
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004333 if (matchlist != NULL)
4334 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004335 else if (matchdict != NULL)
4336 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004337
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004338theend:
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004339 // Restore State, it might have been changed.
4340 State = save_State;
4341
Bram Moolenaar82139082011-09-14 16:52:09 +02004342 if (matchdict != NULL)
4343 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004344 if (matchlist != NULL)
4345 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004346}
4347#endif /* FEAT_COMPL_FUNC */
4348
Bram Moolenaar39f05632006-03-19 22:15:26 +00004349#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004350/*
4351 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004352 */
4353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004354ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004355{
4356 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004357 int dir = compl_direction;
4358
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004359 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004360 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004361 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004362 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4363 /* if dir was BACKWARD then honor it just once */
4364 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004365 else if (did_emsg)
4366 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004367 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004368}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004369
4370/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004371 * Add completions from a dict.
4372 */
4373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004374ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004375{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004376 dictitem_T *di_refresh;
4377 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004378
4379 /* Check for optional "refresh" item. */
4380 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004381 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4382 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004383 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004384 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004385
4386 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4387 compl_opt_refresh_always = TRUE;
4388 }
4389
4390 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004391 di_words = dict_find(dict, (char_u *)"words", 5);
4392 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4393 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004394}
4395
4396/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004397 * Add a match to the list of matches from a typeval_T.
4398 * If the given string is already in the list of completions, then return
4399 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4400 * maybe because alloc() returns NULL, then FAIL is returned.
4401 */
4402 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004403ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004404{
4405 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004406 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004407 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004408 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004409 char_u *(cptext[CPT_COUNT]);
4410
4411 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4412 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004413 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4414 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004415 (char_u *)"abbr", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004416 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004417 (char_u *)"menu", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004418 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004419 (char_u *)"kind", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004420 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004421 (char_u *)"info", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004422 cptext[CPT_USER_DATA] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004423 (char_u *)"user_data", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004424 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4425 icase = dict_get_number(tv->vval.v_dict, (char_u *)"icase");
4426 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
4427 adup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
4428 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4429 aempty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004430 }
4431 else
4432 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004433 word = tv_get_string_chk(tv);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004434 vim_memset(cptext, 0, sizeof(cptext));
4435 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004436 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004437 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004438 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004439}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004440#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004441
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442/*
4443 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004444 * The search starts at position "ini" in curbuf and in the direction
4445 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004446 * When "compl_started" is FALSE start at that position, otherwise continue
4447 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004448 * This may return before finding all the matches.
4449 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 */
4451 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004452ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453{
4454 static pos_T first_match_pos;
4455 static pos_T last_match_pos;
4456 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457 static int found_all = FALSE; /* Found all matches of a
4458 certain type. */
4459 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
Bram Moolenaar572cb562005-08-05 21:35:02 +00004461 pos_T *pos;
4462 char_u **matches;
4463 int save_p_scs;
4464 int save_p_ws;
4465 int save_p_ic;
4466 int i;
4467 int num_matches;
4468 int len;
4469 int found_new_match;
4470 int type = ctrl_x_mode;
4471 char_u *ptr;
4472 char_u *dict = NULL;
4473 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004474 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004476 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004478 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 ins_buf->b_scanned = 0;
4480 found_all = FALSE;
4481 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004482 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004483 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 last_match_pos = first_match_pos = *ini;
4485 }
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004486 else if (ins_buf != curbuf && !buf_valid(ins_buf))
4487 ins_buf = curbuf; // In case the buffer was wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
Bram Moolenaar4475b622017-05-01 20:46:52 +02004489 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004490 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004491
4492 /*
4493 * For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 for (;;)
4496 {
4497 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004498 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004500 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 * or if found_all says this entry is done. For ^X^L only use the
4502 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004503 if ((ctrl_x_mode == CTRL_X_NORMAL
4504 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
4507 found_all = FALSE;
4508 while (*e_cpt == ',' || *e_cpt == ' ')
4509 e_cpt++;
4510 if (*e_cpt == '.' && !curbuf->b_scanned)
4511 {
4512 ins_buf = curbuf;
4513 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004514 /* Move the cursor back one character so that ^N can match the
4515 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004516 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004517 {
4518 /* Move the cursor to after the last character in the
4519 * buffer, so that word at start of buffer is found
4520 * correctly. */
4521 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4522 first_match_pos.col =
4523 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 last_match_pos = first_match_pos;
4526 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004527
4528 /* Remember the first match so that the loop stops when we
4529 * wrap and come back there a second time. */
4530 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 }
4532 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4533 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4534 {
4535 /* Scan a buffer, but not the current one. */
4536 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4537 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 first_match_pos.col = last_match_pos.col = 0;
4540 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4541 last_match_pos.lnum = 0;
4542 type = 0;
4543 }
4544 else /* unloaded buffer, scan like dictionary */
4545 {
4546 found_all = TRUE;
4547 if (ins_buf->b_fname == NULL)
4548 continue;
4549 type = CTRL_X_DICTIONARY;
4550 dict = ins_buf->b_fname;
4551 dict_f = DICT_EXACT;
4552 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004553 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 ins_buf->b_fname == NULL
4555 ? buf_spname(ins_buf)
4556 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004557 ? ins_buf->b_fname
4558 : ins_buf->b_sfname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004559 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
4561 else if (*e_cpt == NUL)
4562 break;
4563 else
4564 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004565 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 type = -1;
4567 else if (*e_cpt == 'k' || *e_cpt == 's')
4568 {
4569 if (*e_cpt == 'k')
4570 type = CTRL_X_DICTIONARY;
4571 else
4572 type = CTRL_X_THESAURUS;
4573 if (*++e_cpt != ',' && *e_cpt != NUL)
4574 {
4575 dict = e_cpt;
4576 dict_f = DICT_FIRST;
4577 }
4578 }
4579#ifdef FEAT_FIND_ID
4580 else if (*e_cpt == 'i')
4581 type = CTRL_X_PATH_PATTERNS;
4582 else if (*e_cpt == 'd')
4583 type = CTRL_X_PATH_DEFINES;
4584#endif
4585 else if (*e_cpt == ']' || *e_cpt == 't')
4586 {
4587 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004588 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar32526b32019-01-19 17:43:09 +01004589 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 else
4592 type = -1;
4593
4594 /* in any case e_cpt is advanced to the next entry */
4595 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4596
4597 found_all = TRUE;
4598 if (type == -1)
4599 continue;
4600 }
4601 }
4602
Bram Moolenaar4475b622017-05-01 20:46:52 +02004603 /* If complete() was called then compl_pattern has been reset. The
4604 * following won't work then, bail out. */
4605 if (compl_pattern == NULL)
4606 break;
4607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 switch (type)
4609 {
4610 case -1:
4611 break;
4612#ifdef FEAT_FIND_ID
4613 case CTRL_X_PATH_PATTERNS:
4614 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004615 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004618 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4620 (linenr_T)1, (linenr_T)MAXLNUM);
4621 break;
4622#endif
4623
4624 case CTRL_X_DICTIONARY:
4625 case CTRL_X_THESAURUS:
4626 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004627 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 : (type == CTRL_X_THESAURUS
4629 ? (*curbuf->b_p_tsr == NUL
4630 ? p_tsr
4631 : curbuf->b_p_tsr)
4632 : (*curbuf->b_p_dict == NUL
4633 ? p_dict
4634 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004635 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004636 dict != NULL ? dict_f
4637 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 dict = NULL;
4639 break;
4640
4641 case CTRL_X_TAGS:
4642 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4643 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004644 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004646 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004647 * of matches is found when compl_pattern is empty */
4648 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004649 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4650 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4652 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004653 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
4655 p_ic = save_p_ic;
4656 break;
4657
4658 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004659 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4661 {
4662
4663 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004664 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004665 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
4667 break;
4668
4669 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004670 if (expand_cmdline(&compl_xp, compl_pattern,
4671 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004673 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 break;
4675
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004676#ifdef FEAT_COMPL_FUNC
4677 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004678 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004679 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004680 break;
4681#endif
4682
Bram Moolenaar488c6512005-08-11 20:09:58 +00004683 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004684#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004685 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004686 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004687 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004688 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004689#endif
4690 break;
4691
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 default: /* normal ^P/^N and ^X^L */
4693 /*
4694 * If 'infercase' is set, don't use 'smartcase' here
4695 */
4696 save_p_scs = p_scs;
4697 if (ins_buf->b_p_inf)
4698 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004699
Bram Moolenaar361aa502014-01-23 22:45:58 +01004700 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 * end but never from the middle, thus setting nowrapscan in this
4702 * buffers is a good idea, on the other hand, we always set
4703 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4704 save_p_ws = p_ws;
4705 if (ins_buf != curbuf)
4706 p_ws = FALSE;
4707 else if (*e_cpt == '.')
4708 p_ws = TRUE;
4709 for (;;)
4710 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004711 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004713 ++msg_silent; /* Don't want messages for wrapscan. */
4714
Bram Moolenaare4214502015-03-05 18:08:43 +01004715 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4716 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004717 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004718 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004719 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004721 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 else
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004723 found_new_match = searchit(NULL, ins_buf, pos, NULL,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004724 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004725 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004726 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004727 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004728 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004730 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004731 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 first_match_pos = *pos;
4733 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004734 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004737 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 found_new_match = FAIL;
4739 if (found_new_match == FAIL)
4740 {
4741 if (ins_buf == curbuf)
4742 found_all = TRUE;
4743 break;
4744 }
4745
4746 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004747 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 && ini->lnum == pos->lnum
4749 && ini->col == pos->col)
4750 continue;
4751 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004752 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004754 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4757 continue;
4758 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4759 if (!p_paste)
4760 ptr = skipwhite(ptr);
4761 }
4762 len = (int)STRLEN(ptr);
4763 }
4764 else
4765 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004766 char_u *tmp_ptr = ptr;
4767
4768 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004770 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 /* Skip if already inside a word. */
4772 if (vim_iswordp(tmp_ptr))
4773 continue;
4774 /* Find start of next word. */
4775 tmp_ptr = find_word_start(tmp_ptr);
4776 }
4777 /* Find end of this word. */
4778 tmp_ptr = find_word_end(tmp_ptr);
4779 len = (int)(tmp_ptr - ptr);
4780
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004781 if ((compl_cont_status & CONT_ADDING)
4782 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
4784 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4785 {
4786 /* Try next line, if any. the new word will be
4787 * "join" as if the normal command "J" was used.
4788 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004789 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 * works -- Acevedo */
4791 STRNCPY(IObuff, ptr, len);
4792 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4793 tmp_ptr = ptr = skipwhite(ptr);
4794 /* Find start of next word. */
4795 tmp_ptr = find_word_start(tmp_ptr);
4796 /* Find end of next word. */
4797 tmp_ptr = find_word_end(tmp_ptr);
4798 if (tmp_ptr > ptr)
4799 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004800 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004802 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 IObuff[len++] = ' ';
4804 /* IObuf =~ "\k.* ", thus len >= 2 */
4805 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004806 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 || (vim_strchr(p_cpo, CPO_JOINSP)
4808 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004809 && (IObuff[len - 2] == '?'
4810 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 IObuff[len++] = ' ';
4812 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004813 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 if (tmp_ptr - ptr >= IOSIZE - len)
4815 tmp_ptr = ptr + IOSIZE - len - 1;
4816 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4817 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004818 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
4820 IObuff[len] = NUL;
4821 ptr = IObuff;
4822 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004823 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 continue;
4825 }
4826 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004827 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004828 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004829 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
4831 found_new_match = OK;
4832 break;
4833 }
4834 }
4835 p_scs = save_p_scs;
4836 p_ws = save_p_ws;
4837 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004838
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004839 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004840 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004841 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 found_new_match = OK;
4843
4844 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004845 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4846 * match */
4847 if ((ctrl_x_mode != CTRL_X_NORMAL
4848 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004849 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004850 {
4851 if (got_int)
4852 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004853 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004854 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004855 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004856
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004857 if ((ctrl_x_mode != CTRL_X_NORMAL
4858 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004859 || compl_interrupted)
4860 break;
4861 compl_started = TRUE;
4862 }
4863 else
4864 {
4865 /* Mark a buffer scanned when it has been scanned completely */
4866 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4867 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004869 compl_started = FALSE;
4870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004872 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004874 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 && *e_cpt == NUL) /* Got to end of 'complete' */
4876 found_new_match = FAIL;
4877
4878 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004879 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4880 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 i = ins_compl_make_cyclic();
4882
Bram Moolenaar4475b622017-05-01 20:46:52 +02004883 if (compl_old_match != NULL)
4884 {
4885 /* If several matches were added (FORWARD) or the search failed and has
4886 * just been made cyclic then we have to move compl_curr_match to the
4887 * next or previous entry (if any) -- Acevedo */
4888 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4889 : compl_old_match->cp_prev;
4890 if (compl_curr_match == NULL)
4891 compl_curr_match = compl_old_match;
4892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 return i;
4894}
4895
4896/* Delete the old text being completed. */
4897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004898ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004900 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901
4902 /*
4903 * In insert mode: Delete the typed part.
4904 * In replace mode: Put the old characters back, if any.
4905 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004906 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4907 if ((int)curwin->w_cursor.col > col)
4908 {
4909 if (stop_arrow() == FAIL)
4910 return;
4911 backspace_until_column(col);
4912 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004913
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004914 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4915 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004917 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004918 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919}
4920
Bram Moolenaar472e8592016-10-15 17:06:47 +02004921/*
4922 * Insert the new text being completed.
4923 * "in_compl_func" is TRUE when called from complete_check().
4924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004926ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004928 dict_T *dict;
4929
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004930 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004931 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4932 compl_used_match = FALSE;
4933 else
4934 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004935
4936 /* Set completed item. */
4937 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004938 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004939 if (dict != NULL)
4940 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004941 dict_add_string(dict, "word", compl_shown_match->cp_str);
4942 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4943 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4944 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4945 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4946 dict_add_string(dict, "user_data",
4947 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004948 }
4949 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004950 if (!in_compl_func)
4951 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952}
4953
4954/*
4955 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004956 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4957 * get more completions. If it is FALSE, then we just do nothing when there
4958 * are no more completions in a given direction. The latter case is used when
4959 * we are still in the middle of finding completions, to allow browsing
4960 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 * Return the total number of matches, or -1 if still unknown -- webb.
4962 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004963 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4964 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 *
4966 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004967 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4968 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 */
4970 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004971ins_compl_next(
4972 int allow_get_expansion,
4973 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004974 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004975 int insert_match, /* Insert the newly selected match */
4976 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977{
4978 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004979 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004980 compl_T *found_compl = NULL;
4981 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004982 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004983 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004985 /* When user complete function return -1 for findstart which is next
4986 * time of 'always', compl_shown_match become NULL. */
4987 if (compl_shown_match == NULL)
4988 return -1;
4989
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004990 if (compl_leader != NULL
4991 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004993 /* Set "compl_shown_match" to the actually shown match, it may differ
4994 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004995 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004996 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004997 && compl_shown_match->cp_next != NULL
4998 && compl_shown_match->cp_next != compl_first_match)
4999 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00005000
5001 /* If we didn't find it searching forward, and compl_shows_dir is
5002 * backward, find the last match. */
5003 if (compl_shows_dir == BACKWARD
5004 && !ins_compl_equal(compl_shown_match,
5005 compl_leader, (int)STRLEN(compl_leader))
5006 && (compl_shown_match->cp_next == NULL
5007 || compl_shown_match->cp_next == compl_first_match))
5008 {
5009 while (!ins_compl_equal(compl_shown_match,
5010 compl_leader, (int)STRLEN(compl_leader))
5011 && compl_shown_match->cp_prev != NULL
5012 && compl_shown_match->cp_prev != compl_first_match)
5013 compl_shown_match = compl_shown_match->cp_prev;
5014 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005015 }
5016
5017 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005018 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 /* Delete old text to be replaced */
5020 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005021
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005022 /* When finding the longest common text we stick at the original text,
5023 * don't let CTRL-N or CTRL-P move to the first match. */
5024 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
5025
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005026 /* When restarting the search don't insert the first match either. */
5027 if (compl_restarting)
5028 {
5029 advance = FALSE;
5030 compl_restarting = FALSE;
5031 }
5032
Bram Moolenaare3226be2005-12-18 22:10:00 +00005033 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
5034 * around. */
5035 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00005037 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00005039 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00005040 found_end = (compl_first_match != NULL
5041 && (compl_shown_match->cp_next == compl_first_match
5042 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00005043 }
5044 else if (compl_shows_dir == BACKWARD
5045 && compl_shown_match->cp_prev != NULL)
5046 {
Bram Moolenaara6557602006-02-04 22:43:20 +00005047 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00005048 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00005049 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
5051 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00005052 {
Bram Moolenaara260a972006-06-23 19:36:29 +00005053 if (!allow_get_expansion)
5054 {
5055 if (advance)
5056 {
5057 if (compl_shows_dir == BACKWARD)
5058 compl_pending -= todo + 1;
5059 else
5060 compl_pending += todo + 1;
5061 }
5062 return -1;
5063 }
5064
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005065 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005066 {
5067 if (compl_shows_dir == BACKWARD)
5068 --compl_pending;
5069 else
5070 ++compl_pending;
5071 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005072
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005073 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005074 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005075
5076 /* handle any pending completions */
5077 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005078 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005079 {
5080 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5081 {
5082 compl_shown_match = compl_shown_match->cp_next;
5083 --compl_pending;
5084 }
5085 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5086 {
5087 compl_shown_match = compl_shown_match->cp_prev;
5088 ++compl_pending;
5089 }
5090 else
5091 break;
5092 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005093 found_end = FALSE;
5094 }
5095 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5096 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005097 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005098 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005099 ++todo;
5100 else
5101 /* Remember a matching item. */
5102 found_compl = compl_shown_match;
5103
5104 /* Stop at the end of the list when we found a usable match. */
5105 if (found_end)
5106 {
5107 if (found_compl != NULL)
5108 {
5109 compl_shown_match = found_compl;
5110 break;
5111 }
5112 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005116 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005117 if (compl_no_insert && !started)
5118 {
5119 ins_bytes(compl_orig_text + ins_compl_len());
5120 compl_used_match = FALSE;
5121 }
5122 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005123 {
5124 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005125 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005126 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005127 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005128 }
5129 else
5130 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131
5132 if (!allow_get_expansion)
5133 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005134 /* may undisplay the popup menu first */
5135 ins_compl_upd_pum();
5136
Bram Moolenaar2a78b7c2019-02-05 20:12:06 +01005137 if (pum_enough_matches())
5138 // Will display the popup menu, don't redraw yet to avoid flicker.
5139 pum_call_update_screen();
5140 else
5141 // Not showing the popup menu yet, redraw to show the user what was
5142 // inserted.
5143 update_screen(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005144
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005145 /* display the updated popup menu */
5146 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005147#ifdef FEAT_GUI
5148 if (gui.in_use)
5149 {
5150 /* Show the cursor after the match, not after the redrawn text. */
5151 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005152 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005153 }
5154#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005155
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 /* Delete old text to be replaced, since we're still searching and
5157 * don't want to match ourselves! */
5158 ins_compl_delete();
5159 }
5160
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005161 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005162 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005163 if (compl_no_insert && !started)
5164 compl_enter_selects = TRUE;
5165 else
5166 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005167
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 /*
5169 * Show the file name for the match (if any)
5170 * Truncate the file name to avoid a wait for return.
5171 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005172 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005174 char *lead = _("match in file");
5175 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5176 char_u *s;
5177 char_u *e;
5178
5179 if (space > 0)
5180 {
5181 /* We need the tail that fits. With double-byte encoding going
5182 * back from the end is very slow, thus go from the start and keep
5183 * the text that fits in "space" between "s" and "e". */
5184 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5185 {
5186 space -= ptr2cells(e);
5187 while (space < 0)
5188 {
5189 space += ptr2cells(s);
5190 MB_PTR_ADV(s);
5191 }
5192 }
5193 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5194 s > compl_shown_match->cp_fname ? "<" : "", s);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005195 msg((char *)IObuff);
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005196 redraw_cmdline = FALSE; /* don't overwrite! */
5197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
5199
5200 return num_matches;
5201}
5202
5203/*
5204 * Call this while finding completions, to check whether the user has hit a key
5205 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005206 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005208 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005209 * "in_compl_func" is TRUE when called from complete_check(), don't set
5210 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 */
5212 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005213ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214{
5215 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005216 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005218 /* Don't check when reading keys from a script, :normal or feedkeys().
5219 * That would break the test scripts. But do check for keys when called
5220 * from complete_check(). */
5221 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 return;
5223
5224 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005225 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 return;
5227 count = 0;
5228
Bram Moolenaara260a972006-06-23 19:36:29 +00005229 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5230 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 if (c != NUL)
5233 {
5234 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5235 {
5236 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005237 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005238 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005239 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005241 else
5242 {
5243 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005244 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005245 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005246 if (c != K_IGNORE)
5247 {
5248 /* Don't interrupt completion when the character wasn't typed,
5249 * e.g., when doing @q to replay keys. */
5250 if (c != Ctrl_R && KeyTyped)
5251 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005252
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005253 vungetc(c);
5254 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005257 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005258 {
5259 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5260
5261 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005262 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005263 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005264}
5265
5266/*
5267 * Decide the direction of Insert mode complete from the key typed.
5268 * Returns BACKWARD or FORWARD.
5269 */
5270 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005271ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005272{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005273 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005274 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005275 return BACKWARD;
5276 return FORWARD;
5277}
5278
5279/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005280 * Return TRUE for keys that are used for completion only when the popup menu
5281 * is visible.
5282 */
5283 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005284ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005285{
5286 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005287 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5288 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005289}
5290
5291/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005292 * Decide the number of completions to move forward.
5293 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5294 */
5295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005296ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005297{
5298 int h;
5299
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005300 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005301 {
5302 h = pum_get_height();
5303 if (h > 3)
5304 h -= 2; /* keep some context */
5305 return h;
5306 }
5307 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308}
5309
5310/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005311 * Return TRUE if completion with "c" should insert the match, FALSE if only
5312 * to change the currently selected completion.
5313 */
5314 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005315ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005316{
5317 switch (c)
5318 {
5319 case K_UP:
5320 case K_DOWN:
5321 case K_PAGEDOWN:
5322 case K_KPAGEDOWN:
5323 case K_S_DOWN:
5324 case K_PAGEUP:
5325 case K_KPAGEUP:
5326 case K_S_UP:
5327 return FALSE;
5328 }
5329 return TRUE;
5330}
5331
5332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 * Do Insert mode completion.
5334 * Called when character "c" was typed, which has a meaning for completion.
5335 * Returns OK if completion was done, FAIL if something failed (out of mem).
5336 */
5337 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005338ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005340 char_u *line;
5341 int startcol = 0; /* column where searched text starts */
5342 colnr_T curs_col; /* cursor column */
5343 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005344 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005345 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005346 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005347 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348
Bram Moolenaare3226be2005-12-18 22:10:00 +00005349 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005350 insert_match = ins_compl_use_match(c);
5351
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005352 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
5354 /* First time we hit ^N or ^P (in a row, I mean) */
5355
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 did_ai = FALSE;
5357#ifdef FEAT_SMARTINDENT
5358 did_si = FALSE;
5359 can_si = FALSE;
5360 can_si_back = FALSE;
5361#endif
5362 if (stop_arrow() == FAIL)
5363 return FAIL;
5364
5365 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005366 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005367 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005369 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005370 * "compl_startpos" to the cursor as a pattern to add a new word
5371 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005372 * "compl_startpos" is not in the same line as the cursor then fix it
5373 * (the line has been split because it was longer than 'tw'). if SOL
5374 * is set then skip the previous pattern, a word at the beginning of
5375 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005376 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5377 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
5379 /*
5380 * it is a continued search
5381 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005382 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005383 if (ctrl_x_mode == CTRL_X_NORMAL
5384 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5385 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005387 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005389 /* line (probably) wrapped, set compl_startpos to the
5390 * first non_blank in the line, if it is not a wordchar
5391 * include it to get a better pattern, but then we don't
5392 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005393 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005394 compl_startpos.col = compl_col;
5395 compl_startpos.lnum = curwin->w_cursor.lnum;
5396 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
5398 else
5399 {
5400 /* S_IPOS was set when we inserted a word that was at the
5401 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005402 * mode but first we need to redefine compl_startpos */
5403 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005405 compl_cont_status |= CONT_SOL;
5406 compl_startpos.col = (colnr_T)(skipwhite(
5407 line + compl_length
5408 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005410 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005412 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005413 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005414 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005416 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005418 compl_cont_status &= ~CONT_SOL;
5419 compl_length = (IOSIZE - MIN_SPACE);
5420 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005422 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5423 if (compl_length < 1)
5424 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005426 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005427 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005429 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005432 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005434 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005437 if (ctrl_x_mode != CTRL_X_NORMAL)
5438 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005439 compl_cont_status = 0;
5440 compl_cont_status |= CONT_N_ADDS;
5441 compl_startpos = curwin->w_cursor;
5442 startcol = (int)curs_col;
5443 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445
5446 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005447 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005449 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5451 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005452 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005454 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005456 compl_col += ++startcol;
5457 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
5459 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005460 compl_pattern = str_foldcase(line + compl_col,
5461 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005463 compl_pattern = vim_strnsave(line + compl_col,
5464 compl_length);
5465 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 return FAIL;
5467 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005468 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {
5470 char_u *prefix = (char_u *)"\\<";
5471
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005472 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005473 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005474 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005475 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005477 if (!vim_iswordp(line + compl_col)
5478 || (compl_col > 0
Bram Moolenaar13505972019-01-24 15:04:48 +01005479 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005481 STRCPY((char *)compl_pattern, prefix);
5482 (void)quote_meta(compl_pattern + STRLEN(prefix),
5483 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
Bram Moolenaar13505972019-01-24 15:04:48 +01005485 else if (--startcol < 0
5486 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 {
5488 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005489 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5490 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005492 compl_col += curs_col;
5493 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
5495 else
5496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 /* Search the point of change class of multibyte character
5498 * or not a word single byte character backward. */
5499 if (has_mbyte)
5500 {
5501 int base_class;
5502 int head_off;
5503
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005504 startcol -= (*mb_head_off)(line, line + startcol);
5505 base_class = mb_get_class(line + startcol);
5506 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005508 head_off = (*mb_head_off)(line, line + startcol);
5509 if (base_class != mb_get_class(line + startcol
5510 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005512 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 }
5514 }
5515 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005516 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005518 compl_col += ++startcol;
5519 compl_length = (int)curs_col - startcol;
5520 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 {
5522 /* Only match word with at least two chars -- webb
5523 * there's no need to call quote_meta,
5524 * alloc(7) is enough -- Acevedo
5525 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005526 compl_pattern = alloc(7);
5527 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005529 STRCPY((char *)compl_pattern, "\\<");
5530 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5531 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 }
5533 else
5534 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005535 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005536 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005537 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005539 STRCPY((char *)compl_pattern, "\\<");
5540 (void)quote_meta(compl_pattern + 2, line + compl_col,
5541 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543 }
5544 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005545 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005547 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005548 compl_length = (int)curs_col - (int)compl_col;
5549 if (compl_length < 0) /* cursor in indent: empty pattern */
5550 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005552 compl_pattern = str_foldcase(line + compl_col, compl_length,
5553 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005555 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5556 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 return FAIL;
5558 }
5559 else if (ctrl_x_mode == CTRL_X_FILES)
5560 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005561 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005562 if (startcol > 0)
5563 {
5564 char_u *p = line + startcol;
5565
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005566 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005567 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005568 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005569 if (p == line && vim_isfilec(PTR2CHAR(p)))
5570 startcol = 0;
5571 else
5572 startcol = (int)(p - line) + 1;
5573 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005574
Bram Moolenaar0300e462013-09-08 16:03:45 +02005575 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005576 compl_length = (int)curs_col - startcol;
5577 compl_pattern = addstar(line + compl_col, compl_length,
5578 EXPAND_FILES);
5579 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 return FAIL;
5581 }
5582 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5583 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005584 compl_pattern = vim_strnsave(line, curs_col);
5585 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005587 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005588 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005589 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5590 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005591 /* No completion possible, use an empty pattern to get a
5592 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005593 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005594 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005595 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5596 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005598 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005599 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005600#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005601 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005602 * Call user defined function 'completefunc' with "a:findstart"
5603 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005604 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005605 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005606 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005607 char_u *funcname;
5608 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005609 win_T *curwin_save;
5610 buf_T *curbuf_save;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005611 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005612
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005613 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005614 * string */
5615 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5616 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5617 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005619 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005620 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005621 /* restore did_ai, so that adding comment leader works */
5622 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005623 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005624 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005625
Bram Moolenaarffa96842018-06-12 22:05:14 +02005626 args[0].v_type = VAR_NUMBER;
5627 args[0].vval.v_number = 1;
5628 args[1].v_type = VAR_STRING;
5629 args[1].vval.v_string = (char_u *)"";
5630 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005631 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005632 curwin_save = curwin;
5633 curbuf_save = curbuf;
Bram Moolenaarded27a12018-08-01 19:06:03 +02005634 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005635
5636 State = save_State;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005637 if (curwin_save != curwin || curbuf_save != curbuf)
5638 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005639 emsg(_(e_complwin));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005640 return FAIL;
5641 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005642 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005643 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005644 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005645 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005646 emsg(_(e_compldel));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005647 return FAIL;
5648 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005649
Bram Moolenaar6110a002012-01-26 18:58:38 +01005650 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005651 * cancel the complete without an error.
5652 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005653 if (col == -2)
5654 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005655 if (col == -3)
5656 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005657 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005658 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005659 if (!shortmess(SHM_COMPLETIONMENU))
5660 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005661 return FAIL;
5662 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005663
Bram Moolenaar82139082011-09-14 16:52:09 +02005664 /*
5665 * Reset extended parameters of completion, when start new
5666 * completion.
5667 */
5668 compl_opt_refresh_always = FALSE;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005669 compl_opt_suppress_empty = FALSE;
Bram Moolenaar82139082011-09-14 16:52:09 +02005670
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005671 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005672 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005673 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005674 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005675 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005676
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005677 /* Setup variables for completion. Need to obtain "line" again,
5678 * it may have become invalid. */
5679 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005680 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005681 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5682 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005683#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005684 return FAIL;
5685 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005686 else if (ctrl_x_mode == CTRL_X_SPELL)
5687 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005688#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005689 if (spell_bad_len > 0)
5690 compl_col = curs_col - spell_bad_len;
5691 else
5692 compl_col = spell_word_start(startcol);
5693 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005694 {
5695 compl_length = 0;
5696 compl_col = curs_col;
5697 }
5698 else
5699 {
5700 spell_expand_check_cap(compl_col);
5701 compl_length = (int)curs_col - compl_col;
5702 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005703 /* Need to obtain "line" again, it may have become invalid. */
5704 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005705 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5706 if (compl_pattern == NULL)
5707#endif
5708 return FAIL;
5709 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005710 else
5711 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005712 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005713 return FAIL;
5714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005716 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
5718 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005719 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 /* Insert a new line, keep indentation but ignore 'comments' */
5722#ifdef FEAT_COMMENTS
5723 char_u *old = curbuf->b_p_com;
5724
5725 curbuf->b_p_com = (char_u *)"";
5726#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005727 compl_startpos.lnum = curwin->w_cursor.lnum;
5728 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 ins_eol('\r');
5730#ifdef FEAT_COMMENTS
5731 curbuf->b_p_com = old;
5732#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005733 compl_length = 0;
5734 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 }
5736 }
5737 else
5738 {
5739 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005740 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
5742
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005743 if (compl_cont_status & CONT_LOCAL)
5744 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 else
5746 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5747
Bram Moolenaar62951b12011-09-21 18:23:05 +02005748 /* If any of the original typed text has been changed we need to fix
5749 * the redo buffer. */
5750 ins_compl_fixRedoBufForLeader(NULL);
5751
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005752 /* Always add completion for the original text. */
5753 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005754 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5755 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005756 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005758 VIM_CLEAR(compl_pattern);
5759 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 return FAIL;
5761 }
5762
5763 /* showmode might reset the internal line pointers, so it must
5764 * be called before line = ml_get(), or when this address is no
5765 * longer needed. -- Acevedo.
5766 */
5767 edit_submode_extra = (char_u *)_("-- Searching...");
5768 edit_submode_highl = HLF_COUNT;
5769 showmode();
5770 edit_submode_extra = NULL;
5771 out_flush();
5772 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005773 else if (insert_match && stop_arrow() == FAIL)
5774 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005776 compl_shown_match = compl_curr_match;
5777 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
5779 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005780 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005782 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005783 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005784 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005786 /* may undisplay the popup menu */
5787 ins_compl_upd_pum();
5788
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005789 if (n > 1) /* all matches have been found */
5790 compl_matches = n;
5791 compl_curr_match = compl_shown_match;
5792 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
Bram Moolenaard68071d2006-05-02 22:08:30 +00005794 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5795 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 if (got_int && !global_busy)
5797 {
5798 (void)vgetc();
5799 got_int = FALSE;
5800 }
5801
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005802 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005803 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005805 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5806 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5808 edit_submode_highl = HLF_E;
5809 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5810 * because we couldn't expand anything at first place, but if we used
5811 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5812 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005813 if ( compl_length > 1
5814 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005815 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5817 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005818 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 }
5820
Bram Moolenaar572cb562005-08-05 21:35:02 +00005821 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005822 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005824 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
5826 if (edit_submode_extra == NULL)
5827 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005828 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 {
5830 edit_submode_extra = (char_u *)_("Back at original");
5831 edit_submode_highl = HLF_W;
5832 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005833 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 {
5835 edit_submode_extra = (char_u *)_("Word from other line");
5836 edit_submode_highl = HLF_COUNT;
5837 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005838 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 {
5840 edit_submode_extra = (char_u *)_("The only match");
5841 edit_submode_highl = HLF_COUNT;
5842 }
5843 else
5844 {
5845 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005846 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005848 int number = 0;
5849 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005851 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {
5853 /* search backwards for the first valid (!= -1) number.
5854 * This should normally succeed already at the first loop
5855 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005856 for (match = compl_curr_match->cp_prev; match != NULL
5857 && match != compl_first_match;
5858 match = match->cp_prev)
5859 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005861 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 break;
5863 }
5864 if (match != NULL)
5865 /* go up and assign all numbers which are not assigned
5866 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005867 for (match = match->cp_next;
5868 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005869 match = match->cp_next)
5870 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 }
5872 else /* BACKWARD */
5873 {
5874 /* search forwards (upwards) for the first valid (!= -1)
5875 * number. This should normally succeed already at the
5876 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005877 for (match = compl_curr_match->cp_next; match != NULL
5878 && match != compl_first_match;
5879 match = match->cp_next)
5880 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005882 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 break;
5884 }
5885 if (match != NULL)
5886 /* go down and assign all numbers which are not
5887 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005888 for (match = match->cp_prev; match
5889 && match->cp_number == -1;
5890 match = match->cp_prev)
5891 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 }
5893 }
5894
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005895 /* The match should always have a sequence number now, this is
5896 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005897 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005899 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5900 * Translations may need more than twice that. */
5901 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005903 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005904 vim_snprintf((char *)match_ref, sizeof(match_ref),
5905 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005906 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005908 vim_snprintf((char *)match_ref, sizeof(match_ref),
5909 _("match %d"),
5910 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 edit_submode_extra = match_ref;
5912 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005913 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 curs_columns(FALSE);
5915 }
5916 }
5917 }
5918
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005919 // Show a message about what (completion) mode we're in.
5920 if (!compl_opt_suppress_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005922 showmode();
5923 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaarea389e92014-05-28 21:40:52 +02005924 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005925 if (edit_submode_extra != NULL)
5926 {
5927 if (!p_smd)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005928 msg_attr((char *)edit_submode_extra,
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005929 edit_submode_highl < HLF_COUNT
5930 ? HL_ATTR(edit_submode_highl) : 0);
5931 }
5932 else
5933 msg_clr_cmdline(); // necessary for "noshowmode"
Bram Moolenaarea389e92014-05-28 21:40:52 +02005934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936
Bram Moolenaard68071d2006-05-02 22:08:30 +00005937 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005938 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005939 show_pum(save_w_wrow, save_w_leftcol);
5940
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005941 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005942 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005943
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 return OK;
5945}
5946
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005947 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005948show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005949{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005950 /* RedrawingDisabled may be set when invoked through complete(). */
5951 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005952
Bram Moolenaarcb036422017-03-01 12:29:10 +01005953 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005954
Bram Moolenaarcb036422017-03-01 12:29:10 +01005955 /* If the cursor moved or the display scrolled we need to remove the pum
5956 * first. */
5957 setcursor();
5958 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5959 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005960
Bram Moolenaarcb036422017-03-01 12:29:10 +01005961 ins_compl_show_pum();
5962 setcursor();
5963 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005964}
5965
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966/*
5967 * Looks in the first "len" chars. of "src" for search-metachars.
5968 * If dest is not NULL the chars. are copied there quoting (with
5969 * a backslash) the metachars, and dest would be NUL terminated.
5970 * Returns the length (needed) of dest
5971 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005972 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005973quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005975 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005977 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 {
5979 switch (*src)
5980 {
5981 case '.':
5982 case '*':
5983 case '[':
5984 if (ctrl_x_mode == CTRL_X_DICTIONARY
5985 || ctrl_x_mode == CTRL_X_THESAURUS)
5986 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005987 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 case '~':
5989 if (!p_magic) /* quote these only if magic is set */
5990 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005991 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 case '\\':
5993 if (ctrl_x_mode == CTRL_X_DICTIONARY
5994 || ctrl_x_mode == CTRL_X_THESAURUS)
5995 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005996 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 case '^': /* currently it's not needed. */
5998 case '$':
5999 m++;
6000 if (dest != NULL)
6001 *dest++ = '\\';
6002 break;
6003 }
6004 if (dest != NULL)
6005 *dest++ = *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 /* Copy remaining bytes of a multibyte character. */
6007 if (has_mbyte)
6008 {
6009 int i, mb_len;
6010
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006011 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 if (mb_len > 0 && len >= mb_len)
6013 for (i = 0; i < mb_len; ++i)
6014 {
6015 --len;
6016 ++src;
6017 if (dest != NULL)
6018 *dest++ = *src;
6019 }
6020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 }
6022 if (dest != NULL)
6023 *dest = NUL;
6024
6025 return m;
6026}
6027#endif /* FEAT_INS_EXPAND */
6028
6029/*
6030 * Next character is interpreted literally.
6031 * A one, two or three digit decimal number is interpreted as its byte value.
6032 * If one or two digits are entered, the next character is given to vungetc().
6033 * For Unicode a character > 255 may be returned.
6034 */
6035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006036get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037{
6038 int cc;
6039 int nc;
6040 int i;
6041 int hex = FALSE;
6042 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044
6045 if (got_int)
6046 return Ctrl_C;
6047
6048#ifdef FEAT_GUI
6049 /*
6050 * In GUI there is no point inserting the internal code for a special key.
6051 * It is more useful to insert the string "<KEY>" instead. This would
6052 * probably be useful in a text window too, but it would not be
6053 * vi-compatible (maybe there should be an option for it?) -- webb
6054 */
6055 if (gui.in_use)
6056 ++allow_keys;
6057#endif
6058#ifdef USE_ON_FLY_SCROLL
6059 dont_scroll = TRUE; /* disallow scrolling here */
6060#endif
6061 ++no_mapping; /* don't map the next key hits */
6062 cc = 0;
6063 i = 0;
6064 for (;;)
6065 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006066 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01006068 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 add_to_showcmd(nc);
6070#endif
6071 if (nc == 'x' || nc == 'X')
6072 hex = TRUE;
6073 else if (nc == 'o' || nc == 'O')
6074 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 else if (nc == 'u' || nc == 'U')
6076 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 else
6078 {
Bram Moolenaar13505972019-01-24 15:04:48 +01006079 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 {
6081 if (!vim_isxdigit(nc))
6082 break;
6083 cc = cc * 16 + hex2nr(nc);
6084 }
6085 else if (octal)
6086 {
6087 if (nc < '0' || nc > '7')
6088 break;
6089 cc = cc * 8 + nc - '0';
6090 }
6091 else
6092 {
6093 if (!VIM_ISDIGIT(nc))
6094 break;
6095 cc = cc * 10 + nc - '0';
6096 }
6097
6098 ++i;
6099 }
6100
Bram Moolenaar13505972019-01-24 15:04:48 +01006101 if (cc > 255 && unicode == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 cc = 255; /* limit range to 0-255 */
6103 nc = 0;
6104
6105 if (hex) /* hex: up to two chars */
6106 {
6107 if (i >= 2)
6108 break;
6109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 else if (unicode) /* Unicode: up to four or eight chars */
6111 {
6112 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6113 break;
6114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 else if (i >= 3) /* decimal or octal: up to three chars */
6116 break;
6117 }
6118 if (i == 0) /* no number entered */
6119 {
6120 if (nc == K_ZERO) /* NUL is stored as NL */
6121 {
6122 cc = '\n';
6123 nc = 0;
6124 }
6125 else
6126 {
6127 cc = nc;
6128 nc = 0;
6129 }
6130 }
6131
6132 if (cc == 0) /* NUL is stored as NL */
6133 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006134 if (enc_dbcs && (cc & 0xff) == 0)
6135 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6136 second byte will cause trouble! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
6138 --no_mapping;
6139#ifdef FEAT_GUI
6140 if (gui.in_use)
6141 --allow_keys;
6142#endif
6143 if (nc)
6144 vungetc(nc);
6145 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6146 return cc;
6147}
6148
6149/*
6150 * Insert character, taking care of special keys and mod_mask
6151 */
6152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006153insert_special(
6154 int c,
6155 int allow_modmask,
6156 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157{
6158 char_u *p;
6159 int len;
6160
6161 /*
6162 * Special function key, translate into "<Key>". Up to the last '>' is
6163 * inserted with ins_str(), so as not to replace characters in replace
6164 * mode.
6165 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6166 * unless 'allow_modmask' is TRUE.
6167 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006168#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 /* Command-key never produces a normal key */
6170 if (mod_mask & MOD_MASK_CMD)
6171 allow_modmask = TRUE;
6172#endif
6173 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6174 {
6175 p = get_special_key_name(c, mod_mask);
6176 len = (int)STRLEN(p);
6177 c = p[len - 1];
6178 if (len > 2)
6179 {
6180 if (stop_arrow() == FAIL)
6181 return;
6182 p[len - 1] = NUL;
6183 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006184 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 ctrlv = FALSE;
6186 }
6187 }
6188 if (stop_arrow() == OK)
6189 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6190}
6191
6192/*
6193 * Special characters in this context are those that need processing other
6194 * than the simple insertion that can be performed here. This includes ESC
6195 * which terminates the insert, and CR/NL which need special processing to
6196 * open up a new line. This routine tries to optimize insertions performed by
6197 * the "redo", "undo" or "put" commands, so it needs to know when it should
6198 * stop and defer processing to the "normal" mechanism.
6199 * '0' and '^' are special, because they can be followed by CTRL-D.
6200 */
6201#ifdef EBCDIC
6202# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6203#else
6204# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6205#endif
6206
Bram Moolenaar13505972019-01-24 15:04:48 +01006207#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006209/*
6210 * "flags": INSCHAR_FORMAT - force formatting
6211 * INSCHAR_CTRLV - char typed just after CTRL-V
6212 * INSCHAR_NO_FEX - don't use 'formatexpr'
6213 *
6214 * NOTE: passes the flags value straight through to internal_format() which,
6215 * beside INSCHAR_FORMAT (above), is also looking for these:
6216 * INSCHAR_DO_COM - format comments
6217 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006220insertchar(
6221 int c, /* character to insert or NUL */
6222 int flags, /* INSCHAR_FORMAT, etc. */
6223 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 int textwidth;
6226#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006230 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006232 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234
6235 /*
6236 * Try to break the line in two or more pieces when:
6237 * - Always do this if we have been called to do formatting only.
6238 * - Always do this when 'formatoptions' has the 'a' flag and the line
6239 * ends in white space.
6240 * - Otherwise:
6241 * - Don't do this if inserting a blank
6242 * - Don't do this if an existing character is being replaced, unless
6243 * we're in VREPLACE mode.
6244 * - Do this if the cursor is not on the line where insert started
6245 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6246 * before the insert.
6247 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6248 * before 'textwidth'
6249 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006250 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006251 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006252 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 && *ml_get_cursor() != NUL)
6256 && (curwin->w_cursor.lnum != Insstart.lnum
6257 || ((!has_format_option(FO_INS_LONG)
6258 || Insstart_textlen <= (colnr_T)textwidth)
6259 && (!fo_ins_blank
6260 || Insstart_blank_vcol <= (colnr_T)textwidth
6261 ))))))
6262 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006263 /* Format with 'formatexpr' when it's set. Use internal formatting
6264 * when 'formatexpr' isn't set or it returns non-zero. */
6265#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006266 int do_internal = TRUE;
6267 colnr_T virtcol = get_nolist_virtcol()
6268 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006269
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006270 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6271 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006272 {
6273 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6274 /* It may be required to save for undo again, e.g. when setline()
6275 * was called. */
6276 ins_need_undo = TRUE;
6277 }
6278 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006280 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006282
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 if (c == NUL) /* only formatting was wanted */
6284 return;
6285
6286#ifdef FEAT_COMMENTS
6287 /* Check whether this character should end a comment. */
6288 if (did_ai && (int)c == end_comment_pending)
6289 {
6290 char_u *line;
6291 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6292 int middle_len, end_len;
6293 int i;
6294
6295 /*
6296 * Need to remove existing (middle) comment leader and insert end
6297 * comment leader. First, check what comment leader we can find.
6298 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006299 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6301 {
6302 /* Skip middle-comment string */
6303 while (*p && p[-1] != ':') /* find end of middle flags */
6304 ++p;
6305 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6306 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006307 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 --middle_len;
6309
6310 /* Find the end-comment string */
6311 while (*p && p[-1] != ':') /* find end of end flags */
6312 ++p;
6313 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6314
6315 /* Skip white space before the cursor */
6316 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006317 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 ;
6319 i++;
6320
6321 /* Skip to before the middle leader */
6322 i -= middle_len;
6323
6324 /* Check some expected things before we go on */
6325 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6326 {
6327 /* Backspace over all the stuff we want to replace */
6328 backspace_until_column(i);
6329
6330 /*
6331 * Insert the end-comment string, except for the last
6332 * character, which will get inserted as normal later.
6333 */
6334 ins_bytes_len(lead_end, end_len - 1);
6335 }
6336 }
6337 }
6338 end_comment_pending = NUL;
6339#endif
6340
6341 did_ai = FALSE;
6342#ifdef FEAT_SMARTINDENT
6343 did_si = FALSE;
6344 can_si = FALSE;
6345 can_si_back = FALSE;
6346#endif
6347
6348 /*
6349 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6350 * This speeds up normal text input considerably.
6351 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6352 * need to re-indent at a ':', or any other character (but not what
6353 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006354 * Don't do this when there an InsertCharPre autocommand is defined,
6355 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006356 * Do the check for InsertCharPre before the call to vpeekc() because the
6357 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 */
6359#ifdef USE_ON_FLY_SCROLL
6360 dont_scroll = FALSE; /* allow scrolling here */
6361#endif
6362
6363 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02006365 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 && vpeekc() != NUL
6367 && !(State & REPLACE_FLAG)
6368#ifdef FEAT_CINDENT
6369 && !cindent_on()
6370#endif
6371#ifdef FEAT_RIGHTLEFT
6372 && !p_ri
6373#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006374 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
6376#define INPUT_BUFLEN 100
6377 char_u buf[INPUT_BUFLEN + 1];
6378 int i;
6379 colnr_T virtcol = 0;
6380
6381 buf[0] = c;
6382 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006383 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 virtcol = get_nolist_virtcol();
6385 /*
6386 * Stop the string when:
6387 * - no more chars available
6388 * - finding a special character (command key)
6389 * - buffer is full
6390 * - running into the 'textwidth' boundary
6391 * - need to check for abbreviation: A non-word char after a word-char
6392 */
6393 while ( (c = vpeekc()) != NUL
6394 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 && i < INPUT_BUFLEN
6397 && (textwidth == 0
6398 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6399 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6400 {
6401#ifdef FEAT_RIGHTLEFT
6402 c = vgetc();
6403 if (p_hkmap && KeyTyped)
6404 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 buf[i++] = c;
6406#else
6407 buf[i++] = vgetc();
6408#endif
6409 }
6410
6411#ifdef FEAT_DIGRAPHS
6412 do_digraph(-1); /* clear digraphs */
6413 do_digraph(buf[i-1]); /* may be the start of a digraph */
6414#endif
6415 buf[i] = NUL;
6416 ins_str(buf);
6417 if (flags & INSCHAR_CTRLV)
6418 {
6419 redo_literal(*buf);
6420 i = 1;
6421 }
6422 else
6423 i = 0;
6424 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006425 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 }
6427 else
6428 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006429 int cc;
6430
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6432 {
6433 char_u buf[MB_MAXBYTES + 1];
6434
6435 (*mb_char2bytes)(c, buf);
6436 buf[cc] = NUL;
6437 ins_char_bytes(buf, cc);
6438 AppendCharToRedobuff(c);
6439 }
6440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 {
6442 ins_char(c);
6443 if (flags & INSCHAR_CTRLV)
6444 redo_literal(c);
6445 else
6446 AppendCharToRedobuff(c);
6447 }
6448 }
6449}
6450
6451/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006452 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006453 *
6454 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6455 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006456 */
6457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006458internal_format(
6459 int textwidth,
6460 int second_indent,
6461 int flags,
6462 int format_only,
6463 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006464{
6465 int cc;
6466 int save_char = NUL;
6467 int haveto_redraw = FALSE;
6468 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006469 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006470 int fo_white_par = has_format_option(FO_WHITE_PAR);
6471 int first_line = TRUE;
6472#ifdef FEAT_COMMENTS
6473 colnr_T leader_len;
6474 int no_leader = FALSE;
6475 int do_comments = (flags & INSCHAR_DO_COM);
6476#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006477#ifdef FEAT_LINEBREAK
6478 int has_lbr = curwin->w_p_lbr;
6479
6480 /* make sure win_lbr_chartabsize() counts correctly */
6481 curwin->w_p_lbr = FALSE;
6482#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006483
6484 /*
6485 * When 'ai' is off we don't want a space under the cursor to be
6486 * deleted. Replace it with an 'x' temporarily.
6487 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006488 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006489 {
6490 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006491 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006492 {
6493 save_char = cc;
6494 pchar_cursor('x');
6495 }
6496 }
6497
6498 /*
6499 * Repeat breaking lines, until the current line is not too long.
6500 */
6501 while (!got_int)
6502 {
6503 int startcol; /* Cursor column at entry */
6504 int wantcol; /* column at textwidth border */
6505 int foundcol; /* column for start of spaces */
6506 int end_foundcol = 0; /* column for start of word */
6507 colnr_T len;
6508 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006509 int orig_col = 0;
6510 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006511 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006512 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006513 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006514
Bram Moolenaar97b98102009-11-17 16:41:01 +00006515 virtcol = get_nolist_virtcol()
6516 + char2cells(c != NUL ? c : gchar_cursor());
6517 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006518 break;
6519
6520#ifdef FEAT_COMMENTS
6521 if (no_leader)
6522 do_comments = FALSE;
6523 else if (!(flags & INSCHAR_FORMAT)
6524 && has_format_option(FO_WRAP_COMS))
6525 do_comments = TRUE;
6526
6527 /* Don't break until after the comment leader */
6528 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006529 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006530 else
6531 leader_len = 0;
6532
6533 /* If the line doesn't start with a comment leader, then don't
6534 * start one in a following broken line. Avoids that a %word
6535 * moved to the start of the next line causes all following lines
6536 * to start with %. */
6537 if (leader_len == 0)
6538 no_leader = TRUE;
6539#endif
6540 if (!(flags & INSCHAR_FORMAT)
6541#ifdef FEAT_COMMENTS
6542 && leader_len == 0
6543#endif
6544 && !has_format_option(FO_WRAP))
6545
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006546 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006547 if ((startcol = curwin->w_cursor.col) == 0)
6548 break;
6549
6550 /* find column of textwidth border */
6551 coladvance((colnr_T)textwidth);
6552 wantcol = curwin->w_cursor.col;
6553
Bram Moolenaar97b98102009-11-17 16:41:01 +00006554 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006555 foundcol = 0;
6556
6557 /*
6558 * Find position to break at.
6559 * Stop at first entered white when 'formatoptions' has 'v'
6560 */
6561 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006562 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006563 || curwin->w_cursor.lnum != Insstart.lnum
6564 || curwin->w_cursor.col >= Insstart.col)
6565 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006566 if (curwin->w_cursor.col == startcol && c != NUL)
6567 cc = c;
6568 else
6569 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006570 if (WHITECHAR(cc))
6571 {
6572 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006573 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006574
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006575 // find start of sequence of blanks
6576 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006577 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6578 {
6579 dec_cursor();
6580 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006581
6582 // Increment count of how many whitespace chars in this
6583 // group; we only need to know if it's more than one.
6584 if (wcc < 2)
6585 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006586 }
6587 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6588 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006589
6590 // Don't break after a period when 'formatoptions' has 'p' and
6591 // there are less than two spaces.
6592 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
6593 continue;
6594
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006595#ifdef FEAT_COMMENTS
6596 /* Don't break until after the comment leader */
6597 if (curwin->w_cursor.col < leader_len)
6598 break;
6599#endif
6600 if (has_format_option(FO_ONE_LETTER))
6601 {
6602 /* do not break after one-letter words */
6603 if (curwin->w_cursor.col == 0)
6604 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006605#ifdef FEAT_COMMENTS
6606 /* do not break "#a b" when 'tw' is 2 */
6607 if (curwin->w_cursor.col <= leader_len)
6608 break;
6609#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006610 col = curwin->w_cursor.col;
6611 dec_cursor();
6612 cc = gchar_cursor();
6613
6614 if (WHITECHAR(cc))
6615 continue; /* one-letter, continue */
6616 curwin->w_cursor.col = col;
6617 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006618
6619 inc_cursor();
6620
6621 end_foundcol = end_col + 1;
6622 foundcol = curwin->w_cursor.col;
6623 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006624 break;
6625 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006626 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006627 {
6628 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006629 if (curwin->w_cursor.col != startcol)
6630 {
6631#ifdef FEAT_COMMENTS
6632 /* Don't break until after the comment leader */
6633 if (curwin->w_cursor.col < leader_len)
6634 break;
6635#endif
6636 col = curwin->w_cursor.col;
6637 inc_cursor();
6638 /* Don't change end_foundcol if already set. */
6639 if (foundcol != curwin->w_cursor.col)
6640 {
6641 foundcol = curwin->w_cursor.col;
6642 end_foundcol = foundcol;
6643 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6644 break;
6645 }
6646 curwin->w_cursor.col = col;
6647 }
6648
6649 if (curwin->w_cursor.col == 0)
6650 break;
6651
6652 col = curwin->w_cursor.col;
6653
6654 dec_cursor();
6655 cc = gchar_cursor();
6656
6657 if (WHITECHAR(cc))
6658 continue; /* break with space */
6659#ifdef FEAT_COMMENTS
6660 /* Don't break until after the comment leader */
6661 if (curwin->w_cursor.col < leader_len)
6662 break;
6663#endif
6664
6665 curwin->w_cursor.col = col;
6666
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006667 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006668 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006669 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6670 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006671 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006672 if (curwin->w_cursor.col == 0)
6673 break;
6674 dec_cursor();
6675 }
6676
6677 if (foundcol == 0) /* no spaces, cannot break line */
6678 {
6679 curwin->w_cursor.col = startcol;
6680 break;
6681 }
6682
6683 /* Going to break the line, remove any "$" now. */
6684 undisplay_dollar();
6685
6686 /*
6687 * Offset between cursor position and line break is used by replace
6688 * stack functions. VREPLACE does not use this, and backspaces
6689 * over the text instead.
6690 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006691 if (State & VREPLACE_FLAG)
6692 orig_col = startcol; /* Will start backspacing from here */
6693 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006694 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006695
6696 /*
6697 * adjust startcol for spaces that will be deleted and
6698 * characters that will remain on top line
6699 */
6700 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006701 while ((cc = gchar_cursor(), WHITECHAR(cc))
6702 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006703 inc_cursor();
6704 startcol -= curwin->w_cursor.col;
6705 if (startcol < 0)
6706 startcol = 0;
6707
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006708 if (State & VREPLACE_FLAG)
6709 {
6710 /*
6711 * In VREPLACE mode, we will backspace over the text to be
6712 * wrapped, so save a copy now to put on the next line.
6713 */
6714 saved_text = vim_strsave(ml_get_cursor());
6715 curwin->w_cursor.col = orig_col;
6716 if (saved_text == NULL)
6717 break; /* Can't do it, out of memory */
6718 saved_text[startcol] = NUL;
6719
6720 /* Backspace over characters that will move to the next line */
6721 if (!fo_white_par)
6722 backspace_until_column(foundcol);
6723 }
6724 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006725 {
6726 /* put cursor after pos. to break line */
6727 if (!fo_white_par)
6728 curwin->w_cursor.col = foundcol;
6729 }
6730
6731 /*
6732 * Split the line just before the margin.
6733 * Only insert/delete lines, but don't really redraw the window.
6734 */
6735 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6736 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6737#ifdef FEAT_COMMENTS
6738 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006739 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006740#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006741 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6742 if (!(flags & INSCHAR_COM_LIST))
6743 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006744
6745 replace_offset = 0;
6746 if (first_line)
6747 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006748 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006749 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006750 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006751 * This section is for auto-wrap of numeric lists. When not
6752 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6753 * flag will be set and open_line() will handle it (as seen
6754 * above). The code here (and in get_number_indent()) will
6755 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006756 */
6757 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006758 second_indent =
6759 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006760 if (second_indent >= 0)
6761 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006762 if (State & VREPLACE_FLAG)
6763 change_indent(INDENT_SET, second_indent,
6764 FALSE, NUL, TRUE);
6765 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006766#ifdef FEAT_COMMENTS
6767 if (leader_len > 0 && second_indent - leader_len > 0)
6768 {
6769 int i;
6770 int padding = second_indent - leader_len;
6771
6772 /* We started at the first_line of a numbered list
6773 * that has a comment. the open_line() function has
6774 * inserted the proper comment leader and positioned
6775 * the cursor at the end of the split line. Now we
6776 * add the additional whitespace needed after the
6777 * comment leader for the numbered list. */
6778 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006779 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006780 }
6781 else
6782 {
6783#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006784 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006785#ifdef FEAT_COMMENTS
6786 }
6787#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006788 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006789 }
6790 first_line = FALSE;
6791 }
6792
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006793 if (State & VREPLACE_FLAG)
6794 {
6795 /*
6796 * In VREPLACE mode we have backspaced over the text to be
6797 * moved, now we re-insert it into the new line.
6798 */
6799 ins_bytes(saved_text);
6800 vim_free(saved_text);
6801 }
6802 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006803 {
6804 /*
6805 * Check if cursor is not past the NUL off the line, cindent
6806 * may have added or removed indent.
6807 */
6808 curwin->w_cursor.col += startcol;
6809 len = (colnr_T)STRLEN(ml_get_curline());
6810 if (curwin->w_cursor.col > len)
6811 curwin->w_cursor.col = len;
6812 }
6813
6814 haveto_redraw = TRUE;
6815#ifdef FEAT_CINDENT
6816 can_cindent = TRUE;
6817#endif
6818 /* moved the cursor, don't autoindent or cindent now */
6819 did_ai = FALSE;
6820#ifdef FEAT_SMARTINDENT
6821 did_si = FALSE;
6822 can_si = FALSE;
6823 can_si_back = FALSE;
6824#endif
6825 line_breakcheck();
6826 }
6827
6828 if (save_char != NUL) /* put back space after cursor */
6829 pchar_cursor(save_char);
6830
Bram Moolenaar0026d472014-09-09 16:32:39 +02006831#ifdef FEAT_LINEBREAK
6832 curwin->w_p_lbr = has_lbr;
6833#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006834 if (!format_only && haveto_redraw)
6835 {
6836 update_topline();
6837 redraw_curbuf_later(VALID);
6838 }
6839}
6840
6841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 * Called after inserting or deleting text: When 'formatoptions' includes the
6843 * 'a' flag format from the current line until the end of the paragraph.
6844 * Keep the cursor at the same position relative to the text.
6845 * The caller must have saved the cursor line for undo, following ones will be
6846 * saved here.
6847 */
6848 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006849auto_format(
6850 int trailblank, /* when TRUE also format with trailing blank */
6851 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
6853 pos_T pos;
6854 colnr_T len;
6855 char_u *old;
6856 char_u *new, *pnew;
6857 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006858 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859
6860 if (!has_format_option(FO_AUTO))
6861 return;
6862
6863 pos = curwin->w_cursor;
6864 old = ml_get_curline();
6865
6866 /* may remove added space */
6867 check_auto_format(FALSE);
6868
6869 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6870 * user might insert normal text next. Also skip formatting when "1" is
6871 * in 'formatoptions' and there is a single character before the cursor.
6872 * Otherwise the line would be broken and when typing another non-white
6873 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006874 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 if (*old != NUL && !trailblank && wasatend)
6876 {
6877 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006878 cc = gchar_cursor();
6879 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6880 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006882 cc = gchar_cursor();
6883 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {
6885 curwin->w_cursor = pos;
6886 return;
6887 }
6888 curwin->w_cursor = pos;
6889 }
6890
6891#ifdef FEAT_COMMENTS
6892 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6893 * comments. */
6894 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006895 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 return;
6897#endif
6898
6899 /*
6900 * May start formatting in a previous line, so that after "x" a word is
6901 * moved to the previous line if it fits there now. Only when this is not
6902 * the start of a paragraph.
6903 */
6904 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6905 {
6906 --curwin->w_cursor.lnum;
6907 if (u_save_cursor() == FAIL)
6908 return;
6909 }
6910
6911 /*
6912 * Do the formatting and restore the cursor position. "saved_cursor" will
6913 * be adjusted for the text formatting.
6914 */
6915 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006916 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 curwin->w_cursor = saved_cursor;
6918 saved_cursor.lnum = 0;
6919
6920 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6921 {
6922 /* "cannot happen" */
6923 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6924 coladvance((colnr_T)MAXCOL);
6925 }
6926 else
6927 check_cursor_col();
6928
6929 /* Insert mode: If the cursor is now after the end of the line while it
6930 * previously wasn't, the line was broken. Because of the rule above we
6931 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6932 * formatted. */
6933 if (!wasatend && has_format_option(FO_WHITE_PAR))
6934 {
6935 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006936 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 if (curwin->w_cursor.col == len)
6938 {
6939 pnew = vim_strnsave(new, len + 2);
6940 pnew[len] = ' ';
6941 pnew[len + 1] = NUL;
6942 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6943 /* remove the space later */
6944 did_add_space = TRUE;
6945 }
6946 else
6947 /* may remove added space */
6948 check_auto_format(FALSE);
6949 }
6950
6951 check_cursor();
6952}
6953
6954/*
6955 * When an extra space was added to continue a paragraph for auto-formatting,
6956 * delete it now. The space must be under the cursor, just after the insert
6957 * position.
6958 */
6959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006960check_auto_format(
6961 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962{
6963 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006964 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965
6966 if (did_add_space)
6967 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006968 cc = gchar_cursor();
6969 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 /* Somehow the space was removed already. */
6971 did_add_space = FALSE;
6972 else
6973 {
6974 if (!end_insert)
6975 {
6976 inc_cursor();
6977 c = gchar_cursor();
6978 dec_cursor();
6979 }
6980 if (c != NUL)
6981 {
6982 /* The space is no longer at the end of the line, delete it. */
6983 del_char(FALSE);
6984 did_add_space = FALSE;
6985 }
6986 }
6987 }
6988}
6989
6990/*
6991 * Find out textwidth to be used for formatting:
6992 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006993 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 * if invalid value, use 0.
6995 * Set default to window width (maximum 79) for "gq" operator.
6996 */
6997 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006998comp_textwidth(
6999 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000{
7001 int textwidth;
7002
7003 textwidth = curbuf->b_p_tw;
7004 if (textwidth == 0 && curbuf->b_p_wm)
7005 {
7006 /* The width is the window width minus 'wrapmargin' minus all the
7007 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02007008 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009#ifdef FEAT_CMDWIN
7010 if (cmdwin_type != 0)
7011 textwidth -= 1;
7012#endif
7013#ifdef FEAT_FOLDING
7014 textwidth -= curwin->w_p_fdc;
7015#endif
7016#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007017 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 textwidth -= 1;
7019#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02007020 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 textwidth -= 8;
7022 }
7023 if (textwidth < 0)
7024 textwidth = 0;
7025 if (ff && textwidth == 0)
7026 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007027 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 if (textwidth > 79)
7029 textwidth = 79;
7030 }
7031 return textwidth;
7032}
7033
7034/*
7035 * Put a character in the redo buffer, for when just after a CTRL-V.
7036 */
7037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007038redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039{
7040 char_u buf[10];
7041
7042 /* Only digits need special treatment. Translate them into a string of
7043 * three digits. */
7044 if (VIM_ISDIGIT(c))
7045 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007046 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 AppendToRedobuff(buf);
7048 }
7049 else
7050 AppendCharToRedobuff(c);
7051}
7052
7053/*
7054 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007055 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 */
7057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007058start_arrow(
7059 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007061 start_arrow_common(end_insert_pos, TRUE);
7062}
7063
7064/*
7065 * Like start_arrow() but with end_change argument.
7066 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7067 */
7068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007069start_arrow_with_change(
7070 pos_T *end_insert_pos, /* can be NULL */
7071 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007072{
7073 start_arrow_common(end_insert_pos, end_change);
7074 if (!end_change)
7075 {
7076 AppendCharToRedobuff(Ctrl_G);
7077 AppendCharToRedobuff('U');
7078 }
7079}
7080
7081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007082start_arrow_common(
7083 pos_T *end_insert_pos, /* can be NULL */
7084 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007085{
7086 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {
7088 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007089 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 arrow_used = TRUE; /* this means we stopped the current insert */
7091 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007092#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007093 check_spell_redraw();
7094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095}
7096
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007097#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007098/*
7099 * If we skipped highlighting word at cursor, do it now.
7100 * It may be skipped again, thus reset spell_redraw_lnum first.
7101 */
7102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007103check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007104{
7105 if (spell_redraw_lnum != 0)
7106 {
7107 linenr_T lnum = spell_redraw_lnum;
7108
7109 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01007110 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00007111 }
7112}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007113
7114/*
7115 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7116 * spelled word, if there is one.
7117 */
7118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007119spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007120{
7121 pos_T tpos = curwin->w_cursor;
7122
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007123 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007124 if (curwin->w_cursor.col != tpos.col)
7125 start_arrow(&tpos);
7126}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007127#endif
7128
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129/*
7130 * stop_arrow() is called before a change is made in insert mode.
7131 * If an arrow key has been used, start a new insertion.
7132 * Returns FAIL if undo is impossible, shouldn't insert then.
7133 */
7134 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007135stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136{
7137 if (arrow_used)
7138 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007139 Insstart = curwin->w_cursor; /* new insertion starts here */
7140 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7141 /* Don't update the original insert position when moved to the
7142 * right, except when nothing was inserted yet. */
7143 update_Insstart_orig = FALSE;
7144 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7145
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (u_save_cursor() == OK)
7147 {
7148 arrow_used = FALSE;
7149 ins_need_undo = FALSE;
7150 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007151
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 if (State & VREPLACE_FLAG)
7154 {
7155 orig_line_count = curbuf->b_ml.ml_line_count;
7156 vr_lines_changed = 1;
7157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 ResetRedobuff();
7159 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007160 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 }
7162 else if (ins_need_undo)
7163 {
7164 if (u_save_cursor() == OK)
7165 ins_need_undo = FALSE;
7166 }
7167
7168#ifdef FEAT_FOLDING
7169 /* Always open fold at the cursor line when inserting something. */
7170 foldOpenCursor();
7171#endif
7172
7173 return (arrow_used || ins_need_undo ? FAIL : OK);
7174}
7175
7176/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007177 * Do a few things to stop inserting.
7178 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7179 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 */
7181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007182stop_insert(
7183 pos_T *end_insert_pos,
7184 int esc, /* called by ins_esc() */
7185 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007187 int cc;
7188 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
7190 stop_redo_ins();
7191 replace_flush(); /* abandon replace stack */
7192
7193 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007194 * Save the inserted text for later redo with ^@ and CTRL-A.
7195 * Don't do it when "restart_edit" was set and nothing was inserted,
7196 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007198 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007199 if (did_restart_edit == 0 || (ptr != NULL
7200 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007201 {
7202 vim_free(last_insert);
7203 last_insert = ptr;
7204 last_insert_skip = new_insert_skip;
7205 }
7206 else
7207 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007209 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 {
7211 /* Auto-format now. It may seem strange to do this when stopping an
7212 * insertion (or moving the cursor), but it's required when appending
7213 * a line and having it end in a space. But only do it when something
7214 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007215 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007217 pos_T tpos = curwin->w_cursor;
7218
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 /* When the cursor is at the end of the line after a space the
7220 * formatting will move it to the following word. Avoid that by
7221 * moving the cursor onto the space. */
7222 cc = 'x';
7223 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7224 {
7225 dec_cursor();
7226 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007227 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007228 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 }
7230
7231 auto_format(TRUE, FALSE);
7232
Bram Moolenaar1c465442017-03-12 20:10:05 +01007233 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007234 {
7235 if (gchar_cursor() != NUL)
7236 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007237 // If the cursor is still at the same character, also keep
7238 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007239 if (gchar_cursor() == NUL
7240 && curwin->w_cursor.lnum == tpos.lnum
7241 && curwin->w_cursor.col == tpos.col)
7242 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245
7246 /* If a space was inserted for auto-formatting, remove it now. */
7247 check_auto_format(TRUE);
7248
7249 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007250 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007251 * Do this when ESC was used or moving the cursor up/down.
7252 * Check for the old position still being valid, just in case the text
7253 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007254 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007255 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7256 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007258 pos_T tpos = curwin->w_cursor;
7259
7260 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007261 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007262 for (;;)
7263 {
7264 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7265 --curwin->w_cursor.col;
7266 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007267 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007268 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007269 if (del_char(TRUE) == FAIL)
7270 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007271 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007272 if (curwin->w_cursor.lnum != tpos.lnum)
7273 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007274 else
7275 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007276 /* reset tpos, could have been invalidated in the loop above */
7277 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007278 tpos.col++;
7279 if (cc != NUL && gchar_pos(&tpos) == NUL)
7280 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 /* <C-S-Right> may have started Visual mode, adjust the position for
7284 * deleted characters. */
7285 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7286 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007287 int len = (int)STRLEN(ml_get_curline());
7288
7289 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007291 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 }
7294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 }
7296 }
7297 did_ai = FALSE;
7298#ifdef FEAT_SMARTINDENT
7299 did_si = FALSE;
7300 can_si = FALSE;
7301 can_si_back = FALSE;
7302#endif
7303
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007304 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7305 * now in a different buffer. */
7306 if (end_insert_pos != NULL)
7307 {
7308 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007309 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007310 curbuf->b_op_end = *end_insert_pos;
7311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312}
7313
7314/*
7315 * Set the last inserted text to a single character.
7316 * Used for the replace command.
7317 */
7318 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007319set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321 char_u *s;
7322
7323 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 if (last_insert != NULL)
7326 {
7327 s = last_insert;
7328 /* Use the CTRL-V only when entering a special char */
7329 if (c < ' ' || c == DEL)
7330 *s++ = Ctrl_V;
7331 s = add_char2buf(c, s);
7332 *s++ = ESC;
7333 *s++ = NUL;
7334 last_insert_skip = 0;
7335 }
7336}
7337
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007338#if defined(EXITFREE) || defined(PROTO)
7339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007340free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007341{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007342 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007343# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007344 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007345# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007346}
7347#endif
7348
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349/*
7350 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7351 * and CSI. Handle multi-byte characters.
7352 * Returns a pointer to after the added bytes.
7353 */
7354 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007355add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007357 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 int i;
7359 int len;
7360
7361 len = (*mb_char2bytes)(c, temp);
7362 for (i = 0; i < len; ++i)
7363 {
7364 c = temp[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7366 if (c == K_SPECIAL)
7367 {
7368 *s++ = K_SPECIAL;
7369 *s++ = KS_SPECIAL;
7370 *s++ = KE_FILLER;
7371 }
7372#ifdef FEAT_GUI
7373 else if (c == CSI)
7374 {
7375 *s++ = CSI;
7376 *s++ = KS_EXTRA;
7377 *s++ = (int)KE_CSI;
7378 }
7379#endif
7380 else
7381 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 return s;
7384}
7385
7386/*
7387 * move cursor to start of line
7388 * if flags & BL_WHITE move to first non-white
7389 * if flags & BL_SOL move to first non-white if startofline is set,
7390 * otherwise keep "curswant" column
7391 * if flags & BL_FIX don't leave the cursor on a NUL.
7392 */
7393 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007394beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 if ((flags & BL_SOL) && !p_sol)
7397 coladvance(curwin->w_curswant);
7398 else
7399 {
7400 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
7403 if (flags & (BL_WHITE | BL_SOL))
7404 {
7405 char_u *ptr;
7406
Bram Moolenaar1c465442017-03-12 20:10:05 +01007407 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7409 ++curwin->w_cursor.col;
7410 }
7411 curwin->w_set_curswant = TRUE;
7412 }
7413}
7414
7415/*
7416 * oneright oneleft cursor_down cursor_up
7417 *
7418 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007419 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 * Return OK when successful, FAIL when we hit a line of file boundary.
7421 */
7422
7423 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007424oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 if (virtual_active())
7430 {
7431 pos_T prevpos = curwin->w_cursor;
7432
7433 /* Adjust for multi-wide char (excluding TAB) */
7434 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01007435 coladvance(getviscol() + ((*ptr != TAB
7436 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 ? ptr2cells(ptr) : 1));
7438 curwin->w_set_curswant = TRUE;
7439 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7440 return (prevpos.col != curwin->w_cursor.col
7441 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443
7444 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007445 if (*ptr == NUL)
7446 return FAIL; /* already at the very end */
7447
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007448 if (has_mbyte)
7449 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007451 l = 1;
7452
7453 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7454 * contains "onemore". */
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007455 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007456 return FAIL;
7457 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
7459 curwin->w_set_curswant = TRUE;
7460 return OK;
7461}
7462
7463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007464oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 if (virtual_active())
7467 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007468#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 int v = getviscol();
7472
7473 if (v == 0)
7474 return FAIL;
7475
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007476#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 /* 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 Moolenaar13505972019-01-24 15:04:48 +01007486 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 break;
7488 ++width;
7489 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007490#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
7494 if (curwin->w_cursor.coladd == 1)
7495 {
7496 char_u *ptr;
7497
7498 /* Adjust for multi-wide char (not a TAB) */
7499 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01007500 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
7501 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 curwin->w_cursor.coladd = 0;
7503 }
7504
7505 curwin->w_set_curswant = TRUE;
7506 return OK;
7507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508
7509 if (curwin->w_cursor.col == 0)
7510 return FAIL;
7511
7512 curwin->w_set_curswant = TRUE;
7513 --curwin->w_cursor.col;
7514
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 /* if the character on the left of the current cursor is a multi-byte
7516 * character, move to its first byte */
7517 if (has_mbyte)
7518 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 return OK;
7520}
7521
7522 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007523cursor_up(
7524 long n,
7525 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526{
7527 linenr_T lnum;
7528
7529 if (n > 0)
7530 {
7531 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007532 /* This fails if the cursor is already in the first line or the count
7533 * is larger than the line number and '-' is in 'cpoptions' */
7534 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 return FAIL;
7536 if (n >= lnum)
7537 lnum = 1;
7538 else
7539#ifdef FEAT_FOLDING
7540 if (hasAnyFolding(curwin))
7541 {
7542 /*
7543 * Count each sequence of folded lines as one logical line.
7544 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007545 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 (void)hasFolding(lnum, &lnum, NULL);
7547
7548 while (n--)
7549 {
7550 /* move up one line */
7551 --lnum;
7552 if (lnum <= 1)
7553 break;
7554 /* If we entered a fold, move to the beginning, unless in
7555 * Insert mode or when 'foldopen' contains "all": it will open
7556 * in a moment. */
7557 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7558 (void)hasFolding(lnum, &lnum, NULL);
7559 }
7560 if (lnum < 1)
7561 lnum = 1;
7562 }
7563 else
7564#endif
7565 lnum -= n;
7566 curwin->w_cursor.lnum = lnum;
7567 }
7568
7569 /* try to advance to the column we want to be at */
7570 coladvance(curwin->w_curswant);
7571
7572 if (upd_topline)
7573 update_topline(); /* make sure curwin->w_topline is valid */
7574
7575 return OK;
7576}
7577
7578/*
7579 * Cursor down a number of logical lines.
7580 */
7581 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007582cursor_down(
7583 long n,
7584 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585{
7586 linenr_T lnum;
7587
7588 if (n > 0)
7589 {
7590 lnum = curwin->w_cursor.lnum;
7591#ifdef FEAT_FOLDING
7592 /* Move to last line of fold, will fail if it's the end-of-file. */
7593 (void)hasFolding(lnum, NULL, &lnum);
7594#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007595 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007596 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007597 if (lnum >= curbuf->b_ml.ml_line_count
7598 || (lnum + n > curbuf->b_ml.ml_line_count
7599 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 return FAIL;
7601 if (lnum + n >= curbuf->b_ml.ml_line_count)
7602 lnum = curbuf->b_ml.ml_line_count;
7603 else
7604#ifdef FEAT_FOLDING
7605 if (hasAnyFolding(curwin))
7606 {
7607 linenr_T last;
7608
7609 /* count each sequence of folded lines as one logical line */
7610 while (n--)
7611 {
7612 if (hasFolding(lnum, NULL, &last))
7613 lnum = last + 1;
7614 else
7615 ++lnum;
7616 if (lnum >= curbuf->b_ml.ml_line_count)
7617 break;
7618 }
7619 if (lnum > curbuf->b_ml.ml_line_count)
7620 lnum = curbuf->b_ml.ml_line_count;
7621 }
7622 else
7623#endif
7624 lnum += n;
7625 curwin->w_cursor.lnum = lnum;
7626 }
7627
7628 /* try to advance to the column we want to be at */
7629 coladvance(curwin->w_curswant);
7630
7631 if (upd_topline)
7632 update_topline(); /* make sure curwin->w_topline is valid */
7633
7634 return OK;
7635}
7636
7637/*
7638 * Stuff the last inserted text in the read buffer.
7639 * Last_insert actually is a copy of the redo buffer, so we
7640 * first have to remove the command.
7641 */
7642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007643stuff_inserted(
7644 int c, /* Command character to be inserted */
7645 long count, /* Repeat this many times */
7646 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
7648 char_u *esc_ptr;
7649 char_u *ptr;
7650 char_u *last_ptr;
7651 char_u last = NUL;
7652
7653 ptr = get_last_insert();
7654 if (ptr == NULL)
7655 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007656 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 return FAIL;
7658 }
7659
7660 /* may want to stuff the command character, to start Insert mode */
7661 if (c != NUL)
7662 stuffcharReadbuff(c);
7663 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7664 *esc_ptr = NUL; /* remove the ESC */
7665
7666 /* when the last char is either "0" or "^" it will be quoted if no ESC
7667 * comes after it OR if it will inserted more than once and "ptr"
7668 * starts with ^D. -- Acevedo
7669 */
7670 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7671 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7672 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7673 {
7674 last = *last_ptr;
7675 *last_ptr = NUL;
7676 }
7677
7678 do
7679 {
7680 stuffReadbuff(ptr);
7681 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7682 if (last)
7683 stuffReadbuff((char_u *)(last == '0'
7684 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7685 : IF_EB("\026^", CTRL_V_STR "^")));
7686 }
7687 while (--count > 0);
7688
7689 if (last)
7690 *last_ptr = last;
7691
7692 if (esc_ptr != NULL)
7693 *esc_ptr = ESC; /* put the ESC back */
7694
7695 /* may want to stuff a trailing ESC, to get out of Insert mode */
7696 if (!no_esc)
7697 stuffcharReadbuff(ESC);
7698
7699 return OK;
7700}
7701
7702 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007703get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
7705 if (last_insert == NULL)
7706 return NULL;
7707 return last_insert + last_insert_skip;
7708}
7709
7710/*
7711 * Get last inserted string, and remove trailing <Esc>.
7712 * Returns pointer to allocated memory (must be freed) or NULL.
7713 */
7714 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007715get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
7717 char_u *s;
7718 int len;
7719
7720 if (last_insert == NULL)
7721 return NULL;
7722 s = vim_strsave(last_insert + last_insert_skip);
7723 if (s != NULL)
7724 {
7725 len = (int)STRLEN(s);
7726 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7727 s[len - 1] = NUL;
7728 }
7729 return s;
7730}
7731
7732/*
7733 * Check the word in front of the cursor for an abbreviation.
7734 * Called when the non-id character "c" has been entered.
7735 * When an abbreviation is recognized it is removed from the text and
7736 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7737 */
7738 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007739echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740{
7741 /* Don't check for abbreviation in paste mode, when disabled and just
7742 * after moving around with cursor keys. */
7743 if (p_paste || no_abbr || arrow_used)
7744 return FALSE;
7745
7746 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7747 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7748}
7749
7750/*
7751 * replace-stack functions
7752 *
7753 * When replacing characters, the replaced characters are remembered for each
7754 * new character. This is used to re-insert the old text when backspacing.
7755 *
7756 * There is a NUL headed list of characters for each character that is
7757 * currently in the file after the insertion point. When BS is used, one NUL
7758 * headed list is put back for the deleted character.
7759 *
7760 * For a newline, there are two NUL headed lists. One contains the characters
7761 * that the NL replaced. The extra one stores the characters after the cursor
7762 * that were deleted (always white space).
7763 *
7764 * Replace_offset is normally 0, in which case replace_push will add a new
7765 * character at the end of the stack. If replace_offset is not 0, that many
7766 * characters will be left on the stack above the newly inserted character.
7767 */
7768
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007769static char_u *replace_stack = NULL;
7770static long replace_stack_nr = 0; /* next entry in replace stack */
7771static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772
7773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007774replace_push(
7775 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776{
7777 char_u *p;
7778
7779 if (replace_stack_nr < replace_offset) /* nothing to do */
7780 return;
7781 if (replace_stack_len <= replace_stack_nr)
7782 {
7783 replace_stack_len += 50;
7784 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7785 if (p == NULL) /* out of memory */
7786 {
7787 replace_stack_len -= 50;
7788 return;
7789 }
7790 if (replace_stack != NULL)
7791 {
7792 mch_memmove(p, replace_stack,
7793 (size_t)(replace_stack_nr * sizeof(char_u)));
7794 vim_free(replace_stack);
7795 }
7796 replace_stack = p;
7797 }
7798 p = replace_stack + replace_stack_nr - replace_offset;
7799 if (replace_offset)
7800 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7801 *p = c;
7802 ++replace_stack_nr;
7803}
7804
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007805/*
7806 * Push a character onto the replace stack. Handles a multi-byte character in
7807 * reverse byte order, so that the first byte is popped off first.
7808 * Return the number of bytes done (includes composing characters).
7809 */
7810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007811replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007812{
7813 int l = (*mb_ptr2len)(p);
7814 int j;
7815
7816 for (j = l - 1; j >= 0; --j)
7817 replace_push(p[j]);
7818 return l;
7819}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007820
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821/*
7822 * Pop one item from the replace stack.
7823 * return -1 if stack empty
7824 * return replaced character or NUL otherwise
7825 */
7826 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007827replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 if (replace_stack_nr == 0)
7830 return -1;
7831 return (int)replace_stack[--replace_stack_nr];
7832}
7833
7834/*
7835 * Join the top two items on the replace stack. This removes to "off"'th NUL
7836 * encountered.
7837 */
7838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007839replace_join(
7840 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
7842 int i;
7843
7844 for (i = replace_stack_nr; --i >= 0; )
7845 if (replace_stack[i] == NUL && off-- <= 0)
7846 {
7847 --replace_stack_nr;
7848 mch_memmove(replace_stack + i, replace_stack + i + 1,
7849 (size_t)(replace_stack_nr - i));
7850 return;
7851 }
7852}
7853
7854/*
7855 * Pop bytes from the replace stack until a NUL is found, and insert them
7856 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7857 */
7858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007859replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
7861 int cc;
7862 int oldState = State;
7863
7864 State = NORMAL; /* don't want REPLACE here */
7865 while ((cc = replace_pop()) > 0)
7866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 dec_cursor();
7869 }
7870 State = oldState;
7871}
7872
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873/*
7874 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7875 * indicates a multi-byte char, pop the other bytes too.
7876 */
7877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007878mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
7880 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007881 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 int i;
7883 int c;
7884
7885 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7886 {
7887 buf[0] = cc;
7888 for (i = 1; i < n; ++i)
7889 buf[i] = replace_pop();
7890 ins_bytes_len(buf, n);
7891 }
7892 else
7893 ins_char(cc);
7894
7895 if (enc_utf8)
7896 /* Handle composing chars. */
7897 for (;;)
7898 {
7899 c = replace_pop();
7900 if (c == -1) /* stack empty */
7901 break;
7902 if ((n = MB_BYTE2LEN(c)) == 1)
7903 {
7904 /* Not a multi-byte char, put it back. */
7905 replace_push(c);
7906 break;
7907 }
7908 else
7909 {
7910 buf[0] = c;
7911 for (i = 1; i < n; ++i)
7912 buf[i] = replace_pop();
7913 if (utf_iscomposing(utf_ptr2char(buf)))
7914 ins_bytes_len(buf, n);
7915 else
7916 {
7917 /* Not a composing char, put it back. */
7918 for (i = n - 1; i >= 0; --i)
7919 replace_push(buf[i]);
7920 break;
7921 }
7922 }
7923 }
7924}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925
7926/*
7927 * make the replace stack empty
7928 * (called when exiting replace mode)
7929 */
7930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007931replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007933 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 replace_stack_len = 0;
7935 replace_stack_nr = 0;
7936}
7937
7938/*
7939 * Handle doing a BS for one character.
7940 * cc < 0: replace stack empty, just move cursor
7941 * cc == 0: character was inserted, delete it
7942 * cc > 0: character was replaced, put cc (first byte of original char) back
7943 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007944 * When "limit_col" is >= 0, don't delete before this column. Matters when
7945 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 */
7947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007948replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949{
7950 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 int orig_len = 0;
7952 int ins_len;
7953 int orig_vcols = 0;
7954 colnr_T start_vcol;
7955 char_u *p;
7956 int i;
7957 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958
7959 cc = replace_pop();
7960 if (cc > 0)
7961 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01007962#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01007963 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01007964
7965 if (curbuf->b_has_textprop)
7966 {
7967 // Do not adjust text properties for individual delete and insert
7968 // operations, do it afterwards on the resulting text.
7969 len_before = STRLEN(ml_get_curline());
7970 ++text_prop_frozen;
7971 }
7972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 if (State & VREPLACE_FLAG)
7974 {
7975 /* Get the number of screen cells used by the character we are
7976 * going to delete. */
7977 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7978 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 if (has_mbyte)
7981 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007982 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007984 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 replace_push(cc);
7986 }
7987 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {
7989 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007991 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 }
7993 replace_pop_ins();
7994
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 if (State & VREPLACE_FLAG)
7996 {
7997 /* Get the number of screen cells used by the inserted characters */
7998 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007999 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 vcol = start_vcol;
8001 for (i = 0; i < ins_len; ++i)
8002 {
8003 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008004 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 }
8006 vcol -= start_vcol;
8007
8008 /* Delete spaces that were inserted after the cursor to keep the
8009 * text aligned. */
8010 curwin->w_cursor.col += ins_len;
8011 while (vcol > orig_vcols && gchar_cursor() == ' ')
8012 {
8013 del_char(FALSE);
8014 ++orig_vcols;
8015 }
8016 curwin->w_cursor.col -= ins_len;
8017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018
Bram Moolenaar196d1572019-01-02 23:47:18 +01008019 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01008021
8022#ifdef FEAT_TEXT_PROP
8023 if (curbuf->b_has_textprop)
8024 {
8025 size_t len_now = STRLEN(ml_get_curline());
8026
8027 --text_prop_frozen;
8028 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
8029 (int)(len_now - len_before));
8030 }
8031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 }
8033 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008034 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035}
8036
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8038/*
8039 * Map Hebrew keyboard when in hkmap mode.
8040 */
8041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008042hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
8044 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8045 {
8046 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8047 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8048 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8049 static char_u map[26] =
8050 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8051 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8052 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8053 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8054 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8055 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8056 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8057 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8058 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8059
8060 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8061 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8062 /* '-1'='sofit' */
8063 else if (c == 'x')
8064 return 'X';
8065 else if (c == 'q')
8066 return '\''; /* {geresh}={'} */
8067 else if (c == 246)
8068 return ' '; /* \"o --> ' ' for a german keyboard */
8069 else if (c == 228)
8070 return ' '; /* \"a --> ' ' -- / -- */
8071 else if (c == 252)
8072 return ' '; /* \"u --> ' ' -- / -- */
8073#ifdef EBCDIC
8074 else if (islower(c))
8075#else
8076 /* NOTE: islower() does not do the right thing for us on Linux so we
8077 * do this the same was as 5.7 and previous, so it works correctly on
8078 * all systems. Specifically, the e.g. Delete and Arrow keys are
8079 * munged and won't work if e.g. searching for Hebrew text.
8080 */
8081 else if (c >= 'a' && c <= 'z')
8082#endif
8083 return (int)(map[CharOrdLow(c)] + p_aleph);
8084 else
8085 return c;
8086 }
8087 else
8088 {
8089 switch (c)
8090 {
8091 case '`': return ';';
8092 case '/': return '.';
8093 case '\'': return ',';
8094 case 'q': return '/';
8095 case 'w': return '\'';
8096
8097 /* Hebrew letters - set offset from 'a' */
8098 case ',': c = '{'; break;
8099 case '.': c = 'v'; break;
8100 case ';': c = 't'; break;
8101 default: {
8102 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8103
8104#ifdef EBCDIC
8105 /* see note about islower() above */
8106 if (!islower(c))
8107#else
8108 if (c < 'a' || c > 'z')
8109#endif
8110 return c;
8111 c = str[CharOrdLow(c)];
8112 break;
8113 }
8114 }
8115
8116 return (int)(CharOrdLow(c) + p_aleph);
8117 }
8118}
8119#endif
8120
8121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008122ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123{
8124 int need_redraw = FALSE;
8125 int regname;
8126 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008127 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128
8129 /*
8130 * If we are going to wait for a character, show a '"'.
8131 */
8132 pc_status = PC_STATUS_UNSET;
8133 if (redrawing() && !char_avail())
8134 {
8135 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008136 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137
8138 edit_putchar('"', TRUE);
8139#ifdef FEAT_CMDL_INFO
8140 add_to_showcmd_c(Ctrl_R);
8141#endif
8142 }
8143
8144#ifdef USE_ON_FLY_SCROLL
8145 dont_scroll = TRUE; /* disallow scrolling here */
8146#endif
8147
8148 /*
8149 * Don't map the register name. This also prevents the mode message to be
8150 * deleted when ESC is hit.
8151 */
8152 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008153 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8156 {
8157 /* Get a third key for literal register insertion */
8158 literally = regname;
8159#ifdef FEAT_CMDL_INFO
8160 add_to_showcmd_c(literally);
8161#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008162 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 }
8165 --no_mapping;
8166
8167#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008168 /* Don't call u_sync() while typing the expression or giving an error
8169 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 ++no_u_sync;
8171 if (regname == '=')
8172 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008173 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008174# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008176# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008177 /* Sync undo when evaluating the expression calls setline() or
8178 * append(), so that it can be undone separately. */
8179 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008180
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008182
8183 // Cursor may be moved back a column.
8184 curwin->w_cursor = curpos;
8185 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008186# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008187 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 if (im_on)
8189 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008192 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8193 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008194 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 else
8198 {
8199#endif
8200 if (literally == Ctrl_O || literally == Ctrl_P)
8201 {
8202 /* Append the command to the redo buffer. */
8203 AppendCharToRedobuff(Ctrl_R);
8204 AppendCharToRedobuff(literally);
8205 AppendCharToRedobuff(regname);
8206
8207 do_put(regname, BACKWARD, 1L,
8208 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8209 }
8210 else if (insert_reg(regname, literally) == FAIL)
8211 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008212 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 need_redraw = TRUE; /* remove the '"' */
8214 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008215 else if (stop_insert_mode)
8216 /* When the '=' register was used and a function was invoked that
8217 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8218 * insert anything, need to remove the '"' */
8219 need_redraw = TRUE;
8220
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221#ifdef FEAT_EVAL
8222 }
8223 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008224 if (u_sync_once == 1)
8225 ins_need_undo = TRUE;
8226 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227#endif
8228#ifdef FEAT_CMDL_INFO
8229 clear_showcmd();
8230#endif
8231
8232 /* If the inserted register is empty, we need to remove the '"' */
8233 if (need_redraw || stuff_empty())
8234 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008235
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008236 /* Disallow starting Visual mode here, would get a weird mode. */
8237 if (!vis_active && VIsual_active)
8238 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239}
8240
8241/*
8242 * CTRL-G commands in Insert mode.
8243 */
8244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008245ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246{
8247 int c;
8248
8249#ifdef FEAT_INS_EXPAND
8250 /* Right after CTRL-X the cursor will be after the ruler. */
8251 setcursor();
8252#endif
8253
8254 /*
8255 * Don't map the second key. This also prevents the mode message to be
8256 * deleted when ESC is hit.
8257 */
8258 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008259 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 --no_mapping;
8261 switch (c)
8262 {
8263 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8264 case K_UP:
8265 case Ctrl_K:
8266 case 'k': ins_up(TRUE);
8267 break;
8268
8269 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8270 case K_DOWN:
8271 case Ctrl_J:
8272 case 'j': ins_down(TRUE);
8273 break;
8274
8275 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008276 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008278
8279 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008280 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008281 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008282 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 break;
8284
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008285 /* CTRL-G U: do not break undo with the next char */
8286 case 'U':
8287 /* Allow one left/right cursor movement with the next char,
8288 * without breaking undo. */
8289 dont_sync_undo = MAYBE;
8290 break;
8291
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008293 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 }
8295}
8296
8297/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008298 * CTRL-^ in Insert mode.
8299 */
8300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008301ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008302{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008303 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008304 {
8305 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8306 if (State & LANGMAP)
8307 {
8308 curbuf->b_p_iminsert = B_IMODE_NONE;
8309 State &= ~LANGMAP;
8310 }
8311 else
8312 {
8313 curbuf->b_p_iminsert = B_IMODE_LMAP;
8314 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008315#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008316 im_set_active(FALSE);
8317#endif
8318 }
8319 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008320#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008321 else
8322 {
8323 /* There are no ":lmap" mappings, toggle IM */
8324 if (im_get_status())
8325 {
8326 curbuf->b_p_iminsert = B_IMODE_NONE;
8327 im_set_active(FALSE);
8328 }
8329 else
8330 {
8331 curbuf->b_p_iminsert = B_IMODE_IM;
8332 State &= ~LANGMAP;
8333 im_set_active(TRUE);
8334 }
8335 }
8336#endif
8337 set_iminsert_global();
8338 showmode();
8339#ifdef FEAT_GUI
8340 /* may show different cursor shape or color */
8341 if (gui.in_use)
8342 gui_update_cursor(TRUE, FALSE);
8343#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008344#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008345 /* Show/unshow value of 'keymap' in status lines. */
8346 status_redraw_curbuf();
8347#endif
8348}
8349
8350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 * Handle ESC in insert mode.
8352 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8353 * insert.
8354 */
8355 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008356ins_esc(
8357 long *count,
8358 int cmdchar,
8359 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360{
8361 int temp;
8362 static int disabled_redraw = FALSE;
8363
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008364#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008365 check_spell_redraw();
8366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367#if defined(FEAT_HANGULIN)
8368# if defined(ESC_CHG_TO_ENG_MODE)
8369 hangul_input_state_set(0);
8370# endif
8371 if (composing_hangul)
8372 {
8373 push_raw_key(composing_hangul_buffer, 2);
8374 composing_hangul = 0;
8375 }
8376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377
8378 temp = curwin->w_cursor.col;
8379 if (disabled_redraw)
8380 {
8381 --RedrawingDisabled;
8382 disabled_redraw = FALSE;
8383 }
8384 if (!arrow_used)
8385 {
8386 /*
8387 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008388 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8389 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 */
8391 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008392 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393
8394 /*
8395 * Repeating insert may take a long time. Check for
8396 * interrupt now and then.
8397 */
8398 if (*count > 0)
8399 {
8400 line_breakcheck();
8401 if (got_int)
8402 *count = 0;
8403 }
8404
8405 if (--*count > 0) /* repeat what was typed */
8406 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008407 /* Vi repeats the insert without replacing characters. */
8408 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8409 State &= ~REPLACE_FLAG;
8410
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 (void)start_redo_ins();
8412 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008413 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 ++RedrawingDisabled;
8415 disabled_redraw = TRUE;
8416 return FALSE; /* repeat the insert */
8417 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008418 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 undisplay_dollar();
8420 }
8421
8422 /* When an autoindent was removed, curswant stays after the
8423 * indent */
8424 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8425 curwin->w_set_curswant = TRUE;
8426
8427 /* Remember the last Insert position in the '^ mark. */
8428 if (!cmdmod.keepjumps)
8429 curbuf->b_last_insert = curwin->w_cursor;
8430
8431 /*
8432 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008433 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008435 if (!nomove
8436 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01008437 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00008438 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008439 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440#ifdef FEAT_RIGHTLEFT
8441 && !revins_on
8442#endif
8443 )
8444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8446 {
8447 oneleft();
8448 if (restart_edit != NUL)
8449 ++curwin->w_cursor.coladd;
8450 }
8451 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {
8453 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 /* Correct cursor for multi-byte character. */
8455 if (has_mbyte)
8456 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 }
8458 }
8459
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008460#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 /* Disable IM to allow typing English directly for Normal mode commands.
8462 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8463 * well). */
8464 if (!(State & LANGMAP))
8465 im_save_status(&curbuf->b_p_iminsert);
8466 im_set_active(FALSE);
8467#endif
8468
8469 State = NORMAL;
8470 /* need to position cursor again (e.g. when on a TAB ) */
8471 changed_cline_bef_curs();
8472
8473#ifdef FEAT_MOUSE
8474 setmouse();
8475#endif
8476#ifdef CURSOR_SHAPE
8477 ui_cursor_shape(); /* may show different cursor shape */
8478#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008479 if (!p_ek)
8480 /* Re-enable bracketed paste mode. */
8481 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482
8483 /*
8484 * When recording or for CTRL-O, need to display the new mode.
8485 * Otherwise remove the mode message.
8486 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008487 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 showmode();
Bram Moolenaarcb574f42019-01-25 22:29:57 +01008489 else if (p_smd && !skip_showmode())
Bram Moolenaar32526b32019-01-19 17:43:09 +01008490 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491
8492 return TRUE; /* exit Insert mode */
8493}
8494
8495#ifdef FEAT_RIGHTLEFT
8496/*
8497 * Toggle language: hkmap and revins_on.
8498 * Move to end of reverse inserted text.
8499 */
8500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008501ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
8503 if (revins_on && revins_chars && revins_scol >= 0)
8504 {
8505 while (gchar_cursor() != NUL && revins_chars--)
8506 ++curwin->w_cursor.col;
8507 }
8508 p_ri = !p_ri;
8509 revins_on = (State == INSERT && p_ri);
8510 if (revins_on)
8511 {
8512 revins_scol = curwin->w_cursor.col;
8513 revins_legal++;
8514 revins_chars = 0;
8515 undisplay_dollar();
8516 }
8517 else
8518 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01008519 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 showmode();
8521}
8522#endif
8523
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524/*
8525 * If 'keymodel' contains "startsel", may start selection.
8526 * Returns TRUE when a CTRL-O and other keys stuffed.
8527 */
8528 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008529ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 if (km_startsel)
8532 switch (c)
8533 {
8534 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 case K_PAGEUP:
8537 case K_KPAGEUP:
8538 case K_PAGEDOWN:
8539 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008540# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 case K_LEFT:
8542 case K_RIGHT:
8543 case K_UP:
8544 case K_DOWN:
8545 case K_END:
8546 case K_HOME:
8547# endif
8548 if (!(mod_mask & MOD_MASK_SHIFT))
8549 break;
8550 /* FALLTHROUGH */
8551 case K_S_LEFT:
8552 case K_S_RIGHT:
8553 case K_S_UP:
8554 case K_S_DOWN:
8555 case K_S_END:
8556 case K_S_HOME:
8557 /* Start selection right away, the cursor can move with
8558 * CTRL-O when beyond the end of the line. */
8559 start_selection();
8560
8561 /* Execute the key in (insert) Select mode. */
8562 stuffcharReadbuff(Ctrl_O);
8563 if (mod_mask)
8564 {
8565 char_u buf[4];
8566
8567 buf[0] = K_SPECIAL;
8568 buf[1] = KS_MODIFIER;
8569 buf[2] = mod_mask;
8570 buf[3] = NUL;
8571 stuffReadbuff(buf);
8572 }
8573 stuffcharReadbuff(c);
8574 return TRUE;
8575 }
8576 return FALSE;
8577}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578
8579/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008580 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008581 */
8582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008583ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008584{
Bram Moolenaar14184a32019-02-16 15:10:30 +01008585#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008586 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008587 (char_u *)((State & REPLACE_FLAG) ? "i"
8588 : replaceState == VREPLACE ? "v"
8589 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01008590#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02008591 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008592 if (State & REPLACE_FLAG)
8593 State = INSERT | (State & LANGMAP);
8594 else
8595 State = replaceState | (State & LANGMAP);
8596 AppendCharToRedobuff(K_INS);
8597 showmode();
8598#ifdef CURSOR_SHAPE
8599 ui_cursor_shape(); /* may show different cursor shape */
8600#endif
8601}
8602
8603/*
8604 * Pressed CTRL-O in Insert mode.
8605 */
8606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008607ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008608{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008609 if (State & VREPLACE_FLAG)
8610 restart_edit = 'V';
8611 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008612 if (State & REPLACE_FLAG)
8613 restart_edit = 'R';
8614 else
8615 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008616 if (virtual_active())
8617 ins_at_eol = FALSE; /* cursor always keeps its column */
8618 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008619 ins_at_eol = (gchar_cursor() == NUL);
8620}
8621
8622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 * If the cursor is on an indent, ^T/^D insert/delete one
8624 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008625 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 * with vi. But vi only supports ^T and ^D after an
8627 * autoindent, we support it everywhere.
8628 */
8629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008630ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631{
8632 if (stop_arrow() == FAIL)
8633 return;
8634 AppendCharToRedobuff(c);
8635
8636 /*
8637 * 0^D and ^^D: remove all indent.
8638 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008639 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8640 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 {
8642 --curwin->w_cursor.col;
8643 (void)del_char(FALSE); /* delete the '^' or '0' */
8644 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8645 if (State & REPLACE_FLAG)
8646 replace_pop_ins();
8647 if (lastc == '^')
8648 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008649 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 }
8651 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008652 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
8654 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8655 did_ai = FALSE;
8656#ifdef FEAT_SMARTINDENT
8657 did_si = FALSE;
8658 can_si = FALSE;
8659 can_si_back = FALSE;
8660#endif
8661#ifdef FEAT_CINDENT
8662 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8663#endif
8664}
8665
8666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008667ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668{
8669 int temp;
8670
8671 if (stop_arrow() == FAIL)
8672 return;
8673 if (gchar_cursor() == NUL) /* delete newline */
8674 {
8675 temp = curwin->w_cursor.col;
8676 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008677 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008678 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008680 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008682 /* Adjust orig_line_count in case more lines have been deleted than
8683 * have been added. That makes sure, that open_line() later
8684 * can access all buffer lines correctly */
8685 if (State & VREPLACE_FLAG &&
8686 orig_line_count > curbuf->b_ml.ml_line_count)
8687 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008690 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8691 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 did_ai = FALSE;
8693#ifdef FEAT_SMARTINDENT
8694 did_si = FALSE;
8695 can_si = FALSE;
8696 can_si_back = FALSE;
8697#endif
8698 AppendCharToRedobuff(K_DEL);
8699}
8700
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008701/*
8702 * Delete one character for ins_bs().
8703 */
8704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008705ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008706{
8707 dec_cursor();
8708 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8709 if (State & REPLACE_FLAG)
8710 {
8711 /* Don't delete characters before the insert point when in
8712 * Replace mode */
8713 if (curwin->w_cursor.lnum != Insstart.lnum
8714 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008715 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008716 }
8717 else
8718 (void)del_char(FALSE);
8719}
8720
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721/*
8722 * Handle Backspace, delete-word and delete-line in Insert mode.
8723 * Return TRUE when backspace was actually used.
8724 */
8725 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008726ins_bs(
8727 int c,
8728 int mode,
8729 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 linenr_T lnum;
8732 int cc;
8733 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008734 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 colnr_T mincol;
8736 int did_backspace = FALSE;
8737 int in_indent;
8738 int oldState;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008739 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740
8741 /*
8742 * can't delete anything in an empty file
8743 * can't backup past first character in buffer
8744 * can't backup past starting point unless 'backspace' > 1
8745 * can backup to a previous line if 'backspace' == 0
8746 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008747 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 || (
8749#ifdef FEAT_RIGHTLEFT
8750 !revins_on &&
8751#endif
8752 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8753 || (!can_bs(BS_START)
8754 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008755 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8756 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8758 && curwin->w_cursor.col <= ai_col)
8759 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8760 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008761 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 return FALSE;
8763 }
8764
8765 if (stop_arrow() == FAIL)
8766 return FALSE;
8767 in_indent = inindent(0);
8768#ifdef FEAT_CINDENT
8769 if (in_indent)
8770 can_cindent = FALSE;
8771#endif
8772#ifdef FEAT_COMMENTS
8773 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8774#endif
8775#ifdef FEAT_RIGHTLEFT
8776 if (revins_on) /* put cursor after last inserted char */
8777 inc_cursor();
8778#endif
8779
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 /* Virtualedit:
8781 * BACKSPACE_CHAR eats a virtual space
8782 * BACKSPACE_WORD eats all coladd
8783 * BACKSPACE_LINE eats all coladd and keeps going
8784 */
8785 if (curwin->w_cursor.coladd > 0)
8786 {
8787 if (mode == BACKSPACE_CHAR)
8788 {
8789 --curwin->w_cursor.coladd;
8790 return TRUE;
8791 }
8792 if (mode == BACKSPACE_WORD)
8793 {
8794 curwin->w_cursor.coladd = 0;
8795 return TRUE;
8796 }
8797 curwin->w_cursor.coladd = 0;
8798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799
8800 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02008801 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 */
8803 if (curwin->w_cursor.col == 0)
8804 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008805 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008806 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807#ifdef FEAT_RIGHTLEFT
8808 || revins_on
8809#endif
8810 )
8811 {
8812 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8813 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8814 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008815 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02008816 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 }
8818 /*
8819 * In replace mode:
8820 * cc < 0: NL was inserted, delete it
8821 * cc >= 0: NL was replaced, put original characters back
8822 */
8823 cc = -1;
8824 if (State & REPLACE_FLAG)
8825 cc = replace_pop(); /* returns -1 if NL was inserted */
8826 /*
8827 * In replace mode, in the line we started replacing, we only move the
8828 * cursor.
8829 */
8830 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8831 {
8832 dec_cursor();
8833 }
8834 else
8835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 if (!(State & VREPLACE_FLAG)
8837 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {
8839 temp = gchar_cursor(); /* remember current char */
8840 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008841
8842 /* When "aw" is in 'formatoptions' we must delete the space at
8843 * the end of the line, otherwise the line will be broken
8844 * again when auto-formatting. */
8845 if (has_format_option(FO_AUTO)
8846 && has_format_option(FO_WHITE_PAR))
8847 {
8848 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8849 TRUE);
8850 int len;
8851
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008852 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008853 if (len > 0 && ptr[len - 1] == ' ')
8854 ptr[len - 1] = NUL;
8855 }
8856
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008857 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 if (temp == NUL && gchar_cursor() != NUL)
8859 inc_cursor();
8860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 else
8862 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863
8864 /*
8865 * In REPLACE mode we have to put back the text that was replaced
8866 * by the NL. On the replace stack is first a NUL-terminated
8867 * sequence of characters that were deleted and then the
8868 * characters that NL replaced.
8869 */
8870 if (State & REPLACE_FLAG)
8871 {
8872 /*
8873 * Do the next ins_char() in NORMAL state, to
8874 * prevent ins_char() from replacing characters and
8875 * avoiding showmatch().
8876 */
8877 oldState = State;
8878 State = NORMAL;
8879 /*
8880 * restore characters (blanks) deleted after cursor
8881 */
8882 while (cc > 0)
8883 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008884 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008886 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 cc = replace_pop();
8888 }
8889 /* restore the characters that NL replaced */
8890 replace_pop_ins();
8891 State = oldState;
8892 }
8893 }
8894 did_ai = FALSE;
8895 }
8896 else
8897 {
8898 /*
8899 * Delete character(s) before the cursor.
8900 */
8901#ifdef FEAT_RIGHTLEFT
8902 if (revins_on) /* put cursor on last inserted char */
8903 dec_cursor();
8904#endif
8905 mincol = 0;
8906 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008907 if (mode == BACKSPACE_LINE
8908 && (curbuf->b_p_ai
8909#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008910 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008911#endif
8912 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913#ifdef FEAT_RIGHTLEFT
8914 && !revins_on
8915#endif
8916 )
8917 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008918 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008920 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008922 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 }
8924
8925 /*
8926 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8927 */
8928 if ( mode == BACKSPACE_CHAR
8929 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008930 || ((get_sts_value() != 0
8931#ifdef FEAT_VARTABS
8932 || tabstop_count(curbuf->b_p_vsts_array)
8933#endif
8934 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008935 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 && (*(ml_get_cursor() - 1) == TAB
8937 || (*(ml_get_cursor() - 1) == ' '
8938 && (!*inserted_space_p
8939 || arrow_used))))))
8940 {
8941 int ts;
8942 colnr_T vcol;
8943 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008944 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945
8946 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 /* Compute the virtual column where we want to be. Since
8948 * 'showbreak' may get in the way, need to get the last column of
8949 * the previous character. */
8950 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008951 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 dec_cursor();
8953 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8954 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008955#ifdef FEAT_VARTABS
8956 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02008957 {
8958 ts = (int)get_sw_value(curbuf);
8959 want_vcol = (want_vcol / ts) * ts;
8960 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008961 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02008962 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008963 curbuf->b_p_vsts_array);
8964#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02008965 if (p_sta && in_indent)
8966 ts = (int)get_sw_value(curbuf);
8967 else
8968 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971
8972 /* delete characters until we are at or before want_vcol */
8973 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01008974 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008975 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976
8977 /* insert extra spaces until we are at want_vcol */
8978 while (vcol < want_vcol)
8979 {
8980 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008981 if (curwin->w_cursor.lnum == Insstart_orig.lnum
8982 && curwin->w_cursor.col < Insstart_orig.col)
8983 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 if (State & VREPLACE_FLAG)
8986 ins_char(' ');
8987 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 {
8989 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008990 if ((State & REPLACE_FLAG))
8991 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 }
8993 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8994 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008995
8996 /* If we are now back where we started delete one character. Can
8997 * happen when using 'sts' and 'linebreak'. */
8998 if (vcol >= start_vcol)
8999 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 }
9001
9002 /*
9003 * Delete upto starting point, start of line or previous word.
9004 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009007 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009009 if (has_mbyte)
9010 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009011 do
9012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009014 if (!revins_on) /* put cursor on char to be deleted */
9015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009017
9018 cc = gchar_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009019 /* look multi-byte character class */
9020 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009022 prev_cclass = cclass;
9023 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009025
9026 /* start of word? */
9027 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9028 {
9029 mode = BACKSPACE_WORD_NOT_SPACE;
9030 temp = vim_iswordc(cc);
9031 }
9032 /* end of word? */
9033 else if (mode == BACKSPACE_WORD_NOT_SPACE
9034 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01009035 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009038 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009040 inc_cursor();
9041#ifdef FEAT_RIGHTLEFT
9042 else if (State & REPLACE_FLAG)
9043 dec_cursor();
9044#endif
9045 break;
9046 }
9047 if (State & REPLACE_FLAG)
9048 replace_do_bs(-1);
9049 else
9050 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009051 if (enc_utf8 && p_deco)
9052 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009053 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009054 /*
9055 * If there are combining characters and 'delcombine' is set
9056 * move the cursor back. Don't back up before the base
9057 * character.
9058 */
9059 if (enc_utf8 && p_deco && cpc[0] != NUL)
9060 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009061#ifdef FEAT_RIGHTLEFT
9062 if (revins_chars)
9063 {
9064 revins_chars--;
9065 revins_legal++;
9066 }
9067 if (revins_on && gchar_cursor() == NUL)
9068 break;
9069#endif
9070 }
9071 /* Just a single backspace?: */
9072 if (mode == BACKSPACE_CHAR)
9073 break;
9074 } while (
9075#ifdef FEAT_RIGHTLEFT
9076 revins_on ||
9077#endif
9078 (curwin->w_cursor.col > mincol
9079 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9080 || curwin->w_cursor.col != Insstart_orig.col)));
9081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 did_backspace = TRUE;
9083 }
9084#ifdef FEAT_SMARTINDENT
9085 did_si = FALSE;
9086 can_si = FALSE;
9087 can_si_back = FALSE;
9088#endif
9089 if (curwin->w_cursor.col <= 1)
9090 did_ai = FALSE;
9091 /*
9092 * It's a little strange to put backspaces into the redo
9093 * buffer, but it makes auto-indent a lot easier to deal
9094 * with.
9095 */
9096 AppendCharToRedobuff(c);
9097
9098 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009099 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009100 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009101 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102
9103 /* vi behaviour: the cursor moves backward but the character that
9104 * was there remains visible
9105 * Vim behaviour: the cursor moves backward and the character that
9106 * was there is erased from the screen.
9107 * We can emulate the vi behaviour by pretending there is a dollar
9108 * displayed even when there isn't.
9109 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009110 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 dollar_vcol = curwin->w_virtcol;
9112
Bram Moolenaarce3be472008-01-14 19:12:28 +00009113#ifdef FEAT_FOLDING
9114 /* When deleting a char the cursor line must never be in a closed fold.
9115 * E.g., when 'foldmethod' is indent and deleting the first non-white
9116 * char before a Tab. */
9117 if (did_backspace)
9118 foldOpenCursor();
9119#endif
9120
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 return did_backspace;
9122}
9123
9124#ifdef FEAT_MOUSE
9125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009126ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127{
9128 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009129 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130
9131# ifdef FEAT_GUI
9132 /* When GUI is active, also move/paste when 'mouse' is empty */
9133 if (!gui.in_use)
9134# endif
9135 if (!mouse_has(MOUSE_INSERT))
9136 return;
9137
9138 undisplay_dollar();
9139 tpos = curwin->w_cursor;
9140 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9141 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009142 win_T *new_curwin = curwin;
9143
9144 if (curwin != old_curwin && win_valid(old_curwin))
9145 {
9146 /* Mouse took us to another window. We need to go back to the
9147 * previous one to stop insert there properly. */
9148 curwin = old_curwin;
9149 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009150#ifdef FEAT_JOB_CHANNEL
9151 if (bt_prompt(curbuf))
9152 // Restart Insert mode when re-entering the prompt buffer.
9153 curbuf->b_prompt_insert = 'A';
9154#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009155 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009156 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009157 if (curwin != new_curwin && win_valid(new_curwin))
9158 {
9159 curwin = new_curwin;
9160 curbuf = curwin->w_buffer;
9161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162# ifdef FEAT_CINDENT
9163 can_cindent = TRUE;
9164# endif
9165 }
9166
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 /* redraw status lines (in case another window became active) */
9168 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169}
9170
9171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009172ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173{
9174 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009175 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009176# ifdef FEAT_INS_EXPAND
9177 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178# endif
9179
9180 tpos = curwin->w_cursor;
9181
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009182 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 {
9184 int row, col;
9185
9186 row = mouse_row;
9187 col = mouse_col;
9188
9189 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009190 wp = mouse_find_win(&row, &col);
9191 if (wp == NULL)
9192 return;
9193 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 curbuf = curwin->w_buffer;
9195 }
9196 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 undisplay_dollar();
9198
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009199# ifdef FEAT_INS_EXPAND
9200 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009201 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009202# endif
9203 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009204 if (dir == MSCR_DOWN || dir == MSCR_UP)
9205 {
9206 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9207 scroll_redraw(dir,
9208 (long)(curwin->w_botline - curwin->w_topline));
9209 else
9210 scroll_redraw(dir, 3L);
9211 }
9212#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009213 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009214 {
9215 int val, step = 6;
9216
9217 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009218 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009219 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9220 if (val < 0)
9221 val = 0;
9222 gui_do_horiz_scroll(val, TRUE);
9223 }
9224#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009225# ifdef FEAT_INS_EXPAND
9226 did_scroll = TRUE;
9227# endif
9228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 curwin->w_redr_status = TRUE;
9231
9232 curwin = old_curwin;
9233 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009235# ifdef FEAT_INS_EXPAND
9236 /* The popup menu may overlay the window, need to redraw it.
9237 * TODO: Would be more efficient to only redraw the windows that are
9238 * overlapped by the popup menu. */
9239 if (pum_visible() && did_scroll)
9240 {
9241 redraw_all_later(NOT_VALID);
9242 ins_compl_show_pum();
9243 }
9244# endif
9245
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009246 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 {
9248 start_arrow(&tpos);
9249# ifdef FEAT_CINDENT
9250 can_cindent = TRUE;
9251# endif
9252 }
9253}
9254#endif
9255
Bram Moolenaarec2da362017-01-21 20:04:22 +01009256/*
9257 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9258 * P_PE literally.
9259 * When "drop" is TRUE then consume the text and drop it.
9260 */
9261 int
9262bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9263{
9264 int c;
9265 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9266 int idx = 0;
9267 char_u *end = find_termcode((char_u *)"PE");
9268 int ret_char = -1;
9269 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009270 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009271
9272 /* If the end code is too long we can't detect it, read everything. */
9273 if (STRLEN(end) >= NUMBUFLEN)
9274 end = NULL;
9275 ++no_mapping;
9276 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009277 if (!p_paste)
9278 // Also have the side effects of setting 'paste' to make it work much
9279 // faster.
9280 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009281
Bram Moolenaarec2da362017-01-21 20:04:22 +01009282 for (;;)
9283 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009284 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009285 if (end == NULL && vpeekc() == NUL)
9286 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009287 do
9288 {
9289 c = vgetc();
9290 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9291 if (c == NUL || got_int)
9292 // When CTRL-C was encountered the typeahead will be flushed and we
9293 // won't get the end sequence.
9294 break;
9295
Bram Moolenaarec2da362017-01-21 20:04:22 +01009296 if (has_mbyte)
9297 idx += (*mb_char2bytes)(c, buf + idx);
9298 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01009299 buf[idx++] = c;
9300 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009301 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009302 {
9303 if (end[idx] == NUL)
9304 break; /* Found the end of paste code. */
9305 continue;
9306 }
9307 if (!drop)
9308 {
9309 switch (mode)
9310 {
9311 case PASTE_CMDLINE:
9312 put_on_cmdline(buf, idx, TRUE);
9313 break;
9314
9315 case PASTE_EX:
9316 if (gap != NULL && ga_grow(gap, idx) == OK)
9317 {
9318 mch_memmove((char *)gap->ga_data + gap->ga_len,
9319 buf, (size_t)idx);
9320 gap->ga_len += idx;
9321 }
9322 break;
9323
9324 case PASTE_INSERT:
9325 if (stop_arrow() == OK)
9326 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009327 c = buf[0];
9328 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9329 ins_eol(c);
9330 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009331 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009332 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009333 AppendToRedobuffLit(buf, idx);
9334 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009335 }
9336 break;
9337
9338 case PASTE_ONE_CHAR:
9339 if (ret_char == -1)
9340 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01009341 if (has_mbyte)
9342 ret_char = (*mb_ptr2char)(buf);
9343 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01009344 ret_char = buf[0];
9345 }
9346 break;
9347 }
9348 }
9349 idx = 0;
9350 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009351
Bram Moolenaarec2da362017-01-21 20:04:22 +01009352 --no_mapping;
9353 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009354 if (!save_paste)
9355 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009356
9357 return ret_char;
9358}
9359
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009360#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009362ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009363{
9364 /* We will be leaving the current window, unless closing another tab. */
9365 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9366 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9367 {
9368 undisplay_dollar();
9369 start_arrow(&curwin->w_cursor);
9370# ifdef FEAT_CINDENT
9371 can_cindent = TRUE;
9372# endif
9373 }
9374
9375 if (c == K_TABLINE)
9376 goto_tabpage(current_tab);
9377 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009378 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009379 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009380 redraw_statuslines(); /* will redraw the tabline when needed */
9381 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009382}
9383#endif
9384
9385#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009387ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388{
9389 pos_T tpos;
9390
9391 undisplay_dollar();
9392 tpos = curwin->w_cursor;
9393 if (gui_do_scroll())
9394 {
9395 start_arrow(&tpos);
9396# ifdef FEAT_CINDENT
9397 can_cindent = TRUE;
9398# endif
9399 }
9400}
9401
9402 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009403ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404{
9405 pos_T tpos;
9406
9407 undisplay_dollar();
9408 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009409 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 {
9411 start_arrow(&tpos);
9412# ifdef FEAT_CINDENT
9413 can_cindent = TRUE;
9414# endif
9415 }
9416}
9417#endif
9418
9419 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01009420ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421{
9422 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01009423 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424
9425#ifdef FEAT_FOLDING
9426 if ((fdo_flags & FDO_HOR) && KeyTyped)
9427 foldOpenCursor();
9428#endif
9429 undisplay_dollar();
9430 tpos = curwin->w_cursor;
9431 if (oneleft() == OK)
9432 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009433#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9434 /* Only call start_arrow() when not busy with preediting, it will
9435 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009436 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009437#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009438 {
9439 start_arrow_with_change(&tpos, end_change);
9440 if (!end_change)
9441 AppendCharToRedobuff(K_LEFT);
9442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443#ifdef FEAT_RIGHTLEFT
9444 /* If exit reversed string, position is fixed */
9445 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9446 revins_legal++;
9447 revins_chars++;
9448#endif
9449 }
9450
9451 /*
9452 * if 'whichwrap' set for cursor in insert mode may go to
9453 * previous line
9454 */
9455 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9456 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009457 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 start_arrow(&tpos);
9459 --(curwin->w_cursor.lnum);
9460 coladvance((colnr_T)MAXCOL);
9461 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9462 }
9463 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009464 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009465 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466}
9467
9468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009469ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 pos_T tpos;
9472
9473#ifdef FEAT_FOLDING
9474 if ((fdo_flags & FDO_HOR) && KeyTyped)
9475 foldOpenCursor();
9476#endif
9477 undisplay_dollar();
9478 tpos = curwin->w_cursor;
9479 if (c == K_C_HOME)
9480 curwin->w_cursor.lnum = 1;
9481 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 curwin->w_curswant = 0;
9484 start_arrow(&tpos);
9485}
9486
9487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009488ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489{
9490 pos_T tpos;
9491
9492#ifdef FEAT_FOLDING
9493 if ((fdo_flags & FDO_HOR) && KeyTyped)
9494 foldOpenCursor();
9495#endif
9496 undisplay_dollar();
9497 tpos = curwin->w_cursor;
9498 if (c == K_C_END)
9499 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9500 coladvance((colnr_T)MAXCOL);
9501 curwin->w_curswant = MAXCOL;
9502
9503 start_arrow(&tpos);
9504}
9505
9506 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01009507ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01009509 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510#ifdef FEAT_FOLDING
9511 if ((fdo_flags & FDO_HOR) && KeyTyped)
9512 foldOpenCursor();
9513#endif
9514 undisplay_dollar();
9515 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9516 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01009517 start_arrow_with_change(&curwin->w_cursor, end_change);
9518 if (!end_change)
9519 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 (void)bck_word(1L, FALSE, FALSE);
9521 curwin->w_set_curswant = TRUE;
9522 }
9523 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009524 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01009525 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526}
9527
9528 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01009529ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01009531 int end_change = dont_sync_undo == FALSE; // end undoable change
9532
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533#ifdef FEAT_FOLDING
9534 if ((fdo_flags & FDO_HOR) && KeyTyped)
9535 foldOpenCursor();
9536#endif
9537 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01009538 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009540 start_arrow_with_change(&curwin->w_cursor, end_change);
9541 if (!end_change)
9542 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 if (virtual_active())
9545 oneright();
9546 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009549 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 ++curwin->w_cursor.col;
9552 }
9553
9554#ifdef FEAT_RIGHTLEFT
9555 revins_legal++;
9556 if (revins_chars)
9557 revins_chars--;
9558#endif
9559 }
9560 /* if 'whichwrap' set for cursor in insert mode, may move the
9561 * cursor to the next line */
9562 else if (vim_strchr(p_ww, ']') != NULL
9563 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9564 {
9565 start_arrow(&curwin->w_cursor);
9566 curwin->w_set_curswant = TRUE;
9567 ++curwin->w_cursor.lnum;
9568 curwin->w_cursor.col = 0;
9569 }
9570 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009571 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009572 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573}
9574
9575 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01009576ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01009578 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579#ifdef FEAT_FOLDING
9580 if ((fdo_flags & FDO_HOR) && KeyTyped)
9581 foldOpenCursor();
9582#endif
9583 undisplay_dollar();
9584 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9585 || gchar_cursor() != NUL)
9586 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01009587 start_arrow_with_change(&curwin->w_cursor, end_change);
9588 if (!end_change)
9589 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 (void)fwd_word(1L, FALSE, 0);
9591 curwin->w_set_curswant = TRUE;
9592 }
9593 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009594 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01009595 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596}
9597
9598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009599ins_up(
9600 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
9602 pos_T tpos;
9603 linenr_T old_topline = curwin->w_topline;
9604#ifdef FEAT_DIFF
9605 int old_topfill = curwin->w_topfill;
9606#endif
9607
9608 undisplay_dollar();
9609 tpos = curwin->w_cursor;
9610 if (cursor_up(1L, TRUE) == OK)
9611 {
9612 if (startcol)
9613 coladvance(getvcol_nolist(&Insstart));
9614 if (old_topline != curwin->w_topline
9615#ifdef FEAT_DIFF
9616 || old_topfill != curwin->w_topfill
9617#endif
9618 )
9619 redraw_later(VALID);
9620 start_arrow(&tpos);
9621#ifdef FEAT_CINDENT
9622 can_cindent = TRUE;
9623#endif
9624 }
9625 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009626 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627}
9628
9629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009630ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631{
9632 pos_T tpos;
9633
9634 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009635
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009636 if (mod_mask & MOD_MASK_CTRL)
9637 {
9638 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009639 if (first_tabpage->tp_next != NULL)
9640 {
9641 start_arrow(&curwin->w_cursor);
9642 goto_tabpage(-1);
9643 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009644 return;
9645 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009646
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 tpos = curwin->w_cursor;
9648 if (onepage(BACKWARD, 1L) == OK)
9649 {
9650 start_arrow(&tpos);
9651#ifdef FEAT_CINDENT
9652 can_cindent = TRUE;
9653#endif
9654 }
9655 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009656 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657}
9658
9659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009660ins_down(
9661 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662{
9663 pos_T tpos;
9664 linenr_T old_topline = curwin->w_topline;
9665#ifdef FEAT_DIFF
9666 int old_topfill = curwin->w_topfill;
9667#endif
9668
9669 undisplay_dollar();
9670 tpos = curwin->w_cursor;
9671 if (cursor_down(1L, TRUE) == OK)
9672 {
9673 if (startcol)
9674 coladvance(getvcol_nolist(&Insstart));
9675 if (old_topline != curwin->w_topline
9676#ifdef FEAT_DIFF
9677 || old_topfill != curwin->w_topfill
9678#endif
9679 )
9680 redraw_later(VALID);
9681 start_arrow(&tpos);
9682#ifdef FEAT_CINDENT
9683 can_cindent = TRUE;
9684#endif
9685 }
9686 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009687 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688}
9689
9690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009691ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692{
9693 pos_T tpos;
9694
9695 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009696
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009697 if (mod_mask & MOD_MASK_CTRL)
9698 {
9699 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009700 if (first_tabpage->tp_next != NULL)
9701 {
9702 start_arrow(&curwin->w_cursor);
9703 goto_tabpage(0);
9704 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009705 return;
9706 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009707
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 tpos = curwin->w_cursor;
9709 if (onepage(FORWARD, 1L) == OK)
9710 {
9711 start_arrow(&tpos);
9712#ifdef FEAT_CINDENT
9713 can_cindent = TRUE;
9714#endif
9715 }
9716 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009717 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718}
9719
9720#ifdef FEAT_DND
9721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009722ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723{
9724 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9725}
9726#endif
9727
9728/*
9729 * Handle TAB in Insert or Replace mode.
9730 * Return TRUE when the TAB needs to be inserted like a normal character.
9731 */
9732 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009733ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734{
9735 int ind;
9736 int i;
9737 int temp;
9738
9739 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9740 Insstart_blank_vcol = get_nolist_virtcol();
9741 if (echeck_abbr(TAB + ABBR_OFF))
9742 return FALSE;
9743
9744 ind = inindent(0);
9745#ifdef FEAT_CINDENT
9746 if (ind)
9747 can_cindent = FALSE;
9748#endif
9749
9750 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009751 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 */
9753 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009754#ifdef FEAT_VARTABS
9755 && !(p_sta && ind
9756 /* These five lines mean 'tabstop' != 'shiftwidth' */
9757 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
9758 || (tabstop_count(curbuf->b_p_vts_array) == 1
9759 && tabstop_first(curbuf->b_p_vts_array)
9760 != get_sw_value(curbuf))
9761 || (tabstop_count(curbuf->b_p_vts_array) == 0
9762 && curbuf->b_p_ts != get_sw_value(curbuf))))
9763 && tabstop_count(curbuf->b_p_vsts_array) == 0
9764#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009765 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009766#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009767 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 return TRUE;
9769
9770 if (stop_arrow() == FAIL)
9771 return TRUE;
9772
9773 did_ai = FALSE;
9774#ifdef FEAT_SMARTINDENT
9775 did_si = FALSE;
9776 can_si = FALSE;
9777 can_si_back = FALSE;
9778#endif
9779 AppendToRedobuff((char_u *)"\t");
9780
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009781#ifdef FEAT_VARTABS
9782 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9783 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009784 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009785 temp -= get_nolist_virtcol() % temp;
9786 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009787 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009788 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009789 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009790 curbuf->b_p_vsts_array);
9791 else /* otherwise use 'tabstop' */
9792 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
9793 curbuf->b_p_vts_array);
9794#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009796 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009797 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9798 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 else /* otherwise use 'tabstop' */
9800 temp = (int)curbuf->b_p_ts;
9801 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803
9804 /*
9805 * Insert the first space with ins_char(). It will delete one char in
9806 * replace mode. Insert the rest with ins_str(); it will not delete any
9807 * chars. For VREPLACE mode, we use ins_char() for all characters.
9808 */
9809 ins_char(' ');
9810 while (--temp > 0)
9811 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 if (State & VREPLACE_FLAG)
9813 ins_char(' ');
9814 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 {
9816 ins_str((char_u *)" ");
9817 if (State & REPLACE_FLAG) /* no char replaced */
9818 replace_push(NUL);
9819 }
9820 }
9821
9822 /*
9823 * When 'expandtab' not set: Replace spaces by TABs where possible.
9824 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009825#ifdef FEAT_VARTABS
9826 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
9827 || get_sts_value() > 0
9828 || (p_sta && ind)))
9829#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009830 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 {
9833 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 char_u *saved_line = NULL; /* init for GCC */
9835 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 pos_T fpos;
9837 pos_T *cursor;
9838 colnr_T want_vcol, vcol;
9839 int change_col = -1;
9840 int save_list = curwin->w_p_list;
9841
9842 /*
9843 * Get the current line. For VREPLACE mode, don't make real changes
9844 * yet, just work on a copy of the line.
9845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 if (State & VREPLACE_FLAG)
9847 {
9848 pos = curwin->w_cursor;
9849 cursor = &pos;
9850 saved_line = vim_strsave(ml_get_curline());
9851 if (saved_line == NULL)
9852 return FALSE;
9853 ptr = saved_line + pos.col;
9854 }
9855 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 {
9857 ptr = ml_get_cursor();
9858 cursor = &curwin->w_cursor;
9859 }
9860
9861 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9862 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9863 curwin->w_p_list = FALSE;
9864
9865 /* Find first white before the cursor */
9866 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01009867 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 {
9869 --fpos.col;
9870 --ptr;
9871 }
9872
9873 /* In Replace mode, don't change characters before the insert point. */
9874 if ((State & REPLACE_FLAG)
9875 && fpos.lnum == Insstart.lnum
9876 && fpos.col < Insstart.col)
9877 {
9878 ptr += Insstart.col - fpos.col;
9879 fpos.col = Insstart.col;
9880 }
9881
9882 /* compute virtual column numbers of first white and cursor */
9883 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9884 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9885
Bram Moolenaar597a4222014-06-25 14:39:50 +02009886 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9887 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01009888 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009890 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 if (vcol + i > want_vcol)
9892 break;
9893 if (*ptr != TAB)
9894 {
9895 *ptr = TAB;
9896 if (change_col < 0)
9897 {
9898 change_col = fpos.col; /* Column of first change */
9899 /* May have to adjust Insstart */
9900 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9901 Insstart.col = fpos.col;
9902 }
9903 }
9904 ++fpos.col;
9905 ++ptr;
9906 vcol += i;
9907 }
9908
9909 if (change_col >= 0)
9910 {
9911 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009912 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
9914 /* Skip over the spaces we need. */
9915 while (vcol < want_vcol && *ptr == ' ')
9916 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009917 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 ++ptr;
9919 ++repl_off;
9920 }
9921 if (vcol > want_vcol)
9922 {
9923 /* Must have a char with 'showbreak' just before it. */
9924 --ptr;
9925 --repl_off;
9926 }
9927 fpos.col += repl_off;
9928
9929 /* Delete following spaces. */
9930 i = cursor->col - fpos.col;
9931 if (i > 0)
9932 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009933 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02009935 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 for (temp = i; --temp >= 0; )
9937 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01009938#ifdef FEAT_TEXT_PROP
9939 curbuf->b_ml.ml_line_len -= i;
9940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009942#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009943 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009944 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009945 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009946 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9947 (char_u *)"\t", 1);
9948 }
9949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 cursor->col -= i;
9951
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 /*
9953 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9954 * backspacing over the changed spacing and then inserting the new
9955 * spacing.
9956 */
9957 if (State & VREPLACE_FLAG)
9958 {
9959 /* Backspace from real cursor to change_col */
9960 backspace_until_column(change_col);
9961
9962 /* Insert each char in saved_line from changed_col to
9963 * ptr-cursor */
9964 ins_bytes_len(saved_line + change_col,
9965 cursor->col - change_col);
9966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 }
9968
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 if (State & VREPLACE_FLAG)
9970 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 curwin->w_p_list = save_list;
9972 }
9973
9974 return FALSE;
9975}
9976
9977/*
9978 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02009979 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 */
9981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009982ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983{
9984 int i;
9985
9986 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02009987 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02009989 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 undisplay_dollar();
9991
9992 /*
9993 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9994 * character under the cursor. Only push a NUL on the replace stack,
9995 * nothing to put back when the NL is deleted.
9996 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02009997 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 replace_push(NUL);
9999
10000 /*
10001 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10002 * replacing the next line, so we push all of the characters left on the
10003 * line onto the replace stack. This is not done here though, it is done
10004 * in open_line().
10005 */
10006
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010007 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10008 * CTRL-O). */
10009 if (virtual_active() && curwin->w_cursor.coladd > 0)
10010 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010011
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 /* NL in reverse insert will always start in the end of
10014 * current line. */
10015 if (revins_on)
10016 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10017#endif
10018
10019 AppendToRedobuff(NL_STR);
10020 i = open_line(FORWARD,
10021#ifdef FEAT_COMMENTS
10022 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10023#endif
10024 0, old_indent);
10025 old_indent = 0;
10026#ifdef FEAT_CINDENT
10027 can_cindent = TRUE;
10028#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010029#ifdef FEAT_FOLDING
10030 /* When inserting a line the cursor line must never be in a closed fold. */
10031 foldOpenCursor();
10032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010034 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035}
10036
10037#ifdef FEAT_DIGRAPHS
10038/*
10039 * Handle digraph in insert mode.
10040 * Returns character still to be inserted, or NUL when nothing remaining to be
10041 * done.
10042 */
10043 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010044ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045{
10046 int c;
10047 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010048 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049
10050 pc_status = PC_STATUS_UNSET;
10051 if (redrawing() && !char_avail())
10052 {
10053 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010054 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055
10056 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010057 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058#ifdef FEAT_CMDL_INFO
10059 add_to_showcmd_c(Ctrl_K);
10060#endif
10061 }
10062
10063#ifdef USE_ON_FLY_SCROLL
10064 dont_scroll = TRUE; /* disallow scrolling here */
10065#endif
10066
10067 /* don't map the digraph chars. This also prevents the
10068 * mode message to be deleted when ESC is hit */
10069 ++no_mapping;
10070 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010071 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 --no_mapping;
10073 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010074 if (did_putchar)
10075 /* when the line fits in 'columns' the '?' is at the start of the next
10076 * line and will not be removed by the redraw */
10077 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010078
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 if (IS_SPECIAL(c) || mod_mask) /* special key */
10080 {
10081#ifdef FEAT_CMDL_INFO
10082 clear_showcmd();
10083#endif
10084 insert_special(c, TRUE, FALSE);
10085 return NUL;
10086 }
10087 if (c != ESC)
10088 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010089 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 if (redrawing() && !char_avail())
10091 {
10092 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010093 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094
10095 if (char2cells(c) == 1)
10096 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010097 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010099 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 }
10101#ifdef FEAT_CMDL_INFO
10102 add_to_showcmd_c(c);
10103#endif
10104 }
10105 ++no_mapping;
10106 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010107 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 --no_mapping;
10109 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010110 if (did_putchar)
10111 /* when the line fits in 'columns' the '?' is at the start of the
10112 * next line and will not be removed by a redraw */
10113 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 if (cc != ESC)
10115 {
10116 AppendToRedobuff((char_u *)CTRL_V_STR);
10117 c = getdigraph(c, cc, TRUE);
10118#ifdef FEAT_CMDL_INFO
10119 clear_showcmd();
10120#endif
10121 return c;
10122 }
10123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124#ifdef FEAT_CMDL_INFO
10125 clear_showcmd();
10126#endif
10127 return NUL;
10128}
10129#endif
10130
10131/*
10132 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10133 * Returns the char to be inserted, or NUL if none found.
10134 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010135 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010136ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137{
10138 int c;
10139 int temp;
10140 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010141 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142
10143 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10144 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010145 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 return NUL;
10147 }
10148
10149 /* try to advance to the cursor column */
10150 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010151 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 prev_ptr = ptr;
10153 validate_virtcol();
10154 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10155 {
10156 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010157 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 }
10159 if ((colnr_T)temp > curwin->w_virtcol)
10160 ptr = prev_ptr;
10161
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010164 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 return c;
10166}
10167
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010168/*
10169 * CTRL-Y or CTRL-E typed in Insert mode.
10170 */
10171 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010172ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010173{
10174 int c = tc;
10175
10176#ifdef FEAT_INS_EXPAND
10177 if (ctrl_x_mode == CTRL_X_SCROLL)
10178 {
10179 if (c == Ctrl_Y)
10180 scrolldown_clamp();
10181 else
10182 scrollup_clamp();
10183 redraw_later(VALID);
10184 }
10185 else
10186#endif
10187 {
10188 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10189 if (c != NUL)
10190 {
10191 long tw_save;
10192
10193 /* The character must be taken literally, insert like it
10194 * was typed after a CTRL-V, and pretend 'textwidth'
10195 * wasn't set. Digits, 'o' and 'x' are special after a
10196 * CTRL-V, don't use it for these. */
10197 if (c < 256 && !isalnum(c))
10198 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10199 tw_save = curbuf->b_p_tw;
10200 curbuf->b_p_tw = -1;
10201 insert_special(c, TRUE, FALSE);
10202 curbuf->b_p_tw = tw_save;
10203#ifdef FEAT_RIGHTLEFT
10204 revins_chars++;
10205 revins_legal++;
10206#endif
10207 c = Ctrl_V; /* pretend CTRL-V is last character */
10208 auto_format(FALSE, TRUE);
10209 }
10210 }
10211 return c;
10212}
10213
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214#ifdef FEAT_SMARTINDENT
10215/*
10216 * Try to do some very smart auto-indenting.
10217 * Used when inserting a "normal" character.
10218 */
10219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010220ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221{
10222 pos_T *pos, old_pos;
10223 char_u *ptr;
10224 int i;
10225 int temp;
10226
10227 /*
10228 * do some very smart indenting when entering '{' or '}'
10229 */
10230 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10231 {
10232 /*
10233 * for '}' set indent equal to indent of line containing matching '{'
10234 */
10235 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10236 {
10237 old_pos = curwin->w_cursor;
10238 /*
10239 * If the matching '{' has a ')' immediately before it (ignoring
10240 * white-space), then line up with the start of the line
10241 * containing the matching '(' if there is one. This handles the
10242 * case where an "if (..\n..) {" statement continues over multiple
10243 * lines -- webb
10244 */
10245 ptr = ml_get(pos->lnum);
10246 i = pos->col;
10247 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010248 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 ;
10250 curwin->w_cursor.lnum = pos->lnum;
10251 curwin->w_cursor.col = i;
10252 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10253 curwin->w_cursor = *pos;
10254 i = get_indent();
10255 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010257 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 (void)set_indent(i, SIN_CHANGED);
10260 }
10261 else if (curwin->w_cursor.col > 0)
10262 {
10263 /*
10264 * when inserting '{' after "O" reduce indent, but not
10265 * more than indent of previous line
10266 */
10267 temp = TRUE;
10268 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10269 {
10270 old_pos = curwin->w_cursor;
10271 i = get_indent();
10272 while (curwin->w_cursor.lnum > 1)
10273 {
10274 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10275
10276 /* ignore empty lines and lines starting with '#'. */
10277 if (*ptr != '#' && *ptr != NUL)
10278 break;
10279 }
10280 if (get_indent() >= i)
10281 temp = FALSE;
10282 curwin->w_cursor = old_pos;
10283 }
10284 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010285 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 }
10287 }
10288
10289 /*
10290 * set indent of '#' always to 0
10291 */
10292 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10293 {
10294 /* remember current indent for next line */
10295 old_indent = get_indent();
10296 (void)set_indent(0, SIN_CHANGED);
10297 }
10298
10299 /* Adjust ai_col, the char at this position can be deleted. */
10300 if (ai_col > curwin->w_cursor.col)
10301 ai_col = curwin->w_cursor.col;
10302}
10303#endif
10304
10305/*
10306 * Get the value that w_virtcol would have when 'list' is off.
10307 * Unless 'cpo' contains the 'L' flag.
10308 */
Bram Moolenaarf9514162018-11-22 03:08:29 +010010309 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010310get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010312 // check validity of cursor in current buffer
10313 if (curwin->w_buffer == NULL
10314 || curwin->w_buffer->b_ml.ml_mfp == NULL
10315 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
10316 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10318 return getvcol_nolist(&curwin->w_cursor);
10319 validate_virtcol();
10320 return curwin->w_virtcol;
10321}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010322
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010323#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010324/*
10325 * Handle the InsertCharPre autocommand.
10326 * "c" is the character that was typed.
10327 * Return a pointer to allocated memory with the replacement string.
10328 * Return NULL to continue inserting "c".
10329 */
10330 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010331do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010332{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010333 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010334 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010335 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010336
10337 /* Return quickly when there is nothing to do. */
10338 if (!has_insertcharpre())
10339 return NULL;
10340
Bram Moolenaar704984a2012-06-01 14:57:51 +020010341 if (has_mbyte)
10342 buf[(*mb_char2bytes)(c, buf)] = NUL;
10343 else
Bram Moolenaar704984a2012-06-01 14:57:51 +020010344 {
10345 buf[0] = c;
10346 buf[1] = NUL;
10347 }
10348
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010349 /* Lock the text to avoid weird things from happening. */
10350 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010351 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010352
Bram Moolenaar704984a2012-06-01 14:57:51 +020010353 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010354 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010355 {
10356 /* Get the value of v:char. It may be empty or more than one
10357 * character. Only use it when changed, otherwise continue with the
10358 * original character to avoid breaking autoindent. */
10359 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10360 res = vim_strsave(get_vim_var_str(VV_CHAR));
10361 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010362
10363 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10364 --textlock;
10365
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010366 // Restore the State, it may have been changed.
10367 State = save_State;
10368
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010369 return res;
10370}
10371#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010372
10373/*
10374 * Trigger "event" and take care of fixing undo.
10375 */
10376 static int
10377ins_apply_autocmds(event_T event)
10378{
10379 varnumber_T tick = CHANGEDTICK(curbuf);
10380 int r;
10381
10382 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
10383
10384 // If u_savesub() was called then we are not prepared to start
10385 // a new line. Call u_save() with no contents to fix that.
10386 if (tick != CHANGEDTICK(curbuf))
10387 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
10388
10389 return r;
10390}