blob: e20857df6d8ddf6e900cdd3a3c1fbf50588c21f7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010020# define CTRL_X_WANT_IDENT 0x100
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010022# define CTRL_X_NORMAL 0 /* CTRL-N CTRL-P completion, default */
23# define CTRL_X_NOT_DEFINED_YET 1
24# define CTRL_X_SCROLL 2
25# define CTRL_X_WHOLE_LINE 3
26# define CTRL_X_FILES 4
27# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
28# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
30# define CTRL_X_FINISHED 8
31# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
32# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
33# define CTRL_X_CMDLINE 11
34# define CTRL_X_FUNCTION 12
35# define CTRL_X_OMNI 13
36# define CTRL_X_SPELL 14
37# define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
38# define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010040# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
41# define CTRL_X_MODE_LINE_OR_EVAL(m) ((m) == CTRL_X_WHOLE_LINE || (m) == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010043/* Message for CTRL-X mode, index is ctrl_x_mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static char *ctrl_x_msgs[] =
45{
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010046 N_(" Keyword completion (^N^P)"), /* CTRL_X_NORMAL, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000047 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010048 NULL, /* CTRL_X_SCROLL: depends on state */
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 N_(" Whole line completion (^L^N^P)"),
50 N_(" File name completion (^F^N^P)"),
51 N_(" Tag completion (^]^N^P)"),
52 N_(" Path pattern completion (^N^P)"),
53 N_(" Definition completion (^D^N^P)"),
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010054 NULL, /* CTRL_X_FINISHED */
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 N_(" Dictionary completion (^K^N^P)"),
56 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000057 N_(" Command-line completion (^V^N^P)"),
58 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000059 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000060 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000061 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010062 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000063};
64
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000065static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010066# ifdef FEAT_COMPL_FUNC
Bram Moolenaar37dd0182010-11-10 16:54:20 +010067static char e_complwin[] = N_("E839: Completion function changed window");
68static char e_compldel[] = N_("E840: Completion function deleted text");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71/*
72 * Structure used to store one match for insert completion.
73 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000074typedef struct compl_S compl_T;
75struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
Bram Moolenaar572cb562005-08-05 21:35:02 +000077 compl_T *cp_next;
78 compl_T *cp_prev;
79 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000080 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000081 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000082 char_u *cp_fname; /* file containing the match, allocated when
83 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000084 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
85 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086};
87
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010088# define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
89# define FREE_FNAME (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
91/*
92 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000093 * "compl_first_match" points to the start of the list.
94 * "compl_curr_match" points to the currently selected entry.
95 * "compl_shown_match" is different from compl_curr_match during
96 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000098static compl_T *compl_first_match = NULL;
99static compl_T *compl_curr_match = NULL;
100static compl_T *compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +0200101static compl_T *compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000103/* After using a cursor key <Enter> selects a match in the popup menu,
104 * otherwise it inserts a line break. */
105static int compl_enter_selects = FALSE;
106
Bram Moolenaara6557602006-02-04 22:43:20 +0000107/* When "compl_leader" is not NULL only matches that start with this string
108 * are used. */
109static char_u *compl_leader = NULL;
110
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000111static int compl_get_longest = FALSE; /* put longest common string
112 in compl_leader */
113
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200114static int compl_no_insert = FALSE; /* FALSE: select & insert
115 TRUE: noinsert */
116static int compl_no_select = FALSE; /* FALSE: select & insert
117 TRUE: noselect */
118
Bram Moolenaara6557602006-02-04 22:43:20 +0000119static int compl_used_match; /* Selected one of the matches. When
120 FALSE the match was edited or using
121 the longest common string. */
122
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000123static int compl_was_interrupted = FALSE; /* didn't finish finding
124 completions. */
125
126static int compl_restarting = FALSE; /* don't insert match */
127
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128/* When the first completion is done "compl_started" is set. When it's
129 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000130static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000131
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100132/* Which Ctrl-X mode are we in? */
133static int ctrl_x_mode = CTRL_X_NORMAL;
134
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000135/* Set when doing something for completion that may call edit() recursively,
136 * which is not allowed. */
137static int compl_busy = FALSE;
138
Bram Moolenaar572cb562005-08-05 21:35:02 +0000139static int compl_matches = 0;
140static char_u *compl_pattern = NULL;
141static int compl_direction = FORWARD;
142static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000143static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000144static pos_T compl_startpos;
145static colnr_T compl_col = 0; /* column where the text starts
146 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000147static char_u *compl_orig_text = NULL; /* text as it was before
148 * completion started */
149static int compl_cont_mode = 0;
150static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
Bram Moolenaar82139082011-09-14 16:52:09 +0200152static int compl_opt_refresh_always = FALSE;
153
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static void ins_ctrl_x(void);
155static int has_compl_option(int dict_opt);
156static int ins_compl_accept_char(int c);
157static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
158static int ins_compl_equal(compl_T *match, char_u *str, int len);
159static void ins_compl_longest_match(compl_T *match);
160static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
161static int ins_compl_make_cyclic(void);
162static void ins_compl_upd_pum(void);
163static void ins_compl_del_pum(void);
164static int pum_wanted(void);
165static int pum_enough_matches(void);
166static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
167static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
168static char_u *find_line_end(char_u *ptr);
169static void ins_compl_free(void);
170static void ins_compl_clear(void);
171static int ins_compl_bs(void);
172static int ins_compl_need_restart(void);
173static void ins_compl_new_leader(void);
174static void ins_compl_addleader(int c);
175static int ins_compl_len(void);
176static void ins_compl_restart(void);
177static void ins_compl_set_original_text(char_u *str);
178static void ins_compl_addfrommatch(void);
179static int ins_compl_prep(int c);
180static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
181static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100182# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100183static void ins_compl_add_list(list_T *list);
184static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100185# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static int ins_compl_get_exp(pos_T *ini);
187static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200188static void ins_compl_insert(int in_compl_func);
189static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static int ins_compl_key2dir(int c);
191static int ins_compl_pum_key(int c);
192static int ins_compl_key2count(int c);
193static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100194static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100195static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100196static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#endif /* FEAT_INS_EXPAND */
198
199#define BACKSPACE_CHAR 1
200#define BACKSPACE_WORD 2
201#define BACKSPACE_WORD_NOT_SPACE 3
202#define BACKSPACE_LINE 4
203
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ins_redraw(int ready);
205static void ins_ctrl_v(void);
206static void undisplay_dollar(void);
207static void insert_special(int, int, int);
208static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
209static void check_auto_format(int);
210static void redo_literal(int c);
211static void start_arrow(pos_T *end_insert_pos);
212static void start_arrow_with_change(pos_T *end_insert_pos, int change);
213static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000214#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void check_spell_redraw(void);
216static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000217static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000218#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
220static int echeck_abbr(int);
221static int replace_pop(void);
222static void replace_join(int off);
223static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void replace_flush(void);
228static void replace_do_bs(int limit_col);
229static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100233static void ins_reg(void);
234static void ins_ctrl_g(void);
235static void ins_ctrl_hat(void);
236static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static int ins_start_select(int c);
241static void ins_insert(int replaceState);
242static void ins_ctrl_o(void);
243static void ins_shift(int c, int lastc);
244static void ins_del(void);
245static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ins_mouse(int c);
248static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000250#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000252#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void ins_left(int end_change);
254static void ins_home(int c);
255static void ins_end(int c);
256static void ins_s_left(void);
257static void ins_right(int end_change);
258static void ins_s_right(void);
259static void ins_up(int startcol);
260static void ins_pageup(void);
261static void ins_down(int startcol);
262static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100266static int ins_tab(void);
267static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100273static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100275static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100276#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100277static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280static colnr_T Insstart_textlen; /* length of line when insert started */
281static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100282static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284static char_u *last_insert = NULL; /* the text of the previous insert,
285 K_SPECIAL and CSI are escaped */
286static int last_insert_skip; /* nr of chars in front of previous insert */
287static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000288static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289
290#ifdef FEAT_CINDENT
291static int can_cindent; /* may do cindenting on this line */
292#endif
293
294static int old_indent = 0; /* for ^^D command in insert mode */
295
296#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000297static int revins_on; /* reverse insert mode on */
298static int revins_chars; /* how much to skip after edit */
299static int revins_legal; /* was the last char 'legal'? */
300static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#endif
302
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303static int ins_need_undo; /* call u_save() before inserting a
304 char. Set when edit() is called.
305 after that arrow_used is used. */
306
307static int did_add_space = FALSE; /* auto_format() added an extra space
308 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200309static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
310 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
312/*
313 * edit(): Start inserting text.
314 *
315 * "cmdchar" can be:
316 * 'i' normal insert command
317 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100318 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 * 'R' replace command
320 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
321 * but still only one <CR> is inserted. The <Esc> is not used for redo.
322 * 'g' "gI" command.
323 * 'V' "gR" command for Virtual Replace mode.
324 * 'v' "gr" command for single character Virtual Replace mode.
325 *
326 * This function is not called recursively. For CTRL-O commands, it returns
327 * and lets the caller handle the Normal-mode command.
328 *
329 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
330 */
331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100332edit(
333 int cmdchar,
334 int startln, /* if set, insert at start of line */
335 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 int c = 0;
338 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100339 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000340 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 int i;
343 int did_backspace = TRUE; /* previous char was backspace */
344#ifdef FEAT_CINDENT
345 int line_is_white = FALSE; /* line is empty before insert */
346#endif
347 linenr_T old_topline = 0; /* topline before insertion */
348#ifdef FEAT_DIFF
349 int old_topfill = -1;
350#endif
351 int inserted_space = FALSE; /* just inserted a space */
352 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000353 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000355 /* Remember whether editing was restarted after CTRL-O. */
356 did_restart_edit = restart_edit;
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 /* sleep before redrawing, needed for "CTRL-O :" that results in an
359 * error message */
360 check_for_delay(TRUE);
361
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100362 /* set Insstart_orig to Insstart */
363 update_Insstart_orig = TRUE;
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365#ifdef HAVE_SANDBOX
366 /* Don't allow inserting in the sandbox. */
367 if (sandbox != 0)
368 {
369 EMSG(_(e_sandbox));
370 return FALSE;
371 }
372#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000373 /* Don't allow changes in the buffer while editing the cmdline. The
374 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000375 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000376 {
377 EMSG(_(e_secure));
378 return FALSE;
379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380
381#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000382 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000383 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000384 {
385 EMSG(_(e_secure));
386 return FALSE;
387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 ins_compl_clear(); /* clear stuff for CTRL-X mode */
389#endif
390
Bram Moolenaar843ee412004-06-30 16:16:41 +0000391 /*
392 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
393 */
394 if (cmdchar != 'r' && cmdchar != 'v')
395 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100396 pos_T save_cursor = curwin->w_cursor;
397
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100398#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000399 if (cmdchar == 'R')
400 ptr = (char_u *)"r";
401 else if (cmdchar == 'V')
402 ptr = (char_u *)"v";
403 else
404 ptr = (char_u *)"i";
405 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200406 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100407#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000408 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100409
Bram Moolenaar097c9922013-05-19 21:15:15 +0200410 /* Make sure the cursor didn't move. Do call check_cursor_col() in
411 * case the text was modified. Since Insert mode was not started yet
412 * a call to check_cursor_col() may move the cursor, especially with
413 * the "A" command, thus set State to avoid that. Also check that the
414 * line number is still valid (lines may have been deleted).
415 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100416 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100417#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200418 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100419#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200420 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100421 {
422 int save_state = State;
423
424 curwin->w_cursor = save_cursor;
425 State = INSERT;
426 check_cursor_col();
427 State = save_state;
428 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000429 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000430
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200431#ifdef FEAT_CONCEAL
432 /* Check if the cursor line needs redrawing before changing State. If
433 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200434 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200435#endif
436
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#ifdef FEAT_MOUSE
438 /*
439 * When doing a paste with the middle mouse button, Insstart is set to
440 * where the paste started.
441 */
442 if (where_paste_started.lnum != 0)
443 Insstart = where_paste_started;
444 else
445#endif
446 {
447 Insstart = curwin->w_cursor;
448 if (startln)
449 Insstart.col = 0;
450 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000451 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 Insstart_blank_vcol = MAXCOL;
453 if (!did_ai)
454 ai_col = 0;
455
456 if (cmdchar != NUL && restart_edit == 0)
457 {
458 ResetRedobuff();
459 AppendNumberToRedobuff(count);
460#ifdef FEAT_VREPLACE
461 if (cmdchar == 'V' || cmdchar == 'v')
462 {
463 /* "gR" or "gr" command */
464 AppendCharToRedobuff('g');
465 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
466 }
467 else
468#endif
469 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100470 if (cmdchar == K_PS)
471 AppendCharToRedobuff('a');
472 else
473 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 if (cmdchar == 'g') /* "gI" command */
475 AppendCharToRedobuff('I');
476 else if (cmdchar == 'r') /* "r<CR>" command */
477 count = 1; /* insert only one <CR> */
478 }
479 }
480
481 if (cmdchar == 'R')
482 {
483#ifdef FEAT_FKMAP
484 if (p_fkmap && p_ri)
485 {
486 beep_flush();
487 EMSG(farsi_text_3); /* encoded in Farsi */
488 State = INSERT;
489 }
490 else
491#endif
492 State = REPLACE;
493 }
494#ifdef FEAT_VREPLACE
495 else if (cmdchar == 'V' || cmdchar == 'v')
496 {
497 State = VREPLACE;
498 replaceState = VREPLACE;
499 orig_line_count = curbuf->b_ml.ml_line_count;
500 vr_lines_changed = 1;
501 }
502#endif
503 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;
580#ifdef FEAT_MBYTE
581 else if (has_mbyte)
582 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000583 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 if (ptr[i] == NUL)
585 curwin->w_cursor.col += i;
586 }
587#endif
588 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000589 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 }
591 else
592 arrow_used = FALSE;
593
594 /* we are in insert mode now, don't need to start it anymore */
595 need_start_insertmode = FALSE;
596
597 /* Need to save the line for undo before inserting the first char. */
598 ins_need_undo = TRUE;
599
600#ifdef FEAT_MOUSE
601 where_paste_started.lnum = 0;
602#endif
603#ifdef FEAT_CINDENT
604 can_cindent = TRUE;
605#endif
606#ifdef FEAT_FOLDING
607 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
608 * restarting. */
609 if (!p_im && did_restart_edit == 0)
610 foldOpenCursor();
611#endif
612
613 /*
614 * If 'showmode' is set, show the current (insert/replace/..) mode.
615 * A warning message for changing a readonly file is given here, before
616 * actually changing anything. It's put after the mode, if any.
617 */
618 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000619 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 i = showmode();
621
622 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000623 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625#ifdef CURSOR_SHAPE
626 ui_cursor_shape(); /* may show different cursor shape */
627#endif
628#ifdef FEAT_DIGRAPHS
629 do_digraph(-1); /* clear digraphs */
630#endif
631
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000632 /*
633 * Get the current length of the redo buffer, those characters have to be
634 * skipped if we want to get to the inserted characters.
635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 ptr = get_inserted();
637 if (ptr == NULL)
638 new_insert_skip = 0;
639 else
640 {
641 new_insert_skip = (int)STRLEN(ptr);
642 vim_free(ptr);
643 }
644
645 old_indent = 0;
646
647 /*
648 * Main loop in Insert mode: repeat until Insert mode is left.
649 */
650 for (;;)
651 {
652#ifdef FEAT_RIGHTLEFT
653 if (!revins_legal)
654 revins_scol = -1; /* reset on illegal motions */
655 else
656 revins_legal = 0;
657#endif
658 if (arrow_used) /* don't repeat insert when arrow key used */
659 count = 0;
660
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100661 if (update_Insstart_orig)
662 Insstart_orig = Insstart;
663
Bram Moolenaar00672e12016-06-26 18:38:13 +0200664 if (stop_insert_mode
665#ifdef FEAT_INS_EXPAND
666 && !pum_visible()
667#endif
668 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {
670 /* ":stopinsert" used or 'insertmode' reset */
671 count = 0;
672 goto doESCkey;
673 }
674
675 /* set curwin->w_curswant for next K_DOWN or K_UP */
676 if (!arrow_used)
677 curwin->w_set_curswant = TRUE;
678
679 /* If there is no typeahead may check for timestamps (e.g., for when a
680 * menu invoked a shell command). */
681 if (stuff_empty())
682 {
683 did_check_timestamps = FALSE;
684 if (need_check_timestamps)
685 check_timestamps(FALSE);
686 }
687
688 /*
689 * When emsg() was called msg_scroll will have been set.
690 */
691 msg_scroll = FALSE;
692
693#ifdef FEAT_GUI
694 /* When 'mousefocus' is set a mouse movement may have taken us to
695 * another window. "need_mouse_correct" may then be set because of an
696 * autocommand. */
697 if (need_mouse_correct)
698 gui_mouse_correct();
699#endif
700
701#ifdef FEAT_FOLDING
702 /* Open fold at the cursor line, according to 'foldopen'. */
703 if (fdo_flags & FDO_INSERT)
704 foldOpenCursor();
705 /* Close folds where the cursor isn't, according to 'foldclose' */
706 if (!char_avail())
707 foldCheckClose();
708#endif
709
710 /*
711 * If we inserted a character at the last position of the last line in
712 * the window, scroll the window one line up. This avoids an extra
713 * redraw.
714 * This is detected when the cursor column is smaller after inserting
715 * something.
716 * Don't do this when the topline changed already, it has
717 * already been adjusted (by insertchar() calling open_line())).
718 */
719 if (curbuf->b_mod_set
720 && curwin->w_p_wrap
721 && !did_backspace
722 && curwin->w_topline == old_topline
723#ifdef FEAT_DIFF
724 && curwin->w_topfill == old_topfill
725#endif
726 )
727 {
728 mincol = curwin->w_wcol;
729 validate_cursor_col();
730
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000731 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 && curwin->w_wrow == W_WINROW(curwin)
733 + curwin->w_height - 1 - p_so
734 && (curwin->w_cursor.lnum != curwin->w_topline
735#ifdef FEAT_DIFF
736 || curwin->w_topfill > 0
737#endif
738 ))
739 {
740#ifdef FEAT_DIFF
741 if (curwin->w_topfill > 0)
742 --curwin->w_topfill;
743 else
744#endif
745#ifdef FEAT_FOLDING
746 if (hasFolding(curwin->w_topline, NULL, &old_topline))
747 set_topline(curwin, old_topline + 1);
748 else
749#endif
750 set_topline(curwin, curwin->w_topline + 1);
751 }
752 }
753
754 /* May need to adjust w_topline to show the cursor. */
755 update_topline();
756
757 did_backspace = FALSE;
758
759 validate_cursor(); /* may set must_redraw */
760
761 /*
762 * Redraw the display when no characters are waiting.
763 * Also shows mode, ruler and positions cursor.
764 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000765 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
767#ifdef FEAT_SCROLLBIND
768 if (curwin->w_p_scb)
769 do_check_scrollbind(TRUE);
770#endif
771
Bram Moolenaar860cae12010-06-05 23:22:07 +0200772#ifdef FEAT_CURSORBIND
773 if (curwin->w_p_crb)
774 do_check_cursorbind();
775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 update_curswant();
777 old_topline = curwin->w_topline;
778#ifdef FEAT_DIFF
779 old_topfill = curwin->w_topfill;
780#endif
781
782#ifdef USE_ON_FLY_SCROLL
783 dont_scroll = FALSE; /* allow scrolling here */
784#endif
785
786 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100787 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100789 if (c != K_CURSORHOLD)
790 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200791
792 /* After using CTRL-G U the next cursor key will not break undo. */
793 if (dont_sync_undo == MAYBE)
794 dont_sync_undo = TRUE;
795 else
796 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100797 if (cmdchar == K_PS)
798 /* Got here from normal mode when bracketed paste started. */
799 c = K_PS;
800 else
801 do
802 {
803 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100804 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000806 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
807 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809#ifdef FEAT_RIGHTLEFT
810 if (p_hkmap && KeyTyped)
811 c = hkmap(c); /* Hebrew mode mapping */
812#endif
813#ifdef FEAT_FKMAP
814 if (p_fkmap && KeyTyped)
815 c = fkmap(c); /* Farsi mode mapping */
816#endif
817
818#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000819 /*
820 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000821 * and the cursor is still in the completed word. Only when there is
822 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000823 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000824 if (compl_started
825 && pum_wanted()
826 && curwin->w_cursor.col >= compl_col
827 && (compl_shown_match == NULL
828 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000829 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000830 /* BS: Delete one character from "compl_leader". */
831 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000832 && curwin->w_cursor.col > compl_col
833 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000834 continue;
835
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000836 /* When no match was selected or it was edited. */
837 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000838 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000839 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000840 * "compl_leader". Except when at the original match and
841 * there is nothing to add, CTRL-L works like CTRL-P then. */
842 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100843 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000844 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000845 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000846 {
847 ins_compl_addfrommatch();
848 continue;
849 }
850
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000851 /* A non-white character that fits in with the current
852 * completion: Add to "compl_leader". */
853 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000854 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100855#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100856 /* Trigger InsertCharPre. */
857 char_u *str = do_insert_char_pre(c);
858 char_u *p;
859
860 if (str != NULL)
861 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100862 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100863 ins_compl_addleader(PTR2CHAR(p));
864 vim_free(str);
865 }
866 else
867#endif
868 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000869 continue;
870 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000871
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000872 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000873 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200874 if ((c == Ctrl_Y || (compl_enter_selects
875 && (c == CAR || c == K_KENTER || c == NL)))
876 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000877 {
878 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200879 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000880 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000881 }
882 }
883
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
885 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000886 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000887 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000888 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889#endif
890
Bram Moolenaar488c6512005-08-11 20:09:58 +0000891 /* CTRL-\ CTRL-N goes to Normal mode,
892 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
893 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (c == Ctrl_BSL)
895 {
896 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000897 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 ++no_mapping;
899 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000900 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 --no_mapping;
902 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000903 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000905 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 vungetc(c);
907 c = Ctrl_BSL;
908 }
909 else if (c == Ctrl_G && p_im)
910 continue;
911 else
912 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000913 if (c == Ctrl_O)
914 {
915 ins_ctrl_o();
916 ins_at_eol = FALSE; /* cursor keeps its column */
917 nomove = TRUE;
918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 count = 0;
920 goto doESCkey;
921 }
922 }
923
924#ifdef FEAT_DIGRAPHS
925 c = do_digraph(c);
926#endif
927
928#ifdef FEAT_INS_EXPAND
929 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
930 goto docomplete;
931#endif
932 if (c == Ctrl_V || c == Ctrl_Q)
933 {
934 ins_ctrl_v();
935 c = Ctrl_V; /* pretend CTRL-V is last typed character */
936 continue;
937 }
938
939#ifdef FEAT_CINDENT
940 if (cindent_on()
941# ifdef FEAT_INS_EXPAND
942 && ctrl_x_mode == 0
943# endif
944 )
945 {
946 /* A key name preceded by a bang means this key is not to be
947 * inserted. Skip ahead to the re-indenting below.
948 * A key name preceded by a star means that indenting has to be
949 * done before inserting the key. */
950 line_is_white = inindent(0);
951 if (in_cinkeys(c, '!', line_is_white))
952 goto force_cindent;
953 if (can_cindent && in_cinkeys(c, '*', line_is_white)
954 && stop_arrow() == OK)
955 do_c_expr_indent();
956 }
957#endif
958
959#ifdef FEAT_RIGHTLEFT
960 if (curwin->w_p_rl)
961 switch (c)
962 {
963 case K_LEFT: c = K_RIGHT; break;
964 case K_S_LEFT: c = K_S_RIGHT; break;
965 case K_C_LEFT: c = K_C_RIGHT; break;
966 case K_RIGHT: c = K_LEFT; break;
967 case K_S_RIGHT: c = K_S_LEFT; break;
968 case K_C_RIGHT: c = K_C_LEFT; break;
969 }
970#endif
971
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 /*
973 * If 'keymodel' contains "startsel", may start selection. If it
974 * does, a CTRL-O and c will be stuffed, we need to get these
975 * characters.
976 */
977 if (ins_start_select(c))
978 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
980 /*
981 * The big switch to handle a character in insert mode.
982 */
983 switch (c)
984 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000985 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 if (echeck_abbr(ESC + ABBR_OFF))
987 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200988 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000990 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef FEAT_CMDWIN
992 if (c == Ctrl_C && cmdwin_type != 0)
993 {
994 /* Close the cmdline window. */
995 cmdwin_result = K_IGNORE;
996 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000997 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 goto doESCkey;
999 }
1000#endif
1001
1002#ifdef UNIX
1003do_intr:
1004#endif
1005 /* when 'insertmode' set, and not halfway a mapping, don't leave
1006 * Insert mode */
1007 if (goto_im())
1008 {
1009 if (got_int)
1010 {
1011 (void)vgetc(); /* flush all buffers */
1012 got_int = FALSE;
1013 }
1014 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001015 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 break;
1017 }
1018doESCkey:
1019 /*
1020 * This is the ONLY return from edit()!
1021 */
1022 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1023 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001024 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 o_lnum = curwin->w_cursor.lnum;
1026
Bram Moolenaar488c6512005-08-11 20:09:58 +00001027 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001028 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00001029 if (cmdchar != 'r' && cmdchar != 'v')
1030 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1031 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001032 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 continue;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case Ctrl_Z: /* suspend when 'insertmode' set */
1038 if (!p_im)
1039 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001040 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001041#ifdef CURSOR_SHAPE
1042 ui_cursor_shape(); /* may need to update cursor shape */
1043#endif
1044 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045
1046 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001047#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001048 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 goto docomplete;
1050#endif
1051 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1052 break;
1053 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001054
1055#ifdef FEAT_VIRTUALEDIT
1056 /* don't move the cursor left when 'virtualedit' has "onemore". */
1057 if (ve_flags & VE_ONEMORE)
1058 {
1059 ins_at_eol = FALSE;
1060 nomove = TRUE;
1061 }
1062#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001063 count = 0;
1064 goto doESCkey;
1065
Bram Moolenaar572cb562005-08-05 21:35:02 +00001066 case K_INS: /* toggle insert/replace mode */
1067 case K_KINS:
1068 ins_insert(replaceState);
1069 break;
1070
1071 case K_SELECT: /* end of Select mode mapping - ignore */
1072 break;
1073
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001074 case K_HELP: /* Help key works like <ESC> <Help> */
1075 case K_F1:
1076 case K_XF1:
1077 stuffcharReadbuff(K_HELP);
1078 if (p_im)
1079 need_start_insertmode = TRUE;
1080 goto doESCkey;
1081
1082#ifdef FEAT_NETBEANS_INTG
1083 case K_F21: /* NetBeans command */
1084 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001085 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 --no_mapping;
1087 netbeans_keycommand(i);
1088 break;
1089#endif
1090
1091 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 case NUL:
1093 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 /* For ^@ the trailing ESC will end the insert, unless there is an
1095 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1097 && c != Ctrl_A && !p_im)
1098 goto doESCkey; /* quit insert mode */
1099 inserted_space = FALSE;
1100 break;
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 ins_reg();
1104 auto_format(FALSE, TRUE);
1105 inserted_space = FALSE;
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_ctrl_g();
1110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case Ctrl_HAT: /* switch input mode and/or langmap */
1113 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 break;
1115
1116#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 if (!p_ari)
1119 goto normalchar;
1120 ins_ctrl_();
1121 break;
1122#endif
1123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1126 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1127 goto docomplete;
1128#endif
1129 /* FALLTHROUGH */
1130
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001131 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132# ifdef FEAT_INS_EXPAND
1133 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1134 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 if (has_compl_option(FALSE))
1136 goto docomplete;
1137 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139# endif
1140 ins_shift(c, lastc);
1141 auto_format(FALSE, TRUE);
1142 inserted_space = FALSE;
1143 break;
1144
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 case K_KDEL:
1147 ins_del();
1148 auto_format(FALSE, TRUE);
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case Ctrl_H:
1153 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1154 auto_format(FALSE, TRUE);
1155 break;
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1159 auto_format(FALSE, TRUE);
1160 break;
1161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001162 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001163# ifdef FEAT_COMPL_FUNC
1164 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001166 goto docomplete;
1167# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1169 auto_format(FALSE, TRUE);
1170 inserted_space = FALSE;
1171 break;
1172
1173#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 case K_LEFTMOUSE_NM:
1176 case K_LEFTDRAG:
1177 case K_LEFTRELEASE:
1178 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001179 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 case K_MIDDLEMOUSE:
1181 case K_MIDDLEDRAG:
1182 case K_MIDDLERELEASE:
1183 case K_RIGHTMOUSE:
1184 case K_RIGHTDRAG:
1185 case K_RIGHTRELEASE:
1186 case K_X1MOUSE:
1187 case K_X1DRAG:
1188 case K_X1RELEASE:
1189 case K_X2MOUSE:
1190 case K_X2DRAG:
1191 case K_X2RELEASE:
1192 ins_mouse(c);
1193 break;
1194
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001195 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001196 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 break;
1198
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001199 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001200 ins_mousescroll(MSCR_UP);
1201 break;
1202
1203 case K_MOUSELEFT: /* Scroll wheel left */
1204 ins_mousescroll(MSCR_LEFT);
1205 break;
1206
1207 case K_MOUSERIGHT: /* Scroll wheel right */
1208 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 break;
1210#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001211 case K_PS:
1212 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1213 if (cmdchar == K_PS)
1214 /* invoked from normal mode, bail out */
1215 goto doESCkey;
1216 break;
1217 case K_PE:
1218 /* Got K_PE without K_PS, ignore. */
1219 break;
1220
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001221#ifdef FEAT_GUI_TABLINE
1222 case K_TABLINE:
1223 case K_TABMENU:
1224 ins_tabline(c);
1225 break;
1226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001228 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 break;
1230
Bram Moolenaar754b5602006-02-09 23:53:20 +00001231 case K_CURSORHOLD: /* Didn't type something for a while. */
1232 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1233 did_cursorhold = TRUE;
1234 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001235
Bram Moolenaar4770d092006-01-12 23:22:24 +00001236#ifdef FEAT_GUI_W32
1237 /* On Win32 ignore <M-F4>, we get it when closing the window was
1238 * cancelled. */
1239 case K_F4:
1240 if (mod_mask != MOD_MASK_ALT)
1241 goto normalchar;
1242 break;
1243#endif
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245#ifdef FEAT_GUI
1246 case K_VER_SCROLLBAR:
1247 ins_scroll();
1248 break;
1249
1250 case K_HOR_SCROLLBAR:
1251 ins_horscroll();
1252 break;
1253#endif
1254
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001255 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 case K_S_HOME:
1258 case K_C_HOME:
1259 ins_home(c);
1260 break;
1261
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001262 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 case K_S_END:
1265 case K_C_END:
1266 ins_end(c);
1267 break;
1268
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001269 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001270 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1271 ins_s_left();
1272 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001273 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 break;
1275
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001276 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 case K_C_LEFT:
1278 ins_s_left();
1279 break;
1280
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001281 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001282 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1283 ins_s_right();
1284 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001285 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 break;
1287
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001288 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 case K_C_RIGHT:
1290 ins_s_right();
1291 break;
1292
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001293 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001294#ifdef FEAT_INS_EXPAND
1295 if (pum_visible())
1296 goto docomplete;
1297#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001298 if (mod_mask & MOD_MASK_SHIFT)
1299 ins_pageup();
1300 else
1301 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 break;
1303
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001304 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 case K_PAGEUP:
1306 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001307#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001308 if (pum_visible())
1309 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 ins_pageup();
1312 break;
1313
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001314 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001315#ifdef FEAT_INS_EXPAND
1316 if (pum_visible())
1317 goto docomplete;
1318#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001319 if (mod_mask & MOD_MASK_SHIFT)
1320 ins_pagedown();
1321 else
1322 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 break;
1324
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001325 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 case K_PAGEDOWN:
1327 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001328#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001329 if (pum_visible())
1330 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 ins_pagedown();
1333 break;
1334
1335#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001336 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 ins_drop();
1338 break;
1339#endif
1340
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001341 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 c = TAB;
1343 /* FALLTHROUGH */
1344
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001345 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1347 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1348 goto docomplete;
1349#endif
1350 inserted_space = FALSE;
1351 if (ins_tab())
1352 goto normalchar; /* insert TAB as a normal char */
1353 auto_format(FALSE, TRUE);
1354 break;
1355
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001356 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 c = CAR;
1358 /* FALLTHROUGH */
1359 case CAR:
1360 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001361#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 /* In a quickfix window a <CR> jumps to the error under the
1363 * cursor. */
1364 if (bt_quickfix(curbuf) && c == CAR)
1365 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001366 if (curwin->w_llist_ref == NULL) /* quickfix window */
1367 do_cmdline_cmd((char_u *)".cc");
1368 else /* location list window */
1369 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 break;
1371 }
1372#endif
1373#ifdef FEAT_CMDWIN
1374 if (cmdwin_type != 0)
1375 {
1376 /* Execute the command in the cmdline window. */
1377 cmdwin_result = CAR;
1378 goto doESCkey;
1379 }
1380#endif
1381 if (ins_eol(c) && !p_im)
1382 goto doESCkey; /* out of memory */
1383 auto_format(FALSE, FALSE);
1384 inserted_space = FALSE;
1385 break;
1386
Bram Moolenaar860cae12010-06-05 23:22:07 +02001387#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001388 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389# ifdef FEAT_INS_EXPAND
1390 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1391 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001392 if (has_compl_option(TRUE))
1393 goto docomplete;
1394 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396# endif
1397# ifdef FEAT_DIGRAPHS
1398 c = ins_digraph();
1399 if (c == NUL)
1400 break;
1401# endif
1402 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
1405#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001406 case Ctrl_X: /* Enter CTRL-X mode */
1407 ins_ctrl_x();
1408 break;
1409
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001410 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if (ctrl_x_mode != CTRL_X_TAGS)
1412 goto normalchar;
1413 goto docomplete;
1414
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001415 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (ctrl_x_mode != CTRL_X_FILES)
1417 goto normalchar;
1418 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001419
1420 case 's': /* Spelling completion after ^X */
1421 case Ctrl_S:
1422 if (ctrl_x_mode != CTRL_X_SPELL)
1423 goto normalchar;
1424 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425#endif
1426
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001427 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428#ifdef FEAT_INS_EXPAND
1429 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1430#endif
1431 {
1432 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1433 if (p_im)
1434 {
1435 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1436 break;
1437 goto doESCkey;
1438 }
1439 goto normalchar;
1440 }
1441#ifdef FEAT_INS_EXPAND
1442 /* FALLTHROUGH */
1443
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001444 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 case Ctrl_N:
1446 /* if 'complete' is empty then plain ^P is no longer special,
1447 * but it is under other ^X modes */
1448 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001449 && (ctrl_x_mode == CTRL_X_NORMAL
1450 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001451 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 goto normalchar;
1453
1454docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001455 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001456#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001457 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001458#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001459 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001460 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001461#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001462 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001463#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001464 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 break;
1466#endif /* FEAT_INS_EXPAND */
1467
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001468 case Ctrl_Y: /* copy from previous line or scroll down */
1469 case Ctrl_E: /* copy from next line or scroll up */
1470 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 break;
1472
1473 default:
1474#ifdef UNIX
1475 if (c == intr_char) /* special interrupt char */
1476 goto do_intr;
1477#endif
1478
Bram Moolenaare659c952011-05-19 17:25:41 +02001479normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001481 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001483#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001484 if (!p_paste)
1485 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001486 /* Trigger InsertCharPre. */
1487 char_u *str = do_insert_char_pre(c);
1488 char_u *p;
1489
1490 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001491 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001492 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001493 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001494 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001495 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001496 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001497 c = PTR2CHAR(p);
1498 if (c == CAR || c == K_KENTER || c == NL)
1499 ins_eol(c);
1500 else
1501 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001502 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001503 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001504 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001505 vim_free(str);
1506 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001507 }
1508
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001509 /* If the new value is already inserted or an empty string
1510 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001511 if (c == NUL)
1512 break;
1513 }
1514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515#ifdef FEAT_SMARTINDENT
1516 /* Try to perform smart-indenting. */
1517 ins_try_si(c);
1518#endif
1519
1520 if (c == ' ')
1521 {
1522 inserted_space = TRUE;
1523#ifdef FEAT_CINDENT
1524 if (inindent(0))
1525 can_cindent = FALSE;
1526#endif
1527 if (Insstart_blank_vcol == MAXCOL
1528 && curwin->w_cursor.lnum == Insstart.lnum)
1529 Insstart_blank_vcol = get_nolist_virtcol();
1530 }
1531
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001532 /* Insert a normal character and check for abbreviations on a
1533 * special character. Let CTRL-] expand abbreviations without
1534 * inserting it. */
1535 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#ifdef FEAT_MBYTE
1537 /* Add ABBR_OFF for characters above 0x100, this is
1538 * what check_abbr() expects. */
1539 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1540#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001541 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
1543 insert_special(c, FALSE, FALSE);
1544#ifdef FEAT_RIGHTLEFT
1545 revins_legal++;
1546 revins_chars++;
1547#endif
1548 }
1549
1550 auto_format(FALSE, TRUE);
1551
1552#ifdef FEAT_FOLDING
1553 /* When inserting a character the cursor line must never be in a
1554 * closed fold. */
1555 foldOpenCursor();
1556#endif
1557 break;
1558 } /* end of switch (c) */
1559
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001560 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001561 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001562#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001563 /* but not in CTRL-X mode, a script can't restore the state */
1564 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001565#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001566 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001567 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001568
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 /* If the cursor was moved we didn't just insert a space */
1570 if (arrow_used)
1571 inserted_space = FALSE;
1572
1573#ifdef FEAT_CINDENT
1574 if (can_cindent && cindent_on()
1575# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001576 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577# endif
1578 )
1579 {
1580force_cindent:
1581 /*
1582 * Indent now if a key was typed that is in 'cinkeys'.
1583 */
1584 if (in_cinkeys(c, ' ', line_is_white))
1585 {
1586 if (stop_arrow() == OK)
1587 /* re-indent the current line */
1588 do_c_expr_indent();
1589 }
1590 }
1591#endif /* FEAT_CINDENT */
1592
1593 } /* for (;;) */
1594 /* NOTREACHED */
1595}
1596
1597/*
1598 * Redraw for Insert mode.
1599 * This is postponed until getting the next character to make '$' in the 'cpo'
1600 * option work correctly.
1601 * Only redraw when there are no characters available. This speeds up
1602 * inserting sequences of characters (e.g., for CTRL-R).
1603 */
1604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001605ins_redraw(
1606 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001608#ifdef FEAT_CONCEAL
1609 linenr_T conceal_old_cursor_line = 0;
1610 linenr_T conceal_new_cursor_line = 0;
1611 int conceal_update_lines = FALSE;
1612#endif
1613
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001614 if (char_avail())
1615 return;
1616
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001617#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001618 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1619 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001620 if (ready && (has_cursormovedI()
1621# if defined(FEAT_CONCEAL)
1622 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001623# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001625 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001626# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001627 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001628# endif
1629 )
1630 {
1631# ifdef FEAT_SYN_HL
1632 /* Need to update the screen first, to make sure syntax
1633 * highlighting is correct after making a change (e.g., inserting
1634 * a "(". The autocommand may also require a redraw, so it's done
1635 * again below, unfortunately. */
1636 if (syntax_present(curwin) && must_redraw)
1637 update_screen(0);
1638# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001639 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001640 {
1641 /* Make sure curswant is correct, an autocommand may call
1642 * getcurpos(). */
1643 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001644 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001645 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001646# ifdef FEAT_CONCEAL
1647 if (curwin->w_p_cole > 0)
1648 {
1649 conceal_old_cursor_line = last_cursormoved.lnum;
1650 conceal_new_cursor_line = curwin->w_cursor.lnum;
1651 conceal_update_lines = TRUE;
1652 }
1653# endif
1654 last_cursormoved = curwin->w_cursor;
1655 }
1656#endif
1657
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001658 /* Trigger TextChangedI if b_changedtick differs. */
1659 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001660 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001661#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001662 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001663#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001664 )
1665 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001666 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1667 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001669
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001670#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001671 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1672 * TextChangedI will need to trigger for backwards compatibility, thus use
1673 * different b_last_changedtick* variables. */
1674 if (ready && has_textchangedP()
1675 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1676 && pum_visible())
1677 {
1678 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
1679 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1680 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001681#endif
1682
1683 if (must_redraw)
1684 update_screen(0);
1685 else if (clear_cmdline || redraw_cmdline)
1686 showmode(); /* clear cmdline and show mode */
1687# if defined(FEAT_CONCEAL)
1688 if ((conceal_update_lines
1689 && (conceal_old_cursor_line != conceal_new_cursor_line
1690 || conceal_cursor_line(curwin)))
1691 || need_cursor_line_redraw)
1692 {
1693 if (conceal_old_cursor_line != conceal_new_cursor_line)
1694 update_single_line(curwin, conceal_old_cursor_line);
1695 update_single_line(curwin, conceal_new_cursor_line == 0
1696 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1697 curwin->w_valid &= ~VALID_CROW;
1698 }
1699# endif
1700 showruler(FALSE);
1701 setcursor();
1702 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703}
1704
1705/*
1706 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1707 */
1708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001709ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001712 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
1714 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001715 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
1717 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001720 did_putchar = TRUE;
1721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1723
1724#ifdef FEAT_CMDL_INFO
1725 add_to_showcmd_c(Ctrl_V);
1726#endif
1727
1728 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001729 if (did_putchar)
1730 /* when the line fits in 'columns' the '^' is at the start of the next
1731 * line and will not removed by the redraw */
1732 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733#ifdef FEAT_CMDL_INFO
1734 clear_showcmd();
1735#endif
1736 insert_special(c, FALSE, TRUE);
1737#ifdef FEAT_RIGHTLEFT
1738 revins_chars++;
1739 revins_legal++;
1740#endif
1741}
1742
1743/*
1744 * Put a character directly onto the screen. It's not stored in a buffer.
1745 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1746 */
1747static int pc_status;
1748#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1749#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1750#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1751#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753static int pc_attr;
1754static int pc_row;
1755static int pc_col;
1756
1757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001758edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
1760 int attr;
1761
1762 if (ScreenLines != NULL)
1763 {
1764 update_topline(); /* just in case w_topline isn't valid */
1765 validate_cursor();
1766 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001767 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 else
1769 attr = 0;
1770 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001771 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1773 pc_status = PC_STATUS_UNSET;
1774#endif
1775#ifdef FEAT_RIGHTLEFT
1776 if (curwin->w_p_rl)
1777 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001778 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779# ifdef FEAT_MBYTE
1780 if (has_mbyte)
1781 {
1782 int fix_col = mb_fix_col(pc_col, pc_row);
1783
1784 if (fix_col != pc_col)
1785 {
1786 screen_putchar(' ', pc_row, fix_col, attr);
1787 --curwin->w_wcol;
1788 pc_status = PC_STATUS_RIGHT;
1789 }
1790 }
1791# endif
1792 }
1793 else
1794#endif
1795 {
1796 pc_col += curwin->w_wcol;
1797#ifdef FEAT_MBYTE
1798 if (mb_lefthalve(pc_row, pc_col))
1799 pc_status = PC_STATUS_LEFT;
1800#endif
1801 }
1802
1803 /* save the character to be able to put it back */
1804#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1805 if (pc_status == PC_STATUS_UNSET)
1806#endif
1807 {
1808 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1809 pc_status = PC_STATUS_SET;
1810 }
1811 screen_putchar(c, pc_row, pc_col, attr);
1812 }
1813}
1814
1815/*
1816 * Undo the previous edit_putchar().
1817 */
1818 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001819edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820{
1821 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1822 {
1823#if defined(FEAT_MBYTE)
1824 if (pc_status == PC_STATUS_RIGHT)
1825 ++curwin->w_wcol;
1826 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1827 redrawWinline(curwin->w_cursor.lnum, FALSE);
1828 else
1829#endif
1830 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1831 }
1832}
1833
1834/*
1835 * Called when p_dollar is set: display a '$' at the end of the changed text
1836 * Only works when cursor is in the line that changes.
1837 */
1838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001839display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 colnr_T save_col;
1842
1843 if (!redrawing())
1844 return;
1845
1846 cursor_off();
1847 save_col = curwin->w_cursor.col;
1848 curwin->w_cursor.col = col;
1849#ifdef FEAT_MBYTE
1850 if (has_mbyte)
1851 {
1852 char_u *p;
1853
1854 /* If on the last byte of a multi-byte move to the first byte. */
1855 p = ml_get_curline();
1856 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1857 }
1858#endif
1859 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001860 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 {
1862 edit_putchar('$', FALSE);
1863 dollar_vcol = curwin->w_virtcol;
1864 }
1865 curwin->w_cursor.col = save_col;
1866}
1867
1868/*
1869 * Call this function before moving the cursor from the normal insert position
1870 * in insert mode.
1871 */
1872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001873undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001875 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001877 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 redrawWinline(curwin->w_cursor.lnum, FALSE);
1879 }
1880}
1881
1882/*
1883 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1884 * Keep the cursor on the same character.
1885 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1886 * type == INDENT_DEC decrease indent (for CTRL-D)
1887 * type == INDENT_SET set indent to "amount"
1888 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1889 */
1890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001891change_indent(
1892 int type,
1893 int amount,
1894 int round,
1895 int replaced, /* replaced character, put on replace stack */
1896 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
1898 int vcol;
1899 int last_vcol;
1900 int insstart_less; /* reduction for Insstart.col */
1901 int new_cursor_col;
1902 int i;
1903 char_u *ptr;
1904 int save_p_list;
1905 int start_col;
1906 colnr_T vc;
1907#ifdef FEAT_VREPLACE
1908 colnr_T orig_col = 0; /* init for GCC */
1909 char_u *new_line, *orig_line = NULL; /* init for GCC */
1910
1911 /* VREPLACE mode needs to know what the line was like before changing */
1912 if (State & VREPLACE_FLAG)
1913 {
1914 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1915 orig_col = curwin->w_cursor.col;
1916 }
1917#endif
1918
1919 /* for the following tricks we don't want list mode */
1920 save_p_list = curwin->w_p_list;
1921 curwin->w_p_list = FALSE;
1922 vc = getvcol_nolist(&curwin->w_cursor);
1923 vcol = vc;
1924
1925 /*
1926 * For Replace mode we need to fix the replace stack later, which is only
1927 * possible when the cursor is in the indent. Remember the number of
1928 * characters before the cursor if it's possible.
1929 */
1930 start_col = curwin->w_cursor.col;
1931
1932 /* determine offset from first non-blank */
1933 new_cursor_col = curwin->w_cursor.col;
1934 beginline(BL_WHITE);
1935 new_cursor_col -= curwin->w_cursor.col;
1936
1937 insstart_less = curwin->w_cursor.col;
1938
1939 /*
1940 * If the cursor is in the indent, compute how many screen columns the
1941 * cursor is to the left of the first non-blank.
1942 */
1943 if (new_cursor_col < 0)
1944 vcol = get_indent() - vcol;
1945
1946 if (new_cursor_col > 0) /* can't fix replace stack */
1947 start_col = -1;
1948
1949 /*
1950 * Set the new indent. The cursor will be put on the first non-blank.
1951 */
1952 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001953 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 else
1955 {
1956#ifdef FEAT_VREPLACE
1957 int save_State = State;
1958
1959 /* Avoid being called recursively. */
1960 if (State & VREPLACE_FLAG)
1961 State = INSERT;
1962#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001963 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964#ifdef FEAT_VREPLACE
1965 State = save_State;
1966#endif
1967 }
1968 insstart_less -= curwin->w_cursor.col;
1969
1970 /*
1971 * Try to put cursor on same character.
1972 * If the cursor is at or after the first non-blank in the line,
1973 * compute the cursor column relative to the column of the first
1974 * non-blank character.
1975 * If we are not in insert mode, leave the cursor on the first non-blank.
1976 * If the cursor is before the first non-blank, position it relative
1977 * to the first non-blank, counted in screen columns.
1978 */
1979 if (new_cursor_col >= 0)
1980 {
1981 /*
1982 * When changing the indent while the cursor is touching it, reset
1983 * Insstart_col to 0.
1984 */
1985 if (new_cursor_col == 0)
1986 insstart_less = MAXCOL;
1987 new_cursor_col += curwin->w_cursor.col;
1988 }
1989 else if (!(State & INSERT))
1990 new_cursor_col = curwin->w_cursor.col;
1991 else
1992 {
1993 /*
1994 * Compute the screen column where the cursor should be.
1995 */
1996 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001997 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998
1999 /*
2000 * Advance the cursor until we reach the right screen column.
2001 */
2002 vcol = last_vcol = 0;
2003 new_cursor_col = -1;
2004 ptr = ml_get_curline();
2005 while (vcol <= (int)curwin->w_virtcol)
2006 {
2007 last_vcol = vcol;
2008#ifdef FEAT_MBYTE
2009 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002010 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 else
2012#endif
2013 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002014 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 vcol = last_vcol;
2017
2018 /*
2019 * May need to insert spaces to be able to position the cursor on
2020 * the right screen column.
2021 */
2022 if (vcol != (int)curwin->w_virtcol)
2023 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002024 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002026 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 if (ptr != NULL)
2028 {
2029 new_cursor_col += i;
2030 ptr[i] = NUL;
2031 while (--i >= 0)
2032 ptr[i] = ' ';
2033 ins_str(ptr);
2034 vim_free(ptr);
2035 }
2036 }
2037
2038 /*
2039 * When changing the indent while the cursor is in it, reset
2040 * Insstart_col to 0.
2041 */
2042 insstart_less = MAXCOL;
2043 }
2044
2045 curwin->w_p_list = save_p_list;
2046
2047 if (new_cursor_col <= 0)
2048 curwin->w_cursor.col = 0;
2049 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002050 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 curwin->w_set_curswant = TRUE;
2052 changed_cline_bef_curs();
2053
2054 /*
2055 * May have to adjust the start of the insert.
2056 */
2057 if (State & INSERT)
2058 {
2059 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2060 {
2061 if ((int)Insstart.col <= insstart_less)
2062 Insstart.col = 0;
2063 else
2064 Insstart.col -= insstart_less;
2065 }
2066 if ((int)ai_col <= insstart_less)
2067 ai_col = 0;
2068 else
2069 ai_col -= insstart_less;
2070 }
2071
2072 /*
2073 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2074 * If the number of characters before the cursor decreased, need to pop a
2075 * few characters from the replace stack.
2076 * If the number of characters before the cursor increased, need to push a
2077 * few NULs onto the replace stack.
2078 */
2079 if (REPLACE_NORMAL(State) && start_col >= 0)
2080 {
2081 while (start_col > (int)curwin->w_cursor.col)
2082 {
2083 replace_join(0); /* remove a NUL from the replace stack */
2084 --start_col;
2085 }
2086 while (start_col < (int)curwin->w_cursor.col || replaced)
2087 {
2088 replace_push(NUL);
2089 if (replaced)
2090 {
2091 replace_push(replaced);
2092 replaced = NUL;
2093 }
2094 ++start_col;
2095 }
2096 }
2097
2098#ifdef FEAT_VREPLACE
2099 /*
2100 * For VREPLACE mode, we also have to fix the replace stack. In this case
2101 * it is always possible because we backspace over the whole line and then
2102 * put it back again the way we wanted it.
2103 */
2104 if (State & VREPLACE_FLAG)
2105 {
2106 /* If orig_line didn't allocate, just return. At least we did the job,
2107 * even if you can't backspace. */
2108 if (orig_line == NULL)
2109 return;
2110
2111 /* Save new line */
2112 new_line = vim_strsave(ml_get_curline());
2113 if (new_line == NULL)
2114 return;
2115
2116 /* We only put back the new line up to the cursor */
2117 new_line[curwin->w_cursor.col] = NUL;
2118
2119 /* Put back original line */
2120 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2121 curwin->w_cursor.col = orig_col;
2122
2123 /* Backspace from cursor to start of line */
2124 backspace_until_column(0);
2125
2126 /* Insert new stuff into line again */
2127 ins_bytes(new_line);
2128
2129 vim_free(new_line);
2130 }
2131#endif
2132}
2133
2134/*
2135 * Truncate the space at the end of a line. This is to be used only in an
2136 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2137 * modes.
2138 */
2139 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002140truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141{
2142 int i;
2143
2144 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002145 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
2147 if (State & REPLACE_FLAG)
2148 replace_join(0); /* remove a NUL from the replace stack */
2149 }
2150 line[i + 1] = NUL;
2151}
2152
2153#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2154 || defined(FEAT_COMMENTS) || defined(PROTO)
2155/*
2156 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2157 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002158 * Will attempt not to go before "col" even when there is a composing
2159 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 */
2161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002162backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163{
2164 while ((int)curwin->w_cursor.col > col)
2165 {
2166 curwin->w_cursor.col--;
2167 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002168 replace_do_bs(col);
2169 else if (!del_char_after_col(col))
2170 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172}
2173#endif
2174
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002175/*
2176 * Like del_char(), but make sure not to go before column "limit_col".
2177 * Only matters when there are composing characters.
2178 * Return TRUE when something was deleted.
2179 */
2180 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002181del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002182{
2183#ifdef FEAT_MBYTE
2184 if (enc_utf8 && limit_col >= 0)
2185 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002186 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002187
2188 /* Make sure the cursor is at the start of a character, but
2189 * skip forward again when going too far back because of a
2190 * composing character. */
2191 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002192 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002193 {
2194 int l = utf_ptr2len(ml_get_cursor());
2195
2196 if (l == 0) /* end of line */
2197 break;
2198 curwin->w_cursor.col += l;
2199 }
2200 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2201 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002202 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002203 }
2204 else
2205#endif
2206 (void)del_char(FALSE);
2207 return TRUE;
2208}
2209
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2211/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002212 * CTRL-X pressed in Insert mode.
2213 */
2214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002215ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002216{
2217 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2218 * CTRL-V works like CTRL-N */
2219 if (ctrl_x_mode != CTRL_X_CMDLINE)
2220 {
2221 /* if the next ^X<> won't ADD nothing, then reset
2222 * compl_cont_status */
2223 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002224 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002225 else
2226 compl_cont_status = 0;
2227 /* We're not sure which CTRL-X mode it will be yet */
2228 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2229 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2230 edit_submode_pre = NULL;
2231 showmode();
2232 }
2233}
2234
2235/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002236 * Whether other than default completion has been selected.
2237 */
2238 int
2239ctrl_x_mode_not_default(void)
2240{
2241 return ctrl_x_mode != CTRL_X_NORMAL;
2242}
2243
2244/*
2245 * Whether CTRL-X was typed without a following character.
2246 */
2247 int
2248ctrl_x_mode_not_defined_yet(void)
2249{
2250 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2251}
2252
2253/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002254 * Return TRUE if the 'dict' or 'tsr' option can be used.
2255 */
2256 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002257has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002258{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002259 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002260# ifdef FEAT_SPELL
2261 && !curwin->w_p_spell
2262# endif
2263 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002264 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2265 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002266 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002267 edit_submode = NULL;
2268 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2269 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002270 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002271 if (emsg_silent == 0)
2272 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002273 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002274 setcursor();
2275 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002276#ifdef FEAT_EVAL
2277 if (!get_vim_var_nr(VV_TESTING))
2278#endif
2279 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002280 }
2281 return FALSE;
2282 }
2283 return TRUE;
2284}
2285
2286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2288 * This depends on the current mode.
2289 */
2290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002291vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
2293 /* Always allow ^R - let it's results then be checked */
2294 if (c == Ctrl_R)
2295 return TRUE;
2296
Bram Moolenaare3226be2005-12-18 22:10:00 +00002297 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002298 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002299 return TRUE;
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 switch (ctrl_x_mode)
2302 {
2303 case 0: /* Not in any CTRL-X mode */
2304 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2305 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002306 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2308 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2309 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002310 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002311 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 case CTRL_X_SCROLL:
2313 return (c == Ctrl_Y || c == Ctrl_E);
2314 case CTRL_X_WHOLE_LINE:
2315 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2316 case CTRL_X_FILES:
2317 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2318 case CTRL_X_DICTIONARY:
2319 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2320 case CTRL_X_THESAURUS:
2321 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2322 case CTRL_X_TAGS:
2323 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2324#ifdef FEAT_FIND_ID
2325 case CTRL_X_PATH_PATTERNS:
2326 return (c == Ctrl_P || c == Ctrl_N);
2327 case CTRL_X_PATH_DEFINES:
2328 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2329#endif
2330 case CTRL_X_CMDLINE:
2331 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2332 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002333#ifdef FEAT_COMPL_FUNC
2334 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002335 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002336 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002337 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002338#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002339 case CTRL_X_SPELL:
2340 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002341 case CTRL_X_EVAL:
2342 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002344 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 return FALSE;
2346}
2347
2348/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002349 * Return TRUE when character "c" is part of the item currently being
2350 * completed. Used to decide whether to abandon complete mode when the menu
2351 * is visible.
2352 */
2353 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002354ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002355{
2356 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2357 /* When expanding an identifier only accept identifier chars. */
2358 return vim_isIDc(c);
2359
2360 switch (ctrl_x_mode)
2361 {
2362 case CTRL_X_FILES:
2363 /* When expanding file name only accept file name chars. But not
2364 * path separators, so that "proto/<Tab>" expands files in
2365 * "proto", not "proto/" as a whole */
2366 return vim_isfilec(c) && !vim_ispathsep(c);
2367
2368 case CTRL_X_CMDLINE:
2369 case CTRL_X_OMNI:
2370 /* Command line and Omni completion can work with just about any
2371 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002372 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002373
2374 case CTRL_X_WHOLE_LINE:
2375 /* For while line completion a space can be part of the line. */
2376 return vim_isprintc(c);
2377 }
2378 return vim_iswordc(c);
2379}
2380
2381/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002382 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002384 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 * the rest of the word to be in -- webb
2386 */
2387 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002388ins_compl_add_infercase(
2389 char_u *str,
2390 int len,
2391 int icase,
2392 char_u *fname,
2393 int dir,
2394 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002396 char_u *p;
2397 int i, c;
2398 int actual_len; /* Take multi-byte characters */
2399 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002400 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002401 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 int has_lower = FALSE;
2403 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002405 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002407 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
Bram Moolenaara2993e12007-08-12 14:38:46 +00002409 /* Find actual length of completion. */
2410#ifdef FEAT_MBYTE
2411 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002413 p = str;
2414 actual_len = 0;
2415 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002417 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002418 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002421 else
2422#endif
2423 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
Bram Moolenaara2993e12007-08-12 14:38:46 +00002425 /* Find actual length of original text. */
2426#ifdef FEAT_MBYTE
2427 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002429 p = compl_orig_text;
2430 actual_compl_length = 0;
2431 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002433 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002434 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
2436 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002437 else
2438#endif
2439 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002441 /* "actual_len" may be smaller than "actual_compl_length" when using
2442 * thesaurus, only use the minimum when comparing. */
2443 min_len = actual_len < actual_compl_length
2444 ? actual_len : actual_compl_length;
2445
Bram Moolenaara2993e12007-08-12 14:38:46 +00002446 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002447 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002448 if (wca != NULL)
2449 {
2450 p = str;
2451 for (i = 0; i < actual_len; ++i)
2452#ifdef FEAT_MBYTE
2453 if (has_mbyte)
2454 wca[i] = mb_ptr2char_adv(&p);
2455 else
2456#endif
2457 wca[i] = *(p++);
2458
2459 /* Rule 1: Were any chars converted to lower? */
2460 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002461 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002462 {
2463#ifdef FEAT_MBYTE
2464 if (has_mbyte)
2465 c = mb_ptr2char_adv(&p);
2466 else
2467#endif
2468 c = *(p++);
2469 if (MB_ISLOWER(c))
2470 {
2471 has_lower = TRUE;
2472 if (MB_ISUPPER(wca[i]))
2473 {
2474 /* Rule 1 is satisfied. */
2475 for (i = actual_compl_length; i < actual_len; ++i)
2476 wca[i] = MB_TOLOWER(wca[i]);
2477 break;
2478 }
2479 }
2480 }
2481
2482 /*
2483 * Rule 2: No lower case, 2nd consecutive letter converted to
2484 * upper case.
2485 */
2486 if (!has_lower)
2487 {
2488 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002489 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002490 {
2491#ifdef FEAT_MBYTE
2492 if (has_mbyte)
2493 c = mb_ptr2char_adv(&p);
2494 else
2495#endif
2496 c = *(p++);
2497 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2498 {
2499 /* Rule 2 is satisfied. */
2500 for (i = actual_compl_length; i < actual_len; ++i)
2501 wca[i] = MB_TOUPPER(wca[i]);
2502 break;
2503 }
2504 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2505 }
2506 }
2507
2508 /* Copy the original case of the part we typed. */
2509 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002510 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002511 {
2512#ifdef FEAT_MBYTE
2513 if (has_mbyte)
2514 c = mb_ptr2char_adv(&p);
2515 else
2516#endif
2517 c = *(p++);
2518 if (MB_ISLOWER(c))
2519 wca[i] = MB_TOLOWER(wca[i]);
2520 else if (MB_ISUPPER(c))
2521 wca[i] = MB_TOUPPER(wca[i]);
2522 }
2523
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002524 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002525 * Generate encoding specific output from wide character array.
2526 * Multi-byte characters can occupy up to five bytes more than
2527 * ASCII characters, and we also need one byte for NUL, so stay
2528 * six bytes away from the edge of IObuff.
2529 */
2530 p = IObuff;
2531 i = 0;
2532 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2533#ifdef FEAT_MBYTE
2534 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002535 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002536 else
2537#endif
2538 *(p++) = wca[i++];
2539 *p = NUL;
2540
2541 vim_free(wca);
2542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002544 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2545 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002547 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548}
2549
2550/*
2551 * Add a match to the list of matches.
2552 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002553 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002554 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002556 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002557ins_compl_add(
2558 char_u *str,
2559 int len,
2560 int icase,
2561 char_u *fname,
2562 char_u **cptext, /* extra text for popup menu or NULL */
2563 int cdir,
2564 int flags,
2565 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002567 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002568 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
2570 ui_breakcheck();
2571 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002572 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (len < 0)
2574 len = (int)STRLEN(str);
2575
2576 /*
2577 * If the same match is already present, don't add it.
2578 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002579 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002581 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 do
2583 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002584 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002585 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002586 && match->cp_str[len] == NUL)
2587 return NOTDONE;
2588 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002589 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002592 /* Remove any popup menu before changing the list of matches. */
2593 ins_compl_del_pum();
2594
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 /*
2596 * Allocate a new match structure.
2597 * Copy the values to the new match structure.
2598 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002599 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002601 return FAIL;
2602 match->cp_number = -1;
2603 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002604 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002605 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 {
2607 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002608 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002610 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002611
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002613 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2615 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002616 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002617 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002618 && compl_curr_match->cp_fname != NULL
2619 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002620 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002621 else if (fname != NULL)
2622 {
2623 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002624 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002627 match->cp_fname = NULL;
2628 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002629
2630 if (cptext != NULL)
2631 {
2632 int i;
2633
2634 for (i = 0; i < CPT_COUNT; ++i)
2635 if (cptext[i] != NULL && *cptext[i] != NUL)
2636 match->cp_text[i] = vim_strsave(cptext[i]);
2637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
2639 /*
2640 * Link the new match structure in the list of matches.
2641 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002642 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002643 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 else if (dir == FORWARD)
2645 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002646 match->cp_next = compl_curr_match->cp_next;
2647 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649 else /* BACKWARD */
2650 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002651 match->cp_next = compl_curr_match;
2652 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002654 if (match->cp_next)
2655 match->cp_next->cp_prev = match;
2656 if (match->cp_prev)
2657 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002659 compl_first_match = match;
2660 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002662 /*
2663 * Find the longest common string if still doing that.
2664 */
2665 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2666 ins_compl_longest_match(match);
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 return OK;
2669}
2670
2671/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002672 * Return TRUE if "str[len]" matches with match->cp_str, considering
2673 * match->cp_icase.
2674 */
2675 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002676ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002677{
2678 if (match->cp_icase)
2679 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2680 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2681}
2682
2683/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002684 * Reduce the longest common string for match "match".
2685 */
2686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002687ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002688{
2689 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002690 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002691 int had_match;
2692
2693 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002694 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002695 /* First match, use it as a whole. */
2696 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002697 if (compl_leader != NULL)
2698 {
2699 had_match = (curwin->w_cursor.col > compl_col);
2700 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002701 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002702 ins_redraw(FALSE);
2703
2704 /* When the match isn't there (to avoid matching itself) remove it
2705 * again after redrawing. */
2706 if (!had_match)
2707 ins_compl_delete();
2708 compl_used_match = FALSE;
2709 }
2710 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002711 else
2712 {
2713 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002714 p = compl_leader;
2715 s = match->cp_str;
2716 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002717 {
2718#ifdef FEAT_MBYTE
2719 if (has_mbyte)
2720 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002721 c1 = mb_ptr2char(p);
2722 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002723 }
2724 else
2725#endif
2726 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002727 c1 = *p;
2728 c2 = *s;
2729 }
2730 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2731 : (c1 != c2))
2732 break;
2733#ifdef FEAT_MBYTE
2734 if (has_mbyte)
2735 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002736 MB_PTR_ADV(p);
2737 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002738 }
2739 else
2740#endif
2741 {
2742 ++p;
2743 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002744 }
2745 }
2746
2747 if (*p != NUL)
2748 {
2749 /* Leader was shortened, need to change the inserted text. */
2750 *p = NUL;
2751 had_match = (curwin->w_cursor.col > compl_col);
2752 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002753 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002754 ins_redraw(FALSE);
2755
2756 /* When the match isn't there (to avoid matching itself) remove it
2757 * again after redrawing. */
2758 if (!had_match)
2759 ins_compl_delete();
2760 }
2761
2762 compl_used_match = FALSE;
2763 }
2764}
2765
2766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 * Add an array of matches to the list of matches.
2768 * Frees matches[].
2769 */
2770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002771ins_compl_add_matches(
2772 int num_matches,
2773 char_u **matches,
2774 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
2776 int i;
2777 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002778 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
Bram Moolenaar572cb562005-08-05 21:35:02 +00002780 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002781 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002782 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002784 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 FreeWild(num_matches, matches);
2786}
2787
2788/* Make the completion list cyclic.
2789 * Return the number of matches (excluding the original).
2790 */
2791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002792ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002794 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 int count = 0;
2796
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002797 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
2799 /*
2800 * Find the end of the list.
2801 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002802 match = compl_first_match;
2803 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002804 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002806 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 ++count;
2808 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002809 match->cp_next = compl_first_match;
2810 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812 return count;
2813}
2814
Bram Moolenaara94bc432006-03-10 21:42:59 +00002815/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002816 * Set variables that store noselect and noinsert behavior from the
2817 * 'completeopt' value.
2818 */
2819 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002820completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002821{
2822 compl_no_insert = FALSE;
2823 compl_no_select = FALSE;
2824 if (strstr((char *)p_cot, "noselect") != NULL)
2825 compl_no_select = TRUE;
2826 if (strstr((char *)p_cot, "noinsert") != NULL)
2827 compl_no_insert = TRUE;
2828}
2829
2830/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002831 * Start completion for the complete() function.
2832 * "startcol" is where the matched text starts (1 is first column).
2833 * "list" is the list of matches.
2834 */
2835 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002836set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002837{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002838 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002839 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002840
Bram Moolenaara94bc432006-03-10 21:42:59 +00002841 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002842 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002843 ins_compl_prep(' ');
2844 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002845 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002846
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002847 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002848 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002849 startcol = curwin->w_cursor.col;
2850 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002851 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002852 /* compl_pattern doesn't need to be set */
2853 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2854 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002855 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002856 return;
2857
Bram Moolenaare4214502015-03-05 18:08:43 +01002858 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002859
2860 ins_compl_add_list(list);
2861 compl_matches = ins_compl_make_cyclic();
2862 compl_started = TRUE;
2863 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002864 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002865
2866 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002867 if (compl_no_insert || compl_no_select)
2868 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002869 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002870 if (compl_no_select)
2871 /* Down/Up has no real effect. */
2872 ins_complete(K_UP, FALSE);
2873 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002874 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002875 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002876 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002877
2878 /* Lazily show the popup menu, unless we got interrupted. */
2879 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002880 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002881 out_flush();
2882}
2883
2884
Bram Moolenaar9372a112005-12-06 19:59:18 +00002885/* "compl_match_array" points the currently displayed list of entries in the
2886 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002887static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002888static int compl_match_arraysize;
2889
2890/*
2891 * Update the screen and when there is any scrolling remove the popup menu.
2892 */
2893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002894ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002895{
2896 int h;
2897
2898 if (compl_match_array != NULL)
2899 {
2900 h = curwin->w_cline_height;
2901 update_screen(0);
2902 if (h != curwin->w_cline_height)
2903 ins_compl_del_pum();
2904 }
2905}
2906
2907/*
2908 * Remove any popup menu.
2909 */
2910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002911ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002912{
2913 if (compl_match_array != NULL)
2914 {
2915 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01002916 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002917 }
2918}
2919
2920/*
2921 * Return TRUE if the popup menu should be displayed.
2922 */
2923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002924pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002925{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002926 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002927 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002928 return FALSE;
2929
2930 /* The display looks bad on a B&W display. */
2931 if (t_colors < 8
2932#ifdef FEAT_GUI
2933 && !gui.in_use
2934#endif
2935 )
2936 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002937 return TRUE;
2938}
2939
2940/*
2941 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002942 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002943 */
2944 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002945pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002946{
2947 compl_T *compl;
2948 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002949
2950 /* Don't display the popup menu if there are no matches or there is only
2951 * one (ignoring the original text). */
2952 compl = compl_first_match;
2953 i = 0;
2954 do
2955 {
2956 if (compl == NULL
2957 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2958 break;
2959 compl = compl->cp_next;
2960 } while (compl != compl_first_match);
2961
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002962 if (strstr((char *)p_cot, "menuone") != NULL)
2963 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002964 return (i >= 2);
2965}
2966
2967/*
2968 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002969 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002970 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002972ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002973{
2974 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002975 compl_T *shown_compl = NULL;
2976 int did_find_shown_match = FALSE;
2977 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002978 int i;
2979 int cur = -1;
2980 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002981 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002982
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002983 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002984 return;
2985
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002986#if defined(FEAT_EVAL)
2987 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2988 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2989#endif
2990
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002991 /* Update the screen before drawing the popup menu over it. */
2992 update_screen(0);
2993
2994 if (compl_match_array == NULL)
2995 {
2996 /* Need to build the popup menu list. */
2997 compl_match_arraysize = 0;
2998 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002999 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003000 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003001 do
3002 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003003 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3004 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003005 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003006 ++compl_match_arraysize;
3007 compl = compl->cp_next;
3008 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003009 if (compl_match_arraysize == 0)
3010 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003011 compl_match_array = (pumitem_T *)alloc_clear(
3012 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003013 * compl_match_arraysize));
3014 if (compl_match_array != NULL)
3015 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003016 /* If the current match is the original text don't find the first
3017 * match after it, don't highlight anything. */
3018 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3019 shown_match_ok = TRUE;
3020
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003021 i = 0;
3022 compl = compl_first_match;
3023 do
3024 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003025 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3026 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003027 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003028 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003029 if (!shown_match_ok)
3030 {
3031 if (compl == compl_shown_match || did_find_shown_match)
3032 {
3033 /* This item is the shown match or this is the
3034 * first displayed item after the shown match. */
3035 compl_shown_match = compl;
3036 did_find_shown_match = TRUE;
3037 shown_match_ok = TRUE;
3038 }
3039 else
3040 /* Remember this displayed match for when the
3041 * shown match is just below it. */
3042 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003043 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003044 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003045
3046 if (compl->cp_text[CPT_ABBR] != NULL)
3047 compl_match_array[i].pum_text =
3048 compl->cp_text[CPT_ABBR];
3049 else
3050 compl_match_array[i].pum_text = compl->cp_str;
3051 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3052 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3053 if (compl->cp_text[CPT_MENU] != NULL)
3054 compl_match_array[i++].pum_extra =
3055 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003056 else
3057 compl_match_array[i++].pum_extra = compl->cp_fname;
3058 }
3059
3060 if (compl == compl_shown_match)
3061 {
3062 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003063
3064 /* When the original text is the shown match don't set
3065 * compl_shown_match. */
3066 if (compl->cp_flags & ORIGINAL_TEXT)
3067 shown_match_ok = TRUE;
3068
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003069 if (!shown_match_ok && shown_compl != NULL)
3070 {
3071 /* The shown match isn't displayed, set it to the
3072 * previously displayed match. */
3073 compl_shown_match = shown_compl;
3074 shown_match_ok = TRUE;
3075 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003076 }
3077 compl = compl->cp_next;
3078 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003079
3080 if (!shown_match_ok) /* no displayed match at all */
3081 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003082 }
3083 }
3084 else
3085 {
3086 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003087 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003088 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3089 || compl_match_array[i].pum_text
3090 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003091 {
3092 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003093 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003094 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003095 }
3096
3097 if (compl_match_array != NULL)
3098 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003099 /* In Replace mode when a $ is displayed at the end of the line only
3100 * part of the screen would be updated. We do need to redraw here. */
3101 dollar_vcol = -1;
3102
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003103 /* Compute the screen column of the start of the completed text.
3104 * Use the cursor to get all wrapping and other settings right. */
3105 col = curwin->w_cursor.col;
3106 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003107 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003108 curwin->w_cursor.col = col;
3109 }
3110}
3111
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112#define DICT_FIRST (1) /* use just first element in "dict" */
3113#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003114
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003116 * Add any identifiers that match the given pattern in the list of dictionary
3117 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 */
3119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003120ins_compl_dictionaries(
3121 char_u *dict_start,
3122 char_u *pat,
3123 int flags, /* DICT_FIRST and/or DICT_EXACT */
3124 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003126 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 char_u *ptr;
3128 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 char_u **files;
3131 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003133 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
Bram Moolenaar0b238792006-03-02 22:49:12 +00003135 if (*dict == NUL)
3136 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003137#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003138 /* When 'dictionary' is empty and spell checking is enabled use
3139 * "spell". */
3140 if (!thesaurus && curwin->w_p_spell)
3141 dict = (char_u *)"spell";
3142 else
3143#endif
3144 return;
3145 }
3146
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003148 if (buf == NULL)
3149 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003150 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003151
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 /* If 'infercase' is set, don't use 'smartcase' here */
3153 save_p_scs = p_scs;
3154 if (curbuf->b_p_inf)
3155 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003156
3157 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3158 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003159 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003160 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003161 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003162 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003163 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003164
3165 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003166 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003167 len = STRLEN(pat_esc) + 10;
3168 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003169 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003170 {
3171 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003172 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003173 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003174 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003175 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3176 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003177 vim_free(ptr);
3178 }
3179 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003180 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003181 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003182 if (regmatch.regprog == NULL)
3183 goto theend;
3184 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003185
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3187 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003188 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
3190 /* copy one dictionary file name into buf */
3191 if (flags == DICT_EXACT)
3192 {
3193 count = 1;
3194 files = &dict;
3195 }
3196 else
3197 {
3198 /* Expand wildcards in the dictionary name, but do not allow
3199 * backticks (for security, the 'dict' option may have been set in
3200 * a modeline). */
3201 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003202# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003203 if (!thesaurus && STRCMP(buf, "spell") == 0)
3204 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003205 else
3206# endif
3207 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 || expand_wildcards(1, &buf, &count, &files,
3209 EW_FILE|EW_SILENT) != OK)
3210 count = 0;
3211 }
3212
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003213# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003214 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003216 /* Complete from active spelling. Skip "\<" in the pattern, we
3217 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003218 if (pat[0] == '\\' && pat[1] == '<')
3219 ptr = pat + 2;
3220 else
3221 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003222 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003224 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003225# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003226 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003227 {
3228 ins_compl_files(count, files, thesaurus, flags,
3229 &regmatch, buf, &dir);
3230 if (flags != DICT_EXACT)
3231 FreeWild(count, files);
3232 }
3233 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 break;
3235 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003236
3237theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003239 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 vim_free(buf);
3241}
3242
Bram Moolenaar0b238792006-03-02 22:49:12 +00003243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003244ins_compl_files(
3245 int count,
3246 char_u **files,
3247 int thesaurus,
3248 int flags,
3249 regmatch_T *regmatch,
3250 char_u *buf,
3251 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003252{
3253 char_u *ptr;
3254 int i;
3255 FILE *fp;
3256 int add_r;
3257
3258 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3259 {
3260 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3261 if (flags != DICT_EXACT)
3262 {
3263 vim_snprintf((char *)IObuff, IOSIZE,
3264 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003265 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003266 }
3267
3268 if (fp != NULL)
3269 {
3270 /*
3271 * Read dictionary file line by line.
3272 * Check each line for a match.
3273 */
3274 while (!got_int && !compl_interrupted
3275 && !vim_fgets(buf, LSIZE, fp))
3276 {
3277 ptr = buf;
3278 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3279 {
3280 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003281 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003282 ptr = find_line_end(ptr);
3283 else
3284 ptr = find_word_end(ptr);
3285 add_r = ins_compl_add_infercase(regmatch->startp[0],
3286 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003287 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003288 if (thesaurus)
3289 {
3290 char_u *wstart;
3291
3292 /*
3293 * Add the other matches on the line
3294 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003295 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003296 while (!got_int)
3297 {
3298 /* Find start of the next word. Skip white
3299 * space and punctuation. */
3300 ptr = find_word_start(ptr);
3301 if (*ptr == NUL || *ptr == NL)
3302 break;
3303 wstart = ptr;
3304
Bram Moolenaara2993e12007-08-12 14:38:46 +00003305 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003306#ifdef FEAT_MBYTE
3307 if (has_mbyte)
3308 /* Japanese words may have characters in
3309 * different classes, only separate words
3310 * with single-byte non-word characters. */
3311 while (*ptr != NUL)
3312 {
3313 int l = (*mb_ptr2len)(ptr);
3314
3315 if (l < 2 && !vim_iswordc(*ptr))
3316 break;
3317 ptr += l;
3318 }
3319 else
3320#endif
3321 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003322
3323 /* Add the word. Skip the regexp match. */
3324 if (wstart != regmatch->startp[0])
3325 add_r = ins_compl_add_infercase(wstart,
3326 (int)(ptr - wstart),
3327 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003328 }
3329 }
3330 if (add_r == OK)
3331 /* if dir was BACKWARD then honor it just once */
3332 *dir = FORWARD;
3333 else if (add_r == FAIL)
3334 break;
3335 /* avoid expensive call to vim_regexec() when at end
3336 * of line */
3337 if (*ptr == '\n' || got_int)
3338 break;
3339 }
3340 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003341 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003342 }
3343 fclose(fp);
3344 }
3345 }
3346}
3347
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348/*
3349 * Find the start of the next word.
3350 * Returns a pointer to the first char of the word. Also stops at a NUL.
3351 */
3352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003353find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355#ifdef FEAT_MBYTE
3356 if (has_mbyte)
3357 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003358 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 else
3360#endif
3361 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3362 ++ptr;
3363 return ptr;
3364}
3365
3366/*
3367 * Find the end of the word. Assumes it starts inside a word.
3368 * Returns a pointer to just after the word.
3369 */
3370 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003371find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373#ifdef FEAT_MBYTE
3374 int start_class;
3375
3376 if (has_mbyte)
3377 {
3378 start_class = mb_get_class(ptr);
3379 if (start_class > 1)
3380 while (*ptr != NUL)
3381 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003382 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 if (mb_get_class(ptr) != start_class)
3384 break;
3385 }
3386 }
3387 else
3388#endif
3389 while (vim_iswordc(*ptr))
3390 ++ptr;
3391 return ptr;
3392}
3393
3394/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003395 * Find the end of the line, omitting CR and NL at the end.
3396 * Returns a pointer to just after the line.
3397 */
3398 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003399find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003400{
3401 char_u *s;
3402
3403 s = ptr + STRLEN(ptr);
3404 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3405 --s;
3406 return s;
3407}
3408
3409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 * Free the list of completions
3411 */
3412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003413ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003415 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003416 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
Bram Moolenaard23a8232018-02-10 18:45:26 +01003418 VIM_CLEAR(compl_pattern);
3419 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003421 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003423
3424 ins_compl_del_pum();
3425 pum_clear();
3426
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003427 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 do
3429 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003430 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003431 compl_curr_match = compl_curr_match->cp_next;
3432 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003434 if (match->cp_flags & FREE_FNAME)
3435 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003436 for (i = 0; i < CPT_COUNT; ++i)
3437 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003439 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3440 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003441 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003442 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443}
3444
3445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003446ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003448 compl_cont_status = 0;
3449 compl_started = FALSE;
3450 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003451 VIM_CLEAR(compl_pattern);
3452 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003454 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003455 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003456 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003457 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458}
3459
3460/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003461 * Return TRUE when Insert completion is active.
3462 */
3463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003464ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003465{
3466 return compl_started;
3467}
3468
3469/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003470 * Delete one character before the cursor and show the subset of the matches
3471 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003472 * Returns the character to be used, NUL if the work is done and another char
3473 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003474 */
3475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003476ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003477{
3478 char_u *line;
3479 char_u *p;
3480
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003481 line = ml_get_curline();
3482 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003483 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003484
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003485 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003486 * allow the word to be deleted, we won't match everything.
3487 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003488 if ((int)(p - line) - (int)compl_col < 0
3489 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003490 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3491 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3492 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003493 return K_BS;
3494
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003495 /* Deleted more than what was used to find matches or didn't finish
3496 * finding all matches: need to look for matches all over again. */
3497 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003498 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003499 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003500
Bram Moolenaara6557602006-02-04 22:43:20 +00003501 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003502 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003503 if (compl_leader != NULL)
3504 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003505 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003506 if (compl_shown_match != NULL)
3507 /* Make sure current match is not a hidden item. */
3508 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003509 return NUL;
3510 }
3511 return K_BS;
3512}
Bram Moolenaara6557602006-02-04 22:43:20 +00003513
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003514/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003515 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3516 * be called.
3517 */
3518 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003519ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003520{
3521 /* Return TRUE if we didn't complete finding matches or when the
3522 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3523 return compl_was_interrupted
3524 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3525 && compl_opt_refresh_always);
3526}
3527
3528/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003529 * Called after changing "compl_leader".
3530 * Show the popup menu with a different set of matches.
3531 * May also search for matches again if the previous search was interrupted.
3532 */
3533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003534ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003535{
3536 ins_compl_del_pum();
3537 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003538 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003539 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003540
3541 if (compl_started)
3542 ins_compl_set_original_text(compl_leader);
3543 else
3544 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003545#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003546 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003547#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003548 /*
3549 * Matches were cleared, need to search for them now. First display
3550 * the changed text before the cursor. Set "compl_restarting" to
3551 * avoid that the first match is inserted.
3552 */
3553 update_screen(0);
3554#ifdef FEAT_GUI
3555 if (gui.in_use)
3556 {
3557 /* Show the cursor after the match, not after the redrawn text. */
3558 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003559 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003560 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003561#endif
3562 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003563 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003564 compl_cont_status = 0;
3565 compl_restarting = FALSE;
3566 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003567
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003568 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003569
3570 /* Show the popup menu with a different set of matches. */
3571 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003572
3573 /* Don't let Enter select the original text when there is no popup menu. */
3574 if (compl_match_array == NULL)
3575 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003576}
3577
3578/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003579 * Return the length of the completion, from the completion start column to
3580 * the cursor column. Making sure it never goes below zero.
3581 */
3582 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003583ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003584{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003585 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003586
3587 if (off < 0)
3588 return 0;
3589 return off;
3590}
3591
3592/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003593 * Append one character to the match leader. May reduce the number of
3594 * matches.
3595 */
3596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003597ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003598{
3599#ifdef FEAT_MBYTE
3600 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003601#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003602
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003603 if (stop_arrow() == FAIL)
3604 return;
3605#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003606 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3607 {
3608 char_u buf[MB_MAXBYTES + 1];
3609
3610 (*mb_char2bytes)(c, buf);
3611 buf[cc] = NUL;
3612 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003613 if (compl_opt_refresh_always)
3614 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003615 }
3616 else
3617#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003618 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003619 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003620 if (compl_opt_refresh_always)
3621 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003622 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003623
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003624 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003625 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003626 ins_compl_restart();
3627
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003628 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003629 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003630 * break redo. */
3631 if (!compl_opt_refresh_always)
3632 {
3633 vim_free(compl_leader);
3634 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003635 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003636 if (compl_leader != NULL)
3637 ins_compl_new_leader();
3638 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003639}
3640
3641/*
3642 * Setup for finding completions again without leaving CTRL-X mode. Used when
3643 * BS or a key was typed while still searching for matches.
3644 */
3645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003646ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003647{
3648 ins_compl_free();
3649 compl_started = FALSE;
3650 compl_matches = 0;
3651 compl_cont_status = 0;
3652 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003653}
3654
3655/*
3656 * Set the first match, the original text.
3657 */
3658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003659ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003660{
3661 char_u *p;
3662
3663 /* Replace the original text entry. */
3664 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3665 {
3666 p = vim_strsave(str);
3667 if (p != NULL)
3668 {
3669 vim_free(compl_first_match->cp_str);
3670 compl_first_match->cp_str = p;
3671 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003672 }
3673}
3674
3675/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003676 * Append one character to the match leader. May reduce the number of
3677 * matches.
3678 */
3679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003680ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003681{
3682 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003683 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003684 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003685 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003686
3687 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003688 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003689 {
3690 /* When still at the original match use the first entry that matches
3691 * the leader. */
3692 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3693 {
3694 p = NULL;
3695 for (cp = compl_shown_match->cp_next; cp != NULL
3696 && cp != compl_first_match; cp = cp->cp_next)
3697 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003698 if (compl_leader == NULL
3699 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003700 (int)STRLEN(compl_leader)))
3701 {
3702 p = cp->cp_str;
3703 break;
3704 }
3705 }
3706 if (p == NULL || (int)STRLEN(p) <= len)
3707 return;
3708 }
3709 else
3710 return;
3711 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003712 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003713 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003714 ins_compl_addleader(c);
3715}
3716
3717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003719 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003720 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003722 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003723ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
3725 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003727 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
3729 /* Forget any previous 'special' messages if this is actually
3730 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3731 */
3732 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3733 edit_submode_extra = NULL;
3734
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003735 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003736 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3737 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003738 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003740 /* Set "compl_get_longest" when finding the first matches. */
3741 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003742 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003743 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003744 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003745 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003746
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003747 }
3748
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3750 {
3751 /*
3752 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3753 * it will be yet. Now we decide.
3754 */
3755 switch (c)
3756 {
3757 case Ctrl_E:
3758 case Ctrl_Y:
3759 ctrl_x_mode = CTRL_X_SCROLL;
3760 if (!(State & REPLACE_FLAG))
3761 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3762 else
3763 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3764 edit_submode_pre = NULL;
3765 showmode();
3766 break;
3767 case Ctrl_L:
3768 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3769 break;
3770 case Ctrl_F:
3771 ctrl_x_mode = CTRL_X_FILES;
3772 break;
3773 case Ctrl_K:
3774 ctrl_x_mode = CTRL_X_DICTIONARY;
3775 break;
3776 case Ctrl_R:
3777 /* Simply allow ^R to happen without affecting ^X mode */
3778 break;
3779 case Ctrl_T:
3780 ctrl_x_mode = CTRL_X_THESAURUS;
3781 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003782#ifdef FEAT_COMPL_FUNC
3783 case Ctrl_U:
3784 ctrl_x_mode = CTRL_X_FUNCTION;
3785 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003786 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003787 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003788 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003789#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003790 case 's':
3791 case Ctrl_S:
3792 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003793#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003794 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003795 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003796 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003797#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003798 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 case Ctrl_RSB:
3800 ctrl_x_mode = CTRL_X_TAGS;
3801 break;
3802#ifdef FEAT_FIND_ID
3803 case Ctrl_I:
3804 case K_S_TAB:
3805 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3806 break;
3807 case Ctrl_D:
3808 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3809 break;
3810#endif
3811 case Ctrl_V:
3812 case Ctrl_Q:
3813 ctrl_x_mode = CTRL_X_CMDLINE;
3814 break;
3815 case Ctrl_P:
3816 case Ctrl_N:
3817 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3818 * just started ^X mode, or there were enough ^X's to cancel
3819 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3820 * do normal expansion when interrupting a different mode (say
3821 * ^X^F^X^P or ^P^X^X^P, see below)
3822 * nothing changes if interrupting mode 0, (eg, the flag
3823 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003824 if (!(compl_cont_status & CONT_INTRPT))
3825 compl_cont_status |= CONT_LOCAL;
3826 else if (compl_cont_mode != 0)
3827 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 /* FALLTHROUGH */
3829 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003830 /* If we have typed at least 2 ^X's... for modes != 0, we set
3831 * compl_cont_status = 0 (eg, as if we had just started ^X
3832 * mode).
3833 * For mode 0, we set "compl_cont_mode" to an impossible
3834 * value, in both cases ^X^X can be used to restart the same
3835 * mode (avoiding ADDING mode).
3836 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3837 * 'complete' and local ^P expansions respectively.
3838 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3839 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 if (c == Ctrl_X)
3841 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003842 if (compl_cont_mode != 0)
3843 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003845 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003847 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 edit_submode = NULL;
3849 showmode();
3850 break;
3851 }
3852 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003853 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
3855 /* We're already in CTRL-X mode, do we stay in it? */
3856 if (!vim_is_ctrl_x_key(c))
3857 {
3858 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003859 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 else
3861 ctrl_x_mode = CTRL_X_FINISHED;
3862 edit_submode = NULL;
3863 }
3864 showmode();
3865 }
3866
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003867 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
3869 /* Show error message from attempted keyword completion (probably
3870 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003873 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3874 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 || ctrl_x_mode == CTRL_X_FINISHED)
3876 {
3877 /* Get here when we have finished typing a sequence of ^N and
3878 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003879 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003880 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 {
3882 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003883 * If any of the original typed text has been changed, eg when
3884 * ignorecase is set, we must add back-spaces to the redo
3885 * buffer. We add as few as necessary to delete just the part
3886 * of the original text that has changed.
3887 * When using the longest match, edited the match or used
3888 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003890 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3891 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003892 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003893 ptr = NULL;
3894 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
3896
3897#ifdef FEAT_CINDENT
3898 want_cindent = (can_cindent && cindent_on());
3899#endif
3900 /*
3901 * When completing whole lines: fix indent for 'cindent'.
3902 * Otherwise, break line if it's too long.
3903 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003904 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
3906#ifdef FEAT_CINDENT
3907 /* re-indent the current line */
3908 if (want_cindent)
3909 {
3910 do_c_expr_indent();
3911 want_cindent = FALSE; /* don't do it again */
3912 }
3913#endif
3914 }
3915 else
3916 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003917 int prev_col = curwin->w_cursor.col;
3918
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003920 if (prev_col > 0)
3921 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003922 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003923 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003925 if (prev_col > 0
3926 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3927 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
3929
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003930 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003931 * the selection without inserting anything. When
3932 * compl_enter_selects is set the Enter key does the same. */
3933 if ((c == Ctrl_Y || (compl_enter_selects
3934 && (c == CAR || c == K_KENTER || c == NL)))
3935 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003936 retval = TRUE;
3937
Bram Moolenaar47247282016-08-02 22:36:02 +02003938 /* CTRL-E means completion is Ended, go back to the typed text.
3939 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003940 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003941 {
3942 ins_compl_delete();
3943 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003944 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003945 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003946 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003947 retval = TRUE;
3948 }
3949
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003950 auto_format(FALSE, TRUE);
3951
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003953 compl_started = FALSE;
3954 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003955 if (!shortmess(SHM_COMPLETIONMENU))
3956 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003957 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003958 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 if (edit_submode != NULL)
3960 {
3961 edit_submode = NULL;
3962 showmode();
3963 }
3964
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003965#ifdef FEAT_CMDWIN
3966 if (c == Ctrl_C && cmdwin_type != 0)
3967 /* Avoid the popup menu remains displayed when leaving the
3968 * command line window. */
3969 update_screen(0);
3970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971#ifdef FEAT_CINDENT
3972 /*
3973 * Indent now if a key was typed that is in 'cinkeys'.
3974 */
3975 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3976 do_c_expr_indent();
3977#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003978 /* Trigger the CompleteDone event to give scripts a chance to act
3979 * upon the completion. */
3980 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003983 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3984 /* Trigger the CompleteDone event to give scripts a chance to act
3985 * upon the (possibly failed) completion. */
3986 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
3988 /* reset continue_* if we left expansion-mode, if we stay they'll be
3989 * (re)set properly in ins_complete() */
3990 if (!vim_is_ctrl_x_key(c))
3991 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003992 compl_cont_status = 0;
3993 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003995
3996 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997}
3998
3999/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004000 * Fix the redo buffer for the completion leader replacing some of the typed
4001 * text. This inserts backspaces and appends the changed text.
4002 * "ptr" is the known leader text or NUL.
4003 */
4004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004005ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004006{
4007 int len;
4008 char_u *p;
4009 char_u *ptr = ptr_arg;
4010
4011 if (ptr == NULL)
4012 {
4013 if (compl_leader != NULL)
4014 ptr = compl_leader;
4015 else
4016 return; /* nothing to do */
4017 }
4018 if (compl_orig_text != NULL)
4019 {
4020 p = compl_orig_text;
4021 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4022 ;
4023#ifdef FEAT_MBYTE
4024 if (len > 0)
4025 len -= (*mb_head_off)(p, p + len);
4026#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004027 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004028 AppendCharToRedobuff(K_BS);
4029 }
4030 else
4031 len = 0;
4032 if (ptr != NULL)
4033 AppendToRedobuffLit(ptr + len, -1);
4034}
4035
4036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4038 * (depending on flag) starting from buf and looking for a non-scanned
4039 * buffer (other than curbuf). curbuf is special, if it is called with
4040 * buf=curbuf then it has to be the first call for a given flag/expansion.
4041 *
4042 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4043 */
4044 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004045ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
4049 if (flag == 'w') /* just windows */
4050 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 if (buf == curbuf) /* first call for this flag/expansion */
4052 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004053 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 && wp->w_buffer->b_scanned)
4055 ;
4056 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
4058 else
4059 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4060 * (unlisted buffers)
4061 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004062 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 && ((flag == 'U'
4064 ? buf->b_p_bl
4065 : (!buf->b_p_bl
4066 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004067 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 ;
4069 return buf;
4070}
4071
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004072#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004073/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004074 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004075 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004076 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004078expand_by_function(
4079 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4080 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004081{
Bram Moolenaar82139082011-09-14 16:52:09 +02004082 list_T *matchlist = NULL;
4083 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004084 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004085 char_u *funcname;
4086 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004087 win_T *curwin_save;
4088 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004089 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004090
Bram Moolenaare344bea2005-09-01 20:46:49 +00004091 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4092 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004093 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004094
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004095 /* Call 'completefunc' to obtain the list of matches. */
4096 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004097 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004098
Bram Moolenaare344bea2005-09-01 20:46:49 +00004099 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004100 curwin_save = curwin;
4101 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004102
4103 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004104 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004105 {
4106 switch (rettv.v_type)
4107 {
4108 case VAR_LIST:
4109 matchlist = rettv.vval.v_list;
4110 break;
4111 case VAR_DICT:
4112 matchdict = rettv.vval.v_dict;
4113 break;
4114 default:
4115 /* TODO: Give error message? */
4116 clear_tv(&rettv);
4117 break;
4118 }
4119 }
4120
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004121 if (curwin_save != curwin || curbuf_save != curbuf)
4122 {
4123 EMSG(_(e_complwin));
4124 goto theend;
4125 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004126 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004127 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004128 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004129 {
4130 EMSG(_(e_compldel));
4131 goto theend;
4132 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004133
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004134 if (matchlist != NULL)
4135 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004136 else if (matchdict != NULL)
4137 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004138
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004139theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004140 if (matchdict != NULL)
4141 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004142 if (matchlist != NULL)
4143 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004144}
4145#endif /* FEAT_COMPL_FUNC */
4146
Bram Moolenaar39f05632006-03-19 22:15:26 +00004147#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004148/*
4149 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004150 */
4151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004152ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004153{
4154 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004155 int dir = compl_direction;
4156
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004157 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004158 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004159 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004160 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4161 /* if dir was BACKWARD then honor it just once */
4162 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004163 else if (did_emsg)
4164 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004165 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004166}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004167
4168/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004169 * Add completions from a dict.
4170 */
4171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004172ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004173{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004174 dictitem_T *di_refresh;
4175 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004176
4177 /* Check for optional "refresh" item. */
4178 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004179 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4180 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004181 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004182 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004183
4184 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4185 compl_opt_refresh_always = TRUE;
4186 }
4187
4188 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004189 di_words = dict_find(dict, (char_u *)"words", 5);
4190 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4191 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004192}
4193
4194/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004195 * Add a match to the list of matches from a typeval_T.
4196 * If the given string is already in the list of completions, then return
4197 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4198 * maybe because alloc() returns NULL, then FAIL is returned.
4199 */
4200 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004201ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004202{
4203 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004204 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004205 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004206 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004207 char_u *(cptext[CPT_COUNT]);
4208
4209 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4210 {
4211 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4212 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4213 (char_u *)"abbr", FALSE);
4214 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4215 (char_u *)"menu", FALSE);
4216 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4217 (char_u *)"kind", FALSE);
4218 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4219 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004220 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4221 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004222 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4223 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004224 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004225 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004226 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4227 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004228 }
4229 else
4230 {
4231 word = get_tv_string_chk(tv);
4232 vim_memset(cptext, 0, sizeof(cptext));
4233 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004234 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004235 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004236 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004237}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004238#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004239
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004240/*
4241 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004242 * The search starts at position "ini" in curbuf and in the direction
4243 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004244 * When "compl_started" is FALSE start at that position, otherwise continue
4245 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246 * This may return before finding all the matches.
4247 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 */
4249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004250ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
4252 static pos_T first_match_pos;
4253 static pos_T last_match_pos;
4254 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004255 static int found_all = FALSE; /* Found all matches of a
4256 certain type. */
4257 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
Bram Moolenaar572cb562005-08-05 21:35:02 +00004259 pos_T *pos;
4260 char_u **matches;
4261 int save_p_scs;
4262 int save_p_ws;
4263 int save_p_ic;
4264 int i;
4265 int num_matches;
4266 int len;
4267 int found_new_match;
4268 int type = ctrl_x_mode;
4269 char_u *ptr;
4270 char_u *dict = NULL;
4271 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004272 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004274 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004276 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 ins_buf->b_scanned = 0;
4278 found_all = FALSE;
4279 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004280 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004281 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 last_match_pos = first_match_pos = *ini;
4283 }
4284
Bram Moolenaar4475b622017-05-01 20:46:52 +02004285 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004286 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4288 for (;;)
4289 {
4290 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004291 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004293 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 * or if found_all says this entry is done. For ^X^L only use the
4295 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004296 if ((ctrl_x_mode == CTRL_X_NORMAL
4297 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004298 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 {
4300 found_all = FALSE;
4301 while (*e_cpt == ',' || *e_cpt == ' ')
4302 e_cpt++;
4303 if (*e_cpt == '.' && !curbuf->b_scanned)
4304 {
4305 ins_buf = curbuf;
4306 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004307 /* Move the cursor back one character so that ^N can match the
4308 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004309 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004310 {
4311 /* Move the cursor to after the last character in the
4312 * buffer, so that word at start of buffer is found
4313 * correctly. */
4314 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4315 first_match_pos.col =
4316 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 last_match_pos = first_match_pos;
4319 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004320
4321 /* Remember the first match so that the loop stops when we
4322 * wrap and come back there a second time. */
4323 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 }
4325 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4326 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4327 {
4328 /* Scan a buffer, but not the current one. */
4329 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4330 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004331 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 first_match_pos.col = last_match_pos.col = 0;
4333 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4334 last_match_pos.lnum = 0;
4335 type = 0;
4336 }
4337 else /* unloaded buffer, scan like dictionary */
4338 {
4339 found_all = TRUE;
4340 if (ins_buf->b_fname == NULL)
4341 continue;
4342 type = CTRL_X_DICTIONARY;
4343 dict = ins_buf->b_fname;
4344 dict_f = DICT_EXACT;
4345 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004346 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 ins_buf->b_fname == NULL
4348 ? buf_spname(ins_buf)
4349 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004350 ? ins_buf->b_fname
4351 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004352 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 else if (*e_cpt == NUL)
4355 break;
4356 else
4357 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004358 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 type = -1;
4360 else if (*e_cpt == 'k' || *e_cpt == 's')
4361 {
4362 if (*e_cpt == 'k')
4363 type = CTRL_X_DICTIONARY;
4364 else
4365 type = CTRL_X_THESAURUS;
4366 if (*++e_cpt != ',' && *e_cpt != NUL)
4367 {
4368 dict = e_cpt;
4369 dict_f = DICT_FIRST;
4370 }
4371 }
4372#ifdef FEAT_FIND_ID
4373 else if (*e_cpt == 'i')
4374 type = CTRL_X_PATH_PATTERNS;
4375 else if (*e_cpt == 'd')
4376 type = CTRL_X_PATH_DEFINES;
4377#endif
4378 else if (*e_cpt == ']' || *e_cpt == 't')
4379 {
4380 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004381 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004382 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384 else
4385 type = -1;
4386
4387 /* in any case e_cpt is advanced to the next entry */
4388 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4389
4390 found_all = TRUE;
4391 if (type == -1)
4392 continue;
4393 }
4394 }
4395
Bram Moolenaar4475b622017-05-01 20:46:52 +02004396 /* If complete() was called then compl_pattern has been reset. The
4397 * following won't work then, bail out. */
4398 if (compl_pattern == NULL)
4399 break;
4400
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 switch (type)
4402 {
4403 case -1:
4404 break;
4405#ifdef FEAT_FIND_ID
4406 case CTRL_X_PATH_PATTERNS:
4407 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004408 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004411 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4413 (linenr_T)1, (linenr_T)MAXLNUM);
4414 break;
4415#endif
4416
4417 case CTRL_X_DICTIONARY:
4418 case CTRL_X_THESAURUS:
4419 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004420 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 : (type == CTRL_X_THESAURUS
4422 ? (*curbuf->b_p_tsr == NUL
4423 ? p_tsr
4424 : curbuf->b_p_tsr)
4425 : (*curbuf->b_p_dict == NUL
4426 ? p_dict
4427 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004428 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004429 dict != NULL ? dict_f
4430 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 dict = NULL;
4432 break;
4433
4434 case CTRL_X_TAGS:
4435 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4436 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004439 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004440 * of matches is found when compl_pattern is empty */
4441 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004442 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4443 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4445 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004446 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 p_ic = save_p_ic;
4449 break;
4450
4451 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004452 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4454 {
4455
4456 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004458 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460 break;
4461
4462 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463 if (expand_cmdline(&compl_xp, compl_pattern,
4464 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004466 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 break;
4468
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004469#ifdef FEAT_COMPL_FUNC
4470 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004471 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004472 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004473 break;
4474#endif
4475
Bram Moolenaar488c6512005-08-11 20:09:58 +00004476 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004477#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004478 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004479 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004480 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004481 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004482#endif
4483 break;
4484
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 default: /* normal ^P/^N and ^X^L */
4486 /*
4487 * If 'infercase' is set, don't use 'smartcase' here
4488 */
4489 save_p_scs = p_scs;
4490 if (ins_buf->b_p_inf)
4491 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004492
Bram Moolenaar361aa502014-01-23 22:45:58 +01004493 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 * end but never from the middle, thus setting nowrapscan in this
4495 * buffers is a good idea, on the other hand, we always set
4496 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4497 save_p_ws = p_ws;
4498 if (ins_buf != curbuf)
4499 p_ws = FALSE;
4500 else if (*e_cpt == '.')
4501 p_ws = TRUE;
4502 for (;;)
4503 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004504 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004506 ++msg_silent; /* Don't want messages for wrapscan. */
4507
Bram Moolenaare4214502015-03-05 18:08:43 +01004508 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4509 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004510 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004511 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004512 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004514 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004516 found_new_match = searchit(NULL, ins_buf, pos,
4517 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004518 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004519 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004520 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004521 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004523 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004524 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 first_match_pos = *pos;
4526 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004527 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
4529 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004530 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 found_new_match = FAIL;
4532 if (found_new_match == FAIL)
4533 {
4534 if (ins_buf == curbuf)
4535 found_all = TRUE;
4536 break;
4537 }
4538
4539 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004540 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 && ini->lnum == pos->lnum
4542 && ini->col == pos->col)
4543 continue;
4544 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004545 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004547 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
4549 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4550 continue;
4551 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4552 if (!p_paste)
4553 ptr = skipwhite(ptr);
4554 }
4555 len = (int)STRLEN(ptr);
4556 }
4557 else
4558 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559 char_u *tmp_ptr = ptr;
4560
4561 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004563 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 /* Skip if already inside a word. */
4565 if (vim_iswordp(tmp_ptr))
4566 continue;
4567 /* Find start of next word. */
4568 tmp_ptr = find_word_start(tmp_ptr);
4569 }
4570 /* Find end of this word. */
4571 tmp_ptr = find_word_end(tmp_ptr);
4572 len = (int)(tmp_ptr - ptr);
4573
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004574 if ((compl_cont_status & CONT_ADDING)
4575 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
4577 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4578 {
4579 /* Try next line, if any. the new word will be
4580 * "join" as if the normal command "J" was used.
4581 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004582 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 * works -- Acevedo */
4584 STRNCPY(IObuff, ptr, len);
4585 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4586 tmp_ptr = ptr = skipwhite(ptr);
4587 /* Find start of next word. */
4588 tmp_ptr = find_word_start(tmp_ptr);
4589 /* Find end of next word. */
4590 tmp_ptr = find_word_end(tmp_ptr);
4591 if (tmp_ptr > ptr)
4592 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004593 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004595 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 IObuff[len++] = ' ';
4597 /* IObuf =~ "\k.* ", thus len >= 2 */
4598 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004599 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 || (vim_strchr(p_cpo, CPO_JOINSP)
4601 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004602 && (IObuff[len - 2] == '?'
4603 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 IObuff[len++] = ' ';
4605 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004606 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 if (tmp_ptr - ptr >= IOSIZE - len)
4608 tmp_ptr = ptr + IOSIZE - len - 1;
4609 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4610 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004611 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
4613 IObuff[len] = NUL;
4614 ptr = IObuff;
4615 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 continue;
4618 }
4619 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004620 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004621 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004622 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
4624 found_new_match = OK;
4625 break;
4626 }
4627 }
4628 p_scs = save_p_scs;
4629 p_ws = save_p_ws;
4630 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004631
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004632 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004633 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004634 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 found_new_match = OK;
4636
4637 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004638 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4639 * match */
4640 if ((ctrl_x_mode != CTRL_X_NORMAL
4641 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004642 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004643 {
4644 if (got_int)
4645 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004646 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004647 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004648 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004649
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004650 if ((ctrl_x_mode != CTRL_X_NORMAL
4651 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004652 || compl_interrupted)
4653 break;
4654 compl_started = TRUE;
4655 }
4656 else
4657 {
4658 /* Mark a buffer scanned when it has been scanned completely */
4659 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4660 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004662 compl_started = FALSE;
4663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004665 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004667 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 && *e_cpt == NUL) /* Got to end of 'complete' */
4669 found_new_match = FAIL;
4670
4671 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004672 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4673 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 i = ins_compl_make_cyclic();
4675
Bram Moolenaar4475b622017-05-01 20:46:52 +02004676 if (compl_old_match != NULL)
4677 {
4678 /* If several matches were added (FORWARD) or the search failed and has
4679 * just been made cyclic then we have to move compl_curr_match to the
4680 * next or previous entry (if any) -- Acevedo */
4681 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4682 : compl_old_match->cp_prev;
4683 if (compl_curr_match == NULL)
4684 compl_curr_match = compl_old_match;
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 return i;
4687}
4688
4689/* Delete the old text being completed. */
4690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004691ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004693 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694
4695 /*
4696 * In insert mode: Delete the typed part.
4697 * In replace mode: Put the old characters back, if any.
4698 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004699 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4700 if ((int)curwin->w_cursor.col > col)
4701 {
4702 if (stop_arrow() == FAIL)
4703 return;
4704 backspace_until_column(col);
4705 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004706
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004707 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4708 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004710 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004711 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712}
4713
Bram Moolenaar472e8592016-10-15 17:06:47 +02004714/*
4715 * Insert the new text being completed.
4716 * "in_compl_func" is TRUE when called from complete_check().
4717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004719ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004721 dict_T *dict;
4722
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004723 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004724 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4725 compl_used_match = FALSE;
4726 else
4727 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004728
4729 /* Set completed item. */
4730 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004731 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004732 if (dict != NULL)
4733 {
4734 dict_add_nr_str(dict, "word", 0L,
4735 EMPTY_IF_NULL(compl_shown_match->cp_str));
4736 dict_add_nr_str(dict, "abbr", 0L,
4737 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4738 dict_add_nr_str(dict, "menu", 0L,
4739 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4740 dict_add_nr_str(dict, "kind", 0L,
4741 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4742 dict_add_nr_str(dict, "info", 0L,
4743 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004744 dict_add_nr_str(dict, "user_data", 0L,
4745 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
Bram Moolenaar42a45122015-07-10 17:56:23 +02004746 }
4747 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004748 if (!in_compl_func)
4749 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750}
4751
4752/*
4753 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004754 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4755 * get more completions. If it is FALSE, then we just do nothing when there
4756 * are no more completions in a given direction. The latter case is used when
4757 * we are still in the middle of finding completions, to allow browsing
4758 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 * Return the total number of matches, or -1 if still unknown -- webb.
4760 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004761 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4762 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 *
4764 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004765 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4766 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 */
4768 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004769ins_compl_next(
4770 int allow_get_expansion,
4771 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004772 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004773 int insert_match, /* Insert the newly selected match */
4774 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004777 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004778 compl_T *found_compl = NULL;
4779 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004780 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004781 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004783 /* When user complete function return -1 for findstart which is next
4784 * time of 'always', compl_shown_match become NULL. */
4785 if (compl_shown_match == NULL)
4786 return -1;
4787
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004788 if (compl_leader != NULL
4789 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004791 /* Set "compl_shown_match" to the actually shown match, it may differ
4792 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004793 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004794 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004795 && compl_shown_match->cp_next != NULL
4796 && compl_shown_match->cp_next != compl_first_match)
4797 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004798
4799 /* If we didn't find it searching forward, and compl_shows_dir is
4800 * backward, find the last match. */
4801 if (compl_shows_dir == BACKWARD
4802 && !ins_compl_equal(compl_shown_match,
4803 compl_leader, (int)STRLEN(compl_leader))
4804 && (compl_shown_match->cp_next == NULL
4805 || compl_shown_match->cp_next == compl_first_match))
4806 {
4807 while (!ins_compl_equal(compl_shown_match,
4808 compl_leader, (int)STRLEN(compl_leader))
4809 && compl_shown_match->cp_prev != NULL
4810 && compl_shown_match->cp_prev != compl_first_match)
4811 compl_shown_match = compl_shown_match->cp_prev;
4812 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004813 }
4814
4815 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004816 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 /* Delete old text to be replaced */
4818 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004819
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004820 /* When finding the longest common text we stick at the original text,
4821 * don't let CTRL-N or CTRL-P move to the first match. */
4822 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4823
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004824 /* When restarting the search don't insert the first match either. */
4825 if (compl_restarting)
4826 {
4827 advance = FALSE;
4828 compl_restarting = FALSE;
4829 }
4830
Bram Moolenaare3226be2005-12-18 22:10:00 +00004831 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4832 * around. */
4833 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004835 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004837 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004838 found_end = (compl_first_match != NULL
4839 && (compl_shown_match->cp_next == compl_first_match
4840 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004841 }
4842 else if (compl_shows_dir == BACKWARD
4843 && compl_shown_match->cp_prev != NULL)
4844 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004845 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004846 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004847 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004850 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004851 if (!allow_get_expansion)
4852 {
4853 if (advance)
4854 {
4855 if (compl_shows_dir == BACKWARD)
4856 compl_pending -= todo + 1;
4857 else
4858 compl_pending += todo + 1;
4859 }
4860 return -1;
4861 }
4862
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004863 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004864 {
4865 if (compl_shows_dir == BACKWARD)
4866 --compl_pending;
4867 else
4868 ++compl_pending;
4869 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004870
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004871 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004872 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004873
4874 /* handle any pending completions */
4875 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004876 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004877 {
4878 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4879 {
4880 compl_shown_match = compl_shown_match->cp_next;
4881 --compl_pending;
4882 }
4883 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4884 {
4885 compl_shown_match = compl_shown_match->cp_prev;
4886 ++compl_pending;
4887 }
4888 else
4889 break;
4890 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004891 found_end = FALSE;
4892 }
4893 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4894 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004895 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004896 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004897 ++todo;
4898 else
4899 /* Remember a matching item. */
4900 found_compl = compl_shown_match;
4901
4902 /* Stop at the end of the list when we found a usable match. */
4903 if (found_end)
4904 {
4905 if (found_compl != NULL)
4906 {
4907 compl_shown_match = found_compl;
4908 break;
4909 }
4910 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004914 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004915 if (compl_no_insert && !started)
4916 {
4917 ins_bytes(compl_orig_text + ins_compl_len());
4918 compl_used_match = FALSE;
4919 }
4920 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004921 {
4922 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004923 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004924 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004925 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004926 }
4927 else
4928 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
4930 if (!allow_get_expansion)
4931 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004932 /* may undisplay the popup menu first */
4933 ins_compl_upd_pum();
4934
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004935 /* redraw to show the user what was inserted */
4936 update_screen(0);
4937
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004938 /* display the updated popup menu */
4939 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004940#ifdef FEAT_GUI
4941 if (gui.in_use)
4942 {
4943 /* Show the cursor after the match, not after the redrawn text. */
4944 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004945 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00004946 }
4947#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004948
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 /* Delete old text to be replaced, since we're still searching and
4950 * don't want to match ourselves! */
4951 ins_compl_delete();
4952 }
4953
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004954 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004955 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004956 if (compl_no_insert && !started)
4957 compl_enter_selects = TRUE;
4958 else
4959 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004960
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 /*
4962 * Show the file name for the match (if any)
4963 * Truncate the file name to avoid a wait for return.
4964 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004965 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02004967 char *lead = _("match in file");
4968 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4969 char_u *s;
4970 char_u *e;
4971
4972 if (space > 0)
4973 {
4974 /* We need the tail that fits. With double-byte encoding going
4975 * back from the end is very slow, thus go from the start and keep
4976 * the text that fits in "space" between "s" and "e". */
4977 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
4978 {
4979 space -= ptr2cells(e);
4980 while (space < 0)
4981 {
4982 space += ptr2cells(s);
4983 MB_PTR_ADV(s);
4984 }
4985 }
4986 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4987 s > compl_shown_match->cp_fname ? "<" : "", s);
4988 msg(IObuff);
4989 redraw_cmdline = FALSE; /* don't overwrite! */
4990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992
4993 return num_matches;
4994}
4995
4996/*
4997 * Call this while finding completions, to check whether the user has hit a key
4998 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004999 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005001 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005002 * "in_compl_func" is TRUE when called from complete_check(), don't set
5003 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 */
5005 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005006ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007{
5008 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005009 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005011 /* Don't check when reading keys from a script, :normal or feedkeys().
5012 * That would break the test scripts. But do check for keys when called
5013 * from complete_check(). */
5014 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 return;
5016
5017 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005018 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 return;
5020 count = 0;
5021
Bram Moolenaara260a972006-06-23 19:36:29 +00005022 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5023 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 if (c != NUL)
5026 {
5027 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5028 {
5029 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005030 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005031 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005032 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005034 else
5035 {
5036 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005037 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005038 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005039 if (c != K_IGNORE)
5040 {
5041 /* Don't interrupt completion when the character wasn't typed,
5042 * e.g., when doing @q to replay keys. */
5043 if (c != Ctrl_R && KeyTyped)
5044 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005045
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005046 vungetc(c);
5047 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005050 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005051 {
5052 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5053
5054 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005055 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005056 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005057}
5058
5059/*
5060 * Decide the direction of Insert mode complete from the key typed.
5061 * Returns BACKWARD or FORWARD.
5062 */
5063 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005064ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005065{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005066 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005067 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005068 return BACKWARD;
5069 return FORWARD;
5070}
5071
5072/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005073 * Return TRUE for keys that are used for completion only when the popup menu
5074 * is visible.
5075 */
5076 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005077ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005078{
5079 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005080 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5081 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005082}
5083
5084/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005085 * Decide the number of completions to move forward.
5086 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5087 */
5088 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005089ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005090{
5091 int h;
5092
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005093 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005094 {
5095 h = pum_get_height();
5096 if (h > 3)
5097 h -= 2; /* keep some context */
5098 return h;
5099 }
5100 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101}
5102
5103/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005104 * Return TRUE if completion with "c" should insert the match, FALSE if only
5105 * to change the currently selected completion.
5106 */
5107 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005108ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005109{
5110 switch (c)
5111 {
5112 case K_UP:
5113 case K_DOWN:
5114 case K_PAGEDOWN:
5115 case K_KPAGEDOWN:
5116 case K_S_DOWN:
5117 case K_PAGEUP:
5118 case K_KPAGEUP:
5119 case K_S_UP:
5120 return FALSE;
5121 }
5122 return TRUE;
5123}
5124
5125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 * Do Insert mode completion.
5127 * Called when character "c" was typed, which has a meaning for completion.
5128 * Returns OK if completion was done, FAIL if something failed (out of mem).
5129 */
5130 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005131ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005133 char_u *line;
5134 int startcol = 0; /* column where searched text starts */
5135 colnr_T curs_col; /* cursor column */
5136 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005137 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005138 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005139 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005140 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
Bram Moolenaare3226be2005-12-18 22:10:00 +00005142 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005143 insert_match = ins_compl_use_match(c);
5144
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005145 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
5147 /* First time we hit ^N or ^P (in a row, I mean) */
5148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 did_ai = FALSE;
5150#ifdef FEAT_SMARTINDENT
5151 did_si = FALSE;
5152 can_si = FALSE;
5153 can_si_back = FALSE;
5154#endif
5155 if (stop_arrow() == FAIL)
5156 return FAIL;
5157
5158 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005159 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005160 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005162 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005163 * "compl_startpos" to the cursor as a pattern to add a new word
5164 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005165 * "compl_startpos" is not in the same line as the cursor then fix it
5166 * (the line has been split because it was longer than 'tw'). if SOL
5167 * is set then skip the previous pattern, a word at the beginning of
5168 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005169 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5170 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
5172 /*
5173 * it is a continued search
5174 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005175 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005176 if (ctrl_x_mode == CTRL_X_NORMAL
5177 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5178 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005180 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005182 /* line (probably) wrapped, set compl_startpos to the
5183 * first non_blank in the line, if it is not a wordchar
5184 * include it to get a better pattern, but then we don't
5185 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005186 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005187 compl_startpos.col = compl_col;
5188 compl_startpos.lnum = curwin->w_cursor.lnum;
5189 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 else
5192 {
5193 /* S_IPOS was set when we inserted a word that was at the
5194 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005195 * mode but first we need to redefine compl_startpos */
5196 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005198 compl_cont_status |= CONT_SOL;
5199 compl_startpos.col = (colnr_T)(skipwhite(
5200 line + compl_length
5201 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005203 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005205 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005206 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005207 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005209 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005211 compl_cont_status &= ~CONT_SOL;
5212 compl_length = (IOSIZE - MIN_SPACE);
5213 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005215 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5216 if (compl_length < 1)
5217 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005219 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005220 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005222 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
5224 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005225 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005227 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005229 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005230 if (ctrl_x_mode != CTRL_X_NORMAL)
5231 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005232 compl_cont_status = 0;
5233 compl_cont_status |= CONT_N_ADDS;
5234 compl_startpos = curwin->w_cursor;
5235 startcol = (int)curs_col;
5236 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238
5239 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005240 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005242 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5244 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005245 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005247 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005249 compl_col += ++startcol;
5250 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 }
5252 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005253 compl_pattern = str_foldcase(line + compl_col,
5254 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005256 compl_pattern = vim_strnsave(line + compl_col,
5257 compl_length);
5258 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 return FAIL;
5260 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005261 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
5263 char_u *prefix = (char_u *)"\\<";
5264
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005265 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005266 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005267 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005268 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005270 if (!vim_iswordp(line + compl_col)
5271 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 && (
5273#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005274 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005276 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277#endif
5278 )))
5279 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 STRCPY((char *)compl_pattern, prefix);
5281 (void)quote_meta(compl_pattern + STRLEN(prefix),
5282 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005284 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005286 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289#endif
5290 )
5291 {
5292 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005293 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5294 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005296 compl_col += curs_col;
5297 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
5299 else
5300 {
5301#ifdef FEAT_MBYTE
5302 /* Search the point of change class of multibyte character
5303 * or not a word single byte character backward. */
5304 if (has_mbyte)
5305 {
5306 int base_class;
5307 int head_off;
5308
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005309 startcol -= (*mb_head_off)(line, line + startcol);
5310 base_class = mb_get_class(line + startcol);
5311 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 head_off = (*mb_head_off)(line, line + startcol);
5314 if (base_class != mb_get_class(line + startcol
5315 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005317 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
5319 }
5320 else
5321#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005322 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005324 compl_col += ++startcol;
5325 compl_length = (int)curs_col - startcol;
5326 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
5328 /* Only match word with at least two chars -- webb
5329 * there's no need to call quote_meta,
5330 * alloc(7) is enough -- Acevedo
5331 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005332 compl_pattern = alloc(7);
5333 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 STRCPY((char *)compl_pattern, "\\<");
5336 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5337 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
5339 else
5340 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005341 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005342 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005343 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005345 STRCPY((char *)compl_pattern, "\\<");
5346 (void)quote_meta(compl_pattern + 2, line + compl_col,
5347 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
5349 }
5350 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005351 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005353 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005354 compl_length = (int)curs_col - (int)compl_col;
5355 if (compl_length < 0) /* cursor in indent: empty pattern */
5356 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005358 compl_pattern = str_foldcase(line + compl_col, compl_length,
5359 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005361 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5362 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 return FAIL;
5364 }
5365 else if (ctrl_x_mode == CTRL_X_FILES)
5366 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005367 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005368 if (startcol > 0)
5369 {
5370 char_u *p = line + startcol;
5371
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005372 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005373 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005374 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005375 if (p == line && vim_isfilec(PTR2CHAR(p)))
5376 startcol = 0;
5377 else
5378 startcol = (int)(p - line) + 1;
5379 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005380
Bram Moolenaar0300e462013-09-08 16:03:45 +02005381 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005382 compl_length = (int)curs_col - startcol;
5383 compl_pattern = addstar(line + compl_col, compl_length,
5384 EXPAND_FILES);
5385 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 return FAIL;
5387 }
5388 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5389 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005390 compl_pattern = vim_strnsave(line, curs_col);
5391 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005393 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005394 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005395 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5396 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005397 /* No completion possible, use an empty pattern to get a
5398 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005399 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005400 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005401 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5402 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005404 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005405 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005406#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005407 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005408 * Call user defined function 'completefunc' with "a:findstart"
5409 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005410 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005411 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005412 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005413 char_u *funcname;
5414 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005415 win_T *curwin_save;
5416 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005417
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005418 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005419 * string */
5420 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5421 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5422 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005423 {
5424 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5425 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005426 /* restore did_ai, so that adding comment leader works */
5427 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005428 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005429 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005430
5431 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005432 args[1] = NULL;
5433 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005434 curwin_save = curwin;
5435 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005436 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005437 if (curwin_save != curwin || curbuf_save != curbuf)
5438 {
5439 EMSG(_(e_complwin));
5440 return FAIL;
5441 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005442 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005443 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005444 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005445 {
5446 EMSG(_(e_compldel));
5447 return FAIL;
5448 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005449
Bram Moolenaar6110a002012-01-26 18:58:38 +01005450 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005451 * cancel the complete without an error.
5452 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005453 if (col == -2)
5454 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005455 if (col == -3)
5456 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005457 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005458 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005459 if (!shortmess(SHM_COMPLETIONMENU))
5460 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005461 return FAIL;
5462 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005463
Bram Moolenaar82139082011-09-14 16:52:09 +02005464 /*
5465 * Reset extended parameters of completion, when start new
5466 * completion.
5467 */
5468 compl_opt_refresh_always = FALSE;
5469
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005470 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005471 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005472 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005473 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005474 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005475
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005476 /* Setup variables for completion. Need to obtain "line" again,
5477 * it may have become invalid. */
5478 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005479 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005480 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5481 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005482#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005483 return FAIL;
5484 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005485 else if (ctrl_x_mode == CTRL_X_SPELL)
5486 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005487#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005488 if (spell_bad_len > 0)
5489 compl_col = curs_col - spell_bad_len;
5490 else
5491 compl_col = spell_word_start(startcol);
5492 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005493 {
5494 compl_length = 0;
5495 compl_col = curs_col;
5496 }
5497 else
5498 {
5499 spell_expand_check_cap(compl_col);
5500 compl_length = (int)curs_col - compl_col;
5501 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005502 /* Need to obtain "line" again, it may have become invalid. */
5503 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005504 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5505 if (compl_pattern == NULL)
5506#endif
5507 return FAIL;
5508 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005509 else
5510 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005511 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005512 return FAIL;
5513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005515 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 {
5517 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005518 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 {
5520 /* Insert a new line, keep indentation but ignore 'comments' */
5521#ifdef FEAT_COMMENTS
5522 char_u *old = curbuf->b_p_com;
5523
5524 curbuf->b_p_com = (char_u *)"";
5525#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005526 compl_startpos.lnum = curwin->w_cursor.lnum;
5527 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 ins_eol('\r');
5529#ifdef FEAT_COMMENTS
5530 curbuf->b_p_com = old;
5531#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005532 compl_length = 0;
5533 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
5535 }
5536 else
5537 {
5538 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005539 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 }
5541
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005542 if (compl_cont_status & CONT_LOCAL)
5543 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 else
5545 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5546
Bram Moolenaar62951b12011-09-21 18:23:05 +02005547 /* If any of the original typed text has been changed we need to fix
5548 * the redo buffer. */
5549 ins_compl_fixRedoBufForLeader(NULL);
5550
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005551 /* Always add completion for the original text. */
5552 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005553 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5554 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005555 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005557 VIM_CLEAR(compl_pattern);
5558 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 return FAIL;
5560 }
5561
5562 /* showmode might reset the internal line pointers, so it must
5563 * be called before line = ml_get(), or when this address is no
5564 * longer needed. -- Acevedo.
5565 */
5566 edit_submode_extra = (char_u *)_("-- Searching...");
5567 edit_submode_highl = HLF_COUNT;
5568 showmode();
5569 edit_submode_extra = NULL;
5570 out_flush();
5571 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005572 else if (insert_match && stop_arrow() == FAIL)
5573 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005575 compl_shown_match = compl_curr_match;
5576 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
5578 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005579 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005581 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005582 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005583 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005585 /* may undisplay the popup menu */
5586 ins_compl_upd_pum();
5587
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005588 if (n > 1) /* all matches have been found */
5589 compl_matches = n;
5590 compl_curr_match = compl_shown_match;
5591 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592
Bram Moolenaard68071d2006-05-02 22:08:30 +00005593 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5594 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 if (got_int && !global_busy)
5596 {
5597 (void)vgetc();
5598 got_int = FALSE;
5599 }
5600
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005601 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005602 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005604 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5605 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5607 edit_submode_highl = HLF_E;
5608 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5609 * because we couldn't expand anything at first place, but if we used
5610 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5611 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005612 if ( compl_length > 1
5613 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005614 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5616 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005617 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619
Bram Moolenaar572cb562005-08-05 21:35:02 +00005620 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005621 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005623 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
5625 if (edit_submode_extra == NULL)
5626 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005627 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 {
5629 edit_submode_extra = (char_u *)_("Back at original");
5630 edit_submode_highl = HLF_W;
5631 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005632 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 {
5634 edit_submode_extra = (char_u *)_("Word from other line");
5635 edit_submode_highl = HLF_COUNT;
5636 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005637 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
5639 edit_submode_extra = (char_u *)_("The only match");
5640 edit_submode_highl = HLF_COUNT;
5641 }
5642 else
5643 {
5644 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005645 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005647 int number = 0;
5648 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005650 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 {
5652 /* search backwards for the first valid (!= -1) number.
5653 * This should normally succeed already at the first loop
5654 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005655 for (match = compl_curr_match->cp_prev; match != NULL
5656 && match != compl_first_match;
5657 match = match->cp_prev)
5658 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005660 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 break;
5662 }
5663 if (match != NULL)
5664 /* go up and assign all numbers which are not assigned
5665 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005666 for (match = match->cp_next;
5667 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005668 match = match->cp_next)
5669 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 }
5671 else /* BACKWARD */
5672 {
5673 /* search forwards (upwards) for the first valid (!= -1)
5674 * number. This should normally succeed already at the
5675 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005676 for (match = compl_curr_match->cp_next; match != NULL
5677 && match != compl_first_match;
5678 match = match->cp_next)
5679 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005681 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 break;
5683 }
5684 if (match != NULL)
5685 /* go down and assign all numbers which are not
5686 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005687 for (match = match->cp_prev; match
5688 && match->cp_number == -1;
5689 match = match->cp_prev)
5690 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 }
5693
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005694 /* The match should always have a sequence number now, this is
5695 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005696 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005698 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5699 * Translations may need more than twice that. */
5700 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005702 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005703 vim_snprintf((char *)match_ref, sizeof(match_ref),
5704 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005705 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005707 vim_snprintf((char *)match_ref, sizeof(match_ref),
5708 _("match %d"),
5709 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 edit_submode_extra = match_ref;
5711 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005712 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 curs_columns(FALSE);
5714 }
5715 }
5716 }
5717
5718 /* Show a message about what (completion) mode we're in. */
5719 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005720 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005722 if (edit_submode_extra != NULL)
5723 {
5724 if (!p_smd)
5725 msg_attr(edit_submode_extra,
5726 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005727 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005728 }
5729 else
5730 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732
Bram Moolenaard68071d2006-05-02 22:08:30 +00005733 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005734 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005735 show_pum(save_w_wrow, save_w_leftcol);
5736
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005737 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005738 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005739
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 return OK;
5741}
5742
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005743 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005744show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005745{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005746 /* RedrawingDisabled may be set when invoked through complete(). */
5747 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005748
Bram Moolenaarcb036422017-03-01 12:29:10 +01005749 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005750
Bram Moolenaarcb036422017-03-01 12:29:10 +01005751 /* If the cursor moved or the display scrolled we need to remove the pum
5752 * first. */
5753 setcursor();
5754 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5755 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005756
Bram Moolenaarcb036422017-03-01 12:29:10 +01005757 ins_compl_show_pum();
5758 setcursor();
5759 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005760}
5761
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762/*
5763 * Looks in the first "len" chars. of "src" for search-metachars.
5764 * If dest is not NULL the chars. are copied there quoting (with
5765 * a backslash) the metachars, and dest would be NUL terminated.
5766 * Returns the length (needed) of dest
5767 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005768 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005769quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005771 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005773 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 switch (*src)
5776 {
5777 case '.':
5778 case '*':
5779 case '[':
5780 if (ctrl_x_mode == CTRL_X_DICTIONARY
5781 || ctrl_x_mode == CTRL_X_THESAURUS)
5782 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005783 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 case '~':
5785 if (!p_magic) /* quote these only if magic is set */
5786 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005787 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 case '\\':
5789 if (ctrl_x_mode == CTRL_X_DICTIONARY
5790 || ctrl_x_mode == CTRL_X_THESAURUS)
5791 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005792 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 case '^': /* currently it's not needed. */
5794 case '$':
5795 m++;
5796 if (dest != NULL)
5797 *dest++ = '\\';
5798 break;
5799 }
5800 if (dest != NULL)
5801 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005802# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 /* Copy remaining bytes of a multibyte character. */
5804 if (has_mbyte)
5805 {
5806 int i, mb_len;
5807
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005808 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 if (mb_len > 0 && len >= mb_len)
5810 for (i = 0; i < mb_len; ++i)
5811 {
5812 --len;
5813 ++src;
5814 if (dest != NULL)
5815 *dest++ = *src;
5816 }
5817 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 }
5820 if (dest != NULL)
5821 *dest = NUL;
5822
5823 return m;
5824}
5825#endif /* FEAT_INS_EXPAND */
5826
5827/*
5828 * Next character is interpreted literally.
5829 * A one, two or three digit decimal number is interpreted as its byte value.
5830 * If one or two digits are entered, the next character is given to vungetc().
5831 * For Unicode a character > 255 may be returned.
5832 */
5833 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005834get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 int cc;
5837 int nc;
5838 int i;
5839 int hex = FALSE;
5840 int octal = FALSE;
5841#ifdef FEAT_MBYTE
5842 int unicode = 0;
5843#endif
5844
5845 if (got_int)
5846 return Ctrl_C;
5847
5848#ifdef FEAT_GUI
5849 /*
5850 * In GUI there is no point inserting the internal code for a special key.
5851 * It is more useful to insert the string "<KEY>" instead. This would
5852 * probably be useful in a text window too, but it would not be
5853 * vi-compatible (maybe there should be an option for it?) -- webb
5854 */
5855 if (gui.in_use)
5856 ++allow_keys;
5857#endif
5858#ifdef USE_ON_FLY_SCROLL
5859 dont_scroll = TRUE; /* disallow scrolling here */
5860#endif
5861 ++no_mapping; /* don't map the next key hits */
5862 cc = 0;
5863 i = 0;
5864 for (;;)
5865 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005866 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867#ifdef FEAT_CMDL_INFO
5868 if (!(State & CMDLINE)
5869# ifdef FEAT_MBYTE
5870 && MB_BYTE2LEN_CHECK(nc) == 1
5871# endif
5872 )
5873 add_to_showcmd(nc);
5874#endif
5875 if (nc == 'x' || nc == 'X')
5876 hex = TRUE;
5877 else if (nc == 'o' || nc == 'O')
5878 octal = TRUE;
5879#ifdef FEAT_MBYTE
5880 else if (nc == 'u' || nc == 'U')
5881 unicode = nc;
5882#endif
5883 else
5884 {
5885 if (hex
5886#ifdef FEAT_MBYTE
5887 || unicode != 0
5888#endif
5889 )
5890 {
5891 if (!vim_isxdigit(nc))
5892 break;
5893 cc = cc * 16 + hex2nr(nc);
5894 }
5895 else if (octal)
5896 {
5897 if (nc < '0' || nc > '7')
5898 break;
5899 cc = cc * 8 + nc - '0';
5900 }
5901 else
5902 {
5903 if (!VIM_ISDIGIT(nc))
5904 break;
5905 cc = cc * 10 + nc - '0';
5906 }
5907
5908 ++i;
5909 }
5910
5911 if (cc > 255
5912#ifdef FEAT_MBYTE
5913 && unicode == 0
5914#endif
5915 )
5916 cc = 255; /* limit range to 0-255 */
5917 nc = 0;
5918
5919 if (hex) /* hex: up to two chars */
5920 {
5921 if (i >= 2)
5922 break;
5923 }
5924#ifdef FEAT_MBYTE
5925 else if (unicode) /* Unicode: up to four or eight chars */
5926 {
5927 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5928 break;
5929 }
5930#endif
5931 else if (i >= 3) /* decimal or octal: up to three chars */
5932 break;
5933 }
5934 if (i == 0) /* no number entered */
5935 {
5936 if (nc == K_ZERO) /* NUL is stored as NL */
5937 {
5938 cc = '\n';
5939 nc = 0;
5940 }
5941 else
5942 {
5943 cc = nc;
5944 nc = 0;
5945 }
5946 }
5947
5948 if (cc == 0) /* NUL is stored as NL */
5949 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005950#ifdef FEAT_MBYTE
5951 if (enc_dbcs && (cc & 0xff) == 0)
5952 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5953 second byte will cause trouble! */
5954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955
5956 --no_mapping;
5957#ifdef FEAT_GUI
5958 if (gui.in_use)
5959 --allow_keys;
5960#endif
5961 if (nc)
5962 vungetc(nc);
5963 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5964 return cc;
5965}
5966
5967/*
5968 * Insert character, taking care of special keys and mod_mask
5969 */
5970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971insert_special(
5972 int c,
5973 int allow_modmask,
5974 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975{
5976 char_u *p;
5977 int len;
5978
5979 /*
5980 * Special function key, translate into "<Key>". Up to the last '>' is
5981 * inserted with ins_str(), so as not to replace characters in replace
5982 * mode.
5983 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5984 * unless 'allow_modmask' is TRUE.
5985 */
Bram Moolenaard0573012017-10-28 21:11:06 +02005986#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 /* Command-key never produces a normal key */
5988 if (mod_mask & MOD_MASK_CMD)
5989 allow_modmask = TRUE;
5990#endif
5991 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5992 {
5993 p = get_special_key_name(c, mod_mask);
5994 len = (int)STRLEN(p);
5995 c = p[len - 1];
5996 if (len > 2)
5997 {
5998 if (stop_arrow() == FAIL)
5999 return;
6000 p[len - 1] = NUL;
6001 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006002 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 ctrlv = FALSE;
6004 }
6005 }
6006 if (stop_arrow() == OK)
6007 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6008}
6009
6010/*
6011 * Special characters in this context are those that need processing other
6012 * than the simple insertion that can be performed here. This includes ESC
6013 * which terminates the insert, and CR/NL which need special processing to
6014 * open up a new line. This routine tries to optimize insertions performed by
6015 * the "redo", "undo" or "put" commands, so it needs to know when it should
6016 * stop and defer processing to the "normal" mechanism.
6017 * '0' and '^' are special, because they can be followed by CTRL-D.
6018 */
6019#ifdef EBCDIC
6020# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6021#else
6022# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6023#endif
6024
6025#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006026# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006028# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029#endif
6030
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006031/*
6032 * "flags": INSCHAR_FORMAT - force formatting
6033 * INSCHAR_CTRLV - char typed just after CTRL-V
6034 * INSCHAR_NO_FEX - don't use 'formatexpr'
6035 *
6036 * NOTE: passes the flags value straight through to internal_format() which,
6037 * beside INSCHAR_FORMAT (above), is also looking for these:
6038 * INSCHAR_DO_COM - format comments
6039 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042insertchar(
6043 int c, /* character to insert or NUL */
6044 int flags, /* INSCHAR_FORMAT, etc. */
6045 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 int textwidth;
6048#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006052 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006054 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
6057 /*
6058 * Try to break the line in two or more pieces when:
6059 * - Always do this if we have been called to do formatting only.
6060 * - Always do this when 'formatoptions' has the 'a' flag and the line
6061 * ends in white space.
6062 * - Otherwise:
6063 * - Don't do this if inserting a blank
6064 * - Don't do this if an existing character is being replaced, unless
6065 * we're in VREPLACE mode.
6066 * - Do this if the cursor is not on the line where insert started
6067 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6068 * before the insert.
6069 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6070 * before 'textwidth'
6071 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006072 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006073 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006074 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 && !((State & REPLACE_FLAG)
6076#ifdef FEAT_VREPLACE
6077 && !(State & VREPLACE_FLAG)
6078#endif
6079 && *ml_get_cursor() != NUL)
6080 && (curwin->w_cursor.lnum != Insstart.lnum
6081 || ((!has_format_option(FO_INS_LONG)
6082 || Insstart_textlen <= (colnr_T)textwidth)
6083 && (!fo_ins_blank
6084 || Insstart_blank_vcol <= (colnr_T)textwidth
6085 ))))))
6086 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006087 /* Format with 'formatexpr' when it's set. Use internal formatting
6088 * when 'formatexpr' isn't set or it returns non-zero. */
6089#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006090 int do_internal = TRUE;
6091 colnr_T virtcol = get_nolist_virtcol()
6092 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006093
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006094 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6095 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006096 {
6097 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6098 /* It may be required to save for undo again, e.g. when setline()
6099 * was called. */
6100 ins_need_undo = TRUE;
6101 }
6102 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006104 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006106
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 if (c == NUL) /* only formatting was wanted */
6108 return;
6109
6110#ifdef FEAT_COMMENTS
6111 /* Check whether this character should end a comment. */
6112 if (did_ai && (int)c == end_comment_pending)
6113 {
6114 char_u *line;
6115 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6116 int middle_len, end_len;
6117 int i;
6118
6119 /*
6120 * Need to remove existing (middle) comment leader and insert end
6121 * comment leader. First, check what comment leader we can find.
6122 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006123 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6125 {
6126 /* Skip middle-comment string */
6127 while (*p && p[-1] != ':') /* find end of middle flags */
6128 ++p;
6129 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6130 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006131 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 --middle_len;
6133
6134 /* Find the end-comment string */
6135 while (*p && p[-1] != ':') /* find end of end flags */
6136 ++p;
6137 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6138
6139 /* Skip white space before the cursor */
6140 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006141 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 ;
6143 i++;
6144
6145 /* Skip to before the middle leader */
6146 i -= middle_len;
6147
6148 /* Check some expected things before we go on */
6149 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6150 {
6151 /* Backspace over all the stuff we want to replace */
6152 backspace_until_column(i);
6153
6154 /*
6155 * Insert the end-comment string, except for the last
6156 * character, which will get inserted as normal later.
6157 */
6158 ins_bytes_len(lead_end, end_len - 1);
6159 }
6160 }
6161 }
6162 end_comment_pending = NUL;
6163#endif
6164
6165 did_ai = FALSE;
6166#ifdef FEAT_SMARTINDENT
6167 did_si = FALSE;
6168 can_si = FALSE;
6169 can_si_back = FALSE;
6170#endif
6171
6172 /*
6173 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6174 * This speeds up normal text input considerably.
6175 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6176 * need to re-indent at a ':', or any other character (but not what
6177 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006178 * Don't do this when there an InsertCharPre autocommand is defined,
6179 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 */
6181#ifdef USE_ON_FLY_SCROLL
6182 dont_scroll = FALSE; /* allow scrolling here */
6183#endif
6184
6185 if ( !ISSPECIAL(c)
6186#ifdef FEAT_MBYTE
6187 && (!has_mbyte || (*mb_char2len)(c) == 1)
6188#endif
6189 && vpeekc() != NUL
6190 && !(State & REPLACE_FLAG)
6191#ifdef FEAT_CINDENT
6192 && !cindent_on()
6193#endif
6194#ifdef FEAT_RIGHTLEFT
6195 && !p_ri
6196#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006197 && !has_insertcharpre())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 {
6199#define INPUT_BUFLEN 100
6200 char_u buf[INPUT_BUFLEN + 1];
6201 int i;
6202 colnr_T virtcol = 0;
6203
6204 buf[0] = c;
6205 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 virtcol = get_nolist_virtcol();
6208 /*
6209 * Stop the string when:
6210 * - no more chars available
6211 * - finding a special character (command key)
6212 * - buffer is full
6213 * - running into the 'textwidth' boundary
6214 * - need to check for abbreviation: A non-word char after a word-char
6215 */
6216 while ( (c = vpeekc()) != NUL
6217 && !ISSPECIAL(c)
6218#ifdef FEAT_MBYTE
6219 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6220#endif
6221 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006222# ifdef FEAT_FKMAP
6223 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 && (textwidth == 0
6226 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6227 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6228 {
6229#ifdef FEAT_RIGHTLEFT
6230 c = vgetc();
6231 if (p_hkmap && KeyTyped)
6232 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 buf[i++] = c;
6234#else
6235 buf[i++] = vgetc();
6236#endif
6237 }
6238
6239#ifdef FEAT_DIGRAPHS
6240 do_digraph(-1); /* clear digraphs */
6241 do_digraph(buf[i-1]); /* may be the start of a digraph */
6242#endif
6243 buf[i] = NUL;
6244 ins_str(buf);
6245 if (flags & INSCHAR_CTRLV)
6246 {
6247 redo_literal(*buf);
6248 i = 1;
6249 }
6250 else
6251 i = 0;
6252 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006253 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255 else
6256 {
6257#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006258 int cc;
6259
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6261 {
6262 char_u buf[MB_MAXBYTES + 1];
6263
6264 (*mb_char2bytes)(c, buf);
6265 buf[cc] = NUL;
6266 ins_char_bytes(buf, cc);
6267 AppendCharToRedobuff(c);
6268 }
6269 else
6270#endif
6271 {
6272 ins_char(c);
6273 if (flags & INSCHAR_CTRLV)
6274 redo_literal(c);
6275 else
6276 AppendCharToRedobuff(c);
6277 }
6278 }
6279}
6280
6281/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006282 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006283 *
6284 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6285 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006286 */
6287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006288internal_format(
6289 int textwidth,
6290 int second_indent,
6291 int flags,
6292 int format_only,
6293 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006294{
6295 int cc;
6296 int save_char = NUL;
6297 int haveto_redraw = FALSE;
6298 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6299#ifdef FEAT_MBYTE
6300 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6301#endif
6302 int fo_white_par = has_format_option(FO_WHITE_PAR);
6303 int first_line = TRUE;
6304#ifdef FEAT_COMMENTS
6305 colnr_T leader_len;
6306 int no_leader = FALSE;
6307 int do_comments = (flags & INSCHAR_DO_COM);
6308#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006309#ifdef FEAT_LINEBREAK
6310 int has_lbr = curwin->w_p_lbr;
6311
6312 /* make sure win_lbr_chartabsize() counts correctly */
6313 curwin->w_p_lbr = FALSE;
6314#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006315
6316 /*
6317 * When 'ai' is off we don't want a space under the cursor to be
6318 * deleted. Replace it with an 'x' temporarily.
6319 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006320 if (!curbuf->b_p_ai
6321#ifdef FEAT_VREPLACE
6322 && !(State & VREPLACE_FLAG)
6323#endif
6324 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006325 {
6326 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006327 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006328 {
6329 save_char = cc;
6330 pchar_cursor('x');
6331 }
6332 }
6333
6334 /*
6335 * Repeat breaking lines, until the current line is not too long.
6336 */
6337 while (!got_int)
6338 {
6339 int startcol; /* Cursor column at entry */
6340 int wantcol; /* column at textwidth border */
6341 int foundcol; /* column for start of spaces */
6342 int end_foundcol = 0; /* column for start of word */
6343 colnr_T len;
6344 colnr_T virtcol;
6345#ifdef FEAT_VREPLACE
6346 int orig_col = 0;
6347 char_u *saved_text = NULL;
6348#endif
6349 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006350 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006351
Bram Moolenaar97b98102009-11-17 16:41:01 +00006352 virtcol = get_nolist_virtcol()
6353 + char2cells(c != NUL ? c : gchar_cursor());
6354 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006355 break;
6356
6357#ifdef FEAT_COMMENTS
6358 if (no_leader)
6359 do_comments = FALSE;
6360 else if (!(flags & INSCHAR_FORMAT)
6361 && has_format_option(FO_WRAP_COMS))
6362 do_comments = TRUE;
6363
6364 /* Don't break until after the comment leader */
6365 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006366 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006367 else
6368 leader_len = 0;
6369
6370 /* If the line doesn't start with a comment leader, then don't
6371 * start one in a following broken line. Avoids that a %word
6372 * moved to the start of the next line causes all following lines
6373 * to start with %. */
6374 if (leader_len == 0)
6375 no_leader = TRUE;
6376#endif
6377 if (!(flags & INSCHAR_FORMAT)
6378#ifdef FEAT_COMMENTS
6379 && leader_len == 0
6380#endif
6381 && !has_format_option(FO_WRAP))
6382
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006383 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006384 if ((startcol = curwin->w_cursor.col) == 0)
6385 break;
6386
6387 /* find column of textwidth border */
6388 coladvance((colnr_T)textwidth);
6389 wantcol = curwin->w_cursor.col;
6390
Bram Moolenaar97b98102009-11-17 16:41:01 +00006391 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006392 foundcol = 0;
6393
6394 /*
6395 * Find position to break at.
6396 * Stop at first entered white when 'formatoptions' has 'v'
6397 */
6398 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006399 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006400 || curwin->w_cursor.lnum != Insstart.lnum
6401 || curwin->w_cursor.col >= Insstart.col)
6402 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006403 if (curwin->w_cursor.col == startcol && c != NUL)
6404 cc = c;
6405 else
6406 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006407 if (WHITECHAR(cc))
6408 {
6409 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006410 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006411
6412 /* find start of sequence of blanks */
6413 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6414 {
6415 dec_cursor();
6416 cc = gchar_cursor();
6417 }
6418 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6419 break; /* only spaces in front of text */
6420#ifdef FEAT_COMMENTS
6421 /* Don't break until after the comment leader */
6422 if (curwin->w_cursor.col < leader_len)
6423 break;
6424#endif
6425 if (has_format_option(FO_ONE_LETTER))
6426 {
6427 /* do not break after one-letter words */
6428 if (curwin->w_cursor.col == 0)
6429 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006430#ifdef FEAT_COMMENTS
6431 /* do not break "#a b" when 'tw' is 2 */
6432 if (curwin->w_cursor.col <= leader_len)
6433 break;
6434#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006435 col = curwin->w_cursor.col;
6436 dec_cursor();
6437 cc = gchar_cursor();
6438
6439 if (WHITECHAR(cc))
6440 continue; /* one-letter, continue */
6441 curwin->w_cursor.col = col;
6442 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006443
6444 inc_cursor();
6445
6446 end_foundcol = end_col + 1;
6447 foundcol = curwin->w_cursor.col;
6448 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006449 break;
6450 }
6451#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006452 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006453 {
6454 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006455 if (curwin->w_cursor.col != startcol)
6456 {
6457#ifdef FEAT_COMMENTS
6458 /* Don't break until after the comment leader */
6459 if (curwin->w_cursor.col < leader_len)
6460 break;
6461#endif
6462 col = curwin->w_cursor.col;
6463 inc_cursor();
6464 /* Don't change end_foundcol if already set. */
6465 if (foundcol != curwin->w_cursor.col)
6466 {
6467 foundcol = curwin->w_cursor.col;
6468 end_foundcol = foundcol;
6469 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6470 break;
6471 }
6472 curwin->w_cursor.col = col;
6473 }
6474
6475 if (curwin->w_cursor.col == 0)
6476 break;
6477
6478 col = curwin->w_cursor.col;
6479
6480 dec_cursor();
6481 cc = gchar_cursor();
6482
6483 if (WHITECHAR(cc))
6484 continue; /* break with space */
6485#ifdef FEAT_COMMENTS
6486 /* Don't break until after the comment leader */
6487 if (curwin->w_cursor.col < leader_len)
6488 break;
6489#endif
6490
6491 curwin->w_cursor.col = col;
6492
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006493 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006494 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006495 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6496 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006497 }
6498#endif
6499 if (curwin->w_cursor.col == 0)
6500 break;
6501 dec_cursor();
6502 }
6503
6504 if (foundcol == 0) /* no spaces, cannot break line */
6505 {
6506 curwin->w_cursor.col = startcol;
6507 break;
6508 }
6509
6510 /* Going to break the line, remove any "$" now. */
6511 undisplay_dollar();
6512
6513 /*
6514 * Offset between cursor position and line break is used by replace
6515 * stack functions. VREPLACE does not use this, and backspaces
6516 * over the text instead.
6517 */
6518#ifdef FEAT_VREPLACE
6519 if (State & VREPLACE_FLAG)
6520 orig_col = startcol; /* Will start backspacing from here */
6521 else
6522#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006523 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006524
6525 /*
6526 * adjust startcol for spaces that will be deleted and
6527 * characters that will remain on top line
6528 */
6529 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006530 while ((cc = gchar_cursor(), WHITECHAR(cc))
6531 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006532 inc_cursor();
6533 startcol -= curwin->w_cursor.col;
6534 if (startcol < 0)
6535 startcol = 0;
6536
6537#ifdef FEAT_VREPLACE
6538 if (State & VREPLACE_FLAG)
6539 {
6540 /*
6541 * In VREPLACE mode, we will backspace over the text to be
6542 * wrapped, so save a copy now to put on the next line.
6543 */
6544 saved_text = vim_strsave(ml_get_cursor());
6545 curwin->w_cursor.col = orig_col;
6546 if (saved_text == NULL)
6547 break; /* Can't do it, out of memory */
6548 saved_text[startcol] = NUL;
6549
6550 /* Backspace over characters that will move to the next line */
6551 if (!fo_white_par)
6552 backspace_until_column(foundcol);
6553 }
6554 else
6555#endif
6556 {
6557 /* put cursor after pos. to break line */
6558 if (!fo_white_par)
6559 curwin->w_cursor.col = foundcol;
6560 }
6561
6562 /*
6563 * Split the line just before the margin.
6564 * Only insert/delete lines, but don't really redraw the window.
6565 */
6566 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6567 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6568#ifdef FEAT_COMMENTS
6569 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006570 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006571#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006572 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6573 if (!(flags & INSCHAR_COM_LIST))
6574 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006575
6576 replace_offset = 0;
6577 if (first_line)
6578 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006579 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006580 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006581 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006582 * This section is for auto-wrap of numeric lists. When not
6583 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6584 * flag will be set and open_line() will handle it (as seen
6585 * above). The code here (and in get_number_indent()) will
6586 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006587 */
6588 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006589 second_indent =
6590 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006591 if (second_indent >= 0)
6592 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006593#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006594 if (State & VREPLACE_FLAG)
6595 change_indent(INDENT_SET, second_indent,
6596 FALSE, NUL, TRUE);
6597 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006598#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006599#ifdef FEAT_COMMENTS
6600 if (leader_len > 0 && second_indent - leader_len > 0)
6601 {
6602 int i;
6603 int padding = second_indent - leader_len;
6604
6605 /* We started at the first_line of a numbered list
6606 * that has a comment. the open_line() function has
6607 * inserted the proper comment leader and positioned
6608 * the cursor at the end of the split line. Now we
6609 * add the additional whitespace needed after the
6610 * comment leader for the numbered list. */
6611 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006612 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006613 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006614 }
6615 else
6616 {
6617#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006618 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006619#ifdef FEAT_COMMENTS
6620 }
6621#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006622 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006623 }
6624 first_line = FALSE;
6625 }
6626
6627#ifdef FEAT_VREPLACE
6628 if (State & VREPLACE_FLAG)
6629 {
6630 /*
6631 * In VREPLACE mode we have backspaced over the text to be
6632 * moved, now we re-insert it into the new line.
6633 */
6634 ins_bytes(saved_text);
6635 vim_free(saved_text);
6636 }
6637 else
6638#endif
6639 {
6640 /*
6641 * Check if cursor is not past the NUL off the line, cindent
6642 * may have added or removed indent.
6643 */
6644 curwin->w_cursor.col += startcol;
6645 len = (colnr_T)STRLEN(ml_get_curline());
6646 if (curwin->w_cursor.col > len)
6647 curwin->w_cursor.col = len;
6648 }
6649
6650 haveto_redraw = TRUE;
6651#ifdef FEAT_CINDENT
6652 can_cindent = TRUE;
6653#endif
6654 /* moved the cursor, don't autoindent or cindent now */
6655 did_ai = FALSE;
6656#ifdef FEAT_SMARTINDENT
6657 did_si = FALSE;
6658 can_si = FALSE;
6659 can_si_back = FALSE;
6660#endif
6661 line_breakcheck();
6662 }
6663
6664 if (save_char != NUL) /* put back space after cursor */
6665 pchar_cursor(save_char);
6666
Bram Moolenaar0026d472014-09-09 16:32:39 +02006667#ifdef FEAT_LINEBREAK
6668 curwin->w_p_lbr = has_lbr;
6669#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006670 if (!format_only && haveto_redraw)
6671 {
6672 update_topline();
6673 redraw_curbuf_later(VALID);
6674 }
6675}
6676
6677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 * Called after inserting or deleting text: When 'formatoptions' includes the
6679 * 'a' flag format from the current line until the end of the paragraph.
6680 * Keep the cursor at the same position relative to the text.
6681 * The caller must have saved the cursor line for undo, following ones will be
6682 * saved here.
6683 */
6684 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685auto_format(
6686 int trailblank, /* when TRUE also format with trailing blank */
6687 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
6689 pos_T pos;
6690 colnr_T len;
6691 char_u *old;
6692 char_u *new, *pnew;
6693 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006694 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
6696 if (!has_format_option(FO_AUTO))
6697 return;
6698
6699 pos = curwin->w_cursor;
6700 old = ml_get_curline();
6701
6702 /* may remove added space */
6703 check_auto_format(FALSE);
6704
6705 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6706 * user might insert normal text next. Also skip formatting when "1" is
6707 * in 'formatoptions' and there is a single character before the cursor.
6708 * Otherwise the line would be broken and when typing another non-white
6709 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006710 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 if (*old != NUL && !trailblank && wasatend)
6712 {
6713 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006714 cc = gchar_cursor();
6715 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6716 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006718 cc = gchar_cursor();
6719 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 {
6721 curwin->w_cursor = pos;
6722 return;
6723 }
6724 curwin->w_cursor = pos;
6725 }
6726
6727#ifdef FEAT_COMMENTS
6728 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6729 * comments. */
6730 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006731 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 return;
6733#endif
6734
6735 /*
6736 * May start formatting in a previous line, so that after "x" a word is
6737 * moved to the previous line if it fits there now. Only when this is not
6738 * the start of a paragraph.
6739 */
6740 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6741 {
6742 --curwin->w_cursor.lnum;
6743 if (u_save_cursor() == FAIL)
6744 return;
6745 }
6746
6747 /*
6748 * Do the formatting and restore the cursor position. "saved_cursor" will
6749 * be adjusted for the text formatting.
6750 */
6751 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006752 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 curwin->w_cursor = saved_cursor;
6754 saved_cursor.lnum = 0;
6755
6756 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6757 {
6758 /* "cannot happen" */
6759 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6760 coladvance((colnr_T)MAXCOL);
6761 }
6762 else
6763 check_cursor_col();
6764
6765 /* Insert mode: If the cursor is now after the end of the line while it
6766 * previously wasn't, the line was broken. Because of the rule above we
6767 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6768 * formatted. */
6769 if (!wasatend && has_format_option(FO_WHITE_PAR))
6770 {
6771 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006772 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 if (curwin->w_cursor.col == len)
6774 {
6775 pnew = vim_strnsave(new, len + 2);
6776 pnew[len] = ' ';
6777 pnew[len + 1] = NUL;
6778 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6779 /* remove the space later */
6780 did_add_space = TRUE;
6781 }
6782 else
6783 /* may remove added space */
6784 check_auto_format(FALSE);
6785 }
6786
6787 check_cursor();
6788}
6789
6790/*
6791 * When an extra space was added to continue a paragraph for auto-formatting,
6792 * delete it now. The space must be under the cursor, just after the insert
6793 * position.
6794 */
6795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006796check_auto_format(
6797 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798{
6799 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006800 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
6802 if (did_add_space)
6803 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006804 cc = gchar_cursor();
6805 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 /* Somehow the space was removed already. */
6807 did_add_space = FALSE;
6808 else
6809 {
6810 if (!end_insert)
6811 {
6812 inc_cursor();
6813 c = gchar_cursor();
6814 dec_cursor();
6815 }
6816 if (c != NUL)
6817 {
6818 /* The space is no longer at the end of the line, delete it. */
6819 del_char(FALSE);
6820 did_add_space = FALSE;
6821 }
6822 }
6823 }
6824}
6825
6826/*
6827 * Find out textwidth to be used for formatting:
6828 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006829 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 * if invalid value, use 0.
6831 * Set default to window width (maximum 79) for "gq" operator.
6832 */
6833 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006834comp_textwidth(
6835 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836{
6837 int textwidth;
6838
6839 textwidth = curbuf->b_p_tw;
6840 if (textwidth == 0 && curbuf->b_p_wm)
6841 {
6842 /* The width is the window width minus 'wrapmargin' minus all the
6843 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006844 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845#ifdef FEAT_CMDWIN
6846 if (cmdwin_type != 0)
6847 textwidth -= 1;
6848#endif
6849#ifdef FEAT_FOLDING
6850 textwidth -= curwin->w_p_fdc;
6851#endif
6852#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006853 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 textwidth -= 1;
6855#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006856 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 textwidth -= 8;
6858 }
6859 if (textwidth < 0)
6860 textwidth = 0;
6861 if (ff && textwidth == 0)
6862 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006863 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 if (textwidth > 79)
6865 textwidth = 79;
6866 }
6867 return textwidth;
6868}
6869
6870/*
6871 * Put a character in the redo buffer, for when just after a CTRL-V.
6872 */
6873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006874redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875{
6876 char_u buf[10];
6877
6878 /* Only digits need special treatment. Translate them into a string of
6879 * three digits. */
6880 if (VIM_ISDIGIT(c))
6881 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006882 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 AppendToRedobuff(buf);
6884 }
6885 else
6886 AppendCharToRedobuff(c);
6887}
6888
6889/*
6890 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006891 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 */
6893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006894start_arrow(
6895 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006897 start_arrow_common(end_insert_pos, TRUE);
6898}
6899
6900/*
6901 * Like start_arrow() but with end_change argument.
6902 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6903 */
6904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006905start_arrow_with_change(
6906 pos_T *end_insert_pos, /* can be NULL */
6907 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006908{
6909 start_arrow_common(end_insert_pos, end_change);
6910 if (!end_change)
6911 {
6912 AppendCharToRedobuff(Ctrl_G);
6913 AppendCharToRedobuff('U');
6914 }
6915}
6916
6917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006918start_arrow_common(
6919 pos_T *end_insert_pos, /* can be NULL */
6920 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006921{
6922 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 {
6924 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006925 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 arrow_used = TRUE; /* this means we stopped the current insert */
6927 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006928#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006929 check_spell_redraw();
6930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931}
6932
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006933#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006934/*
6935 * If we skipped highlighting word at cursor, do it now.
6936 * It may be skipped again, thus reset spell_redraw_lnum first.
6937 */
6938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006939check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006940{
6941 if (spell_redraw_lnum != 0)
6942 {
6943 linenr_T lnum = spell_redraw_lnum;
6944
6945 spell_redraw_lnum = 0;
6946 redrawWinline(lnum, FALSE);
6947 }
6948}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006949
6950/*
6951 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6952 * spelled word, if there is one.
6953 */
6954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006955spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006956{
6957 pos_T tpos = curwin->w_cursor;
6958
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006959 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006960 if (curwin->w_cursor.col != tpos.col)
6961 start_arrow(&tpos);
6962}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006963#endif
6964
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965/*
6966 * stop_arrow() is called before a change is made in insert mode.
6967 * If an arrow key has been used, start a new insertion.
6968 * Returns FAIL if undo is impossible, shouldn't insert then.
6969 */
6970 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006971stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972{
6973 if (arrow_used)
6974 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006975 Insstart = curwin->w_cursor; /* new insertion starts here */
6976 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6977 /* Don't update the original insert position when moved to the
6978 * right, except when nothing was inserted yet. */
6979 update_Insstart_orig = FALSE;
6980 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6981
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 if (u_save_cursor() == OK)
6983 {
6984 arrow_used = FALSE;
6985 ins_need_undo = FALSE;
6986 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006987
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 ai_col = 0;
6989#ifdef FEAT_VREPLACE
6990 if (State & VREPLACE_FLAG)
6991 {
6992 orig_line_count = curbuf->b_ml.ml_line_count;
6993 vr_lines_changed = 1;
6994 }
6995#endif
6996 ResetRedobuff();
6997 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006998 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 }
7000 else if (ins_need_undo)
7001 {
7002 if (u_save_cursor() == OK)
7003 ins_need_undo = FALSE;
7004 }
7005
7006#ifdef FEAT_FOLDING
7007 /* Always open fold at the cursor line when inserting something. */
7008 foldOpenCursor();
7009#endif
7010
7011 return (arrow_used || ins_need_undo ? FAIL : OK);
7012}
7013
7014/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007015 * Do a few things to stop inserting.
7016 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7017 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 */
7019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007020stop_insert(
7021 pos_T *end_insert_pos,
7022 int esc, /* called by ins_esc() */
7023 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007025 int cc;
7026 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
7028 stop_redo_ins();
7029 replace_flush(); /* abandon replace stack */
7030
7031 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007032 * Save the inserted text for later redo with ^@ and CTRL-A.
7033 * Don't do it when "restart_edit" was set and nothing was inserted,
7034 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007036 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007037 if (did_restart_edit == 0 || (ptr != NULL
7038 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007039 {
7040 vim_free(last_insert);
7041 last_insert = ptr;
7042 last_insert_skip = new_insert_skip;
7043 }
7044 else
7045 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007047 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 {
7049 /* Auto-format now. It may seem strange to do this when stopping an
7050 * insertion (or moving the cursor), but it's required when appending
7051 * a line and having it end in a space. But only do it when something
7052 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007053 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007055 pos_T tpos = curwin->w_cursor;
7056
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 /* When the cursor is at the end of the line after a space the
7058 * formatting will move it to the following word. Avoid that by
7059 * moving the cursor onto the space. */
7060 cc = 'x';
7061 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7062 {
7063 dec_cursor();
7064 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007065 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007066 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 }
7068
7069 auto_format(TRUE, FALSE);
7070
Bram Moolenaar1c465442017-03-12 20:10:05 +01007071 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007072 {
7073 if (gchar_cursor() != NUL)
7074 inc_cursor();
7075#ifdef FEAT_VIRTUALEDIT
7076 /* If the cursor is still at the same character, also keep
7077 * the "coladd". */
7078 if (gchar_cursor() == NUL
7079 && curwin->w_cursor.lnum == tpos.lnum
7080 && curwin->w_cursor.col == tpos.col)
7081 curwin->w_cursor.coladd = tpos.coladd;
7082#endif
7083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
7085
7086 /* If a space was inserted for auto-formatting, remove it now. */
7087 check_auto_format(TRUE);
7088
7089 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007090 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007091 * Do this when ESC was used or moving the cursor up/down.
7092 * Check for the old position still being valid, just in case the text
7093 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007094 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007095 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7096 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007098 pos_T tpos = curwin->w_cursor;
7099
7100 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007101 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007102 for (;;)
7103 {
7104 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7105 --curwin->w_cursor.col;
7106 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007107 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007108 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007109 if (del_char(TRUE) == FAIL)
7110 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007111 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007112 if (curwin->w_cursor.lnum != tpos.lnum)
7113 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007114 else
7115 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007116 /* reset tpos, could have been invalidated in the loop above */
7117 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007118 tpos.col++;
7119 if (cc != NUL && gchar_pos(&tpos) == NUL)
7120 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 /* <C-S-Right> may have started Visual mode, adjust the position for
7124 * deleted characters. */
7125 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7126 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007127 int len = (int)STRLEN(ml_get_curline());
7128
7129 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007131 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007132#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 }
7136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 }
7138 }
7139 did_ai = FALSE;
7140#ifdef FEAT_SMARTINDENT
7141 did_si = FALSE;
7142 can_si = FALSE;
7143 can_si_back = FALSE;
7144#endif
7145
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007146 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7147 * now in a different buffer. */
7148 if (end_insert_pos != NULL)
7149 {
7150 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007151 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007152 curbuf->b_op_end = *end_insert_pos;
7153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154}
7155
7156/*
7157 * Set the last inserted text to a single character.
7158 * Used for the replace command.
7159 */
7160 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007161set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
7163 char_u *s;
7164
7165 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 if (last_insert != NULL)
7168 {
7169 s = last_insert;
7170 /* Use the CTRL-V only when entering a special char */
7171 if (c < ' ' || c == DEL)
7172 *s++ = Ctrl_V;
7173 s = add_char2buf(c, s);
7174 *s++ = ESC;
7175 *s++ = NUL;
7176 last_insert_skip = 0;
7177 }
7178}
7179
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007180#if defined(EXITFREE) || defined(PROTO)
7181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007182free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007183{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007184 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007185# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007186 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007187# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007188}
7189#endif
7190
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191/*
7192 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7193 * and CSI. Handle multi-byte characters.
7194 * Returns a pointer to after the added bytes.
7195 */
7196 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007197add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
7199#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007200 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 int i;
7202 int len;
7203
7204 len = (*mb_char2bytes)(c, temp);
7205 for (i = 0; i < len; ++i)
7206 {
7207 c = temp[i];
7208#endif
7209 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7210 if (c == K_SPECIAL)
7211 {
7212 *s++ = K_SPECIAL;
7213 *s++ = KS_SPECIAL;
7214 *s++ = KE_FILLER;
7215 }
7216#ifdef FEAT_GUI
7217 else if (c == CSI)
7218 {
7219 *s++ = CSI;
7220 *s++ = KS_EXTRA;
7221 *s++ = (int)KE_CSI;
7222 }
7223#endif
7224 else
7225 *s++ = c;
7226#ifdef FEAT_MBYTE
7227 }
7228#endif
7229 return s;
7230}
7231
7232/*
7233 * move cursor to start of line
7234 * if flags & BL_WHITE move to first non-white
7235 * if flags & BL_SOL move to first non-white if startofline is set,
7236 * otherwise keep "curswant" column
7237 * if flags & BL_FIX don't leave the cursor on a NUL.
7238 */
7239 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007240beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241{
7242 if ((flags & BL_SOL) && !p_sol)
7243 coladvance(curwin->w_curswant);
7244 else
7245 {
7246 curwin->w_cursor.col = 0;
7247#ifdef FEAT_VIRTUALEDIT
7248 curwin->w_cursor.coladd = 0;
7249#endif
7250
7251 if (flags & (BL_WHITE | BL_SOL))
7252 {
7253 char_u *ptr;
7254
Bram Moolenaar1c465442017-03-12 20:10:05 +01007255 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7257 ++curwin->w_cursor.col;
7258 }
7259 curwin->w_set_curswant = TRUE;
7260 }
7261}
7262
7263/*
7264 * oneright oneleft cursor_down cursor_up
7265 *
7266 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007267 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 * Return OK when successful, FAIL when we hit a line of file boundary.
7269 */
7270
7271 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007272oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273{
7274 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276
7277#ifdef FEAT_VIRTUALEDIT
7278 if (virtual_active())
7279 {
7280 pos_T prevpos = curwin->w_cursor;
7281
7282 /* Adjust for multi-wide char (excluding TAB) */
7283 ptr = ml_get_cursor();
7284 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007285# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007287# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 ))
7291 ? ptr2cells(ptr) : 1));
7292 curwin->w_set_curswant = TRUE;
7293 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7294 return (prevpos.col != curwin->w_cursor.col
7295 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7296 }
7297#endif
7298
7299 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007300 if (*ptr == NUL)
7301 return FAIL; /* already at the very end */
7302
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007304 if (has_mbyte)
7305 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 else
7307#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007308 l = 1;
7309
7310 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7311 * contains "onemore". */
7312 if (ptr[l] == NUL
7313#ifdef FEAT_VIRTUALEDIT
7314 && (ve_flags & VE_ONEMORE) == 0
7315#endif
7316 )
7317 return FAIL;
7318 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320 curwin->w_set_curswant = TRUE;
7321 return OK;
7322}
7323
7324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007325oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326{
7327#ifdef FEAT_VIRTUALEDIT
7328 if (virtual_active())
7329 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007330# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 int v = getviscol();
7334
7335 if (v == 0)
7336 return FAIL;
7337
7338# ifdef FEAT_LINEBREAK
7339 /* We might get stuck on 'showbreak', skip over it. */
7340 width = 1;
7341 for (;;)
7342 {
7343 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007344 /* getviscol() is slow, skip it when 'showbreak' is empty,
7345 * 'breakindent' is not set and there are no multi-byte
7346 * characters */
7347 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348# ifdef FEAT_MBYTE
7349 && !has_mbyte
7350# endif
7351 ) || getviscol() < v)
7352 break;
7353 ++width;
7354 }
7355# else
7356 coladvance(v - 1);
7357# endif
7358
7359 if (curwin->w_cursor.coladd == 1)
7360 {
7361 char_u *ptr;
7362
7363 /* Adjust for multi-wide char (not a TAB) */
7364 ptr = ml_get_cursor();
7365 if (*ptr != TAB && vim_isprintc(
7366# ifdef FEAT_MBYTE
7367 (*mb_ptr2char)(ptr)
7368# else
7369 *ptr
7370# endif
7371 ) && ptr2cells(ptr) > 1)
7372 curwin->w_cursor.coladd = 0;
7373 }
7374
7375 curwin->w_set_curswant = TRUE;
7376 return OK;
7377 }
7378#endif
7379
7380 if (curwin->w_cursor.col == 0)
7381 return FAIL;
7382
7383 curwin->w_set_curswant = TRUE;
7384 --curwin->w_cursor.col;
7385
7386#ifdef FEAT_MBYTE
7387 /* if the character on the left of the current cursor is a multi-byte
7388 * character, move to its first byte */
7389 if (has_mbyte)
7390 mb_adjust_cursor();
7391#endif
7392 return OK;
7393}
7394
7395 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007396cursor_up(
7397 long n,
7398 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399{
7400 linenr_T lnum;
7401
7402 if (n > 0)
7403 {
7404 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007405 /* This fails if the cursor is already in the first line or the count
7406 * is larger than the line number and '-' is in 'cpoptions' */
7407 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 return FAIL;
7409 if (n >= lnum)
7410 lnum = 1;
7411 else
7412#ifdef FEAT_FOLDING
7413 if (hasAnyFolding(curwin))
7414 {
7415 /*
7416 * Count each sequence of folded lines as one logical line.
7417 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007418 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 (void)hasFolding(lnum, &lnum, NULL);
7420
7421 while (n--)
7422 {
7423 /* move up one line */
7424 --lnum;
7425 if (lnum <= 1)
7426 break;
7427 /* If we entered a fold, move to the beginning, unless in
7428 * Insert mode or when 'foldopen' contains "all": it will open
7429 * in a moment. */
7430 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7431 (void)hasFolding(lnum, &lnum, NULL);
7432 }
7433 if (lnum < 1)
7434 lnum = 1;
7435 }
7436 else
7437#endif
7438 lnum -= n;
7439 curwin->w_cursor.lnum = lnum;
7440 }
7441
7442 /* try to advance to the column we want to be at */
7443 coladvance(curwin->w_curswant);
7444
7445 if (upd_topline)
7446 update_topline(); /* make sure curwin->w_topline is valid */
7447
7448 return OK;
7449}
7450
7451/*
7452 * Cursor down a number of logical lines.
7453 */
7454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007455cursor_down(
7456 long n,
7457 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
7459 linenr_T lnum;
7460
7461 if (n > 0)
7462 {
7463 lnum = curwin->w_cursor.lnum;
7464#ifdef FEAT_FOLDING
7465 /* Move to last line of fold, will fail if it's the end-of-file. */
7466 (void)hasFolding(lnum, NULL, &lnum);
7467#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007468 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007469 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007470 if (lnum >= curbuf->b_ml.ml_line_count
7471 || (lnum + n > curbuf->b_ml.ml_line_count
7472 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 return FAIL;
7474 if (lnum + n >= curbuf->b_ml.ml_line_count)
7475 lnum = curbuf->b_ml.ml_line_count;
7476 else
7477#ifdef FEAT_FOLDING
7478 if (hasAnyFolding(curwin))
7479 {
7480 linenr_T last;
7481
7482 /* count each sequence of folded lines as one logical line */
7483 while (n--)
7484 {
7485 if (hasFolding(lnum, NULL, &last))
7486 lnum = last + 1;
7487 else
7488 ++lnum;
7489 if (lnum >= curbuf->b_ml.ml_line_count)
7490 break;
7491 }
7492 if (lnum > curbuf->b_ml.ml_line_count)
7493 lnum = curbuf->b_ml.ml_line_count;
7494 }
7495 else
7496#endif
7497 lnum += n;
7498 curwin->w_cursor.lnum = lnum;
7499 }
7500
7501 /* try to advance to the column we want to be at */
7502 coladvance(curwin->w_curswant);
7503
7504 if (upd_topline)
7505 update_topline(); /* make sure curwin->w_topline is valid */
7506
7507 return OK;
7508}
7509
7510/*
7511 * Stuff the last inserted text in the read buffer.
7512 * Last_insert actually is a copy of the redo buffer, so we
7513 * first have to remove the command.
7514 */
7515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007516stuff_inserted(
7517 int c, /* Command character to be inserted */
7518 long count, /* Repeat this many times */
7519 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
7521 char_u *esc_ptr;
7522 char_u *ptr;
7523 char_u *last_ptr;
7524 char_u last = NUL;
7525
7526 ptr = get_last_insert();
7527 if (ptr == NULL)
7528 {
7529 EMSG(_(e_noinstext));
7530 return FAIL;
7531 }
7532
7533 /* may want to stuff the command character, to start Insert mode */
7534 if (c != NUL)
7535 stuffcharReadbuff(c);
7536 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7537 *esc_ptr = NUL; /* remove the ESC */
7538
7539 /* when the last char is either "0" or "^" it will be quoted if no ESC
7540 * comes after it OR if it will inserted more than once and "ptr"
7541 * starts with ^D. -- Acevedo
7542 */
7543 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7544 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7545 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7546 {
7547 last = *last_ptr;
7548 *last_ptr = NUL;
7549 }
7550
7551 do
7552 {
7553 stuffReadbuff(ptr);
7554 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7555 if (last)
7556 stuffReadbuff((char_u *)(last == '0'
7557 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7558 : IF_EB("\026^", CTRL_V_STR "^")));
7559 }
7560 while (--count > 0);
7561
7562 if (last)
7563 *last_ptr = last;
7564
7565 if (esc_ptr != NULL)
7566 *esc_ptr = ESC; /* put the ESC back */
7567
7568 /* may want to stuff a trailing ESC, to get out of Insert mode */
7569 if (!no_esc)
7570 stuffcharReadbuff(ESC);
7571
7572 return OK;
7573}
7574
7575 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007576get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577{
7578 if (last_insert == NULL)
7579 return NULL;
7580 return last_insert + last_insert_skip;
7581}
7582
7583/*
7584 * Get last inserted string, and remove trailing <Esc>.
7585 * Returns pointer to allocated memory (must be freed) or NULL.
7586 */
7587 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007588get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589{
7590 char_u *s;
7591 int len;
7592
7593 if (last_insert == NULL)
7594 return NULL;
7595 s = vim_strsave(last_insert + last_insert_skip);
7596 if (s != NULL)
7597 {
7598 len = (int)STRLEN(s);
7599 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7600 s[len - 1] = NUL;
7601 }
7602 return s;
7603}
7604
7605/*
7606 * Check the word in front of the cursor for an abbreviation.
7607 * Called when the non-id character "c" has been entered.
7608 * When an abbreviation is recognized it is removed from the text and
7609 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7610 */
7611 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007612echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613{
7614 /* Don't check for abbreviation in paste mode, when disabled and just
7615 * after moving around with cursor keys. */
7616 if (p_paste || no_abbr || arrow_used)
7617 return FALSE;
7618
7619 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7620 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7621}
7622
7623/*
7624 * replace-stack functions
7625 *
7626 * When replacing characters, the replaced characters are remembered for each
7627 * new character. This is used to re-insert the old text when backspacing.
7628 *
7629 * There is a NUL headed list of characters for each character that is
7630 * currently in the file after the insertion point. When BS is used, one NUL
7631 * headed list is put back for the deleted character.
7632 *
7633 * For a newline, there are two NUL headed lists. One contains the characters
7634 * that the NL replaced. The extra one stores the characters after the cursor
7635 * that were deleted (always white space).
7636 *
7637 * Replace_offset is normally 0, in which case replace_push will add a new
7638 * character at the end of the stack. If replace_offset is not 0, that many
7639 * characters will be left on the stack above the newly inserted character.
7640 */
7641
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007642static char_u *replace_stack = NULL;
7643static long replace_stack_nr = 0; /* next entry in replace stack */
7644static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645
7646 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007647replace_push(
7648 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649{
7650 char_u *p;
7651
7652 if (replace_stack_nr < replace_offset) /* nothing to do */
7653 return;
7654 if (replace_stack_len <= replace_stack_nr)
7655 {
7656 replace_stack_len += 50;
7657 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7658 if (p == NULL) /* out of memory */
7659 {
7660 replace_stack_len -= 50;
7661 return;
7662 }
7663 if (replace_stack != NULL)
7664 {
7665 mch_memmove(p, replace_stack,
7666 (size_t)(replace_stack_nr * sizeof(char_u)));
7667 vim_free(replace_stack);
7668 }
7669 replace_stack = p;
7670 }
7671 p = replace_stack + replace_stack_nr - replace_offset;
7672 if (replace_offset)
7673 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7674 *p = c;
7675 ++replace_stack_nr;
7676}
7677
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007678#if defined(FEAT_MBYTE) || defined(PROTO)
7679/*
7680 * Push a character onto the replace stack. Handles a multi-byte character in
7681 * reverse byte order, so that the first byte is popped off first.
7682 * Return the number of bytes done (includes composing characters).
7683 */
7684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007685replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007686{
7687 int l = (*mb_ptr2len)(p);
7688 int j;
7689
7690 for (j = l - 1; j >= 0; --j)
7691 replace_push(p[j]);
7692 return l;
7693}
7694#endif
7695
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696/*
7697 * Pop one item from the replace stack.
7698 * return -1 if stack empty
7699 * return replaced character or NUL otherwise
7700 */
7701 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007702replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703{
7704 if (replace_stack_nr == 0)
7705 return -1;
7706 return (int)replace_stack[--replace_stack_nr];
7707}
7708
7709/*
7710 * Join the top two items on the replace stack. This removes to "off"'th NUL
7711 * encountered.
7712 */
7713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007714replace_join(
7715 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
7717 int i;
7718
7719 for (i = replace_stack_nr; --i >= 0; )
7720 if (replace_stack[i] == NUL && off-- <= 0)
7721 {
7722 --replace_stack_nr;
7723 mch_memmove(replace_stack + i, replace_stack + i + 1,
7724 (size_t)(replace_stack_nr - i));
7725 return;
7726 }
7727}
7728
7729/*
7730 * Pop bytes from the replace stack until a NUL is found, and insert them
7731 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7732 */
7733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007734replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735{
7736 int cc;
7737 int oldState = State;
7738
7739 State = NORMAL; /* don't want REPLACE here */
7740 while ((cc = replace_pop()) > 0)
7741 {
7742#ifdef FEAT_MBYTE
7743 mb_replace_pop_ins(cc);
7744#else
7745 ins_char(cc);
7746#endif
7747 dec_cursor();
7748 }
7749 State = oldState;
7750}
7751
7752#ifdef FEAT_MBYTE
7753/*
7754 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7755 * indicates a multi-byte char, pop the other bytes too.
7756 */
7757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007758mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759{
7760 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007761 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 int i;
7763 int c;
7764
7765 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7766 {
7767 buf[0] = cc;
7768 for (i = 1; i < n; ++i)
7769 buf[i] = replace_pop();
7770 ins_bytes_len(buf, n);
7771 }
7772 else
7773 ins_char(cc);
7774
7775 if (enc_utf8)
7776 /* Handle composing chars. */
7777 for (;;)
7778 {
7779 c = replace_pop();
7780 if (c == -1) /* stack empty */
7781 break;
7782 if ((n = MB_BYTE2LEN(c)) == 1)
7783 {
7784 /* Not a multi-byte char, put it back. */
7785 replace_push(c);
7786 break;
7787 }
7788 else
7789 {
7790 buf[0] = c;
7791 for (i = 1; i < n; ++i)
7792 buf[i] = replace_pop();
7793 if (utf_iscomposing(utf_ptr2char(buf)))
7794 ins_bytes_len(buf, n);
7795 else
7796 {
7797 /* Not a composing char, put it back. */
7798 for (i = n - 1; i >= 0; --i)
7799 replace_push(buf[i]);
7800 break;
7801 }
7802 }
7803 }
7804}
7805#endif
7806
7807/*
7808 * make the replace stack empty
7809 * (called when exiting replace mode)
7810 */
7811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007812replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007814 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 replace_stack_len = 0;
7816 replace_stack_nr = 0;
7817}
7818
7819/*
7820 * Handle doing a BS for one character.
7821 * cc < 0: replace stack empty, just move cursor
7822 * cc == 0: character was inserted, delete it
7823 * cc > 0: character was replaced, put cc (first byte of original char) back
7824 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007825 * When "limit_col" is >= 0, don't delete before this column. Matters when
7826 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 */
7828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007829replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830{
7831 int cc;
7832#ifdef FEAT_VREPLACE
7833 int orig_len = 0;
7834 int ins_len;
7835 int orig_vcols = 0;
7836 colnr_T start_vcol;
7837 char_u *p;
7838 int i;
7839 int vcol;
7840#endif
7841
7842 cc = replace_pop();
7843 if (cc > 0)
7844 {
7845#ifdef FEAT_VREPLACE
7846 if (State & VREPLACE_FLAG)
7847 {
7848 /* Get the number of screen cells used by the character we are
7849 * going to delete. */
7850 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7851 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7852 }
7853#endif
7854#ifdef FEAT_MBYTE
7855 if (has_mbyte)
7856 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007857 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858# ifdef FEAT_VREPLACE
7859 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007860 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861# endif
7862 replace_push(cc);
7863 }
7864 else
7865#endif
7866 {
7867 pchar_cursor(cc);
7868#ifdef FEAT_VREPLACE
7869 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007870 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871#endif
7872 }
7873 replace_pop_ins();
7874
7875#ifdef FEAT_VREPLACE
7876 if (State & VREPLACE_FLAG)
7877 {
7878 /* Get the number of screen cells used by the inserted characters */
7879 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007880 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 vcol = start_vcol;
7882 for (i = 0; i < ins_len; ++i)
7883 {
7884 vcol += chartabsize(p + i, vcol);
7885#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007886 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887#endif
7888 }
7889 vcol -= start_vcol;
7890
7891 /* Delete spaces that were inserted after the cursor to keep the
7892 * text aligned. */
7893 curwin->w_cursor.col += ins_len;
7894 while (vcol > orig_vcols && gchar_cursor() == ' ')
7895 {
7896 del_char(FALSE);
7897 ++orig_vcols;
7898 }
7899 curwin->w_cursor.col -= ins_len;
7900 }
7901#endif
7902
7903 /* mark the buffer as changed and prepare for displaying */
7904 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7905 }
7906 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007907 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908}
7909
7910#ifdef FEAT_CINDENT
7911/*
7912 * Return TRUE if C-indenting is on.
7913 */
7914 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007915cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916{
7917 return (!p_paste && (curbuf->b_p_cin
7918# ifdef FEAT_EVAL
7919 || *curbuf->b_p_inde != NUL
7920# endif
7921 ));
7922}
7923#endif
7924
7925#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7926/*
7927 * Re-indent the current line, based on the current contents of it and the
7928 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7929 * confused what all the part that handles Control-T is doing that I'm not.
7930 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7931 */
7932
7933 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007934fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007936 int amount = get_the_indent();
7937
7938 if (amount >= 0)
7939 {
7940 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7941 if (linewhite(curwin->w_cursor.lnum))
7942 did_ai = TRUE; /* delete the indent if the line stays empty */
7943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944}
7945
7946 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007947fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948{
7949 if (p_paste)
7950 return;
7951# ifdef FEAT_LISP
7952 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7953 fixthisline(get_lisp_indent);
7954# endif
7955# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7956 else
7957# endif
7958# ifdef FEAT_CINDENT
7959 if (cindent_on())
7960 do_c_expr_indent();
7961# endif
7962}
7963
7964#endif
7965
7966#ifdef FEAT_CINDENT
7967/*
7968 * return TRUE if 'cinkeys' contains the key "keytyped",
7969 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007970 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7972 *
7973 * "keytyped" can have a few special values:
7974 * KEY_OPEN_FORW
7975 * KEY_OPEN_BACK
7976 * KEY_COMPLETE just finished completion.
7977 *
7978 * If line_is_empty is TRUE accept keys with '0' before them.
7979 */
7980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007981in_cinkeys(
7982 int keytyped,
7983 int when,
7984 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986 char_u *look;
7987 int try_match;
7988 int try_match_word;
7989 char_u *p;
7990 char_u *line;
7991 int icase;
7992 int i;
7993
Bram Moolenaar30848942009-12-24 14:46:12 +00007994 if (keytyped == NUL)
7995 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7996 return FALSE;
7997
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998#ifdef FEAT_EVAL
7999 if (*curbuf->b_p_inde != NUL)
8000 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8001 else
8002#endif
8003 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8004 while (*look)
8005 {
8006 /*
8007 * Find out if we want to try a match with this key, depending on
8008 * 'when' and a '*' or '!' before the key.
8009 */
8010 switch (when)
8011 {
8012 case '*': try_match = (*look == '*'); break;
8013 case '!': try_match = (*look == '!'); break;
8014 default: try_match = (*look != '*'); break;
8015 }
8016 if (*look == '*' || *look == '!')
8017 ++look;
8018
8019 /*
8020 * If there is a '0', only accept a match if the line is empty.
8021 * But may still match when typing last char of a word.
8022 */
8023 if (*look == '0')
8024 {
8025 try_match_word = try_match;
8026 if (!line_is_empty)
8027 try_match = FALSE;
8028 ++look;
8029 }
8030 else
8031 try_match_word = FALSE;
8032
8033 /*
8034 * does it look like a control character?
8035 */
8036 if (*look == '^'
8037#ifdef EBCDIC
8038 && (Ctrl_chr(look[1]) != 0)
8039#else
8040 && look[1] >= '?' && look[1] <= '_'
8041#endif
8042 )
8043 {
8044 if (try_match && keytyped == Ctrl_chr(look[1]))
8045 return TRUE;
8046 look += 2;
8047 }
8048 /*
8049 * 'o' means "o" command, open forward.
8050 * 'O' means "O" command, open backward.
8051 */
8052 else if (*look == 'o')
8053 {
8054 if (try_match && keytyped == KEY_OPEN_FORW)
8055 return TRUE;
8056 ++look;
8057 }
8058 else if (*look == 'O')
8059 {
8060 if (try_match && keytyped == KEY_OPEN_BACK)
8061 return TRUE;
8062 ++look;
8063 }
8064
8065 /*
8066 * 'e' means to check for "else" at start of line and just before the
8067 * cursor.
8068 */
8069 else if (*look == 'e')
8070 {
8071 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8072 {
8073 p = ml_get_curline();
8074 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8075 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8076 return TRUE;
8077 }
8078 ++look;
8079 }
8080
8081 /*
8082 * ':' only causes an indent if it is at the end of a label or case
8083 * statement, or when it was before typing the ':' (to fix
8084 * class::method for C++).
8085 */
8086 else if (*look == ':')
8087 {
8088 if (try_match && keytyped == ':')
8089 {
8090 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008091 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008093 /* Need to get the line again after cin_islabel(). */
8094 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 if (curwin->w_cursor.col > 2
8096 && p[curwin->w_cursor.col - 1] == ':'
8097 && p[curwin->w_cursor.col - 2] == ':')
8098 {
8099 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008100 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008101 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 p = ml_get_curline();
8103 p[curwin->w_cursor.col - 1] = ':';
8104 if (i)
8105 return TRUE;
8106 }
8107 }
8108 ++look;
8109 }
8110
8111
8112 /*
8113 * Is it a key in <>, maybe?
8114 */
8115 else if (*look == '<')
8116 {
8117 if (try_match)
8118 {
8119 /*
8120 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8121 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8122 * >, *, : and ! keys if they really really want to.
8123 */
8124 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8125 && keytyped == look[1])
8126 return TRUE;
8127
8128 if (keytyped == get_special_key_code(look + 1))
8129 return TRUE;
8130 }
8131 while (*look && *look != '>')
8132 look++;
8133 while (*look == '>')
8134 look++;
8135 }
8136
8137 /*
8138 * Is it a word: "=word"?
8139 */
8140 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8141 {
8142 ++look;
8143 if (*look == '~')
8144 {
8145 icase = TRUE;
8146 ++look;
8147 }
8148 else
8149 icase = FALSE;
8150 p = vim_strchr(look, ',');
8151 if (p == NULL)
8152 p = look + STRLEN(look);
8153 if ((try_match || try_match_word)
8154 && curwin->w_cursor.col >= (colnr_T)(p - look))
8155 {
8156 int match = FALSE;
8157
8158#ifdef FEAT_INS_EXPAND
8159 if (keytyped == KEY_COMPLETE)
8160 {
8161 char_u *s;
8162
8163 /* Just completed a word, check if it starts with "look".
8164 * search back for the start of a word. */
8165 line = ml_get_curline();
8166# ifdef FEAT_MBYTE
8167 if (has_mbyte)
8168 {
8169 char_u *n;
8170
8171 for (s = line + curwin->w_cursor.col; s > line; s = n)
8172 {
8173 n = mb_prevptr(line, s);
8174 if (!vim_iswordp(n))
8175 break;
8176 }
8177 }
8178 else
8179# endif
8180 for (s = line + curwin->w_cursor.col; s > line; --s)
8181 if (!vim_iswordc(s[-1]))
8182 break;
8183 if (s + (p - look) <= line + curwin->w_cursor.col
8184 && (icase
8185 ? MB_STRNICMP(s, look, p - look)
8186 : STRNCMP(s, look, p - look)) == 0)
8187 match = TRUE;
8188 }
8189 else
8190#endif
8191 /* TODO: multi-byte */
8192 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8193 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8194 {
8195 line = ml_get_cursor();
8196 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8197 || !vim_iswordc(line[-(p - look) - 1]))
8198 && (icase
8199 ? MB_STRNICMP(line - (p - look), look, p - look)
8200 : STRNCMP(line - (p - look), look, p - look))
8201 == 0)
8202 match = TRUE;
8203 }
8204 if (match && try_match_word && !try_match)
8205 {
8206 /* "0=word": Check if there are only blanks before the
8207 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008208 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 (int)(curwin->w_cursor.col - (p - look)))
8210 match = FALSE;
8211 }
8212 if (match)
8213 return TRUE;
8214 }
8215 look = p;
8216 }
8217
8218 /*
8219 * ok, it's a boring generic character.
8220 */
8221 else
8222 {
8223 if (try_match && *look == keytyped)
8224 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008225 if (*look != NUL)
8226 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 }
8228
8229 /*
8230 * Skip over ", ".
8231 */
8232 look = skip_to_option_part(look);
8233 }
8234 return FALSE;
8235}
8236#endif /* FEAT_CINDENT */
8237
8238#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8239/*
8240 * Map Hebrew keyboard when in hkmap mode.
8241 */
8242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008243hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244{
8245 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8246 {
8247 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8248 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8249 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8250 static char_u map[26] =
8251 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8252 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8253 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8254 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8255 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8256 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8257 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8258 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8259 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8260
8261 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8262 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8263 /* '-1'='sofit' */
8264 else if (c == 'x')
8265 return 'X';
8266 else if (c == 'q')
8267 return '\''; /* {geresh}={'} */
8268 else if (c == 246)
8269 return ' '; /* \"o --> ' ' for a german keyboard */
8270 else if (c == 228)
8271 return ' '; /* \"a --> ' ' -- / -- */
8272 else if (c == 252)
8273 return ' '; /* \"u --> ' ' -- / -- */
8274#ifdef EBCDIC
8275 else if (islower(c))
8276#else
8277 /* NOTE: islower() does not do the right thing for us on Linux so we
8278 * do this the same was as 5.7 and previous, so it works correctly on
8279 * all systems. Specifically, the e.g. Delete and Arrow keys are
8280 * munged and won't work if e.g. searching for Hebrew text.
8281 */
8282 else if (c >= 'a' && c <= 'z')
8283#endif
8284 return (int)(map[CharOrdLow(c)] + p_aleph);
8285 else
8286 return c;
8287 }
8288 else
8289 {
8290 switch (c)
8291 {
8292 case '`': return ';';
8293 case '/': return '.';
8294 case '\'': return ',';
8295 case 'q': return '/';
8296 case 'w': return '\'';
8297
8298 /* Hebrew letters - set offset from 'a' */
8299 case ',': c = '{'; break;
8300 case '.': c = 'v'; break;
8301 case ';': c = 't'; break;
8302 default: {
8303 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8304
8305#ifdef EBCDIC
8306 /* see note about islower() above */
8307 if (!islower(c))
8308#else
8309 if (c < 'a' || c > 'z')
8310#endif
8311 return c;
8312 c = str[CharOrdLow(c)];
8313 break;
8314 }
8315 }
8316
8317 return (int)(CharOrdLow(c) + p_aleph);
8318 }
8319}
8320#endif
8321
8322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008323ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324{
8325 int need_redraw = FALSE;
8326 int regname;
8327 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008328 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329
8330 /*
8331 * If we are going to wait for a character, show a '"'.
8332 */
8333 pc_status = PC_STATUS_UNSET;
8334 if (redrawing() && !char_avail())
8335 {
8336 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008337 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338
8339 edit_putchar('"', TRUE);
8340#ifdef FEAT_CMDL_INFO
8341 add_to_showcmd_c(Ctrl_R);
8342#endif
8343 }
8344
8345#ifdef USE_ON_FLY_SCROLL
8346 dont_scroll = TRUE; /* disallow scrolling here */
8347#endif
8348
8349 /*
8350 * Don't map the register name. This also prevents the mode message to be
8351 * deleted when ESC is hit.
8352 */
8353 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008354 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8357 {
8358 /* Get a third key for literal register insertion */
8359 literally = regname;
8360#ifdef FEAT_CMDL_INFO
8361 add_to_showcmd_c(literally);
8362#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008363 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 }
8366 --no_mapping;
8367
8368#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008369 /* Don't call u_sync() while typing the expression or giving an error
8370 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 ++no_u_sync;
8372 if (regname == '=')
8373 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008374# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008376# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008377 /* Sync undo when evaluating the expression calls setline() or
8378 * append(), so that it can be undone separately. */
8379 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008380
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008382# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 /* Restore the Input Method. */
8384 if (im_on)
8385 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008386# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008388 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8389 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008390 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 else
8394 {
8395#endif
8396 if (literally == Ctrl_O || literally == Ctrl_P)
8397 {
8398 /* Append the command to the redo buffer. */
8399 AppendCharToRedobuff(Ctrl_R);
8400 AppendCharToRedobuff(literally);
8401 AppendCharToRedobuff(regname);
8402
8403 do_put(regname, BACKWARD, 1L,
8404 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8405 }
8406 else if (insert_reg(regname, literally) == FAIL)
8407 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008408 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 need_redraw = TRUE; /* remove the '"' */
8410 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008411 else if (stop_insert_mode)
8412 /* When the '=' register was used and a function was invoked that
8413 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8414 * insert anything, need to remove the '"' */
8415 need_redraw = TRUE;
8416
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417#ifdef FEAT_EVAL
8418 }
8419 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008420 if (u_sync_once == 1)
8421 ins_need_undo = TRUE;
8422 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423#endif
8424#ifdef FEAT_CMDL_INFO
8425 clear_showcmd();
8426#endif
8427
8428 /* If the inserted register is empty, we need to remove the '"' */
8429 if (need_redraw || stuff_empty())
8430 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008431
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008432 /* Disallow starting Visual mode here, would get a weird mode. */
8433 if (!vis_active && VIsual_active)
8434 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435}
8436
8437/*
8438 * CTRL-G commands in Insert mode.
8439 */
8440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008441ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442{
8443 int c;
8444
8445#ifdef FEAT_INS_EXPAND
8446 /* Right after CTRL-X the cursor will be after the ruler. */
8447 setcursor();
8448#endif
8449
8450 /*
8451 * Don't map the second key. This also prevents the mode message to be
8452 * deleted when ESC is hit.
8453 */
8454 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008455 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 --no_mapping;
8457 switch (c)
8458 {
8459 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8460 case K_UP:
8461 case Ctrl_K:
8462 case 'k': ins_up(TRUE);
8463 break;
8464
8465 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8466 case K_DOWN:
8467 case Ctrl_J:
8468 case 'j': ins_down(TRUE);
8469 break;
8470
8471 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008472 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008474
8475 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008476 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008477 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008478 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 break;
8480
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008481 /* CTRL-G U: do not break undo with the next char */
8482 case 'U':
8483 /* Allow one left/right cursor movement with the next char,
8484 * without breaking undo. */
8485 dont_sync_undo = MAYBE;
8486 break;
8487
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008489 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 }
8491}
8492
8493/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008494 * CTRL-^ in Insert mode.
8495 */
8496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008497ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008498{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008499 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008500 {
8501 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8502 if (State & LANGMAP)
8503 {
8504 curbuf->b_p_iminsert = B_IMODE_NONE;
8505 State &= ~LANGMAP;
8506 }
8507 else
8508 {
8509 curbuf->b_p_iminsert = B_IMODE_LMAP;
8510 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008511#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008512 im_set_active(FALSE);
8513#endif
8514 }
8515 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008516#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008517 else
8518 {
8519 /* There are no ":lmap" mappings, toggle IM */
8520 if (im_get_status())
8521 {
8522 curbuf->b_p_iminsert = B_IMODE_NONE;
8523 im_set_active(FALSE);
8524 }
8525 else
8526 {
8527 curbuf->b_p_iminsert = B_IMODE_IM;
8528 State &= ~LANGMAP;
8529 im_set_active(TRUE);
8530 }
8531 }
8532#endif
8533 set_iminsert_global();
8534 showmode();
8535#ifdef FEAT_GUI
8536 /* may show different cursor shape or color */
8537 if (gui.in_use)
8538 gui_update_cursor(TRUE, FALSE);
8539#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008540#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008541 /* Show/unshow value of 'keymap' in status lines. */
8542 status_redraw_curbuf();
8543#endif
8544}
8545
8546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 * Handle ESC in insert mode.
8548 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8549 * insert.
8550 */
8551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008552ins_esc(
8553 long *count,
8554 int cmdchar,
8555 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556{
8557 int temp;
8558 static int disabled_redraw = FALSE;
8559
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008560#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008561 check_spell_redraw();
8562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563#if defined(FEAT_HANGULIN)
8564# if defined(ESC_CHG_TO_ENG_MODE)
8565 hangul_input_state_set(0);
8566# endif
8567 if (composing_hangul)
8568 {
8569 push_raw_key(composing_hangul_buffer, 2);
8570 composing_hangul = 0;
8571 }
8572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573
8574 temp = curwin->w_cursor.col;
8575 if (disabled_redraw)
8576 {
8577 --RedrawingDisabled;
8578 disabled_redraw = FALSE;
8579 }
8580 if (!arrow_used)
8581 {
8582 /*
8583 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008584 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8585 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 */
8587 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008588 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
8590 /*
8591 * Repeating insert may take a long time. Check for
8592 * interrupt now and then.
8593 */
8594 if (*count > 0)
8595 {
8596 line_breakcheck();
8597 if (got_int)
8598 *count = 0;
8599 }
8600
8601 if (--*count > 0) /* repeat what was typed */
8602 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008603 /* Vi repeats the insert without replacing characters. */
8604 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8605 State &= ~REPLACE_FLAG;
8606
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 (void)start_redo_ins();
8608 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008609 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 ++RedrawingDisabled;
8611 disabled_redraw = TRUE;
8612 return FALSE; /* repeat the insert */
8613 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008614 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 undisplay_dollar();
8616 }
8617
8618 /* When an autoindent was removed, curswant stays after the
8619 * indent */
8620 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8621 curwin->w_set_curswant = TRUE;
8622
8623 /* Remember the last Insert position in the '^ mark. */
8624 if (!cmdmod.keepjumps)
8625 curbuf->b_last_insert = curwin->w_cursor;
8626
8627 /*
8628 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008629 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008631 if (!nomove
8632 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633#ifdef FEAT_VIRTUALEDIT
8634 || curwin->w_cursor.coladd > 0
8635#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008636 )
8637 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008638 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639#ifdef FEAT_RIGHTLEFT
8640 && !revins_on
8641#endif
8642 )
8643 {
8644#ifdef FEAT_VIRTUALEDIT
8645 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8646 {
8647 oneleft();
8648 if (restart_edit != NUL)
8649 ++curwin->w_cursor.coladd;
8650 }
8651 else
8652#endif
8653 {
8654 --curwin->w_cursor.col;
8655#ifdef FEAT_MBYTE
8656 /* Correct cursor for multi-byte character. */
8657 if (has_mbyte)
8658 mb_adjust_cursor();
8659#endif
8660 }
8661 }
8662
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008663#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 /* Disable IM to allow typing English directly for Normal mode commands.
8665 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8666 * well). */
8667 if (!(State & LANGMAP))
8668 im_save_status(&curbuf->b_p_iminsert);
8669 im_set_active(FALSE);
8670#endif
8671
8672 State = NORMAL;
8673 /* need to position cursor again (e.g. when on a TAB ) */
8674 changed_cline_bef_curs();
8675
8676#ifdef FEAT_MOUSE
8677 setmouse();
8678#endif
8679#ifdef CURSOR_SHAPE
8680 ui_cursor_shape(); /* may show different cursor shape */
8681#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008682 if (!p_ek)
8683 /* Re-enable bracketed paste mode. */
8684 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685
8686 /*
8687 * When recording or for CTRL-O, need to display the new mode.
8688 * Otherwise remove the mode message.
8689 */
8690 if (Recording || restart_edit != NUL)
8691 showmode();
8692 else if (p_smd)
8693 MSG("");
8694
8695 return TRUE; /* exit Insert mode */
8696}
8697
8698#ifdef FEAT_RIGHTLEFT
8699/*
8700 * Toggle language: hkmap and revins_on.
8701 * Move to end of reverse inserted text.
8702 */
8703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008704ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705{
8706 if (revins_on && revins_chars && revins_scol >= 0)
8707 {
8708 while (gchar_cursor() != NUL && revins_chars--)
8709 ++curwin->w_cursor.col;
8710 }
8711 p_ri = !p_ri;
8712 revins_on = (State == INSERT && p_ri);
8713 if (revins_on)
8714 {
8715 revins_scol = curwin->w_cursor.col;
8716 revins_legal++;
8717 revins_chars = 0;
8718 undisplay_dollar();
8719 }
8720 else
8721 revins_scol = -1;
8722#ifdef FEAT_FKMAP
8723 if (p_altkeymap)
8724 {
8725 /*
8726 * to be consistent also for redo command, using '.'
8727 * set arrow_used to true and stop it - causing to redo
8728 * characters entered in one mode (normal/reverse insert).
8729 */
8730 arrow_used = TRUE;
8731 (void)stop_arrow();
8732 p_fkmap = curwin->w_p_rl ^ p_ri;
8733 if (p_fkmap && p_ri)
8734 State = INSERT;
8735 }
8736 else
8737#endif
8738 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8739 showmode();
8740}
8741#endif
8742
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743/*
8744 * If 'keymodel' contains "startsel", may start selection.
8745 * Returns TRUE when a CTRL-O and other keys stuffed.
8746 */
8747 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008748ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749{
8750 if (km_startsel)
8751 switch (c)
8752 {
8753 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 case K_PAGEUP:
8756 case K_KPAGEUP:
8757 case K_PAGEDOWN:
8758 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008759# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 case K_LEFT:
8761 case K_RIGHT:
8762 case K_UP:
8763 case K_DOWN:
8764 case K_END:
8765 case K_HOME:
8766# endif
8767 if (!(mod_mask & MOD_MASK_SHIFT))
8768 break;
8769 /* FALLTHROUGH */
8770 case K_S_LEFT:
8771 case K_S_RIGHT:
8772 case K_S_UP:
8773 case K_S_DOWN:
8774 case K_S_END:
8775 case K_S_HOME:
8776 /* Start selection right away, the cursor can move with
8777 * CTRL-O when beyond the end of the line. */
8778 start_selection();
8779
8780 /* Execute the key in (insert) Select mode. */
8781 stuffcharReadbuff(Ctrl_O);
8782 if (mod_mask)
8783 {
8784 char_u buf[4];
8785
8786 buf[0] = K_SPECIAL;
8787 buf[1] = KS_MODIFIER;
8788 buf[2] = mod_mask;
8789 buf[3] = NUL;
8790 stuffReadbuff(buf);
8791 }
8792 stuffcharReadbuff(c);
8793 return TRUE;
8794 }
8795 return FALSE;
8796}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797
8798/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008799 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008800 */
8801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008802ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008803{
8804#ifdef FEAT_FKMAP
8805 if (p_fkmap && p_ri)
8806 {
8807 beep_flush();
8808 EMSG(farsi_text_3); /* encoded in Farsi */
8809 return;
8810 }
8811#endif
8812
Bram Moolenaar1e015462005-09-25 22:16:38 +00008813# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008814 set_vim_var_string(VV_INSERTMODE,
8815 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008816# ifdef FEAT_VREPLACE
8817 replaceState == VREPLACE ? "v" :
8818# endif
8819 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008820# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008821 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008822 if (State & REPLACE_FLAG)
8823 State = INSERT | (State & LANGMAP);
8824 else
8825 State = replaceState | (State & LANGMAP);
8826 AppendCharToRedobuff(K_INS);
8827 showmode();
8828#ifdef CURSOR_SHAPE
8829 ui_cursor_shape(); /* may show different cursor shape */
8830#endif
8831}
8832
8833/*
8834 * Pressed CTRL-O in Insert mode.
8835 */
8836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008837ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008838{
8839#ifdef FEAT_VREPLACE
8840 if (State & VREPLACE_FLAG)
8841 restart_edit = 'V';
8842 else
8843#endif
8844 if (State & REPLACE_FLAG)
8845 restart_edit = 'R';
8846 else
8847 restart_edit = 'I';
8848#ifdef FEAT_VIRTUALEDIT
8849 if (virtual_active())
8850 ins_at_eol = FALSE; /* cursor always keeps its column */
8851 else
8852#endif
8853 ins_at_eol = (gchar_cursor() == NUL);
8854}
8855
8856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 * If the cursor is on an indent, ^T/^D insert/delete one
8858 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008859 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 * with vi. But vi only supports ^T and ^D after an
8861 * autoindent, we support it everywhere.
8862 */
8863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008864ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
8866 if (stop_arrow() == FAIL)
8867 return;
8868 AppendCharToRedobuff(c);
8869
8870 /*
8871 * 0^D and ^^D: remove all indent.
8872 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008873 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8874 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 {
8876 --curwin->w_cursor.col;
8877 (void)del_char(FALSE); /* delete the '^' or '0' */
8878 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8879 if (State & REPLACE_FLAG)
8880 replace_pop_ins();
8881 if (lastc == '^')
8882 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008883 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 }
8885 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008886 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887
8888 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8889 did_ai = FALSE;
8890#ifdef FEAT_SMARTINDENT
8891 did_si = FALSE;
8892 can_si = FALSE;
8893 can_si_back = FALSE;
8894#endif
8895#ifdef FEAT_CINDENT
8896 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8897#endif
8898}
8899
8900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008901ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
8903 int temp;
8904
8905 if (stop_arrow() == FAIL)
8906 return;
8907 if (gchar_cursor() == NUL) /* delete newline */
8908 {
8909 temp = curwin->w_cursor.col;
8910 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008911 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008912 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 else
8914 curwin->w_cursor.col = temp;
8915 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008916 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8917 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 did_ai = FALSE;
8919#ifdef FEAT_SMARTINDENT
8920 did_si = FALSE;
8921 can_si = FALSE;
8922 can_si_back = FALSE;
8923#endif
8924 AppendCharToRedobuff(K_DEL);
8925}
8926
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008927static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008928
8929/*
8930 * Delete one character for ins_bs().
8931 */
8932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008933ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008934{
8935 dec_cursor();
8936 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8937 if (State & REPLACE_FLAG)
8938 {
8939 /* Don't delete characters before the insert point when in
8940 * Replace mode */
8941 if (curwin->w_cursor.lnum != Insstart.lnum
8942 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008943 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008944 }
8945 else
8946 (void)del_char(FALSE);
8947}
8948
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949/*
8950 * Handle Backspace, delete-word and delete-line in Insert mode.
8951 * Return TRUE when backspace was actually used.
8952 */
8953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008954ins_bs(
8955 int c,
8956 int mode,
8957 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
8959 linenr_T lnum;
8960 int cc;
8961 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008962 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 colnr_T mincol;
8964 int did_backspace = FALSE;
8965 int in_indent;
8966 int oldState;
8967#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008968 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969#endif
8970
8971 /*
8972 * can't delete anything in an empty file
8973 * can't backup past first character in buffer
8974 * can't backup past starting point unless 'backspace' > 1
8975 * can backup to a previous line if 'backspace' == 0
8976 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008977 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 || (
8979#ifdef FEAT_RIGHTLEFT
8980 !revins_on &&
8981#endif
8982 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8983 || (!can_bs(BS_START)
8984 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008985 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8986 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8988 && curwin->w_cursor.col <= ai_col)
8989 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8990 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008991 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 return FALSE;
8993 }
8994
8995 if (stop_arrow() == FAIL)
8996 return FALSE;
8997 in_indent = inindent(0);
8998#ifdef FEAT_CINDENT
8999 if (in_indent)
9000 can_cindent = FALSE;
9001#endif
9002#ifdef FEAT_COMMENTS
9003 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9004#endif
9005#ifdef FEAT_RIGHTLEFT
9006 if (revins_on) /* put cursor after last inserted char */
9007 inc_cursor();
9008#endif
9009
9010#ifdef FEAT_VIRTUALEDIT
9011 /* Virtualedit:
9012 * BACKSPACE_CHAR eats a virtual space
9013 * BACKSPACE_WORD eats all coladd
9014 * BACKSPACE_LINE eats all coladd and keeps going
9015 */
9016 if (curwin->w_cursor.coladd > 0)
9017 {
9018 if (mode == BACKSPACE_CHAR)
9019 {
9020 --curwin->w_cursor.coladd;
9021 return TRUE;
9022 }
9023 if (mode == BACKSPACE_WORD)
9024 {
9025 curwin->w_cursor.coladd = 0;
9026 return TRUE;
9027 }
9028 curwin->w_cursor.coladd = 0;
9029 }
9030#endif
9031
9032 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009033 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 */
9035 if (curwin->w_cursor.col == 0)
9036 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009037 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009038 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039#ifdef FEAT_RIGHTLEFT
9040 || revins_on
9041#endif
9042 )
9043 {
9044 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9045 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9046 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009047 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009048 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 }
9050 /*
9051 * In replace mode:
9052 * cc < 0: NL was inserted, delete it
9053 * cc >= 0: NL was replaced, put original characters back
9054 */
9055 cc = -1;
9056 if (State & REPLACE_FLAG)
9057 cc = replace_pop(); /* returns -1 if NL was inserted */
9058 /*
9059 * In replace mode, in the line we started replacing, we only move the
9060 * cursor.
9061 */
9062 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9063 {
9064 dec_cursor();
9065 }
9066 else
9067 {
9068#ifdef FEAT_VREPLACE
9069 if (!(State & VREPLACE_FLAG)
9070 || curwin->w_cursor.lnum > orig_line_count)
9071#endif
9072 {
9073 temp = gchar_cursor(); /* remember current char */
9074 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009075
9076 /* When "aw" is in 'formatoptions' we must delete the space at
9077 * the end of the line, otherwise the line will be broken
9078 * again when auto-formatting. */
9079 if (has_format_option(FO_AUTO)
9080 && has_format_option(FO_WHITE_PAR))
9081 {
9082 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9083 TRUE);
9084 int len;
9085
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009086 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009087 if (len > 0 && ptr[len - 1] == ' ')
9088 ptr[len - 1] = NUL;
9089 }
9090
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009091 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 if (temp == NUL && gchar_cursor() != NUL)
9093 inc_cursor();
9094 }
9095#ifdef FEAT_VREPLACE
9096 else
9097 dec_cursor();
9098#endif
9099
9100 /*
9101 * In REPLACE mode we have to put back the text that was replaced
9102 * by the NL. On the replace stack is first a NUL-terminated
9103 * sequence of characters that were deleted and then the
9104 * characters that NL replaced.
9105 */
9106 if (State & REPLACE_FLAG)
9107 {
9108 /*
9109 * Do the next ins_char() in NORMAL state, to
9110 * prevent ins_char() from replacing characters and
9111 * avoiding showmatch().
9112 */
9113 oldState = State;
9114 State = NORMAL;
9115 /*
9116 * restore characters (blanks) deleted after cursor
9117 */
9118 while (cc > 0)
9119 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009120 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121#ifdef FEAT_MBYTE
9122 mb_replace_pop_ins(cc);
9123#else
9124 ins_char(cc);
9125#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009126 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 cc = replace_pop();
9128 }
9129 /* restore the characters that NL replaced */
9130 replace_pop_ins();
9131 State = oldState;
9132 }
9133 }
9134 did_ai = FALSE;
9135 }
9136 else
9137 {
9138 /*
9139 * Delete character(s) before the cursor.
9140 */
9141#ifdef FEAT_RIGHTLEFT
9142 if (revins_on) /* put cursor on last inserted char */
9143 dec_cursor();
9144#endif
9145 mincol = 0;
9146 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009147 if (mode == BACKSPACE_LINE
9148 && (curbuf->b_p_ai
9149#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009150 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009151#endif
9152 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153#ifdef FEAT_RIGHTLEFT
9154 && !revins_on
9155#endif
9156 )
9157 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009158 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009160 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009162 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 }
9164
9165 /*
9166 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9167 */
9168 if ( mode == BACKSPACE_CHAR
9169 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009170 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009171 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 && (*(ml_get_cursor() - 1) == TAB
9173 || (*(ml_get_cursor() - 1) == ' '
9174 && (!*inserted_space_p
9175 || arrow_used))))))
9176 {
9177 int ts;
9178 colnr_T vcol;
9179 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009180 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181
9182 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009183 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009184 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009186 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 /* Compute the virtual column where we want to be. Since
9188 * 'showbreak' may get in the way, need to get the last column of
9189 * the previous character. */
9190 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009191 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 dec_cursor();
9193 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9194 inc_cursor();
9195 want_vcol = (want_vcol / ts) * ts;
9196
9197 /* delete characters until we are at or before want_vcol */
9198 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009199 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009200 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
9202 /* insert extra spaces until we are at want_vcol */
9203 while (vcol < want_vcol)
9204 {
9205 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009206 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9207 && curwin->w_cursor.col < Insstart_orig.col)
9208 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209
9210#ifdef FEAT_VREPLACE
9211 if (State & VREPLACE_FLAG)
9212 ins_char(' ');
9213 else
9214#endif
9215 {
9216 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009217 if ((State & REPLACE_FLAG))
9218 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 }
9220 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9221 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009222
9223 /* If we are now back where we started delete one character. Can
9224 * happen when using 'sts' and 'linebreak'. */
9225 if (vcol >= start_vcol)
9226 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 }
9228
9229 /*
9230 * Delete upto starting point, start of line or previous word.
9231 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009232 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009234#ifdef FEAT_MBYTE
9235 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009237 if (has_mbyte)
9238 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009240 do
9241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009243 if (!revins_on) /* put cursor on char to be deleted */
9244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009246
9247 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009249 /* look multi-byte character class */
9250 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009252 prev_cclass = cclass;
9253 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009256
9257 /* start of word? */
9258 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9259 {
9260 mode = BACKSPACE_WORD_NOT_SPACE;
9261 temp = vim_iswordc(cc);
9262 }
9263 /* end of word? */
9264 else if (mode == BACKSPACE_WORD_NOT_SPACE
9265 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9266#ifdef FEAT_MBYTE
9267 || prev_cclass != cclass
9268#endif
9269 ))
9270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009272 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009274 inc_cursor();
9275#ifdef FEAT_RIGHTLEFT
9276 else if (State & REPLACE_FLAG)
9277 dec_cursor();
9278#endif
9279 break;
9280 }
9281 if (State & REPLACE_FLAG)
9282 replace_do_bs(-1);
9283 else
9284 {
9285#ifdef FEAT_MBYTE
9286 if (enc_utf8 && p_deco)
9287 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9288#endif
9289 (void)del_char(FALSE);
9290#ifdef FEAT_MBYTE
9291 /*
9292 * If there are combining characters and 'delcombine' is set
9293 * move the cursor back. Don't back up before the base
9294 * character.
9295 */
9296 if (enc_utf8 && p_deco && cpc[0] != NUL)
9297 inc_cursor();
9298#endif
9299#ifdef FEAT_RIGHTLEFT
9300 if (revins_chars)
9301 {
9302 revins_chars--;
9303 revins_legal++;
9304 }
9305 if (revins_on && gchar_cursor() == NUL)
9306 break;
9307#endif
9308 }
9309 /* Just a single backspace?: */
9310 if (mode == BACKSPACE_CHAR)
9311 break;
9312 } while (
9313#ifdef FEAT_RIGHTLEFT
9314 revins_on ||
9315#endif
9316 (curwin->w_cursor.col > mincol
9317 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9318 || curwin->w_cursor.col != Insstart_orig.col)));
9319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 did_backspace = TRUE;
9321 }
9322#ifdef FEAT_SMARTINDENT
9323 did_si = FALSE;
9324 can_si = FALSE;
9325 can_si_back = FALSE;
9326#endif
9327 if (curwin->w_cursor.col <= 1)
9328 did_ai = FALSE;
9329 /*
9330 * It's a little strange to put backspaces into the redo
9331 * buffer, but it makes auto-indent a lot easier to deal
9332 * with.
9333 */
9334 AppendCharToRedobuff(c);
9335
9336 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009337 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9338 && curwin->w_cursor.col < Insstart_orig.col)
9339 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340
9341 /* vi behaviour: the cursor moves backward but the character that
9342 * was there remains visible
9343 * Vim behaviour: the cursor moves backward and the character that
9344 * was there is erased from the screen.
9345 * We can emulate the vi behaviour by pretending there is a dollar
9346 * displayed even when there isn't.
9347 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009348 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 dollar_vcol = curwin->w_virtcol;
9350
Bram Moolenaarce3be472008-01-14 19:12:28 +00009351#ifdef FEAT_FOLDING
9352 /* When deleting a char the cursor line must never be in a closed fold.
9353 * E.g., when 'foldmethod' is indent and deleting the first non-white
9354 * char before a Tab. */
9355 if (did_backspace)
9356 foldOpenCursor();
9357#endif
9358
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 return did_backspace;
9360}
9361
9362#ifdef FEAT_MOUSE
9363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009364ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
9366 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009367 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368
9369# ifdef FEAT_GUI
9370 /* When GUI is active, also move/paste when 'mouse' is empty */
9371 if (!gui.in_use)
9372# endif
9373 if (!mouse_has(MOUSE_INSERT))
9374 return;
9375
9376 undisplay_dollar();
9377 tpos = curwin->w_cursor;
9378 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9379 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009380 win_T *new_curwin = curwin;
9381
9382 if (curwin != old_curwin && win_valid(old_curwin))
9383 {
9384 /* Mouse took us to another window. We need to go back to the
9385 * previous one to stop insert there properly. */
9386 curwin = old_curwin;
9387 curbuf = curwin->w_buffer;
9388 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009389 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009390 if (curwin != new_curwin && win_valid(new_curwin))
9391 {
9392 curwin = new_curwin;
9393 curbuf = curwin->w_buffer;
9394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395# ifdef FEAT_CINDENT
9396 can_cindent = TRUE;
9397# endif
9398 }
9399
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 /* redraw status lines (in case another window became active) */
9401 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402}
9403
9404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009405ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406{
9407 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009408 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009409# ifdef FEAT_INS_EXPAND
9410 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411# endif
9412
9413 tpos = curwin->w_cursor;
9414
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009415 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 {
9417 int row, col;
9418
9419 row = mouse_row;
9420 col = mouse_col;
9421
9422 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009423 wp = mouse_find_win(&row, &col);
9424 if (wp == NULL)
9425 return;
9426 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 curbuf = curwin->w_buffer;
9428 }
9429 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 undisplay_dollar();
9431
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009432# ifdef FEAT_INS_EXPAND
9433 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009434 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009435# endif
9436 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009437 if (dir == MSCR_DOWN || dir == MSCR_UP)
9438 {
9439 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9440 scroll_redraw(dir,
9441 (long)(curwin->w_botline - curwin->w_topline));
9442 else
9443 scroll_redraw(dir, 3L);
9444 }
9445#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009446 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009447 {
9448 int val, step = 6;
9449
9450 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009451 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009452 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9453 if (val < 0)
9454 val = 0;
9455 gui_do_horiz_scroll(val, TRUE);
9456 }
9457#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009458# ifdef FEAT_INS_EXPAND
9459 did_scroll = TRUE;
9460# endif
9461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 curwin->w_redr_status = TRUE;
9464
9465 curwin = old_curwin;
9466 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009468# ifdef FEAT_INS_EXPAND
9469 /* The popup menu may overlay the window, need to redraw it.
9470 * TODO: Would be more efficient to only redraw the windows that are
9471 * overlapped by the popup menu. */
9472 if (pum_visible() && did_scroll)
9473 {
9474 redraw_all_later(NOT_VALID);
9475 ins_compl_show_pum();
9476 }
9477# endif
9478
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009479 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 {
9481 start_arrow(&tpos);
9482# ifdef FEAT_CINDENT
9483 can_cindent = TRUE;
9484# endif
9485 }
9486}
9487#endif
9488
Bram Moolenaarec2da362017-01-21 20:04:22 +01009489/*
9490 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9491 * P_PE literally.
9492 * When "drop" is TRUE then consume the text and drop it.
9493 */
9494 int
9495bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9496{
9497 int c;
9498 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9499 int idx = 0;
9500 char_u *end = find_termcode((char_u *)"PE");
9501 int ret_char = -1;
9502 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009503 int save_paste = p_paste;
9504 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009505
9506 /* If the end code is too long we can't detect it, read everything. */
9507 if (STRLEN(end) >= NUMBUFLEN)
9508 end = NULL;
9509 ++no_mapping;
9510 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009511 p_paste = TRUE;
9512 curbuf->b_p_ai = FALSE;
9513
Bram Moolenaarec2da362017-01-21 20:04:22 +01009514 for (;;)
9515 {
9516 /* When the end is not defined read everything. */
9517 if (end == NULL && vpeekc() == NUL)
9518 break;
9519 c = plain_vgetc();
9520#ifdef FEAT_MBYTE
9521 if (has_mbyte)
9522 idx += (*mb_char2bytes)(c, buf + idx);
9523 else
9524#endif
9525 buf[idx++] = c;
9526 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009527 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009528 {
9529 if (end[idx] == NUL)
9530 break; /* Found the end of paste code. */
9531 continue;
9532 }
9533 if (!drop)
9534 {
9535 switch (mode)
9536 {
9537 case PASTE_CMDLINE:
9538 put_on_cmdline(buf, idx, TRUE);
9539 break;
9540
9541 case PASTE_EX:
9542 if (gap != NULL && ga_grow(gap, idx) == OK)
9543 {
9544 mch_memmove((char *)gap->ga_data + gap->ga_len,
9545 buf, (size_t)idx);
9546 gap->ga_len += idx;
9547 }
9548 break;
9549
9550 case PASTE_INSERT:
9551 if (stop_arrow() == OK)
9552 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009553 c = buf[0];
9554 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9555 ins_eol(c);
9556 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009557 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009558 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009559 AppendToRedobuffLit(buf, idx);
9560 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009561 }
9562 break;
9563
9564 case PASTE_ONE_CHAR:
9565 if (ret_char == -1)
9566 {
9567#ifdef FEAT_MBYTE
9568 if (has_mbyte)
9569 ret_char = (*mb_ptr2char)(buf);
9570 else
9571#endif
9572 ret_char = buf[0];
9573 }
9574 break;
9575 }
9576 }
9577 idx = 0;
9578 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009579
Bram Moolenaarec2da362017-01-21 20:04:22 +01009580 --no_mapping;
9581 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009582 p_paste = save_paste;
9583 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009584
9585 return ret_char;
9586}
9587
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009588#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009590ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009591{
9592 /* We will be leaving the current window, unless closing another tab. */
9593 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9594 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9595 {
9596 undisplay_dollar();
9597 start_arrow(&curwin->w_cursor);
9598# ifdef FEAT_CINDENT
9599 can_cindent = TRUE;
9600# endif
9601 }
9602
9603 if (c == K_TABLINE)
9604 goto_tabpage(current_tab);
9605 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009606 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009607 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009608 redraw_statuslines(); /* will redraw the tabline when needed */
9609 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009610}
9611#endif
9612
9613#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009615ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616{
9617 pos_T tpos;
9618
9619 undisplay_dollar();
9620 tpos = curwin->w_cursor;
9621 if (gui_do_scroll())
9622 {
9623 start_arrow(&tpos);
9624# ifdef FEAT_CINDENT
9625 can_cindent = TRUE;
9626# endif
9627 }
9628}
9629
9630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009631ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
9633 pos_T tpos;
9634
9635 undisplay_dollar();
9636 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009637 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 {
9639 start_arrow(&tpos);
9640# ifdef FEAT_CINDENT
9641 can_cindent = TRUE;
9642# endif
9643 }
9644}
9645#endif
9646
9647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009648ins_left(
9649 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650{
9651 pos_T tpos;
9652
9653#ifdef FEAT_FOLDING
9654 if ((fdo_flags & FDO_HOR) && KeyTyped)
9655 foldOpenCursor();
9656#endif
9657 undisplay_dollar();
9658 tpos = curwin->w_cursor;
9659 if (oneleft() == OK)
9660 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009661#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9662 /* Only call start_arrow() when not busy with preediting, it will
9663 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009664 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009665#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009666 {
9667 start_arrow_with_change(&tpos, end_change);
9668 if (!end_change)
9669 AppendCharToRedobuff(K_LEFT);
9670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671#ifdef FEAT_RIGHTLEFT
9672 /* If exit reversed string, position is fixed */
9673 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9674 revins_legal++;
9675 revins_chars++;
9676#endif
9677 }
9678
9679 /*
9680 * if 'whichwrap' set for cursor in insert mode may go to
9681 * previous line
9682 */
9683 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9684 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009685 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 start_arrow(&tpos);
9687 --(curwin->w_cursor.lnum);
9688 coladvance((colnr_T)MAXCOL);
9689 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9690 }
9691 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009692 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009693 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694}
9695
9696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009697ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
9699 pos_T tpos;
9700
9701#ifdef FEAT_FOLDING
9702 if ((fdo_flags & FDO_HOR) && KeyTyped)
9703 foldOpenCursor();
9704#endif
9705 undisplay_dollar();
9706 tpos = curwin->w_cursor;
9707 if (c == K_C_HOME)
9708 curwin->w_cursor.lnum = 1;
9709 curwin->w_cursor.col = 0;
9710#ifdef FEAT_VIRTUALEDIT
9711 curwin->w_cursor.coladd = 0;
9712#endif
9713 curwin->w_curswant = 0;
9714 start_arrow(&tpos);
9715}
9716
9717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009718ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719{
9720 pos_T tpos;
9721
9722#ifdef FEAT_FOLDING
9723 if ((fdo_flags & FDO_HOR) && KeyTyped)
9724 foldOpenCursor();
9725#endif
9726 undisplay_dollar();
9727 tpos = curwin->w_cursor;
9728 if (c == K_C_END)
9729 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9730 coladvance((colnr_T)MAXCOL);
9731 curwin->w_curswant = MAXCOL;
9732
9733 start_arrow(&tpos);
9734}
9735
9736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009737ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738{
9739#ifdef FEAT_FOLDING
9740 if ((fdo_flags & FDO_HOR) && KeyTyped)
9741 foldOpenCursor();
9742#endif
9743 undisplay_dollar();
9744 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9745 {
9746 start_arrow(&curwin->w_cursor);
9747 (void)bck_word(1L, FALSE, FALSE);
9748 curwin->w_set_curswant = TRUE;
9749 }
9750 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009751 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752}
9753
9754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009755ins_right(
9756 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757{
9758#ifdef FEAT_FOLDING
9759 if ((fdo_flags & FDO_HOR) && KeyTyped)
9760 foldOpenCursor();
9761#endif
9762 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009763 if (gchar_cursor() != NUL
9764#ifdef FEAT_VIRTUALEDIT
9765 || virtual_active()
9766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 )
9768 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009769 start_arrow_with_change(&curwin->w_cursor, end_change);
9770 if (!end_change)
9771 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 curwin->w_set_curswant = TRUE;
9773#ifdef FEAT_VIRTUALEDIT
9774 if (virtual_active())
9775 oneright();
9776 else
9777#endif
9778 {
9779#ifdef FEAT_MBYTE
9780 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009781 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 else
9783#endif
9784 ++curwin->w_cursor.col;
9785 }
9786
9787#ifdef FEAT_RIGHTLEFT
9788 revins_legal++;
9789 if (revins_chars)
9790 revins_chars--;
9791#endif
9792 }
9793 /* if 'whichwrap' set for cursor in insert mode, may move the
9794 * cursor to the next line */
9795 else if (vim_strchr(p_ww, ']') != NULL
9796 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9797 {
9798 start_arrow(&curwin->w_cursor);
9799 curwin->w_set_curswant = TRUE;
9800 ++curwin->w_cursor.lnum;
9801 curwin->w_cursor.col = 0;
9802 }
9803 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009804 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009805 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806}
9807
9808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009809ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810{
9811#ifdef FEAT_FOLDING
9812 if ((fdo_flags & FDO_HOR) && KeyTyped)
9813 foldOpenCursor();
9814#endif
9815 undisplay_dollar();
9816 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9817 || gchar_cursor() != NUL)
9818 {
9819 start_arrow(&curwin->w_cursor);
9820 (void)fwd_word(1L, FALSE, 0);
9821 curwin->w_set_curswant = TRUE;
9822 }
9823 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009824 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825}
9826
9827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009828ins_up(
9829 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830{
9831 pos_T tpos;
9832 linenr_T old_topline = curwin->w_topline;
9833#ifdef FEAT_DIFF
9834 int old_topfill = curwin->w_topfill;
9835#endif
9836
9837 undisplay_dollar();
9838 tpos = curwin->w_cursor;
9839 if (cursor_up(1L, TRUE) == OK)
9840 {
9841 if (startcol)
9842 coladvance(getvcol_nolist(&Insstart));
9843 if (old_topline != curwin->w_topline
9844#ifdef FEAT_DIFF
9845 || old_topfill != curwin->w_topfill
9846#endif
9847 )
9848 redraw_later(VALID);
9849 start_arrow(&tpos);
9850#ifdef FEAT_CINDENT
9851 can_cindent = TRUE;
9852#endif
9853 }
9854 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009855 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856}
9857
9858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009859ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860{
9861 pos_T tpos;
9862
9863 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009864
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009865 if (mod_mask & MOD_MASK_CTRL)
9866 {
9867 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009868 if (first_tabpage->tp_next != NULL)
9869 {
9870 start_arrow(&curwin->w_cursor);
9871 goto_tabpage(-1);
9872 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009873 return;
9874 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009875
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 tpos = curwin->w_cursor;
9877 if (onepage(BACKWARD, 1L) == OK)
9878 {
9879 start_arrow(&tpos);
9880#ifdef FEAT_CINDENT
9881 can_cindent = TRUE;
9882#endif
9883 }
9884 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009885 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886}
9887
9888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009889ins_down(
9890 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891{
9892 pos_T tpos;
9893 linenr_T old_topline = curwin->w_topline;
9894#ifdef FEAT_DIFF
9895 int old_topfill = curwin->w_topfill;
9896#endif
9897
9898 undisplay_dollar();
9899 tpos = curwin->w_cursor;
9900 if (cursor_down(1L, TRUE) == OK)
9901 {
9902 if (startcol)
9903 coladvance(getvcol_nolist(&Insstart));
9904 if (old_topline != curwin->w_topline
9905#ifdef FEAT_DIFF
9906 || old_topfill != curwin->w_topfill
9907#endif
9908 )
9909 redraw_later(VALID);
9910 start_arrow(&tpos);
9911#ifdef FEAT_CINDENT
9912 can_cindent = TRUE;
9913#endif
9914 }
9915 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009916 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917}
9918
9919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009920ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921{
9922 pos_T tpos;
9923
9924 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009925
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009926 if (mod_mask & MOD_MASK_CTRL)
9927 {
9928 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009929 if (first_tabpage->tp_next != NULL)
9930 {
9931 start_arrow(&curwin->w_cursor);
9932 goto_tabpage(0);
9933 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009934 return;
9935 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009936
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 tpos = curwin->w_cursor;
9938 if (onepage(FORWARD, 1L) == OK)
9939 {
9940 start_arrow(&tpos);
9941#ifdef FEAT_CINDENT
9942 can_cindent = TRUE;
9943#endif
9944 }
9945 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009946 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947}
9948
9949#ifdef FEAT_DND
9950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009951ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952{
9953 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9954}
9955#endif
9956
9957/*
9958 * Handle TAB in Insert or Replace mode.
9959 * Return TRUE when the TAB needs to be inserted like a normal character.
9960 */
9961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009962ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963{
9964 int ind;
9965 int i;
9966 int temp;
9967
9968 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9969 Insstart_blank_vcol = get_nolist_virtcol();
9970 if (echeck_abbr(TAB + ABBR_OFF))
9971 return FALSE;
9972
9973 ind = inindent(0);
9974#ifdef FEAT_CINDENT
9975 if (ind)
9976 can_cindent = FALSE;
9977#endif
9978
9979 /*
9980 * When nothing special, insert TAB like a normal character
9981 */
9982 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009983 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009984 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 return TRUE;
9986
9987 if (stop_arrow() == FAIL)
9988 return TRUE;
9989
9990 did_ai = FALSE;
9991#ifdef FEAT_SMARTINDENT
9992 did_si = FALSE;
9993 can_si = FALSE;
9994 can_si_back = FALSE;
9995#endif
9996 AppendToRedobuff((char_u *)"\t");
9997
9998 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009999 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010000 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10001 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 else /* otherwise use 'tabstop' */
10003 temp = (int)curbuf->b_p_ts;
10004 temp -= get_nolist_virtcol() % temp;
10005
10006 /*
10007 * Insert the first space with ins_char(). It will delete one char in
10008 * replace mode. Insert the rest with ins_str(); it will not delete any
10009 * chars. For VREPLACE mode, we use ins_char() for all characters.
10010 */
10011 ins_char(' ');
10012 while (--temp > 0)
10013 {
10014#ifdef FEAT_VREPLACE
10015 if (State & VREPLACE_FLAG)
10016 ins_char(' ');
10017 else
10018#endif
10019 {
10020 ins_str((char_u *)" ");
10021 if (State & REPLACE_FLAG) /* no char replaced */
10022 replace_push(NUL);
10023 }
10024 }
10025
10026 /*
10027 * When 'expandtab' not set: Replace spaces by TABs where possible.
10028 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010029 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 {
10031 char_u *ptr;
10032#ifdef FEAT_VREPLACE
10033 char_u *saved_line = NULL; /* init for GCC */
10034 pos_T pos;
10035#endif
10036 pos_T fpos;
10037 pos_T *cursor;
10038 colnr_T want_vcol, vcol;
10039 int change_col = -1;
10040 int save_list = curwin->w_p_list;
10041
10042 /*
10043 * Get the current line. For VREPLACE mode, don't make real changes
10044 * yet, just work on a copy of the line.
10045 */
10046#ifdef FEAT_VREPLACE
10047 if (State & VREPLACE_FLAG)
10048 {
10049 pos = curwin->w_cursor;
10050 cursor = &pos;
10051 saved_line = vim_strsave(ml_get_curline());
10052 if (saved_line == NULL)
10053 return FALSE;
10054 ptr = saved_line + pos.col;
10055 }
10056 else
10057#endif
10058 {
10059 ptr = ml_get_cursor();
10060 cursor = &curwin->w_cursor;
10061 }
10062
10063 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10064 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10065 curwin->w_p_list = FALSE;
10066
10067 /* Find first white before the cursor */
10068 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010069 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 {
10071 --fpos.col;
10072 --ptr;
10073 }
10074
10075 /* In Replace mode, don't change characters before the insert point. */
10076 if ((State & REPLACE_FLAG)
10077 && fpos.lnum == Insstart.lnum
10078 && fpos.col < Insstart.col)
10079 {
10080 ptr += Insstart.col - fpos.col;
10081 fpos.col = Insstart.col;
10082 }
10083
10084 /* compute virtual column numbers of first white and cursor */
10085 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10086 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10087
Bram Moolenaar597a4222014-06-25 14:39:50 +020010088 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10089 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010090 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010092 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 if (vcol + i > want_vcol)
10094 break;
10095 if (*ptr != TAB)
10096 {
10097 *ptr = TAB;
10098 if (change_col < 0)
10099 {
10100 change_col = fpos.col; /* Column of first change */
10101 /* May have to adjust Insstart */
10102 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10103 Insstart.col = fpos.col;
10104 }
10105 }
10106 ++fpos.col;
10107 ++ptr;
10108 vcol += i;
10109 }
10110
10111 if (change_col >= 0)
10112 {
10113 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010114 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115
10116 /* Skip over the spaces we need. */
10117 while (vcol < want_vcol && *ptr == ' ')
10118 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010119 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 ++ptr;
10121 ++repl_off;
10122 }
10123 if (vcol > want_vcol)
10124 {
10125 /* Must have a char with 'showbreak' just before it. */
10126 --ptr;
10127 --repl_off;
10128 }
10129 fpos.col += repl_off;
10130
10131 /* Delete following spaces. */
10132 i = cursor->col - fpos.col;
10133 if (i > 0)
10134 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010135 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 /* correct replace stack. */
10137 if ((State & REPLACE_FLAG)
10138#ifdef FEAT_VREPLACE
10139 && !(State & VREPLACE_FLAG)
10140#endif
10141 )
10142 for (temp = i; --temp >= 0; )
10143 replace_join(repl_off);
10144 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010145#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010146 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010147 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010148 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010149 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10150 (char_u *)"\t", 1);
10151 }
10152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 cursor->col -= i;
10154
10155#ifdef FEAT_VREPLACE
10156 /*
10157 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10158 * backspacing over the changed spacing and then inserting the new
10159 * spacing.
10160 */
10161 if (State & VREPLACE_FLAG)
10162 {
10163 /* Backspace from real cursor to change_col */
10164 backspace_until_column(change_col);
10165
10166 /* Insert each char in saved_line from changed_col to
10167 * ptr-cursor */
10168 ins_bytes_len(saved_line + change_col,
10169 cursor->col - change_col);
10170 }
10171#endif
10172 }
10173
10174#ifdef FEAT_VREPLACE
10175 if (State & VREPLACE_FLAG)
10176 vim_free(saved_line);
10177#endif
10178 curwin->w_p_list = save_list;
10179 }
10180
10181 return FALSE;
10182}
10183
10184/*
10185 * Handle CR or NL in insert mode.
10186 * Return TRUE when out of memory or can't undo.
10187 */
10188 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010189ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190{
10191 int i;
10192
10193 if (echeck_abbr(c + ABBR_OFF))
10194 return FALSE;
10195 if (stop_arrow() == FAIL)
10196 return TRUE;
10197 undisplay_dollar();
10198
10199 /*
10200 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10201 * character under the cursor. Only push a NUL on the replace stack,
10202 * nothing to put back when the NL is deleted.
10203 */
10204 if ((State & REPLACE_FLAG)
10205#ifdef FEAT_VREPLACE
10206 && !(State & VREPLACE_FLAG)
10207#endif
10208 )
10209 replace_push(NUL);
10210
10211 /*
10212 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10213 * replacing the next line, so we push all of the characters left on the
10214 * line onto the replace stack. This is not done here though, it is done
10215 * in open_line().
10216 */
10217
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010218#ifdef FEAT_VIRTUALEDIT
10219 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10220 * CTRL-O). */
10221 if (virtual_active() && curwin->w_cursor.coladd > 0)
10222 coladvance(getviscol());
10223#endif
10224
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225#ifdef FEAT_RIGHTLEFT
10226# ifdef FEAT_FKMAP
10227 if (p_altkeymap && p_fkmap)
10228 fkmap(NL);
10229# endif
10230 /* NL in reverse insert will always start in the end of
10231 * current line. */
10232 if (revins_on)
10233 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10234#endif
10235
10236 AppendToRedobuff(NL_STR);
10237 i = open_line(FORWARD,
10238#ifdef FEAT_COMMENTS
10239 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10240#endif
10241 0, old_indent);
10242 old_indent = 0;
10243#ifdef FEAT_CINDENT
10244 can_cindent = TRUE;
10245#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010246#ifdef FEAT_FOLDING
10247 /* When inserting a line the cursor line must never be in a closed fold. */
10248 foldOpenCursor();
10249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250
10251 return (!i);
10252}
10253
10254#ifdef FEAT_DIGRAPHS
10255/*
10256 * Handle digraph in insert mode.
10257 * Returns character still to be inserted, or NUL when nothing remaining to be
10258 * done.
10259 */
10260 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010261ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262{
10263 int c;
10264 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010265 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266
10267 pc_status = PC_STATUS_UNSET;
10268 if (redrawing() && !char_avail())
10269 {
10270 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010271 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272
10273 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010274 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275#ifdef FEAT_CMDL_INFO
10276 add_to_showcmd_c(Ctrl_K);
10277#endif
10278 }
10279
10280#ifdef USE_ON_FLY_SCROLL
10281 dont_scroll = TRUE; /* disallow scrolling here */
10282#endif
10283
10284 /* don't map the digraph chars. This also prevents the
10285 * mode message to be deleted when ESC is hit */
10286 ++no_mapping;
10287 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010288 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 --no_mapping;
10290 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010291 if (did_putchar)
10292 /* when the line fits in 'columns' the '?' is at the start of the next
10293 * line and will not be removed by the redraw */
10294 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010295
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 if (IS_SPECIAL(c) || mod_mask) /* special key */
10297 {
10298#ifdef FEAT_CMDL_INFO
10299 clear_showcmd();
10300#endif
10301 insert_special(c, TRUE, FALSE);
10302 return NUL;
10303 }
10304 if (c != ESC)
10305 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010306 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 if (redrawing() && !char_avail())
10308 {
10309 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010310 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311
10312 if (char2cells(c) == 1)
10313 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010314 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010316 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 }
10318#ifdef FEAT_CMDL_INFO
10319 add_to_showcmd_c(c);
10320#endif
10321 }
10322 ++no_mapping;
10323 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010324 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 --no_mapping;
10326 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010327 if (did_putchar)
10328 /* when the line fits in 'columns' the '?' is at the start of the
10329 * next line and will not be removed by a redraw */
10330 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 if (cc != ESC)
10332 {
10333 AppendToRedobuff((char_u *)CTRL_V_STR);
10334 c = getdigraph(c, cc, TRUE);
10335#ifdef FEAT_CMDL_INFO
10336 clear_showcmd();
10337#endif
10338 return c;
10339 }
10340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341#ifdef FEAT_CMDL_INFO
10342 clear_showcmd();
10343#endif
10344 return NUL;
10345}
10346#endif
10347
10348/*
10349 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10350 * Returns the char to be inserted, or NUL if none found.
10351 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010353ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354{
10355 int c;
10356 int temp;
10357 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010358 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359
10360 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10361 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010362 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 return NUL;
10364 }
10365
10366 /* try to advance to the cursor column */
10367 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010368 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 prev_ptr = ptr;
10370 validate_virtcol();
10371 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10372 {
10373 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010374 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 }
10376 if ((colnr_T)temp > curwin->w_virtcol)
10377 ptr = prev_ptr;
10378
10379#ifdef FEAT_MBYTE
10380 c = (*mb_ptr2char)(ptr);
10381#else
10382 c = *ptr;
10383#endif
10384 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010385 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 return c;
10387}
10388
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010389/*
10390 * CTRL-Y or CTRL-E typed in Insert mode.
10391 */
10392 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010393ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010394{
10395 int c = tc;
10396
10397#ifdef FEAT_INS_EXPAND
10398 if (ctrl_x_mode == CTRL_X_SCROLL)
10399 {
10400 if (c == Ctrl_Y)
10401 scrolldown_clamp();
10402 else
10403 scrollup_clamp();
10404 redraw_later(VALID);
10405 }
10406 else
10407#endif
10408 {
10409 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10410 if (c != NUL)
10411 {
10412 long tw_save;
10413
10414 /* The character must be taken literally, insert like it
10415 * was typed after a CTRL-V, and pretend 'textwidth'
10416 * wasn't set. Digits, 'o' and 'x' are special after a
10417 * CTRL-V, don't use it for these. */
10418 if (c < 256 && !isalnum(c))
10419 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10420 tw_save = curbuf->b_p_tw;
10421 curbuf->b_p_tw = -1;
10422 insert_special(c, TRUE, FALSE);
10423 curbuf->b_p_tw = tw_save;
10424#ifdef FEAT_RIGHTLEFT
10425 revins_chars++;
10426 revins_legal++;
10427#endif
10428 c = Ctrl_V; /* pretend CTRL-V is last character */
10429 auto_format(FALSE, TRUE);
10430 }
10431 }
10432 return c;
10433}
10434
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435#ifdef FEAT_SMARTINDENT
10436/*
10437 * Try to do some very smart auto-indenting.
10438 * Used when inserting a "normal" character.
10439 */
10440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010441ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442{
10443 pos_T *pos, old_pos;
10444 char_u *ptr;
10445 int i;
10446 int temp;
10447
10448 /*
10449 * do some very smart indenting when entering '{' or '}'
10450 */
10451 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10452 {
10453 /*
10454 * for '}' set indent equal to indent of line containing matching '{'
10455 */
10456 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10457 {
10458 old_pos = curwin->w_cursor;
10459 /*
10460 * If the matching '{' has a ')' immediately before it (ignoring
10461 * white-space), then line up with the start of the line
10462 * containing the matching '(' if there is one. This handles the
10463 * case where an "if (..\n..) {" statement continues over multiple
10464 * lines -- webb
10465 */
10466 ptr = ml_get(pos->lnum);
10467 i = pos->col;
10468 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010469 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 ;
10471 curwin->w_cursor.lnum = pos->lnum;
10472 curwin->w_cursor.col = i;
10473 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10474 curwin->w_cursor = *pos;
10475 i = get_indent();
10476 curwin->w_cursor = old_pos;
10477#ifdef FEAT_VREPLACE
10478 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010479 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 else
10481#endif
10482 (void)set_indent(i, SIN_CHANGED);
10483 }
10484 else if (curwin->w_cursor.col > 0)
10485 {
10486 /*
10487 * when inserting '{' after "O" reduce indent, but not
10488 * more than indent of previous line
10489 */
10490 temp = TRUE;
10491 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10492 {
10493 old_pos = curwin->w_cursor;
10494 i = get_indent();
10495 while (curwin->w_cursor.lnum > 1)
10496 {
10497 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10498
10499 /* ignore empty lines and lines starting with '#'. */
10500 if (*ptr != '#' && *ptr != NUL)
10501 break;
10502 }
10503 if (get_indent() >= i)
10504 temp = FALSE;
10505 curwin->w_cursor = old_pos;
10506 }
10507 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010508 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 }
10510 }
10511
10512 /*
10513 * set indent of '#' always to 0
10514 */
10515 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10516 {
10517 /* remember current indent for next line */
10518 old_indent = get_indent();
10519 (void)set_indent(0, SIN_CHANGED);
10520 }
10521
10522 /* Adjust ai_col, the char at this position can be deleted. */
10523 if (ai_col > curwin->w_cursor.col)
10524 ai_col = curwin->w_cursor.col;
10525}
10526#endif
10527
10528/*
10529 * Get the value that w_virtcol would have when 'list' is off.
10530 * Unless 'cpo' contains the 'L' flag.
10531 */
10532 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010533get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534{
10535 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10536 return getvcol_nolist(&curwin->w_cursor);
10537 validate_virtcol();
10538 return curwin->w_virtcol;
10539}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010540
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010541#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010542/*
10543 * Handle the InsertCharPre autocommand.
10544 * "c" is the character that was typed.
10545 * Return a pointer to allocated memory with the replacement string.
10546 * Return NULL to continue inserting "c".
10547 */
10548 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010549do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010550{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010551 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010552 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010553
10554 /* Return quickly when there is nothing to do. */
10555 if (!has_insertcharpre())
10556 return NULL;
10557
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010558# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010559 if (has_mbyte)
10560 buf[(*mb_char2bytes)(c, buf)] = NUL;
10561 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010562# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010563 {
10564 buf[0] = c;
10565 buf[1] = NUL;
10566 }
10567
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010568 /* Lock the text to avoid weird things from happening. */
10569 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010570 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010571
Bram Moolenaar704984a2012-06-01 14:57:51 +020010572 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010573 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010574 {
10575 /* Get the value of v:char. It may be empty or more than one
10576 * character. Only use it when changed, otherwise continue with the
10577 * original character to avoid breaking autoindent. */
10578 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10579 res = vim_strsave(get_vim_var_str(VV_CHAR));
10580 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010581
10582 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10583 --textlock;
10584
10585 return res;
10586}
10587#endif