blob: fa1d84bbc7a685f42c26e90f2631cfcf8dfb5a2f [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 Moolenaarf5876f12012-02-29 18:22:08 +0100276#ifdef FEAT_AUTOCMD
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#ifdef FEAT_AUTOCMD
392 /*
393 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
394 */
395 if (cmdchar != 'r' && cmdchar != 'v')
396 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100397 pos_T save_cursor = curwin->w_cursor;
398
Bram Moolenaar1e015462005-09-25 22:16:38 +0000399# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000400 if (cmdchar == 'R')
401 ptr = (char_u *)"r";
402 else if (cmdchar == 'V')
403 ptr = (char_u *)"v";
404 else
405 ptr = (char_u *)"i";
406 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200407 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000408# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000409 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100410
Bram Moolenaar097c9922013-05-19 21:15:15 +0200411 /* Make sure the cursor didn't move. Do call check_cursor_col() in
412 * case the text was modified. Since Insert mode was not started yet
413 * a call to check_cursor_col() may move the cursor, especially with
414 * the "A" command, thus set State to avoid that. Also check that the
415 * line number is still valid (lines may have been deleted).
416 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100417 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaar097c9922013-05-19 21:15:15 +0200418# ifdef FEAT_EVAL
419 && *get_vim_var_str(VV_CHAR) == NUL
420# endif
421 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100422 {
423 int save_state = State;
424
425 curwin->w_cursor = save_cursor;
426 State = INSERT;
427 check_cursor_col();
428 State = save_state;
429 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000430 }
431#endif
432
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200433#ifdef FEAT_CONCEAL
434 /* Check if the cursor line needs redrawing before changing State. If
435 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200436 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200437#endif
438
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439#ifdef FEAT_MOUSE
440 /*
441 * When doing a paste with the middle mouse button, Insstart is set to
442 * where the paste started.
443 */
444 if (where_paste_started.lnum != 0)
445 Insstart = where_paste_started;
446 else
447#endif
448 {
449 Insstart = curwin->w_cursor;
450 if (startln)
451 Insstart.col = 0;
452 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000453 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 Insstart_blank_vcol = MAXCOL;
455 if (!did_ai)
456 ai_col = 0;
457
458 if (cmdchar != NUL && restart_edit == 0)
459 {
460 ResetRedobuff();
461 AppendNumberToRedobuff(count);
462#ifdef FEAT_VREPLACE
463 if (cmdchar == 'V' || cmdchar == 'v')
464 {
465 /* "gR" or "gr" command */
466 AppendCharToRedobuff('g');
467 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
468 }
469 else
470#endif
471 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100472 if (cmdchar == K_PS)
473 AppendCharToRedobuff('a');
474 else
475 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 if (cmdchar == 'g') /* "gI" command */
477 AppendCharToRedobuff('I');
478 else if (cmdchar == 'r') /* "r<CR>" command */
479 count = 1; /* insert only one <CR> */
480 }
481 }
482
483 if (cmdchar == 'R')
484 {
485#ifdef FEAT_FKMAP
486 if (p_fkmap && p_ri)
487 {
488 beep_flush();
489 EMSG(farsi_text_3); /* encoded in Farsi */
490 State = INSERT;
491 }
492 else
493#endif
494 State = REPLACE;
495 }
496#ifdef FEAT_VREPLACE
497 else if (cmdchar == 'V' || cmdchar == 'v')
498 {
499 State = VREPLACE;
500 replaceState = VREPLACE;
501 orig_line_count = curbuf->b_ml.ml_line_count;
502 vr_lines_changed = 1;
503 }
504#endif
505 else
506 State = INSERT;
507
508 stop_insert_mode = FALSE;
509
510 /*
511 * Need to recompute the cursor position, it might move when the cursor is
512 * on a TAB or special character.
513 */
514 curs_columns(TRUE);
515
516 /*
517 * Enable langmap or IME, indicated by 'iminsert'.
518 * Note that IME may enabled/disabled without us noticing here, thus the
519 * 'iminsert' value may not reflect what is actually used. It is updated
520 * when hitting <Esc>.
521 */
522 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
523 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100524#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
526#endif
527
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528#ifdef FEAT_MOUSE
529 setmouse();
530#endif
531#ifdef FEAT_CMDL_INFO
532 clear_showcmd();
533#endif
534#ifdef FEAT_RIGHTLEFT
535 /* there is no reverse replace mode */
536 revins_on = (State == INSERT && p_ri);
537 if (revins_on)
538 undisplay_dollar();
539 revins_chars = 0;
540 revins_legal = 0;
541 revins_scol = -1;
542#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100543 if (!p_ek)
544 /* Disable bracketed paste mode, we won't recognize the escape
545 * sequences. */
546 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
548 /*
549 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100550 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
551 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
553 if (restart_edit != 0 && stuff_empty())
554 {
555#ifdef FEAT_MOUSE
556 /*
557 * After a paste we consider text typed to be part of the insert for
558 * the pasted text. You can backspace over the pasted text too.
559 */
560 if (where_paste_started.lnum)
561 arrow_used = FALSE;
562 else
563#endif
564 arrow_used = TRUE;
565 restart_edit = 0;
566
567 /*
568 * If the cursor was after the end-of-line before the CTRL-O and it is
569 * now at the end-of-line, put it after the end-of-line (this is not
570 * correct in very rare cases).
571 * Also do this if curswant is greater than the current virtual
572 * column. Eg after "^O$" or "^O80|".
573 */
574 validate_virtcol();
575 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000576 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 || curwin->w_curswant > curwin->w_virtcol)
578 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
579 {
580 if (ptr[1] == NUL)
581 ++curwin->w_cursor.col;
582#ifdef FEAT_MBYTE
583 else if (has_mbyte)
584 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000585 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (ptr[i] == NUL)
587 curwin->w_cursor.col += i;
588 }
589#endif
590 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000591 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
593 else
594 arrow_used = FALSE;
595
596 /* we are in insert mode now, don't need to start it anymore */
597 need_start_insertmode = FALSE;
598
599 /* Need to save the line for undo before inserting the first char. */
600 ins_need_undo = TRUE;
601
602#ifdef FEAT_MOUSE
603 where_paste_started.lnum = 0;
604#endif
605#ifdef FEAT_CINDENT
606 can_cindent = TRUE;
607#endif
608#ifdef FEAT_FOLDING
609 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
610 * restarting. */
611 if (!p_im && did_restart_edit == 0)
612 foldOpenCursor();
613#endif
614
615 /*
616 * If 'showmode' is set, show the current (insert/replace/..) mode.
617 * A warning message for changing a readonly file is given here, before
618 * actually changing anything. It's put after the mode, if any.
619 */
620 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000621 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 i = showmode();
623
624 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000625 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627#ifdef CURSOR_SHAPE
628 ui_cursor_shape(); /* may show different cursor shape */
629#endif
630#ifdef FEAT_DIGRAPHS
631 do_digraph(-1); /* clear digraphs */
632#endif
633
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000634 /*
635 * Get the current length of the redo buffer, those characters have to be
636 * skipped if we want to get to the inserted characters.
637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ptr = get_inserted();
639 if (ptr == NULL)
640 new_insert_skip = 0;
641 else
642 {
643 new_insert_skip = (int)STRLEN(ptr);
644 vim_free(ptr);
645 }
646
647 old_indent = 0;
648
649 /*
650 * Main loop in Insert mode: repeat until Insert mode is left.
651 */
652 for (;;)
653 {
654#ifdef FEAT_RIGHTLEFT
655 if (!revins_legal)
656 revins_scol = -1; /* reset on illegal motions */
657 else
658 revins_legal = 0;
659#endif
660 if (arrow_used) /* don't repeat insert when arrow key used */
661 count = 0;
662
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100663 if (update_Insstart_orig)
664 Insstart_orig = Insstart;
665
Bram Moolenaar00672e12016-06-26 18:38:13 +0200666 if (stop_insert_mode
667#ifdef FEAT_INS_EXPAND
668 && !pum_visible()
669#endif
670 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
672 /* ":stopinsert" used or 'insertmode' reset */
673 count = 0;
674 goto doESCkey;
675 }
676
677 /* set curwin->w_curswant for next K_DOWN or K_UP */
678 if (!arrow_used)
679 curwin->w_set_curswant = TRUE;
680
681 /* If there is no typeahead may check for timestamps (e.g., for when a
682 * menu invoked a shell command). */
683 if (stuff_empty())
684 {
685 did_check_timestamps = FALSE;
686 if (need_check_timestamps)
687 check_timestamps(FALSE);
688 }
689
690 /*
691 * When emsg() was called msg_scroll will have been set.
692 */
693 msg_scroll = FALSE;
694
695#ifdef FEAT_GUI
696 /* When 'mousefocus' is set a mouse movement may have taken us to
697 * another window. "need_mouse_correct" may then be set because of an
698 * autocommand. */
699 if (need_mouse_correct)
700 gui_mouse_correct();
701#endif
702
703#ifdef FEAT_FOLDING
704 /* Open fold at the cursor line, according to 'foldopen'. */
705 if (fdo_flags & FDO_INSERT)
706 foldOpenCursor();
707 /* Close folds where the cursor isn't, according to 'foldclose' */
708 if (!char_avail())
709 foldCheckClose();
710#endif
711
712 /*
713 * If we inserted a character at the last position of the last line in
714 * the window, scroll the window one line up. This avoids an extra
715 * redraw.
716 * This is detected when the cursor column is smaller after inserting
717 * something.
718 * Don't do this when the topline changed already, it has
719 * already been adjusted (by insertchar() calling open_line())).
720 */
721 if (curbuf->b_mod_set
722 && curwin->w_p_wrap
723 && !did_backspace
724 && curwin->w_topline == old_topline
725#ifdef FEAT_DIFF
726 && curwin->w_topfill == old_topfill
727#endif
728 )
729 {
730 mincol = curwin->w_wcol;
731 validate_cursor_col();
732
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000733 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 && curwin->w_wrow == W_WINROW(curwin)
735 + curwin->w_height - 1 - p_so
736 && (curwin->w_cursor.lnum != curwin->w_topline
737#ifdef FEAT_DIFF
738 || curwin->w_topfill > 0
739#endif
740 ))
741 {
742#ifdef FEAT_DIFF
743 if (curwin->w_topfill > 0)
744 --curwin->w_topfill;
745 else
746#endif
747#ifdef FEAT_FOLDING
748 if (hasFolding(curwin->w_topline, NULL, &old_topline))
749 set_topline(curwin, old_topline + 1);
750 else
751#endif
752 set_topline(curwin, curwin->w_topline + 1);
753 }
754 }
755
756 /* May need to adjust w_topline to show the cursor. */
757 update_topline();
758
759 did_backspace = FALSE;
760
761 validate_cursor(); /* may set must_redraw */
762
763 /*
764 * Redraw the display when no characters are waiting.
765 * Also shows mode, ruler and positions cursor.
766 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000767 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
769#ifdef FEAT_SCROLLBIND
770 if (curwin->w_p_scb)
771 do_check_scrollbind(TRUE);
772#endif
773
Bram Moolenaar860cae12010-06-05 23:22:07 +0200774#ifdef FEAT_CURSORBIND
775 if (curwin->w_p_crb)
776 do_check_cursorbind();
777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 update_curswant();
779 old_topline = curwin->w_topline;
780#ifdef FEAT_DIFF
781 old_topfill = curwin->w_topfill;
782#endif
783
784#ifdef USE_ON_FLY_SCROLL
785 dont_scroll = FALSE; /* allow scrolling here */
786#endif
787
788 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100789 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100791 if (c != K_CURSORHOLD)
792 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200793
794 /* After using CTRL-G U the next cursor key will not break undo. */
795 if (dont_sync_undo == MAYBE)
796 dont_sync_undo = TRUE;
797 else
798 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100799 if (cmdchar == K_PS)
800 /* Got here from normal mode when bracketed paste started. */
801 c = K_PS;
802 else
803 do
804 {
805 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100806 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000808#ifdef FEAT_AUTOCMD
809 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
810 did_cursorhold = TRUE;
811#endif
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#ifdef FEAT_RIGHTLEFT
814 if (p_hkmap && KeyTyped)
815 c = hkmap(c); /* Hebrew mode mapping */
816#endif
817#ifdef FEAT_FKMAP
818 if (p_fkmap && KeyTyped)
819 c = fkmap(c); /* Farsi mode mapping */
820#endif
821
822#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000823 /*
824 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000825 * and the cursor is still in the completed word. Only when there is
826 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000827 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000828 if (compl_started
829 && pum_wanted()
830 && curwin->w_cursor.col >= compl_col
831 && (compl_shown_match == NULL
832 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000833 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000834 /* BS: Delete one character from "compl_leader". */
835 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000836 && curwin->w_cursor.col > compl_col
837 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000838 continue;
839
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000840 /* When no match was selected or it was edited. */
841 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000842 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000843 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000844 * "compl_leader". Except when at the original match and
845 * there is nothing to add, CTRL-L works like CTRL-P then. */
846 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100847 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000848 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000849 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000850 {
851 ins_compl_addfrommatch();
852 continue;
853 }
854
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000855 /* A non-white character that fits in with the current
856 * completion: Add to "compl_leader". */
857 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000858 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100859#ifdef FEAT_AUTOCMD
860 /* Trigger InsertCharPre. */
861 char_u *str = do_insert_char_pre(c);
862 char_u *p;
863
864 if (str != NULL)
865 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100866 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100867 ins_compl_addleader(PTR2CHAR(p));
868 vim_free(str);
869 }
870 else
871#endif
872 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000873 continue;
874 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000875
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000876 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000877 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200878 if ((c == Ctrl_Y || (compl_enter_selects
879 && (c == CAR || c == K_KENTER || c == NL)))
880 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000881 {
882 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200883 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000884 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000885 }
886 }
887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
889 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000890 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000891 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000892 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#endif
894
Bram Moolenaar488c6512005-08-11 20:09:58 +0000895 /* CTRL-\ CTRL-N goes to Normal mode,
896 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
897 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (c == Ctrl_BSL)
899 {
900 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000901 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 ++no_mapping;
903 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000904 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 --no_mapping;
906 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000907 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000909 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 vungetc(c);
911 c = Ctrl_BSL;
912 }
913 else if (c == Ctrl_G && p_im)
914 continue;
915 else
916 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000917 if (c == Ctrl_O)
918 {
919 ins_ctrl_o();
920 ins_at_eol = FALSE; /* cursor keeps its column */
921 nomove = TRUE;
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 count = 0;
924 goto doESCkey;
925 }
926 }
927
928#ifdef FEAT_DIGRAPHS
929 c = do_digraph(c);
930#endif
931
932#ifdef FEAT_INS_EXPAND
933 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
934 goto docomplete;
935#endif
936 if (c == Ctrl_V || c == Ctrl_Q)
937 {
938 ins_ctrl_v();
939 c = Ctrl_V; /* pretend CTRL-V is last typed character */
940 continue;
941 }
942
943#ifdef FEAT_CINDENT
944 if (cindent_on()
945# ifdef FEAT_INS_EXPAND
946 && ctrl_x_mode == 0
947# endif
948 )
949 {
950 /* A key name preceded by a bang means this key is not to be
951 * inserted. Skip ahead to the re-indenting below.
952 * A key name preceded by a star means that indenting has to be
953 * done before inserting the key. */
954 line_is_white = inindent(0);
955 if (in_cinkeys(c, '!', line_is_white))
956 goto force_cindent;
957 if (can_cindent && in_cinkeys(c, '*', line_is_white)
958 && stop_arrow() == OK)
959 do_c_expr_indent();
960 }
961#endif
962
963#ifdef FEAT_RIGHTLEFT
964 if (curwin->w_p_rl)
965 switch (c)
966 {
967 case K_LEFT: c = K_RIGHT; break;
968 case K_S_LEFT: c = K_S_RIGHT; break;
969 case K_C_LEFT: c = K_C_RIGHT; break;
970 case K_RIGHT: c = K_LEFT; break;
971 case K_S_RIGHT: c = K_S_LEFT; break;
972 case K_C_RIGHT: c = K_C_LEFT; break;
973 }
974#endif
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 /*
977 * If 'keymodel' contains "startsel", may start selection. If it
978 * does, a CTRL-O and c will be stuffed, we need to get these
979 * characters.
980 */
981 if (ins_start_select(c))
982 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984 /*
985 * The big switch to handle a character in insert mode.
986 */
987 switch (c)
988 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000989 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 if (echeck_abbr(ESC + ABBR_OFF))
991 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200992 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#ifdef FEAT_CMDWIN
996 if (c == Ctrl_C && cmdwin_type != 0)
997 {
998 /* Close the cmdline window. */
999 cmdwin_result = K_IGNORE;
1000 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001001 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 goto doESCkey;
1003 }
1004#endif
1005
1006#ifdef UNIX
1007do_intr:
1008#endif
1009 /* when 'insertmode' set, and not halfway a mapping, don't leave
1010 * Insert mode */
1011 if (goto_im())
1012 {
1013 if (got_int)
1014 {
1015 (void)vgetc(); /* flush all buffers */
1016 got_int = FALSE;
1017 }
1018 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001019 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 break;
1021 }
1022doESCkey:
1023 /*
1024 * This is the ONLY return from edit()!
1025 */
1026 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1027 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001028 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 o_lnum = curwin->w_cursor.lnum;
1030
Bram Moolenaar488c6512005-08-11 20:09:58 +00001031 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001032 {
1033#ifdef FEAT_AUTOCMD
1034 if (cmdchar != 'r' && cmdchar != 'v')
1035 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1036 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001037 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 continue;
1042
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 case Ctrl_Z: /* suspend when 'insertmode' set */
1044 if (!p_im)
1045 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001046 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001047#ifdef CURSOR_SHAPE
1048 ui_cursor_shape(); /* may need to update cursor shape */
1049#endif
1050 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001051
1052 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001053#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001054 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 goto docomplete;
1056#endif
1057 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1058 break;
1059 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001060
1061#ifdef FEAT_VIRTUALEDIT
1062 /* don't move the cursor left when 'virtualedit' has "onemore". */
1063 if (ve_flags & VE_ONEMORE)
1064 {
1065 ins_at_eol = FALSE;
1066 nomove = TRUE;
1067 }
1068#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001069 count = 0;
1070 goto doESCkey;
1071
Bram Moolenaar572cb562005-08-05 21:35:02 +00001072 case K_INS: /* toggle insert/replace mode */
1073 case K_KINS:
1074 ins_insert(replaceState);
1075 break;
1076
1077 case K_SELECT: /* end of Select mode mapping - ignore */
1078 break;
1079
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 case K_HELP: /* Help key works like <ESC> <Help> */
1081 case K_F1:
1082 case K_XF1:
1083 stuffcharReadbuff(K_HELP);
1084 if (p_im)
1085 need_start_insertmode = TRUE;
1086 goto doESCkey;
1087
1088#ifdef FEAT_NETBEANS_INTG
1089 case K_F21: /* NetBeans command */
1090 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001091 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 --no_mapping;
1093 netbeans_keycommand(i);
1094 break;
1095#endif
1096
1097 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 case NUL:
1099 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 /* For ^@ the trailing ESC will end the insert, unless there is an
1101 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1103 && c != Ctrl_A && !p_im)
1104 goto doESCkey; /* quit insert mode */
1105 inserted_space = FALSE;
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_reg();
1110 auto_format(FALSE, TRUE);
1111 inserted_space = FALSE;
1112 break;
1113
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001114 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 ins_ctrl_g();
1116 break;
1117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case Ctrl_HAT: /* switch input mode and/or langmap */
1119 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
1122#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001123 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (!p_ari)
1125 goto normalchar;
1126 ins_ctrl_();
1127 break;
1128#endif
1129
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001130 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1132 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1133 goto docomplete;
1134#endif
1135 /* FALLTHROUGH */
1136
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138# ifdef FEAT_INS_EXPAND
1139 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1140 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001141 if (has_compl_option(FALSE))
1142 goto docomplete;
1143 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145# endif
1146 ins_shift(c, lastc);
1147 auto_format(FALSE, TRUE);
1148 inserted_space = FALSE;
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case K_KDEL:
1153 ins_del();
1154 auto_format(FALSE, TRUE);
1155 break;
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 case Ctrl_H:
1159 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1160 auto_format(FALSE, TRUE);
1161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1165 auto_format(FALSE, TRUE);
1166 break;
1167
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001168 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001169# ifdef FEAT_COMPL_FUNC
1170 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001171 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001172 goto docomplete;
1173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1175 auto_format(FALSE, TRUE);
1176 inserted_space = FALSE;
1177 break;
1178
1179#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 case K_LEFTMOUSE_NM:
1182 case K_LEFTDRAG:
1183 case K_LEFTRELEASE:
1184 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001185 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 case K_MIDDLEMOUSE:
1187 case K_MIDDLEDRAG:
1188 case K_MIDDLERELEASE:
1189 case K_RIGHTMOUSE:
1190 case K_RIGHTDRAG:
1191 case K_RIGHTRELEASE:
1192 case K_X1MOUSE:
1193 case K_X1DRAG:
1194 case K_X1RELEASE:
1195 case K_X2MOUSE:
1196 case K_X2DRAG:
1197 case K_X2RELEASE:
1198 ins_mouse(c);
1199 break;
1200
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001201 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001202 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 break;
1204
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001205 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001206 ins_mousescroll(MSCR_UP);
1207 break;
1208
1209 case K_MOUSELEFT: /* Scroll wheel left */
1210 ins_mousescroll(MSCR_LEFT);
1211 break;
1212
1213 case K_MOUSERIGHT: /* Scroll wheel right */
1214 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 break;
1216#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001217 case K_PS:
1218 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1219 if (cmdchar == K_PS)
1220 /* invoked from normal mode, bail out */
1221 goto doESCkey;
1222 break;
1223 case K_PE:
1224 /* Got K_PE without K_PS, ignore. */
1225 break;
1226
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001227#ifdef FEAT_GUI_TABLINE
1228 case K_TABLINE:
1229 case K_TABMENU:
1230 ins_tabline(c);
1231 break;
1232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 break;
1236
Bram Moolenaar754b5602006-02-09 23:53:20 +00001237#ifdef FEAT_AUTOCMD
1238 case K_CURSORHOLD: /* Didn't type something for a while. */
1239 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1240 did_cursorhold = TRUE;
1241 break;
1242#endif
1243
Bram Moolenaar4770d092006-01-12 23:22:24 +00001244#ifdef FEAT_GUI_W32
1245 /* On Win32 ignore <M-F4>, we get it when closing the window was
1246 * cancelled. */
1247 case K_F4:
1248 if (mod_mask != MOD_MASK_ALT)
1249 goto normalchar;
1250 break;
1251#endif
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253#ifdef FEAT_GUI
1254 case K_VER_SCROLLBAR:
1255 ins_scroll();
1256 break;
1257
1258 case K_HOR_SCROLLBAR:
1259 ins_horscroll();
1260 break;
1261#endif
1262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 case K_S_HOME:
1266 case K_C_HOME:
1267 ins_home(c);
1268 break;
1269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001270 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 case K_S_END:
1273 case K_C_END:
1274 ins_end(c);
1275 break;
1276
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001277 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001278 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1279 ins_s_left();
1280 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001281 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 break;
1283
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001284 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 case K_C_LEFT:
1286 ins_s_left();
1287 break;
1288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001289 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001290 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1291 ins_s_right();
1292 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001293 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 break;
1295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 case K_C_RIGHT:
1298 ins_s_right();
1299 break;
1300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001301 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001302#ifdef FEAT_INS_EXPAND
1303 if (pum_visible())
1304 goto docomplete;
1305#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001306 if (mod_mask & MOD_MASK_SHIFT)
1307 ins_pageup();
1308 else
1309 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 break;
1311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001312 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 case K_PAGEUP:
1314 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001315#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001316 if (pum_visible())
1317 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 ins_pageup();
1320 break;
1321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001322 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001323#ifdef FEAT_INS_EXPAND
1324 if (pum_visible())
1325 goto docomplete;
1326#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001327 if (mod_mask & MOD_MASK_SHIFT)
1328 ins_pagedown();
1329 else
1330 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 break;
1332
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001333 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 case K_PAGEDOWN:
1335 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001336#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001337 if (pum_visible())
1338 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 ins_pagedown();
1341 break;
1342
1343#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 ins_drop();
1346 break;
1347#endif
1348
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001349 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 c = TAB;
1351 /* FALLTHROUGH */
1352
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001353 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1355 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1356 goto docomplete;
1357#endif
1358 inserted_space = FALSE;
1359 if (ins_tab())
1360 goto normalchar; /* insert TAB as a normal char */
1361 auto_format(FALSE, TRUE);
1362 break;
1363
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001364 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 c = CAR;
1366 /* FALLTHROUGH */
1367 case CAR:
1368 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001369#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /* In a quickfix window a <CR> jumps to the error under the
1371 * cursor. */
1372 if (bt_quickfix(curbuf) && c == CAR)
1373 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001374 if (curwin->w_llist_ref == NULL) /* quickfix window */
1375 do_cmdline_cmd((char_u *)".cc");
1376 else /* location list window */
1377 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 break;
1379 }
1380#endif
1381#ifdef FEAT_CMDWIN
1382 if (cmdwin_type != 0)
1383 {
1384 /* Execute the command in the cmdline window. */
1385 cmdwin_result = CAR;
1386 goto doESCkey;
1387 }
1388#endif
1389 if (ins_eol(c) && !p_im)
1390 goto doESCkey; /* out of memory */
1391 auto_format(FALSE, FALSE);
1392 inserted_space = FALSE;
1393 break;
1394
Bram Moolenaar860cae12010-06-05 23:22:07 +02001395#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001396 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397# ifdef FEAT_INS_EXPAND
1398 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1399 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001400 if (has_compl_option(TRUE))
1401 goto docomplete;
1402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404# endif
1405# ifdef FEAT_DIGRAPHS
1406 c = ins_digraph();
1407 if (c == NUL)
1408 break;
1409# endif
1410 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
1413#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001414 case Ctrl_X: /* Enter CTRL-X mode */
1415 ins_ctrl_x();
1416 break;
1417
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001418 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (ctrl_x_mode != CTRL_X_TAGS)
1420 goto normalchar;
1421 goto docomplete;
1422
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001423 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (ctrl_x_mode != CTRL_X_FILES)
1425 goto normalchar;
1426 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001427
1428 case 's': /* Spelling completion after ^X */
1429 case Ctrl_S:
1430 if (ctrl_x_mode != CTRL_X_SPELL)
1431 goto normalchar;
1432 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433#endif
1434
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001435 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436#ifdef FEAT_INS_EXPAND
1437 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1438#endif
1439 {
1440 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1441 if (p_im)
1442 {
1443 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1444 break;
1445 goto doESCkey;
1446 }
1447 goto normalchar;
1448 }
1449#ifdef FEAT_INS_EXPAND
1450 /* FALLTHROUGH */
1451
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001452 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 case Ctrl_N:
1454 /* if 'complete' is empty then plain ^P is no longer special,
1455 * but it is under other ^X modes */
1456 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001457 && (ctrl_x_mode == CTRL_X_NORMAL
1458 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001459 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 goto normalchar;
1461
1462docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001463 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001464#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001465 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001466#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001467 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001468 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001469#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001470 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001471#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001472 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 break;
1474#endif /* FEAT_INS_EXPAND */
1475
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001476 case Ctrl_Y: /* copy from previous line or scroll down */
1477 case Ctrl_E: /* copy from next line or scroll up */
1478 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 break;
1480
1481 default:
1482#ifdef UNIX
1483 if (c == intr_char) /* special interrupt char */
1484 goto do_intr;
1485#endif
1486
Bram Moolenaare659c952011-05-19 17:25:41 +02001487normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001489 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001491#ifdef FEAT_AUTOCMD
1492 if (!p_paste)
1493 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001494 /* Trigger InsertCharPre. */
1495 char_u *str = do_insert_char_pre(c);
1496 char_u *p;
1497
1498 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001499 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001500 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001501 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001502 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001503 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001504 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001505 c = PTR2CHAR(p);
1506 if (c == CAR || c == K_KENTER || c == NL)
1507 ins_eol(c);
1508 else
1509 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001510 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001511 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001512 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001513 vim_free(str);
1514 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001515 }
1516
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001517 /* If the new value is already inserted or an empty string
1518 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001519 if (c == NUL)
1520 break;
1521 }
1522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523#ifdef FEAT_SMARTINDENT
1524 /* Try to perform smart-indenting. */
1525 ins_try_si(c);
1526#endif
1527
1528 if (c == ' ')
1529 {
1530 inserted_space = TRUE;
1531#ifdef FEAT_CINDENT
1532 if (inindent(0))
1533 can_cindent = FALSE;
1534#endif
1535 if (Insstart_blank_vcol == MAXCOL
1536 && curwin->w_cursor.lnum == Insstart.lnum)
1537 Insstart_blank_vcol = get_nolist_virtcol();
1538 }
1539
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001540 /* Insert a normal character and check for abbreviations on a
1541 * special character. Let CTRL-] expand abbreviations without
1542 * inserting it. */
1543 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544#ifdef FEAT_MBYTE
1545 /* Add ABBR_OFF for characters above 0x100, this is
1546 * what check_abbr() expects. */
1547 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1548#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001549 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551 insert_special(c, FALSE, FALSE);
1552#ifdef FEAT_RIGHTLEFT
1553 revins_legal++;
1554 revins_chars++;
1555#endif
1556 }
1557
1558 auto_format(FALSE, TRUE);
1559
1560#ifdef FEAT_FOLDING
1561 /* When inserting a character the cursor line must never be in a
1562 * closed fold. */
1563 foldOpenCursor();
1564#endif
1565 break;
1566 } /* end of switch (c) */
1567
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001568#ifdef FEAT_AUTOCMD
1569 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001570 if (c != K_CURSORHOLD
1571# ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001572 /* but not in CTRL-X mode, a script can't restore the state */
1573 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar245c4102016-04-20 17:37:41 +02001574# endif
1575 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001576 did_cursorhold = FALSE;
1577#endif
1578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 /* If the cursor was moved we didn't just insert a space */
1580 if (arrow_used)
1581 inserted_space = FALSE;
1582
1583#ifdef FEAT_CINDENT
1584 if (can_cindent && cindent_on()
1585# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001586 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587# endif
1588 )
1589 {
1590force_cindent:
1591 /*
1592 * Indent now if a key was typed that is in 'cinkeys'.
1593 */
1594 if (in_cinkeys(c, ' ', line_is_white))
1595 {
1596 if (stop_arrow() == OK)
1597 /* re-indent the current line */
1598 do_c_expr_indent();
1599 }
1600 }
1601#endif /* FEAT_CINDENT */
1602
1603 } /* for (;;) */
1604 /* NOTREACHED */
1605}
1606
1607/*
1608 * Redraw for Insert mode.
1609 * This is postponed until getting the next character to make '$' in the 'cpo'
1610 * option work correctly.
1611 * Only redraw when there are no characters available. This speeds up
1612 * inserting sequences of characters (e.g., for CTRL-R).
1613 */
1614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001615ins_redraw(
1616 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001618#ifdef FEAT_CONCEAL
1619 linenr_T conceal_old_cursor_line = 0;
1620 linenr_T conceal_new_cursor_line = 0;
1621 int conceal_update_lines = FALSE;
1622#endif
1623
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624 if (char_avail())
1625 return;
1626
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001627#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001628 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1629 * visible, the command might delete it. */
1630 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001631# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001632 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001633# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001634# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001635 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001636# endif
1637# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001638 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001639# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001640 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001641# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001642 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001643# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001644# ifdef FEAT_INS_EXPAND
1645 && !pum_visible()
1646# endif
1647 )
1648 {
1649# ifdef FEAT_SYN_HL
1650 /* Need to update the screen first, to make sure syntax
1651 * highlighting is correct after making a change (e.g., inserting
1652 * a "(". The autocommand may also require a redraw, so it's done
1653 * again below, unfortunately. */
1654 if (syntax_present(curwin) && must_redraw)
1655 update_screen(0);
1656# endif
1657# ifdef FEAT_AUTOCMD
1658 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001659 {
1660 /* Make sure curswant is correct, an autocommand may call
1661 * getcurpos(). */
1662 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001663 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001664 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665# endif
1666# ifdef FEAT_CONCEAL
1667 if (curwin->w_p_cole > 0)
1668 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001669# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001670 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001671# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001672 conceal_new_cursor_line = curwin->w_cursor.lnum;
1673 conceal_update_lines = TRUE;
1674 }
1675# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001676# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001677 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001678# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001679 }
1680#endif
1681
1682#ifdef FEAT_AUTOCMD
1683 /* Trigger TextChangedI if b_changedtick differs. */
1684 if (ready && has_textchangedI()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001685 && last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001686# ifdef FEAT_INS_EXPAND
1687 && !pum_visible()
1688# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001689 )
1690 {
1691 if (last_changedtick_buf == curbuf)
1692 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1693 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001694 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001696#endif
1697
1698 if (must_redraw)
1699 update_screen(0);
1700 else if (clear_cmdline || redraw_cmdline)
1701 showmode(); /* clear cmdline and show mode */
1702# if defined(FEAT_CONCEAL)
1703 if ((conceal_update_lines
1704 && (conceal_old_cursor_line != conceal_new_cursor_line
1705 || conceal_cursor_line(curwin)))
1706 || need_cursor_line_redraw)
1707 {
1708 if (conceal_old_cursor_line != conceal_new_cursor_line)
1709 update_single_line(curwin, conceal_old_cursor_line);
1710 update_single_line(curwin, conceal_new_cursor_line == 0
1711 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1712 curwin->w_valid &= ~VALID_CROW;
1713 }
1714# endif
1715 showruler(FALSE);
1716 setcursor();
1717 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718}
1719
1720/*
1721 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1722 */
1723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001724ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001727 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
1729 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001730 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
1732 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001735 did_putchar = TRUE;
1736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1738
1739#ifdef FEAT_CMDL_INFO
1740 add_to_showcmd_c(Ctrl_V);
1741#endif
1742
1743 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001744 if (did_putchar)
1745 /* when the line fits in 'columns' the '^' is at the start of the next
1746 * line and will not removed by the redraw */
1747 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#ifdef FEAT_CMDL_INFO
1749 clear_showcmd();
1750#endif
1751 insert_special(c, FALSE, TRUE);
1752#ifdef FEAT_RIGHTLEFT
1753 revins_chars++;
1754 revins_legal++;
1755#endif
1756}
1757
1758/*
1759 * Put a character directly onto the screen. It's not stored in a buffer.
1760 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1761 */
1762static int pc_status;
1763#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1764#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1765#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1766#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768static int pc_attr;
1769static int pc_row;
1770static int pc_col;
1771
1772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001773edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 int attr;
1776
1777 if (ScreenLines != NULL)
1778 {
1779 update_topline(); /* just in case w_topline isn't valid */
1780 validate_cursor();
1781 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001782 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 else
1784 attr = 0;
1785 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001786 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1788 pc_status = PC_STATUS_UNSET;
1789#endif
1790#ifdef FEAT_RIGHTLEFT
1791 if (curwin->w_p_rl)
1792 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001793 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794# ifdef FEAT_MBYTE
1795 if (has_mbyte)
1796 {
1797 int fix_col = mb_fix_col(pc_col, pc_row);
1798
1799 if (fix_col != pc_col)
1800 {
1801 screen_putchar(' ', pc_row, fix_col, attr);
1802 --curwin->w_wcol;
1803 pc_status = PC_STATUS_RIGHT;
1804 }
1805 }
1806# endif
1807 }
1808 else
1809#endif
1810 {
1811 pc_col += curwin->w_wcol;
1812#ifdef FEAT_MBYTE
1813 if (mb_lefthalve(pc_row, pc_col))
1814 pc_status = PC_STATUS_LEFT;
1815#endif
1816 }
1817
1818 /* save the character to be able to put it back */
1819#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1820 if (pc_status == PC_STATUS_UNSET)
1821#endif
1822 {
1823 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1824 pc_status = PC_STATUS_SET;
1825 }
1826 screen_putchar(c, pc_row, pc_col, attr);
1827 }
1828}
1829
1830/*
1831 * Undo the previous edit_putchar().
1832 */
1833 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001834edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
1836 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1837 {
1838#if defined(FEAT_MBYTE)
1839 if (pc_status == PC_STATUS_RIGHT)
1840 ++curwin->w_wcol;
1841 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1842 redrawWinline(curwin->w_cursor.lnum, FALSE);
1843 else
1844#endif
1845 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1846 }
1847}
1848
1849/*
1850 * Called when p_dollar is set: display a '$' at the end of the changed text
1851 * Only works when cursor is in the line that changes.
1852 */
1853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001854display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
1856 colnr_T save_col;
1857
1858 if (!redrawing())
1859 return;
1860
1861 cursor_off();
1862 save_col = curwin->w_cursor.col;
1863 curwin->w_cursor.col = col;
1864#ifdef FEAT_MBYTE
1865 if (has_mbyte)
1866 {
1867 char_u *p;
1868
1869 /* If on the last byte of a multi-byte move to the first byte. */
1870 p = ml_get_curline();
1871 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1872 }
1873#endif
1874 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001875 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
1877 edit_putchar('$', FALSE);
1878 dollar_vcol = curwin->w_virtcol;
1879 }
1880 curwin->w_cursor.col = save_col;
1881}
1882
1883/*
1884 * Call this function before moving the cursor from the normal insert position
1885 * in insert mode.
1886 */
1887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001888undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001890 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001892 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 redrawWinline(curwin->w_cursor.lnum, FALSE);
1894 }
1895}
1896
1897/*
1898 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1899 * Keep the cursor on the same character.
1900 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1901 * type == INDENT_DEC decrease indent (for CTRL-D)
1902 * type == INDENT_SET set indent to "amount"
1903 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1904 */
1905 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001906change_indent(
1907 int type,
1908 int amount,
1909 int round,
1910 int replaced, /* replaced character, put on replace stack */
1911 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
1913 int vcol;
1914 int last_vcol;
1915 int insstart_less; /* reduction for Insstart.col */
1916 int new_cursor_col;
1917 int i;
1918 char_u *ptr;
1919 int save_p_list;
1920 int start_col;
1921 colnr_T vc;
1922#ifdef FEAT_VREPLACE
1923 colnr_T orig_col = 0; /* init for GCC */
1924 char_u *new_line, *orig_line = NULL; /* init for GCC */
1925
1926 /* VREPLACE mode needs to know what the line was like before changing */
1927 if (State & VREPLACE_FLAG)
1928 {
1929 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1930 orig_col = curwin->w_cursor.col;
1931 }
1932#endif
1933
1934 /* for the following tricks we don't want list mode */
1935 save_p_list = curwin->w_p_list;
1936 curwin->w_p_list = FALSE;
1937 vc = getvcol_nolist(&curwin->w_cursor);
1938 vcol = vc;
1939
1940 /*
1941 * For Replace mode we need to fix the replace stack later, which is only
1942 * possible when the cursor is in the indent. Remember the number of
1943 * characters before the cursor if it's possible.
1944 */
1945 start_col = curwin->w_cursor.col;
1946
1947 /* determine offset from first non-blank */
1948 new_cursor_col = curwin->w_cursor.col;
1949 beginline(BL_WHITE);
1950 new_cursor_col -= curwin->w_cursor.col;
1951
1952 insstart_less = curwin->w_cursor.col;
1953
1954 /*
1955 * If the cursor is in the indent, compute how many screen columns the
1956 * cursor is to the left of the first non-blank.
1957 */
1958 if (new_cursor_col < 0)
1959 vcol = get_indent() - vcol;
1960
1961 if (new_cursor_col > 0) /* can't fix replace stack */
1962 start_col = -1;
1963
1964 /*
1965 * Set the new indent. The cursor will be put on the first non-blank.
1966 */
1967 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001968 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 else
1970 {
1971#ifdef FEAT_VREPLACE
1972 int save_State = State;
1973
1974 /* Avoid being called recursively. */
1975 if (State & VREPLACE_FLAG)
1976 State = INSERT;
1977#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001978 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979#ifdef FEAT_VREPLACE
1980 State = save_State;
1981#endif
1982 }
1983 insstart_less -= curwin->w_cursor.col;
1984
1985 /*
1986 * Try to put cursor on same character.
1987 * If the cursor is at or after the first non-blank in the line,
1988 * compute the cursor column relative to the column of the first
1989 * non-blank character.
1990 * If we are not in insert mode, leave the cursor on the first non-blank.
1991 * If the cursor is before the first non-blank, position it relative
1992 * to the first non-blank, counted in screen columns.
1993 */
1994 if (new_cursor_col >= 0)
1995 {
1996 /*
1997 * When changing the indent while the cursor is touching it, reset
1998 * Insstart_col to 0.
1999 */
2000 if (new_cursor_col == 0)
2001 insstart_less = MAXCOL;
2002 new_cursor_col += curwin->w_cursor.col;
2003 }
2004 else if (!(State & INSERT))
2005 new_cursor_col = curwin->w_cursor.col;
2006 else
2007 {
2008 /*
2009 * Compute the screen column where the cursor should be.
2010 */
2011 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002012 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014 /*
2015 * Advance the cursor until we reach the right screen column.
2016 */
2017 vcol = last_vcol = 0;
2018 new_cursor_col = -1;
2019 ptr = ml_get_curline();
2020 while (vcol <= (int)curwin->w_virtcol)
2021 {
2022 last_vcol = vcol;
2023#ifdef FEAT_MBYTE
2024 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002025 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 else
2027#endif
2028 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002029 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
2031 vcol = last_vcol;
2032
2033 /*
2034 * May need to insert spaces to be able to position the cursor on
2035 * the right screen column.
2036 */
2037 if (vcol != (int)curwin->w_virtcol)
2038 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002039 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002041 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (ptr != NULL)
2043 {
2044 new_cursor_col += i;
2045 ptr[i] = NUL;
2046 while (--i >= 0)
2047 ptr[i] = ' ';
2048 ins_str(ptr);
2049 vim_free(ptr);
2050 }
2051 }
2052
2053 /*
2054 * When changing the indent while the cursor is in it, reset
2055 * Insstart_col to 0.
2056 */
2057 insstart_less = MAXCOL;
2058 }
2059
2060 curwin->w_p_list = save_p_list;
2061
2062 if (new_cursor_col <= 0)
2063 curwin->w_cursor.col = 0;
2064 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002065 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 curwin->w_set_curswant = TRUE;
2067 changed_cline_bef_curs();
2068
2069 /*
2070 * May have to adjust the start of the insert.
2071 */
2072 if (State & INSERT)
2073 {
2074 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2075 {
2076 if ((int)Insstart.col <= insstart_less)
2077 Insstart.col = 0;
2078 else
2079 Insstart.col -= insstart_less;
2080 }
2081 if ((int)ai_col <= insstart_less)
2082 ai_col = 0;
2083 else
2084 ai_col -= insstart_less;
2085 }
2086
2087 /*
2088 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2089 * If the number of characters before the cursor decreased, need to pop a
2090 * few characters from the replace stack.
2091 * If the number of characters before the cursor increased, need to push a
2092 * few NULs onto the replace stack.
2093 */
2094 if (REPLACE_NORMAL(State) && start_col >= 0)
2095 {
2096 while (start_col > (int)curwin->w_cursor.col)
2097 {
2098 replace_join(0); /* remove a NUL from the replace stack */
2099 --start_col;
2100 }
2101 while (start_col < (int)curwin->w_cursor.col || replaced)
2102 {
2103 replace_push(NUL);
2104 if (replaced)
2105 {
2106 replace_push(replaced);
2107 replaced = NUL;
2108 }
2109 ++start_col;
2110 }
2111 }
2112
2113#ifdef FEAT_VREPLACE
2114 /*
2115 * For VREPLACE mode, we also have to fix the replace stack. In this case
2116 * it is always possible because we backspace over the whole line and then
2117 * put it back again the way we wanted it.
2118 */
2119 if (State & VREPLACE_FLAG)
2120 {
2121 /* If orig_line didn't allocate, just return. At least we did the job,
2122 * even if you can't backspace. */
2123 if (orig_line == NULL)
2124 return;
2125
2126 /* Save new line */
2127 new_line = vim_strsave(ml_get_curline());
2128 if (new_line == NULL)
2129 return;
2130
2131 /* We only put back the new line up to the cursor */
2132 new_line[curwin->w_cursor.col] = NUL;
2133
2134 /* Put back original line */
2135 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2136 curwin->w_cursor.col = orig_col;
2137
2138 /* Backspace from cursor to start of line */
2139 backspace_until_column(0);
2140
2141 /* Insert new stuff into line again */
2142 ins_bytes(new_line);
2143
2144 vim_free(new_line);
2145 }
2146#endif
2147}
2148
2149/*
2150 * Truncate the space at the end of a line. This is to be used only in an
2151 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2152 * modes.
2153 */
2154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002155truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
2157 int i;
2158
2159 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002160 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
2162 if (State & REPLACE_FLAG)
2163 replace_join(0); /* remove a NUL from the replace stack */
2164 }
2165 line[i + 1] = NUL;
2166}
2167
2168#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2169 || defined(FEAT_COMMENTS) || defined(PROTO)
2170/*
2171 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2172 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002173 * Will attempt not to go before "col" even when there is a composing
2174 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 */
2176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002177backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
2179 while ((int)curwin->w_cursor.col > col)
2180 {
2181 curwin->w_cursor.col--;
2182 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002183 replace_do_bs(col);
2184 else if (!del_char_after_col(col))
2185 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187}
2188#endif
2189
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002190/*
2191 * Like del_char(), but make sure not to go before column "limit_col".
2192 * Only matters when there are composing characters.
2193 * Return TRUE when something was deleted.
2194 */
2195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002196del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002197{
2198#ifdef FEAT_MBYTE
2199 if (enc_utf8 && limit_col >= 0)
2200 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002201 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002202
2203 /* Make sure the cursor is at the start of a character, but
2204 * skip forward again when going too far back because of a
2205 * composing character. */
2206 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002207 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002208 {
2209 int l = utf_ptr2len(ml_get_cursor());
2210
2211 if (l == 0) /* end of line */
2212 break;
2213 curwin->w_cursor.col += l;
2214 }
2215 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2216 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002217 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002218 }
2219 else
2220#endif
2221 (void)del_char(FALSE);
2222 return TRUE;
2223}
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2226/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002227 * CTRL-X pressed in Insert mode.
2228 */
2229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002230ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002231{
2232 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2233 * CTRL-V works like CTRL-N */
2234 if (ctrl_x_mode != CTRL_X_CMDLINE)
2235 {
2236 /* if the next ^X<> won't ADD nothing, then reset
2237 * compl_cont_status */
2238 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002239 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002240 else
2241 compl_cont_status = 0;
2242 /* We're not sure which CTRL-X mode it will be yet */
2243 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2244 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2245 edit_submode_pre = NULL;
2246 showmode();
2247 }
2248}
2249
2250/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002251 * Whether other than default completion has been selected.
2252 */
2253 int
2254ctrl_x_mode_not_default(void)
2255{
2256 return ctrl_x_mode != CTRL_X_NORMAL;
2257}
2258
2259/*
2260 * Whether CTRL-X was typed without a following character.
2261 */
2262 int
2263ctrl_x_mode_not_defined_yet(void)
2264{
2265 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2266}
2267
2268/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002269 * Return TRUE if the 'dict' or 'tsr' option can be used.
2270 */
2271 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002272has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002273{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002274 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002275# ifdef FEAT_SPELL
2276 && !curwin->w_p_spell
2277# endif
2278 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002279 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2280 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002281 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002282 edit_submode = NULL;
2283 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2284 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002285 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002286 if (emsg_silent == 0)
2287 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002288 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002289 setcursor();
2290 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002291#ifdef FEAT_EVAL
2292 if (!get_vim_var_nr(VV_TESTING))
2293#endif
2294 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002295 }
2296 return FALSE;
2297 }
2298 return TRUE;
2299}
2300
2301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2303 * This depends on the current mode.
2304 */
2305 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002306vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 /* Always allow ^R - let it's results then be checked */
2309 if (c == Ctrl_R)
2310 return TRUE;
2311
Bram Moolenaare3226be2005-12-18 22:10:00 +00002312 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002313 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002314 return TRUE;
2315
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 switch (ctrl_x_mode)
2317 {
2318 case 0: /* Not in any CTRL-X mode */
2319 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2320 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002321 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2323 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2324 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002325 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002326 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 case CTRL_X_SCROLL:
2328 return (c == Ctrl_Y || c == Ctrl_E);
2329 case CTRL_X_WHOLE_LINE:
2330 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2331 case CTRL_X_FILES:
2332 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2333 case CTRL_X_DICTIONARY:
2334 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2335 case CTRL_X_THESAURUS:
2336 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2337 case CTRL_X_TAGS:
2338 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2339#ifdef FEAT_FIND_ID
2340 case CTRL_X_PATH_PATTERNS:
2341 return (c == Ctrl_P || c == Ctrl_N);
2342 case CTRL_X_PATH_DEFINES:
2343 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2344#endif
2345 case CTRL_X_CMDLINE:
2346 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2347 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002348#ifdef FEAT_COMPL_FUNC
2349 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002350 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002351 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002352 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002353#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002354 case CTRL_X_SPELL:
2355 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002356 case CTRL_X_EVAL:
2357 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002359 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 return FALSE;
2361}
2362
2363/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002364 * Return TRUE when character "c" is part of the item currently being
2365 * completed. Used to decide whether to abandon complete mode when the menu
2366 * is visible.
2367 */
2368 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002369ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002370{
2371 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2372 /* When expanding an identifier only accept identifier chars. */
2373 return vim_isIDc(c);
2374
2375 switch (ctrl_x_mode)
2376 {
2377 case CTRL_X_FILES:
2378 /* When expanding file name only accept file name chars. But not
2379 * path separators, so that "proto/<Tab>" expands files in
2380 * "proto", not "proto/" as a whole */
2381 return vim_isfilec(c) && !vim_ispathsep(c);
2382
2383 case CTRL_X_CMDLINE:
2384 case CTRL_X_OMNI:
2385 /* Command line and Omni completion can work with just about any
2386 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002387 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002388
2389 case CTRL_X_WHOLE_LINE:
2390 /* For while line completion a space can be part of the line. */
2391 return vim_isprintc(c);
2392 }
2393 return vim_iswordc(c);
2394}
2395
2396/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002397 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002399 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 * the rest of the word to be in -- webb
2401 */
2402 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002403ins_compl_add_infercase(
2404 char_u *str,
2405 int len,
2406 int icase,
2407 char_u *fname,
2408 int dir,
2409 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002411 char_u *p;
2412 int i, c;
2413 int actual_len; /* Take multi-byte characters */
2414 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002415 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002416 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 int has_lower = FALSE;
2418 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002420 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002422 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
Bram Moolenaara2993e12007-08-12 14:38:46 +00002424 /* Find actual length of completion. */
2425#ifdef FEAT_MBYTE
2426 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002428 p = str;
2429 actual_len = 0;
2430 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002432 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002433 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002436 else
2437#endif
2438 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
Bram Moolenaara2993e12007-08-12 14:38:46 +00002440 /* Find actual length of original text. */
2441#ifdef FEAT_MBYTE
2442 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002444 p = compl_orig_text;
2445 actual_compl_length = 0;
2446 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002448 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002449 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 }
2451 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002452 else
2453#endif
2454 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002456 /* "actual_len" may be smaller than "actual_compl_length" when using
2457 * thesaurus, only use the minimum when comparing. */
2458 min_len = actual_len < actual_compl_length
2459 ? actual_len : actual_compl_length;
2460
Bram Moolenaara2993e12007-08-12 14:38:46 +00002461 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002462 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002463 if (wca != NULL)
2464 {
2465 p = str;
2466 for (i = 0; i < actual_len; ++i)
2467#ifdef FEAT_MBYTE
2468 if (has_mbyte)
2469 wca[i] = mb_ptr2char_adv(&p);
2470 else
2471#endif
2472 wca[i] = *(p++);
2473
2474 /* Rule 1: Were any chars converted to lower? */
2475 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002476 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002477 {
2478#ifdef FEAT_MBYTE
2479 if (has_mbyte)
2480 c = mb_ptr2char_adv(&p);
2481 else
2482#endif
2483 c = *(p++);
2484 if (MB_ISLOWER(c))
2485 {
2486 has_lower = TRUE;
2487 if (MB_ISUPPER(wca[i]))
2488 {
2489 /* Rule 1 is satisfied. */
2490 for (i = actual_compl_length; i < actual_len; ++i)
2491 wca[i] = MB_TOLOWER(wca[i]);
2492 break;
2493 }
2494 }
2495 }
2496
2497 /*
2498 * Rule 2: No lower case, 2nd consecutive letter converted to
2499 * upper case.
2500 */
2501 if (!has_lower)
2502 {
2503 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002504 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002505 {
2506#ifdef FEAT_MBYTE
2507 if (has_mbyte)
2508 c = mb_ptr2char_adv(&p);
2509 else
2510#endif
2511 c = *(p++);
2512 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2513 {
2514 /* Rule 2 is satisfied. */
2515 for (i = actual_compl_length; i < actual_len; ++i)
2516 wca[i] = MB_TOUPPER(wca[i]);
2517 break;
2518 }
2519 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2520 }
2521 }
2522
2523 /* Copy the original case of the part we typed. */
2524 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002525 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002526 {
2527#ifdef FEAT_MBYTE
2528 if (has_mbyte)
2529 c = mb_ptr2char_adv(&p);
2530 else
2531#endif
2532 c = *(p++);
2533 if (MB_ISLOWER(c))
2534 wca[i] = MB_TOLOWER(wca[i]);
2535 else if (MB_ISUPPER(c))
2536 wca[i] = MB_TOUPPER(wca[i]);
2537 }
2538
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002539 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002540 * Generate encoding specific output from wide character array.
2541 * Multi-byte characters can occupy up to five bytes more than
2542 * ASCII characters, and we also need one byte for NUL, so stay
2543 * six bytes away from the edge of IObuff.
2544 */
2545 p = IObuff;
2546 i = 0;
2547 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2548#ifdef FEAT_MBYTE
2549 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002550 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002551 else
2552#endif
2553 *(p++) = wca[i++];
2554 *p = NUL;
2555
2556 vim_free(wca);
2557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002559 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2560 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002562 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563}
2564
2565/*
2566 * Add a match to the list of matches.
2567 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002568 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002569 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002571 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002572ins_compl_add(
2573 char_u *str,
2574 int len,
2575 int icase,
2576 char_u *fname,
2577 char_u **cptext, /* extra text for popup menu or NULL */
2578 int cdir,
2579 int flags,
2580 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002582 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002583 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585 ui_breakcheck();
2586 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002587 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (len < 0)
2589 len = (int)STRLEN(str);
2590
2591 /*
2592 * If the same match is already present, don't add it.
2593 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002594 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002596 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 do
2598 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002599 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002600 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002601 && match->cp_str[len] == NUL)
2602 return NOTDONE;
2603 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002604 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002607 /* Remove any popup menu before changing the list of matches. */
2608 ins_compl_del_pum();
2609
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 /*
2611 * Allocate a new match structure.
2612 * Copy the values to the new match structure.
2613 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002614 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002616 return FAIL;
2617 match->cp_number = -1;
2618 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002619 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002620 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 {
2622 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002623 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002625 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002626
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002628 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2630 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002631 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002632 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002633 && compl_curr_match->cp_fname != NULL
2634 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002635 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002636 else if (fname != NULL)
2637 {
2638 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002639 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002642 match->cp_fname = NULL;
2643 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002644
2645 if (cptext != NULL)
2646 {
2647 int i;
2648
2649 for (i = 0; i < CPT_COUNT; ++i)
2650 if (cptext[i] != NULL && *cptext[i] != NUL)
2651 match->cp_text[i] = vim_strsave(cptext[i]);
2652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654 /*
2655 * Link the new match structure in the list of matches.
2656 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002657 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002658 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 else if (dir == FORWARD)
2660 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002661 match->cp_next = compl_curr_match->cp_next;
2662 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
2664 else /* BACKWARD */
2665 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002666 match->cp_next = compl_curr_match;
2667 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002669 if (match->cp_next)
2670 match->cp_next->cp_prev = match;
2671 if (match->cp_prev)
2672 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002674 compl_first_match = match;
2675 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002677 /*
2678 * Find the longest common string if still doing that.
2679 */
2680 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2681 ins_compl_longest_match(match);
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 return OK;
2684}
2685
2686/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002687 * Return TRUE if "str[len]" matches with match->cp_str, considering
2688 * match->cp_icase.
2689 */
2690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002691ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002692{
2693 if (match->cp_icase)
2694 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2695 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2696}
2697
2698/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002699 * Reduce the longest common string for match "match".
2700 */
2701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002702ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002703{
2704 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002705 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002706 int had_match;
2707
2708 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002709 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002710 /* First match, use it as a whole. */
2711 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002712 if (compl_leader != NULL)
2713 {
2714 had_match = (curwin->w_cursor.col > compl_col);
2715 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002716 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002717 ins_redraw(FALSE);
2718
2719 /* When the match isn't there (to avoid matching itself) remove it
2720 * again after redrawing. */
2721 if (!had_match)
2722 ins_compl_delete();
2723 compl_used_match = FALSE;
2724 }
2725 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002726 else
2727 {
2728 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002729 p = compl_leader;
2730 s = match->cp_str;
2731 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002732 {
2733#ifdef FEAT_MBYTE
2734 if (has_mbyte)
2735 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002736 c1 = mb_ptr2char(p);
2737 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002738 }
2739 else
2740#endif
2741 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002742 c1 = *p;
2743 c2 = *s;
2744 }
2745 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2746 : (c1 != c2))
2747 break;
2748#ifdef FEAT_MBYTE
2749 if (has_mbyte)
2750 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002751 MB_PTR_ADV(p);
2752 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002753 }
2754 else
2755#endif
2756 {
2757 ++p;
2758 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002759 }
2760 }
2761
2762 if (*p != NUL)
2763 {
2764 /* Leader was shortened, need to change the inserted text. */
2765 *p = NUL;
2766 had_match = (curwin->w_cursor.col > compl_col);
2767 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002768 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002769 ins_redraw(FALSE);
2770
2771 /* When the match isn't there (to avoid matching itself) remove it
2772 * again after redrawing. */
2773 if (!had_match)
2774 ins_compl_delete();
2775 }
2776
2777 compl_used_match = FALSE;
2778 }
2779}
2780
2781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 * Add an array of matches to the list of matches.
2783 * Frees matches[].
2784 */
2785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002786ins_compl_add_matches(
2787 int num_matches,
2788 char_u **matches,
2789 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 int i;
2792 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002793 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
Bram Moolenaar572cb562005-08-05 21:35:02 +00002795 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002796 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002797 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002799 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 FreeWild(num_matches, matches);
2801}
2802
2803/* Make the completion list cyclic.
2804 * Return the number of matches (excluding the original).
2805 */
2806 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002807ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002809 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 int count = 0;
2811
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002812 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
2814 /*
2815 * Find the end of the list.
2816 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002817 match = compl_first_match;
2818 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002819 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002821 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 ++count;
2823 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002824 match->cp_next = compl_first_match;
2825 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827 return count;
2828}
2829
Bram Moolenaara94bc432006-03-10 21:42:59 +00002830/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002831 * Set variables that store noselect and noinsert behavior from the
2832 * 'completeopt' value.
2833 */
2834 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002835completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002836{
2837 compl_no_insert = FALSE;
2838 compl_no_select = FALSE;
2839 if (strstr((char *)p_cot, "noselect") != NULL)
2840 compl_no_select = TRUE;
2841 if (strstr((char *)p_cot, "noinsert") != NULL)
2842 compl_no_insert = TRUE;
2843}
2844
2845/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002846 * Start completion for the complete() function.
2847 * "startcol" is where the matched text starts (1 is first column).
2848 * "list" is the list of matches.
2849 */
2850 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002851set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002852{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002853 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002854 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002855
Bram Moolenaara94bc432006-03-10 21:42:59 +00002856 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002857 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002858 ins_compl_prep(' ');
2859 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002860 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002861
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002862 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002863 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002864 startcol = curwin->w_cursor.col;
2865 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002866 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002867 /* compl_pattern doesn't need to be set */
2868 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2869 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002870 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002871 return;
2872
Bram Moolenaare4214502015-03-05 18:08:43 +01002873 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002874
2875 ins_compl_add_list(list);
2876 compl_matches = ins_compl_make_cyclic();
2877 compl_started = TRUE;
2878 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002879 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002880
2881 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002882 if (compl_no_insert || compl_no_select)
2883 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002884 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002885 if (compl_no_select)
2886 /* Down/Up has no real effect. */
2887 ins_complete(K_UP, FALSE);
2888 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002889 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002890 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002891 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002892
2893 /* Lazily show the popup menu, unless we got interrupted. */
2894 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002895 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002896 out_flush();
2897}
2898
2899
Bram Moolenaar9372a112005-12-06 19:59:18 +00002900/* "compl_match_array" points the currently displayed list of entries in the
2901 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002902static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002903static int compl_match_arraysize;
2904
2905/*
2906 * Update the screen and when there is any scrolling remove the popup menu.
2907 */
2908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002909ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002910{
2911 int h;
2912
2913 if (compl_match_array != NULL)
2914 {
2915 h = curwin->w_cline_height;
2916 update_screen(0);
2917 if (h != curwin->w_cline_height)
2918 ins_compl_del_pum();
2919 }
2920}
2921
2922/*
2923 * Remove any popup menu.
2924 */
2925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002926ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002927{
2928 if (compl_match_array != NULL)
2929 {
2930 pum_undisplay();
Bram Moolenaar15675582018-02-09 12:29:56 +01002931 vim_clear((void **)&compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002932 }
2933}
2934
2935/*
2936 * Return TRUE if the popup menu should be displayed.
2937 */
2938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002939pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002940{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002941 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002942 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002943 return FALSE;
2944
2945 /* The display looks bad on a B&W display. */
2946 if (t_colors < 8
2947#ifdef FEAT_GUI
2948 && !gui.in_use
2949#endif
2950 )
2951 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002952 return TRUE;
2953}
2954
2955/*
2956 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002957 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002958 */
2959 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002960pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002961{
2962 compl_T *compl;
2963 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002964
2965 /* Don't display the popup menu if there are no matches or there is only
2966 * one (ignoring the original text). */
2967 compl = compl_first_match;
2968 i = 0;
2969 do
2970 {
2971 if (compl == NULL
2972 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2973 break;
2974 compl = compl->cp_next;
2975 } while (compl != compl_first_match);
2976
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002977 if (strstr((char *)p_cot, "menuone") != NULL)
2978 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002979 return (i >= 2);
2980}
2981
2982/*
2983 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002984 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002985 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002986 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002987ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002988{
2989 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002990 compl_T *shown_compl = NULL;
2991 int did_find_shown_match = FALSE;
2992 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002993 int i;
2994 int cur = -1;
2995 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002996 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002997
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002998 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002999 return;
3000
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003001#if defined(FEAT_EVAL)
3002 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3003 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3004#endif
3005
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003006 /* Update the screen before drawing the popup menu over it. */
3007 update_screen(0);
3008
3009 if (compl_match_array == NULL)
3010 {
3011 /* Need to build the popup menu list. */
3012 compl_match_arraysize = 0;
3013 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003014 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003015 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003016 do
3017 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003018 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3019 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003020 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003021 ++compl_match_arraysize;
3022 compl = compl->cp_next;
3023 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003024 if (compl_match_arraysize == 0)
3025 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003026 compl_match_array = (pumitem_T *)alloc_clear(
3027 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003028 * compl_match_arraysize));
3029 if (compl_match_array != NULL)
3030 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003031 /* If the current match is the original text don't find the first
3032 * match after it, don't highlight anything. */
3033 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3034 shown_match_ok = TRUE;
3035
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003036 i = 0;
3037 compl = compl_first_match;
3038 do
3039 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003040 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3041 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003042 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003043 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003044 if (!shown_match_ok)
3045 {
3046 if (compl == compl_shown_match || did_find_shown_match)
3047 {
3048 /* This item is the shown match or this is the
3049 * first displayed item after the shown match. */
3050 compl_shown_match = compl;
3051 did_find_shown_match = TRUE;
3052 shown_match_ok = TRUE;
3053 }
3054 else
3055 /* Remember this displayed match for when the
3056 * shown match is just below it. */
3057 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003058 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003059 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003060
3061 if (compl->cp_text[CPT_ABBR] != NULL)
3062 compl_match_array[i].pum_text =
3063 compl->cp_text[CPT_ABBR];
3064 else
3065 compl_match_array[i].pum_text = compl->cp_str;
3066 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3067 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3068 if (compl->cp_text[CPT_MENU] != NULL)
3069 compl_match_array[i++].pum_extra =
3070 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003071 else
3072 compl_match_array[i++].pum_extra = compl->cp_fname;
3073 }
3074
3075 if (compl == compl_shown_match)
3076 {
3077 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003078
3079 /* When the original text is the shown match don't set
3080 * compl_shown_match. */
3081 if (compl->cp_flags & ORIGINAL_TEXT)
3082 shown_match_ok = TRUE;
3083
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003084 if (!shown_match_ok && shown_compl != NULL)
3085 {
3086 /* The shown match isn't displayed, set it to the
3087 * previously displayed match. */
3088 compl_shown_match = shown_compl;
3089 shown_match_ok = TRUE;
3090 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003091 }
3092 compl = compl->cp_next;
3093 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003094
3095 if (!shown_match_ok) /* no displayed match at all */
3096 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003097 }
3098 }
3099 else
3100 {
3101 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003102 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003103 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3104 || compl_match_array[i].pum_text
3105 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003106 {
3107 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003108 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003109 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003110 }
3111
3112 if (compl_match_array != NULL)
3113 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003114 /* In Replace mode when a $ is displayed at the end of the line only
3115 * part of the screen would be updated. We do need to redraw here. */
3116 dollar_vcol = -1;
3117
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003118 /* Compute the screen column of the start of the completed text.
3119 * Use the cursor to get all wrapping and other settings right. */
3120 col = curwin->w_cursor.col;
3121 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003122 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003123 curwin->w_cursor.col = col;
3124 }
3125}
3126
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#define DICT_FIRST (1) /* use just first element in "dict" */
3128#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003129
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003131 * Add any identifiers that match the given pattern in the list of dictionary
3132 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 */
3134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003135ins_compl_dictionaries(
3136 char_u *dict_start,
3137 char_u *pat,
3138 int flags, /* DICT_FIRST and/or DICT_EXACT */
3139 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003141 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 char_u *ptr;
3143 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 char_u **files;
3146 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003148 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
Bram Moolenaar0b238792006-03-02 22:49:12 +00003150 if (*dict == NUL)
3151 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003152#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003153 /* When 'dictionary' is empty and spell checking is enabled use
3154 * "spell". */
3155 if (!thesaurus && curwin->w_p_spell)
3156 dict = (char_u *)"spell";
3157 else
3158#endif
3159 return;
3160 }
3161
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003163 if (buf == NULL)
3164 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003165 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003166
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 /* If 'infercase' is set, don't use 'smartcase' here */
3168 save_p_scs = p_scs;
3169 if (curbuf->b_p_inf)
3170 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003171
3172 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3173 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003174 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003175 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003176 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003177 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003178 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003179
3180 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003181 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003182 len = STRLEN(pat_esc) + 10;
3183 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003184 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003185 {
3186 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003187 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003188 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003189 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003190 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3191 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003192 vim_free(ptr);
3193 }
3194 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003195 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003196 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003197 if (regmatch.regprog == NULL)
3198 goto theend;
3199 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3202 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003203 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
3205 /* copy one dictionary file name into buf */
3206 if (flags == DICT_EXACT)
3207 {
3208 count = 1;
3209 files = &dict;
3210 }
3211 else
3212 {
3213 /* Expand wildcards in the dictionary name, but do not allow
3214 * backticks (for security, the 'dict' option may have been set in
3215 * a modeline). */
3216 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003217# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003218 if (!thesaurus && STRCMP(buf, "spell") == 0)
3219 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003220 else
3221# endif
3222 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 || expand_wildcards(1, &buf, &count, &files,
3224 EW_FILE|EW_SILENT) != OK)
3225 count = 0;
3226 }
3227
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003228# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003229 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003231 /* Complete from active spelling. Skip "\<" in the pattern, we
3232 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003233 if (pat[0] == '\\' && pat[1] == '<')
3234 ptr = pat + 2;
3235 else
3236 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003237 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003239 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003240# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003241 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003242 {
3243 ins_compl_files(count, files, thesaurus, flags,
3244 &regmatch, buf, &dir);
3245 if (flags != DICT_EXACT)
3246 FreeWild(count, files);
3247 }
3248 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 break;
3250 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003251
3252theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003254 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 vim_free(buf);
3256}
3257
Bram Moolenaar0b238792006-03-02 22:49:12 +00003258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003259ins_compl_files(
3260 int count,
3261 char_u **files,
3262 int thesaurus,
3263 int flags,
3264 regmatch_T *regmatch,
3265 char_u *buf,
3266 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003267{
3268 char_u *ptr;
3269 int i;
3270 FILE *fp;
3271 int add_r;
3272
3273 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3274 {
3275 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3276 if (flags != DICT_EXACT)
3277 {
3278 vim_snprintf((char *)IObuff, IOSIZE,
3279 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003280 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003281 }
3282
3283 if (fp != NULL)
3284 {
3285 /*
3286 * Read dictionary file line by line.
3287 * Check each line for a match.
3288 */
3289 while (!got_int && !compl_interrupted
3290 && !vim_fgets(buf, LSIZE, fp))
3291 {
3292 ptr = buf;
3293 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3294 {
3295 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003296 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003297 ptr = find_line_end(ptr);
3298 else
3299 ptr = find_word_end(ptr);
3300 add_r = ins_compl_add_infercase(regmatch->startp[0],
3301 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003302 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003303 if (thesaurus)
3304 {
3305 char_u *wstart;
3306
3307 /*
3308 * Add the other matches on the line
3309 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003310 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003311 while (!got_int)
3312 {
3313 /* Find start of the next word. Skip white
3314 * space and punctuation. */
3315 ptr = find_word_start(ptr);
3316 if (*ptr == NUL || *ptr == NL)
3317 break;
3318 wstart = ptr;
3319
Bram Moolenaara2993e12007-08-12 14:38:46 +00003320 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003321#ifdef FEAT_MBYTE
3322 if (has_mbyte)
3323 /* Japanese words may have characters in
3324 * different classes, only separate words
3325 * with single-byte non-word characters. */
3326 while (*ptr != NUL)
3327 {
3328 int l = (*mb_ptr2len)(ptr);
3329
3330 if (l < 2 && !vim_iswordc(*ptr))
3331 break;
3332 ptr += l;
3333 }
3334 else
3335#endif
3336 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003337
3338 /* Add the word. Skip the regexp match. */
3339 if (wstart != regmatch->startp[0])
3340 add_r = ins_compl_add_infercase(wstart,
3341 (int)(ptr - wstart),
3342 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003343 }
3344 }
3345 if (add_r == OK)
3346 /* if dir was BACKWARD then honor it just once */
3347 *dir = FORWARD;
3348 else if (add_r == FAIL)
3349 break;
3350 /* avoid expensive call to vim_regexec() when at end
3351 * of line */
3352 if (*ptr == '\n' || got_int)
3353 break;
3354 }
3355 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003356 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003357 }
3358 fclose(fp);
3359 }
3360 }
3361}
3362
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363/*
3364 * Find the start of the next word.
3365 * Returns a pointer to the first char of the word. Also stops at a NUL.
3366 */
3367 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003368find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370#ifdef FEAT_MBYTE
3371 if (has_mbyte)
3372 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003373 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 else
3375#endif
3376 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3377 ++ptr;
3378 return ptr;
3379}
3380
3381/*
3382 * Find the end of the word. Assumes it starts inside a word.
3383 * Returns a pointer to just after the word.
3384 */
3385 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003386find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388#ifdef FEAT_MBYTE
3389 int start_class;
3390
3391 if (has_mbyte)
3392 {
3393 start_class = mb_get_class(ptr);
3394 if (start_class > 1)
3395 while (*ptr != NUL)
3396 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003397 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (mb_get_class(ptr) != start_class)
3399 break;
3400 }
3401 }
3402 else
3403#endif
3404 while (vim_iswordc(*ptr))
3405 ++ptr;
3406 return ptr;
3407}
3408
3409/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003410 * Find the end of the line, omitting CR and NL at the end.
3411 * Returns a pointer to just after the line.
3412 */
3413 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003414find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003415{
3416 char_u *s;
3417
3418 s = ptr + STRLEN(ptr);
3419 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3420 --s;
3421 return s;
3422}
3423
3424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 * Free the list of completions
3426 */
3427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003428ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003430 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003431 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
Bram Moolenaar15675582018-02-09 12:29:56 +01003433 vim_clear((void **)&compl_pattern);
3434 vim_clear((void **)&compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003436 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003438
3439 ins_compl_del_pum();
3440 pum_clear();
3441
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003442 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 do
3444 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003445 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003446 compl_curr_match = compl_curr_match->cp_next;
3447 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003449 if (match->cp_flags & FREE_FNAME)
3450 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003451 for (i = 0; i < CPT_COUNT; ++i)
3452 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003454 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3455 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003456 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003457 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458}
3459
3460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003461ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003463 compl_cont_status = 0;
3464 compl_started = FALSE;
3465 compl_matches = 0;
Bram Moolenaar15675582018-02-09 12:29:56 +01003466 vim_clear((void **)&compl_pattern);
3467 vim_clear((void **)&compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 edit_submode_extra = NULL;
Bram Moolenaar15675582018-02-09 12:29:56 +01003469 vim_clear((void **)&compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003470 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003471 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003472 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473}
3474
3475/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003476 * Return TRUE when Insert completion is active.
3477 */
3478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003479ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003480{
3481 return compl_started;
3482}
3483
3484/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003485 * Delete one character before the cursor and show the subset of the matches
3486 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003487 * Returns the character to be used, NUL if the work is done and another char
3488 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003489 */
3490 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003491ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003492{
3493 char_u *line;
3494 char_u *p;
3495
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003496 line = ml_get_curline();
3497 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003498 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003499
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003500 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003501 * allow the word to be deleted, we won't match everything.
3502 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003503 if ((int)(p - line) - (int)compl_col < 0
3504 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003505 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3506 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3507 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003508 return K_BS;
3509
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003510 /* Deleted more than what was used to find matches or didn't finish
3511 * finding all matches: need to look for matches all over again. */
3512 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003513 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003514 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003515
Bram Moolenaara6557602006-02-04 22:43:20 +00003516 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003517 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003518 if (compl_leader != NULL)
3519 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003520 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003521 if (compl_shown_match != NULL)
3522 /* Make sure current match is not a hidden item. */
3523 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003524 return NUL;
3525 }
3526 return K_BS;
3527}
Bram Moolenaara6557602006-02-04 22:43:20 +00003528
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003529/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003530 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3531 * be called.
3532 */
3533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003534ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003535{
3536 /* Return TRUE if we didn't complete finding matches or when the
3537 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3538 return compl_was_interrupted
3539 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3540 && compl_opt_refresh_always);
3541}
3542
3543/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003544 * Called after changing "compl_leader".
3545 * Show the popup menu with a different set of matches.
3546 * May also search for matches again if the previous search was interrupted.
3547 */
3548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003549ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003550{
3551 ins_compl_del_pum();
3552 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003553 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003554 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003555
3556 if (compl_started)
3557 ins_compl_set_original_text(compl_leader);
3558 else
3559 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003560#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003561 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003562#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003563 /*
3564 * Matches were cleared, need to search for them now. First display
3565 * the changed text before the cursor. Set "compl_restarting" to
3566 * avoid that the first match is inserted.
3567 */
3568 update_screen(0);
3569#ifdef FEAT_GUI
3570 if (gui.in_use)
3571 {
3572 /* Show the cursor after the match, not after the redrawn text. */
3573 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003574 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003575 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003576#endif
3577 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003578 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003579 compl_cont_status = 0;
3580 compl_restarting = FALSE;
3581 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003582
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003583 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003584
3585 /* Show the popup menu with a different set of matches. */
3586 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003587
3588 /* Don't let Enter select the original text when there is no popup menu. */
3589 if (compl_match_array == NULL)
3590 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003591}
3592
3593/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003594 * Return the length of the completion, from the completion start column to
3595 * the cursor column. Making sure it never goes below zero.
3596 */
3597 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003598ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003599{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003600 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003601
3602 if (off < 0)
3603 return 0;
3604 return off;
3605}
3606
3607/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003608 * Append one character to the match leader. May reduce the number of
3609 * matches.
3610 */
3611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003612ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003613{
3614#ifdef FEAT_MBYTE
3615 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003616#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003617
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003618 if (stop_arrow() == FAIL)
3619 return;
3620#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003621 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3622 {
3623 char_u buf[MB_MAXBYTES + 1];
3624
3625 (*mb_char2bytes)(c, buf);
3626 buf[cc] = NUL;
3627 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003628 if (compl_opt_refresh_always)
3629 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003630 }
3631 else
3632#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003633 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003634 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003635 if (compl_opt_refresh_always)
3636 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003637 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003638
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003639 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003640 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003641 ins_compl_restart();
3642
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003643 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003644 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003645 * break redo. */
3646 if (!compl_opt_refresh_always)
3647 {
3648 vim_free(compl_leader);
3649 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003650 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003651 if (compl_leader != NULL)
3652 ins_compl_new_leader();
3653 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003654}
3655
3656/*
3657 * Setup for finding completions again without leaving CTRL-X mode. Used when
3658 * BS or a key was typed while still searching for matches.
3659 */
3660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003661ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003662{
3663 ins_compl_free();
3664 compl_started = FALSE;
3665 compl_matches = 0;
3666 compl_cont_status = 0;
3667 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003668}
3669
3670/*
3671 * Set the first match, the original text.
3672 */
3673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003674ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003675{
3676 char_u *p;
3677
3678 /* Replace the original text entry. */
3679 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3680 {
3681 p = vim_strsave(str);
3682 if (p != NULL)
3683 {
3684 vim_free(compl_first_match->cp_str);
3685 compl_first_match->cp_str = p;
3686 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003687 }
3688}
3689
3690/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003691 * Append one character to the match leader. May reduce the number of
3692 * matches.
3693 */
3694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003695ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003696{
3697 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003698 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003699 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003700 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003701
3702 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003703 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003704 {
3705 /* When still at the original match use the first entry that matches
3706 * the leader. */
3707 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3708 {
3709 p = NULL;
3710 for (cp = compl_shown_match->cp_next; cp != NULL
3711 && cp != compl_first_match; cp = cp->cp_next)
3712 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003713 if (compl_leader == NULL
3714 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003715 (int)STRLEN(compl_leader)))
3716 {
3717 p = cp->cp_str;
3718 break;
3719 }
3720 }
3721 if (p == NULL || (int)STRLEN(p) <= len)
3722 return;
3723 }
3724 else
3725 return;
3726 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003727 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003728 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003729 ins_compl_addleader(c);
3730}
3731
3732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003734 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003735 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003737 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003738ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
3740 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003742 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
3744 /* Forget any previous 'special' messages if this is actually
3745 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3746 */
3747 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3748 edit_submode_extra = NULL;
3749
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003750 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003751 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3752 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003753 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003755 /* Set "compl_get_longest" when finding the first matches. */
3756 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003757 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003758 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003759 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003760 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003761
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003762 }
3763
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3765 {
3766 /*
3767 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3768 * it will be yet. Now we decide.
3769 */
3770 switch (c)
3771 {
3772 case Ctrl_E:
3773 case Ctrl_Y:
3774 ctrl_x_mode = CTRL_X_SCROLL;
3775 if (!(State & REPLACE_FLAG))
3776 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3777 else
3778 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3779 edit_submode_pre = NULL;
3780 showmode();
3781 break;
3782 case Ctrl_L:
3783 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3784 break;
3785 case Ctrl_F:
3786 ctrl_x_mode = CTRL_X_FILES;
3787 break;
3788 case Ctrl_K:
3789 ctrl_x_mode = CTRL_X_DICTIONARY;
3790 break;
3791 case Ctrl_R:
3792 /* Simply allow ^R to happen without affecting ^X mode */
3793 break;
3794 case Ctrl_T:
3795 ctrl_x_mode = CTRL_X_THESAURUS;
3796 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003797#ifdef FEAT_COMPL_FUNC
3798 case Ctrl_U:
3799 ctrl_x_mode = CTRL_X_FUNCTION;
3800 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003801 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003802 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003803 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003804#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003805 case 's':
3806 case Ctrl_S:
3807 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003808#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003809 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003810 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003811 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003812#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003813 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 case Ctrl_RSB:
3815 ctrl_x_mode = CTRL_X_TAGS;
3816 break;
3817#ifdef FEAT_FIND_ID
3818 case Ctrl_I:
3819 case K_S_TAB:
3820 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3821 break;
3822 case Ctrl_D:
3823 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3824 break;
3825#endif
3826 case Ctrl_V:
3827 case Ctrl_Q:
3828 ctrl_x_mode = CTRL_X_CMDLINE;
3829 break;
3830 case Ctrl_P:
3831 case Ctrl_N:
3832 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3833 * just started ^X mode, or there were enough ^X's to cancel
3834 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3835 * do normal expansion when interrupting a different mode (say
3836 * ^X^F^X^P or ^P^X^X^P, see below)
3837 * nothing changes if interrupting mode 0, (eg, the flag
3838 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003839 if (!(compl_cont_status & CONT_INTRPT))
3840 compl_cont_status |= CONT_LOCAL;
3841 else if (compl_cont_mode != 0)
3842 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 /* FALLTHROUGH */
3844 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003845 /* If we have typed at least 2 ^X's... for modes != 0, we set
3846 * compl_cont_status = 0 (eg, as if we had just started ^X
3847 * mode).
3848 * For mode 0, we set "compl_cont_mode" to an impossible
3849 * value, in both cases ^X^X can be used to restart the same
3850 * mode (avoiding ADDING mode).
3851 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3852 * 'complete' and local ^P expansions respectively.
3853 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3854 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (c == Ctrl_X)
3856 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003857 if (compl_cont_mode != 0)
3858 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003860 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003862 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 edit_submode = NULL;
3864 showmode();
3865 break;
3866 }
3867 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003868 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
3870 /* We're already in CTRL-X mode, do we stay in it? */
3871 if (!vim_is_ctrl_x_key(c))
3872 {
3873 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003874 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 else
3876 ctrl_x_mode = CTRL_X_FINISHED;
3877 edit_submode = NULL;
3878 }
3879 showmode();
3880 }
3881
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003882 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
3884 /* Show error message from attempted keyword completion (probably
3885 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003886 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003888 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3889 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 || ctrl_x_mode == CTRL_X_FINISHED)
3891 {
3892 /* Get here when we have finished typing a sequence of ^N and
3893 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003894 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003895 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
3897 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003898 * If any of the original typed text has been changed, eg when
3899 * ignorecase is set, we must add back-spaces to the redo
3900 * buffer. We add as few as necessary to delete just the part
3901 * of the original text that has changed.
3902 * When using the longest match, edited the match or used
3903 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003905 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3906 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003907 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003908 ptr = NULL;
3909 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911
3912#ifdef FEAT_CINDENT
3913 want_cindent = (can_cindent && cindent_on());
3914#endif
3915 /*
3916 * When completing whole lines: fix indent for 'cindent'.
3917 * Otherwise, break line if it's too long.
3918 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003919 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
3921#ifdef FEAT_CINDENT
3922 /* re-indent the current line */
3923 if (want_cindent)
3924 {
3925 do_c_expr_indent();
3926 want_cindent = FALSE; /* don't do it again */
3927 }
3928#endif
3929 }
3930 else
3931 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003932 int prev_col = curwin->w_cursor.col;
3933
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003935 if (prev_col > 0)
3936 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003937 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003938 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003940 if (prev_col > 0
3941 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3942 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003945 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003946 * the selection without inserting anything. When
3947 * compl_enter_selects is set the Enter key does the same. */
3948 if ((c == Ctrl_Y || (compl_enter_selects
3949 && (c == CAR || c == K_KENTER || c == NL)))
3950 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003951 retval = TRUE;
3952
Bram Moolenaar47247282016-08-02 22:36:02 +02003953 /* CTRL-E means completion is Ended, go back to the typed text.
3954 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003955 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003956 {
3957 ins_compl_delete();
3958 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003959 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003960 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003961 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003962 retval = TRUE;
3963 }
3964
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003965 auto_format(FALSE, TRUE);
3966
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003968 compl_started = FALSE;
3969 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003970 if (!shortmess(SHM_COMPLETIONMENU))
3971 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003972 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003973 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 if (edit_submode != NULL)
3975 {
3976 edit_submode = NULL;
3977 showmode();
3978 }
3979
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003980#ifdef FEAT_CMDWIN
3981 if (c == Ctrl_C && cmdwin_type != 0)
3982 /* Avoid the popup menu remains displayed when leaving the
3983 * command line window. */
3984 update_screen(0);
3985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986#ifdef FEAT_CINDENT
3987 /*
3988 * Indent now if a key was typed that is in 'cinkeys'.
3989 */
3990 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3991 do_c_expr_indent();
3992#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003993#ifdef FEAT_AUTOCMD
3994 /* Trigger the CompleteDone event to give scripts a chance to act
3995 * upon the completion. */
3996 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004000#ifdef FEAT_AUTOCMD
4001 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4002 /* Trigger the CompleteDone event to give scripts a chance to act
4003 * upon the (possibly failed) completion. */
4004 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
4005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007 /* reset continue_* if we left expansion-mode, if we stay they'll be
4008 * (re)set properly in ins_complete() */
4009 if (!vim_is_ctrl_x_key(c))
4010 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004011 compl_cont_status = 0;
4012 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004014
4015 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016}
4017
4018/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004019 * Fix the redo buffer for the completion leader replacing some of the typed
4020 * text. This inserts backspaces and appends the changed text.
4021 * "ptr" is the known leader text or NUL.
4022 */
4023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004024ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004025{
4026 int len;
4027 char_u *p;
4028 char_u *ptr = ptr_arg;
4029
4030 if (ptr == NULL)
4031 {
4032 if (compl_leader != NULL)
4033 ptr = compl_leader;
4034 else
4035 return; /* nothing to do */
4036 }
4037 if (compl_orig_text != NULL)
4038 {
4039 p = compl_orig_text;
4040 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4041 ;
4042#ifdef FEAT_MBYTE
4043 if (len > 0)
4044 len -= (*mb_head_off)(p, p + len);
4045#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004046 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004047 AppendCharToRedobuff(K_BS);
4048 }
4049 else
4050 len = 0;
4051 if (ptr != NULL)
4052 AppendToRedobuffLit(ptr + len, -1);
4053}
4054
4055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4057 * (depending on flag) starting from buf and looking for a non-scanned
4058 * buffer (other than curbuf). curbuf is special, if it is called with
4059 * buf=curbuf then it has to be the first call for a given flag/expansion.
4060 *
4061 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4062 */
4063 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004064ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068 if (flag == 'w') /* just windows */
4069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if (buf == curbuf) /* first call for this flag/expansion */
4071 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004072 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 && wp->w_buffer->b_scanned)
4074 ;
4075 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077 else
4078 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4079 * (unlisted buffers)
4080 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004081 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 && ((flag == 'U'
4083 ? buf->b_p_bl
4084 : (!buf->b_p_bl
4085 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004086 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 ;
4088 return buf;
4089}
4090
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004091#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004092/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004093 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004094 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004095 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004097expand_by_function(
4098 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4099 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004100{
Bram Moolenaar82139082011-09-14 16:52:09 +02004101 list_T *matchlist = NULL;
4102 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004103 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004104 char_u *funcname;
4105 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004106 win_T *curwin_save;
4107 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004108 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004109
Bram Moolenaare344bea2005-09-01 20:46:49 +00004110 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4111 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004112 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004113
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004114 /* Call 'completefunc' to obtain the list of matches. */
4115 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004116 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004117
Bram Moolenaare344bea2005-09-01 20:46:49 +00004118 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004119 curwin_save = curwin;
4120 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004121
4122 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004123 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004124 {
4125 switch (rettv.v_type)
4126 {
4127 case VAR_LIST:
4128 matchlist = rettv.vval.v_list;
4129 break;
4130 case VAR_DICT:
4131 matchdict = rettv.vval.v_dict;
4132 break;
4133 default:
4134 /* TODO: Give error message? */
4135 clear_tv(&rettv);
4136 break;
4137 }
4138 }
4139
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004140 if (curwin_save != curwin || curbuf_save != curbuf)
4141 {
4142 EMSG(_(e_complwin));
4143 goto theend;
4144 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004145 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004146 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004147 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004148 {
4149 EMSG(_(e_compldel));
4150 goto theend;
4151 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004152
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004153 if (matchlist != NULL)
4154 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004155 else if (matchdict != NULL)
4156 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004157
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004158theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004159 if (matchdict != NULL)
4160 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004161 if (matchlist != NULL)
4162 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004163}
4164#endif /* FEAT_COMPL_FUNC */
4165
Bram Moolenaar39f05632006-03-19 22:15:26 +00004166#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004167/*
4168 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004169 */
4170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004171ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004172{
4173 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004174 int dir = compl_direction;
4175
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004176 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004177 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004178 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004179 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4180 /* if dir was BACKWARD then honor it just once */
4181 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004182 else if (did_emsg)
4183 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004184 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004185}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004186
4187/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004188 * Add completions from a dict.
4189 */
4190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004191ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004192{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004193 dictitem_T *di_refresh;
4194 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004195
4196 /* Check for optional "refresh" item. */
4197 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004198 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4199 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004200 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004201 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004202
4203 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4204 compl_opt_refresh_always = TRUE;
4205 }
4206
4207 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004208 di_words = dict_find(dict, (char_u *)"words", 5);
4209 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4210 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004211}
4212
4213/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004214 * Add a match to the list of matches from a typeval_T.
4215 * If the given string is already in the list of completions, then return
4216 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4217 * maybe because alloc() returns NULL, then FAIL is returned.
4218 */
4219 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004220ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004221{
4222 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004223 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004224 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004225 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004226 char_u *(cptext[CPT_COUNT]);
4227
4228 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4229 {
4230 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4231 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4232 (char_u *)"abbr", FALSE);
4233 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4234 (char_u *)"menu", FALSE);
4235 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4236 (char_u *)"kind", FALSE);
4237 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4238 (char_u *)"info", FALSE);
4239 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4240 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004241 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004242 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004243 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4244 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004245 }
4246 else
4247 {
4248 word = get_tv_string_chk(tv);
4249 vim_memset(cptext, 0, sizeof(cptext));
4250 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004251 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004252 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004253 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004254}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004255#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004257/*
4258 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004259 * The search starts at position "ini" in curbuf and in the direction
4260 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004261 * When "compl_started" is FALSE start at that position, otherwise continue
4262 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004263 * This may return before finding all the matches.
4264 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 */
4266 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004267ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268{
4269 static pos_T first_match_pos;
4270 static pos_T last_match_pos;
4271 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 static int found_all = FALSE; /* Found all matches of a
4273 certain type. */
4274 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
Bram Moolenaar572cb562005-08-05 21:35:02 +00004276 pos_T *pos;
4277 char_u **matches;
4278 int save_p_scs;
4279 int save_p_ws;
4280 int save_p_ic;
4281 int i;
4282 int num_matches;
4283 int len;
4284 int found_new_match;
4285 int type = ctrl_x_mode;
4286 char_u *ptr;
4287 char_u *dict = NULL;
4288 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004289 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004291 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004293 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 ins_buf->b_scanned = 0;
4295 found_all = FALSE;
4296 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004297 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004298 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 last_match_pos = first_match_pos = *ini;
4300 }
4301
Bram Moolenaar4475b622017-05-01 20:46:52 +02004302 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004303 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4305 for (;;)
4306 {
4307 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004308 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004310 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 * or if found_all says this entry is done. For ^X^L only use the
4312 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004313 if ((ctrl_x_mode == CTRL_X_NORMAL
4314 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004315 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 {
4317 found_all = FALSE;
4318 while (*e_cpt == ',' || *e_cpt == ' ')
4319 e_cpt++;
4320 if (*e_cpt == '.' && !curbuf->b_scanned)
4321 {
4322 ins_buf = curbuf;
4323 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004324 /* Move the cursor back one character so that ^N can match the
4325 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004326 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004327 {
4328 /* Move the cursor to after the last character in the
4329 * buffer, so that word at start of buffer is found
4330 * correctly. */
4331 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4332 first_match_pos.col =
4333 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 last_match_pos = first_match_pos;
4336 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004337
4338 /* Remember the first match so that the loop stops when we
4339 * wrap and come back there a second time. */
4340 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
4342 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4343 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4344 {
4345 /* Scan a buffer, but not the current one. */
4346 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4347 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004348 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 first_match_pos.col = last_match_pos.col = 0;
4350 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4351 last_match_pos.lnum = 0;
4352 type = 0;
4353 }
4354 else /* unloaded buffer, scan like dictionary */
4355 {
4356 found_all = TRUE;
4357 if (ins_buf->b_fname == NULL)
4358 continue;
4359 type = CTRL_X_DICTIONARY;
4360 dict = ins_buf->b_fname;
4361 dict_f = DICT_EXACT;
4362 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004363 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 ins_buf->b_fname == NULL
4365 ? buf_spname(ins_buf)
4366 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004367 ? ins_buf->b_fname
4368 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004369 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371 else if (*e_cpt == NUL)
4372 break;
4373 else
4374 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004375 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 type = -1;
4377 else if (*e_cpt == 'k' || *e_cpt == 's')
4378 {
4379 if (*e_cpt == 'k')
4380 type = CTRL_X_DICTIONARY;
4381 else
4382 type = CTRL_X_THESAURUS;
4383 if (*++e_cpt != ',' && *e_cpt != NUL)
4384 {
4385 dict = e_cpt;
4386 dict_f = DICT_FIRST;
4387 }
4388 }
4389#ifdef FEAT_FIND_ID
4390 else if (*e_cpt == 'i')
4391 type = CTRL_X_PATH_PATTERNS;
4392 else if (*e_cpt == 'd')
4393 type = CTRL_X_PATH_DEFINES;
4394#endif
4395 else if (*e_cpt == ']' || *e_cpt == 't')
4396 {
4397 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004398 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004399 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 }
4401 else
4402 type = -1;
4403
4404 /* in any case e_cpt is advanced to the next entry */
4405 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4406
4407 found_all = TRUE;
4408 if (type == -1)
4409 continue;
4410 }
4411 }
4412
Bram Moolenaar4475b622017-05-01 20:46:52 +02004413 /* If complete() was called then compl_pattern has been reset. The
4414 * following won't work then, bail out. */
4415 if (compl_pattern == NULL)
4416 break;
4417
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 switch (type)
4419 {
4420 case -1:
4421 break;
4422#ifdef FEAT_FIND_ID
4423 case CTRL_X_PATH_PATTERNS:
4424 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004425 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004426 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4430 (linenr_T)1, (linenr_T)MAXLNUM);
4431 break;
4432#endif
4433
4434 case CTRL_X_DICTIONARY:
4435 case CTRL_X_THESAURUS:
4436 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004437 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 : (type == CTRL_X_THESAURUS
4439 ? (*curbuf->b_p_tsr == NUL
4440 ? p_tsr
4441 : curbuf->b_p_tsr)
4442 : (*curbuf->b_p_dict == NUL
4443 ? p_dict
4444 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004445 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004446 dict != NULL ? dict_f
4447 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 dict = NULL;
4449 break;
4450
4451 case CTRL_X_TAGS:
4452 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4453 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004456 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457 * of matches is found when compl_pattern is empty */
4458 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004459 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4460 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4462 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004463 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465 p_ic = save_p_ic;
4466 break;
4467
4468 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004469 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4471 {
4472
4473 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004474 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004475 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 break;
4478
4479 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480 if (expand_cmdline(&compl_xp, compl_pattern,
4481 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004483 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 break;
4485
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004486#ifdef FEAT_COMPL_FUNC
4487 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004488 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004489 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004490 break;
4491#endif
4492
Bram Moolenaar488c6512005-08-11 20:09:58 +00004493 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004494#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004495 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004496 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004497 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004498 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004499#endif
4500 break;
4501
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 default: /* normal ^P/^N and ^X^L */
4503 /*
4504 * If 'infercase' is set, don't use 'smartcase' here
4505 */
4506 save_p_scs = p_scs;
4507 if (ins_buf->b_p_inf)
4508 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004509
Bram Moolenaar361aa502014-01-23 22:45:58 +01004510 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 * end but never from the middle, thus setting nowrapscan in this
4512 * buffers is a good idea, on the other hand, we always set
4513 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4514 save_p_ws = p_ws;
4515 if (ins_buf != curbuf)
4516 p_ws = FALSE;
4517 else if (*e_cpt == '.')
4518 p_ws = TRUE;
4519 for (;;)
4520 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004521 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004523 ++msg_silent; /* Don't want messages for wrapscan. */
4524
Bram Moolenaare4214502015-03-05 18:08:43 +01004525 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4526 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004527 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004528 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004529 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004531 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004533 found_new_match = searchit(NULL, ins_buf, pos,
4534 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004535 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004536 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004537 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004538 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004540 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004541 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 first_match_pos = *pos;
4543 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004544 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 }
4546 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004547 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 found_new_match = FAIL;
4549 if (found_new_match == FAIL)
4550 {
4551 if (ins_buf == curbuf)
4552 found_all = TRUE;
4553 break;
4554 }
4555
4556 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 && ini->lnum == pos->lnum
4559 && ini->col == pos->col)
4560 continue;
4561 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004562 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 {
4566 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4567 continue;
4568 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4569 if (!p_paste)
4570 ptr = skipwhite(ptr);
4571 }
4572 len = (int)STRLEN(ptr);
4573 }
4574 else
4575 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004576 char_u *tmp_ptr = ptr;
4577
4578 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004580 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 /* Skip if already inside a word. */
4582 if (vim_iswordp(tmp_ptr))
4583 continue;
4584 /* Find start of next word. */
4585 tmp_ptr = find_word_start(tmp_ptr);
4586 }
4587 /* Find end of this word. */
4588 tmp_ptr = find_word_end(tmp_ptr);
4589 len = (int)(tmp_ptr - ptr);
4590
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004591 if ((compl_cont_status & CONT_ADDING)
4592 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
4594 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4595 {
4596 /* Try next line, if any. the new word will be
4597 * "join" as if the normal command "J" was used.
4598 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004599 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 * works -- Acevedo */
4601 STRNCPY(IObuff, ptr, len);
4602 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4603 tmp_ptr = ptr = skipwhite(ptr);
4604 /* Find start of next word. */
4605 tmp_ptr = find_word_start(tmp_ptr);
4606 /* Find end of next word. */
4607 tmp_ptr = find_word_end(tmp_ptr);
4608 if (tmp_ptr > ptr)
4609 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004610 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004612 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 IObuff[len++] = ' ';
4614 /* IObuf =~ "\k.* ", thus len >= 2 */
4615 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004616 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 || (vim_strchr(p_cpo, CPO_JOINSP)
4618 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004619 && (IObuff[len - 2] == '?'
4620 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 IObuff[len++] = ' ';
4622 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004623 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (tmp_ptr - ptr >= IOSIZE - len)
4625 tmp_ptr = ptr + IOSIZE - len - 1;
4626 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4627 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004628 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 }
4630 IObuff[len] = NUL;
4631 ptr = IObuff;
4632 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004633 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 continue;
4635 }
4636 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004637 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004638 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004639 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
4641 found_new_match = OK;
4642 break;
4643 }
4644 }
4645 p_scs = save_p_scs;
4646 p_ws = save_p_ws;
4647 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004648
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004649 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004650 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004651 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 found_new_match = OK;
4653
4654 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004655 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4656 * match */
4657 if ((ctrl_x_mode != CTRL_X_NORMAL
4658 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004659 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004660 {
4661 if (got_int)
4662 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004663 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004664 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004665 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004666
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004667 if ((ctrl_x_mode != CTRL_X_NORMAL
4668 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004669 || compl_interrupted)
4670 break;
4671 compl_started = TRUE;
4672 }
4673 else
4674 {
4675 /* Mark a buffer scanned when it has been scanned completely */
4676 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4677 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004679 compl_started = FALSE;
4680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004682 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004684 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 && *e_cpt == NUL) /* Got to end of 'complete' */
4686 found_new_match = FAIL;
4687
4688 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004689 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4690 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 i = ins_compl_make_cyclic();
4692
Bram Moolenaar4475b622017-05-01 20:46:52 +02004693 if (compl_old_match != NULL)
4694 {
4695 /* If several matches were added (FORWARD) or the search failed and has
4696 * just been made cyclic then we have to move compl_curr_match to the
4697 * next or previous entry (if any) -- Acevedo */
4698 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4699 : compl_old_match->cp_prev;
4700 if (compl_curr_match == NULL)
4701 compl_curr_match = compl_old_match;
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 return i;
4704}
4705
4706/* Delete the old text being completed. */
4707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004708ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004710 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711
4712 /*
4713 * In insert mode: Delete the typed part.
4714 * In replace mode: Put the old characters back, if any.
4715 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004716 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4717 if ((int)curwin->w_cursor.col > col)
4718 {
4719 if (stop_arrow() == FAIL)
4720 return;
4721 backspace_until_column(col);
4722 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004723
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004724 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4725 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004727 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004728 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729}
4730
Bram Moolenaar472e8592016-10-15 17:06:47 +02004731/*
4732 * Insert the new text being completed.
4733 * "in_compl_func" is TRUE when called from complete_check().
4734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004736ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004738 dict_T *dict;
4739
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004740 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004741 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4742 compl_used_match = FALSE;
4743 else
4744 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004745
4746 /* Set completed item. */
4747 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004748 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004749 if (dict != NULL)
4750 {
4751 dict_add_nr_str(dict, "word", 0L,
4752 EMPTY_IF_NULL(compl_shown_match->cp_str));
4753 dict_add_nr_str(dict, "abbr", 0L,
4754 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4755 dict_add_nr_str(dict, "menu", 0L,
4756 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4757 dict_add_nr_str(dict, "kind", 0L,
4758 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4759 dict_add_nr_str(dict, "info", 0L,
4760 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4761 }
4762 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004763 if (!in_compl_func)
4764 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765}
4766
4767/*
4768 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004769 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4770 * get more completions. If it is FALSE, then we just do nothing when there
4771 * are no more completions in a given direction. The latter case is used when
4772 * we are still in the middle of finding completions, to allow browsing
4773 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 * Return the total number of matches, or -1 if still unknown -- webb.
4775 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004776 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4777 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 *
4779 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004780 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4781 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 */
4783 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004784ins_compl_next(
4785 int allow_get_expansion,
4786 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004787 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004788 int insert_match, /* Insert the newly selected match */
4789 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790{
4791 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004792 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004793 compl_T *found_compl = NULL;
4794 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004795 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004796 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004798 /* When user complete function return -1 for findstart which is next
4799 * time of 'always', compl_shown_match become NULL. */
4800 if (compl_shown_match == NULL)
4801 return -1;
4802
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004803 if (compl_leader != NULL
4804 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004806 /* Set "compl_shown_match" to the actually shown match, it may differ
4807 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004808 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004809 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004810 && compl_shown_match->cp_next != NULL
4811 && compl_shown_match->cp_next != compl_first_match)
4812 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004813
4814 /* If we didn't find it searching forward, and compl_shows_dir is
4815 * backward, find the last match. */
4816 if (compl_shows_dir == BACKWARD
4817 && !ins_compl_equal(compl_shown_match,
4818 compl_leader, (int)STRLEN(compl_leader))
4819 && (compl_shown_match->cp_next == NULL
4820 || compl_shown_match->cp_next == compl_first_match))
4821 {
4822 while (!ins_compl_equal(compl_shown_match,
4823 compl_leader, (int)STRLEN(compl_leader))
4824 && compl_shown_match->cp_prev != NULL
4825 && compl_shown_match->cp_prev != compl_first_match)
4826 compl_shown_match = compl_shown_match->cp_prev;
4827 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004828 }
4829
4830 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004831 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 /* Delete old text to be replaced */
4833 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004834
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004835 /* When finding the longest common text we stick at the original text,
4836 * don't let CTRL-N or CTRL-P move to the first match. */
4837 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4838
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004839 /* When restarting the search don't insert the first match either. */
4840 if (compl_restarting)
4841 {
4842 advance = FALSE;
4843 compl_restarting = FALSE;
4844 }
4845
Bram Moolenaare3226be2005-12-18 22:10:00 +00004846 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4847 * around. */
4848 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004850 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004852 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004853 found_end = (compl_first_match != NULL
4854 && (compl_shown_match->cp_next == compl_first_match
4855 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004856 }
4857 else if (compl_shows_dir == BACKWARD
4858 && compl_shown_match->cp_prev != NULL)
4859 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004860 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004861 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004862 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 }
4864 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004865 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004866 if (!allow_get_expansion)
4867 {
4868 if (advance)
4869 {
4870 if (compl_shows_dir == BACKWARD)
4871 compl_pending -= todo + 1;
4872 else
4873 compl_pending += todo + 1;
4874 }
4875 return -1;
4876 }
4877
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004878 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004879 {
4880 if (compl_shows_dir == BACKWARD)
4881 --compl_pending;
4882 else
4883 ++compl_pending;
4884 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004885
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004886 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004887 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004888
4889 /* handle any pending completions */
4890 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004891 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004892 {
4893 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4894 {
4895 compl_shown_match = compl_shown_match->cp_next;
4896 --compl_pending;
4897 }
4898 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4899 {
4900 compl_shown_match = compl_shown_match->cp_prev;
4901 ++compl_pending;
4902 }
4903 else
4904 break;
4905 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004906 found_end = FALSE;
4907 }
4908 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4909 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004910 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004911 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004912 ++todo;
4913 else
4914 /* Remember a matching item. */
4915 found_compl = compl_shown_match;
4916
4917 /* Stop at the end of the list when we found a usable match. */
4918 if (found_end)
4919 {
4920 if (found_compl != NULL)
4921 {
4922 compl_shown_match = found_compl;
4923 break;
4924 }
4925 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004929 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004930 if (compl_no_insert && !started)
4931 {
4932 ins_bytes(compl_orig_text + ins_compl_len());
4933 compl_used_match = FALSE;
4934 }
4935 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004936 {
4937 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004938 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004939 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004940 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004941 }
4942 else
4943 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945 if (!allow_get_expansion)
4946 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004947 /* may undisplay the popup menu first */
4948 ins_compl_upd_pum();
4949
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004950 /* redraw to show the user what was inserted */
4951 update_screen(0);
4952
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004953 /* display the updated popup menu */
4954 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004955#ifdef FEAT_GUI
4956 if (gui.in_use)
4957 {
4958 /* Show the cursor after the match, not after the redrawn text. */
4959 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004960 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00004961 }
4962#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004963
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 /* Delete old text to be replaced, since we're still searching and
4965 * don't want to match ourselves! */
4966 ins_compl_delete();
4967 }
4968
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004969 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004970 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004971 if (compl_no_insert && !started)
4972 compl_enter_selects = TRUE;
4973 else
4974 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004975
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 /*
4977 * Show the file name for the match (if any)
4978 * Truncate the file name to avoid a wait for return.
4979 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004980 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02004982 char *lead = _("match in file");
4983 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4984 char_u *s;
4985 char_u *e;
4986
4987 if (space > 0)
4988 {
4989 /* We need the tail that fits. With double-byte encoding going
4990 * back from the end is very slow, thus go from the start and keep
4991 * the text that fits in "space" between "s" and "e". */
4992 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
4993 {
4994 space -= ptr2cells(e);
4995 while (space < 0)
4996 {
4997 space += ptr2cells(s);
4998 MB_PTR_ADV(s);
4999 }
5000 }
5001 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5002 s > compl_shown_match->cp_fname ? "<" : "", s);
5003 msg(IObuff);
5004 redraw_cmdline = FALSE; /* don't overwrite! */
5005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
5007
5008 return num_matches;
5009}
5010
5011/*
5012 * Call this while finding completions, to check whether the user has hit a key
5013 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005014 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005016 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005017 * "in_compl_func" is TRUE when called from complete_check(), don't set
5018 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 */
5020 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005021ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
5023 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005024 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005026 /* Don't check when reading keys from a script, :normal or feedkeys().
5027 * That would break the test scripts. But do check for keys when called
5028 * from complete_check(). */
5029 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 return;
5031
5032 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005033 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 return;
5035 count = 0;
5036
Bram Moolenaara260a972006-06-23 19:36:29 +00005037 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5038 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 if (c != NUL)
5041 {
5042 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5043 {
5044 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005045 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005046 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005047 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005049 else
5050 {
5051 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005052 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005053 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005054 if (c != K_IGNORE)
5055 {
5056 /* Don't interrupt completion when the character wasn't typed,
5057 * e.g., when doing @q to replay keys. */
5058 if (c != Ctrl_R && KeyTyped)
5059 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005060
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005061 vungetc(c);
5062 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005065 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005066 {
5067 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5068
5069 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005070 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005071 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005072}
5073
5074/*
5075 * Decide the direction of Insert mode complete from the key typed.
5076 * Returns BACKWARD or FORWARD.
5077 */
5078 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005079ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005080{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005081 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005082 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005083 return BACKWARD;
5084 return FORWARD;
5085}
5086
5087/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005088 * Return TRUE for keys that are used for completion only when the popup menu
5089 * is visible.
5090 */
5091 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005092ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005093{
5094 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005095 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5096 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005097}
5098
5099/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005100 * Decide the number of completions to move forward.
5101 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5102 */
5103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005104ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005105{
5106 int h;
5107
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005108 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005109 {
5110 h = pum_get_height();
5111 if (h > 3)
5112 h -= 2; /* keep some context */
5113 return h;
5114 }
5115 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116}
5117
5118/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005119 * Return TRUE if completion with "c" should insert the match, FALSE if only
5120 * to change the currently selected completion.
5121 */
5122 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005123ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005124{
5125 switch (c)
5126 {
5127 case K_UP:
5128 case K_DOWN:
5129 case K_PAGEDOWN:
5130 case K_KPAGEDOWN:
5131 case K_S_DOWN:
5132 case K_PAGEUP:
5133 case K_KPAGEUP:
5134 case K_S_UP:
5135 return FALSE;
5136 }
5137 return TRUE;
5138}
5139
5140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 * Do Insert mode completion.
5142 * Called when character "c" was typed, which has a meaning for completion.
5143 * Returns OK if completion was done, FAIL if something failed (out of mem).
5144 */
5145 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005146ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005148 char_u *line;
5149 int startcol = 0; /* column where searched text starts */
5150 colnr_T curs_col; /* cursor column */
5151 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005152 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005153 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005154 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005155 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
Bram Moolenaare3226be2005-12-18 22:10:00 +00005157 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005158 insert_match = ins_compl_use_match(c);
5159
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005160 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
5162 /* First time we hit ^N or ^P (in a row, I mean) */
5163
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 did_ai = FALSE;
5165#ifdef FEAT_SMARTINDENT
5166 did_si = FALSE;
5167 can_si = FALSE;
5168 can_si_back = FALSE;
5169#endif
5170 if (stop_arrow() == FAIL)
5171 return FAIL;
5172
5173 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005174 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005175 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005177 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005178 * "compl_startpos" to the cursor as a pattern to add a new word
5179 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005180 * "compl_startpos" is not in the same line as the cursor then fix it
5181 * (the line has been split because it was longer than 'tw'). if SOL
5182 * is set then skip the previous pattern, a word at the beginning of
5183 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005184 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5185 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 {
5187 /*
5188 * it is a continued search
5189 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005191 if (ctrl_x_mode == CTRL_X_NORMAL
5192 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5193 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005195 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005197 /* line (probably) wrapped, set compl_startpos to the
5198 * first non_blank in the line, if it is not a wordchar
5199 * include it to get a better pattern, but then we don't
5200 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005201 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005202 compl_startpos.col = compl_col;
5203 compl_startpos.lnum = curwin->w_cursor.lnum;
5204 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
5206 else
5207 {
5208 /* S_IPOS was set when we inserted a word that was at the
5209 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005210 * mode but first we need to redefine compl_startpos */
5211 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005213 compl_cont_status |= CONT_SOL;
5214 compl_startpos.col = (colnr_T)(skipwhite(
5215 line + compl_length
5216 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005218 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005220 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005221 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005222 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005224 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005226 compl_cont_status &= ~CONT_SOL;
5227 compl_length = (IOSIZE - MIN_SPACE);
5228 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005230 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5231 if (compl_length < 1)
5232 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005234 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005235 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005237 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
5239 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005240 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005242 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005245 if (ctrl_x_mode != CTRL_X_NORMAL)
5246 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005247 compl_cont_status = 0;
5248 compl_cont_status |= CONT_N_ADDS;
5249 compl_startpos = curwin->w_cursor;
5250 startcol = (int)curs_col;
5251 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 }
5253
5254 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005255 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5259 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005260 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005262 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005264 compl_col += ++startcol;
5265 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
5267 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005268 compl_pattern = str_foldcase(line + compl_col,
5269 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005271 compl_pattern = vim_strnsave(line + compl_col,
5272 compl_length);
5273 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 return FAIL;
5275 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005276 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
5278 char_u *prefix = (char_u *)"\\<";
5279
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005280 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005282 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005283 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005285 if (!vim_iswordp(line + compl_col)
5286 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 && (
5288#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005289 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292#endif
5293 )))
5294 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005295 STRCPY((char *)compl_pattern, prefix);
5296 (void)quote_meta(compl_pattern + STRLEN(prefix),
5297 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005299 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005303 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304#endif
5305 )
5306 {
5307 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5309 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005311 compl_col += curs_col;
5312 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 }
5314 else
5315 {
5316#ifdef FEAT_MBYTE
5317 /* Search the point of change class of multibyte character
5318 * or not a word single byte character backward. */
5319 if (has_mbyte)
5320 {
5321 int base_class;
5322 int head_off;
5323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005324 startcol -= (*mb_head_off)(line, line + startcol);
5325 base_class = mb_get_class(line + startcol);
5326 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 head_off = (*mb_head_off)(line, line + startcol);
5329 if (base_class != mb_get_class(line + startcol
5330 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005332 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 }
5334 }
5335 else
5336#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005339 compl_col += ++startcol;
5340 compl_length = (int)curs_col - startcol;
5341 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
5343 /* Only match word with at least two chars -- webb
5344 * there's no need to call quote_meta,
5345 * alloc(7) is enough -- Acevedo
5346 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005347 compl_pattern = alloc(7);
5348 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005350 STRCPY((char *)compl_pattern, "\\<");
5351 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5352 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
5354 else
5355 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005356 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005357 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005358 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005360 STRCPY((char *)compl_pattern, "\\<");
5361 (void)quote_meta(compl_pattern + 2, line + compl_col,
5362 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 }
5364 }
5365 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005366 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005368 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005369 compl_length = (int)curs_col - (int)compl_col;
5370 if (compl_length < 0) /* cursor in indent: empty pattern */
5371 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005373 compl_pattern = str_foldcase(line + compl_col, compl_length,
5374 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005376 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5377 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 return FAIL;
5379 }
5380 else if (ctrl_x_mode == CTRL_X_FILES)
5381 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005382 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005383 if (startcol > 0)
5384 {
5385 char_u *p = line + startcol;
5386
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005387 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005388 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005389 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005390 if (p == line && vim_isfilec(PTR2CHAR(p)))
5391 startcol = 0;
5392 else
5393 startcol = (int)(p - line) + 1;
5394 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005395
Bram Moolenaar0300e462013-09-08 16:03:45 +02005396 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005397 compl_length = (int)curs_col - startcol;
5398 compl_pattern = addstar(line + compl_col, compl_length,
5399 EXPAND_FILES);
5400 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 return FAIL;
5402 }
5403 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5404 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005405 compl_pattern = vim_strnsave(line, curs_col);
5406 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005408 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005409 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005410 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5411 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005412 /* No completion possible, use an empty pattern to get a
5413 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005414 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005415 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005416 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5417 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005419 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005420 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005421#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005422 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005423 * Call user defined function 'completefunc' with "a:findstart"
5424 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005425 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005426 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005427 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005428 char_u *funcname;
5429 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005430 win_T *curwin_save;
5431 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005432
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005433 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005434 * string */
5435 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5436 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5437 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005438 {
5439 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5440 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005441 /* restore did_ai, so that adding comment leader works */
5442 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005443 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005444 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005445
5446 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005447 args[1] = NULL;
5448 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005449 curwin_save = curwin;
5450 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005451 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005452 if (curwin_save != curwin || curbuf_save != curbuf)
5453 {
5454 EMSG(_(e_complwin));
5455 return FAIL;
5456 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005457 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005458 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005459 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005460 {
5461 EMSG(_(e_compldel));
5462 return FAIL;
5463 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005464
Bram Moolenaar6110a002012-01-26 18:58:38 +01005465 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005466 * cancel the complete without an error.
5467 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005468 if (col == -2)
5469 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005470 if (col == -3)
5471 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005472 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005473 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005474 if (!shortmess(SHM_COMPLETIONMENU))
5475 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005476 return FAIL;
5477 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005478
Bram Moolenaar82139082011-09-14 16:52:09 +02005479 /*
5480 * Reset extended parameters of completion, when start new
5481 * completion.
5482 */
5483 compl_opt_refresh_always = FALSE;
5484
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005485 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005486 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005487 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005488 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005489 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005490
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005491 /* Setup variables for completion. Need to obtain "line" again,
5492 * it may have become invalid. */
5493 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005494 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005495 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5496 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005497#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005498 return FAIL;
5499 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005500 else if (ctrl_x_mode == CTRL_X_SPELL)
5501 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005502#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005503 if (spell_bad_len > 0)
5504 compl_col = curs_col - spell_bad_len;
5505 else
5506 compl_col = spell_word_start(startcol);
5507 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005508 {
5509 compl_length = 0;
5510 compl_col = curs_col;
5511 }
5512 else
5513 {
5514 spell_expand_check_cap(compl_col);
5515 compl_length = (int)curs_col - compl_col;
5516 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005517 /* Need to obtain "line" again, it may have become invalid. */
5518 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005519 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5520 if (compl_pattern == NULL)
5521#endif
5522 return FAIL;
5523 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005524 else
5525 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005526 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005527 return FAIL;
5528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005530 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
5532 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005533 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 {
5535 /* Insert a new line, keep indentation but ignore 'comments' */
5536#ifdef FEAT_COMMENTS
5537 char_u *old = curbuf->b_p_com;
5538
5539 curbuf->b_p_com = (char_u *)"";
5540#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005541 compl_startpos.lnum = curwin->w_cursor.lnum;
5542 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 ins_eol('\r');
5544#ifdef FEAT_COMMENTS
5545 curbuf->b_p_com = old;
5546#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005547 compl_length = 0;
5548 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
5550 }
5551 else
5552 {
5553 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005554 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005557 if (compl_cont_status & CONT_LOCAL)
5558 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 else
5560 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5561
Bram Moolenaar62951b12011-09-21 18:23:05 +02005562 /* If any of the original typed text has been changed we need to fix
5563 * the redo buffer. */
5564 ins_compl_fixRedoBufForLeader(NULL);
5565
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005566 /* Always add completion for the original text. */
5567 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005568 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5569 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005570 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 {
Bram Moolenaar15675582018-02-09 12:29:56 +01005572 vim_clear((void **)&compl_pattern);
5573 vim_clear((void **)&compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 return FAIL;
5575 }
5576
5577 /* showmode might reset the internal line pointers, so it must
5578 * be called before line = ml_get(), or when this address is no
5579 * longer needed. -- Acevedo.
5580 */
5581 edit_submode_extra = (char_u *)_("-- Searching...");
5582 edit_submode_highl = HLF_COUNT;
5583 showmode();
5584 edit_submode_extra = NULL;
5585 out_flush();
5586 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005587 else if (insert_match && stop_arrow() == FAIL)
5588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005590 compl_shown_match = compl_curr_match;
5591 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592
5593 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005594 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005596 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005597 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005598 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005600 /* may undisplay the popup menu */
5601 ins_compl_upd_pum();
5602
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005603 if (n > 1) /* all matches have been found */
5604 compl_matches = n;
5605 compl_curr_match = compl_shown_match;
5606 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607
Bram Moolenaard68071d2006-05-02 22:08:30 +00005608 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5609 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 if (got_int && !global_busy)
5611 {
5612 (void)vgetc();
5613 got_int = FALSE;
5614 }
5615
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005616 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005617 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005619 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5620 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5622 edit_submode_highl = HLF_E;
5623 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5624 * because we couldn't expand anything at first place, but if we used
5625 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5626 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005627 if ( compl_length > 1
5628 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005629 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5631 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005632 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 }
5634
Bram Moolenaar572cb562005-08-05 21:35:02 +00005635 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005636 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005638 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
5640 if (edit_submode_extra == NULL)
5641 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005642 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 {
5644 edit_submode_extra = (char_u *)_("Back at original");
5645 edit_submode_highl = HLF_W;
5646 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005647 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
5649 edit_submode_extra = (char_u *)_("Word from other line");
5650 edit_submode_highl = HLF_COUNT;
5651 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005652 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 {
5654 edit_submode_extra = (char_u *)_("The only match");
5655 edit_submode_highl = HLF_COUNT;
5656 }
5657 else
5658 {
5659 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005660 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005662 int number = 0;
5663 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005665 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
5667 /* search backwards for the first valid (!= -1) number.
5668 * This should normally succeed already at the first loop
5669 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005670 for (match = compl_curr_match->cp_prev; match != NULL
5671 && match != compl_first_match;
5672 match = match->cp_prev)
5673 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005675 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 break;
5677 }
5678 if (match != NULL)
5679 /* go up and assign all numbers which are not assigned
5680 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005681 for (match = match->cp_next;
5682 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005683 match = match->cp_next)
5684 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 else /* BACKWARD */
5687 {
5688 /* search forwards (upwards) for the first valid (!= -1)
5689 * number. This should normally succeed already at the
5690 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005691 for (match = compl_curr_match->cp_next; match != NULL
5692 && match != compl_first_match;
5693 match = match->cp_next)
5694 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005696 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 break;
5698 }
5699 if (match != NULL)
5700 /* go down and assign all numbers which are not
5701 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005702 for (match = match->cp_prev; match
5703 && match->cp_number == -1;
5704 match = match->cp_prev)
5705 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
5707 }
5708
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005709 /* The match should always have a sequence number now, this is
5710 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005711 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005713 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5714 * Translations may need more than twice that. */
5715 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005717 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005718 vim_snprintf((char *)match_ref, sizeof(match_ref),
5719 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005720 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005722 vim_snprintf((char *)match_ref, sizeof(match_ref),
5723 _("match %d"),
5724 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 edit_submode_extra = match_ref;
5726 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005727 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 curs_columns(FALSE);
5729 }
5730 }
5731 }
5732
5733 /* Show a message about what (completion) mode we're in. */
5734 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005735 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005737 if (edit_submode_extra != NULL)
5738 {
5739 if (!p_smd)
5740 msg_attr(edit_submode_extra,
5741 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005742 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005743 }
5744 else
5745 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
Bram Moolenaard68071d2006-05-02 22:08:30 +00005748 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005749 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005750 show_pum(save_w_wrow, save_w_leftcol);
5751
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005752 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005753 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 return OK;
5756}
5757
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005758 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005759show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005760{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005761 /* RedrawingDisabled may be set when invoked through complete(). */
5762 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005763
Bram Moolenaarcb036422017-03-01 12:29:10 +01005764 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005765
Bram Moolenaarcb036422017-03-01 12:29:10 +01005766 /* If the cursor moved or the display scrolled we need to remove the pum
5767 * first. */
5768 setcursor();
5769 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5770 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005771
Bram Moolenaarcb036422017-03-01 12:29:10 +01005772 ins_compl_show_pum();
5773 setcursor();
5774 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005775}
5776
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777/*
5778 * Looks in the first "len" chars. of "src" for search-metachars.
5779 * If dest is not NULL the chars. are copied there quoting (with
5780 * a backslash) the metachars, and dest would be NUL terminated.
5781 * Returns the length (needed) of dest
5782 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005783 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005784quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005786 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005788 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
5790 switch (*src)
5791 {
5792 case '.':
5793 case '*':
5794 case '[':
5795 if (ctrl_x_mode == CTRL_X_DICTIONARY
5796 || ctrl_x_mode == CTRL_X_THESAURUS)
5797 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005798 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 case '~':
5800 if (!p_magic) /* quote these only if magic is set */
5801 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005802 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 case '\\':
5804 if (ctrl_x_mode == CTRL_X_DICTIONARY
5805 || ctrl_x_mode == CTRL_X_THESAURUS)
5806 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005807 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 case '^': /* currently it's not needed. */
5809 case '$':
5810 m++;
5811 if (dest != NULL)
5812 *dest++ = '\\';
5813 break;
5814 }
5815 if (dest != NULL)
5816 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005817# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 /* Copy remaining bytes of a multibyte character. */
5819 if (has_mbyte)
5820 {
5821 int i, mb_len;
5822
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005823 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 if (mb_len > 0 && len >= mb_len)
5825 for (i = 0; i < mb_len; ++i)
5826 {
5827 --len;
5828 ++src;
5829 if (dest != NULL)
5830 *dest++ = *src;
5831 }
5832 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 }
5835 if (dest != NULL)
5836 *dest = NUL;
5837
5838 return m;
5839}
5840#endif /* FEAT_INS_EXPAND */
5841
5842/*
5843 * Next character is interpreted literally.
5844 * A one, two or three digit decimal number is interpreted as its byte value.
5845 * If one or two digits are entered, the next character is given to vungetc().
5846 * For Unicode a character > 255 may be returned.
5847 */
5848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005849get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
5851 int cc;
5852 int nc;
5853 int i;
5854 int hex = FALSE;
5855 int octal = FALSE;
5856#ifdef FEAT_MBYTE
5857 int unicode = 0;
5858#endif
5859
5860 if (got_int)
5861 return Ctrl_C;
5862
5863#ifdef FEAT_GUI
5864 /*
5865 * In GUI there is no point inserting the internal code for a special key.
5866 * It is more useful to insert the string "<KEY>" instead. This would
5867 * probably be useful in a text window too, but it would not be
5868 * vi-compatible (maybe there should be an option for it?) -- webb
5869 */
5870 if (gui.in_use)
5871 ++allow_keys;
5872#endif
5873#ifdef USE_ON_FLY_SCROLL
5874 dont_scroll = TRUE; /* disallow scrolling here */
5875#endif
5876 ++no_mapping; /* don't map the next key hits */
5877 cc = 0;
5878 i = 0;
5879 for (;;)
5880 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005881 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882#ifdef FEAT_CMDL_INFO
5883 if (!(State & CMDLINE)
5884# ifdef FEAT_MBYTE
5885 && MB_BYTE2LEN_CHECK(nc) == 1
5886# endif
5887 )
5888 add_to_showcmd(nc);
5889#endif
5890 if (nc == 'x' || nc == 'X')
5891 hex = TRUE;
5892 else if (nc == 'o' || nc == 'O')
5893 octal = TRUE;
5894#ifdef FEAT_MBYTE
5895 else if (nc == 'u' || nc == 'U')
5896 unicode = nc;
5897#endif
5898 else
5899 {
5900 if (hex
5901#ifdef FEAT_MBYTE
5902 || unicode != 0
5903#endif
5904 )
5905 {
5906 if (!vim_isxdigit(nc))
5907 break;
5908 cc = cc * 16 + hex2nr(nc);
5909 }
5910 else if (octal)
5911 {
5912 if (nc < '0' || nc > '7')
5913 break;
5914 cc = cc * 8 + nc - '0';
5915 }
5916 else
5917 {
5918 if (!VIM_ISDIGIT(nc))
5919 break;
5920 cc = cc * 10 + nc - '0';
5921 }
5922
5923 ++i;
5924 }
5925
5926 if (cc > 255
5927#ifdef FEAT_MBYTE
5928 && unicode == 0
5929#endif
5930 )
5931 cc = 255; /* limit range to 0-255 */
5932 nc = 0;
5933
5934 if (hex) /* hex: up to two chars */
5935 {
5936 if (i >= 2)
5937 break;
5938 }
5939#ifdef FEAT_MBYTE
5940 else if (unicode) /* Unicode: up to four or eight chars */
5941 {
5942 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5943 break;
5944 }
5945#endif
5946 else if (i >= 3) /* decimal or octal: up to three chars */
5947 break;
5948 }
5949 if (i == 0) /* no number entered */
5950 {
5951 if (nc == K_ZERO) /* NUL is stored as NL */
5952 {
5953 cc = '\n';
5954 nc = 0;
5955 }
5956 else
5957 {
5958 cc = nc;
5959 nc = 0;
5960 }
5961 }
5962
5963 if (cc == 0) /* NUL is stored as NL */
5964 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005965#ifdef FEAT_MBYTE
5966 if (enc_dbcs && (cc & 0xff) == 0)
5967 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5968 second byte will cause trouble! */
5969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
5971 --no_mapping;
5972#ifdef FEAT_GUI
5973 if (gui.in_use)
5974 --allow_keys;
5975#endif
5976 if (nc)
5977 vungetc(nc);
5978 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5979 return cc;
5980}
5981
5982/*
5983 * Insert character, taking care of special keys and mod_mask
5984 */
5985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005986insert_special(
5987 int c,
5988 int allow_modmask,
5989 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
5991 char_u *p;
5992 int len;
5993
5994 /*
5995 * Special function key, translate into "<Key>". Up to the last '>' is
5996 * inserted with ins_str(), so as not to replace characters in replace
5997 * mode.
5998 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5999 * unless 'allow_modmask' is TRUE.
6000 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006001#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 /* Command-key never produces a normal key */
6003 if (mod_mask & MOD_MASK_CMD)
6004 allow_modmask = TRUE;
6005#endif
6006 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6007 {
6008 p = get_special_key_name(c, mod_mask);
6009 len = (int)STRLEN(p);
6010 c = p[len - 1];
6011 if (len > 2)
6012 {
6013 if (stop_arrow() == FAIL)
6014 return;
6015 p[len - 1] = NUL;
6016 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006017 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 ctrlv = FALSE;
6019 }
6020 }
6021 if (stop_arrow() == OK)
6022 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6023}
6024
6025/*
6026 * Special characters in this context are those that need processing other
6027 * than the simple insertion that can be performed here. This includes ESC
6028 * which terminates the insert, and CR/NL which need special processing to
6029 * open up a new line. This routine tries to optimize insertions performed by
6030 * the "redo", "undo" or "put" commands, so it needs to know when it should
6031 * stop and defer processing to the "normal" mechanism.
6032 * '0' and '^' are special, because they can be followed by CTRL-D.
6033 */
6034#ifdef EBCDIC
6035# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6036#else
6037# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6038#endif
6039
6040#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006041# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006043# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044#endif
6045
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006046/*
6047 * "flags": INSCHAR_FORMAT - force formatting
6048 * INSCHAR_CTRLV - char typed just after CTRL-V
6049 * INSCHAR_NO_FEX - don't use 'formatexpr'
6050 *
6051 * NOTE: passes the flags value straight through to internal_format() which,
6052 * beside INSCHAR_FORMAT (above), is also looking for these:
6053 * INSCHAR_DO_COM - format comments
6054 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006057insertchar(
6058 int c, /* character to insert or NUL */
6059 int flags, /* INSCHAR_FORMAT, etc. */
6060 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 int textwidth;
6063#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006067 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006069 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
6072 /*
6073 * Try to break the line in two or more pieces when:
6074 * - Always do this if we have been called to do formatting only.
6075 * - Always do this when 'formatoptions' has the 'a' flag and the line
6076 * ends in white space.
6077 * - Otherwise:
6078 * - Don't do this if inserting a blank
6079 * - Don't do this if an existing character is being replaced, unless
6080 * we're in VREPLACE mode.
6081 * - Do this if the cursor is not on the line where insert started
6082 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6083 * before the insert.
6084 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6085 * before 'textwidth'
6086 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006087 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006088 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006089 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 && !((State & REPLACE_FLAG)
6091#ifdef FEAT_VREPLACE
6092 && !(State & VREPLACE_FLAG)
6093#endif
6094 && *ml_get_cursor() != NUL)
6095 && (curwin->w_cursor.lnum != Insstart.lnum
6096 || ((!has_format_option(FO_INS_LONG)
6097 || Insstart_textlen <= (colnr_T)textwidth)
6098 && (!fo_ins_blank
6099 || Insstart_blank_vcol <= (colnr_T)textwidth
6100 ))))))
6101 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006102 /* Format with 'formatexpr' when it's set. Use internal formatting
6103 * when 'formatexpr' isn't set or it returns non-zero. */
6104#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006105 int do_internal = TRUE;
6106 colnr_T virtcol = get_nolist_virtcol()
6107 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006108
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006109 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6110 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006111 {
6112 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6113 /* It may be required to save for undo again, e.g. when setline()
6114 * was called. */
6115 ins_need_undo = TRUE;
6116 }
6117 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006119 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006121
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 if (c == NUL) /* only formatting was wanted */
6123 return;
6124
6125#ifdef FEAT_COMMENTS
6126 /* Check whether this character should end a comment. */
6127 if (did_ai && (int)c == end_comment_pending)
6128 {
6129 char_u *line;
6130 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6131 int middle_len, end_len;
6132 int i;
6133
6134 /*
6135 * Need to remove existing (middle) comment leader and insert end
6136 * comment leader. First, check what comment leader we can find.
6137 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006138 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6140 {
6141 /* Skip middle-comment string */
6142 while (*p && p[-1] != ':') /* find end of middle flags */
6143 ++p;
6144 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6145 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006146 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 --middle_len;
6148
6149 /* Find the end-comment string */
6150 while (*p && p[-1] != ':') /* find end of end flags */
6151 ++p;
6152 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6153
6154 /* Skip white space before the cursor */
6155 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006156 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 ;
6158 i++;
6159
6160 /* Skip to before the middle leader */
6161 i -= middle_len;
6162
6163 /* Check some expected things before we go on */
6164 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6165 {
6166 /* Backspace over all the stuff we want to replace */
6167 backspace_until_column(i);
6168
6169 /*
6170 * Insert the end-comment string, except for the last
6171 * character, which will get inserted as normal later.
6172 */
6173 ins_bytes_len(lead_end, end_len - 1);
6174 }
6175 }
6176 }
6177 end_comment_pending = NUL;
6178#endif
6179
6180 did_ai = FALSE;
6181#ifdef FEAT_SMARTINDENT
6182 did_si = FALSE;
6183 can_si = FALSE;
6184 can_si_back = FALSE;
6185#endif
6186
6187 /*
6188 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6189 * This speeds up normal text input considerably.
6190 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6191 * need to re-indent at a ':', or any other character (but not what
6192 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006193 * Don't do this when there an InsertCharPre autocommand is defined,
6194 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 */
6196#ifdef USE_ON_FLY_SCROLL
6197 dont_scroll = FALSE; /* allow scrolling here */
6198#endif
6199
6200 if ( !ISSPECIAL(c)
6201#ifdef FEAT_MBYTE
6202 && (!has_mbyte || (*mb_char2len)(c) == 1)
6203#endif
6204 && vpeekc() != NUL
6205 && !(State & REPLACE_FLAG)
6206#ifdef FEAT_CINDENT
6207 && !cindent_on()
6208#endif
6209#ifdef FEAT_RIGHTLEFT
6210 && !p_ri
6211#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006212#ifdef FEAT_AUTOCMD
6213 && !has_insertcharpre()
6214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 )
6216 {
6217#define INPUT_BUFLEN 100
6218 char_u buf[INPUT_BUFLEN + 1];
6219 int i;
6220 colnr_T virtcol = 0;
6221
6222 buf[0] = c;
6223 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006224 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 virtcol = get_nolist_virtcol();
6226 /*
6227 * Stop the string when:
6228 * - no more chars available
6229 * - finding a special character (command key)
6230 * - buffer is full
6231 * - running into the 'textwidth' boundary
6232 * - need to check for abbreviation: A non-word char after a word-char
6233 */
6234 while ( (c = vpeekc()) != NUL
6235 && !ISSPECIAL(c)
6236#ifdef FEAT_MBYTE
6237 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6238#endif
6239 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006240# ifdef FEAT_FKMAP
6241 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 && (textwidth == 0
6244 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6245 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6246 {
6247#ifdef FEAT_RIGHTLEFT
6248 c = vgetc();
6249 if (p_hkmap && KeyTyped)
6250 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 buf[i++] = c;
6252#else
6253 buf[i++] = vgetc();
6254#endif
6255 }
6256
6257#ifdef FEAT_DIGRAPHS
6258 do_digraph(-1); /* clear digraphs */
6259 do_digraph(buf[i-1]); /* may be the start of a digraph */
6260#endif
6261 buf[i] = NUL;
6262 ins_str(buf);
6263 if (flags & INSCHAR_CTRLV)
6264 {
6265 redo_literal(*buf);
6266 i = 1;
6267 }
6268 else
6269 i = 0;
6270 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006271 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 }
6273 else
6274 {
6275#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006276 int cc;
6277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6279 {
6280 char_u buf[MB_MAXBYTES + 1];
6281
6282 (*mb_char2bytes)(c, buf);
6283 buf[cc] = NUL;
6284 ins_char_bytes(buf, cc);
6285 AppendCharToRedobuff(c);
6286 }
6287 else
6288#endif
6289 {
6290 ins_char(c);
6291 if (flags & INSCHAR_CTRLV)
6292 redo_literal(c);
6293 else
6294 AppendCharToRedobuff(c);
6295 }
6296 }
6297}
6298
6299/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006300 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006301 *
6302 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6303 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006304 */
6305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006306internal_format(
6307 int textwidth,
6308 int second_indent,
6309 int flags,
6310 int format_only,
6311 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006312{
6313 int cc;
6314 int save_char = NUL;
6315 int haveto_redraw = FALSE;
6316 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6317#ifdef FEAT_MBYTE
6318 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6319#endif
6320 int fo_white_par = has_format_option(FO_WHITE_PAR);
6321 int first_line = TRUE;
6322#ifdef FEAT_COMMENTS
6323 colnr_T leader_len;
6324 int no_leader = FALSE;
6325 int do_comments = (flags & INSCHAR_DO_COM);
6326#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006327#ifdef FEAT_LINEBREAK
6328 int has_lbr = curwin->w_p_lbr;
6329
6330 /* make sure win_lbr_chartabsize() counts correctly */
6331 curwin->w_p_lbr = FALSE;
6332#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006333
6334 /*
6335 * When 'ai' is off we don't want a space under the cursor to be
6336 * deleted. Replace it with an 'x' temporarily.
6337 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006338 if (!curbuf->b_p_ai
6339#ifdef FEAT_VREPLACE
6340 && !(State & VREPLACE_FLAG)
6341#endif
6342 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006343 {
6344 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006345 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006346 {
6347 save_char = cc;
6348 pchar_cursor('x');
6349 }
6350 }
6351
6352 /*
6353 * Repeat breaking lines, until the current line is not too long.
6354 */
6355 while (!got_int)
6356 {
6357 int startcol; /* Cursor column at entry */
6358 int wantcol; /* column at textwidth border */
6359 int foundcol; /* column for start of spaces */
6360 int end_foundcol = 0; /* column for start of word */
6361 colnr_T len;
6362 colnr_T virtcol;
6363#ifdef FEAT_VREPLACE
6364 int orig_col = 0;
6365 char_u *saved_text = NULL;
6366#endif
6367 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006368 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006369
Bram Moolenaar97b98102009-11-17 16:41:01 +00006370 virtcol = get_nolist_virtcol()
6371 + char2cells(c != NUL ? c : gchar_cursor());
6372 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006373 break;
6374
6375#ifdef FEAT_COMMENTS
6376 if (no_leader)
6377 do_comments = FALSE;
6378 else if (!(flags & INSCHAR_FORMAT)
6379 && has_format_option(FO_WRAP_COMS))
6380 do_comments = TRUE;
6381
6382 /* Don't break until after the comment leader */
6383 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006384 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006385 else
6386 leader_len = 0;
6387
6388 /* If the line doesn't start with a comment leader, then don't
6389 * start one in a following broken line. Avoids that a %word
6390 * moved to the start of the next line causes all following lines
6391 * to start with %. */
6392 if (leader_len == 0)
6393 no_leader = TRUE;
6394#endif
6395 if (!(flags & INSCHAR_FORMAT)
6396#ifdef FEAT_COMMENTS
6397 && leader_len == 0
6398#endif
6399 && !has_format_option(FO_WRAP))
6400
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006401 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006402 if ((startcol = curwin->w_cursor.col) == 0)
6403 break;
6404
6405 /* find column of textwidth border */
6406 coladvance((colnr_T)textwidth);
6407 wantcol = curwin->w_cursor.col;
6408
Bram Moolenaar97b98102009-11-17 16:41:01 +00006409 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006410 foundcol = 0;
6411
6412 /*
6413 * Find position to break at.
6414 * Stop at first entered white when 'formatoptions' has 'v'
6415 */
6416 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006417 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006418 || curwin->w_cursor.lnum != Insstart.lnum
6419 || curwin->w_cursor.col >= Insstart.col)
6420 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006421 if (curwin->w_cursor.col == startcol && c != NUL)
6422 cc = c;
6423 else
6424 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006425 if (WHITECHAR(cc))
6426 {
6427 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006428 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006429
6430 /* find start of sequence of blanks */
6431 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6432 {
6433 dec_cursor();
6434 cc = gchar_cursor();
6435 }
6436 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6437 break; /* only spaces in front of text */
6438#ifdef FEAT_COMMENTS
6439 /* Don't break until after the comment leader */
6440 if (curwin->w_cursor.col < leader_len)
6441 break;
6442#endif
6443 if (has_format_option(FO_ONE_LETTER))
6444 {
6445 /* do not break after one-letter words */
6446 if (curwin->w_cursor.col == 0)
6447 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006448#ifdef FEAT_COMMENTS
6449 /* do not break "#a b" when 'tw' is 2 */
6450 if (curwin->w_cursor.col <= leader_len)
6451 break;
6452#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006453 col = curwin->w_cursor.col;
6454 dec_cursor();
6455 cc = gchar_cursor();
6456
6457 if (WHITECHAR(cc))
6458 continue; /* one-letter, continue */
6459 curwin->w_cursor.col = col;
6460 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006461
6462 inc_cursor();
6463
6464 end_foundcol = end_col + 1;
6465 foundcol = curwin->w_cursor.col;
6466 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006467 break;
6468 }
6469#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006470 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006471 {
6472 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006473 if (curwin->w_cursor.col != startcol)
6474 {
6475#ifdef FEAT_COMMENTS
6476 /* Don't break until after the comment leader */
6477 if (curwin->w_cursor.col < leader_len)
6478 break;
6479#endif
6480 col = curwin->w_cursor.col;
6481 inc_cursor();
6482 /* Don't change end_foundcol if already set. */
6483 if (foundcol != curwin->w_cursor.col)
6484 {
6485 foundcol = curwin->w_cursor.col;
6486 end_foundcol = foundcol;
6487 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6488 break;
6489 }
6490 curwin->w_cursor.col = col;
6491 }
6492
6493 if (curwin->w_cursor.col == 0)
6494 break;
6495
6496 col = curwin->w_cursor.col;
6497
6498 dec_cursor();
6499 cc = gchar_cursor();
6500
6501 if (WHITECHAR(cc))
6502 continue; /* break with space */
6503#ifdef FEAT_COMMENTS
6504 /* Don't break until after the comment leader */
6505 if (curwin->w_cursor.col < leader_len)
6506 break;
6507#endif
6508
6509 curwin->w_cursor.col = col;
6510
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006511 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006512 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006513 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6514 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006515 }
6516#endif
6517 if (curwin->w_cursor.col == 0)
6518 break;
6519 dec_cursor();
6520 }
6521
6522 if (foundcol == 0) /* no spaces, cannot break line */
6523 {
6524 curwin->w_cursor.col = startcol;
6525 break;
6526 }
6527
6528 /* Going to break the line, remove any "$" now. */
6529 undisplay_dollar();
6530
6531 /*
6532 * Offset between cursor position and line break is used by replace
6533 * stack functions. VREPLACE does not use this, and backspaces
6534 * over the text instead.
6535 */
6536#ifdef FEAT_VREPLACE
6537 if (State & VREPLACE_FLAG)
6538 orig_col = startcol; /* Will start backspacing from here */
6539 else
6540#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006541 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006542
6543 /*
6544 * adjust startcol for spaces that will be deleted and
6545 * characters that will remain on top line
6546 */
6547 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006548 while ((cc = gchar_cursor(), WHITECHAR(cc))
6549 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006550 inc_cursor();
6551 startcol -= curwin->w_cursor.col;
6552 if (startcol < 0)
6553 startcol = 0;
6554
6555#ifdef FEAT_VREPLACE
6556 if (State & VREPLACE_FLAG)
6557 {
6558 /*
6559 * In VREPLACE mode, we will backspace over the text to be
6560 * wrapped, so save a copy now to put on the next line.
6561 */
6562 saved_text = vim_strsave(ml_get_cursor());
6563 curwin->w_cursor.col = orig_col;
6564 if (saved_text == NULL)
6565 break; /* Can't do it, out of memory */
6566 saved_text[startcol] = NUL;
6567
6568 /* Backspace over characters that will move to the next line */
6569 if (!fo_white_par)
6570 backspace_until_column(foundcol);
6571 }
6572 else
6573#endif
6574 {
6575 /* put cursor after pos. to break line */
6576 if (!fo_white_par)
6577 curwin->w_cursor.col = foundcol;
6578 }
6579
6580 /*
6581 * Split the line just before the margin.
6582 * Only insert/delete lines, but don't really redraw the window.
6583 */
6584 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6585 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6586#ifdef FEAT_COMMENTS
6587 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006588 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006589#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006590 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6591 if (!(flags & INSCHAR_COM_LIST))
6592 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006593
6594 replace_offset = 0;
6595 if (first_line)
6596 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006597 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006598 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006599 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006600 * This section is for auto-wrap of numeric lists. When not
6601 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6602 * flag will be set and open_line() will handle it (as seen
6603 * above). The code here (and in get_number_indent()) will
6604 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006605 */
6606 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006607 second_indent =
6608 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006609 if (second_indent >= 0)
6610 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006611#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006612 if (State & VREPLACE_FLAG)
6613 change_indent(INDENT_SET, second_indent,
6614 FALSE, NUL, TRUE);
6615 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006616#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006617#ifdef FEAT_COMMENTS
6618 if (leader_len > 0 && second_indent - leader_len > 0)
6619 {
6620 int i;
6621 int padding = second_indent - leader_len;
6622
6623 /* We started at the first_line of a numbered list
6624 * that has a comment. the open_line() function has
6625 * inserted the proper comment leader and positioned
6626 * the cursor at the end of the split line. Now we
6627 * add the additional whitespace needed after the
6628 * comment leader for the numbered list. */
6629 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006630 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006631 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006632 }
6633 else
6634 {
6635#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006636 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006637#ifdef FEAT_COMMENTS
6638 }
6639#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006640 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006641 }
6642 first_line = FALSE;
6643 }
6644
6645#ifdef FEAT_VREPLACE
6646 if (State & VREPLACE_FLAG)
6647 {
6648 /*
6649 * In VREPLACE mode we have backspaced over the text to be
6650 * moved, now we re-insert it into the new line.
6651 */
6652 ins_bytes(saved_text);
6653 vim_free(saved_text);
6654 }
6655 else
6656#endif
6657 {
6658 /*
6659 * Check if cursor is not past the NUL off the line, cindent
6660 * may have added or removed indent.
6661 */
6662 curwin->w_cursor.col += startcol;
6663 len = (colnr_T)STRLEN(ml_get_curline());
6664 if (curwin->w_cursor.col > len)
6665 curwin->w_cursor.col = len;
6666 }
6667
6668 haveto_redraw = TRUE;
6669#ifdef FEAT_CINDENT
6670 can_cindent = TRUE;
6671#endif
6672 /* moved the cursor, don't autoindent or cindent now */
6673 did_ai = FALSE;
6674#ifdef FEAT_SMARTINDENT
6675 did_si = FALSE;
6676 can_si = FALSE;
6677 can_si_back = FALSE;
6678#endif
6679 line_breakcheck();
6680 }
6681
6682 if (save_char != NUL) /* put back space after cursor */
6683 pchar_cursor(save_char);
6684
Bram Moolenaar0026d472014-09-09 16:32:39 +02006685#ifdef FEAT_LINEBREAK
6686 curwin->w_p_lbr = has_lbr;
6687#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006688 if (!format_only && haveto_redraw)
6689 {
6690 update_topline();
6691 redraw_curbuf_later(VALID);
6692 }
6693}
6694
6695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 * Called after inserting or deleting text: When 'formatoptions' includes the
6697 * 'a' flag format from the current line until the end of the paragraph.
6698 * Keep the cursor at the same position relative to the text.
6699 * The caller must have saved the cursor line for undo, following ones will be
6700 * saved here.
6701 */
6702 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006703auto_format(
6704 int trailblank, /* when TRUE also format with trailing blank */
6705 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
6707 pos_T pos;
6708 colnr_T len;
6709 char_u *old;
6710 char_u *new, *pnew;
6711 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006712 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713
6714 if (!has_format_option(FO_AUTO))
6715 return;
6716
6717 pos = curwin->w_cursor;
6718 old = ml_get_curline();
6719
6720 /* may remove added space */
6721 check_auto_format(FALSE);
6722
6723 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6724 * user might insert normal text next. Also skip formatting when "1" is
6725 * in 'formatoptions' and there is a single character before the cursor.
6726 * Otherwise the line would be broken and when typing another non-white
6727 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006728 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if (*old != NUL && !trailblank && wasatend)
6730 {
6731 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006732 cc = gchar_cursor();
6733 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6734 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006736 cc = gchar_cursor();
6737 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 {
6739 curwin->w_cursor = pos;
6740 return;
6741 }
6742 curwin->w_cursor = pos;
6743 }
6744
6745#ifdef FEAT_COMMENTS
6746 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6747 * comments. */
6748 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006749 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 return;
6751#endif
6752
6753 /*
6754 * May start formatting in a previous line, so that after "x" a word is
6755 * moved to the previous line if it fits there now. Only when this is not
6756 * the start of a paragraph.
6757 */
6758 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6759 {
6760 --curwin->w_cursor.lnum;
6761 if (u_save_cursor() == FAIL)
6762 return;
6763 }
6764
6765 /*
6766 * Do the formatting and restore the cursor position. "saved_cursor" will
6767 * be adjusted for the text formatting.
6768 */
6769 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006770 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 curwin->w_cursor = saved_cursor;
6772 saved_cursor.lnum = 0;
6773
6774 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6775 {
6776 /* "cannot happen" */
6777 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6778 coladvance((colnr_T)MAXCOL);
6779 }
6780 else
6781 check_cursor_col();
6782
6783 /* Insert mode: If the cursor is now after the end of the line while it
6784 * previously wasn't, the line was broken. Because of the rule above we
6785 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6786 * formatted. */
6787 if (!wasatend && has_format_option(FO_WHITE_PAR))
6788 {
6789 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006790 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 if (curwin->w_cursor.col == len)
6792 {
6793 pnew = vim_strnsave(new, len + 2);
6794 pnew[len] = ' ';
6795 pnew[len + 1] = NUL;
6796 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6797 /* remove the space later */
6798 did_add_space = TRUE;
6799 }
6800 else
6801 /* may remove added space */
6802 check_auto_format(FALSE);
6803 }
6804
6805 check_cursor();
6806}
6807
6808/*
6809 * When an extra space was added to continue a paragraph for auto-formatting,
6810 * delete it now. The space must be under the cursor, just after the insert
6811 * position.
6812 */
6813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006814check_auto_format(
6815 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816{
6817 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006818 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819
6820 if (did_add_space)
6821 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006822 cc = gchar_cursor();
6823 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 /* Somehow the space was removed already. */
6825 did_add_space = FALSE;
6826 else
6827 {
6828 if (!end_insert)
6829 {
6830 inc_cursor();
6831 c = gchar_cursor();
6832 dec_cursor();
6833 }
6834 if (c != NUL)
6835 {
6836 /* The space is no longer at the end of the line, delete it. */
6837 del_char(FALSE);
6838 did_add_space = FALSE;
6839 }
6840 }
6841 }
6842}
6843
6844/*
6845 * Find out textwidth to be used for formatting:
6846 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006847 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 * if invalid value, use 0.
6849 * Set default to window width (maximum 79) for "gq" operator.
6850 */
6851 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006852comp_textwidth(
6853 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854{
6855 int textwidth;
6856
6857 textwidth = curbuf->b_p_tw;
6858 if (textwidth == 0 && curbuf->b_p_wm)
6859 {
6860 /* The width is the window width minus 'wrapmargin' minus all the
6861 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006862 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863#ifdef FEAT_CMDWIN
6864 if (cmdwin_type != 0)
6865 textwidth -= 1;
6866#endif
6867#ifdef FEAT_FOLDING
6868 textwidth -= curwin->w_p_fdc;
6869#endif
6870#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006871 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 textwidth -= 1;
6873#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006874 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 textwidth -= 8;
6876 }
6877 if (textwidth < 0)
6878 textwidth = 0;
6879 if (ff && textwidth == 0)
6880 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006881 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 if (textwidth > 79)
6883 textwidth = 79;
6884 }
6885 return textwidth;
6886}
6887
6888/*
6889 * Put a character in the redo buffer, for when just after a CTRL-V.
6890 */
6891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006892redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894 char_u buf[10];
6895
6896 /* Only digits need special treatment. Translate them into a string of
6897 * three digits. */
6898 if (VIM_ISDIGIT(c))
6899 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006900 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 AppendToRedobuff(buf);
6902 }
6903 else
6904 AppendCharToRedobuff(c);
6905}
6906
6907/*
6908 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006909 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 */
6911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006912start_arrow(
6913 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006915 start_arrow_common(end_insert_pos, TRUE);
6916}
6917
6918/*
6919 * Like start_arrow() but with end_change argument.
6920 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6921 */
6922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006923start_arrow_with_change(
6924 pos_T *end_insert_pos, /* can be NULL */
6925 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006926{
6927 start_arrow_common(end_insert_pos, end_change);
6928 if (!end_change)
6929 {
6930 AppendCharToRedobuff(Ctrl_G);
6931 AppendCharToRedobuff('U');
6932 }
6933}
6934
6935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006936start_arrow_common(
6937 pos_T *end_insert_pos, /* can be NULL */
6938 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006939{
6940 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {
6942 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006943 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 arrow_used = TRUE; /* this means we stopped the current insert */
6945 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006946#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006947 check_spell_redraw();
6948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949}
6950
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006951#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006952/*
6953 * If we skipped highlighting word at cursor, do it now.
6954 * It may be skipped again, thus reset spell_redraw_lnum first.
6955 */
6956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006957check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006958{
6959 if (spell_redraw_lnum != 0)
6960 {
6961 linenr_T lnum = spell_redraw_lnum;
6962
6963 spell_redraw_lnum = 0;
6964 redrawWinline(lnum, FALSE);
6965 }
6966}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006967
6968/*
6969 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6970 * spelled word, if there is one.
6971 */
6972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006973spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006974{
6975 pos_T tpos = curwin->w_cursor;
6976
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006977 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006978 if (curwin->w_cursor.col != tpos.col)
6979 start_arrow(&tpos);
6980}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006981#endif
6982
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983/*
6984 * stop_arrow() is called before a change is made in insert mode.
6985 * If an arrow key has been used, start a new insertion.
6986 * Returns FAIL if undo is impossible, shouldn't insert then.
6987 */
6988 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006989stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
6991 if (arrow_used)
6992 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006993 Insstart = curwin->w_cursor; /* new insertion starts here */
6994 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6995 /* Don't update the original insert position when moved to the
6996 * right, except when nothing was inserted yet. */
6997 update_Insstart_orig = FALSE;
6998 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 if (u_save_cursor() == OK)
7001 {
7002 arrow_used = FALSE;
7003 ins_need_undo = FALSE;
7004 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007005
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 ai_col = 0;
7007#ifdef FEAT_VREPLACE
7008 if (State & VREPLACE_FLAG)
7009 {
7010 orig_line_count = curbuf->b_ml.ml_line_count;
7011 vr_lines_changed = 1;
7012 }
7013#endif
7014 ResetRedobuff();
7015 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007016 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 }
7018 else if (ins_need_undo)
7019 {
7020 if (u_save_cursor() == OK)
7021 ins_need_undo = FALSE;
7022 }
7023
7024#ifdef FEAT_FOLDING
7025 /* Always open fold at the cursor line when inserting something. */
7026 foldOpenCursor();
7027#endif
7028
7029 return (arrow_used || ins_need_undo ? FAIL : OK);
7030}
7031
7032/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007033 * Do a few things to stop inserting.
7034 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7035 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 */
7037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007038stop_insert(
7039 pos_T *end_insert_pos,
7040 int esc, /* called by ins_esc() */
7041 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007043 int cc;
7044 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
7046 stop_redo_ins();
7047 replace_flush(); /* abandon replace stack */
7048
7049 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007050 * Save the inserted text for later redo with ^@ and CTRL-A.
7051 * Don't do it when "restart_edit" was set and nothing was inserted,
7052 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007054 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007055 if (did_restart_edit == 0 || (ptr != NULL
7056 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007057 {
7058 vim_free(last_insert);
7059 last_insert = ptr;
7060 last_insert_skip = new_insert_skip;
7061 }
7062 else
7063 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007065 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 {
7067 /* Auto-format now. It may seem strange to do this when stopping an
7068 * insertion (or moving the cursor), but it's required when appending
7069 * a line and having it end in a space. But only do it when something
7070 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007071 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007073 pos_T tpos = curwin->w_cursor;
7074
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 /* When the cursor is at the end of the line after a space the
7076 * formatting will move it to the following word. Avoid that by
7077 * moving the cursor onto the space. */
7078 cc = 'x';
7079 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7080 {
7081 dec_cursor();
7082 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007083 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007084 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 }
7086
7087 auto_format(TRUE, FALSE);
7088
Bram Moolenaar1c465442017-03-12 20:10:05 +01007089 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007090 {
7091 if (gchar_cursor() != NUL)
7092 inc_cursor();
7093#ifdef FEAT_VIRTUALEDIT
7094 /* If the cursor is still at the same character, also keep
7095 * the "coladd". */
7096 if (gchar_cursor() == NUL
7097 && curwin->w_cursor.lnum == tpos.lnum
7098 && curwin->w_cursor.col == tpos.col)
7099 curwin->w_cursor.coladd = tpos.coladd;
7100#endif
7101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 }
7103
7104 /* If a space was inserted for auto-formatting, remove it now. */
7105 check_auto_format(TRUE);
7106
7107 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007108 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007109 * Do this when ESC was used or moving the cursor up/down.
7110 * Check for the old position still being valid, just in case the text
7111 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007112 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007113 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7114 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007116 pos_T tpos = curwin->w_cursor;
7117
7118 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007119 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007120 for (;;)
7121 {
7122 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7123 --curwin->w_cursor.col;
7124 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007125 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007126 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007127 if (del_char(TRUE) == FAIL)
7128 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007129 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007130 if (curwin->w_cursor.lnum != tpos.lnum)
7131 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007132 else
7133 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007134 /* reset tpos, could have been invalidated in the loop above */
7135 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007136 tpos.col++;
7137 if (cc != NUL && gchar_pos(&tpos) == NUL)
7138 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 /* <C-S-Right> may have started Visual mode, adjust the position for
7142 * deleted characters. */
7143 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7144 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007145 int len = (int)STRLEN(ml_get_curline());
7146
7147 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007149 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007150#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 }
7154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 }
7156 }
7157 did_ai = FALSE;
7158#ifdef FEAT_SMARTINDENT
7159 did_si = FALSE;
7160 can_si = FALSE;
7161 can_si_back = FALSE;
7162#endif
7163
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007164 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7165 * now in a different buffer. */
7166 if (end_insert_pos != NULL)
7167 {
7168 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007169 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007170 curbuf->b_op_end = *end_insert_pos;
7171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172}
7173
7174/*
7175 * Set the last inserted text to a single character.
7176 * Used for the replace command.
7177 */
7178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007179set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180{
7181 char_u *s;
7182
7183 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 if (last_insert != NULL)
7186 {
7187 s = last_insert;
7188 /* Use the CTRL-V only when entering a special char */
7189 if (c < ' ' || c == DEL)
7190 *s++ = Ctrl_V;
7191 s = add_char2buf(c, s);
7192 *s++ = ESC;
7193 *s++ = NUL;
7194 last_insert_skip = 0;
7195 }
7196}
7197
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007198#if defined(EXITFREE) || defined(PROTO)
7199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007200free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007201{
Bram Moolenaar15675582018-02-09 12:29:56 +01007202 vim_clear((void **)&last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007203# ifdef FEAT_INS_EXPAND
Bram Moolenaar15675582018-02-09 12:29:56 +01007204 vim_clear((void **)&compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007205# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007206}
7207#endif
7208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209/*
7210 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7211 * and CSI. Handle multi-byte characters.
7212 * Returns a pointer to after the added bytes.
7213 */
7214 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007215add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007218 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 int i;
7220 int len;
7221
7222 len = (*mb_char2bytes)(c, temp);
7223 for (i = 0; i < len; ++i)
7224 {
7225 c = temp[i];
7226#endif
7227 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7228 if (c == K_SPECIAL)
7229 {
7230 *s++ = K_SPECIAL;
7231 *s++ = KS_SPECIAL;
7232 *s++ = KE_FILLER;
7233 }
7234#ifdef FEAT_GUI
7235 else if (c == CSI)
7236 {
7237 *s++ = CSI;
7238 *s++ = KS_EXTRA;
7239 *s++ = (int)KE_CSI;
7240 }
7241#endif
7242 else
7243 *s++ = c;
7244#ifdef FEAT_MBYTE
7245 }
7246#endif
7247 return s;
7248}
7249
7250/*
7251 * move cursor to start of line
7252 * if flags & BL_WHITE move to first non-white
7253 * if flags & BL_SOL move to first non-white if startofline is set,
7254 * otherwise keep "curswant" column
7255 * if flags & BL_FIX don't leave the cursor on a NUL.
7256 */
7257 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007258beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259{
7260 if ((flags & BL_SOL) && !p_sol)
7261 coladvance(curwin->w_curswant);
7262 else
7263 {
7264 curwin->w_cursor.col = 0;
7265#ifdef FEAT_VIRTUALEDIT
7266 curwin->w_cursor.coladd = 0;
7267#endif
7268
7269 if (flags & (BL_WHITE | BL_SOL))
7270 {
7271 char_u *ptr;
7272
Bram Moolenaar1c465442017-03-12 20:10:05 +01007273 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7275 ++curwin->w_cursor.col;
7276 }
7277 curwin->w_set_curswant = TRUE;
7278 }
7279}
7280
7281/*
7282 * oneright oneleft cursor_down cursor_up
7283 *
7284 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007285 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 * Return OK when successful, FAIL when we hit a line of file boundary.
7287 */
7288
7289 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007290oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
7292 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
7295#ifdef FEAT_VIRTUALEDIT
7296 if (virtual_active())
7297 {
7298 pos_T prevpos = curwin->w_cursor;
7299
7300 /* Adjust for multi-wide char (excluding TAB) */
7301 ptr = ml_get_cursor();
7302 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007303# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007305# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 ))
7309 ? ptr2cells(ptr) : 1));
7310 curwin->w_set_curswant = TRUE;
7311 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7312 return (prevpos.col != curwin->w_cursor.col
7313 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7314 }
7315#endif
7316
7317 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007318 if (*ptr == NUL)
7319 return FAIL; /* already at the very end */
7320
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007322 if (has_mbyte)
7323 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 else
7325#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007326 l = 1;
7327
7328 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7329 * contains "onemore". */
7330 if (ptr[l] == NUL
7331#ifdef FEAT_VIRTUALEDIT
7332 && (ve_flags & VE_ONEMORE) == 0
7333#endif
7334 )
7335 return FAIL;
7336 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337
7338 curwin->w_set_curswant = TRUE;
7339 return OK;
7340}
7341
7342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007343oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344{
7345#ifdef FEAT_VIRTUALEDIT
7346 if (virtual_active())
7347 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007348# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 int v = getviscol();
7352
7353 if (v == 0)
7354 return FAIL;
7355
7356# ifdef FEAT_LINEBREAK
7357 /* We might get stuck on 'showbreak', skip over it. */
7358 width = 1;
7359 for (;;)
7360 {
7361 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007362 /* getviscol() is slow, skip it when 'showbreak' is empty,
7363 * 'breakindent' is not set and there are no multi-byte
7364 * characters */
7365 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366# ifdef FEAT_MBYTE
7367 && !has_mbyte
7368# endif
7369 ) || getviscol() < v)
7370 break;
7371 ++width;
7372 }
7373# else
7374 coladvance(v - 1);
7375# endif
7376
7377 if (curwin->w_cursor.coladd == 1)
7378 {
7379 char_u *ptr;
7380
7381 /* Adjust for multi-wide char (not a TAB) */
7382 ptr = ml_get_cursor();
7383 if (*ptr != TAB && vim_isprintc(
7384# ifdef FEAT_MBYTE
7385 (*mb_ptr2char)(ptr)
7386# else
7387 *ptr
7388# endif
7389 ) && ptr2cells(ptr) > 1)
7390 curwin->w_cursor.coladd = 0;
7391 }
7392
7393 curwin->w_set_curswant = TRUE;
7394 return OK;
7395 }
7396#endif
7397
7398 if (curwin->w_cursor.col == 0)
7399 return FAIL;
7400
7401 curwin->w_set_curswant = TRUE;
7402 --curwin->w_cursor.col;
7403
7404#ifdef FEAT_MBYTE
7405 /* if the character on the left of the current cursor is a multi-byte
7406 * character, move to its first byte */
7407 if (has_mbyte)
7408 mb_adjust_cursor();
7409#endif
7410 return OK;
7411}
7412
7413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007414cursor_up(
7415 long n,
7416 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
7418 linenr_T lnum;
7419
7420 if (n > 0)
7421 {
7422 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007423 /* This fails if the cursor is already in the first line or the count
7424 * is larger than the line number and '-' is in 'cpoptions' */
7425 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 return FAIL;
7427 if (n >= lnum)
7428 lnum = 1;
7429 else
7430#ifdef FEAT_FOLDING
7431 if (hasAnyFolding(curwin))
7432 {
7433 /*
7434 * Count each sequence of folded lines as one logical line.
7435 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007436 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 (void)hasFolding(lnum, &lnum, NULL);
7438
7439 while (n--)
7440 {
7441 /* move up one line */
7442 --lnum;
7443 if (lnum <= 1)
7444 break;
7445 /* If we entered a fold, move to the beginning, unless in
7446 * Insert mode or when 'foldopen' contains "all": it will open
7447 * in a moment. */
7448 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7449 (void)hasFolding(lnum, &lnum, NULL);
7450 }
7451 if (lnum < 1)
7452 lnum = 1;
7453 }
7454 else
7455#endif
7456 lnum -= n;
7457 curwin->w_cursor.lnum = lnum;
7458 }
7459
7460 /* try to advance to the column we want to be at */
7461 coladvance(curwin->w_curswant);
7462
7463 if (upd_topline)
7464 update_topline(); /* make sure curwin->w_topline is valid */
7465
7466 return OK;
7467}
7468
7469/*
7470 * Cursor down a number of logical lines.
7471 */
7472 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007473cursor_down(
7474 long n,
7475 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476{
7477 linenr_T lnum;
7478
7479 if (n > 0)
7480 {
7481 lnum = curwin->w_cursor.lnum;
7482#ifdef FEAT_FOLDING
7483 /* Move to last line of fold, will fail if it's the end-of-file. */
7484 (void)hasFolding(lnum, NULL, &lnum);
7485#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007486 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007487 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007488 if (lnum >= curbuf->b_ml.ml_line_count
7489 || (lnum + n > curbuf->b_ml.ml_line_count
7490 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 return FAIL;
7492 if (lnum + n >= curbuf->b_ml.ml_line_count)
7493 lnum = curbuf->b_ml.ml_line_count;
7494 else
7495#ifdef FEAT_FOLDING
7496 if (hasAnyFolding(curwin))
7497 {
7498 linenr_T last;
7499
7500 /* count each sequence of folded lines as one logical line */
7501 while (n--)
7502 {
7503 if (hasFolding(lnum, NULL, &last))
7504 lnum = last + 1;
7505 else
7506 ++lnum;
7507 if (lnum >= curbuf->b_ml.ml_line_count)
7508 break;
7509 }
7510 if (lnum > curbuf->b_ml.ml_line_count)
7511 lnum = curbuf->b_ml.ml_line_count;
7512 }
7513 else
7514#endif
7515 lnum += n;
7516 curwin->w_cursor.lnum = lnum;
7517 }
7518
7519 /* try to advance to the column we want to be at */
7520 coladvance(curwin->w_curswant);
7521
7522 if (upd_topline)
7523 update_topline(); /* make sure curwin->w_topline is valid */
7524
7525 return OK;
7526}
7527
7528/*
7529 * Stuff the last inserted text in the read buffer.
7530 * Last_insert actually is a copy of the redo buffer, so we
7531 * first have to remove the command.
7532 */
7533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007534stuff_inserted(
7535 int c, /* Command character to be inserted */
7536 long count, /* Repeat this many times */
7537 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538{
7539 char_u *esc_ptr;
7540 char_u *ptr;
7541 char_u *last_ptr;
7542 char_u last = NUL;
7543
7544 ptr = get_last_insert();
7545 if (ptr == NULL)
7546 {
7547 EMSG(_(e_noinstext));
7548 return FAIL;
7549 }
7550
7551 /* may want to stuff the command character, to start Insert mode */
7552 if (c != NUL)
7553 stuffcharReadbuff(c);
7554 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7555 *esc_ptr = NUL; /* remove the ESC */
7556
7557 /* when the last char is either "0" or "^" it will be quoted if no ESC
7558 * comes after it OR if it will inserted more than once and "ptr"
7559 * starts with ^D. -- Acevedo
7560 */
7561 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7562 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7563 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7564 {
7565 last = *last_ptr;
7566 *last_ptr = NUL;
7567 }
7568
7569 do
7570 {
7571 stuffReadbuff(ptr);
7572 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7573 if (last)
7574 stuffReadbuff((char_u *)(last == '0'
7575 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7576 : IF_EB("\026^", CTRL_V_STR "^")));
7577 }
7578 while (--count > 0);
7579
7580 if (last)
7581 *last_ptr = last;
7582
7583 if (esc_ptr != NULL)
7584 *esc_ptr = ESC; /* put the ESC back */
7585
7586 /* may want to stuff a trailing ESC, to get out of Insert mode */
7587 if (!no_esc)
7588 stuffcharReadbuff(ESC);
7589
7590 return OK;
7591}
7592
7593 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007594get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
7596 if (last_insert == NULL)
7597 return NULL;
7598 return last_insert + last_insert_skip;
7599}
7600
7601/*
7602 * Get last inserted string, and remove trailing <Esc>.
7603 * Returns pointer to allocated memory (must be freed) or NULL.
7604 */
7605 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007606get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
7608 char_u *s;
7609 int len;
7610
7611 if (last_insert == NULL)
7612 return NULL;
7613 s = vim_strsave(last_insert + last_insert_skip);
7614 if (s != NULL)
7615 {
7616 len = (int)STRLEN(s);
7617 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7618 s[len - 1] = NUL;
7619 }
7620 return s;
7621}
7622
7623/*
7624 * Check the word in front of the cursor for an abbreviation.
7625 * Called when the non-id character "c" has been entered.
7626 * When an abbreviation is recognized it is removed from the text and
7627 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7628 */
7629 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007630echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631{
7632 /* Don't check for abbreviation in paste mode, when disabled and just
7633 * after moving around with cursor keys. */
7634 if (p_paste || no_abbr || arrow_used)
7635 return FALSE;
7636
7637 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7638 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7639}
7640
7641/*
7642 * replace-stack functions
7643 *
7644 * When replacing characters, the replaced characters are remembered for each
7645 * new character. This is used to re-insert the old text when backspacing.
7646 *
7647 * There is a NUL headed list of characters for each character that is
7648 * currently in the file after the insertion point. When BS is used, one NUL
7649 * headed list is put back for the deleted character.
7650 *
7651 * For a newline, there are two NUL headed lists. One contains the characters
7652 * that the NL replaced. The extra one stores the characters after the cursor
7653 * that were deleted (always white space).
7654 *
7655 * Replace_offset is normally 0, in which case replace_push will add a new
7656 * character at the end of the stack. If replace_offset is not 0, that many
7657 * characters will be left on the stack above the newly inserted character.
7658 */
7659
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007660static char_u *replace_stack = NULL;
7661static long replace_stack_nr = 0; /* next entry in replace stack */
7662static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663
7664 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007665replace_push(
7666 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667{
7668 char_u *p;
7669
7670 if (replace_stack_nr < replace_offset) /* nothing to do */
7671 return;
7672 if (replace_stack_len <= replace_stack_nr)
7673 {
7674 replace_stack_len += 50;
7675 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7676 if (p == NULL) /* out of memory */
7677 {
7678 replace_stack_len -= 50;
7679 return;
7680 }
7681 if (replace_stack != NULL)
7682 {
7683 mch_memmove(p, replace_stack,
7684 (size_t)(replace_stack_nr * sizeof(char_u)));
7685 vim_free(replace_stack);
7686 }
7687 replace_stack = p;
7688 }
7689 p = replace_stack + replace_stack_nr - replace_offset;
7690 if (replace_offset)
7691 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7692 *p = c;
7693 ++replace_stack_nr;
7694}
7695
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007696#if defined(FEAT_MBYTE) || defined(PROTO)
7697/*
7698 * Push a character onto the replace stack. Handles a multi-byte character in
7699 * reverse byte order, so that the first byte is popped off first.
7700 * Return the number of bytes done (includes composing characters).
7701 */
7702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007703replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007704{
7705 int l = (*mb_ptr2len)(p);
7706 int j;
7707
7708 for (j = l - 1; j >= 0; --j)
7709 replace_push(p[j]);
7710 return l;
7711}
7712#endif
7713
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714/*
7715 * Pop one item from the replace stack.
7716 * return -1 if stack empty
7717 * return replaced character or NUL otherwise
7718 */
7719 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007720replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
7722 if (replace_stack_nr == 0)
7723 return -1;
7724 return (int)replace_stack[--replace_stack_nr];
7725}
7726
7727/*
7728 * Join the top two items on the replace stack. This removes to "off"'th NUL
7729 * encountered.
7730 */
7731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007732replace_join(
7733 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734{
7735 int i;
7736
7737 for (i = replace_stack_nr; --i >= 0; )
7738 if (replace_stack[i] == NUL && off-- <= 0)
7739 {
7740 --replace_stack_nr;
7741 mch_memmove(replace_stack + i, replace_stack + i + 1,
7742 (size_t)(replace_stack_nr - i));
7743 return;
7744 }
7745}
7746
7747/*
7748 * Pop bytes from the replace stack until a NUL is found, and insert them
7749 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7750 */
7751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007752replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754 int cc;
7755 int oldState = State;
7756
7757 State = NORMAL; /* don't want REPLACE here */
7758 while ((cc = replace_pop()) > 0)
7759 {
7760#ifdef FEAT_MBYTE
7761 mb_replace_pop_ins(cc);
7762#else
7763 ins_char(cc);
7764#endif
7765 dec_cursor();
7766 }
7767 State = oldState;
7768}
7769
7770#ifdef FEAT_MBYTE
7771/*
7772 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7773 * indicates a multi-byte char, pop the other bytes too.
7774 */
7775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007776mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007779 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 int i;
7781 int c;
7782
7783 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7784 {
7785 buf[0] = cc;
7786 for (i = 1; i < n; ++i)
7787 buf[i] = replace_pop();
7788 ins_bytes_len(buf, n);
7789 }
7790 else
7791 ins_char(cc);
7792
7793 if (enc_utf8)
7794 /* Handle composing chars. */
7795 for (;;)
7796 {
7797 c = replace_pop();
7798 if (c == -1) /* stack empty */
7799 break;
7800 if ((n = MB_BYTE2LEN(c)) == 1)
7801 {
7802 /* Not a multi-byte char, put it back. */
7803 replace_push(c);
7804 break;
7805 }
7806 else
7807 {
7808 buf[0] = c;
7809 for (i = 1; i < n; ++i)
7810 buf[i] = replace_pop();
7811 if (utf_iscomposing(utf_ptr2char(buf)))
7812 ins_bytes_len(buf, n);
7813 else
7814 {
7815 /* Not a composing char, put it back. */
7816 for (i = n - 1; i >= 0; --i)
7817 replace_push(buf[i]);
7818 break;
7819 }
7820 }
7821 }
7822}
7823#endif
7824
7825/*
7826 * make the replace stack empty
7827 * (called when exiting replace mode)
7828 */
7829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007830replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831{
Bram Moolenaar15675582018-02-09 12:29:56 +01007832 vim_clear((void **)&replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 replace_stack_len = 0;
7834 replace_stack_nr = 0;
7835}
7836
7837/*
7838 * Handle doing a BS for one character.
7839 * cc < 0: replace stack empty, just move cursor
7840 * cc == 0: character was inserted, delete it
7841 * cc > 0: character was replaced, put cc (first byte of original char) back
7842 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007843 * When "limit_col" is >= 0, don't delete before this column. Matters when
7844 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 */
7846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007847replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848{
7849 int cc;
7850#ifdef FEAT_VREPLACE
7851 int orig_len = 0;
7852 int ins_len;
7853 int orig_vcols = 0;
7854 colnr_T start_vcol;
7855 char_u *p;
7856 int i;
7857 int vcol;
7858#endif
7859
7860 cc = replace_pop();
7861 if (cc > 0)
7862 {
7863#ifdef FEAT_VREPLACE
7864 if (State & VREPLACE_FLAG)
7865 {
7866 /* Get the number of screen cells used by the character we are
7867 * going to delete. */
7868 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7869 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7870 }
7871#endif
7872#ifdef FEAT_MBYTE
7873 if (has_mbyte)
7874 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007875 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876# ifdef FEAT_VREPLACE
7877 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007878 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879# endif
7880 replace_push(cc);
7881 }
7882 else
7883#endif
7884 {
7885 pchar_cursor(cc);
7886#ifdef FEAT_VREPLACE
7887 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007888 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889#endif
7890 }
7891 replace_pop_ins();
7892
7893#ifdef FEAT_VREPLACE
7894 if (State & VREPLACE_FLAG)
7895 {
7896 /* Get the number of screen cells used by the inserted characters */
7897 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007898 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 vcol = start_vcol;
7900 for (i = 0; i < ins_len; ++i)
7901 {
7902 vcol += chartabsize(p + i, vcol);
7903#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007904 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905#endif
7906 }
7907 vcol -= start_vcol;
7908
7909 /* Delete spaces that were inserted after the cursor to keep the
7910 * text aligned. */
7911 curwin->w_cursor.col += ins_len;
7912 while (vcol > orig_vcols && gchar_cursor() == ' ')
7913 {
7914 del_char(FALSE);
7915 ++orig_vcols;
7916 }
7917 curwin->w_cursor.col -= ins_len;
7918 }
7919#endif
7920
7921 /* mark the buffer as changed and prepare for displaying */
7922 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7923 }
7924 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007925 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926}
7927
7928#ifdef FEAT_CINDENT
7929/*
7930 * Return TRUE if C-indenting is on.
7931 */
7932 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007933cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
7935 return (!p_paste && (curbuf->b_p_cin
7936# ifdef FEAT_EVAL
7937 || *curbuf->b_p_inde != NUL
7938# endif
7939 ));
7940}
7941#endif
7942
7943#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7944/*
7945 * Re-indent the current line, based on the current contents of it and the
7946 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7947 * confused what all the part that handles Control-T is doing that I'm not.
7948 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7949 */
7950
7951 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007952fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007954 int amount = get_the_indent();
7955
7956 if (amount >= 0)
7957 {
7958 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7959 if (linewhite(curwin->w_cursor.lnum))
7960 did_ai = TRUE; /* delete the indent if the line stays empty */
7961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962}
7963
7964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007965fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966{
7967 if (p_paste)
7968 return;
7969# ifdef FEAT_LISP
7970 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7971 fixthisline(get_lisp_indent);
7972# endif
7973# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7974 else
7975# endif
7976# ifdef FEAT_CINDENT
7977 if (cindent_on())
7978 do_c_expr_indent();
7979# endif
7980}
7981
7982#endif
7983
7984#ifdef FEAT_CINDENT
7985/*
7986 * return TRUE if 'cinkeys' contains the key "keytyped",
7987 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007988 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7990 *
7991 * "keytyped" can have a few special values:
7992 * KEY_OPEN_FORW
7993 * KEY_OPEN_BACK
7994 * KEY_COMPLETE just finished completion.
7995 *
7996 * If line_is_empty is TRUE accept keys with '0' before them.
7997 */
7998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007999in_cinkeys(
8000 int keytyped,
8001 int when,
8002 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003{
8004 char_u *look;
8005 int try_match;
8006 int try_match_word;
8007 char_u *p;
8008 char_u *line;
8009 int icase;
8010 int i;
8011
Bram Moolenaar30848942009-12-24 14:46:12 +00008012 if (keytyped == NUL)
8013 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8014 return FALSE;
8015
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016#ifdef FEAT_EVAL
8017 if (*curbuf->b_p_inde != NUL)
8018 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8019 else
8020#endif
8021 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8022 while (*look)
8023 {
8024 /*
8025 * Find out if we want to try a match with this key, depending on
8026 * 'when' and a '*' or '!' before the key.
8027 */
8028 switch (when)
8029 {
8030 case '*': try_match = (*look == '*'); break;
8031 case '!': try_match = (*look == '!'); break;
8032 default: try_match = (*look != '*'); break;
8033 }
8034 if (*look == '*' || *look == '!')
8035 ++look;
8036
8037 /*
8038 * If there is a '0', only accept a match if the line is empty.
8039 * But may still match when typing last char of a word.
8040 */
8041 if (*look == '0')
8042 {
8043 try_match_word = try_match;
8044 if (!line_is_empty)
8045 try_match = FALSE;
8046 ++look;
8047 }
8048 else
8049 try_match_word = FALSE;
8050
8051 /*
8052 * does it look like a control character?
8053 */
8054 if (*look == '^'
8055#ifdef EBCDIC
8056 && (Ctrl_chr(look[1]) != 0)
8057#else
8058 && look[1] >= '?' && look[1] <= '_'
8059#endif
8060 )
8061 {
8062 if (try_match && keytyped == Ctrl_chr(look[1]))
8063 return TRUE;
8064 look += 2;
8065 }
8066 /*
8067 * 'o' means "o" command, open forward.
8068 * 'O' means "O" command, open backward.
8069 */
8070 else if (*look == 'o')
8071 {
8072 if (try_match && keytyped == KEY_OPEN_FORW)
8073 return TRUE;
8074 ++look;
8075 }
8076 else if (*look == 'O')
8077 {
8078 if (try_match && keytyped == KEY_OPEN_BACK)
8079 return TRUE;
8080 ++look;
8081 }
8082
8083 /*
8084 * 'e' means to check for "else" at start of line and just before the
8085 * cursor.
8086 */
8087 else if (*look == 'e')
8088 {
8089 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8090 {
8091 p = ml_get_curline();
8092 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8093 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8094 return TRUE;
8095 }
8096 ++look;
8097 }
8098
8099 /*
8100 * ':' only causes an indent if it is at the end of a label or case
8101 * statement, or when it was before typing the ':' (to fix
8102 * class::method for C++).
8103 */
8104 else if (*look == ':')
8105 {
8106 if (try_match && keytyped == ':')
8107 {
8108 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008109 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008111 /* Need to get the line again after cin_islabel(). */
8112 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 if (curwin->w_cursor.col > 2
8114 && p[curwin->w_cursor.col - 1] == ':'
8115 && p[curwin->w_cursor.col - 2] == ':')
8116 {
8117 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008118 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008119 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 p = ml_get_curline();
8121 p[curwin->w_cursor.col - 1] = ':';
8122 if (i)
8123 return TRUE;
8124 }
8125 }
8126 ++look;
8127 }
8128
8129
8130 /*
8131 * Is it a key in <>, maybe?
8132 */
8133 else if (*look == '<')
8134 {
8135 if (try_match)
8136 {
8137 /*
8138 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8139 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8140 * >, *, : and ! keys if they really really want to.
8141 */
8142 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8143 && keytyped == look[1])
8144 return TRUE;
8145
8146 if (keytyped == get_special_key_code(look + 1))
8147 return TRUE;
8148 }
8149 while (*look && *look != '>')
8150 look++;
8151 while (*look == '>')
8152 look++;
8153 }
8154
8155 /*
8156 * Is it a word: "=word"?
8157 */
8158 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8159 {
8160 ++look;
8161 if (*look == '~')
8162 {
8163 icase = TRUE;
8164 ++look;
8165 }
8166 else
8167 icase = FALSE;
8168 p = vim_strchr(look, ',');
8169 if (p == NULL)
8170 p = look + STRLEN(look);
8171 if ((try_match || try_match_word)
8172 && curwin->w_cursor.col >= (colnr_T)(p - look))
8173 {
8174 int match = FALSE;
8175
8176#ifdef FEAT_INS_EXPAND
8177 if (keytyped == KEY_COMPLETE)
8178 {
8179 char_u *s;
8180
8181 /* Just completed a word, check if it starts with "look".
8182 * search back for the start of a word. */
8183 line = ml_get_curline();
8184# ifdef FEAT_MBYTE
8185 if (has_mbyte)
8186 {
8187 char_u *n;
8188
8189 for (s = line + curwin->w_cursor.col; s > line; s = n)
8190 {
8191 n = mb_prevptr(line, s);
8192 if (!vim_iswordp(n))
8193 break;
8194 }
8195 }
8196 else
8197# endif
8198 for (s = line + curwin->w_cursor.col; s > line; --s)
8199 if (!vim_iswordc(s[-1]))
8200 break;
8201 if (s + (p - look) <= line + curwin->w_cursor.col
8202 && (icase
8203 ? MB_STRNICMP(s, look, p - look)
8204 : STRNCMP(s, look, p - look)) == 0)
8205 match = TRUE;
8206 }
8207 else
8208#endif
8209 /* TODO: multi-byte */
8210 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8211 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8212 {
8213 line = ml_get_cursor();
8214 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8215 || !vim_iswordc(line[-(p - look) - 1]))
8216 && (icase
8217 ? MB_STRNICMP(line - (p - look), look, p - look)
8218 : STRNCMP(line - (p - look), look, p - look))
8219 == 0)
8220 match = TRUE;
8221 }
8222 if (match && try_match_word && !try_match)
8223 {
8224 /* "0=word": Check if there are only blanks before the
8225 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008226 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 (int)(curwin->w_cursor.col - (p - look)))
8228 match = FALSE;
8229 }
8230 if (match)
8231 return TRUE;
8232 }
8233 look = p;
8234 }
8235
8236 /*
8237 * ok, it's a boring generic character.
8238 */
8239 else
8240 {
8241 if (try_match && *look == keytyped)
8242 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008243 if (*look != NUL)
8244 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 }
8246
8247 /*
8248 * Skip over ", ".
8249 */
8250 look = skip_to_option_part(look);
8251 }
8252 return FALSE;
8253}
8254#endif /* FEAT_CINDENT */
8255
8256#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8257/*
8258 * Map Hebrew keyboard when in hkmap mode.
8259 */
8260 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008261hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262{
8263 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8264 {
8265 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8266 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8267 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8268 static char_u map[26] =
8269 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8270 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8271 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8272 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8273 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8274 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8275 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8276 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8277 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8278
8279 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8280 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8281 /* '-1'='sofit' */
8282 else if (c == 'x')
8283 return 'X';
8284 else if (c == 'q')
8285 return '\''; /* {geresh}={'} */
8286 else if (c == 246)
8287 return ' '; /* \"o --> ' ' for a german keyboard */
8288 else if (c == 228)
8289 return ' '; /* \"a --> ' ' -- / -- */
8290 else if (c == 252)
8291 return ' '; /* \"u --> ' ' -- / -- */
8292#ifdef EBCDIC
8293 else if (islower(c))
8294#else
8295 /* NOTE: islower() does not do the right thing for us on Linux so we
8296 * do this the same was as 5.7 and previous, so it works correctly on
8297 * all systems. Specifically, the e.g. Delete and Arrow keys are
8298 * munged and won't work if e.g. searching for Hebrew text.
8299 */
8300 else if (c >= 'a' && c <= 'z')
8301#endif
8302 return (int)(map[CharOrdLow(c)] + p_aleph);
8303 else
8304 return c;
8305 }
8306 else
8307 {
8308 switch (c)
8309 {
8310 case '`': return ';';
8311 case '/': return '.';
8312 case '\'': return ',';
8313 case 'q': return '/';
8314 case 'w': return '\'';
8315
8316 /* Hebrew letters - set offset from 'a' */
8317 case ',': c = '{'; break;
8318 case '.': c = 'v'; break;
8319 case ';': c = 't'; break;
8320 default: {
8321 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8322
8323#ifdef EBCDIC
8324 /* see note about islower() above */
8325 if (!islower(c))
8326#else
8327 if (c < 'a' || c > 'z')
8328#endif
8329 return c;
8330 c = str[CharOrdLow(c)];
8331 break;
8332 }
8333 }
8334
8335 return (int)(CharOrdLow(c) + p_aleph);
8336 }
8337}
8338#endif
8339
8340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008341ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342{
8343 int need_redraw = FALSE;
8344 int regname;
8345 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008346 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347
8348 /*
8349 * If we are going to wait for a character, show a '"'.
8350 */
8351 pc_status = PC_STATUS_UNSET;
8352 if (redrawing() && !char_avail())
8353 {
8354 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008355 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
8357 edit_putchar('"', TRUE);
8358#ifdef FEAT_CMDL_INFO
8359 add_to_showcmd_c(Ctrl_R);
8360#endif
8361 }
8362
8363#ifdef USE_ON_FLY_SCROLL
8364 dont_scroll = TRUE; /* disallow scrolling here */
8365#endif
8366
8367 /*
8368 * Don't map the register name. This also prevents the mode message to be
8369 * deleted when ESC is hit.
8370 */
8371 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008372 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8375 {
8376 /* Get a third key for literal register insertion */
8377 literally = regname;
8378#ifdef FEAT_CMDL_INFO
8379 add_to_showcmd_c(literally);
8380#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008381 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 }
8384 --no_mapping;
8385
8386#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008387 /* Don't call u_sync() while typing the expression or giving an error
8388 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 ++no_u_sync;
8390 if (regname == '=')
8391 {
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008392# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008394# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008395 /* Sync undo when evaluating the expression calls setline() or
8396 * append(), so that it can be undone separately. */
8397 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008398
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 regname = get_expr_register();
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008400# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 /* Restore the Input Method. */
8402 if (im_on)
8403 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008406 if (regname == NUL || !valid_yank_reg(regname, FALSE))
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 '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 else
8412 {
8413#endif
8414 if (literally == Ctrl_O || literally == Ctrl_P)
8415 {
8416 /* Append the command to the redo buffer. */
8417 AppendCharToRedobuff(Ctrl_R);
8418 AppendCharToRedobuff(literally);
8419 AppendCharToRedobuff(regname);
8420
8421 do_put(regname, BACKWARD, 1L,
8422 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8423 }
8424 else if (insert_reg(regname, literally) == FAIL)
8425 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008426 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 need_redraw = TRUE; /* remove the '"' */
8428 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008429 else if (stop_insert_mode)
8430 /* When the '=' register was used and a function was invoked that
8431 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8432 * insert anything, need to remove the '"' */
8433 need_redraw = TRUE;
8434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435#ifdef FEAT_EVAL
8436 }
8437 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008438 if (u_sync_once == 1)
8439 ins_need_undo = TRUE;
8440 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441#endif
8442#ifdef FEAT_CMDL_INFO
8443 clear_showcmd();
8444#endif
8445
8446 /* If the inserted register is empty, we need to remove the '"' */
8447 if (need_redraw || stuff_empty())
8448 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008449
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008450 /* Disallow starting Visual mode here, would get a weird mode. */
8451 if (!vis_active && VIsual_active)
8452 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453}
8454
8455/*
8456 * CTRL-G commands in Insert mode.
8457 */
8458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008459ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461 int c;
8462
8463#ifdef FEAT_INS_EXPAND
8464 /* Right after CTRL-X the cursor will be after the ruler. */
8465 setcursor();
8466#endif
8467
8468 /*
8469 * Don't map the second key. This also prevents the mode message to be
8470 * deleted when ESC is hit.
8471 */
8472 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008473 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 --no_mapping;
8475 switch (c)
8476 {
8477 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8478 case K_UP:
8479 case Ctrl_K:
8480 case 'k': ins_up(TRUE);
8481 break;
8482
8483 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8484 case K_DOWN:
8485 case Ctrl_J:
8486 case 'j': ins_down(TRUE);
8487 break;
8488
8489 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008490 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008492
8493 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008494 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008495 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008496 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 break;
8498
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008499 /* CTRL-G U: do not break undo with the next char */
8500 case 'U':
8501 /* Allow one left/right cursor movement with the next char,
8502 * without breaking undo. */
8503 dont_sync_undo = MAYBE;
8504 break;
8505
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008507 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 }
8509}
8510
8511/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008512 * CTRL-^ in Insert mode.
8513 */
8514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008515ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008516{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008517 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008518 {
8519 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8520 if (State & LANGMAP)
8521 {
8522 curbuf->b_p_iminsert = B_IMODE_NONE;
8523 State &= ~LANGMAP;
8524 }
8525 else
8526 {
8527 curbuf->b_p_iminsert = B_IMODE_LMAP;
8528 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008529#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008530 im_set_active(FALSE);
8531#endif
8532 }
8533 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008534#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008535 else
8536 {
8537 /* There are no ":lmap" mappings, toggle IM */
8538 if (im_get_status())
8539 {
8540 curbuf->b_p_iminsert = B_IMODE_NONE;
8541 im_set_active(FALSE);
8542 }
8543 else
8544 {
8545 curbuf->b_p_iminsert = B_IMODE_IM;
8546 State &= ~LANGMAP;
8547 im_set_active(TRUE);
8548 }
8549 }
8550#endif
8551 set_iminsert_global();
8552 showmode();
8553#ifdef FEAT_GUI
8554 /* may show different cursor shape or color */
8555 if (gui.in_use)
8556 gui_update_cursor(TRUE, FALSE);
8557#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008558#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008559 /* Show/unshow value of 'keymap' in status lines. */
8560 status_redraw_curbuf();
8561#endif
8562}
8563
8564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 * Handle ESC in insert mode.
8566 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8567 * insert.
8568 */
8569 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008570ins_esc(
8571 long *count,
8572 int cmdchar,
8573 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575 int temp;
8576 static int disabled_redraw = FALSE;
8577
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008578#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008579 check_spell_redraw();
8580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581#if defined(FEAT_HANGULIN)
8582# if defined(ESC_CHG_TO_ENG_MODE)
8583 hangul_input_state_set(0);
8584# endif
8585 if (composing_hangul)
8586 {
8587 push_raw_key(composing_hangul_buffer, 2);
8588 composing_hangul = 0;
8589 }
8590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591
8592 temp = curwin->w_cursor.col;
8593 if (disabled_redraw)
8594 {
8595 --RedrawingDisabled;
8596 disabled_redraw = FALSE;
8597 }
8598 if (!arrow_used)
8599 {
8600 /*
8601 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008602 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8603 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 */
8605 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008606 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
8608 /*
8609 * Repeating insert may take a long time. Check for
8610 * interrupt now and then.
8611 */
8612 if (*count > 0)
8613 {
8614 line_breakcheck();
8615 if (got_int)
8616 *count = 0;
8617 }
8618
8619 if (--*count > 0) /* repeat what was typed */
8620 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008621 /* Vi repeats the insert without replacing characters. */
8622 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8623 State &= ~REPLACE_FLAG;
8624
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 (void)start_redo_ins();
8626 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008627 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 ++RedrawingDisabled;
8629 disabled_redraw = TRUE;
8630 return FALSE; /* repeat the insert */
8631 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008632 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 undisplay_dollar();
8634 }
8635
8636 /* When an autoindent was removed, curswant stays after the
8637 * indent */
8638 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8639 curwin->w_set_curswant = TRUE;
8640
8641 /* Remember the last Insert position in the '^ mark. */
8642 if (!cmdmod.keepjumps)
8643 curbuf->b_last_insert = curwin->w_cursor;
8644
8645 /*
8646 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008647 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008649 if (!nomove
8650 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651#ifdef FEAT_VIRTUALEDIT
8652 || curwin->w_cursor.coladd > 0
8653#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008654 )
8655 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008656 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657#ifdef FEAT_RIGHTLEFT
8658 && !revins_on
8659#endif
8660 )
8661 {
8662#ifdef FEAT_VIRTUALEDIT
8663 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8664 {
8665 oneleft();
8666 if (restart_edit != NUL)
8667 ++curwin->w_cursor.coladd;
8668 }
8669 else
8670#endif
8671 {
8672 --curwin->w_cursor.col;
8673#ifdef FEAT_MBYTE
8674 /* Correct cursor for multi-byte character. */
8675 if (has_mbyte)
8676 mb_adjust_cursor();
8677#endif
8678 }
8679 }
8680
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008681#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 /* Disable IM to allow typing English directly for Normal mode commands.
8683 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8684 * well). */
8685 if (!(State & LANGMAP))
8686 im_save_status(&curbuf->b_p_iminsert);
8687 im_set_active(FALSE);
8688#endif
8689
8690 State = NORMAL;
8691 /* need to position cursor again (e.g. when on a TAB ) */
8692 changed_cline_bef_curs();
8693
8694#ifdef FEAT_MOUSE
8695 setmouse();
8696#endif
8697#ifdef CURSOR_SHAPE
8698 ui_cursor_shape(); /* may show different cursor shape */
8699#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008700 if (!p_ek)
8701 /* Re-enable bracketed paste mode. */
8702 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703
8704 /*
8705 * When recording or for CTRL-O, need to display the new mode.
8706 * Otherwise remove the mode message.
8707 */
8708 if (Recording || restart_edit != NUL)
8709 showmode();
8710 else if (p_smd)
8711 MSG("");
8712
8713 return TRUE; /* exit Insert mode */
8714}
8715
8716#ifdef FEAT_RIGHTLEFT
8717/*
8718 * Toggle language: hkmap and revins_on.
8719 * Move to end of reverse inserted text.
8720 */
8721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008722ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
8724 if (revins_on && revins_chars && revins_scol >= 0)
8725 {
8726 while (gchar_cursor() != NUL && revins_chars--)
8727 ++curwin->w_cursor.col;
8728 }
8729 p_ri = !p_ri;
8730 revins_on = (State == INSERT && p_ri);
8731 if (revins_on)
8732 {
8733 revins_scol = curwin->w_cursor.col;
8734 revins_legal++;
8735 revins_chars = 0;
8736 undisplay_dollar();
8737 }
8738 else
8739 revins_scol = -1;
8740#ifdef FEAT_FKMAP
8741 if (p_altkeymap)
8742 {
8743 /*
8744 * to be consistent also for redo command, using '.'
8745 * set arrow_used to true and stop it - causing to redo
8746 * characters entered in one mode (normal/reverse insert).
8747 */
8748 arrow_used = TRUE;
8749 (void)stop_arrow();
8750 p_fkmap = curwin->w_p_rl ^ p_ri;
8751 if (p_fkmap && p_ri)
8752 State = INSERT;
8753 }
8754 else
8755#endif
8756 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8757 showmode();
8758}
8759#endif
8760
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761/*
8762 * If 'keymodel' contains "startsel", may start selection.
8763 * Returns TRUE when a CTRL-O and other keys stuffed.
8764 */
8765 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008766ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
8768 if (km_startsel)
8769 switch (c)
8770 {
8771 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 case K_PAGEUP:
8774 case K_KPAGEUP:
8775 case K_PAGEDOWN:
8776 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008777# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 case K_LEFT:
8779 case K_RIGHT:
8780 case K_UP:
8781 case K_DOWN:
8782 case K_END:
8783 case K_HOME:
8784# endif
8785 if (!(mod_mask & MOD_MASK_SHIFT))
8786 break;
8787 /* FALLTHROUGH */
8788 case K_S_LEFT:
8789 case K_S_RIGHT:
8790 case K_S_UP:
8791 case K_S_DOWN:
8792 case K_S_END:
8793 case K_S_HOME:
8794 /* Start selection right away, the cursor can move with
8795 * CTRL-O when beyond the end of the line. */
8796 start_selection();
8797
8798 /* Execute the key in (insert) Select mode. */
8799 stuffcharReadbuff(Ctrl_O);
8800 if (mod_mask)
8801 {
8802 char_u buf[4];
8803
8804 buf[0] = K_SPECIAL;
8805 buf[1] = KS_MODIFIER;
8806 buf[2] = mod_mask;
8807 buf[3] = NUL;
8808 stuffReadbuff(buf);
8809 }
8810 stuffcharReadbuff(c);
8811 return TRUE;
8812 }
8813 return FALSE;
8814}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815
8816/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008817 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008818 */
8819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008820ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008821{
8822#ifdef FEAT_FKMAP
8823 if (p_fkmap && p_ri)
8824 {
8825 beep_flush();
8826 EMSG(farsi_text_3); /* encoded in Farsi */
8827 return;
8828 }
8829#endif
8830
8831#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008832# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008833 set_vim_var_string(VV_INSERTMODE,
8834 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008835# ifdef FEAT_VREPLACE
8836 replaceState == VREPLACE ? "v" :
8837# endif
8838 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008839# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008840 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8841#endif
8842 if (State & REPLACE_FLAG)
8843 State = INSERT | (State & LANGMAP);
8844 else
8845 State = replaceState | (State & LANGMAP);
8846 AppendCharToRedobuff(K_INS);
8847 showmode();
8848#ifdef CURSOR_SHAPE
8849 ui_cursor_shape(); /* may show different cursor shape */
8850#endif
8851}
8852
8853/*
8854 * Pressed CTRL-O in Insert mode.
8855 */
8856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008857ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008858{
8859#ifdef FEAT_VREPLACE
8860 if (State & VREPLACE_FLAG)
8861 restart_edit = 'V';
8862 else
8863#endif
8864 if (State & REPLACE_FLAG)
8865 restart_edit = 'R';
8866 else
8867 restart_edit = 'I';
8868#ifdef FEAT_VIRTUALEDIT
8869 if (virtual_active())
8870 ins_at_eol = FALSE; /* cursor always keeps its column */
8871 else
8872#endif
8873 ins_at_eol = (gchar_cursor() == NUL);
8874}
8875
8876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 * If the cursor is on an indent, ^T/^D insert/delete one
8878 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008879 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 * with vi. But vi only supports ^T and ^D after an
8881 * autoindent, we support it everywhere.
8882 */
8883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008884ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885{
8886 if (stop_arrow() == FAIL)
8887 return;
8888 AppendCharToRedobuff(c);
8889
8890 /*
8891 * 0^D and ^^D: remove all indent.
8892 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008893 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8894 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 {
8896 --curwin->w_cursor.col;
8897 (void)del_char(FALSE); /* delete the '^' or '0' */
8898 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8899 if (State & REPLACE_FLAG)
8900 replace_pop_ins();
8901 if (lastc == '^')
8902 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008903 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 }
8905 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008906 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907
8908 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8909 did_ai = FALSE;
8910#ifdef FEAT_SMARTINDENT
8911 did_si = FALSE;
8912 can_si = FALSE;
8913 can_si_back = FALSE;
8914#endif
8915#ifdef FEAT_CINDENT
8916 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8917#endif
8918}
8919
8920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008921ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
8923 int temp;
8924
8925 if (stop_arrow() == FAIL)
8926 return;
8927 if (gchar_cursor() == NUL) /* delete newline */
8928 {
8929 temp = curwin->w_cursor.col;
8930 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008931 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008932 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 else
8934 curwin->w_cursor.col = temp;
8935 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008936 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8937 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 did_ai = FALSE;
8939#ifdef FEAT_SMARTINDENT
8940 did_si = FALSE;
8941 can_si = FALSE;
8942 can_si_back = FALSE;
8943#endif
8944 AppendCharToRedobuff(K_DEL);
8945}
8946
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008947static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008948
8949/*
8950 * Delete one character for ins_bs().
8951 */
8952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008953ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008954{
8955 dec_cursor();
8956 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8957 if (State & REPLACE_FLAG)
8958 {
8959 /* Don't delete characters before the insert point when in
8960 * Replace mode */
8961 if (curwin->w_cursor.lnum != Insstart.lnum
8962 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008963 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008964 }
8965 else
8966 (void)del_char(FALSE);
8967}
8968
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969/*
8970 * Handle Backspace, delete-word and delete-line in Insert mode.
8971 * Return TRUE when backspace was actually used.
8972 */
8973 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008974ins_bs(
8975 int c,
8976 int mode,
8977 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978{
8979 linenr_T lnum;
8980 int cc;
8981 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008982 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 colnr_T mincol;
8984 int did_backspace = FALSE;
8985 int in_indent;
8986 int oldState;
8987#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008988 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989#endif
8990
8991 /*
8992 * can't delete anything in an empty file
8993 * can't backup past first character in buffer
8994 * can't backup past starting point unless 'backspace' > 1
8995 * can backup to a previous line if 'backspace' == 0
8996 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008997 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 || (
8999#ifdef FEAT_RIGHTLEFT
9000 !revins_on &&
9001#endif
9002 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9003 || (!can_bs(BS_START)
9004 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009005 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9006 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9008 && curwin->w_cursor.col <= ai_col)
9009 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9010 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009011 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 return FALSE;
9013 }
9014
9015 if (stop_arrow() == FAIL)
9016 return FALSE;
9017 in_indent = inindent(0);
9018#ifdef FEAT_CINDENT
9019 if (in_indent)
9020 can_cindent = FALSE;
9021#endif
9022#ifdef FEAT_COMMENTS
9023 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9024#endif
9025#ifdef FEAT_RIGHTLEFT
9026 if (revins_on) /* put cursor after last inserted char */
9027 inc_cursor();
9028#endif
9029
9030#ifdef FEAT_VIRTUALEDIT
9031 /* Virtualedit:
9032 * BACKSPACE_CHAR eats a virtual space
9033 * BACKSPACE_WORD eats all coladd
9034 * BACKSPACE_LINE eats all coladd and keeps going
9035 */
9036 if (curwin->w_cursor.coladd > 0)
9037 {
9038 if (mode == BACKSPACE_CHAR)
9039 {
9040 --curwin->w_cursor.coladd;
9041 return TRUE;
9042 }
9043 if (mode == BACKSPACE_WORD)
9044 {
9045 curwin->w_cursor.coladd = 0;
9046 return TRUE;
9047 }
9048 curwin->w_cursor.coladd = 0;
9049 }
9050#endif
9051
9052 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009053 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 */
9055 if (curwin->w_cursor.col == 0)
9056 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009057 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009058 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059#ifdef FEAT_RIGHTLEFT
9060 || revins_on
9061#endif
9062 )
9063 {
9064 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9065 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9066 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009067 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009068 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 }
9070 /*
9071 * In replace mode:
9072 * cc < 0: NL was inserted, delete it
9073 * cc >= 0: NL was replaced, put original characters back
9074 */
9075 cc = -1;
9076 if (State & REPLACE_FLAG)
9077 cc = replace_pop(); /* returns -1 if NL was inserted */
9078 /*
9079 * In replace mode, in the line we started replacing, we only move the
9080 * cursor.
9081 */
9082 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9083 {
9084 dec_cursor();
9085 }
9086 else
9087 {
9088#ifdef FEAT_VREPLACE
9089 if (!(State & VREPLACE_FLAG)
9090 || curwin->w_cursor.lnum > orig_line_count)
9091#endif
9092 {
9093 temp = gchar_cursor(); /* remember current char */
9094 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009095
9096 /* When "aw" is in 'formatoptions' we must delete the space at
9097 * the end of the line, otherwise the line will be broken
9098 * again when auto-formatting. */
9099 if (has_format_option(FO_AUTO)
9100 && has_format_option(FO_WHITE_PAR))
9101 {
9102 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9103 TRUE);
9104 int len;
9105
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009106 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009107 if (len > 0 && ptr[len - 1] == ' ')
9108 ptr[len - 1] = NUL;
9109 }
9110
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009111 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 if (temp == NUL && gchar_cursor() != NUL)
9113 inc_cursor();
9114 }
9115#ifdef FEAT_VREPLACE
9116 else
9117 dec_cursor();
9118#endif
9119
9120 /*
9121 * In REPLACE mode we have to put back the text that was replaced
9122 * by the NL. On the replace stack is first a NUL-terminated
9123 * sequence of characters that were deleted and then the
9124 * characters that NL replaced.
9125 */
9126 if (State & REPLACE_FLAG)
9127 {
9128 /*
9129 * Do the next ins_char() in NORMAL state, to
9130 * prevent ins_char() from replacing characters and
9131 * avoiding showmatch().
9132 */
9133 oldState = State;
9134 State = NORMAL;
9135 /*
9136 * restore characters (blanks) deleted after cursor
9137 */
9138 while (cc > 0)
9139 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009140 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141#ifdef FEAT_MBYTE
9142 mb_replace_pop_ins(cc);
9143#else
9144 ins_char(cc);
9145#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009146 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 cc = replace_pop();
9148 }
9149 /* restore the characters that NL replaced */
9150 replace_pop_ins();
9151 State = oldState;
9152 }
9153 }
9154 did_ai = FALSE;
9155 }
9156 else
9157 {
9158 /*
9159 * Delete character(s) before the cursor.
9160 */
9161#ifdef FEAT_RIGHTLEFT
9162 if (revins_on) /* put cursor on last inserted char */
9163 dec_cursor();
9164#endif
9165 mincol = 0;
9166 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009167 if (mode == BACKSPACE_LINE
9168 && (curbuf->b_p_ai
9169#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009170 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009171#endif
9172 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173#ifdef FEAT_RIGHTLEFT
9174 && !revins_on
9175#endif
9176 )
9177 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009178 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009180 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009182 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 }
9184
9185 /*
9186 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9187 */
9188 if ( mode == BACKSPACE_CHAR
9189 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009190 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009191 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 && (*(ml_get_cursor() - 1) == TAB
9193 || (*(ml_get_cursor() - 1) == ' '
9194 && (!*inserted_space_p
9195 || arrow_used))))))
9196 {
9197 int ts;
9198 colnr_T vcol;
9199 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009200 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
9202 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009203 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009204 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009206 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 /* Compute the virtual column where we want to be. Since
9208 * 'showbreak' may get in the way, need to get the last column of
9209 * the previous character. */
9210 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009211 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 dec_cursor();
9213 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9214 inc_cursor();
9215 want_vcol = (want_vcol / ts) * ts;
9216
9217 /* delete characters until we are at or before want_vcol */
9218 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009219 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009220 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221
9222 /* insert extra spaces until we are at want_vcol */
9223 while (vcol < want_vcol)
9224 {
9225 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009226 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9227 && curwin->w_cursor.col < Insstart_orig.col)
9228 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229
9230#ifdef FEAT_VREPLACE
9231 if (State & VREPLACE_FLAG)
9232 ins_char(' ');
9233 else
9234#endif
9235 {
9236 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009237 if ((State & REPLACE_FLAG))
9238 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 }
9240 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9241 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009242
9243 /* If we are now back where we started delete one character. Can
9244 * happen when using 'sts' and 'linebreak'. */
9245 if (vcol >= start_vcol)
9246 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 }
9248
9249 /*
9250 * Delete upto starting point, start of line or previous word.
9251 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009252 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009254#ifdef FEAT_MBYTE
9255 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009257 if (has_mbyte)
9258 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009260 do
9261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009263 if (!revins_on) /* put cursor on char to be deleted */
9264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009266
9267 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009269 /* look multi-byte character class */
9270 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009272 prev_cclass = cclass;
9273 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009276
9277 /* start of word? */
9278 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9279 {
9280 mode = BACKSPACE_WORD_NOT_SPACE;
9281 temp = vim_iswordc(cc);
9282 }
9283 /* end of word? */
9284 else if (mode == BACKSPACE_WORD_NOT_SPACE
9285 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9286#ifdef FEAT_MBYTE
9287 || prev_cclass != cclass
9288#endif
9289 ))
9290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009292 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009294 inc_cursor();
9295#ifdef FEAT_RIGHTLEFT
9296 else if (State & REPLACE_FLAG)
9297 dec_cursor();
9298#endif
9299 break;
9300 }
9301 if (State & REPLACE_FLAG)
9302 replace_do_bs(-1);
9303 else
9304 {
9305#ifdef FEAT_MBYTE
9306 if (enc_utf8 && p_deco)
9307 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9308#endif
9309 (void)del_char(FALSE);
9310#ifdef FEAT_MBYTE
9311 /*
9312 * If there are combining characters and 'delcombine' is set
9313 * move the cursor back. Don't back up before the base
9314 * character.
9315 */
9316 if (enc_utf8 && p_deco && cpc[0] != NUL)
9317 inc_cursor();
9318#endif
9319#ifdef FEAT_RIGHTLEFT
9320 if (revins_chars)
9321 {
9322 revins_chars--;
9323 revins_legal++;
9324 }
9325 if (revins_on && gchar_cursor() == NUL)
9326 break;
9327#endif
9328 }
9329 /* Just a single backspace?: */
9330 if (mode == BACKSPACE_CHAR)
9331 break;
9332 } while (
9333#ifdef FEAT_RIGHTLEFT
9334 revins_on ||
9335#endif
9336 (curwin->w_cursor.col > mincol
9337 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9338 || curwin->w_cursor.col != Insstart_orig.col)));
9339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 did_backspace = TRUE;
9341 }
9342#ifdef FEAT_SMARTINDENT
9343 did_si = FALSE;
9344 can_si = FALSE;
9345 can_si_back = FALSE;
9346#endif
9347 if (curwin->w_cursor.col <= 1)
9348 did_ai = FALSE;
9349 /*
9350 * It's a little strange to put backspaces into the redo
9351 * buffer, but it makes auto-indent a lot easier to deal
9352 * with.
9353 */
9354 AppendCharToRedobuff(c);
9355
9356 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009357 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9358 && curwin->w_cursor.col < Insstart_orig.col)
9359 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360
9361 /* vi behaviour: the cursor moves backward but the character that
9362 * was there remains visible
9363 * Vim behaviour: the cursor moves backward and the character that
9364 * was there is erased from the screen.
9365 * We can emulate the vi behaviour by pretending there is a dollar
9366 * displayed even when there isn't.
9367 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009368 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 dollar_vcol = curwin->w_virtcol;
9370
Bram Moolenaarce3be472008-01-14 19:12:28 +00009371#ifdef FEAT_FOLDING
9372 /* When deleting a char the cursor line must never be in a closed fold.
9373 * E.g., when 'foldmethod' is indent and deleting the first non-white
9374 * char before a Tab. */
9375 if (did_backspace)
9376 foldOpenCursor();
9377#endif
9378
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 return did_backspace;
9380}
9381
9382#ifdef FEAT_MOUSE
9383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009384ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009387 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388
9389# ifdef FEAT_GUI
9390 /* When GUI is active, also move/paste when 'mouse' is empty */
9391 if (!gui.in_use)
9392# endif
9393 if (!mouse_has(MOUSE_INSERT))
9394 return;
9395
9396 undisplay_dollar();
9397 tpos = curwin->w_cursor;
9398 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9399 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009400 win_T *new_curwin = curwin;
9401
9402 if (curwin != old_curwin && win_valid(old_curwin))
9403 {
9404 /* Mouse took us to another window. We need to go back to the
9405 * previous one to stop insert there properly. */
9406 curwin = old_curwin;
9407 curbuf = curwin->w_buffer;
9408 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009409 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009410 if (curwin != new_curwin && win_valid(new_curwin))
9411 {
9412 curwin = new_curwin;
9413 curbuf = curwin->w_buffer;
9414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415# ifdef FEAT_CINDENT
9416 can_cindent = TRUE;
9417# endif
9418 }
9419
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 /* redraw status lines (in case another window became active) */
9421 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422}
9423
9424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009425ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426{
9427 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009428 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009429# ifdef FEAT_INS_EXPAND
9430 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431# endif
9432
9433 tpos = curwin->w_cursor;
9434
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009435 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 {
9437 int row, col;
9438
9439 row = mouse_row;
9440 col = mouse_col;
9441
9442 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009443 wp = mouse_find_win(&row, &col);
9444 if (wp == NULL)
9445 return;
9446 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 curbuf = curwin->w_buffer;
9448 }
9449 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 undisplay_dollar();
9451
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009452# ifdef FEAT_INS_EXPAND
9453 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009454 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009455# endif
9456 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009457 if (dir == MSCR_DOWN || dir == MSCR_UP)
9458 {
9459 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9460 scroll_redraw(dir,
9461 (long)(curwin->w_botline - curwin->w_topline));
9462 else
9463 scroll_redraw(dir, 3L);
9464 }
9465#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009466 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009467 {
9468 int val, step = 6;
9469
9470 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009471 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009472 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9473 if (val < 0)
9474 val = 0;
9475 gui_do_horiz_scroll(val, TRUE);
9476 }
9477#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009478# ifdef FEAT_INS_EXPAND
9479 did_scroll = TRUE;
9480# endif
9481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 curwin->w_redr_status = TRUE;
9484
9485 curwin = old_curwin;
9486 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009488# ifdef FEAT_INS_EXPAND
9489 /* The popup menu may overlay the window, need to redraw it.
9490 * TODO: Would be more efficient to only redraw the windows that are
9491 * overlapped by the popup menu. */
9492 if (pum_visible() && did_scroll)
9493 {
9494 redraw_all_later(NOT_VALID);
9495 ins_compl_show_pum();
9496 }
9497# endif
9498
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009499 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 {
9501 start_arrow(&tpos);
9502# ifdef FEAT_CINDENT
9503 can_cindent = TRUE;
9504# endif
9505 }
9506}
9507#endif
9508
Bram Moolenaarec2da362017-01-21 20:04:22 +01009509/*
9510 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9511 * P_PE literally.
9512 * When "drop" is TRUE then consume the text and drop it.
9513 */
9514 int
9515bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9516{
9517 int c;
9518 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9519 int idx = 0;
9520 char_u *end = find_termcode((char_u *)"PE");
9521 int ret_char = -1;
9522 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009523 int save_paste = p_paste;
9524 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009525
9526 /* If the end code is too long we can't detect it, read everything. */
9527 if (STRLEN(end) >= NUMBUFLEN)
9528 end = NULL;
9529 ++no_mapping;
9530 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009531 p_paste = TRUE;
9532 curbuf->b_p_ai = FALSE;
9533
Bram Moolenaarec2da362017-01-21 20:04:22 +01009534 for (;;)
9535 {
9536 /* When the end is not defined read everything. */
9537 if (end == NULL && vpeekc() == NUL)
9538 break;
9539 c = plain_vgetc();
9540#ifdef FEAT_MBYTE
9541 if (has_mbyte)
9542 idx += (*mb_char2bytes)(c, buf + idx);
9543 else
9544#endif
9545 buf[idx++] = c;
9546 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009547 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009548 {
9549 if (end[idx] == NUL)
9550 break; /* Found the end of paste code. */
9551 continue;
9552 }
9553 if (!drop)
9554 {
9555 switch (mode)
9556 {
9557 case PASTE_CMDLINE:
9558 put_on_cmdline(buf, idx, TRUE);
9559 break;
9560
9561 case PASTE_EX:
9562 if (gap != NULL && ga_grow(gap, idx) == OK)
9563 {
9564 mch_memmove((char *)gap->ga_data + gap->ga_len,
9565 buf, (size_t)idx);
9566 gap->ga_len += idx;
9567 }
9568 break;
9569
9570 case PASTE_INSERT:
9571 if (stop_arrow() == OK)
9572 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009573 c = buf[0];
9574 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9575 ins_eol(c);
9576 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009577 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009578 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009579 AppendToRedobuffLit(buf, idx);
9580 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009581 }
9582 break;
9583
9584 case PASTE_ONE_CHAR:
9585 if (ret_char == -1)
9586 {
9587#ifdef FEAT_MBYTE
9588 if (has_mbyte)
9589 ret_char = (*mb_ptr2char)(buf);
9590 else
9591#endif
9592 ret_char = buf[0];
9593 }
9594 break;
9595 }
9596 }
9597 idx = 0;
9598 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009599
Bram Moolenaarec2da362017-01-21 20:04:22 +01009600 --no_mapping;
9601 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009602 p_paste = save_paste;
9603 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009604
9605 return ret_char;
9606}
9607
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009608#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009610ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009611{
9612 /* We will be leaving the current window, unless closing another tab. */
9613 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9614 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9615 {
9616 undisplay_dollar();
9617 start_arrow(&curwin->w_cursor);
9618# ifdef FEAT_CINDENT
9619 can_cindent = TRUE;
9620# endif
9621 }
9622
9623 if (c == K_TABLINE)
9624 goto_tabpage(current_tab);
9625 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009626 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009627 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009628 redraw_statuslines(); /* will redraw the tabline when needed */
9629 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009630}
9631#endif
9632
9633#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009635ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636{
9637 pos_T tpos;
9638
9639 undisplay_dollar();
9640 tpos = curwin->w_cursor;
9641 if (gui_do_scroll())
9642 {
9643 start_arrow(&tpos);
9644# ifdef FEAT_CINDENT
9645 can_cindent = TRUE;
9646# endif
9647 }
9648}
9649
9650 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009651ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
9653 pos_T tpos;
9654
9655 undisplay_dollar();
9656 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009657 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 {
9659 start_arrow(&tpos);
9660# ifdef FEAT_CINDENT
9661 can_cindent = TRUE;
9662# endif
9663 }
9664}
9665#endif
9666
9667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009668ins_left(
9669 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670{
9671 pos_T tpos;
9672
9673#ifdef FEAT_FOLDING
9674 if ((fdo_flags & FDO_HOR) && KeyTyped)
9675 foldOpenCursor();
9676#endif
9677 undisplay_dollar();
9678 tpos = curwin->w_cursor;
9679 if (oneleft() == OK)
9680 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009681#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9682 /* Only call start_arrow() when not busy with preediting, it will
9683 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009684 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009685#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009686 {
9687 start_arrow_with_change(&tpos, end_change);
9688 if (!end_change)
9689 AppendCharToRedobuff(K_LEFT);
9690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691#ifdef FEAT_RIGHTLEFT
9692 /* If exit reversed string, position is fixed */
9693 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9694 revins_legal++;
9695 revins_chars++;
9696#endif
9697 }
9698
9699 /*
9700 * if 'whichwrap' set for cursor in insert mode may go to
9701 * previous line
9702 */
9703 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9704 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009705 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 start_arrow(&tpos);
9707 --(curwin->w_cursor.lnum);
9708 coladvance((colnr_T)MAXCOL);
9709 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9710 }
9711 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009712 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009713 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714}
9715
9716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009717ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
9719 pos_T tpos;
9720
9721#ifdef FEAT_FOLDING
9722 if ((fdo_flags & FDO_HOR) && KeyTyped)
9723 foldOpenCursor();
9724#endif
9725 undisplay_dollar();
9726 tpos = curwin->w_cursor;
9727 if (c == K_C_HOME)
9728 curwin->w_cursor.lnum = 1;
9729 curwin->w_cursor.col = 0;
9730#ifdef FEAT_VIRTUALEDIT
9731 curwin->w_cursor.coladd = 0;
9732#endif
9733 curwin->w_curswant = 0;
9734 start_arrow(&tpos);
9735}
9736
9737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009738ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739{
9740 pos_T tpos;
9741
9742#ifdef FEAT_FOLDING
9743 if ((fdo_flags & FDO_HOR) && KeyTyped)
9744 foldOpenCursor();
9745#endif
9746 undisplay_dollar();
9747 tpos = curwin->w_cursor;
9748 if (c == K_C_END)
9749 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9750 coladvance((colnr_T)MAXCOL);
9751 curwin->w_curswant = MAXCOL;
9752
9753 start_arrow(&tpos);
9754}
9755
9756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009757ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758{
9759#ifdef FEAT_FOLDING
9760 if ((fdo_flags & FDO_HOR) && KeyTyped)
9761 foldOpenCursor();
9762#endif
9763 undisplay_dollar();
9764 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9765 {
9766 start_arrow(&curwin->w_cursor);
9767 (void)bck_word(1L, FALSE, FALSE);
9768 curwin->w_set_curswant = TRUE;
9769 }
9770 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009771 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772}
9773
9774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009775ins_right(
9776 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
9778#ifdef FEAT_FOLDING
9779 if ((fdo_flags & FDO_HOR) && KeyTyped)
9780 foldOpenCursor();
9781#endif
9782 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009783 if (gchar_cursor() != NUL
9784#ifdef FEAT_VIRTUALEDIT
9785 || virtual_active()
9786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 )
9788 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009789 start_arrow_with_change(&curwin->w_cursor, end_change);
9790 if (!end_change)
9791 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 curwin->w_set_curswant = TRUE;
9793#ifdef FEAT_VIRTUALEDIT
9794 if (virtual_active())
9795 oneright();
9796 else
9797#endif
9798 {
9799#ifdef FEAT_MBYTE
9800 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009801 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 else
9803#endif
9804 ++curwin->w_cursor.col;
9805 }
9806
9807#ifdef FEAT_RIGHTLEFT
9808 revins_legal++;
9809 if (revins_chars)
9810 revins_chars--;
9811#endif
9812 }
9813 /* if 'whichwrap' set for cursor in insert mode, may move the
9814 * cursor to the next line */
9815 else if (vim_strchr(p_ww, ']') != NULL
9816 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9817 {
9818 start_arrow(&curwin->w_cursor);
9819 curwin->w_set_curswant = TRUE;
9820 ++curwin->w_cursor.lnum;
9821 curwin->w_cursor.col = 0;
9822 }
9823 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009824 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009825 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826}
9827
9828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009829ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830{
9831#ifdef FEAT_FOLDING
9832 if ((fdo_flags & FDO_HOR) && KeyTyped)
9833 foldOpenCursor();
9834#endif
9835 undisplay_dollar();
9836 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9837 || gchar_cursor() != NUL)
9838 {
9839 start_arrow(&curwin->w_cursor);
9840 (void)fwd_word(1L, FALSE, 0);
9841 curwin->w_set_curswant = TRUE;
9842 }
9843 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009844 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845}
9846
9847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009848ins_up(
9849 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850{
9851 pos_T tpos;
9852 linenr_T old_topline = curwin->w_topline;
9853#ifdef FEAT_DIFF
9854 int old_topfill = curwin->w_topfill;
9855#endif
9856
9857 undisplay_dollar();
9858 tpos = curwin->w_cursor;
9859 if (cursor_up(1L, TRUE) == OK)
9860 {
9861 if (startcol)
9862 coladvance(getvcol_nolist(&Insstart));
9863 if (old_topline != curwin->w_topline
9864#ifdef FEAT_DIFF
9865 || old_topfill != curwin->w_topfill
9866#endif
9867 )
9868 redraw_later(VALID);
9869 start_arrow(&tpos);
9870#ifdef FEAT_CINDENT
9871 can_cindent = TRUE;
9872#endif
9873 }
9874 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009875 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876}
9877
9878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009879ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880{
9881 pos_T tpos;
9882
9883 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009884
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009885 if (mod_mask & MOD_MASK_CTRL)
9886 {
9887 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009888 if (first_tabpage->tp_next != NULL)
9889 {
9890 start_arrow(&curwin->w_cursor);
9891 goto_tabpage(-1);
9892 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009893 return;
9894 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009895
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 tpos = curwin->w_cursor;
9897 if (onepage(BACKWARD, 1L) == OK)
9898 {
9899 start_arrow(&tpos);
9900#ifdef FEAT_CINDENT
9901 can_cindent = TRUE;
9902#endif
9903 }
9904 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009905 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906}
9907
9908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009909ins_down(
9910 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911{
9912 pos_T tpos;
9913 linenr_T old_topline = curwin->w_topline;
9914#ifdef FEAT_DIFF
9915 int old_topfill = curwin->w_topfill;
9916#endif
9917
9918 undisplay_dollar();
9919 tpos = curwin->w_cursor;
9920 if (cursor_down(1L, TRUE) == OK)
9921 {
9922 if (startcol)
9923 coladvance(getvcol_nolist(&Insstart));
9924 if (old_topline != curwin->w_topline
9925#ifdef FEAT_DIFF
9926 || old_topfill != curwin->w_topfill
9927#endif
9928 )
9929 redraw_later(VALID);
9930 start_arrow(&tpos);
9931#ifdef FEAT_CINDENT
9932 can_cindent = TRUE;
9933#endif
9934 }
9935 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009936 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937}
9938
9939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009940ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941{
9942 pos_T tpos;
9943
9944 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009945
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009946 if (mod_mask & MOD_MASK_CTRL)
9947 {
9948 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009949 if (first_tabpage->tp_next != NULL)
9950 {
9951 start_arrow(&curwin->w_cursor);
9952 goto_tabpage(0);
9953 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009954 return;
9955 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009956
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 tpos = curwin->w_cursor;
9958 if (onepage(FORWARD, 1L) == OK)
9959 {
9960 start_arrow(&tpos);
9961#ifdef FEAT_CINDENT
9962 can_cindent = TRUE;
9963#endif
9964 }
9965 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009966 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967}
9968
9969#ifdef FEAT_DND
9970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009971ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972{
9973 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9974}
9975#endif
9976
9977/*
9978 * Handle TAB in Insert or Replace mode.
9979 * Return TRUE when the TAB needs to be inserted like a normal character.
9980 */
9981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009982ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983{
9984 int ind;
9985 int i;
9986 int temp;
9987
9988 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9989 Insstart_blank_vcol = get_nolist_virtcol();
9990 if (echeck_abbr(TAB + ABBR_OFF))
9991 return FALSE;
9992
9993 ind = inindent(0);
9994#ifdef FEAT_CINDENT
9995 if (ind)
9996 can_cindent = FALSE;
9997#endif
9998
9999 /*
10000 * When nothing special, insert TAB like a normal character
10001 */
10002 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010003 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010004 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 return TRUE;
10006
10007 if (stop_arrow() == FAIL)
10008 return TRUE;
10009
10010 did_ai = FALSE;
10011#ifdef FEAT_SMARTINDENT
10012 did_si = FALSE;
10013 can_si = FALSE;
10014 can_si_back = FALSE;
10015#endif
10016 AppendToRedobuff((char_u *)"\t");
10017
10018 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010019 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010020 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10021 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 else /* otherwise use 'tabstop' */
10023 temp = (int)curbuf->b_p_ts;
10024 temp -= get_nolist_virtcol() % temp;
10025
10026 /*
10027 * Insert the first space with ins_char(). It will delete one char in
10028 * replace mode. Insert the rest with ins_str(); it will not delete any
10029 * chars. For VREPLACE mode, we use ins_char() for all characters.
10030 */
10031 ins_char(' ');
10032 while (--temp > 0)
10033 {
10034#ifdef FEAT_VREPLACE
10035 if (State & VREPLACE_FLAG)
10036 ins_char(' ');
10037 else
10038#endif
10039 {
10040 ins_str((char_u *)" ");
10041 if (State & REPLACE_FLAG) /* no char replaced */
10042 replace_push(NUL);
10043 }
10044 }
10045
10046 /*
10047 * When 'expandtab' not set: Replace spaces by TABs where possible.
10048 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010049 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 {
10051 char_u *ptr;
10052#ifdef FEAT_VREPLACE
10053 char_u *saved_line = NULL; /* init for GCC */
10054 pos_T pos;
10055#endif
10056 pos_T fpos;
10057 pos_T *cursor;
10058 colnr_T want_vcol, vcol;
10059 int change_col = -1;
10060 int save_list = curwin->w_p_list;
10061
10062 /*
10063 * Get the current line. For VREPLACE mode, don't make real changes
10064 * yet, just work on a copy of the line.
10065 */
10066#ifdef FEAT_VREPLACE
10067 if (State & VREPLACE_FLAG)
10068 {
10069 pos = curwin->w_cursor;
10070 cursor = &pos;
10071 saved_line = vim_strsave(ml_get_curline());
10072 if (saved_line == NULL)
10073 return FALSE;
10074 ptr = saved_line + pos.col;
10075 }
10076 else
10077#endif
10078 {
10079 ptr = ml_get_cursor();
10080 cursor = &curwin->w_cursor;
10081 }
10082
10083 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10084 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10085 curwin->w_p_list = FALSE;
10086
10087 /* Find first white before the cursor */
10088 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010089 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 {
10091 --fpos.col;
10092 --ptr;
10093 }
10094
10095 /* In Replace mode, don't change characters before the insert point. */
10096 if ((State & REPLACE_FLAG)
10097 && fpos.lnum == Insstart.lnum
10098 && fpos.col < Insstart.col)
10099 {
10100 ptr += Insstart.col - fpos.col;
10101 fpos.col = Insstart.col;
10102 }
10103
10104 /* compute virtual column numbers of first white and cursor */
10105 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10106 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10107
Bram Moolenaar597a4222014-06-25 14:39:50 +020010108 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10109 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010110 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010112 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 if (vcol + i > want_vcol)
10114 break;
10115 if (*ptr != TAB)
10116 {
10117 *ptr = TAB;
10118 if (change_col < 0)
10119 {
10120 change_col = fpos.col; /* Column of first change */
10121 /* May have to adjust Insstart */
10122 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10123 Insstart.col = fpos.col;
10124 }
10125 }
10126 ++fpos.col;
10127 ++ptr;
10128 vcol += i;
10129 }
10130
10131 if (change_col >= 0)
10132 {
10133 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010134 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135
10136 /* Skip over the spaces we need. */
10137 while (vcol < want_vcol && *ptr == ' ')
10138 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010139 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 ++ptr;
10141 ++repl_off;
10142 }
10143 if (vcol > want_vcol)
10144 {
10145 /* Must have a char with 'showbreak' just before it. */
10146 --ptr;
10147 --repl_off;
10148 }
10149 fpos.col += repl_off;
10150
10151 /* Delete following spaces. */
10152 i = cursor->col - fpos.col;
10153 if (i > 0)
10154 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010155 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 /* correct replace stack. */
10157 if ((State & REPLACE_FLAG)
10158#ifdef FEAT_VREPLACE
10159 && !(State & VREPLACE_FLAG)
10160#endif
10161 )
10162 for (temp = i; --temp >= 0; )
10163 replace_join(repl_off);
10164 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010165#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010166 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010167 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010168 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010169 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10170 (char_u *)"\t", 1);
10171 }
10172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 cursor->col -= i;
10174
10175#ifdef FEAT_VREPLACE
10176 /*
10177 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10178 * backspacing over the changed spacing and then inserting the new
10179 * spacing.
10180 */
10181 if (State & VREPLACE_FLAG)
10182 {
10183 /* Backspace from real cursor to change_col */
10184 backspace_until_column(change_col);
10185
10186 /* Insert each char in saved_line from changed_col to
10187 * ptr-cursor */
10188 ins_bytes_len(saved_line + change_col,
10189 cursor->col - change_col);
10190 }
10191#endif
10192 }
10193
10194#ifdef FEAT_VREPLACE
10195 if (State & VREPLACE_FLAG)
10196 vim_free(saved_line);
10197#endif
10198 curwin->w_p_list = save_list;
10199 }
10200
10201 return FALSE;
10202}
10203
10204/*
10205 * Handle CR or NL in insert mode.
10206 * Return TRUE when out of memory or can't undo.
10207 */
10208 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010209ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210{
10211 int i;
10212
10213 if (echeck_abbr(c + ABBR_OFF))
10214 return FALSE;
10215 if (stop_arrow() == FAIL)
10216 return TRUE;
10217 undisplay_dollar();
10218
10219 /*
10220 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10221 * character under the cursor. Only push a NUL on the replace stack,
10222 * nothing to put back when the NL is deleted.
10223 */
10224 if ((State & REPLACE_FLAG)
10225#ifdef FEAT_VREPLACE
10226 && !(State & VREPLACE_FLAG)
10227#endif
10228 )
10229 replace_push(NUL);
10230
10231 /*
10232 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10233 * replacing the next line, so we push all of the characters left on the
10234 * line onto the replace stack. This is not done here though, it is done
10235 * in open_line().
10236 */
10237
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010238#ifdef FEAT_VIRTUALEDIT
10239 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10240 * CTRL-O). */
10241 if (virtual_active() && curwin->w_cursor.coladd > 0)
10242 coladvance(getviscol());
10243#endif
10244
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245#ifdef FEAT_RIGHTLEFT
10246# ifdef FEAT_FKMAP
10247 if (p_altkeymap && p_fkmap)
10248 fkmap(NL);
10249# endif
10250 /* NL in reverse insert will always start in the end of
10251 * current line. */
10252 if (revins_on)
10253 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10254#endif
10255
10256 AppendToRedobuff(NL_STR);
10257 i = open_line(FORWARD,
10258#ifdef FEAT_COMMENTS
10259 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10260#endif
10261 0, old_indent);
10262 old_indent = 0;
10263#ifdef FEAT_CINDENT
10264 can_cindent = TRUE;
10265#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010266#ifdef FEAT_FOLDING
10267 /* When inserting a line the cursor line must never be in a closed fold. */
10268 foldOpenCursor();
10269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270
10271 return (!i);
10272}
10273
10274#ifdef FEAT_DIGRAPHS
10275/*
10276 * Handle digraph in insert mode.
10277 * Returns character still to be inserted, or NUL when nothing remaining to be
10278 * done.
10279 */
10280 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010281ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282{
10283 int c;
10284 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010285 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286
10287 pc_status = PC_STATUS_UNSET;
10288 if (redrawing() && !char_avail())
10289 {
10290 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010291 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292
10293 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010294 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295#ifdef FEAT_CMDL_INFO
10296 add_to_showcmd_c(Ctrl_K);
10297#endif
10298 }
10299
10300#ifdef USE_ON_FLY_SCROLL
10301 dont_scroll = TRUE; /* disallow scrolling here */
10302#endif
10303
10304 /* don't map the digraph chars. This also prevents the
10305 * mode message to be deleted when ESC is hit */
10306 ++no_mapping;
10307 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010308 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 --no_mapping;
10310 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010311 if (did_putchar)
10312 /* when the line fits in 'columns' the '?' is at the start of the next
10313 * line and will not be removed by the redraw */
10314 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010315
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 if (IS_SPECIAL(c) || mod_mask) /* special key */
10317 {
10318#ifdef FEAT_CMDL_INFO
10319 clear_showcmd();
10320#endif
10321 insert_special(c, TRUE, FALSE);
10322 return NUL;
10323 }
10324 if (c != ESC)
10325 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010326 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 if (redrawing() && !char_avail())
10328 {
10329 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010330 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331
10332 if (char2cells(c) == 1)
10333 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010334 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010336 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 }
10338#ifdef FEAT_CMDL_INFO
10339 add_to_showcmd_c(c);
10340#endif
10341 }
10342 ++no_mapping;
10343 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010344 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 --no_mapping;
10346 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010347 if (did_putchar)
10348 /* when the line fits in 'columns' the '?' is at the start of the
10349 * next line and will not be removed by a redraw */
10350 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 if (cc != ESC)
10352 {
10353 AppendToRedobuff((char_u *)CTRL_V_STR);
10354 c = getdigraph(c, cc, TRUE);
10355#ifdef FEAT_CMDL_INFO
10356 clear_showcmd();
10357#endif
10358 return c;
10359 }
10360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361#ifdef FEAT_CMDL_INFO
10362 clear_showcmd();
10363#endif
10364 return NUL;
10365}
10366#endif
10367
10368/*
10369 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10370 * Returns the char to be inserted, or NUL if none found.
10371 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010372 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010373ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374{
10375 int c;
10376 int temp;
10377 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010378 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379
10380 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10381 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010382 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 return NUL;
10384 }
10385
10386 /* try to advance to the cursor column */
10387 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010388 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 prev_ptr = ptr;
10390 validate_virtcol();
10391 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10392 {
10393 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010394 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 }
10396 if ((colnr_T)temp > curwin->w_virtcol)
10397 ptr = prev_ptr;
10398
10399#ifdef FEAT_MBYTE
10400 c = (*mb_ptr2char)(ptr);
10401#else
10402 c = *ptr;
10403#endif
10404 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010405 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 return c;
10407}
10408
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010409/*
10410 * CTRL-Y or CTRL-E typed in Insert mode.
10411 */
10412 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010413ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010414{
10415 int c = tc;
10416
10417#ifdef FEAT_INS_EXPAND
10418 if (ctrl_x_mode == CTRL_X_SCROLL)
10419 {
10420 if (c == Ctrl_Y)
10421 scrolldown_clamp();
10422 else
10423 scrollup_clamp();
10424 redraw_later(VALID);
10425 }
10426 else
10427#endif
10428 {
10429 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10430 if (c != NUL)
10431 {
10432 long tw_save;
10433
10434 /* The character must be taken literally, insert like it
10435 * was typed after a CTRL-V, and pretend 'textwidth'
10436 * wasn't set. Digits, 'o' and 'x' are special after a
10437 * CTRL-V, don't use it for these. */
10438 if (c < 256 && !isalnum(c))
10439 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10440 tw_save = curbuf->b_p_tw;
10441 curbuf->b_p_tw = -1;
10442 insert_special(c, TRUE, FALSE);
10443 curbuf->b_p_tw = tw_save;
10444#ifdef FEAT_RIGHTLEFT
10445 revins_chars++;
10446 revins_legal++;
10447#endif
10448 c = Ctrl_V; /* pretend CTRL-V is last character */
10449 auto_format(FALSE, TRUE);
10450 }
10451 }
10452 return c;
10453}
10454
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455#ifdef FEAT_SMARTINDENT
10456/*
10457 * Try to do some very smart auto-indenting.
10458 * Used when inserting a "normal" character.
10459 */
10460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010461ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462{
10463 pos_T *pos, old_pos;
10464 char_u *ptr;
10465 int i;
10466 int temp;
10467
10468 /*
10469 * do some very smart indenting when entering '{' or '}'
10470 */
10471 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10472 {
10473 /*
10474 * for '}' set indent equal to indent of line containing matching '{'
10475 */
10476 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10477 {
10478 old_pos = curwin->w_cursor;
10479 /*
10480 * If the matching '{' has a ')' immediately before it (ignoring
10481 * white-space), then line up with the start of the line
10482 * containing the matching '(' if there is one. This handles the
10483 * case where an "if (..\n..) {" statement continues over multiple
10484 * lines -- webb
10485 */
10486 ptr = ml_get(pos->lnum);
10487 i = pos->col;
10488 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010489 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 ;
10491 curwin->w_cursor.lnum = pos->lnum;
10492 curwin->w_cursor.col = i;
10493 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10494 curwin->w_cursor = *pos;
10495 i = get_indent();
10496 curwin->w_cursor = old_pos;
10497#ifdef FEAT_VREPLACE
10498 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010499 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 else
10501#endif
10502 (void)set_indent(i, SIN_CHANGED);
10503 }
10504 else if (curwin->w_cursor.col > 0)
10505 {
10506 /*
10507 * when inserting '{' after "O" reduce indent, but not
10508 * more than indent of previous line
10509 */
10510 temp = TRUE;
10511 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10512 {
10513 old_pos = curwin->w_cursor;
10514 i = get_indent();
10515 while (curwin->w_cursor.lnum > 1)
10516 {
10517 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10518
10519 /* ignore empty lines and lines starting with '#'. */
10520 if (*ptr != '#' && *ptr != NUL)
10521 break;
10522 }
10523 if (get_indent() >= i)
10524 temp = FALSE;
10525 curwin->w_cursor = old_pos;
10526 }
10527 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010528 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 }
10530 }
10531
10532 /*
10533 * set indent of '#' always to 0
10534 */
10535 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10536 {
10537 /* remember current indent for next line */
10538 old_indent = get_indent();
10539 (void)set_indent(0, SIN_CHANGED);
10540 }
10541
10542 /* Adjust ai_col, the char at this position can be deleted. */
10543 if (ai_col > curwin->w_cursor.col)
10544 ai_col = curwin->w_cursor.col;
10545}
10546#endif
10547
10548/*
10549 * Get the value that w_virtcol would have when 'list' is off.
10550 * Unless 'cpo' contains the 'L' flag.
10551 */
10552 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010553get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554{
10555 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10556 return getvcol_nolist(&curwin->w_cursor);
10557 validate_virtcol();
10558 return curwin->w_virtcol;
10559}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010560
10561#ifdef FEAT_AUTOCMD
10562/*
10563 * Handle the InsertCharPre autocommand.
10564 * "c" is the character that was typed.
10565 * Return a pointer to allocated memory with the replacement string.
10566 * Return NULL to continue inserting "c".
10567 */
10568 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010569do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010570{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010571 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010572 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010573
10574 /* Return quickly when there is nothing to do. */
10575 if (!has_insertcharpre())
10576 return NULL;
10577
Bram Moolenaar704984a2012-06-01 14:57:51 +020010578#ifdef FEAT_MBYTE
10579 if (has_mbyte)
10580 buf[(*mb_char2bytes)(c, buf)] = NUL;
10581 else
10582#endif
10583 {
10584 buf[0] = c;
10585 buf[1] = NUL;
10586 }
10587
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010588 /* Lock the text to avoid weird things from happening. */
10589 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010590 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010591
Bram Moolenaar704984a2012-06-01 14:57:51 +020010592 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010593 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010594 {
10595 /* Get the value of v:char. It may be empty or more than one
10596 * character. Only use it when changed, otherwise continue with the
10597 * original character to avoid breaking autoindent. */
10598 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10599 res = vim_strsave(get_vim_var_str(VV_CHAR));
10600 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010601
10602 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10603 --textlock;
10604
10605 return res;
10606}
10607#endif