blob: 9330a401da8464b319bf858a12f3094aeb886563 [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 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaare4214502015-03-05 18:08:43 +010037#define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
Bram Moolenaare4214502015-03-05 18:08:43 +010040#define CTRL_X_MODE_LINE_OR_EVAL(m) (m == CTRL_X_WHOLE_LINE || m == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42static char *ctrl_x_msgs[] =
43{
44 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000046 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 N_(" Whole line completion (^L^N^P)"),
48 N_(" File name completion (^F^N^P)"),
49 N_(" Tag completion (^]^N^P)"),
50 N_(" Path pattern completion (^N^P)"),
51 N_(" Definition completion (^D^N^P)"),
52 NULL,
53 N_(" Dictionary completion (^K^N^P)"),
54 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000055 N_(" Command-line completion (^V^N^P)"),
56 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000057 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000058 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000059 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010060 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061};
62
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000063static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010064#ifdef FEAT_COMPL_FUNC
65static char e_complwin[] = N_("E839: Completion function changed window");
66static char e_compldel[] = N_("E840: Completion function deleted text");
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
69/*
70 * Structure used to store one match for insert completion.
71 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000072typedef struct compl_S compl_T;
73struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000074{
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 compl_T *cp_next;
76 compl_T *cp_prev;
77 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000078 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000079 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000080 char_u *cp_fname; /* file containing the match, allocated when
81 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000082 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
83 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084};
85
Bram Moolenaar572cb562005-08-05 21:35:02 +000086#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#define FREE_FNAME (2)
88
89/*
90 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000091 * "compl_first_match" points to the start of the list.
92 * "compl_curr_match" points to the currently selected entry.
93 * "compl_shown_match" is different from compl_curr_match during
94 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000096static compl_T *compl_first_match = NULL;
97static compl_T *compl_curr_match = NULL;
98static compl_T *compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +020099static compl_T *compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000101/* After using a cursor key <Enter> selects a match in the popup menu,
102 * otherwise it inserts a line break. */
103static int compl_enter_selects = FALSE;
104
Bram Moolenaara6557602006-02-04 22:43:20 +0000105/* When "compl_leader" is not NULL only matches that start with this string
106 * are used. */
107static char_u *compl_leader = NULL;
108
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000109static int compl_get_longest = FALSE; /* put longest common string
110 in compl_leader */
111
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200112static int compl_no_insert = FALSE; /* FALSE: select & insert
113 TRUE: noinsert */
114static int compl_no_select = FALSE; /* FALSE: select & insert
115 TRUE: noselect */
116
Bram Moolenaara6557602006-02-04 22:43:20 +0000117static int compl_used_match; /* Selected one of the matches. When
118 FALSE the match was edited or using
119 the longest common string. */
120
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000121static int compl_was_interrupted = FALSE; /* didn't finish finding
122 completions. */
123
124static int compl_restarting = FALSE; /* don't insert match */
125
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000126/* When the first completion is done "compl_started" is set. When it's
127 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000128static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000129
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000130/* Set when doing something for completion that may call edit() recursively,
131 * which is not allowed. */
132static int compl_busy = FALSE;
133
Bram Moolenaar572cb562005-08-05 21:35:02 +0000134static int compl_matches = 0;
135static char_u *compl_pattern = NULL;
136static int compl_direction = FORWARD;
137static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000138static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000139static pos_T compl_startpos;
140static colnr_T compl_col = 0; /* column where the text starts
141 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000142static char_u *compl_orig_text = NULL; /* text as it was before
143 * completion started */
144static int compl_cont_mode = 0;
145static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
Bram Moolenaar82139082011-09-14 16:52:09 +0200147static int compl_opt_refresh_always = FALSE;
148
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static void ins_ctrl_x(void);
150static int has_compl_option(int dict_opt);
151static int ins_compl_accept_char(int c);
152static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
153static int ins_compl_equal(compl_T *match, char_u *str, int len);
154static void ins_compl_longest_match(compl_T *match);
155static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
156static int ins_compl_make_cyclic(void);
157static void ins_compl_upd_pum(void);
158static void ins_compl_del_pum(void);
159static int pum_wanted(void);
160static int pum_enough_matches(void);
161static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
162static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
163static char_u *find_line_end(char_u *ptr);
164static void ins_compl_free(void);
165static void ins_compl_clear(void);
166static int ins_compl_bs(void);
167static int ins_compl_need_restart(void);
168static void ins_compl_new_leader(void);
169static void ins_compl_addleader(int c);
170static int ins_compl_len(void);
171static void ins_compl_restart(void);
172static void ins_compl_set_original_text(char_u *str);
173static void ins_compl_addfrommatch(void);
174static int ins_compl_prep(int c);
175static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
176static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000177#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100178static void ins_compl_add_list(list_T *list);
179static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000180#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100181static int ins_compl_get_exp(pos_T *ini);
182static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200183static void ins_compl_insert(int in_compl_func);
184static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100185static int ins_compl_key2dir(int c);
186static int ins_compl_pum_key(int c);
187static int ins_compl_key2count(int c);
188static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100189static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100190static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100191static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#endif /* FEAT_INS_EXPAND */
193
194#define BACKSPACE_CHAR 1
195#define BACKSPACE_WORD 2
196#define BACKSPACE_WORD_NOT_SPACE 3
197#define BACKSPACE_LINE 4
198
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100199static void ins_redraw(int ready);
200static void ins_ctrl_v(void);
201static void undisplay_dollar(void);
202static void insert_special(int, int, int);
203static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
204static void check_auto_format(int);
205static void redo_literal(int c);
206static void start_arrow(pos_T *end_insert_pos);
207static void start_arrow_with_change(pos_T *end_insert_pos, int change);
208static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000209#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100210static void check_spell_redraw(void);
211static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000212static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000213#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100214static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
215static int echeck_abbr(int);
216static int replace_pop(void);
217static void replace_join(int off);
218static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100220static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void replace_flush(void);
223static void replace_do_bs(int limit_col);
224static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ins_reg(void);
229static void ins_ctrl_g(void);
230static void ins_ctrl_hat(void);
231static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100233static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100235static int ins_start_select(int c);
236static void ins_insert(int replaceState);
237static void ins_ctrl_o(void);
238static void ins_shift(int c, int lastc);
239static void ins_del(void);
240static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ins_mouse(int c);
243static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000245#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000247#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ins_left(int end_change);
249static void ins_home(int c);
250static void ins_end(int c);
251static void ins_s_left(void);
252static void ins_right(int end_change);
253static void ins_s_right(void);
254static void ins_up(int startcol);
255static void ins_pageup(void);
256static void ins_down(int startcol);
257static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static int ins_tab(void);
262static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100266static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100268static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100270static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100271#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100272static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275static colnr_T Insstart_textlen; /* length of line when insert started */
276static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100277static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
279static char_u *last_insert = NULL; /* the text of the previous insert,
280 K_SPECIAL and CSI are escaped */
281static int last_insert_skip; /* nr of chars in front of previous insert */
282static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000283static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
285#ifdef FEAT_CINDENT
286static int can_cindent; /* may do cindenting on this line */
287#endif
288
289static int old_indent = 0; /* for ^^D command in insert mode */
290
291#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000292static int revins_on; /* reverse insert mode on */
293static int revins_chars; /* how much to skip after edit */
294static int revins_legal; /* was the last char 'legal'? */
295static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296#endif
297
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static int ins_need_undo; /* call u_save() before inserting a
299 char. Set when edit() is called.
300 after that arrow_used is used. */
301
302static int did_add_space = FALSE; /* auto_format() added an extra space
303 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200304static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
305 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306
307/*
308 * edit(): Start inserting text.
309 *
310 * "cmdchar" can be:
311 * 'i' normal insert command
312 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100313 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 * 'R' replace command
315 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
316 * but still only one <CR> is inserted. The <Esc> is not used for redo.
317 * 'g' "gI" command.
318 * 'V' "gR" command for Virtual Replace mode.
319 * 'v' "gr" command for single character Virtual Replace mode.
320 *
321 * This function is not called recursively. For CTRL-O commands, it returns
322 * and lets the caller handle the Normal-mode command.
323 *
324 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
325 */
326 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100327edit(
328 int cmdchar,
329 int startln, /* if set, insert at start of line */
330 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331{
332 int c = 0;
333 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100334 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000335 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 int i;
338 int did_backspace = TRUE; /* previous char was backspace */
339#ifdef FEAT_CINDENT
340 int line_is_white = FALSE; /* line is empty before insert */
341#endif
342 linenr_T old_topline = 0; /* topline before insertion */
343#ifdef FEAT_DIFF
344 int old_topfill = -1;
345#endif
346 int inserted_space = FALSE; /* just inserted a space */
347 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000348 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000350 /* Remember whether editing was restarted after CTRL-O. */
351 did_restart_edit = restart_edit;
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /* sleep before redrawing, needed for "CTRL-O :" that results in an
354 * error message */
355 check_for_delay(TRUE);
356
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100357 /* set Insstart_orig to Insstart */
358 update_Insstart_orig = TRUE;
359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360#ifdef HAVE_SANDBOX
361 /* Don't allow inserting in the sandbox. */
362 if (sandbox != 0)
363 {
364 EMSG(_(e_sandbox));
365 return FALSE;
366 }
367#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000368 /* Don't allow changes in the buffer while editing the cmdline. The
369 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000370 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000371 {
372 EMSG(_(e_secure));
373 return FALSE;
374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
376#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000377 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000378 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000379 {
380 EMSG(_(e_secure));
381 return FALSE;
382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 ins_compl_clear(); /* clear stuff for CTRL-X mode */
384#endif
385
Bram Moolenaar843ee412004-06-30 16:16:41 +0000386#ifdef FEAT_AUTOCMD
387 /*
388 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
389 */
390 if (cmdchar != 'r' && cmdchar != 'v')
391 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100392 pos_T save_cursor = curwin->w_cursor;
393
Bram Moolenaar1e015462005-09-25 22:16:38 +0000394# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000395 if (cmdchar == 'R')
396 ptr = (char_u *)"r";
397 else if (cmdchar == 'V')
398 ptr = (char_u *)"v";
399 else
400 ptr = (char_u *)"i";
401 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200402 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000403# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000404 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100405
Bram Moolenaar097c9922013-05-19 21:15:15 +0200406 /* Make sure the cursor didn't move. Do call check_cursor_col() in
407 * case the text was modified. Since Insert mode was not started yet
408 * a call to check_cursor_col() may move the cursor, especially with
409 * the "A" command, thus set State to avoid that. Also check that the
410 * line number is still valid (lines may have been deleted).
411 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100412 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaar097c9922013-05-19 21:15:15 +0200413# ifdef FEAT_EVAL
414 && *get_vim_var_str(VV_CHAR) == NUL
415# endif
416 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100417 {
418 int save_state = State;
419
420 curwin->w_cursor = save_cursor;
421 State = INSERT;
422 check_cursor_col();
423 State = save_state;
424 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000425 }
426#endif
427
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200428#ifdef FEAT_CONCEAL
429 /* Check if the cursor line needs redrawing before changing State. If
430 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200431 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200432#endif
433
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434#ifdef FEAT_MOUSE
435 /*
436 * When doing a paste with the middle mouse button, Insstart is set to
437 * where the paste started.
438 */
439 if (where_paste_started.lnum != 0)
440 Insstart = where_paste_started;
441 else
442#endif
443 {
444 Insstart = curwin->w_cursor;
445 if (startln)
446 Insstart.col = 0;
447 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000448 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 Insstart_blank_vcol = MAXCOL;
450 if (!did_ai)
451 ai_col = 0;
452
453 if (cmdchar != NUL && restart_edit == 0)
454 {
455 ResetRedobuff();
456 AppendNumberToRedobuff(count);
457#ifdef FEAT_VREPLACE
458 if (cmdchar == 'V' || cmdchar == 'v')
459 {
460 /* "gR" or "gr" command */
461 AppendCharToRedobuff('g');
462 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
463 }
464 else
465#endif
466 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100467 if (cmdchar == K_PS)
468 AppendCharToRedobuff('a');
469 else
470 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 if (cmdchar == 'g') /* "gI" command */
472 AppendCharToRedobuff('I');
473 else if (cmdchar == 'r') /* "r<CR>" command */
474 count = 1; /* insert only one <CR> */
475 }
476 }
477
478 if (cmdchar == 'R')
479 {
480#ifdef FEAT_FKMAP
481 if (p_fkmap && p_ri)
482 {
483 beep_flush();
484 EMSG(farsi_text_3); /* encoded in Farsi */
485 State = INSERT;
486 }
487 else
488#endif
489 State = REPLACE;
490 }
491#ifdef FEAT_VREPLACE
492 else if (cmdchar == 'V' || cmdchar == 'v')
493 {
494 State = VREPLACE;
495 replaceState = VREPLACE;
496 orig_line_count = curbuf->b_ml.ml_line_count;
497 vr_lines_changed = 1;
498 }
499#endif
500 else
501 State = INSERT;
502
503 stop_insert_mode = FALSE;
504
505 /*
506 * Need to recompute the cursor position, it might move when the cursor is
507 * on a TAB or special character.
508 */
509 curs_columns(TRUE);
510
511 /*
512 * Enable langmap or IME, indicated by 'iminsert'.
513 * Note that IME may enabled/disabled without us noticing here, thus the
514 * 'iminsert' value may not reflect what is actually used. It is updated
515 * when hitting <Esc>.
516 */
517 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
518 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100519#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
521#endif
522
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523#ifdef FEAT_MOUSE
524 setmouse();
525#endif
526#ifdef FEAT_CMDL_INFO
527 clear_showcmd();
528#endif
529#ifdef FEAT_RIGHTLEFT
530 /* there is no reverse replace mode */
531 revins_on = (State == INSERT && p_ri);
532 if (revins_on)
533 undisplay_dollar();
534 revins_chars = 0;
535 revins_legal = 0;
536 revins_scol = -1;
537#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100538 if (!p_ek)
539 /* Disable bracketed paste mode, we won't recognize the escape
540 * sequences. */
541 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
543 /*
544 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100545 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
546 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
548 if (restart_edit != 0 && stuff_empty())
549 {
550#ifdef FEAT_MOUSE
551 /*
552 * After a paste we consider text typed to be part of the insert for
553 * the pasted text. You can backspace over the pasted text too.
554 */
555 if (where_paste_started.lnum)
556 arrow_used = FALSE;
557 else
558#endif
559 arrow_used = TRUE;
560 restart_edit = 0;
561
562 /*
563 * If the cursor was after the end-of-line before the CTRL-O and it is
564 * now at the end-of-line, put it after the end-of-line (this is not
565 * correct in very rare cases).
566 * Also do this if curswant is greater than the current virtual
567 * column. Eg after "^O$" or "^O80|".
568 */
569 validate_virtcol();
570 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000571 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 || curwin->w_curswant > curwin->w_virtcol)
573 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
574 {
575 if (ptr[1] == NUL)
576 ++curwin->w_cursor.col;
577#ifdef FEAT_MBYTE
578 else if (has_mbyte)
579 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000580 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 if (ptr[i] == NUL)
582 curwin->w_cursor.col += i;
583 }
584#endif
585 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000586 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
588 else
589 arrow_used = FALSE;
590
591 /* we are in insert mode now, don't need to start it anymore */
592 need_start_insertmode = FALSE;
593
594 /* Need to save the line for undo before inserting the first char. */
595 ins_need_undo = TRUE;
596
597#ifdef FEAT_MOUSE
598 where_paste_started.lnum = 0;
599#endif
600#ifdef FEAT_CINDENT
601 can_cindent = TRUE;
602#endif
603#ifdef FEAT_FOLDING
604 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
605 * restarting. */
606 if (!p_im && did_restart_edit == 0)
607 foldOpenCursor();
608#endif
609
610 /*
611 * If 'showmode' is set, show the current (insert/replace/..) mode.
612 * A warning message for changing a readonly file is given here, before
613 * actually changing anything. It's put after the mode, if any.
614 */
615 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000616 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 i = showmode();
618
619 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000620 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622#ifdef CURSOR_SHAPE
623 ui_cursor_shape(); /* may show different cursor shape */
624#endif
625#ifdef FEAT_DIGRAPHS
626 do_digraph(-1); /* clear digraphs */
627#endif
628
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000629 /*
630 * Get the current length of the redo buffer, those characters have to be
631 * skipped if we want to get to the inserted characters.
632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 ptr = get_inserted();
634 if (ptr == NULL)
635 new_insert_skip = 0;
636 else
637 {
638 new_insert_skip = (int)STRLEN(ptr);
639 vim_free(ptr);
640 }
641
642 old_indent = 0;
643
644 /*
645 * Main loop in Insert mode: repeat until Insert mode is left.
646 */
647 for (;;)
648 {
649#ifdef FEAT_RIGHTLEFT
650 if (!revins_legal)
651 revins_scol = -1; /* reset on illegal motions */
652 else
653 revins_legal = 0;
654#endif
655 if (arrow_used) /* don't repeat insert when arrow key used */
656 count = 0;
657
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100658 if (update_Insstart_orig)
659 Insstart_orig = Insstart;
660
Bram Moolenaar00672e12016-06-26 18:38:13 +0200661 if (stop_insert_mode
662#ifdef FEAT_INS_EXPAND
663 && !pum_visible()
664#endif
665 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 {
667 /* ":stopinsert" used or 'insertmode' reset */
668 count = 0;
669 goto doESCkey;
670 }
671
672 /* set curwin->w_curswant for next K_DOWN or K_UP */
673 if (!arrow_used)
674 curwin->w_set_curswant = TRUE;
675
676 /* If there is no typeahead may check for timestamps (e.g., for when a
677 * menu invoked a shell command). */
678 if (stuff_empty())
679 {
680 did_check_timestamps = FALSE;
681 if (need_check_timestamps)
682 check_timestamps(FALSE);
683 }
684
685 /*
686 * When emsg() was called msg_scroll will have been set.
687 */
688 msg_scroll = FALSE;
689
690#ifdef FEAT_GUI
691 /* When 'mousefocus' is set a mouse movement may have taken us to
692 * another window. "need_mouse_correct" may then be set because of an
693 * autocommand. */
694 if (need_mouse_correct)
695 gui_mouse_correct();
696#endif
697
698#ifdef FEAT_FOLDING
699 /* Open fold at the cursor line, according to 'foldopen'. */
700 if (fdo_flags & FDO_INSERT)
701 foldOpenCursor();
702 /* Close folds where the cursor isn't, according to 'foldclose' */
703 if (!char_avail())
704 foldCheckClose();
705#endif
706
707 /*
708 * If we inserted a character at the last position of the last line in
709 * the window, scroll the window one line up. This avoids an extra
710 * redraw.
711 * This is detected when the cursor column is smaller after inserting
712 * something.
713 * Don't do this when the topline changed already, it has
714 * already been adjusted (by insertchar() calling open_line())).
715 */
716 if (curbuf->b_mod_set
717 && curwin->w_p_wrap
718 && !did_backspace
719 && curwin->w_topline == old_topline
720#ifdef FEAT_DIFF
721 && curwin->w_topfill == old_topfill
722#endif
723 )
724 {
725 mincol = curwin->w_wcol;
726 validate_cursor_col();
727
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000728 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 && curwin->w_wrow == W_WINROW(curwin)
730 + curwin->w_height - 1 - p_so
731 && (curwin->w_cursor.lnum != curwin->w_topline
732#ifdef FEAT_DIFF
733 || curwin->w_topfill > 0
734#endif
735 ))
736 {
737#ifdef FEAT_DIFF
738 if (curwin->w_topfill > 0)
739 --curwin->w_topfill;
740 else
741#endif
742#ifdef FEAT_FOLDING
743 if (hasFolding(curwin->w_topline, NULL, &old_topline))
744 set_topline(curwin, old_topline + 1);
745 else
746#endif
747 set_topline(curwin, curwin->w_topline + 1);
748 }
749 }
750
751 /* May need to adjust w_topline to show the cursor. */
752 update_topline();
753
754 did_backspace = FALSE;
755
756 validate_cursor(); /* may set must_redraw */
757
758 /*
759 * Redraw the display when no characters are waiting.
760 * Also shows mode, ruler and positions cursor.
761 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000762 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763
764#ifdef FEAT_SCROLLBIND
765 if (curwin->w_p_scb)
766 do_check_scrollbind(TRUE);
767#endif
768
Bram Moolenaar860cae12010-06-05 23:22:07 +0200769#ifdef FEAT_CURSORBIND
770 if (curwin->w_p_crb)
771 do_check_cursorbind();
772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 update_curswant();
774 old_topline = curwin->w_topline;
775#ifdef FEAT_DIFF
776 old_topfill = curwin->w_topfill;
777#endif
778
779#ifdef USE_ON_FLY_SCROLL
780 dont_scroll = FALSE; /* allow scrolling here */
781#endif
782
783 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100784 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100786 if (c != K_CURSORHOLD)
787 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200788
789 /* After using CTRL-G U the next cursor key will not break undo. */
790 if (dont_sync_undo == MAYBE)
791 dont_sync_undo = TRUE;
792 else
793 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100794 if (cmdchar == K_PS)
795 /* Got here from normal mode when bracketed paste started. */
796 c = K_PS;
797 else
798 do
799 {
800 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100801 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000803#ifdef FEAT_AUTOCMD
804 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
805 did_cursorhold = TRUE;
806#endif
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808#ifdef FEAT_RIGHTLEFT
809 if (p_hkmap && KeyTyped)
810 c = hkmap(c); /* Hebrew mode mapping */
811#endif
812#ifdef FEAT_FKMAP
813 if (p_fkmap && KeyTyped)
814 c = fkmap(c); /* Farsi mode mapping */
815#endif
816
817#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000818 /*
819 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000820 * and the cursor is still in the completed word. Only when there is
821 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000822 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000823 if (compl_started
824 && pum_wanted()
825 && curwin->w_cursor.col >= compl_col
826 && (compl_shown_match == NULL
827 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000828 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000829 /* BS: Delete one character from "compl_leader". */
830 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000831 && curwin->w_cursor.col > compl_col
832 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000833 continue;
834
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000835 /* When no match was selected or it was edited. */
836 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000837 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000838 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000839 * "compl_leader". Except when at the original match and
840 * there is nothing to add, CTRL-L works like CTRL-P then. */
841 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100842 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000843 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000844 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000845 {
846 ins_compl_addfrommatch();
847 continue;
848 }
849
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000850 /* A non-white character that fits in with the current
851 * completion: Add to "compl_leader". */
852 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000853 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100854#ifdef FEAT_AUTOCMD
855 /* Trigger InsertCharPre. */
856 char_u *str = do_insert_char_pre(c);
857 char_u *p;
858
859 if (str != NULL)
860 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100861 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100862 ins_compl_addleader(PTR2CHAR(p));
863 vim_free(str);
864 }
865 else
866#endif
867 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000868 continue;
869 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000870
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000871 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000872 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200873 if ((c == Ctrl_Y || (compl_enter_selects
874 && (c == CAR || c == K_KENTER || c == NL)))
875 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000876 {
877 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200878 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000879 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000880 }
881 }
882
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
884 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000885 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000886 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000887 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888#endif
889
Bram Moolenaar488c6512005-08-11 20:09:58 +0000890 /* CTRL-\ CTRL-N goes to Normal mode,
891 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
892 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (c == Ctrl_BSL)
894 {
895 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000896 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 ++no_mapping;
898 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000899 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 --no_mapping;
901 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000902 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000904 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 vungetc(c);
906 c = Ctrl_BSL;
907 }
908 else if (c == Ctrl_G && p_im)
909 continue;
910 else
911 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000912 if (c == Ctrl_O)
913 {
914 ins_ctrl_o();
915 ins_at_eol = FALSE; /* cursor keeps its column */
916 nomove = TRUE;
917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 count = 0;
919 goto doESCkey;
920 }
921 }
922
923#ifdef FEAT_DIGRAPHS
924 c = do_digraph(c);
925#endif
926
927#ifdef FEAT_INS_EXPAND
928 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
929 goto docomplete;
930#endif
931 if (c == Ctrl_V || c == Ctrl_Q)
932 {
933 ins_ctrl_v();
934 c = Ctrl_V; /* pretend CTRL-V is last typed character */
935 continue;
936 }
937
938#ifdef FEAT_CINDENT
939 if (cindent_on()
940# ifdef FEAT_INS_EXPAND
941 && ctrl_x_mode == 0
942# endif
943 )
944 {
945 /* A key name preceded by a bang means this key is not to be
946 * inserted. Skip ahead to the re-indenting below.
947 * A key name preceded by a star means that indenting has to be
948 * done before inserting the key. */
949 line_is_white = inindent(0);
950 if (in_cinkeys(c, '!', line_is_white))
951 goto force_cindent;
952 if (can_cindent && in_cinkeys(c, '*', line_is_white)
953 && stop_arrow() == OK)
954 do_c_expr_indent();
955 }
956#endif
957
958#ifdef FEAT_RIGHTLEFT
959 if (curwin->w_p_rl)
960 switch (c)
961 {
962 case K_LEFT: c = K_RIGHT; break;
963 case K_S_LEFT: c = K_S_RIGHT; break;
964 case K_C_LEFT: c = K_C_RIGHT; break;
965 case K_RIGHT: c = K_LEFT; break;
966 case K_S_RIGHT: c = K_S_LEFT; break;
967 case K_C_RIGHT: c = K_C_LEFT; break;
968 }
969#endif
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 /*
972 * If 'keymodel' contains "startsel", may start selection. If it
973 * does, a CTRL-O and c will be stuffed, we need to get these
974 * characters.
975 */
976 if (ins_start_select(c))
977 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
979 /*
980 * The big switch to handle a character in insert mode.
981 */
982 switch (c)
983 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000984 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (echeck_abbr(ESC + ABBR_OFF))
986 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200987 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000989 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#ifdef FEAT_CMDWIN
991 if (c == Ctrl_C && cmdwin_type != 0)
992 {
993 /* Close the cmdline window. */
994 cmdwin_result = K_IGNORE;
995 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000996 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 goto doESCkey;
998 }
999#endif
1000
1001#ifdef UNIX
1002do_intr:
1003#endif
1004 /* when 'insertmode' set, and not halfway a mapping, don't leave
1005 * Insert mode */
1006 if (goto_im())
1007 {
1008 if (got_int)
1009 {
1010 (void)vgetc(); /* flush all buffers */
1011 got_int = FALSE;
1012 }
1013 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001014 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 break;
1016 }
1017doESCkey:
1018 /*
1019 * This is the ONLY return from edit()!
1020 */
1021 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1022 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001023 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 o_lnum = curwin->w_cursor.lnum;
1025
Bram Moolenaar488c6512005-08-11 20:09:58 +00001026 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001027 {
1028#ifdef FEAT_AUTOCMD
1029 if (cmdchar != 'r' && cmdchar != 'v')
1030 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1031 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001032 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 continue;
1037
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case Ctrl_Z: /* suspend when 'insertmode' set */
1039 if (!p_im)
1040 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001041 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001042#ifdef CURSOR_SHAPE
1043 ui_cursor_shape(); /* may need to update cursor shape */
1044#endif
1045 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001046
1047 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001048#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001049 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001050 goto docomplete;
1051#endif
1052 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1053 break;
1054 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001055
1056#ifdef FEAT_VIRTUALEDIT
1057 /* don't move the cursor left when 'virtualedit' has "onemore". */
1058 if (ve_flags & VE_ONEMORE)
1059 {
1060 ins_at_eol = FALSE;
1061 nomove = TRUE;
1062 }
1063#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001064 count = 0;
1065 goto doESCkey;
1066
Bram Moolenaar572cb562005-08-05 21:35:02 +00001067 case K_INS: /* toggle insert/replace mode */
1068 case K_KINS:
1069 ins_insert(replaceState);
1070 break;
1071
1072 case K_SELECT: /* end of Select mode mapping - ignore */
1073 break;
1074
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001075 case K_HELP: /* Help key works like <ESC> <Help> */
1076 case K_F1:
1077 case K_XF1:
1078 stuffcharReadbuff(K_HELP);
1079 if (p_im)
1080 need_start_insertmode = TRUE;
1081 goto doESCkey;
1082
1083#ifdef FEAT_NETBEANS_INTG
1084 case K_F21: /* NetBeans command */
1085 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001086 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001087 --no_mapping;
1088 netbeans_keycommand(i);
1089 break;
1090#endif
1091
1092 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 case NUL:
1094 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 /* For ^@ the trailing ESC will end the insert, unless there is an
1096 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1098 && c != Ctrl_A && !p_im)
1099 goto doESCkey; /* quit insert mode */
1100 inserted_space = FALSE;
1101 break;
1102
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001103 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ins_reg();
1105 auto_format(FALSE, TRUE);
1106 inserted_space = FALSE;
1107 break;
1108
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 ins_ctrl_g();
1111 break;
1112
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001113 case Ctrl_HAT: /* switch input mode and/or langmap */
1114 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 break;
1116
1117#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (!p_ari)
1120 goto normalchar;
1121 ins_ctrl_();
1122 break;
1123#endif
1124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001125 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1127 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1128 goto docomplete;
1129#endif
1130 /* FALLTHROUGH */
1131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133# ifdef FEAT_INS_EXPAND
1134 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1135 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 if (has_compl_option(FALSE))
1137 goto docomplete;
1138 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140# endif
1141 ins_shift(c, lastc);
1142 auto_format(FALSE, TRUE);
1143 inserted_space = FALSE;
1144 break;
1145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 case K_KDEL:
1148 ins_del();
1149 auto_format(FALSE, TRUE);
1150 break;
1151
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001152 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 case Ctrl_H:
1154 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1155 auto_format(FALSE, TRUE);
1156 break;
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1160 auto_format(FALSE, TRUE);
1161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001164# ifdef FEAT_COMPL_FUNC
1165 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001166 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001167 goto docomplete;
1168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1170 auto_format(FALSE, TRUE);
1171 inserted_space = FALSE;
1172 break;
1173
1174#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001175 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 case K_LEFTMOUSE_NM:
1177 case K_LEFTDRAG:
1178 case K_LEFTRELEASE:
1179 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001180 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 case K_MIDDLEMOUSE:
1182 case K_MIDDLEDRAG:
1183 case K_MIDDLERELEASE:
1184 case K_RIGHTMOUSE:
1185 case K_RIGHTDRAG:
1186 case K_RIGHTRELEASE:
1187 case K_X1MOUSE:
1188 case K_X1DRAG:
1189 case K_X1RELEASE:
1190 case K_X2MOUSE:
1191 case K_X2DRAG:
1192 case K_X2RELEASE:
1193 ins_mouse(c);
1194 break;
1195
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001196 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001197 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 break;
1199
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001200 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001201 ins_mousescroll(MSCR_UP);
1202 break;
1203
1204 case K_MOUSELEFT: /* Scroll wheel left */
1205 ins_mousescroll(MSCR_LEFT);
1206 break;
1207
1208 case K_MOUSERIGHT: /* Scroll wheel right */
1209 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 break;
1211#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001212 case K_PS:
1213 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1214 if (cmdchar == K_PS)
1215 /* invoked from normal mode, bail out */
1216 goto doESCkey;
1217 break;
1218 case K_PE:
1219 /* Got K_PE without K_PS, ignore. */
1220 break;
1221
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001222#ifdef FEAT_GUI_TABLINE
1223 case K_TABLINE:
1224 case K_TABMENU:
1225 ins_tabline(c);
1226 break;
1227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001229 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 break;
1231
Bram Moolenaar754b5602006-02-09 23:53:20 +00001232#ifdef FEAT_AUTOCMD
1233 case K_CURSORHOLD: /* Didn't type something for a while. */
1234 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1235 did_cursorhold = TRUE;
1236 break;
1237#endif
1238
Bram Moolenaar4770d092006-01-12 23:22:24 +00001239#ifdef FEAT_GUI_W32
1240 /* On Win32 ignore <M-F4>, we get it when closing the window was
1241 * cancelled. */
1242 case K_F4:
1243 if (mod_mask != MOD_MASK_ALT)
1244 goto normalchar;
1245 break;
1246#endif
1247
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248#ifdef FEAT_GUI
1249 case K_VER_SCROLLBAR:
1250 ins_scroll();
1251 break;
1252
1253 case K_HOR_SCROLLBAR:
1254 ins_horscroll();
1255 break;
1256#endif
1257
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001258 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 case K_S_HOME:
1261 case K_C_HOME:
1262 ins_home(c);
1263 break;
1264
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001265 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 case K_S_END:
1268 case K_C_END:
1269 ins_end(c);
1270 break;
1271
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001272 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001273 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1274 ins_s_left();
1275 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001276 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 break;
1278
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001279 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 case K_C_LEFT:
1281 ins_s_left();
1282 break;
1283
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001284 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001285 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1286 ins_s_right();
1287 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001288 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 break;
1290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 case K_C_RIGHT:
1293 ins_s_right();
1294 break;
1295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001297#ifdef FEAT_INS_EXPAND
1298 if (pum_visible())
1299 goto docomplete;
1300#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001301 if (mod_mask & MOD_MASK_SHIFT)
1302 ins_pageup();
1303 else
1304 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 break;
1306
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001307 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 case K_PAGEUP:
1309 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001310#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001311 if (pum_visible())
1312 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 ins_pageup();
1315 break;
1316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001317 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001318#ifdef FEAT_INS_EXPAND
1319 if (pum_visible())
1320 goto docomplete;
1321#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001322 if (mod_mask & MOD_MASK_SHIFT)
1323 ins_pagedown();
1324 else
1325 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 break;
1327
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001328 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 case K_PAGEDOWN:
1330 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001331#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001332 if (pum_visible())
1333 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 ins_pagedown();
1336 break;
1337
1338#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 ins_drop();
1341 break;
1342#endif
1343
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 c = TAB;
1346 /* FALLTHROUGH */
1347
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001348 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1350 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1351 goto docomplete;
1352#endif
1353 inserted_space = FALSE;
1354 if (ins_tab())
1355 goto normalchar; /* insert TAB as a normal char */
1356 auto_format(FALSE, TRUE);
1357 break;
1358
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001359 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 c = CAR;
1361 /* FALLTHROUGH */
1362 case CAR:
1363 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001364#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 /* In a quickfix window a <CR> jumps to the error under the
1366 * cursor. */
1367 if (bt_quickfix(curbuf) && c == CAR)
1368 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001369 if (curwin->w_llist_ref == NULL) /* quickfix window */
1370 do_cmdline_cmd((char_u *)".cc");
1371 else /* location list window */
1372 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 break;
1374 }
1375#endif
1376#ifdef FEAT_CMDWIN
1377 if (cmdwin_type != 0)
1378 {
1379 /* Execute the command in the cmdline window. */
1380 cmdwin_result = CAR;
1381 goto doESCkey;
1382 }
1383#endif
1384 if (ins_eol(c) && !p_im)
1385 goto doESCkey; /* out of memory */
1386 auto_format(FALSE, FALSE);
1387 inserted_space = FALSE;
1388 break;
1389
Bram Moolenaar860cae12010-06-05 23:22:07 +02001390#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001391 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392# ifdef FEAT_INS_EXPAND
1393 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1394 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001395 if (has_compl_option(TRUE))
1396 goto docomplete;
1397 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
1399# endif
1400# ifdef FEAT_DIGRAPHS
1401 c = ins_digraph();
1402 if (c == NUL)
1403 break;
1404# endif
1405 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
1408#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001409 case Ctrl_X: /* Enter CTRL-X mode */
1410 ins_ctrl_x();
1411 break;
1412
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001413 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 if (ctrl_x_mode != CTRL_X_TAGS)
1415 goto normalchar;
1416 goto docomplete;
1417
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001418 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (ctrl_x_mode != CTRL_X_FILES)
1420 goto normalchar;
1421 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001422
1423 case 's': /* Spelling completion after ^X */
1424 case Ctrl_S:
1425 if (ctrl_x_mode != CTRL_X_SPELL)
1426 goto normalchar;
1427 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428#endif
1429
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001430 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431#ifdef FEAT_INS_EXPAND
1432 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1433#endif
1434 {
1435 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1436 if (p_im)
1437 {
1438 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1439 break;
1440 goto doESCkey;
1441 }
1442 goto normalchar;
1443 }
1444#ifdef FEAT_INS_EXPAND
1445 /* FALLTHROUGH */
1446
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001447 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 case Ctrl_N:
1449 /* if 'complete' is empty then plain ^P is no longer special,
1450 * but it is under other ^X modes */
1451 if (*curbuf->b_p_cpt == NUL
1452 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001453 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 goto normalchar;
1455
1456docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001457 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001458#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001459 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001460#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001461 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001462 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001463#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001464 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001465#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001466 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 break;
1468#endif /* FEAT_INS_EXPAND */
1469
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001470 case Ctrl_Y: /* copy from previous line or scroll down */
1471 case Ctrl_E: /* copy from next line or scroll up */
1472 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 break;
1474
1475 default:
1476#ifdef UNIX
1477 if (c == intr_char) /* special interrupt char */
1478 goto do_intr;
1479#endif
1480
Bram Moolenaare659c952011-05-19 17:25:41 +02001481normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001483 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001485#ifdef FEAT_AUTOCMD
1486 if (!p_paste)
1487 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001488 /* Trigger InsertCharPre. */
1489 char_u *str = do_insert_char_pre(c);
1490 char_u *p;
1491
1492 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001493 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001494 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001495 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001496 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001497 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001498 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001499 c = PTR2CHAR(p);
1500 if (c == CAR || c == K_KENTER || c == NL)
1501 ins_eol(c);
1502 else
1503 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001504 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001505 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001506 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001507 vim_free(str);
1508 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001509 }
1510
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001511 /* If the new value is already inserted or an empty string
1512 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001513 if (c == NUL)
1514 break;
1515 }
1516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517#ifdef FEAT_SMARTINDENT
1518 /* Try to perform smart-indenting. */
1519 ins_try_si(c);
1520#endif
1521
1522 if (c == ' ')
1523 {
1524 inserted_space = TRUE;
1525#ifdef FEAT_CINDENT
1526 if (inindent(0))
1527 can_cindent = FALSE;
1528#endif
1529 if (Insstart_blank_vcol == MAXCOL
1530 && curwin->w_cursor.lnum == Insstart.lnum)
1531 Insstart_blank_vcol = get_nolist_virtcol();
1532 }
1533
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001534 /* Insert a normal character and check for abbreviations on a
1535 * special character. Let CTRL-] expand abbreviations without
1536 * inserting it. */
1537 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538#ifdef FEAT_MBYTE
1539 /* Add ABBR_OFF for characters above 0x100, this is
1540 * what check_abbr() expects. */
1541 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1542#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001543 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 {
1545 insert_special(c, FALSE, FALSE);
1546#ifdef FEAT_RIGHTLEFT
1547 revins_legal++;
1548 revins_chars++;
1549#endif
1550 }
1551
1552 auto_format(FALSE, TRUE);
1553
1554#ifdef FEAT_FOLDING
1555 /* When inserting a character the cursor line must never be in a
1556 * closed fold. */
1557 foldOpenCursor();
1558#endif
1559 break;
1560 } /* end of switch (c) */
1561
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001562#ifdef FEAT_AUTOCMD
1563 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001564 if (c != K_CURSORHOLD
1565# ifdef FEAT_COMPL_FUNC
1566 /* but not in CTRL-X mode, a script can't restore the state */
1567 && ctrl_x_mode == 0
1568# endif
1569 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001570 did_cursorhold = FALSE;
1571#endif
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 /* If the cursor was moved we didn't just insert a space */
1574 if (arrow_used)
1575 inserted_space = FALSE;
1576
1577#ifdef FEAT_CINDENT
1578 if (can_cindent && cindent_on()
1579# ifdef FEAT_INS_EXPAND
1580 && ctrl_x_mode == 0
1581# endif
1582 )
1583 {
1584force_cindent:
1585 /*
1586 * Indent now if a key was typed that is in 'cinkeys'.
1587 */
1588 if (in_cinkeys(c, ' ', line_is_white))
1589 {
1590 if (stop_arrow() == OK)
1591 /* re-indent the current line */
1592 do_c_expr_indent();
1593 }
1594 }
1595#endif /* FEAT_CINDENT */
1596
1597 } /* for (;;) */
1598 /* NOTREACHED */
1599}
1600
1601/*
1602 * Redraw for Insert mode.
1603 * This is postponed until getting the next character to make '$' in the 'cpo'
1604 * option work correctly.
1605 * Only redraw when there are no characters available. This speeds up
1606 * inserting sequences of characters (e.g., for CTRL-R).
1607 */
1608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001609ins_redraw(
1610 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001612#ifdef FEAT_CONCEAL
1613 linenr_T conceal_old_cursor_line = 0;
1614 linenr_T conceal_new_cursor_line = 0;
1615 int conceal_update_lines = FALSE;
1616#endif
1617
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001618 if (char_avail())
1619 return;
1620
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001621#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001622 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1623 * visible, the command might delete it. */
1624 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001625# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001626 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001627# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001628# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001629 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001630# endif
1631# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001632 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001633# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001634 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001635# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001636 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001637# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001638# ifdef FEAT_INS_EXPAND
1639 && !pum_visible()
1640# endif
1641 )
1642 {
1643# ifdef FEAT_SYN_HL
1644 /* Need to update the screen first, to make sure syntax
1645 * highlighting is correct after making a change (e.g., inserting
1646 * a "(". The autocommand may also require a redraw, so it's done
1647 * again below, unfortunately. */
1648 if (syntax_present(curwin) && must_redraw)
1649 update_screen(0);
1650# endif
1651# ifdef FEAT_AUTOCMD
1652 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001653 {
1654 /* Make sure curswant is correct, an autocommand may call
1655 * getcurpos(). */
1656 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001657 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001658 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001659# endif
1660# ifdef FEAT_CONCEAL
1661 if (curwin->w_p_cole > 0)
1662 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001663# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001664 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001665# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001666 conceal_new_cursor_line = curwin->w_cursor.lnum;
1667 conceal_update_lines = TRUE;
1668 }
1669# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001670# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001671 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001672# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001673 }
1674#endif
1675
1676#ifdef FEAT_AUTOCMD
1677 /* Trigger TextChangedI if b_changedtick differs. */
1678 if (ready && has_textchangedI()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001679 && last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001680# ifdef FEAT_INS_EXPAND
1681 && !pum_visible()
1682# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001683 )
1684 {
1685 if (last_changedtick_buf == curbuf)
1686 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1687 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001688 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001690#endif
1691
1692 if (must_redraw)
1693 update_screen(0);
1694 else if (clear_cmdline || redraw_cmdline)
1695 showmode(); /* clear cmdline and show mode */
1696# if defined(FEAT_CONCEAL)
1697 if ((conceal_update_lines
1698 && (conceal_old_cursor_line != conceal_new_cursor_line
1699 || conceal_cursor_line(curwin)))
1700 || need_cursor_line_redraw)
1701 {
1702 if (conceal_old_cursor_line != conceal_new_cursor_line)
1703 update_single_line(curwin, conceal_old_cursor_line);
1704 update_single_line(curwin, conceal_new_cursor_line == 0
1705 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1706 curwin->w_valid &= ~VALID_CROW;
1707 }
1708# endif
1709 showruler(FALSE);
1710 setcursor();
1711 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712}
1713
1714/*
1715 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1716 */
1717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001718ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001721 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
1723 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001724 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
1726 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001729 did_putchar = TRUE;
1730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1732
1733#ifdef FEAT_CMDL_INFO
1734 add_to_showcmd_c(Ctrl_V);
1735#endif
1736
1737 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001738 if (did_putchar)
1739 /* when the line fits in 'columns' the '^' is at the start of the next
1740 * line and will not removed by the redraw */
1741 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742#ifdef FEAT_CMDL_INFO
1743 clear_showcmd();
1744#endif
1745 insert_special(c, FALSE, TRUE);
1746#ifdef FEAT_RIGHTLEFT
1747 revins_chars++;
1748 revins_legal++;
1749#endif
1750}
1751
1752/*
1753 * Put a character directly onto the screen. It's not stored in a buffer.
1754 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1755 */
1756static int pc_status;
1757#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1758#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1759#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1760#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762static int pc_attr;
1763static int pc_row;
1764static int pc_col;
1765
1766 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001767edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 int attr;
1770
1771 if (ScreenLines != NULL)
1772 {
1773 update_topline(); /* just in case w_topline isn't valid */
1774 validate_cursor();
1775 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001776 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 else
1778 attr = 0;
1779 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001780 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1782 pc_status = PC_STATUS_UNSET;
1783#endif
1784#ifdef FEAT_RIGHTLEFT
1785 if (curwin->w_p_rl)
1786 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001787 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788# ifdef FEAT_MBYTE
1789 if (has_mbyte)
1790 {
1791 int fix_col = mb_fix_col(pc_col, pc_row);
1792
1793 if (fix_col != pc_col)
1794 {
1795 screen_putchar(' ', pc_row, fix_col, attr);
1796 --curwin->w_wcol;
1797 pc_status = PC_STATUS_RIGHT;
1798 }
1799 }
1800# endif
1801 }
1802 else
1803#endif
1804 {
1805 pc_col += curwin->w_wcol;
1806#ifdef FEAT_MBYTE
1807 if (mb_lefthalve(pc_row, pc_col))
1808 pc_status = PC_STATUS_LEFT;
1809#endif
1810 }
1811
1812 /* save the character to be able to put it back */
1813#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1814 if (pc_status == PC_STATUS_UNSET)
1815#endif
1816 {
1817 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1818 pc_status = PC_STATUS_SET;
1819 }
1820 screen_putchar(c, pc_row, pc_col, attr);
1821 }
1822}
1823
1824/*
1825 * Undo the previous edit_putchar().
1826 */
1827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001828edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
1830 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1831 {
1832#if defined(FEAT_MBYTE)
1833 if (pc_status == PC_STATUS_RIGHT)
1834 ++curwin->w_wcol;
1835 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1836 redrawWinline(curwin->w_cursor.lnum, FALSE);
1837 else
1838#endif
1839 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1840 }
1841}
1842
1843/*
1844 * Called when p_dollar is set: display a '$' at the end of the changed text
1845 * Only works when cursor is in the line that changes.
1846 */
1847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001848display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849{
1850 colnr_T save_col;
1851
1852 if (!redrawing())
1853 return;
1854
1855 cursor_off();
1856 save_col = curwin->w_cursor.col;
1857 curwin->w_cursor.col = col;
1858#ifdef FEAT_MBYTE
1859 if (has_mbyte)
1860 {
1861 char_u *p;
1862
1863 /* If on the last byte of a multi-byte move to the first byte. */
1864 p = ml_get_curline();
1865 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1866 }
1867#endif
1868 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001869 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
1871 edit_putchar('$', FALSE);
1872 dollar_vcol = curwin->w_virtcol;
1873 }
1874 curwin->w_cursor.col = save_col;
1875}
1876
1877/*
1878 * Call this function before moving the cursor from the normal insert position
1879 * in insert mode.
1880 */
1881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001882undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001884 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001886 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 redrawWinline(curwin->w_cursor.lnum, FALSE);
1888 }
1889}
1890
1891/*
1892 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1893 * Keep the cursor on the same character.
1894 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1895 * type == INDENT_DEC decrease indent (for CTRL-D)
1896 * type == INDENT_SET set indent to "amount"
1897 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1898 */
1899 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001900change_indent(
1901 int type,
1902 int amount,
1903 int round,
1904 int replaced, /* replaced character, put on replace stack */
1905 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906{
1907 int vcol;
1908 int last_vcol;
1909 int insstart_less; /* reduction for Insstart.col */
1910 int new_cursor_col;
1911 int i;
1912 char_u *ptr;
1913 int save_p_list;
1914 int start_col;
1915 colnr_T vc;
1916#ifdef FEAT_VREPLACE
1917 colnr_T orig_col = 0; /* init for GCC */
1918 char_u *new_line, *orig_line = NULL; /* init for GCC */
1919
1920 /* VREPLACE mode needs to know what the line was like before changing */
1921 if (State & VREPLACE_FLAG)
1922 {
1923 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1924 orig_col = curwin->w_cursor.col;
1925 }
1926#endif
1927
1928 /* for the following tricks we don't want list mode */
1929 save_p_list = curwin->w_p_list;
1930 curwin->w_p_list = FALSE;
1931 vc = getvcol_nolist(&curwin->w_cursor);
1932 vcol = vc;
1933
1934 /*
1935 * For Replace mode we need to fix the replace stack later, which is only
1936 * possible when the cursor is in the indent. Remember the number of
1937 * characters before the cursor if it's possible.
1938 */
1939 start_col = curwin->w_cursor.col;
1940
1941 /* determine offset from first non-blank */
1942 new_cursor_col = curwin->w_cursor.col;
1943 beginline(BL_WHITE);
1944 new_cursor_col -= curwin->w_cursor.col;
1945
1946 insstart_less = curwin->w_cursor.col;
1947
1948 /*
1949 * If the cursor is in the indent, compute how many screen columns the
1950 * cursor is to the left of the first non-blank.
1951 */
1952 if (new_cursor_col < 0)
1953 vcol = get_indent() - vcol;
1954
1955 if (new_cursor_col > 0) /* can't fix replace stack */
1956 start_col = -1;
1957
1958 /*
1959 * Set the new indent. The cursor will be put on the first non-blank.
1960 */
1961 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001962 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 else
1964 {
1965#ifdef FEAT_VREPLACE
1966 int save_State = State;
1967
1968 /* Avoid being called recursively. */
1969 if (State & VREPLACE_FLAG)
1970 State = INSERT;
1971#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001972 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973#ifdef FEAT_VREPLACE
1974 State = save_State;
1975#endif
1976 }
1977 insstart_less -= curwin->w_cursor.col;
1978
1979 /*
1980 * Try to put cursor on same character.
1981 * If the cursor is at or after the first non-blank in the line,
1982 * compute the cursor column relative to the column of the first
1983 * non-blank character.
1984 * If we are not in insert mode, leave the cursor on the first non-blank.
1985 * If the cursor is before the first non-blank, position it relative
1986 * to the first non-blank, counted in screen columns.
1987 */
1988 if (new_cursor_col >= 0)
1989 {
1990 /*
1991 * When changing the indent while the cursor is touching it, reset
1992 * Insstart_col to 0.
1993 */
1994 if (new_cursor_col == 0)
1995 insstart_less = MAXCOL;
1996 new_cursor_col += curwin->w_cursor.col;
1997 }
1998 else if (!(State & INSERT))
1999 new_cursor_col = curwin->w_cursor.col;
2000 else
2001 {
2002 /*
2003 * Compute the screen column where the cursor should be.
2004 */
2005 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002006 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007
2008 /*
2009 * Advance the cursor until we reach the right screen column.
2010 */
2011 vcol = last_vcol = 0;
2012 new_cursor_col = -1;
2013 ptr = ml_get_curline();
2014 while (vcol <= (int)curwin->w_virtcol)
2015 {
2016 last_vcol = vcol;
2017#ifdef FEAT_MBYTE
2018 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002019 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 else
2021#endif
2022 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002023 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025 vcol = last_vcol;
2026
2027 /*
2028 * May need to insert spaces to be able to position the cursor on
2029 * the right screen column.
2030 */
2031 if (vcol != (int)curwin->w_virtcol)
2032 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002033 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002035 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (ptr != NULL)
2037 {
2038 new_cursor_col += i;
2039 ptr[i] = NUL;
2040 while (--i >= 0)
2041 ptr[i] = ' ';
2042 ins_str(ptr);
2043 vim_free(ptr);
2044 }
2045 }
2046
2047 /*
2048 * When changing the indent while the cursor is in it, reset
2049 * Insstart_col to 0.
2050 */
2051 insstart_less = MAXCOL;
2052 }
2053
2054 curwin->w_p_list = save_p_list;
2055
2056 if (new_cursor_col <= 0)
2057 curwin->w_cursor.col = 0;
2058 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002059 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 curwin->w_set_curswant = TRUE;
2061 changed_cline_bef_curs();
2062
2063 /*
2064 * May have to adjust the start of the insert.
2065 */
2066 if (State & INSERT)
2067 {
2068 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2069 {
2070 if ((int)Insstart.col <= insstart_less)
2071 Insstart.col = 0;
2072 else
2073 Insstart.col -= insstart_less;
2074 }
2075 if ((int)ai_col <= insstart_less)
2076 ai_col = 0;
2077 else
2078 ai_col -= insstart_less;
2079 }
2080
2081 /*
2082 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2083 * If the number of characters before the cursor decreased, need to pop a
2084 * few characters from the replace stack.
2085 * If the number of characters before the cursor increased, need to push a
2086 * few NULs onto the replace stack.
2087 */
2088 if (REPLACE_NORMAL(State) && start_col >= 0)
2089 {
2090 while (start_col > (int)curwin->w_cursor.col)
2091 {
2092 replace_join(0); /* remove a NUL from the replace stack */
2093 --start_col;
2094 }
2095 while (start_col < (int)curwin->w_cursor.col || replaced)
2096 {
2097 replace_push(NUL);
2098 if (replaced)
2099 {
2100 replace_push(replaced);
2101 replaced = NUL;
2102 }
2103 ++start_col;
2104 }
2105 }
2106
2107#ifdef FEAT_VREPLACE
2108 /*
2109 * For VREPLACE mode, we also have to fix the replace stack. In this case
2110 * it is always possible because we backspace over the whole line and then
2111 * put it back again the way we wanted it.
2112 */
2113 if (State & VREPLACE_FLAG)
2114 {
2115 /* If orig_line didn't allocate, just return. At least we did the job,
2116 * even if you can't backspace. */
2117 if (orig_line == NULL)
2118 return;
2119
2120 /* Save new line */
2121 new_line = vim_strsave(ml_get_curline());
2122 if (new_line == NULL)
2123 return;
2124
2125 /* We only put back the new line up to the cursor */
2126 new_line[curwin->w_cursor.col] = NUL;
2127
2128 /* Put back original line */
2129 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2130 curwin->w_cursor.col = orig_col;
2131
2132 /* Backspace from cursor to start of line */
2133 backspace_until_column(0);
2134
2135 /* Insert new stuff into line again */
2136 ins_bytes(new_line);
2137
2138 vim_free(new_line);
2139 }
2140#endif
2141}
2142
2143/*
2144 * Truncate the space at the end of a line. This is to be used only in an
2145 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2146 * modes.
2147 */
2148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002149truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150{
2151 int i;
2152
2153 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002154 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
2156 if (State & REPLACE_FLAG)
2157 replace_join(0); /* remove a NUL from the replace stack */
2158 }
2159 line[i + 1] = NUL;
2160}
2161
2162#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2163 || defined(FEAT_COMMENTS) || defined(PROTO)
2164/*
2165 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2166 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002167 * Will attempt not to go before "col" even when there is a composing
2168 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 */
2170 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002171backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
2173 while ((int)curwin->w_cursor.col > col)
2174 {
2175 curwin->w_cursor.col--;
2176 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002177 replace_do_bs(col);
2178 else if (!del_char_after_col(col))
2179 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 }
2181}
2182#endif
2183
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002184/*
2185 * Like del_char(), but make sure not to go before column "limit_col".
2186 * Only matters when there are composing characters.
2187 * Return TRUE when something was deleted.
2188 */
2189 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002190del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002191{
2192#ifdef FEAT_MBYTE
2193 if (enc_utf8 && limit_col >= 0)
2194 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002195 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002196
2197 /* Make sure the cursor is at the start of a character, but
2198 * skip forward again when going too far back because of a
2199 * composing character. */
2200 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002201 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002202 {
2203 int l = utf_ptr2len(ml_get_cursor());
2204
2205 if (l == 0) /* end of line */
2206 break;
2207 curwin->w_cursor.col += l;
2208 }
2209 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2210 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002211 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002212 }
2213 else
2214#endif
2215 (void)del_char(FALSE);
2216 return TRUE;
2217}
2218
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2220/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002221 * CTRL-X pressed in Insert mode.
2222 */
2223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002224ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002225{
2226 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2227 * CTRL-V works like CTRL-N */
2228 if (ctrl_x_mode != CTRL_X_CMDLINE)
2229 {
2230 /* if the next ^X<> won't ADD nothing, then reset
2231 * compl_cont_status */
2232 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002233 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002234 else
2235 compl_cont_status = 0;
2236 /* We're not sure which CTRL-X mode it will be yet */
2237 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2238 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2239 edit_submode_pre = NULL;
2240 showmode();
2241 }
2242}
2243
2244/*
2245 * Return TRUE if the 'dict' or 'tsr' option can be used.
2246 */
2247 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002249{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002250 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002251# ifdef FEAT_SPELL
2252 && !curwin->w_p_spell
2253# endif
2254 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002255 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2256 {
2257 ctrl_x_mode = 0;
2258 edit_submode = NULL;
2259 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2260 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002261 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002262 if (emsg_silent == 0)
2263 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002264 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002265 setcursor();
2266 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002267#ifdef FEAT_EVAL
2268 if (!get_vim_var_nr(VV_TESTING))
2269#endif
2270 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002271 }
2272 return FALSE;
2273 }
2274 return TRUE;
2275}
2276
2277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2279 * This depends on the current mode.
2280 */
2281 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002282vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283{
2284 /* Always allow ^R - let it's results then be checked */
2285 if (c == Ctrl_R)
2286 return TRUE;
2287
Bram Moolenaare3226be2005-12-18 22:10:00 +00002288 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002289 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002290 return TRUE;
2291
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 switch (ctrl_x_mode)
2293 {
2294 case 0: /* Not in any CTRL-X mode */
2295 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2296 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002297 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2299 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2300 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002301 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002302 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 case CTRL_X_SCROLL:
2304 return (c == Ctrl_Y || c == Ctrl_E);
2305 case CTRL_X_WHOLE_LINE:
2306 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2307 case CTRL_X_FILES:
2308 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2309 case CTRL_X_DICTIONARY:
2310 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2311 case CTRL_X_THESAURUS:
2312 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2313 case CTRL_X_TAGS:
2314 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2315#ifdef FEAT_FIND_ID
2316 case CTRL_X_PATH_PATTERNS:
2317 return (c == Ctrl_P || c == Ctrl_N);
2318 case CTRL_X_PATH_DEFINES:
2319 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2320#endif
2321 case CTRL_X_CMDLINE:
2322 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2323 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002324#ifdef FEAT_COMPL_FUNC
2325 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002326 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002327 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002328 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002329#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002330 case CTRL_X_SPELL:
2331 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002332 case CTRL_X_EVAL:
2333 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002335 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 return FALSE;
2337}
2338
2339/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002340 * Return TRUE when character "c" is part of the item currently being
2341 * completed. Used to decide whether to abandon complete mode when the menu
2342 * is visible.
2343 */
2344 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002345ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002346{
2347 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2348 /* When expanding an identifier only accept identifier chars. */
2349 return vim_isIDc(c);
2350
2351 switch (ctrl_x_mode)
2352 {
2353 case CTRL_X_FILES:
2354 /* When expanding file name only accept file name chars. But not
2355 * path separators, so that "proto/<Tab>" expands files in
2356 * "proto", not "proto/" as a whole */
2357 return vim_isfilec(c) && !vim_ispathsep(c);
2358
2359 case CTRL_X_CMDLINE:
2360 case CTRL_X_OMNI:
2361 /* Command line and Omni completion can work with just about any
2362 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002363 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002364
2365 case CTRL_X_WHOLE_LINE:
2366 /* For while line completion a space can be part of the line. */
2367 return vim_isprintc(c);
2368 }
2369 return vim_iswordc(c);
2370}
2371
2372/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002373 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002375 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 * the rest of the word to be in -- webb
2377 */
2378 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002379ins_compl_add_infercase(
2380 char_u *str,
2381 int len,
2382 int icase,
2383 char_u *fname,
2384 int dir,
2385 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002387 char_u *p;
2388 int i, c;
2389 int actual_len; /* Take multi-byte characters */
2390 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002391 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002392 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 int has_lower = FALSE;
2394 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002396 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002398 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
Bram Moolenaara2993e12007-08-12 14:38:46 +00002400 /* Find actual length of completion. */
2401#ifdef FEAT_MBYTE
2402 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002404 p = str;
2405 actual_len = 0;
2406 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002408 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002409 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
2411 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002412 else
2413#endif
2414 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
Bram Moolenaara2993e12007-08-12 14:38:46 +00002416 /* Find actual length of original text. */
2417#ifdef FEAT_MBYTE
2418 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002420 p = compl_orig_text;
2421 actual_compl_length = 0;
2422 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002424 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002425 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 }
2427 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002428 else
2429#endif
2430 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002432 /* "actual_len" may be smaller than "actual_compl_length" when using
2433 * thesaurus, only use the minimum when comparing. */
2434 min_len = actual_len < actual_compl_length
2435 ? actual_len : actual_compl_length;
2436
Bram Moolenaara2993e12007-08-12 14:38:46 +00002437 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002438 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002439 if (wca != NULL)
2440 {
2441 p = str;
2442 for (i = 0; i < actual_len; ++i)
2443#ifdef FEAT_MBYTE
2444 if (has_mbyte)
2445 wca[i] = mb_ptr2char_adv(&p);
2446 else
2447#endif
2448 wca[i] = *(p++);
2449
2450 /* Rule 1: Were any chars converted to lower? */
2451 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002452 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002453 {
2454#ifdef FEAT_MBYTE
2455 if (has_mbyte)
2456 c = mb_ptr2char_adv(&p);
2457 else
2458#endif
2459 c = *(p++);
2460 if (MB_ISLOWER(c))
2461 {
2462 has_lower = TRUE;
2463 if (MB_ISUPPER(wca[i]))
2464 {
2465 /* Rule 1 is satisfied. */
2466 for (i = actual_compl_length; i < actual_len; ++i)
2467 wca[i] = MB_TOLOWER(wca[i]);
2468 break;
2469 }
2470 }
2471 }
2472
2473 /*
2474 * Rule 2: No lower case, 2nd consecutive letter converted to
2475 * upper case.
2476 */
2477 if (!has_lower)
2478 {
2479 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002480 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002481 {
2482#ifdef FEAT_MBYTE
2483 if (has_mbyte)
2484 c = mb_ptr2char_adv(&p);
2485 else
2486#endif
2487 c = *(p++);
2488 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2489 {
2490 /* Rule 2 is satisfied. */
2491 for (i = actual_compl_length; i < actual_len; ++i)
2492 wca[i] = MB_TOUPPER(wca[i]);
2493 break;
2494 }
2495 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2496 }
2497 }
2498
2499 /* Copy the original case of the part we typed. */
2500 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002501 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002502 {
2503#ifdef FEAT_MBYTE
2504 if (has_mbyte)
2505 c = mb_ptr2char_adv(&p);
2506 else
2507#endif
2508 c = *(p++);
2509 if (MB_ISLOWER(c))
2510 wca[i] = MB_TOLOWER(wca[i]);
2511 else if (MB_ISUPPER(c))
2512 wca[i] = MB_TOUPPER(wca[i]);
2513 }
2514
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002515 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002516 * Generate encoding specific output from wide character array.
2517 * Multi-byte characters can occupy up to five bytes more than
2518 * ASCII characters, and we also need one byte for NUL, so stay
2519 * six bytes away from the edge of IObuff.
2520 */
2521 p = IObuff;
2522 i = 0;
2523 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2524#ifdef FEAT_MBYTE
2525 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002526 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002527 else
2528#endif
2529 *(p++) = wca[i++];
2530 *p = NUL;
2531
2532 vim_free(wca);
2533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002535 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2536 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002538 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539}
2540
2541/*
2542 * Add a match to the list of matches.
2543 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002544 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002545 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002547 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002548ins_compl_add(
2549 char_u *str,
2550 int len,
2551 int icase,
2552 char_u *fname,
2553 char_u **cptext, /* extra text for popup menu or NULL */
2554 int cdir,
2555 int flags,
2556 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002558 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002559 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
2561 ui_breakcheck();
2562 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002563 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 if (len < 0)
2565 len = (int)STRLEN(str);
2566
2567 /*
2568 * If the same match is already present, don't add it.
2569 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002570 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002572 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 do
2574 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002575 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002576 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002577 && match->cp_str[len] == NUL)
2578 return NOTDONE;
2579 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002580 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002583 /* Remove any popup menu before changing the list of matches. */
2584 ins_compl_del_pum();
2585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 /*
2587 * Allocate a new match structure.
2588 * Copy the values to the new match structure.
2589 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002590 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002592 return FAIL;
2593 match->cp_number = -1;
2594 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002595 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002596 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 {
2598 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002601 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002602
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002604 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2606 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002607 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002608 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002609 && compl_curr_match->cp_fname != NULL
2610 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002611 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002612 else if (fname != NULL)
2613 {
2614 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002615 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002618 match->cp_fname = NULL;
2619 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002620
2621 if (cptext != NULL)
2622 {
2623 int i;
2624
2625 for (i = 0; i < CPT_COUNT; ++i)
2626 if (cptext[i] != NULL && *cptext[i] != NUL)
2627 match->cp_text[i] = vim_strsave(cptext[i]);
2628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629
2630 /*
2631 * Link the new match structure in the list of matches.
2632 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002633 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002634 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 else if (dir == FORWARD)
2636 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002637 match->cp_next = compl_curr_match->cp_next;
2638 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
2640 else /* BACKWARD */
2641 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002642 match->cp_next = compl_curr_match;
2643 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002645 if (match->cp_next)
2646 match->cp_next->cp_prev = match;
2647 if (match->cp_prev)
2648 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002650 compl_first_match = match;
2651 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002653 /*
2654 * Find the longest common string if still doing that.
2655 */
2656 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2657 ins_compl_longest_match(match);
2658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 return OK;
2660}
2661
2662/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002663 * Return TRUE if "str[len]" matches with match->cp_str, considering
2664 * match->cp_icase.
2665 */
2666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002667ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002668{
2669 if (match->cp_icase)
2670 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2671 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2672}
2673
2674/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002675 * Reduce the longest common string for match "match".
2676 */
2677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002678ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002679{
2680 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002681 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002682 int had_match;
2683
2684 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002685 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002686 /* First match, use it as a whole. */
2687 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002688 if (compl_leader != NULL)
2689 {
2690 had_match = (curwin->w_cursor.col > compl_col);
2691 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002692 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002693 ins_redraw(FALSE);
2694
2695 /* When the match isn't there (to avoid matching itself) remove it
2696 * again after redrawing. */
2697 if (!had_match)
2698 ins_compl_delete();
2699 compl_used_match = FALSE;
2700 }
2701 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002702 else
2703 {
2704 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002705 p = compl_leader;
2706 s = match->cp_str;
2707 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002708 {
2709#ifdef FEAT_MBYTE
2710 if (has_mbyte)
2711 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002712 c1 = mb_ptr2char(p);
2713 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002714 }
2715 else
2716#endif
2717 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002718 c1 = *p;
2719 c2 = *s;
2720 }
2721 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2722 : (c1 != c2))
2723 break;
2724#ifdef FEAT_MBYTE
2725 if (has_mbyte)
2726 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002727 MB_PTR_ADV(p);
2728 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002729 }
2730 else
2731#endif
2732 {
2733 ++p;
2734 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002735 }
2736 }
2737
2738 if (*p != NUL)
2739 {
2740 /* Leader was shortened, need to change the inserted text. */
2741 *p = NUL;
2742 had_match = (curwin->w_cursor.col > compl_col);
2743 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002744 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002745 ins_redraw(FALSE);
2746
2747 /* When the match isn't there (to avoid matching itself) remove it
2748 * again after redrawing. */
2749 if (!had_match)
2750 ins_compl_delete();
2751 }
2752
2753 compl_used_match = FALSE;
2754 }
2755}
2756
2757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 * Add an array of matches to the list of matches.
2759 * Frees matches[].
2760 */
2761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002762ins_compl_add_matches(
2763 int num_matches,
2764 char_u **matches,
2765 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
2767 int i;
2768 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002769 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
Bram Moolenaar572cb562005-08-05 21:35:02 +00002771 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002772 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002773 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002775 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 FreeWild(num_matches, matches);
2777}
2778
2779/* Make the completion list cyclic.
2780 * Return the number of matches (excluding the original).
2781 */
2782 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002783ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002785 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 int count = 0;
2787
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002788 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 {
2790 /*
2791 * Find the end of the list.
2792 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002793 match = compl_first_match;
2794 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002795 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002797 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 ++count;
2799 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002800 match->cp_next = compl_first_match;
2801 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 }
2803 return count;
2804}
2805
Bram Moolenaara94bc432006-03-10 21:42:59 +00002806/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002807 * Set variables that store noselect and noinsert behavior from the
2808 * 'completeopt' value.
2809 */
2810 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002811completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002812{
2813 compl_no_insert = FALSE;
2814 compl_no_select = FALSE;
2815 if (strstr((char *)p_cot, "noselect") != NULL)
2816 compl_no_select = TRUE;
2817 if (strstr((char *)p_cot, "noinsert") != NULL)
2818 compl_no_insert = TRUE;
2819}
2820
2821/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002822 * Start completion for the complete() function.
2823 * "startcol" is where the matched text starts (1 is first column).
2824 * "list" is the list of matches.
2825 */
2826 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002827set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002828{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002829 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002830 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002831
Bram Moolenaara94bc432006-03-10 21:42:59 +00002832 /* If already doing completions stop it. */
2833 if (ctrl_x_mode != 0)
2834 ins_compl_prep(' ');
2835 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002836 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002837
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002838 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002839 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002840 startcol = curwin->w_cursor.col;
2841 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002842 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002843 /* compl_pattern doesn't need to be set */
2844 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2845 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002846 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002847 return;
2848
Bram Moolenaare4214502015-03-05 18:08:43 +01002849 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002850
2851 ins_compl_add_list(list);
2852 compl_matches = ins_compl_make_cyclic();
2853 compl_started = TRUE;
2854 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002855 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002856
2857 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002858 if (compl_no_insert || compl_no_select)
2859 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002860 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002861 if (compl_no_select)
2862 /* Down/Up has no real effect. */
2863 ins_complete(K_UP, FALSE);
2864 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002865 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002866 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002867 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002868
2869 /* Lazily show the popup menu, unless we got interrupted. */
2870 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002871 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002872 out_flush();
2873}
2874
2875
Bram Moolenaar9372a112005-12-06 19:59:18 +00002876/* "compl_match_array" points the currently displayed list of entries in the
2877 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002878static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002879static int compl_match_arraysize;
2880
2881/*
2882 * Update the screen and when there is any scrolling remove the popup menu.
2883 */
2884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002885ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002886{
2887 int h;
2888
2889 if (compl_match_array != NULL)
2890 {
2891 h = curwin->w_cline_height;
2892 update_screen(0);
2893 if (h != curwin->w_cline_height)
2894 ins_compl_del_pum();
2895 }
2896}
2897
2898/*
2899 * Remove any popup menu.
2900 */
2901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002902ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002903{
2904 if (compl_match_array != NULL)
2905 {
2906 pum_undisplay();
2907 vim_free(compl_match_array);
2908 compl_match_array = NULL;
2909 }
2910}
2911
2912/*
2913 * Return TRUE if the popup menu should be displayed.
2914 */
2915 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002916pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002917{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002918 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002919 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002920 return FALSE;
2921
2922 /* The display looks bad on a B&W display. */
2923 if (t_colors < 8
2924#ifdef FEAT_GUI
2925 && !gui.in_use
2926#endif
2927 )
2928 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002929 return TRUE;
2930}
2931
2932/*
2933 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002934 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002935 */
2936 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002937pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002938{
2939 compl_T *compl;
2940 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002941
2942 /* Don't display the popup menu if there are no matches or there is only
2943 * one (ignoring the original text). */
2944 compl = compl_first_match;
2945 i = 0;
2946 do
2947 {
2948 if (compl == NULL
2949 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2950 break;
2951 compl = compl->cp_next;
2952 } while (compl != compl_first_match);
2953
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002954 if (strstr((char *)p_cot, "menuone") != NULL)
2955 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002956 return (i >= 2);
2957}
2958
2959/*
2960 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002961 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002962 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002963 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002964ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002965{
2966 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002967 compl_T *shown_compl = NULL;
2968 int did_find_shown_match = FALSE;
2969 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002970 int i;
2971 int cur = -1;
2972 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002973 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002974
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002975 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002976 return;
2977
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002978#if defined(FEAT_EVAL)
2979 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2980 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2981#endif
2982
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002983 /* Update the screen before drawing the popup menu over it. */
2984 update_screen(0);
2985
2986 if (compl_match_array == NULL)
2987 {
2988 /* Need to build the popup menu list. */
2989 compl_match_arraysize = 0;
2990 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002991 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002992 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002993 do
2994 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002995 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2996 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002997 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002998 ++compl_match_arraysize;
2999 compl = compl->cp_next;
3000 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003001 if (compl_match_arraysize == 0)
3002 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003003 compl_match_array = (pumitem_T *)alloc_clear(
3004 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003005 * compl_match_arraysize));
3006 if (compl_match_array != NULL)
3007 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003008 /* If the current match is the original text don't find the first
3009 * match after it, don't highlight anything. */
3010 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3011 shown_match_ok = TRUE;
3012
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003013 i = 0;
3014 compl = compl_first_match;
3015 do
3016 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003017 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3018 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003019 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003020 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003021 if (!shown_match_ok)
3022 {
3023 if (compl == compl_shown_match || did_find_shown_match)
3024 {
3025 /* This item is the shown match or this is the
3026 * first displayed item after the shown match. */
3027 compl_shown_match = compl;
3028 did_find_shown_match = TRUE;
3029 shown_match_ok = TRUE;
3030 }
3031 else
3032 /* Remember this displayed match for when the
3033 * shown match is just below it. */
3034 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003035 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003036 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003037
3038 if (compl->cp_text[CPT_ABBR] != NULL)
3039 compl_match_array[i].pum_text =
3040 compl->cp_text[CPT_ABBR];
3041 else
3042 compl_match_array[i].pum_text = compl->cp_str;
3043 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3044 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3045 if (compl->cp_text[CPT_MENU] != NULL)
3046 compl_match_array[i++].pum_extra =
3047 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003048 else
3049 compl_match_array[i++].pum_extra = compl->cp_fname;
3050 }
3051
3052 if (compl == compl_shown_match)
3053 {
3054 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003055
3056 /* When the original text is the shown match don't set
3057 * compl_shown_match. */
3058 if (compl->cp_flags & ORIGINAL_TEXT)
3059 shown_match_ok = TRUE;
3060
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003061 if (!shown_match_ok && shown_compl != NULL)
3062 {
3063 /* The shown match isn't displayed, set it to the
3064 * previously displayed match. */
3065 compl_shown_match = shown_compl;
3066 shown_match_ok = TRUE;
3067 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003068 }
3069 compl = compl->cp_next;
3070 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003071
3072 if (!shown_match_ok) /* no displayed match at all */
3073 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003074 }
3075 }
3076 else
3077 {
3078 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003079 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003080 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3081 || compl_match_array[i].pum_text
3082 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003083 {
3084 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003085 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003086 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003087 }
3088
3089 if (compl_match_array != NULL)
3090 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003091 /* In Replace mode when a $ is displayed at the end of the line only
3092 * part of the screen would be updated. We do need to redraw here. */
3093 dollar_vcol = -1;
3094
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003095 /* Compute the screen column of the start of the completed text.
3096 * Use the cursor to get all wrapping and other settings right. */
3097 col = curwin->w_cursor.col;
3098 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003099 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003100 curwin->w_cursor.col = col;
3101 }
3102}
3103
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104#define DICT_FIRST (1) /* use just first element in "dict" */
3105#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003106
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003108 * Add any identifiers that match the given pattern in the list of dictionary
3109 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 */
3111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003112ins_compl_dictionaries(
3113 char_u *dict_start,
3114 char_u *pat,
3115 int flags, /* DICT_FIRST and/or DICT_EXACT */
3116 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003118 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 char_u *ptr;
3120 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 char_u **files;
3123 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003125 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
Bram Moolenaar0b238792006-03-02 22:49:12 +00003127 if (*dict == NUL)
3128 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003129#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003130 /* When 'dictionary' is empty and spell checking is enabled use
3131 * "spell". */
3132 if (!thesaurus && curwin->w_p_spell)
3133 dict = (char_u *)"spell";
3134 else
3135#endif
3136 return;
3137 }
3138
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003140 if (buf == NULL)
3141 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003142 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 /* If 'infercase' is set, don't use 'smartcase' here */
3145 save_p_scs = p_scs;
3146 if (curbuf->b_p_inf)
3147 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003148
3149 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3150 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003151 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003152 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003153 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003154 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003155 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003156
3157 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003158 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003159 len = STRLEN(pat_esc) + 10;
3160 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003161 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003162 {
3163 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003164 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003165 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003166 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003167 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3168 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003169 vim_free(ptr);
3170 }
3171 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003172 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003173 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003174 if (regmatch.regprog == NULL)
3175 goto theend;
3176 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003177
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3179 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003180 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
3182 /* copy one dictionary file name into buf */
3183 if (flags == DICT_EXACT)
3184 {
3185 count = 1;
3186 files = &dict;
3187 }
3188 else
3189 {
3190 /* Expand wildcards in the dictionary name, but do not allow
3191 * backticks (for security, the 'dict' option may have been set in
3192 * a modeline). */
3193 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003194# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003195 if (!thesaurus && STRCMP(buf, "spell") == 0)
3196 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003197 else
3198# endif
3199 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 || expand_wildcards(1, &buf, &count, &files,
3201 EW_FILE|EW_SILENT) != OK)
3202 count = 0;
3203 }
3204
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003205# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003206 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003208 /* Complete from active spelling. Skip "\<" in the pattern, we
3209 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003210 if (pat[0] == '\\' && pat[1] == '<')
3211 ptr = pat + 2;
3212 else
3213 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003214 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003216 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003217# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003218 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003219 {
3220 ins_compl_files(count, files, thesaurus, flags,
3221 &regmatch, buf, &dir);
3222 if (flags != DICT_EXACT)
3223 FreeWild(count, files);
3224 }
3225 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 break;
3227 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003228
3229theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003231 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 vim_free(buf);
3233}
3234
Bram Moolenaar0b238792006-03-02 22:49:12 +00003235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003236ins_compl_files(
3237 int count,
3238 char_u **files,
3239 int thesaurus,
3240 int flags,
3241 regmatch_T *regmatch,
3242 char_u *buf,
3243 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003244{
3245 char_u *ptr;
3246 int i;
3247 FILE *fp;
3248 int add_r;
3249
3250 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3251 {
3252 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3253 if (flags != DICT_EXACT)
3254 {
3255 vim_snprintf((char *)IObuff, IOSIZE,
3256 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003257 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003258 }
3259
3260 if (fp != NULL)
3261 {
3262 /*
3263 * Read dictionary file line by line.
3264 * Check each line for a match.
3265 */
3266 while (!got_int && !compl_interrupted
3267 && !vim_fgets(buf, LSIZE, fp))
3268 {
3269 ptr = buf;
3270 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3271 {
3272 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003273 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003274 ptr = find_line_end(ptr);
3275 else
3276 ptr = find_word_end(ptr);
3277 add_r = ins_compl_add_infercase(regmatch->startp[0],
3278 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003279 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003280 if (thesaurus)
3281 {
3282 char_u *wstart;
3283
3284 /*
3285 * Add the other matches on the line
3286 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003287 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003288 while (!got_int)
3289 {
3290 /* Find start of the next word. Skip white
3291 * space and punctuation. */
3292 ptr = find_word_start(ptr);
3293 if (*ptr == NUL || *ptr == NL)
3294 break;
3295 wstart = ptr;
3296
Bram Moolenaara2993e12007-08-12 14:38:46 +00003297 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003298#ifdef FEAT_MBYTE
3299 if (has_mbyte)
3300 /* Japanese words may have characters in
3301 * different classes, only separate words
3302 * with single-byte non-word characters. */
3303 while (*ptr != NUL)
3304 {
3305 int l = (*mb_ptr2len)(ptr);
3306
3307 if (l < 2 && !vim_iswordc(*ptr))
3308 break;
3309 ptr += l;
3310 }
3311 else
3312#endif
3313 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003314
3315 /* Add the word. Skip the regexp match. */
3316 if (wstart != regmatch->startp[0])
3317 add_r = ins_compl_add_infercase(wstart,
3318 (int)(ptr - wstart),
3319 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003320 }
3321 }
3322 if (add_r == OK)
3323 /* if dir was BACKWARD then honor it just once */
3324 *dir = FORWARD;
3325 else if (add_r == FAIL)
3326 break;
3327 /* avoid expensive call to vim_regexec() when at end
3328 * of line */
3329 if (*ptr == '\n' || got_int)
3330 break;
3331 }
3332 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003333 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003334 }
3335 fclose(fp);
3336 }
3337 }
3338}
3339
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340/*
3341 * Find the start of the next word.
3342 * Returns a pointer to the first char of the word. Also stops at a NUL.
3343 */
3344 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003345find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347#ifdef FEAT_MBYTE
3348 if (has_mbyte)
3349 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003350 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 else
3352#endif
3353 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3354 ++ptr;
3355 return ptr;
3356}
3357
3358/*
3359 * Find the end of the word. Assumes it starts inside a word.
3360 * Returns a pointer to just after the word.
3361 */
3362 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003363find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365#ifdef FEAT_MBYTE
3366 int start_class;
3367
3368 if (has_mbyte)
3369 {
3370 start_class = mb_get_class(ptr);
3371 if (start_class > 1)
3372 while (*ptr != NUL)
3373 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003374 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (mb_get_class(ptr) != start_class)
3376 break;
3377 }
3378 }
3379 else
3380#endif
3381 while (vim_iswordc(*ptr))
3382 ++ptr;
3383 return ptr;
3384}
3385
3386/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003387 * Find the end of the line, omitting CR and NL at the end.
3388 * Returns a pointer to just after the line.
3389 */
3390 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003391find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003392{
3393 char_u *s;
3394
3395 s = ptr + STRLEN(ptr);
3396 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3397 --s;
3398 return s;
3399}
3400
3401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 * Free the list of completions
3403 */
3404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003405ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003407 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003408 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003410 vim_free(compl_pattern);
3411 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003412 vim_free(compl_leader);
3413 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003415 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003417
3418 ins_compl_del_pum();
3419 pum_clear();
3420
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003421 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 do
3423 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003424 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003425 compl_curr_match = compl_curr_match->cp_next;
3426 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003428 if (match->cp_flags & FREE_FNAME)
3429 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003430 for (i = 0; i < CPT_COUNT; ++i)
3431 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003433 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3434 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003435 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003436 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437}
3438
3439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003440ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003442 compl_cont_status = 0;
3443 compl_started = FALSE;
3444 compl_matches = 0;
3445 vim_free(compl_pattern);
3446 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003447 vim_free(compl_leader);
3448 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003450 vim_free(compl_orig_text);
3451 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003452 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003453 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003454 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455}
3456
3457/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003458 * Return TRUE when Insert completion is active.
3459 */
3460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003461ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003462{
3463 return compl_started;
3464}
3465
3466/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003467 * Delete one character before the cursor and show the subset of the matches
3468 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003469 * Returns the character to be used, NUL if the work is done and another char
3470 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003471 */
3472 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003473ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003474{
3475 char_u *line;
3476 char_u *p;
3477
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003478 line = ml_get_curline();
3479 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003480 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003481
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003482 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003483 * allow the word to be deleted, we won't match everything.
3484 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003485 if ((int)(p - line) - (int)compl_col < 0
3486 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003487 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3488 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3489 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003490 return K_BS;
3491
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003492 /* Deleted more than what was used to find matches or didn't finish
3493 * finding all matches: need to look for matches all over again. */
3494 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003495 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003496 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003497
Bram Moolenaara6557602006-02-04 22:43:20 +00003498 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003499 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003500 if (compl_leader != NULL)
3501 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003502 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003503 if (compl_shown_match != NULL)
3504 /* Make sure current match is not a hidden item. */
3505 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003506 return NUL;
3507 }
3508 return K_BS;
3509}
Bram Moolenaara6557602006-02-04 22:43:20 +00003510
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003511/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003512 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3513 * be called.
3514 */
3515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003516ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003517{
3518 /* Return TRUE if we didn't complete finding matches or when the
3519 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3520 return compl_was_interrupted
3521 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3522 && compl_opt_refresh_always);
3523}
3524
3525/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003526 * Called after changing "compl_leader".
3527 * Show the popup menu with a different set of matches.
3528 * May also search for matches again if the previous search was interrupted.
3529 */
3530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003531ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003532{
3533 ins_compl_del_pum();
3534 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003535 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003536 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003537
3538 if (compl_started)
3539 ins_compl_set_original_text(compl_leader);
3540 else
3541 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003542#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003543 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003544#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003545 /*
3546 * Matches were cleared, need to search for them now. First display
3547 * the changed text before the cursor. Set "compl_restarting" to
3548 * avoid that the first match is inserted.
3549 */
3550 update_screen(0);
3551#ifdef FEAT_GUI
3552 if (gui.in_use)
3553 {
3554 /* Show the cursor after the match, not after the redrawn text. */
3555 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003556 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003557 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003558#endif
3559 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003560 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003561 compl_cont_status = 0;
3562 compl_restarting = FALSE;
3563 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003564
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003565 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003566
3567 /* Show the popup menu with a different set of matches. */
3568 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003569
3570 /* Don't let Enter select the original text when there is no popup menu. */
3571 if (compl_match_array == NULL)
3572 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003573}
3574
3575/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003576 * Return the length of the completion, from the completion start column to
3577 * the cursor column. Making sure it never goes below zero.
3578 */
3579 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003580ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003581{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003582 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003583
3584 if (off < 0)
3585 return 0;
3586 return off;
3587}
3588
3589/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003590 * Append one character to the match leader. May reduce the number of
3591 * matches.
3592 */
3593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003594ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003595{
3596#ifdef FEAT_MBYTE
3597 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003598#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003599
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003600 if (stop_arrow() == FAIL)
3601 return;
3602#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003603 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3604 {
3605 char_u buf[MB_MAXBYTES + 1];
3606
3607 (*mb_char2bytes)(c, buf);
3608 buf[cc] = NUL;
3609 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003610 if (compl_opt_refresh_always)
3611 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003612 }
3613 else
3614#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003615 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003616 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003617 if (compl_opt_refresh_always)
3618 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003619 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003620
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003621 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003622 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003623 ins_compl_restart();
3624
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003625 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003626 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003627 * break redo. */
3628 if (!compl_opt_refresh_always)
3629 {
3630 vim_free(compl_leader);
3631 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003632 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003633 if (compl_leader != NULL)
3634 ins_compl_new_leader();
3635 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003636}
3637
3638/*
3639 * Setup for finding completions again without leaving CTRL-X mode. Used when
3640 * BS or a key was typed while still searching for matches.
3641 */
3642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003643ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003644{
3645 ins_compl_free();
3646 compl_started = FALSE;
3647 compl_matches = 0;
3648 compl_cont_status = 0;
3649 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003650}
3651
3652/*
3653 * Set the first match, the original text.
3654 */
3655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003656ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003657{
3658 char_u *p;
3659
3660 /* Replace the original text entry. */
3661 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3662 {
3663 p = vim_strsave(str);
3664 if (p != NULL)
3665 {
3666 vim_free(compl_first_match->cp_str);
3667 compl_first_match->cp_str = p;
3668 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003669 }
3670}
3671
3672/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003673 * Append one character to the match leader. May reduce the number of
3674 * matches.
3675 */
3676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003677ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003678{
3679 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003680 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003681 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003682 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003683
3684 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003685 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003686 {
3687 /* When still at the original match use the first entry that matches
3688 * the leader. */
3689 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3690 {
3691 p = NULL;
3692 for (cp = compl_shown_match->cp_next; cp != NULL
3693 && cp != compl_first_match; cp = cp->cp_next)
3694 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003695 if (compl_leader == NULL
3696 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003697 (int)STRLEN(compl_leader)))
3698 {
3699 p = cp->cp_str;
3700 break;
3701 }
3702 }
3703 if (p == NULL || (int)STRLEN(p) <= len)
3704 return;
3705 }
3706 else
3707 return;
3708 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003709 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003710 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003711 ins_compl_addleader(c);
3712}
3713
3714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003716 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003717 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003719 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003720ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
3722 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003724 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
3726 /* Forget any previous 'special' messages if this is actually
3727 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3728 */
3729 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3730 edit_submode_extra = NULL;
3731
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003732 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003733 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3734 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003735 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003737 /* Set "compl_get_longest" when finding the first matches. */
3738 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3739 || (ctrl_x_mode == 0 && !compl_started))
3740 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003741 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003742 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003743
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003744 }
3745
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3747 {
3748 /*
3749 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3750 * it will be yet. Now we decide.
3751 */
3752 switch (c)
3753 {
3754 case Ctrl_E:
3755 case Ctrl_Y:
3756 ctrl_x_mode = CTRL_X_SCROLL;
3757 if (!(State & REPLACE_FLAG))
3758 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3759 else
3760 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3761 edit_submode_pre = NULL;
3762 showmode();
3763 break;
3764 case Ctrl_L:
3765 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3766 break;
3767 case Ctrl_F:
3768 ctrl_x_mode = CTRL_X_FILES;
3769 break;
3770 case Ctrl_K:
3771 ctrl_x_mode = CTRL_X_DICTIONARY;
3772 break;
3773 case Ctrl_R:
3774 /* Simply allow ^R to happen without affecting ^X mode */
3775 break;
3776 case Ctrl_T:
3777 ctrl_x_mode = CTRL_X_THESAURUS;
3778 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003779#ifdef FEAT_COMPL_FUNC
3780 case Ctrl_U:
3781 ctrl_x_mode = CTRL_X_FUNCTION;
3782 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003783 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003784 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003785 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003786#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003787 case 's':
3788 case Ctrl_S:
3789 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003790#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003791 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003792 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003793 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003794#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003795 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 case Ctrl_RSB:
3797 ctrl_x_mode = CTRL_X_TAGS;
3798 break;
3799#ifdef FEAT_FIND_ID
3800 case Ctrl_I:
3801 case K_S_TAB:
3802 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3803 break;
3804 case Ctrl_D:
3805 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3806 break;
3807#endif
3808 case Ctrl_V:
3809 case Ctrl_Q:
3810 ctrl_x_mode = CTRL_X_CMDLINE;
3811 break;
3812 case Ctrl_P:
3813 case Ctrl_N:
3814 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3815 * just started ^X mode, or there were enough ^X's to cancel
3816 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3817 * do normal expansion when interrupting a different mode (say
3818 * ^X^F^X^P or ^P^X^X^P, see below)
3819 * nothing changes if interrupting mode 0, (eg, the flag
3820 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003821 if (!(compl_cont_status & CONT_INTRPT))
3822 compl_cont_status |= CONT_LOCAL;
3823 else if (compl_cont_mode != 0)
3824 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 /* FALLTHROUGH */
3826 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003827 /* If we have typed at least 2 ^X's... for modes != 0, we set
3828 * compl_cont_status = 0 (eg, as if we had just started ^X
3829 * mode).
3830 * For mode 0, we set "compl_cont_mode" to an impossible
3831 * value, in both cases ^X^X can be used to restart the same
3832 * mode (avoiding ADDING mode).
3833 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3834 * 'complete' and local ^P expansions respectively.
3835 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3836 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 if (c == Ctrl_X)
3838 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003839 if (compl_cont_mode != 0)
3840 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003842 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 }
3844 ctrl_x_mode = 0;
3845 edit_submode = NULL;
3846 showmode();
3847 break;
3848 }
3849 }
3850 else if (ctrl_x_mode != 0)
3851 {
3852 /* We're already in CTRL-X mode, do we stay in it? */
3853 if (!vim_is_ctrl_x_key(c))
3854 {
3855 if (ctrl_x_mode == CTRL_X_SCROLL)
3856 ctrl_x_mode = 0;
3857 else
3858 ctrl_x_mode = CTRL_X_FINISHED;
3859 edit_submode = NULL;
3860 }
3861 showmode();
3862 }
3863
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003864 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
3866 /* Show error message from attempted keyword completion (probably
3867 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003868 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003870 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3871 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 || ctrl_x_mode == CTRL_X_FINISHED)
3873 {
3874 /* Get here when we have finished typing a sequence of ^N and
3875 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003876 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003877 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
3879 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003880 * If any of the original typed text has been changed, eg when
3881 * ignorecase is set, we must add back-spaces to the redo
3882 * buffer. We add as few as necessary to delete just the part
3883 * of the original text that has changed.
3884 * When using the longest match, edited the match or used
3885 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003887 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3888 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003889 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003890 ptr = NULL;
3891 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893
3894#ifdef FEAT_CINDENT
3895 want_cindent = (can_cindent && cindent_on());
3896#endif
3897 /*
3898 * When completing whole lines: fix indent for 'cindent'.
3899 * Otherwise, break line if it's too long.
3900 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003901 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 {
3903#ifdef FEAT_CINDENT
3904 /* re-indent the current line */
3905 if (want_cindent)
3906 {
3907 do_c_expr_indent();
3908 want_cindent = FALSE; /* don't do it again */
3909 }
3910#endif
3911 }
3912 else
3913 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003914 int prev_col = curwin->w_cursor.col;
3915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003917 if (prev_col > 0)
3918 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003919 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003920 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003922 if (prev_col > 0
3923 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3924 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
3926
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003927 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003928 * the selection without inserting anything. When
3929 * compl_enter_selects is set the Enter key does the same. */
3930 if ((c == Ctrl_Y || (compl_enter_selects
3931 && (c == CAR || c == K_KENTER || c == NL)))
3932 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003933 retval = TRUE;
3934
Bram Moolenaar47247282016-08-02 22:36:02 +02003935 /* CTRL-E means completion is Ended, go back to the typed text.
3936 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003937 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003938 {
3939 ins_compl_delete();
3940 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003941 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003942 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003943 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003944 retval = TRUE;
3945 }
3946
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003947 auto_format(FALSE, TRUE);
3948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003950 compl_started = FALSE;
3951 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003952 if (!shortmess(SHM_COMPLETIONMENU))
3953 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003955 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (edit_submode != NULL)
3957 {
3958 edit_submode = NULL;
3959 showmode();
3960 }
3961
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003962#ifdef FEAT_CMDWIN
3963 if (c == Ctrl_C && cmdwin_type != 0)
3964 /* Avoid the popup menu remains displayed when leaving the
3965 * command line window. */
3966 update_screen(0);
3967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968#ifdef FEAT_CINDENT
3969 /*
3970 * Indent now if a key was typed that is in 'cinkeys'.
3971 */
3972 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3973 do_c_expr_indent();
3974#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003975#ifdef FEAT_AUTOCMD
3976 /* Trigger the CompleteDone event to give scripts a chance to act
3977 * upon the completion. */
3978 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003982#ifdef FEAT_AUTOCMD
3983 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3984 /* Trigger the CompleteDone event to give scripts a chance to act
3985 * upon the (possibly failed) completion. */
3986 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988
3989 /* reset continue_* if we left expansion-mode, if we stay they'll be
3990 * (re)set properly in ins_complete() */
3991 if (!vim_is_ctrl_x_key(c))
3992 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003993 compl_cont_status = 0;
3994 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003996
3997 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998}
3999
4000/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004001 * Fix the redo buffer for the completion leader replacing some of the typed
4002 * text. This inserts backspaces and appends the changed text.
4003 * "ptr" is the known leader text or NUL.
4004 */
4005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004006ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004007{
4008 int len;
4009 char_u *p;
4010 char_u *ptr = ptr_arg;
4011
4012 if (ptr == NULL)
4013 {
4014 if (compl_leader != NULL)
4015 ptr = compl_leader;
4016 else
4017 return; /* nothing to do */
4018 }
4019 if (compl_orig_text != NULL)
4020 {
4021 p = compl_orig_text;
4022 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4023 ;
4024#ifdef FEAT_MBYTE
4025 if (len > 0)
4026 len -= (*mb_head_off)(p, p + len);
4027#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004028 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004029 AppendCharToRedobuff(K_BS);
4030 }
4031 else
4032 len = 0;
4033 if (ptr != NULL)
4034 AppendToRedobuffLit(ptr + len, -1);
4035}
4036
4037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4039 * (depending on flag) starting from buf and looking for a non-scanned
4040 * buffer (other than curbuf). curbuf is special, if it is called with
4041 * buf=curbuf then it has to be the first call for a given flag/expansion.
4042 *
4043 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4044 */
4045 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004046ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
4050 if (flag == 'w') /* just windows */
4051 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 if (buf == curbuf) /* first call for this flag/expansion */
4053 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004054 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 && wp->w_buffer->b_scanned)
4056 ;
4057 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
4059 else
4060 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4061 * (unlisted buffers)
4062 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004063 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 && ((flag == 'U'
4065 ? buf->b_p_bl
4066 : (!buf->b_p_bl
4067 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004068 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 ;
4070 return buf;
4071}
4072
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004073#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004074/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004075 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004076 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004077 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004079expand_by_function(
4080 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4081 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004082{
Bram Moolenaar82139082011-09-14 16:52:09 +02004083 list_T *matchlist = NULL;
4084 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004085 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004086 char_u *funcname;
4087 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004088 win_T *curwin_save;
4089 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004090 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004091
Bram Moolenaare344bea2005-09-01 20:46:49 +00004092 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4093 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004094 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004095
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004096 /* Call 'completefunc' to obtain the list of matches. */
4097 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004098 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004099
Bram Moolenaare344bea2005-09-01 20:46:49 +00004100 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004101 curwin_save = curwin;
4102 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004103
4104 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004105 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004106 {
4107 switch (rettv.v_type)
4108 {
4109 case VAR_LIST:
4110 matchlist = rettv.vval.v_list;
4111 break;
4112 case VAR_DICT:
4113 matchdict = rettv.vval.v_dict;
4114 break;
4115 default:
4116 /* TODO: Give error message? */
4117 clear_tv(&rettv);
4118 break;
4119 }
4120 }
4121
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004122 if (curwin_save != curwin || curbuf_save != curbuf)
4123 {
4124 EMSG(_(e_complwin));
4125 goto theend;
4126 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004127 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004128 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004129 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004130 {
4131 EMSG(_(e_compldel));
4132 goto theend;
4133 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004134
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004135 if (matchlist != NULL)
4136 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004137 else if (matchdict != NULL)
4138 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004139
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004140theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004141 if (matchdict != NULL)
4142 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004143 if (matchlist != NULL)
4144 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004145}
4146#endif /* FEAT_COMPL_FUNC */
4147
Bram Moolenaar39f05632006-03-19 22:15:26 +00004148#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004149/*
4150 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004151 */
4152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004153ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004154{
4155 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004156 int dir = compl_direction;
4157
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004158 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004159 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004160 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004161 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4162 /* if dir was BACKWARD then honor it just once */
4163 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004164 else if (did_emsg)
4165 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004166 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004167}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004168
4169/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004170 * Add completions from a dict.
4171 */
4172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004173ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004174{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004175 dictitem_T *di_refresh;
4176 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004177
4178 /* Check for optional "refresh" item. */
4179 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004180 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4181 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004182 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004183 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004184
4185 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4186 compl_opt_refresh_always = TRUE;
4187 }
4188
4189 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004190 di_words = dict_find(dict, (char_u *)"words", 5);
4191 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4192 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004193}
4194
4195/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004196 * Add a match to the list of matches from a typeval_T.
4197 * If the given string is already in the list of completions, then return
4198 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4199 * maybe because alloc() returns NULL, then FAIL is returned.
4200 */
4201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004202ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004203{
4204 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004205 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004206 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004207 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004208 char_u *(cptext[CPT_COUNT]);
4209
4210 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4211 {
4212 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4213 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4214 (char_u *)"abbr", FALSE);
4215 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4216 (char_u *)"menu", FALSE);
4217 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4218 (char_u *)"kind", FALSE);
4219 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4220 (char_u *)"info", FALSE);
4221 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4222 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004223 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004224 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004225 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4226 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004227 }
4228 else
4229 {
4230 word = get_tv_string_chk(tv);
4231 vim_memset(cptext, 0, sizeof(cptext));
4232 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004233 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004234 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004235 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004236}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004237#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004238
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004239/*
4240 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004241 * The search starts at position "ini" in curbuf and in the direction
4242 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004243 * When "compl_started" is FALSE start at that position, otherwise continue
4244 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004245 * This may return before finding all the matches.
4246 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 */
4248 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004249ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250{
4251 static pos_T first_match_pos;
4252 static pos_T last_match_pos;
4253 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004254 static int found_all = FALSE; /* Found all matches of a
4255 certain type. */
4256 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
Bram Moolenaar572cb562005-08-05 21:35:02 +00004258 pos_T *pos;
4259 char_u **matches;
4260 int save_p_scs;
4261 int save_p_ws;
4262 int save_p_ic;
4263 int i;
4264 int num_matches;
4265 int len;
4266 int found_new_match;
4267 int type = ctrl_x_mode;
4268 char_u *ptr;
4269 char_u *dict = NULL;
4270 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004271 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004273 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004275 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 ins_buf->b_scanned = 0;
4277 found_all = FALSE;
4278 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004279 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004280 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 last_match_pos = first_match_pos = *ini;
4282 }
4283
Bram Moolenaar4475b622017-05-01 20:46:52 +02004284 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004285 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4287 for (;;)
4288 {
4289 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004290 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004292 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 * or if found_all says this entry is done. For ^X^L only use the
4294 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004295 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004296 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 {
4298 found_all = FALSE;
4299 while (*e_cpt == ',' || *e_cpt == ' ')
4300 e_cpt++;
4301 if (*e_cpt == '.' && !curbuf->b_scanned)
4302 {
4303 ins_buf = curbuf;
4304 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004305 /* Move the cursor back one character so that ^N can match the
4306 * word immediately after the cursor. */
4307 if (ctrl_x_mode == 0 && dec(&first_match_pos) < 0)
4308 {
4309 /* Move the cursor to after the last character in the
4310 * buffer, so that word at start of buffer is found
4311 * correctly. */
4312 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4313 first_match_pos.col =
4314 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 last_match_pos = first_match_pos;
4317 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004318
4319 /* Remember the first match so that the loop stops when we
4320 * wrap and come back there a second time. */
4321 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
4323 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4324 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4325 {
4326 /* Scan a buffer, but not the current one. */
4327 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4328 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004329 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 first_match_pos.col = last_match_pos.col = 0;
4331 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4332 last_match_pos.lnum = 0;
4333 type = 0;
4334 }
4335 else /* unloaded buffer, scan like dictionary */
4336 {
4337 found_all = TRUE;
4338 if (ins_buf->b_fname == NULL)
4339 continue;
4340 type = CTRL_X_DICTIONARY;
4341 dict = ins_buf->b_fname;
4342 dict_f = DICT_EXACT;
4343 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004344 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 ins_buf->b_fname == NULL
4346 ? buf_spname(ins_buf)
4347 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004348 ? ins_buf->b_fname
4349 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004350 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 else if (*e_cpt == NUL)
4353 break;
4354 else
4355 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004356 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 type = -1;
4358 else if (*e_cpt == 'k' || *e_cpt == 's')
4359 {
4360 if (*e_cpt == 'k')
4361 type = CTRL_X_DICTIONARY;
4362 else
4363 type = CTRL_X_THESAURUS;
4364 if (*++e_cpt != ',' && *e_cpt != NUL)
4365 {
4366 dict = e_cpt;
4367 dict_f = DICT_FIRST;
4368 }
4369 }
4370#ifdef FEAT_FIND_ID
4371 else if (*e_cpt == 'i')
4372 type = CTRL_X_PATH_PATTERNS;
4373 else if (*e_cpt == 'd')
4374 type = CTRL_X_PATH_DEFINES;
4375#endif
4376 else if (*e_cpt == ']' || *e_cpt == 't')
4377 {
4378 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004379 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004380 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 }
4382 else
4383 type = -1;
4384
4385 /* in any case e_cpt is advanced to the next entry */
4386 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4387
4388 found_all = TRUE;
4389 if (type == -1)
4390 continue;
4391 }
4392 }
4393
Bram Moolenaar4475b622017-05-01 20:46:52 +02004394 /* If complete() was called then compl_pattern has been reset. The
4395 * following won't work then, bail out. */
4396 if (compl_pattern == NULL)
4397 break;
4398
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 switch (type)
4400 {
4401 case -1:
4402 break;
4403#ifdef FEAT_FIND_ID
4404 case CTRL_X_PATH_PATTERNS:
4405 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004406 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004407 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4411 (linenr_T)1, (linenr_T)MAXLNUM);
4412 break;
4413#endif
4414
4415 case CTRL_X_DICTIONARY:
4416 case CTRL_X_THESAURUS:
4417 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004418 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 : (type == CTRL_X_THESAURUS
4420 ? (*curbuf->b_p_tsr == NUL
4421 ? p_tsr
4422 : curbuf->b_p_tsr)
4423 : (*curbuf->b_p_dict == NUL
4424 ? p_dict
4425 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004426 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004427 dict != NULL ? dict_f
4428 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 dict = NULL;
4430 break;
4431
4432 case CTRL_X_TAGS:
4433 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4434 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004435 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004437 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004438 * of matches is found when compl_pattern is empty */
4439 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4441 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4442 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4443 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004444 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446 p_ic = save_p_ic;
4447 break;
4448
4449 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004450 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4452 {
4453
4454 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004455 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004456 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458 break;
4459
4460 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004461 if (expand_cmdline(&compl_xp, compl_pattern,
4462 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004464 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 break;
4466
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004467#ifdef FEAT_COMPL_FUNC
4468 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004469 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004470 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004471 break;
4472#endif
4473
Bram Moolenaar488c6512005-08-11 20:09:58 +00004474 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004475#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004476 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004477 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004478 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004479 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004480#endif
4481 break;
4482
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 default: /* normal ^P/^N and ^X^L */
4484 /*
4485 * If 'infercase' is set, don't use 'smartcase' here
4486 */
4487 save_p_scs = p_scs;
4488 if (ins_buf->b_p_inf)
4489 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004490
Bram Moolenaar361aa502014-01-23 22:45:58 +01004491 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 * end but never from the middle, thus setting nowrapscan in this
4493 * buffers is a good idea, on the other hand, we always set
4494 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4495 save_p_ws = p_ws;
4496 if (ins_buf != curbuf)
4497 p_ws = FALSE;
4498 else if (*e_cpt == '.')
4499 p_ws = TRUE;
4500 for (;;)
4501 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004502 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004504 ++msg_silent; /* Don't want messages for wrapscan. */
4505
Bram Moolenaare4214502015-03-05 18:08:43 +01004506 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4507 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004508 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004509 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004510 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004512 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004514 found_new_match = searchit(NULL, ins_buf, pos,
4515 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004516 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004517 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004518 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004519 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004521 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004522 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 first_match_pos = *pos;
4524 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004525 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004528 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 found_new_match = FAIL;
4530 if (found_new_match == FAIL)
4531 {
4532 if (ins_buf == curbuf)
4533 found_all = TRUE;
4534 break;
4535 }
4536
4537 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 && ini->lnum == pos->lnum
4540 && ini->col == pos->col)
4541 continue;
4542 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004543 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004545 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
4547 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4548 continue;
4549 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4550 if (!p_paste)
4551 ptr = skipwhite(ptr);
4552 }
4553 len = (int)STRLEN(ptr);
4554 }
4555 else
4556 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 char_u *tmp_ptr = ptr;
4558
4559 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004561 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 /* Skip if already inside a word. */
4563 if (vim_iswordp(tmp_ptr))
4564 continue;
4565 /* Find start of next word. */
4566 tmp_ptr = find_word_start(tmp_ptr);
4567 }
4568 /* Find end of this word. */
4569 tmp_ptr = find_word_end(tmp_ptr);
4570 len = (int)(tmp_ptr - ptr);
4571
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004572 if ((compl_cont_status & CONT_ADDING)
4573 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
4575 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4576 {
4577 /* Try next line, if any. the new word will be
4578 * "join" as if the normal command "J" was used.
4579 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004580 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 * works -- Acevedo */
4582 STRNCPY(IObuff, ptr, len);
4583 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4584 tmp_ptr = ptr = skipwhite(ptr);
4585 /* Find start of next word. */
4586 tmp_ptr = find_word_start(tmp_ptr);
4587 /* Find end of next word. */
4588 tmp_ptr = find_word_end(tmp_ptr);
4589 if (tmp_ptr > ptr)
4590 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004591 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004593 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 IObuff[len++] = ' ';
4595 /* IObuf =~ "\k.* ", thus len >= 2 */
4596 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004597 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 || (vim_strchr(p_cpo, CPO_JOINSP)
4599 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004600 && (IObuff[len - 2] == '?'
4601 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 IObuff[len++] = ' ';
4603 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004604 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (tmp_ptr - ptr >= IOSIZE - len)
4606 tmp_ptr = ptr + IOSIZE - len - 1;
4607 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4608 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004609 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611 IObuff[len] = NUL;
4612 ptr = IObuff;
4613 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004614 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 continue;
4616 }
4617 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004618 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004619 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004620 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
4622 found_new_match = OK;
4623 break;
4624 }
4625 }
4626 p_scs = save_p_scs;
4627 p_ws = save_p_ws;
4628 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004629
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004630 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004631 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004632 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 found_new_match = OK;
4634
4635 /* break the loop for specialized modes (use 'complete' just for the
4636 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004637 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004638 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004639 {
4640 if (got_int)
4641 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004642 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004643 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004644 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004645
Bram Moolenaare4214502015-03-05 18:08:43 +01004646 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004647 || compl_interrupted)
4648 break;
4649 compl_started = TRUE;
4650 }
4651 else
4652 {
4653 /* Mark a buffer scanned when it has been scanned completely */
4654 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4655 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004657 compl_started = FALSE;
4658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004660 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
Bram Moolenaare4214502015-03-05 18:08:43 +01004662 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 && *e_cpt == NUL) /* Got to end of 'complete' */
4664 found_new_match = FAIL;
4665
4666 i = -1; /* total of matches, unknown */
4667 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004668 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 i = ins_compl_make_cyclic();
4670
Bram Moolenaar4475b622017-05-01 20:46:52 +02004671 if (compl_old_match != NULL)
4672 {
4673 /* If several matches were added (FORWARD) or the search failed and has
4674 * just been made cyclic then we have to move compl_curr_match to the
4675 * next or previous entry (if any) -- Acevedo */
4676 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4677 : compl_old_match->cp_prev;
4678 if (compl_curr_match == NULL)
4679 compl_curr_match = compl_old_match;
4680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 return i;
4682}
4683
4684/* Delete the old text being completed. */
4685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004686ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004688 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
4690 /*
4691 * In insert mode: Delete the typed part.
4692 * In replace mode: Put the old characters back, if any.
4693 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004694 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4695 if ((int)curwin->w_cursor.col > col)
4696 {
4697 if (stop_arrow() == FAIL)
4698 return;
4699 backspace_until_column(col);
4700 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004701
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004702 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4703 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004705 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004706 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707}
4708
Bram Moolenaar472e8592016-10-15 17:06:47 +02004709/*
4710 * Insert the new text being completed.
4711 * "in_compl_func" is TRUE when called from complete_check().
4712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004714ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004716 dict_T *dict;
4717
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004718 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004719 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4720 compl_used_match = FALSE;
4721 else
4722 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004723
4724 /* Set completed item. */
4725 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004726 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004727 if (dict != NULL)
4728 {
4729 dict_add_nr_str(dict, "word", 0L,
4730 EMPTY_IF_NULL(compl_shown_match->cp_str));
4731 dict_add_nr_str(dict, "abbr", 0L,
4732 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4733 dict_add_nr_str(dict, "menu", 0L,
4734 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4735 dict_add_nr_str(dict, "kind", 0L,
4736 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4737 dict_add_nr_str(dict, "info", 0L,
4738 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4739 }
4740 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004741 if (!in_compl_func)
4742 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743}
4744
4745/*
4746 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004747 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4748 * get more completions. If it is FALSE, then we just do nothing when there
4749 * are no more completions in a given direction. The latter case is used when
4750 * we are still in the middle of finding completions, to allow browsing
4751 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 * Return the total number of matches, or -1 if still unknown -- webb.
4753 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004754 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4755 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 *
4757 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004758 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4759 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 */
4761 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004762ins_compl_next(
4763 int allow_get_expansion,
4764 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004765 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004766 int insert_match, /* Insert the newly selected match */
4767 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768{
4769 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004770 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004771 compl_T *found_compl = NULL;
4772 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004773 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004774 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004776 /* When user complete function return -1 for findstart which is next
4777 * time of 'always', compl_shown_match become NULL. */
4778 if (compl_shown_match == NULL)
4779 return -1;
4780
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004781 if (compl_leader != NULL
4782 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004784 /* Set "compl_shown_match" to the actually shown match, it may differ
4785 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004786 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004787 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004788 && compl_shown_match->cp_next != NULL
4789 && compl_shown_match->cp_next != compl_first_match)
4790 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004791
4792 /* If we didn't find it searching forward, and compl_shows_dir is
4793 * backward, find the last match. */
4794 if (compl_shows_dir == BACKWARD
4795 && !ins_compl_equal(compl_shown_match,
4796 compl_leader, (int)STRLEN(compl_leader))
4797 && (compl_shown_match->cp_next == NULL
4798 || compl_shown_match->cp_next == compl_first_match))
4799 {
4800 while (!ins_compl_equal(compl_shown_match,
4801 compl_leader, (int)STRLEN(compl_leader))
4802 && compl_shown_match->cp_prev != NULL
4803 && compl_shown_match->cp_prev != compl_first_match)
4804 compl_shown_match = compl_shown_match->cp_prev;
4805 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004806 }
4807
4808 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004809 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 /* Delete old text to be replaced */
4811 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004812
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004813 /* When finding the longest common text we stick at the original text,
4814 * don't let CTRL-N or CTRL-P move to the first match. */
4815 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4816
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004817 /* When restarting the search don't insert the first match either. */
4818 if (compl_restarting)
4819 {
4820 advance = FALSE;
4821 compl_restarting = FALSE;
4822 }
4823
Bram Moolenaare3226be2005-12-18 22:10:00 +00004824 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4825 * around. */
4826 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004828 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004830 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004831 found_end = (compl_first_match != NULL
4832 && (compl_shown_match->cp_next == compl_first_match
4833 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004834 }
4835 else if (compl_shows_dir == BACKWARD
4836 && compl_shown_match->cp_prev != NULL)
4837 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004838 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004839 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004840 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004843 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004844 if (!allow_get_expansion)
4845 {
4846 if (advance)
4847 {
4848 if (compl_shows_dir == BACKWARD)
4849 compl_pending -= todo + 1;
4850 else
4851 compl_pending += todo + 1;
4852 }
4853 return -1;
4854 }
4855
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004856 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004857 {
4858 if (compl_shows_dir == BACKWARD)
4859 --compl_pending;
4860 else
4861 ++compl_pending;
4862 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004863
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004864 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004865 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004866
4867 /* handle any pending completions */
4868 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004869 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004870 {
4871 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4872 {
4873 compl_shown_match = compl_shown_match->cp_next;
4874 --compl_pending;
4875 }
4876 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4877 {
4878 compl_shown_match = compl_shown_match->cp_prev;
4879 ++compl_pending;
4880 }
4881 else
4882 break;
4883 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004884 found_end = FALSE;
4885 }
4886 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4887 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004888 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004889 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004890 ++todo;
4891 else
4892 /* Remember a matching item. */
4893 found_compl = compl_shown_match;
4894
4895 /* Stop at the end of the list when we found a usable match. */
4896 if (found_end)
4897 {
4898 if (found_compl != NULL)
4899 {
4900 compl_shown_match = found_compl;
4901 break;
4902 }
4903 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004907 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004908 if (compl_no_insert && !started)
4909 {
4910 ins_bytes(compl_orig_text + ins_compl_len());
4911 compl_used_match = FALSE;
4912 }
4913 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004914 {
4915 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004916 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004917 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004918 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004919 }
4920 else
4921 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923 if (!allow_get_expansion)
4924 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004925 /* may undisplay the popup menu first */
4926 ins_compl_upd_pum();
4927
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004928 /* redraw to show the user what was inserted */
4929 update_screen(0);
4930
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004931 /* display the updated popup menu */
4932 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004933#ifdef FEAT_GUI
4934 if (gui.in_use)
4935 {
4936 /* Show the cursor after the match, not after the redrawn text. */
4937 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004938 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00004939 }
4940#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004941
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 /* Delete old text to be replaced, since we're still searching and
4943 * don't want to match ourselves! */
4944 ins_compl_delete();
4945 }
4946
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004947 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004948 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004949 if (compl_no_insert && !started)
4950 compl_enter_selects = TRUE;
4951 else
4952 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004953
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 /*
4955 * Show the file name for the match (if any)
4956 * Truncate the file name to avoid a wait for return.
4957 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004958 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02004960 char *lead = _("match in file");
4961 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4962 char_u *s;
4963 char_u *e;
4964
4965 if (space > 0)
4966 {
4967 /* We need the tail that fits. With double-byte encoding going
4968 * back from the end is very slow, thus go from the start and keep
4969 * the text that fits in "space" between "s" and "e". */
4970 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
4971 {
4972 space -= ptr2cells(e);
4973 while (space < 0)
4974 {
4975 space += ptr2cells(s);
4976 MB_PTR_ADV(s);
4977 }
4978 }
4979 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4980 s > compl_shown_match->cp_fname ? "<" : "", s);
4981 msg(IObuff);
4982 redraw_cmdline = FALSE; /* don't overwrite! */
4983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985
4986 return num_matches;
4987}
4988
4989/*
4990 * Call this while finding completions, to check whether the user has hit a key
4991 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004992 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004994 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004995 * "in_compl_func" is TRUE when called from complete_check(), don't set
4996 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
4998 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004999ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
5001 static int count = 0;
5002
5003 int c;
5004
5005 /* Don't check when reading keys from a script. That would break the test
5006 * scripts */
5007 if (using_script())
5008 return;
5009
5010 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005011 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 return;
5013 count = 0;
5014
Bram Moolenaara260a972006-06-23 19:36:29 +00005015 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5016 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 if (c != NUL)
5019 {
5020 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5021 {
5022 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005023 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005024 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005025 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005027 else
5028 {
5029 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005030 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005031 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005032 if (c != K_IGNORE)
5033 {
5034 /* Don't interrupt completion when the character wasn't typed,
5035 * e.g., when doing @q to replay keys. */
5036 if (c != Ctrl_R && KeyTyped)
5037 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005038
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005039 vungetc(c);
5040 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005043 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005044 {
5045 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5046
5047 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005048 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005049 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005050}
5051
5052/*
5053 * Decide the direction of Insert mode complete from the key typed.
5054 * Returns BACKWARD or FORWARD.
5055 */
5056 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005057ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005058{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005059 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005060 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005061 return BACKWARD;
5062 return FORWARD;
5063}
5064
5065/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005066 * Return TRUE for keys that are used for completion only when the popup menu
5067 * is visible.
5068 */
5069 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005070ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005071{
5072 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005073 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5074 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005075}
5076
5077/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005078 * Decide the number of completions to move forward.
5079 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5080 */
5081 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005082ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005083{
5084 int h;
5085
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005086 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005087 {
5088 h = pum_get_height();
5089 if (h > 3)
5090 h -= 2; /* keep some context */
5091 return h;
5092 }
5093 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094}
5095
5096/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005097 * Return TRUE if completion with "c" should insert the match, FALSE if only
5098 * to change the currently selected completion.
5099 */
5100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005101ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005102{
5103 switch (c)
5104 {
5105 case K_UP:
5106 case K_DOWN:
5107 case K_PAGEDOWN:
5108 case K_KPAGEDOWN:
5109 case K_S_DOWN:
5110 case K_PAGEUP:
5111 case K_KPAGEUP:
5112 case K_S_UP:
5113 return FALSE;
5114 }
5115 return TRUE;
5116}
5117
5118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 * Do Insert mode completion.
5120 * Called when character "c" was typed, which has a meaning for completion.
5121 * Returns OK if completion was done, FAIL if something failed (out of mem).
5122 */
5123 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005124ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005126 char_u *line;
5127 int startcol = 0; /* column where searched text starts */
5128 colnr_T curs_col; /* cursor column */
5129 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005130 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005131 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005132 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005133 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
Bram Moolenaare3226be2005-12-18 22:10:00 +00005135 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005136 insert_match = ins_compl_use_match(c);
5137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005138 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
5140 /* First time we hit ^N or ^P (in a row, I mean) */
5141
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 did_ai = FALSE;
5143#ifdef FEAT_SMARTINDENT
5144 did_si = FALSE;
5145 can_si = FALSE;
5146 can_si_back = FALSE;
5147#endif
5148 if (stop_arrow() == FAIL)
5149 return FAIL;
5150
5151 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005153 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005155 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005156 * "compl_startpos" to the cursor as a pattern to add a new word
5157 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005158 * "compl_startpos" is not in the same line as the cursor then fix it
5159 * (the line has been split because it was longer than 'tw'). if SOL
5160 * is set then skip the previous pattern, a word at the beginning of
5161 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005162 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5163 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {
5165 /*
5166 * it is a continued search
5167 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005168 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5170 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5171 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005172 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005174 /* line (probably) wrapped, set compl_startpos to the
5175 * first non_blank in the line, if it is not a wordchar
5176 * include it to get a better pattern, but then we don't
5177 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005178 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005179 compl_startpos.col = compl_col;
5180 compl_startpos.lnum = curwin->w_cursor.lnum;
5181 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
5183 else
5184 {
5185 /* S_IPOS was set when we inserted a word that was at the
5186 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005187 * mode but first we need to redefine compl_startpos */
5188 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 compl_cont_status |= CONT_SOL;
5191 compl_startpos.col = (colnr_T)(skipwhite(
5192 line + compl_length
5193 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005195 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005197 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005198 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005199 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005201 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005203 compl_cont_status &= ~CONT_SOL;
5204 compl_length = (IOSIZE - MIN_SPACE);
5205 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005207 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5208 if (compl_length < 1)
5209 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005211 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005212 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005214 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
5216 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005217 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005219 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005221 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 compl_cont_status = 0;
5224 compl_cont_status |= CONT_N_ADDS;
5225 compl_startpos = curwin->w_cursor;
5226 startcol = (int)curs_col;
5227 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229
5230 /* Work out completion pattern and original text -- webb */
5231 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5232 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005233 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5235 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005238 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005240 compl_col += ++startcol;
5241 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
5243 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 compl_pattern = str_foldcase(line + compl_col,
5245 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005247 compl_pattern = vim_strnsave(line + compl_col,
5248 compl_length);
5249 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 return FAIL;
5251 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005252 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
5254 char_u *prefix = (char_u *)"\\<";
5255
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005256 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005258 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005259 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005261 if (!vim_iswordp(line + compl_col)
5262 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 && (
5264#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005265 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005267 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268#endif
5269 )))
5270 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005271 STRCPY((char *)compl_pattern, prefix);
5272 (void)quote_meta(compl_pattern + STRLEN(prefix),
5273 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005275 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005277 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005279 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280#endif
5281 )
5282 {
5283 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005284 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5285 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005287 compl_col += curs_col;
5288 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
5290 else
5291 {
5292#ifdef FEAT_MBYTE
5293 /* Search the point of change class of multibyte character
5294 * or not a word single byte character backward. */
5295 if (has_mbyte)
5296 {
5297 int base_class;
5298 int head_off;
5299
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005300 startcol -= (*mb_head_off)(line, line + startcol);
5301 base_class = mb_get_class(line + startcol);
5302 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005304 head_off = (*mb_head_off)(line, line + startcol);
5305 if (base_class != mb_get_class(line + startcol
5306 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
5310 }
5311 else
5312#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 compl_col += ++startcol;
5316 compl_length = (int)curs_col - startcol;
5317 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
5319 /* Only match word with at least two chars -- webb
5320 * there's no need to call quote_meta,
5321 * alloc(7) is enough -- Acevedo
5322 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005323 compl_pattern = alloc(7);
5324 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005326 STRCPY((char *)compl_pattern, "\\<");
5327 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5328 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330 else
5331 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005332 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005333 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005334 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005336 STRCPY((char *)compl_pattern, "\\<");
5337 (void)quote_meta(compl_pattern + 2, line + compl_col,
5338 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
5340 }
5341 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005342 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005344 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005345 compl_length = (int)curs_col - (int)compl_col;
5346 if (compl_length < 0) /* cursor in indent: empty pattern */
5347 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005349 compl_pattern = str_foldcase(line + compl_col, compl_length,
5350 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005352 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5353 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 return FAIL;
5355 }
5356 else if (ctrl_x_mode == CTRL_X_FILES)
5357 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005358 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005359 if (startcol > 0)
5360 {
5361 char_u *p = line + startcol;
5362
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005363 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005364 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005365 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005366 if (p == line && vim_isfilec(PTR2CHAR(p)))
5367 startcol = 0;
5368 else
5369 startcol = (int)(p - line) + 1;
5370 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005371
Bram Moolenaar0300e462013-09-08 16:03:45 +02005372 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005373 compl_length = (int)curs_col - startcol;
5374 compl_pattern = addstar(line + compl_col, compl_length,
5375 EXPAND_FILES);
5376 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 return FAIL;
5378 }
5379 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5380 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005381 compl_pattern = vim_strnsave(line, curs_col);
5382 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005384 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005385 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005386 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5387 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005388 /* No completion possible, use an empty pattern to get a
5389 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005390 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005391 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005392 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5393 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005395 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005396 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005397#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005398 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005399 * Call user defined function 'completefunc' with "a:findstart"
5400 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005401 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005402 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005403 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005404 char_u *funcname;
5405 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005406 win_T *curwin_save;
5407 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005408
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005409 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005410 * string */
5411 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5412 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5413 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005414 {
5415 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5416 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005417 /* restore did_ai, so that adding comment leader works */
5418 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005419 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005420 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005421
5422 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005423 args[1] = NULL;
5424 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005425 curwin_save = curwin;
5426 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005427 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005428 if (curwin_save != curwin || curbuf_save != curbuf)
5429 {
5430 EMSG(_(e_complwin));
5431 return FAIL;
5432 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005433 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005434 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005435 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005436 {
5437 EMSG(_(e_compldel));
5438 return FAIL;
5439 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005440
Bram Moolenaar6110a002012-01-26 18:58:38 +01005441 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005442 * cancel the complete without an error.
5443 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005444 if (col == -2)
5445 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005446 if (col == -3)
5447 {
5448 ctrl_x_mode = 0;
5449 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005450 if (!shortmess(SHM_COMPLETIONMENU))
5451 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005452 return FAIL;
5453 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005454
Bram Moolenaar82139082011-09-14 16:52:09 +02005455 /*
5456 * Reset extended parameters of completion, when start new
5457 * completion.
5458 */
5459 compl_opt_refresh_always = FALSE;
5460
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005461 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005462 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005463 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005464 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005465 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005466
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005467 /* Setup variables for completion. Need to obtain "line" again,
5468 * it may have become invalid. */
5469 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005470 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005471 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5472 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005473#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005474 return FAIL;
5475 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005476 else if (ctrl_x_mode == CTRL_X_SPELL)
5477 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005478#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005479 if (spell_bad_len > 0)
5480 compl_col = curs_col - spell_bad_len;
5481 else
5482 compl_col = spell_word_start(startcol);
5483 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005484 {
5485 compl_length = 0;
5486 compl_col = curs_col;
5487 }
5488 else
5489 {
5490 spell_expand_check_cap(compl_col);
5491 compl_length = (int)curs_col - compl_col;
5492 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005493 /* Need to obtain "line" again, it may have become invalid. */
5494 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005495 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5496 if (compl_pattern == NULL)
5497#endif
5498 return FAIL;
5499 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005500 else
5501 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005502 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005503 return FAIL;
5504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005506 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
5508 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005509 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
5511 /* Insert a new line, keep indentation but ignore 'comments' */
5512#ifdef FEAT_COMMENTS
5513 char_u *old = curbuf->b_p_com;
5514
5515 curbuf->b_p_com = (char_u *)"";
5516#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005517 compl_startpos.lnum = curwin->w_cursor.lnum;
5518 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 ins_eol('\r');
5520#ifdef FEAT_COMMENTS
5521 curbuf->b_p_com = old;
5522#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005523 compl_length = 0;
5524 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 }
5526 }
5527 else
5528 {
5529 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005530 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 }
5532
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005533 if (compl_cont_status & CONT_LOCAL)
5534 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 else
5536 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5537
Bram Moolenaar62951b12011-09-21 18:23:05 +02005538 /* If any of the original typed text has been changed we need to fix
5539 * the redo buffer. */
5540 ins_compl_fixRedoBufForLeader(NULL);
5541
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005542 /* Always add completion for the original text. */
5543 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005544 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5545 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005546 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005548 vim_free(compl_pattern);
5549 compl_pattern = NULL;
5550 vim_free(compl_orig_text);
5551 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 return FAIL;
5553 }
5554
5555 /* showmode might reset the internal line pointers, so it must
5556 * be called before line = ml_get(), or when this address is no
5557 * longer needed. -- Acevedo.
5558 */
5559 edit_submode_extra = (char_u *)_("-- Searching...");
5560 edit_submode_highl = HLF_COUNT;
5561 showmode();
5562 edit_submode_extra = NULL;
5563 out_flush();
5564 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005565 else if (insert_match && stop_arrow() == FAIL)
5566 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005568 compl_shown_match = compl_curr_match;
5569 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
5571 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005572 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005574 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005575 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005576 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005578 /* may undisplay the popup menu */
5579 ins_compl_upd_pum();
5580
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005581 if (n > 1) /* all matches have been found */
5582 compl_matches = n;
5583 compl_curr_match = compl_shown_match;
5584 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
Bram Moolenaard68071d2006-05-02 22:08:30 +00005586 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5587 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 if (got_int && !global_busy)
5589 {
5590 (void)vgetc();
5591 got_int = FALSE;
5592 }
5593
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005594 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005595 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005597 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5598 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5600 edit_submode_highl = HLF_E;
5601 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5602 * because we couldn't expand anything at first place, but if we used
5603 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5604 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005605 if ( compl_length > 1
5606 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 || (ctrl_x_mode != 0
5608 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5609 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005610 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 }
5612
Bram Moolenaar572cb562005-08-05 21:35:02 +00005613 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005614 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005616 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
5618 if (edit_submode_extra == NULL)
5619 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005620 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
5622 edit_submode_extra = (char_u *)_("Back at original");
5623 edit_submode_highl = HLF_W;
5624 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005625 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 {
5627 edit_submode_extra = (char_u *)_("Word from other line");
5628 edit_submode_highl = HLF_COUNT;
5629 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005630 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
5632 edit_submode_extra = (char_u *)_("The only match");
5633 edit_submode_highl = HLF_COUNT;
5634 }
5635 else
5636 {
5637 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005638 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005640 int number = 0;
5641 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005643 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {
5645 /* search backwards for the first valid (!= -1) number.
5646 * This should normally succeed already at the first loop
5647 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005648 for (match = compl_curr_match->cp_prev; match != NULL
5649 && match != compl_first_match;
5650 match = match->cp_prev)
5651 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005653 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 break;
5655 }
5656 if (match != NULL)
5657 /* go up and assign all numbers which are not assigned
5658 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005659 for (match = match->cp_next;
5660 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005661 match = match->cp_next)
5662 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 }
5664 else /* BACKWARD */
5665 {
5666 /* search forwards (upwards) for the first valid (!= -1)
5667 * number. This should normally succeed already at the
5668 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005669 for (match = compl_curr_match->cp_next; match != NULL
5670 && match != compl_first_match;
5671 match = match->cp_next)
5672 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005674 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 break;
5676 }
5677 if (match != NULL)
5678 /* go down and assign all numbers which are not
5679 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005680 for (match = match->cp_prev; match
5681 && match->cp_number == -1;
5682 match = match->cp_prev)
5683 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685 }
5686
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005687 /* The match should always have a sequence number now, this is
5688 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005689 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005691 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5692 * Translations may need more than twice that. */
5693 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005695 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005696 vim_snprintf((char *)match_ref, sizeof(match_ref),
5697 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005698 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005700 vim_snprintf((char *)match_ref, sizeof(match_ref),
5701 _("match %d"),
5702 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 edit_submode_extra = match_ref;
5704 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005705 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 curs_columns(FALSE);
5707 }
5708 }
5709 }
5710
5711 /* Show a message about what (completion) mode we're in. */
5712 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005713 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005715 if (edit_submode_extra != NULL)
5716 {
5717 if (!p_smd)
5718 msg_attr(edit_submode_extra,
5719 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005720 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005721 }
5722 else
5723 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaard68071d2006-05-02 22:08:30 +00005726 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005727 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005728 show_pum(save_w_wrow, save_w_leftcol);
5729
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005730 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005731 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005732
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 return OK;
5734}
5735
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005736 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005737show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005738{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005739 /* RedrawingDisabled may be set when invoked through complete(). */
5740 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005741
Bram Moolenaarcb036422017-03-01 12:29:10 +01005742 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005743
Bram Moolenaarcb036422017-03-01 12:29:10 +01005744 /* If the cursor moved or the display scrolled we need to remove the pum
5745 * first. */
5746 setcursor();
5747 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5748 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005749
Bram Moolenaarcb036422017-03-01 12:29:10 +01005750 ins_compl_show_pum();
5751 setcursor();
5752 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005753}
5754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755/*
5756 * Looks in the first "len" chars. of "src" for search-metachars.
5757 * If dest is not NULL the chars. are copied there quoting (with
5758 * a backslash) the metachars, and dest would be NUL terminated.
5759 * Returns the length (needed) of dest
5760 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005761 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005762quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005764 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005766 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 {
5768 switch (*src)
5769 {
5770 case '.':
5771 case '*':
5772 case '[':
5773 if (ctrl_x_mode == CTRL_X_DICTIONARY
5774 || ctrl_x_mode == CTRL_X_THESAURUS)
5775 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005776 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 case '~':
5778 if (!p_magic) /* quote these only if magic is set */
5779 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005780 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 case '\\':
5782 if (ctrl_x_mode == CTRL_X_DICTIONARY
5783 || ctrl_x_mode == CTRL_X_THESAURUS)
5784 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005785 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 case '^': /* currently it's not needed. */
5787 case '$':
5788 m++;
5789 if (dest != NULL)
5790 *dest++ = '\\';
5791 break;
5792 }
5793 if (dest != NULL)
5794 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005795# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 /* Copy remaining bytes of a multibyte character. */
5797 if (has_mbyte)
5798 {
5799 int i, mb_len;
5800
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005801 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 if (mb_len > 0 && len >= mb_len)
5803 for (i = 0; i < mb_len; ++i)
5804 {
5805 --len;
5806 ++src;
5807 if (dest != NULL)
5808 *dest++ = *src;
5809 }
5810 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 }
5813 if (dest != NULL)
5814 *dest = NUL;
5815
5816 return m;
5817}
5818#endif /* FEAT_INS_EXPAND */
5819
5820/*
5821 * Next character is interpreted literally.
5822 * A one, two or three digit decimal number is interpreted as its byte value.
5823 * If one or two digits are entered, the next character is given to vungetc().
5824 * For Unicode a character > 255 may be returned.
5825 */
5826 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005827get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828{
5829 int cc;
5830 int nc;
5831 int i;
5832 int hex = FALSE;
5833 int octal = FALSE;
5834#ifdef FEAT_MBYTE
5835 int unicode = 0;
5836#endif
5837
5838 if (got_int)
5839 return Ctrl_C;
5840
5841#ifdef FEAT_GUI
5842 /*
5843 * In GUI there is no point inserting the internal code for a special key.
5844 * It is more useful to insert the string "<KEY>" instead. This would
5845 * probably be useful in a text window too, but it would not be
5846 * vi-compatible (maybe there should be an option for it?) -- webb
5847 */
5848 if (gui.in_use)
5849 ++allow_keys;
5850#endif
5851#ifdef USE_ON_FLY_SCROLL
5852 dont_scroll = TRUE; /* disallow scrolling here */
5853#endif
5854 ++no_mapping; /* don't map the next key hits */
5855 cc = 0;
5856 i = 0;
5857 for (;;)
5858 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005859 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860#ifdef FEAT_CMDL_INFO
5861 if (!(State & CMDLINE)
5862# ifdef FEAT_MBYTE
5863 && MB_BYTE2LEN_CHECK(nc) == 1
5864# endif
5865 )
5866 add_to_showcmd(nc);
5867#endif
5868 if (nc == 'x' || nc == 'X')
5869 hex = TRUE;
5870 else if (nc == 'o' || nc == 'O')
5871 octal = TRUE;
5872#ifdef FEAT_MBYTE
5873 else if (nc == 'u' || nc == 'U')
5874 unicode = nc;
5875#endif
5876 else
5877 {
5878 if (hex
5879#ifdef FEAT_MBYTE
5880 || unicode != 0
5881#endif
5882 )
5883 {
5884 if (!vim_isxdigit(nc))
5885 break;
5886 cc = cc * 16 + hex2nr(nc);
5887 }
5888 else if (octal)
5889 {
5890 if (nc < '0' || nc > '7')
5891 break;
5892 cc = cc * 8 + nc - '0';
5893 }
5894 else
5895 {
5896 if (!VIM_ISDIGIT(nc))
5897 break;
5898 cc = cc * 10 + nc - '0';
5899 }
5900
5901 ++i;
5902 }
5903
5904 if (cc > 255
5905#ifdef FEAT_MBYTE
5906 && unicode == 0
5907#endif
5908 )
5909 cc = 255; /* limit range to 0-255 */
5910 nc = 0;
5911
5912 if (hex) /* hex: up to two chars */
5913 {
5914 if (i >= 2)
5915 break;
5916 }
5917#ifdef FEAT_MBYTE
5918 else if (unicode) /* Unicode: up to four or eight chars */
5919 {
5920 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5921 break;
5922 }
5923#endif
5924 else if (i >= 3) /* decimal or octal: up to three chars */
5925 break;
5926 }
5927 if (i == 0) /* no number entered */
5928 {
5929 if (nc == K_ZERO) /* NUL is stored as NL */
5930 {
5931 cc = '\n';
5932 nc = 0;
5933 }
5934 else
5935 {
5936 cc = nc;
5937 nc = 0;
5938 }
5939 }
5940
5941 if (cc == 0) /* NUL is stored as NL */
5942 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005943#ifdef FEAT_MBYTE
5944 if (enc_dbcs && (cc & 0xff) == 0)
5945 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5946 second byte will cause trouble! */
5947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948
5949 --no_mapping;
5950#ifdef FEAT_GUI
5951 if (gui.in_use)
5952 --allow_keys;
5953#endif
5954 if (nc)
5955 vungetc(nc);
5956 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5957 return cc;
5958}
5959
5960/*
5961 * Insert character, taking care of special keys and mod_mask
5962 */
5963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005964insert_special(
5965 int c,
5966 int allow_modmask,
5967 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968{
5969 char_u *p;
5970 int len;
5971
5972 /*
5973 * Special function key, translate into "<Key>". Up to the last '>' is
5974 * inserted with ins_str(), so as not to replace characters in replace
5975 * mode.
5976 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5977 * unless 'allow_modmask' is TRUE.
5978 */
Bram Moolenaard0573012017-10-28 21:11:06 +02005979#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 /* Command-key never produces a normal key */
5981 if (mod_mask & MOD_MASK_CMD)
5982 allow_modmask = TRUE;
5983#endif
5984 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5985 {
5986 p = get_special_key_name(c, mod_mask);
5987 len = (int)STRLEN(p);
5988 c = p[len - 1];
5989 if (len > 2)
5990 {
5991 if (stop_arrow() == FAIL)
5992 return;
5993 p[len - 1] = NUL;
5994 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005995 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 ctrlv = FALSE;
5997 }
5998 }
5999 if (stop_arrow() == OK)
6000 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6001}
6002
6003/*
6004 * Special characters in this context are those that need processing other
6005 * than the simple insertion that can be performed here. This includes ESC
6006 * which terminates the insert, and CR/NL which need special processing to
6007 * open up a new line. This routine tries to optimize insertions performed by
6008 * the "redo", "undo" or "put" commands, so it needs to know when it should
6009 * stop and defer processing to the "normal" mechanism.
6010 * '0' and '^' are special, because they can be followed by CTRL-D.
6011 */
6012#ifdef EBCDIC
6013# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6014#else
6015# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6016#endif
6017
6018#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006019# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006021# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022#endif
6023
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006024/*
6025 * "flags": INSCHAR_FORMAT - force formatting
6026 * INSCHAR_CTRLV - char typed just after CTRL-V
6027 * INSCHAR_NO_FEX - don't use 'formatexpr'
6028 *
6029 * NOTE: passes the flags value straight through to internal_format() which,
6030 * beside INSCHAR_FORMAT (above), is also looking for these:
6031 * INSCHAR_DO_COM - format comments
6032 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006035insertchar(
6036 int c, /* character to insert or NUL */
6037 int flags, /* INSCHAR_FORMAT, etc. */
6038 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 int textwidth;
6041#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006045 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006047 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049
6050 /*
6051 * Try to break the line in two or more pieces when:
6052 * - Always do this if we have been called to do formatting only.
6053 * - Always do this when 'formatoptions' has the 'a' flag and the line
6054 * ends in white space.
6055 * - Otherwise:
6056 * - Don't do this if inserting a blank
6057 * - Don't do this if an existing character is being replaced, unless
6058 * we're in VREPLACE mode.
6059 * - Do this if the cursor is not on the line where insert started
6060 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6061 * before the insert.
6062 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6063 * before 'textwidth'
6064 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006065 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006066 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006067 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 && !((State & REPLACE_FLAG)
6069#ifdef FEAT_VREPLACE
6070 && !(State & VREPLACE_FLAG)
6071#endif
6072 && *ml_get_cursor() != NUL)
6073 && (curwin->w_cursor.lnum != Insstart.lnum
6074 || ((!has_format_option(FO_INS_LONG)
6075 || Insstart_textlen <= (colnr_T)textwidth)
6076 && (!fo_ins_blank
6077 || Insstart_blank_vcol <= (colnr_T)textwidth
6078 ))))))
6079 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006080 /* Format with 'formatexpr' when it's set. Use internal formatting
6081 * when 'formatexpr' isn't set or it returns non-zero. */
6082#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006083 int do_internal = TRUE;
6084 colnr_T virtcol = get_nolist_virtcol()
6085 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006086
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006087 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6088 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006089 {
6090 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6091 /* It may be required to save for undo again, e.g. when setline()
6092 * was called. */
6093 ins_need_undo = TRUE;
6094 }
6095 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006097 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006099
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 if (c == NUL) /* only formatting was wanted */
6101 return;
6102
6103#ifdef FEAT_COMMENTS
6104 /* Check whether this character should end a comment. */
6105 if (did_ai && (int)c == end_comment_pending)
6106 {
6107 char_u *line;
6108 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6109 int middle_len, end_len;
6110 int i;
6111
6112 /*
6113 * Need to remove existing (middle) comment leader and insert end
6114 * comment leader. First, check what comment leader we can find.
6115 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006116 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6118 {
6119 /* Skip middle-comment string */
6120 while (*p && p[-1] != ':') /* find end of middle flags */
6121 ++p;
6122 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6123 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006124 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 --middle_len;
6126
6127 /* Find the end-comment string */
6128 while (*p && p[-1] != ':') /* find end of end flags */
6129 ++p;
6130 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6131
6132 /* Skip white space before the cursor */
6133 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006134 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 ;
6136 i++;
6137
6138 /* Skip to before the middle leader */
6139 i -= middle_len;
6140
6141 /* Check some expected things before we go on */
6142 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6143 {
6144 /* Backspace over all the stuff we want to replace */
6145 backspace_until_column(i);
6146
6147 /*
6148 * Insert the end-comment string, except for the last
6149 * character, which will get inserted as normal later.
6150 */
6151 ins_bytes_len(lead_end, end_len - 1);
6152 }
6153 }
6154 }
6155 end_comment_pending = NUL;
6156#endif
6157
6158 did_ai = FALSE;
6159#ifdef FEAT_SMARTINDENT
6160 did_si = FALSE;
6161 can_si = FALSE;
6162 can_si_back = FALSE;
6163#endif
6164
6165 /*
6166 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6167 * This speeds up normal text input considerably.
6168 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6169 * need to re-indent at a ':', or any other character (but not what
6170 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006171 * Don't do this when there an InsertCharPre autocommand is defined,
6172 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 */
6174#ifdef USE_ON_FLY_SCROLL
6175 dont_scroll = FALSE; /* allow scrolling here */
6176#endif
6177
6178 if ( !ISSPECIAL(c)
6179#ifdef FEAT_MBYTE
6180 && (!has_mbyte || (*mb_char2len)(c) == 1)
6181#endif
6182 && vpeekc() != NUL
6183 && !(State & REPLACE_FLAG)
6184#ifdef FEAT_CINDENT
6185 && !cindent_on()
6186#endif
6187#ifdef FEAT_RIGHTLEFT
6188 && !p_ri
6189#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006190#ifdef FEAT_AUTOCMD
6191 && !has_insertcharpre()
6192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 )
6194 {
6195#define INPUT_BUFLEN 100
6196 char_u buf[INPUT_BUFLEN + 1];
6197 int i;
6198 colnr_T virtcol = 0;
6199
6200 buf[0] = c;
6201 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006202 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 virtcol = get_nolist_virtcol();
6204 /*
6205 * Stop the string when:
6206 * - no more chars available
6207 * - finding a special character (command key)
6208 * - buffer is full
6209 * - running into the 'textwidth' boundary
6210 * - need to check for abbreviation: A non-word char after a word-char
6211 */
6212 while ( (c = vpeekc()) != NUL
6213 && !ISSPECIAL(c)
6214#ifdef FEAT_MBYTE
6215 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6216#endif
6217 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006218# ifdef FEAT_FKMAP
6219 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 && (textwidth == 0
6222 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6223 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6224 {
6225#ifdef FEAT_RIGHTLEFT
6226 c = vgetc();
6227 if (p_hkmap && KeyTyped)
6228 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 buf[i++] = c;
6230#else
6231 buf[i++] = vgetc();
6232#endif
6233 }
6234
6235#ifdef FEAT_DIGRAPHS
6236 do_digraph(-1); /* clear digraphs */
6237 do_digraph(buf[i-1]); /* may be the start of a digraph */
6238#endif
6239 buf[i] = NUL;
6240 ins_str(buf);
6241 if (flags & INSCHAR_CTRLV)
6242 {
6243 redo_literal(*buf);
6244 i = 1;
6245 }
6246 else
6247 i = 0;
6248 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006249 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251 else
6252 {
6253#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006254 int cc;
6255
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6257 {
6258 char_u buf[MB_MAXBYTES + 1];
6259
6260 (*mb_char2bytes)(c, buf);
6261 buf[cc] = NUL;
6262 ins_char_bytes(buf, cc);
6263 AppendCharToRedobuff(c);
6264 }
6265 else
6266#endif
6267 {
6268 ins_char(c);
6269 if (flags & INSCHAR_CTRLV)
6270 redo_literal(c);
6271 else
6272 AppendCharToRedobuff(c);
6273 }
6274 }
6275}
6276
6277/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006278 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006279 *
6280 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6281 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006282 */
6283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006284internal_format(
6285 int textwidth,
6286 int second_indent,
6287 int flags,
6288 int format_only,
6289 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006290{
6291 int cc;
6292 int save_char = NUL;
6293 int haveto_redraw = FALSE;
6294 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6295#ifdef FEAT_MBYTE
6296 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6297#endif
6298 int fo_white_par = has_format_option(FO_WHITE_PAR);
6299 int first_line = TRUE;
6300#ifdef FEAT_COMMENTS
6301 colnr_T leader_len;
6302 int no_leader = FALSE;
6303 int do_comments = (flags & INSCHAR_DO_COM);
6304#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006305#ifdef FEAT_LINEBREAK
6306 int has_lbr = curwin->w_p_lbr;
6307
6308 /* make sure win_lbr_chartabsize() counts correctly */
6309 curwin->w_p_lbr = FALSE;
6310#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006311
6312 /*
6313 * When 'ai' is off we don't want a space under the cursor to be
6314 * deleted. Replace it with an 'x' temporarily.
6315 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006316 if (!curbuf->b_p_ai
6317#ifdef FEAT_VREPLACE
6318 && !(State & VREPLACE_FLAG)
6319#endif
6320 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006321 {
6322 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006323 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006324 {
6325 save_char = cc;
6326 pchar_cursor('x');
6327 }
6328 }
6329
6330 /*
6331 * Repeat breaking lines, until the current line is not too long.
6332 */
6333 while (!got_int)
6334 {
6335 int startcol; /* Cursor column at entry */
6336 int wantcol; /* column at textwidth border */
6337 int foundcol; /* column for start of spaces */
6338 int end_foundcol = 0; /* column for start of word */
6339 colnr_T len;
6340 colnr_T virtcol;
6341#ifdef FEAT_VREPLACE
6342 int orig_col = 0;
6343 char_u *saved_text = NULL;
6344#endif
6345 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006346 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006347
Bram Moolenaar97b98102009-11-17 16:41:01 +00006348 virtcol = get_nolist_virtcol()
6349 + char2cells(c != NUL ? c : gchar_cursor());
6350 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006351 break;
6352
6353#ifdef FEAT_COMMENTS
6354 if (no_leader)
6355 do_comments = FALSE;
6356 else if (!(flags & INSCHAR_FORMAT)
6357 && has_format_option(FO_WRAP_COMS))
6358 do_comments = TRUE;
6359
6360 /* Don't break until after the comment leader */
6361 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006362 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006363 else
6364 leader_len = 0;
6365
6366 /* If the line doesn't start with a comment leader, then don't
6367 * start one in a following broken line. Avoids that a %word
6368 * moved to the start of the next line causes all following lines
6369 * to start with %. */
6370 if (leader_len == 0)
6371 no_leader = TRUE;
6372#endif
6373 if (!(flags & INSCHAR_FORMAT)
6374#ifdef FEAT_COMMENTS
6375 && leader_len == 0
6376#endif
6377 && !has_format_option(FO_WRAP))
6378
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006379 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006380 if ((startcol = curwin->w_cursor.col) == 0)
6381 break;
6382
6383 /* find column of textwidth border */
6384 coladvance((colnr_T)textwidth);
6385 wantcol = curwin->w_cursor.col;
6386
Bram Moolenaar97b98102009-11-17 16:41:01 +00006387 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006388 foundcol = 0;
6389
6390 /*
6391 * Find position to break at.
6392 * Stop at first entered white when 'formatoptions' has 'v'
6393 */
6394 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006395 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006396 || curwin->w_cursor.lnum != Insstart.lnum
6397 || curwin->w_cursor.col >= Insstart.col)
6398 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006399 if (curwin->w_cursor.col == startcol && c != NUL)
6400 cc = c;
6401 else
6402 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006403 if (WHITECHAR(cc))
6404 {
6405 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006406 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006407
6408 /* find start of sequence of blanks */
6409 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6410 {
6411 dec_cursor();
6412 cc = gchar_cursor();
6413 }
6414 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6415 break; /* only spaces in front of text */
6416#ifdef FEAT_COMMENTS
6417 /* Don't break until after the comment leader */
6418 if (curwin->w_cursor.col < leader_len)
6419 break;
6420#endif
6421 if (has_format_option(FO_ONE_LETTER))
6422 {
6423 /* do not break after one-letter words */
6424 if (curwin->w_cursor.col == 0)
6425 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006426#ifdef FEAT_COMMENTS
6427 /* do not break "#a b" when 'tw' is 2 */
6428 if (curwin->w_cursor.col <= leader_len)
6429 break;
6430#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006431 col = curwin->w_cursor.col;
6432 dec_cursor();
6433 cc = gchar_cursor();
6434
6435 if (WHITECHAR(cc))
6436 continue; /* one-letter, continue */
6437 curwin->w_cursor.col = col;
6438 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006439
6440 inc_cursor();
6441
6442 end_foundcol = end_col + 1;
6443 foundcol = curwin->w_cursor.col;
6444 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006445 break;
6446 }
6447#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006448 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006449 {
6450 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006451 if (curwin->w_cursor.col != startcol)
6452 {
6453#ifdef FEAT_COMMENTS
6454 /* Don't break until after the comment leader */
6455 if (curwin->w_cursor.col < leader_len)
6456 break;
6457#endif
6458 col = curwin->w_cursor.col;
6459 inc_cursor();
6460 /* Don't change end_foundcol if already set. */
6461 if (foundcol != curwin->w_cursor.col)
6462 {
6463 foundcol = curwin->w_cursor.col;
6464 end_foundcol = foundcol;
6465 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6466 break;
6467 }
6468 curwin->w_cursor.col = col;
6469 }
6470
6471 if (curwin->w_cursor.col == 0)
6472 break;
6473
6474 col = curwin->w_cursor.col;
6475
6476 dec_cursor();
6477 cc = gchar_cursor();
6478
6479 if (WHITECHAR(cc))
6480 continue; /* break with space */
6481#ifdef FEAT_COMMENTS
6482 /* Don't break until after the comment leader */
6483 if (curwin->w_cursor.col < leader_len)
6484 break;
6485#endif
6486
6487 curwin->w_cursor.col = col;
6488
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006489 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006490 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006491 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6492 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006493 }
6494#endif
6495 if (curwin->w_cursor.col == 0)
6496 break;
6497 dec_cursor();
6498 }
6499
6500 if (foundcol == 0) /* no spaces, cannot break line */
6501 {
6502 curwin->w_cursor.col = startcol;
6503 break;
6504 }
6505
6506 /* Going to break the line, remove any "$" now. */
6507 undisplay_dollar();
6508
6509 /*
6510 * Offset between cursor position and line break is used by replace
6511 * stack functions. VREPLACE does not use this, and backspaces
6512 * over the text instead.
6513 */
6514#ifdef FEAT_VREPLACE
6515 if (State & VREPLACE_FLAG)
6516 orig_col = startcol; /* Will start backspacing from here */
6517 else
6518#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006519 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006520
6521 /*
6522 * adjust startcol for spaces that will be deleted and
6523 * characters that will remain on top line
6524 */
6525 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006526 while ((cc = gchar_cursor(), WHITECHAR(cc))
6527 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006528 inc_cursor();
6529 startcol -= curwin->w_cursor.col;
6530 if (startcol < 0)
6531 startcol = 0;
6532
6533#ifdef FEAT_VREPLACE
6534 if (State & VREPLACE_FLAG)
6535 {
6536 /*
6537 * In VREPLACE mode, we will backspace over the text to be
6538 * wrapped, so save a copy now to put on the next line.
6539 */
6540 saved_text = vim_strsave(ml_get_cursor());
6541 curwin->w_cursor.col = orig_col;
6542 if (saved_text == NULL)
6543 break; /* Can't do it, out of memory */
6544 saved_text[startcol] = NUL;
6545
6546 /* Backspace over characters that will move to the next line */
6547 if (!fo_white_par)
6548 backspace_until_column(foundcol);
6549 }
6550 else
6551#endif
6552 {
6553 /* put cursor after pos. to break line */
6554 if (!fo_white_par)
6555 curwin->w_cursor.col = foundcol;
6556 }
6557
6558 /*
6559 * Split the line just before the margin.
6560 * Only insert/delete lines, but don't really redraw the window.
6561 */
6562 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6563 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6564#ifdef FEAT_COMMENTS
6565 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006566 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006567#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006568 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6569 if (!(flags & INSCHAR_COM_LIST))
6570 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006571
6572 replace_offset = 0;
6573 if (first_line)
6574 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006575 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006576 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006577 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006578 * This section is for auto-wrap of numeric lists. When not
6579 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6580 * flag will be set and open_line() will handle it (as seen
6581 * above). The code here (and in get_number_indent()) will
6582 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006583 */
6584 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006585 second_indent =
6586 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006587 if (second_indent >= 0)
6588 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006589#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006590 if (State & VREPLACE_FLAG)
6591 change_indent(INDENT_SET, second_indent,
6592 FALSE, NUL, TRUE);
6593 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006594#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006595#ifdef FEAT_COMMENTS
6596 if (leader_len > 0 && second_indent - leader_len > 0)
6597 {
6598 int i;
6599 int padding = second_indent - leader_len;
6600
6601 /* We started at the first_line of a numbered list
6602 * that has a comment. the open_line() function has
6603 * inserted the proper comment leader and positioned
6604 * the cursor at the end of the split line. Now we
6605 * add the additional whitespace needed after the
6606 * comment leader for the numbered list. */
6607 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006608 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006609 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006610 }
6611 else
6612 {
6613#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006614 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006615#ifdef FEAT_COMMENTS
6616 }
6617#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006618 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006619 }
6620 first_line = FALSE;
6621 }
6622
6623#ifdef FEAT_VREPLACE
6624 if (State & VREPLACE_FLAG)
6625 {
6626 /*
6627 * In VREPLACE mode we have backspaced over the text to be
6628 * moved, now we re-insert it into the new line.
6629 */
6630 ins_bytes(saved_text);
6631 vim_free(saved_text);
6632 }
6633 else
6634#endif
6635 {
6636 /*
6637 * Check if cursor is not past the NUL off the line, cindent
6638 * may have added or removed indent.
6639 */
6640 curwin->w_cursor.col += startcol;
6641 len = (colnr_T)STRLEN(ml_get_curline());
6642 if (curwin->w_cursor.col > len)
6643 curwin->w_cursor.col = len;
6644 }
6645
6646 haveto_redraw = TRUE;
6647#ifdef FEAT_CINDENT
6648 can_cindent = TRUE;
6649#endif
6650 /* moved the cursor, don't autoindent or cindent now */
6651 did_ai = FALSE;
6652#ifdef FEAT_SMARTINDENT
6653 did_si = FALSE;
6654 can_si = FALSE;
6655 can_si_back = FALSE;
6656#endif
6657 line_breakcheck();
6658 }
6659
6660 if (save_char != NUL) /* put back space after cursor */
6661 pchar_cursor(save_char);
6662
Bram Moolenaar0026d472014-09-09 16:32:39 +02006663#ifdef FEAT_LINEBREAK
6664 curwin->w_p_lbr = has_lbr;
6665#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006666 if (!format_only && haveto_redraw)
6667 {
6668 update_topline();
6669 redraw_curbuf_later(VALID);
6670 }
6671}
6672
6673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 * Called after inserting or deleting text: When 'formatoptions' includes the
6675 * 'a' flag format from the current line until the end of the paragraph.
6676 * Keep the cursor at the same position relative to the text.
6677 * The caller must have saved the cursor line for undo, following ones will be
6678 * saved here.
6679 */
6680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006681auto_format(
6682 int trailblank, /* when TRUE also format with trailing blank */
6683 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
6685 pos_T pos;
6686 colnr_T len;
6687 char_u *old;
6688 char_u *new, *pnew;
6689 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006690 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
6692 if (!has_format_option(FO_AUTO))
6693 return;
6694
6695 pos = curwin->w_cursor;
6696 old = ml_get_curline();
6697
6698 /* may remove added space */
6699 check_auto_format(FALSE);
6700
6701 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6702 * user might insert normal text next. Also skip formatting when "1" is
6703 * in 'formatoptions' and there is a single character before the cursor.
6704 * Otherwise the line would be broken and when typing another non-white
6705 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006706 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 if (*old != NUL && !trailblank && wasatend)
6708 {
6709 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006710 cc = gchar_cursor();
6711 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6712 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006714 cc = gchar_cursor();
6715 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 {
6717 curwin->w_cursor = pos;
6718 return;
6719 }
6720 curwin->w_cursor = pos;
6721 }
6722
6723#ifdef FEAT_COMMENTS
6724 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6725 * comments. */
6726 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006727 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 return;
6729#endif
6730
6731 /*
6732 * May start formatting in a previous line, so that after "x" a word is
6733 * moved to the previous line if it fits there now. Only when this is not
6734 * the start of a paragraph.
6735 */
6736 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6737 {
6738 --curwin->w_cursor.lnum;
6739 if (u_save_cursor() == FAIL)
6740 return;
6741 }
6742
6743 /*
6744 * Do the formatting and restore the cursor position. "saved_cursor" will
6745 * be adjusted for the text formatting.
6746 */
6747 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006748 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 curwin->w_cursor = saved_cursor;
6750 saved_cursor.lnum = 0;
6751
6752 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6753 {
6754 /* "cannot happen" */
6755 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6756 coladvance((colnr_T)MAXCOL);
6757 }
6758 else
6759 check_cursor_col();
6760
6761 /* Insert mode: If the cursor is now after the end of the line while it
6762 * previously wasn't, the line was broken. Because of the rule above we
6763 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6764 * formatted. */
6765 if (!wasatend && has_format_option(FO_WHITE_PAR))
6766 {
6767 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006768 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 if (curwin->w_cursor.col == len)
6770 {
6771 pnew = vim_strnsave(new, len + 2);
6772 pnew[len] = ' ';
6773 pnew[len + 1] = NUL;
6774 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6775 /* remove the space later */
6776 did_add_space = TRUE;
6777 }
6778 else
6779 /* may remove added space */
6780 check_auto_format(FALSE);
6781 }
6782
6783 check_cursor();
6784}
6785
6786/*
6787 * When an extra space was added to continue a paragraph for auto-formatting,
6788 * delete it now. The space must be under the cursor, just after the insert
6789 * position.
6790 */
6791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006792check_auto_format(
6793 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
6795 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006796 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
6798 if (did_add_space)
6799 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006800 cc = gchar_cursor();
6801 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 /* Somehow the space was removed already. */
6803 did_add_space = FALSE;
6804 else
6805 {
6806 if (!end_insert)
6807 {
6808 inc_cursor();
6809 c = gchar_cursor();
6810 dec_cursor();
6811 }
6812 if (c != NUL)
6813 {
6814 /* The space is no longer at the end of the line, delete it. */
6815 del_char(FALSE);
6816 did_add_space = FALSE;
6817 }
6818 }
6819 }
6820}
6821
6822/*
6823 * Find out textwidth to be used for formatting:
6824 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006825 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 * if invalid value, use 0.
6827 * Set default to window width (maximum 79) for "gq" operator.
6828 */
6829 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006830comp_textwidth(
6831 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832{
6833 int textwidth;
6834
6835 textwidth = curbuf->b_p_tw;
6836 if (textwidth == 0 && curbuf->b_p_wm)
6837 {
6838 /* The width is the window width minus 'wrapmargin' minus all the
6839 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006840 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841#ifdef FEAT_CMDWIN
6842 if (cmdwin_type != 0)
6843 textwidth -= 1;
6844#endif
6845#ifdef FEAT_FOLDING
6846 textwidth -= curwin->w_p_fdc;
6847#endif
6848#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006849 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 textwidth -= 1;
6851#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006852 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 textwidth -= 8;
6854 }
6855 if (textwidth < 0)
6856 textwidth = 0;
6857 if (ff && textwidth == 0)
6858 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006859 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 if (textwidth > 79)
6861 textwidth = 79;
6862 }
6863 return textwidth;
6864}
6865
6866/*
6867 * Put a character in the redo buffer, for when just after a CTRL-V.
6868 */
6869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006870redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871{
6872 char_u buf[10];
6873
6874 /* Only digits need special treatment. Translate them into a string of
6875 * three digits. */
6876 if (VIM_ISDIGIT(c))
6877 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006878 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 AppendToRedobuff(buf);
6880 }
6881 else
6882 AppendCharToRedobuff(c);
6883}
6884
6885/*
6886 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006887 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 */
6889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006890start_arrow(
6891 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006893 start_arrow_common(end_insert_pos, TRUE);
6894}
6895
6896/*
6897 * Like start_arrow() but with end_change argument.
6898 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6899 */
6900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006901start_arrow_with_change(
6902 pos_T *end_insert_pos, /* can be NULL */
6903 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006904{
6905 start_arrow_common(end_insert_pos, end_change);
6906 if (!end_change)
6907 {
6908 AppendCharToRedobuff(Ctrl_G);
6909 AppendCharToRedobuff('U');
6910 }
6911}
6912
6913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006914start_arrow_common(
6915 pos_T *end_insert_pos, /* can be NULL */
6916 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006917{
6918 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 {
6920 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006921 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 arrow_used = TRUE; /* this means we stopped the current insert */
6923 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006924#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006925 check_spell_redraw();
6926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927}
6928
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006929#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006930/*
6931 * If we skipped highlighting word at cursor, do it now.
6932 * It may be skipped again, thus reset spell_redraw_lnum first.
6933 */
6934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006935check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006936{
6937 if (spell_redraw_lnum != 0)
6938 {
6939 linenr_T lnum = spell_redraw_lnum;
6940
6941 spell_redraw_lnum = 0;
6942 redrawWinline(lnum, FALSE);
6943 }
6944}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006945
6946/*
6947 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6948 * spelled word, if there is one.
6949 */
6950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006951spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006952{
6953 pos_T tpos = curwin->w_cursor;
6954
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006955 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006956 if (curwin->w_cursor.col != tpos.col)
6957 start_arrow(&tpos);
6958}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006959#endif
6960
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961/*
6962 * stop_arrow() is called before a change is made in insert mode.
6963 * If an arrow key has been used, start a new insertion.
6964 * Returns FAIL if undo is impossible, shouldn't insert then.
6965 */
6966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006967stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968{
6969 if (arrow_used)
6970 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006971 Insstart = curwin->w_cursor; /* new insertion starts here */
6972 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6973 /* Don't update the original insert position when moved to the
6974 * right, except when nothing was inserted yet. */
6975 update_Insstart_orig = FALSE;
6976 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6977
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 if (u_save_cursor() == OK)
6979 {
6980 arrow_used = FALSE;
6981 ins_need_undo = FALSE;
6982 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006983
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 ai_col = 0;
6985#ifdef FEAT_VREPLACE
6986 if (State & VREPLACE_FLAG)
6987 {
6988 orig_line_count = curbuf->b_ml.ml_line_count;
6989 vr_lines_changed = 1;
6990 }
6991#endif
6992 ResetRedobuff();
6993 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006994 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 }
6996 else if (ins_need_undo)
6997 {
6998 if (u_save_cursor() == OK)
6999 ins_need_undo = FALSE;
7000 }
7001
7002#ifdef FEAT_FOLDING
7003 /* Always open fold at the cursor line when inserting something. */
7004 foldOpenCursor();
7005#endif
7006
7007 return (arrow_used || ins_need_undo ? FAIL : OK);
7008}
7009
7010/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007011 * Do a few things to stop inserting.
7012 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7013 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 */
7015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007016stop_insert(
7017 pos_T *end_insert_pos,
7018 int esc, /* called by ins_esc() */
7019 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007021 int cc;
7022 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
7024 stop_redo_ins();
7025 replace_flush(); /* abandon replace stack */
7026
7027 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007028 * Save the inserted text for later redo with ^@ and CTRL-A.
7029 * Don't do it when "restart_edit" was set and nothing was inserted,
7030 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007032 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007033 if (did_restart_edit == 0 || (ptr != NULL
7034 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007035 {
7036 vim_free(last_insert);
7037 last_insert = ptr;
7038 last_insert_skip = new_insert_skip;
7039 }
7040 else
7041 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007043 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {
7045 /* Auto-format now. It may seem strange to do this when stopping an
7046 * insertion (or moving the cursor), but it's required when appending
7047 * a line and having it end in a space. But only do it when something
7048 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007049 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007051 pos_T tpos = curwin->w_cursor;
7052
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 /* When the cursor is at the end of the line after a space the
7054 * formatting will move it to the following word. Avoid that by
7055 * moving the cursor onto the space. */
7056 cc = 'x';
7057 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7058 {
7059 dec_cursor();
7060 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007061 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007062 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 }
7064
7065 auto_format(TRUE, FALSE);
7066
Bram Moolenaar1c465442017-03-12 20:10:05 +01007067 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007068 {
7069 if (gchar_cursor() != NUL)
7070 inc_cursor();
7071#ifdef FEAT_VIRTUALEDIT
7072 /* If the cursor is still at the same character, also keep
7073 * the "coladd". */
7074 if (gchar_cursor() == NUL
7075 && curwin->w_cursor.lnum == tpos.lnum
7076 && curwin->w_cursor.col == tpos.col)
7077 curwin->w_cursor.coladd = tpos.coladd;
7078#endif
7079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
7081
7082 /* If a space was inserted for auto-formatting, remove it now. */
7083 check_auto_format(TRUE);
7084
7085 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007086 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007087 * Do this when ESC was used or moving the cursor up/down.
7088 * Check for the old position still being valid, just in case the text
7089 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007090 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007091 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7092 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007094 pos_T tpos = curwin->w_cursor;
7095
7096 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007097 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007098 for (;;)
7099 {
7100 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7101 --curwin->w_cursor.col;
7102 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007103 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007104 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007105 if (del_char(TRUE) == FAIL)
7106 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007107 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007108 if (curwin->w_cursor.lnum != tpos.lnum)
7109 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007110 else
7111 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007112 /* reset tpos, could have been invalidated in the loop above */
7113 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007114 tpos.col++;
7115 if (cc != NUL && gchar_pos(&tpos) == NUL)
7116 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 /* <C-S-Right> may have started Visual mode, adjust the position for
7120 * deleted characters. */
7121 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7122 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007123 int len = (int)STRLEN(ml_get_curline());
7124
7125 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007127 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007128#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 }
7132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 }
7134 }
7135 did_ai = FALSE;
7136#ifdef FEAT_SMARTINDENT
7137 did_si = FALSE;
7138 can_si = FALSE;
7139 can_si_back = FALSE;
7140#endif
7141
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007142 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7143 * now in a different buffer. */
7144 if (end_insert_pos != NULL)
7145 {
7146 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007147 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007148 curbuf->b_op_end = *end_insert_pos;
7149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150}
7151
7152/*
7153 * Set the last inserted text to a single character.
7154 * Used for the replace command.
7155 */
7156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007157set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158{
7159 char_u *s;
7160
7161 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 if (last_insert != NULL)
7164 {
7165 s = last_insert;
7166 /* Use the CTRL-V only when entering a special char */
7167 if (c < ' ' || c == DEL)
7168 *s++ = Ctrl_V;
7169 s = add_char2buf(c, s);
7170 *s++ = ESC;
7171 *s++ = NUL;
7172 last_insert_skip = 0;
7173 }
7174}
7175
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007176#if defined(EXITFREE) || defined(PROTO)
7177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007178free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007179{
7180 vim_free(last_insert);
7181 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007182# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007183 vim_free(compl_orig_text);
7184 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007185# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007186}
7187#endif
7188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189/*
7190 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7191 * and CSI. Handle multi-byte characters.
7192 * Returns a pointer to after the added bytes.
7193 */
7194 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007195add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196{
7197#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007198 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 int i;
7200 int len;
7201
7202 len = (*mb_char2bytes)(c, temp);
7203 for (i = 0; i < len; ++i)
7204 {
7205 c = temp[i];
7206#endif
7207 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7208 if (c == K_SPECIAL)
7209 {
7210 *s++ = K_SPECIAL;
7211 *s++ = KS_SPECIAL;
7212 *s++ = KE_FILLER;
7213 }
7214#ifdef FEAT_GUI
7215 else if (c == CSI)
7216 {
7217 *s++ = CSI;
7218 *s++ = KS_EXTRA;
7219 *s++ = (int)KE_CSI;
7220 }
7221#endif
7222 else
7223 *s++ = c;
7224#ifdef FEAT_MBYTE
7225 }
7226#endif
7227 return s;
7228}
7229
7230/*
7231 * move cursor to start of line
7232 * if flags & BL_WHITE move to first non-white
7233 * if flags & BL_SOL move to first non-white if startofline is set,
7234 * otherwise keep "curswant" column
7235 * if flags & BL_FIX don't leave the cursor on a NUL.
7236 */
7237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007238beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239{
7240 if ((flags & BL_SOL) && !p_sol)
7241 coladvance(curwin->w_curswant);
7242 else
7243 {
7244 curwin->w_cursor.col = 0;
7245#ifdef FEAT_VIRTUALEDIT
7246 curwin->w_cursor.coladd = 0;
7247#endif
7248
7249 if (flags & (BL_WHITE | BL_SOL))
7250 {
7251 char_u *ptr;
7252
Bram Moolenaar1c465442017-03-12 20:10:05 +01007253 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7255 ++curwin->w_cursor.col;
7256 }
7257 curwin->w_set_curswant = TRUE;
7258 }
7259}
7260
7261/*
7262 * oneright oneleft cursor_down cursor_up
7263 *
7264 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007265 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 * Return OK when successful, FAIL when we hit a line of file boundary.
7267 */
7268
7269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007270oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
7272 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
7275#ifdef FEAT_VIRTUALEDIT
7276 if (virtual_active())
7277 {
7278 pos_T prevpos = curwin->w_cursor;
7279
7280 /* Adjust for multi-wide char (excluding TAB) */
7281 ptr = ml_get_cursor();
7282 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007283# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007285# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 ))
7289 ? ptr2cells(ptr) : 1));
7290 curwin->w_set_curswant = TRUE;
7291 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7292 return (prevpos.col != curwin->w_cursor.col
7293 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7294 }
7295#endif
7296
7297 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007298 if (*ptr == NUL)
7299 return FAIL; /* already at the very end */
7300
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007302 if (has_mbyte)
7303 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 else
7305#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007306 l = 1;
7307
7308 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7309 * contains "onemore". */
7310 if (ptr[l] == NUL
7311#ifdef FEAT_VIRTUALEDIT
7312 && (ve_flags & VE_ONEMORE) == 0
7313#endif
7314 )
7315 return FAIL;
7316 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317
7318 curwin->w_set_curswant = TRUE;
7319 return OK;
7320}
7321
7322 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007323oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
7325#ifdef FEAT_VIRTUALEDIT
7326 if (virtual_active())
7327 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007328# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 int v = getviscol();
7332
7333 if (v == 0)
7334 return FAIL;
7335
7336# ifdef FEAT_LINEBREAK
7337 /* We might get stuck on 'showbreak', skip over it. */
7338 width = 1;
7339 for (;;)
7340 {
7341 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007342 /* getviscol() is slow, skip it when 'showbreak' is empty,
7343 * 'breakindent' is not set and there are no multi-byte
7344 * characters */
7345 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346# ifdef FEAT_MBYTE
7347 && !has_mbyte
7348# endif
7349 ) || getviscol() < v)
7350 break;
7351 ++width;
7352 }
7353# else
7354 coladvance(v - 1);
7355# endif
7356
7357 if (curwin->w_cursor.coladd == 1)
7358 {
7359 char_u *ptr;
7360
7361 /* Adjust for multi-wide char (not a TAB) */
7362 ptr = ml_get_cursor();
7363 if (*ptr != TAB && vim_isprintc(
7364# ifdef FEAT_MBYTE
7365 (*mb_ptr2char)(ptr)
7366# else
7367 *ptr
7368# endif
7369 ) && ptr2cells(ptr) > 1)
7370 curwin->w_cursor.coladd = 0;
7371 }
7372
7373 curwin->w_set_curswant = TRUE;
7374 return OK;
7375 }
7376#endif
7377
7378 if (curwin->w_cursor.col == 0)
7379 return FAIL;
7380
7381 curwin->w_set_curswant = TRUE;
7382 --curwin->w_cursor.col;
7383
7384#ifdef FEAT_MBYTE
7385 /* if the character on the left of the current cursor is a multi-byte
7386 * character, move to its first byte */
7387 if (has_mbyte)
7388 mb_adjust_cursor();
7389#endif
7390 return OK;
7391}
7392
7393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007394cursor_up(
7395 long n,
7396 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
7398 linenr_T lnum;
7399
7400 if (n > 0)
7401 {
7402 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007403 /* This fails if the cursor is already in the first line or the count
7404 * is larger than the line number and '-' is in 'cpoptions' */
7405 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 return FAIL;
7407 if (n >= lnum)
7408 lnum = 1;
7409 else
7410#ifdef FEAT_FOLDING
7411 if (hasAnyFolding(curwin))
7412 {
7413 /*
7414 * Count each sequence of folded lines as one logical line.
7415 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007416 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 (void)hasFolding(lnum, &lnum, NULL);
7418
7419 while (n--)
7420 {
7421 /* move up one line */
7422 --lnum;
7423 if (lnum <= 1)
7424 break;
7425 /* If we entered a fold, move to the beginning, unless in
7426 * Insert mode or when 'foldopen' contains "all": it will open
7427 * in a moment. */
7428 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7429 (void)hasFolding(lnum, &lnum, NULL);
7430 }
7431 if (lnum < 1)
7432 lnum = 1;
7433 }
7434 else
7435#endif
7436 lnum -= n;
7437 curwin->w_cursor.lnum = lnum;
7438 }
7439
7440 /* try to advance to the column we want to be at */
7441 coladvance(curwin->w_curswant);
7442
7443 if (upd_topline)
7444 update_topline(); /* make sure curwin->w_topline is valid */
7445
7446 return OK;
7447}
7448
7449/*
7450 * Cursor down a number of logical lines.
7451 */
7452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007453cursor_down(
7454 long n,
7455 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456{
7457 linenr_T lnum;
7458
7459 if (n > 0)
7460 {
7461 lnum = curwin->w_cursor.lnum;
7462#ifdef FEAT_FOLDING
7463 /* Move to last line of fold, will fail if it's the end-of-file. */
7464 (void)hasFolding(lnum, NULL, &lnum);
7465#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007466 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007467 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007468 if (lnum >= curbuf->b_ml.ml_line_count
7469 || (lnum + n > curbuf->b_ml.ml_line_count
7470 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 return FAIL;
7472 if (lnum + n >= curbuf->b_ml.ml_line_count)
7473 lnum = curbuf->b_ml.ml_line_count;
7474 else
7475#ifdef FEAT_FOLDING
7476 if (hasAnyFolding(curwin))
7477 {
7478 linenr_T last;
7479
7480 /* count each sequence of folded lines as one logical line */
7481 while (n--)
7482 {
7483 if (hasFolding(lnum, NULL, &last))
7484 lnum = last + 1;
7485 else
7486 ++lnum;
7487 if (lnum >= curbuf->b_ml.ml_line_count)
7488 break;
7489 }
7490 if (lnum > curbuf->b_ml.ml_line_count)
7491 lnum = curbuf->b_ml.ml_line_count;
7492 }
7493 else
7494#endif
7495 lnum += n;
7496 curwin->w_cursor.lnum = lnum;
7497 }
7498
7499 /* try to advance to the column we want to be at */
7500 coladvance(curwin->w_curswant);
7501
7502 if (upd_topline)
7503 update_topline(); /* make sure curwin->w_topline is valid */
7504
7505 return OK;
7506}
7507
7508/*
7509 * Stuff the last inserted text in the read buffer.
7510 * Last_insert actually is a copy of the redo buffer, so we
7511 * first have to remove the command.
7512 */
7513 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007514stuff_inserted(
7515 int c, /* Command character to be inserted */
7516 long count, /* Repeat this many times */
7517 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518{
7519 char_u *esc_ptr;
7520 char_u *ptr;
7521 char_u *last_ptr;
7522 char_u last = NUL;
7523
7524 ptr = get_last_insert();
7525 if (ptr == NULL)
7526 {
7527 EMSG(_(e_noinstext));
7528 return FAIL;
7529 }
7530
7531 /* may want to stuff the command character, to start Insert mode */
7532 if (c != NUL)
7533 stuffcharReadbuff(c);
7534 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7535 *esc_ptr = NUL; /* remove the ESC */
7536
7537 /* when the last char is either "0" or "^" it will be quoted if no ESC
7538 * comes after it OR if it will inserted more than once and "ptr"
7539 * starts with ^D. -- Acevedo
7540 */
7541 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7542 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7543 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7544 {
7545 last = *last_ptr;
7546 *last_ptr = NUL;
7547 }
7548
7549 do
7550 {
7551 stuffReadbuff(ptr);
7552 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7553 if (last)
7554 stuffReadbuff((char_u *)(last == '0'
7555 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7556 : IF_EB("\026^", CTRL_V_STR "^")));
7557 }
7558 while (--count > 0);
7559
7560 if (last)
7561 *last_ptr = last;
7562
7563 if (esc_ptr != NULL)
7564 *esc_ptr = ESC; /* put the ESC back */
7565
7566 /* may want to stuff a trailing ESC, to get out of Insert mode */
7567 if (!no_esc)
7568 stuffcharReadbuff(ESC);
7569
7570 return OK;
7571}
7572
7573 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007574get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
7576 if (last_insert == NULL)
7577 return NULL;
7578 return last_insert + last_insert_skip;
7579}
7580
7581/*
7582 * Get last inserted string, and remove trailing <Esc>.
7583 * Returns pointer to allocated memory (must be freed) or NULL.
7584 */
7585 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007586get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587{
7588 char_u *s;
7589 int len;
7590
7591 if (last_insert == NULL)
7592 return NULL;
7593 s = vim_strsave(last_insert + last_insert_skip);
7594 if (s != NULL)
7595 {
7596 len = (int)STRLEN(s);
7597 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7598 s[len - 1] = NUL;
7599 }
7600 return s;
7601}
7602
7603/*
7604 * Check the word in front of the cursor for an abbreviation.
7605 * Called when the non-id character "c" has been entered.
7606 * When an abbreviation is recognized it is removed from the text and
7607 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7608 */
7609 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007610echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611{
7612 /* Don't check for abbreviation in paste mode, when disabled and just
7613 * after moving around with cursor keys. */
7614 if (p_paste || no_abbr || arrow_used)
7615 return FALSE;
7616
7617 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7618 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7619}
7620
7621/*
7622 * replace-stack functions
7623 *
7624 * When replacing characters, the replaced characters are remembered for each
7625 * new character. This is used to re-insert the old text when backspacing.
7626 *
7627 * There is a NUL headed list of characters for each character that is
7628 * currently in the file after the insertion point. When BS is used, one NUL
7629 * headed list is put back for the deleted character.
7630 *
7631 * For a newline, there are two NUL headed lists. One contains the characters
7632 * that the NL replaced. The extra one stores the characters after the cursor
7633 * that were deleted (always white space).
7634 *
7635 * Replace_offset is normally 0, in which case replace_push will add a new
7636 * character at the end of the stack. If replace_offset is not 0, that many
7637 * characters will be left on the stack above the newly inserted character.
7638 */
7639
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007640static char_u *replace_stack = NULL;
7641static long replace_stack_nr = 0; /* next entry in replace stack */
7642static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643
7644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007645replace_push(
7646 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
7648 char_u *p;
7649
7650 if (replace_stack_nr < replace_offset) /* nothing to do */
7651 return;
7652 if (replace_stack_len <= replace_stack_nr)
7653 {
7654 replace_stack_len += 50;
7655 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7656 if (p == NULL) /* out of memory */
7657 {
7658 replace_stack_len -= 50;
7659 return;
7660 }
7661 if (replace_stack != NULL)
7662 {
7663 mch_memmove(p, replace_stack,
7664 (size_t)(replace_stack_nr * sizeof(char_u)));
7665 vim_free(replace_stack);
7666 }
7667 replace_stack = p;
7668 }
7669 p = replace_stack + replace_stack_nr - replace_offset;
7670 if (replace_offset)
7671 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7672 *p = c;
7673 ++replace_stack_nr;
7674}
7675
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007676#if defined(FEAT_MBYTE) || defined(PROTO)
7677/*
7678 * Push a character onto the replace stack. Handles a multi-byte character in
7679 * reverse byte order, so that the first byte is popped off first.
7680 * Return the number of bytes done (includes composing characters).
7681 */
7682 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007683replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007684{
7685 int l = (*mb_ptr2len)(p);
7686 int j;
7687
7688 for (j = l - 1; j >= 0; --j)
7689 replace_push(p[j]);
7690 return l;
7691}
7692#endif
7693
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694/*
7695 * Pop one item from the replace stack.
7696 * return -1 if stack empty
7697 * return replaced character or NUL otherwise
7698 */
7699 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007700replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
7702 if (replace_stack_nr == 0)
7703 return -1;
7704 return (int)replace_stack[--replace_stack_nr];
7705}
7706
7707/*
7708 * Join the top two items on the replace stack. This removes to "off"'th NUL
7709 * encountered.
7710 */
7711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007712replace_join(
7713 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714{
7715 int i;
7716
7717 for (i = replace_stack_nr; --i >= 0; )
7718 if (replace_stack[i] == NUL && off-- <= 0)
7719 {
7720 --replace_stack_nr;
7721 mch_memmove(replace_stack + i, replace_stack + i + 1,
7722 (size_t)(replace_stack_nr - i));
7723 return;
7724 }
7725}
7726
7727/*
7728 * Pop bytes from the replace stack until a NUL is found, and insert them
7729 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7730 */
7731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007732replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734 int cc;
7735 int oldState = State;
7736
7737 State = NORMAL; /* don't want REPLACE here */
7738 while ((cc = replace_pop()) > 0)
7739 {
7740#ifdef FEAT_MBYTE
7741 mb_replace_pop_ins(cc);
7742#else
7743 ins_char(cc);
7744#endif
7745 dec_cursor();
7746 }
7747 State = oldState;
7748}
7749
7750#ifdef FEAT_MBYTE
7751/*
7752 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7753 * indicates a multi-byte char, pop the other bytes too.
7754 */
7755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007756mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757{
7758 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007759 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 int i;
7761 int c;
7762
7763 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7764 {
7765 buf[0] = cc;
7766 for (i = 1; i < n; ++i)
7767 buf[i] = replace_pop();
7768 ins_bytes_len(buf, n);
7769 }
7770 else
7771 ins_char(cc);
7772
7773 if (enc_utf8)
7774 /* Handle composing chars. */
7775 for (;;)
7776 {
7777 c = replace_pop();
7778 if (c == -1) /* stack empty */
7779 break;
7780 if ((n = MB_BYTE2LEN(c)) == 1)
7781 {
7782 /* Not a multi-byte char, put it back. */
7783 replace_push(c);
7784 break;
7785 }
7786 else
7787 {
7788 buf[0] = c;
7789 for (i = 1; i < n; ++i)
7790 buf[i] = replace_pop();
7791 if (utf_iscomposing(utf_ptr2char(buf)))
7792 ins_bytes_len(buf, n);
7793 else
7794 {
7795 /* Not a composing char, put it back. */
7796 for (i = n - 1; i >= 0; --i)
7797 replace_push(buf[i]);
7798 break;
7799 }
7800 }
7801 }
7802}
7803#endif
7804
7805/*
7806 * make the replace stack empty
7807 * (called when exiting replace mode)
7808 */
7809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007810replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811{
7812 vim_free(replace_stack);
7813 replace_stack = NULL;
7814 replace_stack_len = 0;
7815 replace_stack_nr = 0;
7816}
7817
7818/*
7819 * Handle doing a BS for one character.
7820 * cc < 0: replace stack empty, just move cursor
7821 * cc == 0: character was inserted, delete it
7822 * cc > 0: character was replaced, put cc (first byte of original char) back
7823 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007824 * When "limit_col" is >= 0, don't delete before this column. Matters when
7825 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 */
7827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007828replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829{
7830 int cc;
7831#ifdef FEAT_VREPLACE
7832 int orig_len = 0;
7833 int ins_len;
7834 int orig_vcols = 0;
7835 colnr_T start_vcol;
7836 char_u *p;
7837 int i;
7838 int vcol;
7839#endif
7840
7841 cc = replace_pop();
7842 if (cc > 0)
7843 {
7844#ifdef FEAT_VREPLACE
7845 if (State & VREPLACE_FLAG)
7846 {
7847 /* Get the number of screen cells used by the character we are
7848 * going to delete. */
7849 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7850 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7851 }
7852#endif
7853#ifdef FEAT_MBYTE
7854 if (has_mbyte)
7855 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007856 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857# ifdef FEAT_VREPLACE
7858 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007859 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860# endif
7861 replace_push(cc);
7862 }
7863 else
7864#endif
7865 {
7866 pchar_cursor(cc);
7867#ifdef FEAT_VREPLACE
7868 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007869 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870#endif
7871 }
7872 replace_pop_ins();
7873
7874#ifdef FEAT_VREPLACE
7875 if (State & VREPLACE_FLAG)
7876 {
7877 /* Get the number of screen cells used by the inserted characters */
7878 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007879 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 vcol = start_vcol;
7881 for (i = 0; i < ins_len; ++i)
7882 {
7883 vcol += chartabsize(p + i, vcol);
7884#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007885 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886#endif
7887 }
7888 vcol -= start_vcol;
7889
7890 /* Delete spaces that were inserted after the cursor to keep the
7891 * text aligned. */
7892 curwin->w_cursor.col += ins_len;
7893 while (vcol > orig_vcols && gchar_cursor() == ' ')
7894 {
7895 del_char(FALSE);
7896 ++orig_vcols;
7897 }
7898 curwin->w_cursor.col -= ins_len;
7899 }
7900#endif
7901
7902 /* mark the buffer as changed and prepare for displaying */
7903 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7904 }
7905 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007906 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907}
7908
7909#ifdef FEAT_CINDENT
7910/*
7911 * Return TRUE if C-indenting is on.
7912 */
7913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007914cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916 return (!p_paste && (curbuf->b_p_cin
7917# ifdef FEAT_EVAL
7918 || *curbuf->b_p_inde != NUL
7919# endif
7920 ));
7921}
7922#endif
7923
7924#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7925/*
7926 * Re-indent the current line, based on the current contents of it and the
7927 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7928 * confused what all the part that handles Control-T is doing that I'm not.
7929 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7930 */
7931
7932 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007933fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007935 int amount = get_the_indent();
7936
7937 if (amount >= 0)
7938 {
7939 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7940 if (linewhite(curwin->w_cursor.lnum))
7941 did_ai = TRUE; /* delete the indent if the line stays empty */
7942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943}
7944
7945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007946fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947{
7948 if (p_paste)
7949 return;
7950# ifdef FEAT_LISP
7951 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7952 fixthisline(get_lisp_indent);
7953# endif
7954# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7955 else
7956# endif
7957# ifdef FEAT_CINDENT
7958 if (cindent_on())
7959 do_c_expr_indent();
7960# endif
7961}
7962
7963#endif
7964
7965#ifdef FEAT_CINDENT
7966/*
7967 * return TRUE if 'cinkeys' contains the key "keytyped",
7968 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007969 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7971 *
7972 * "keytyped" can have a few special values:
7973 * KEY_OPEN_FORW
7974 * KEY_OPEN_BACK
7975 * KEY_COMPLETE just finished completion.
7976 *
7977 * If line_is_empty is TRUE accept keys with '0' before them.
7978 */
7979 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007980in_cinkeys(
7981 int keytyped,
7982 int when,
7983 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984{
7985 char_u *look;
7986 int try_match;
7987 int try_match_word;
7988 char_u *p;
7989 char_u *line;
7990 int icase;
7991 int i;
7992
Bram Moolenaar30848942009-12-24 14:46:12 +00007993 if (keytyped == NUL)
7994 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7995 return FALSE;
7996
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997#ifdef FEAT_EVAL
7998 if (*curbuf->b_p_inde != NUL)
7999 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8000 else
8001#endif
8002 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8003 while (*look)
8004 {
8005 /*
8006 * Find out if we want to try a match with this key, depending on
8007 * 'when' and a '*' or '!' before the key.
8008 */
8009 switch (when)
8010 {
8011 case '*': try_match = (*look == '*'); break;
8012 case '!': try_match = (*look == '!'); break;
8013 default: try_match = (*look != '*'); break;
8014 }
8015 if (*look == '*' || *look == '!')
8016 ++look;
8017
8018 /*
8019 * If there is a '0', only accept a match if the line is empty.
8020 * But may still match when typing last char of a word.
8021 */
8022 if (*look == '0')
8023 {
8024 try_match_word = try_match;
8025 if (!line_is_empty)
8026 try_match = FALSE;
8027 ++look;
8028 }
8029 else
8030 try_match_word = FALSE;
8031
8032 /*
8033 * does it look like a control character?
8034 */
8035 if (*look == '^'
8036#ifdef EBCDIC
8037 && (Ctrl_chr(look[1]) != 0)
8038#else
8039 && look[1] >= '?' && look[1] <= '_'
8040#endif
8041 )
8042 {
8043 if (try_match && keytyped == Ctrl_chr(look[1]))
8044 return TRUE;
8045 look += 2;
8046 }
8047 /*
8048 * 'o' means "o" command, open forward.
8049 * 'O' means "O" command, open backward.
8050 */
8051 else if (*look == 'o')
8052 {
8053 if (try_match && keytyped == KEY_OPEN_FORW)
8054 return TRUE;
8055 ++look;
8056 }
8057 else if (*look == 'O')
8058 {
8059 if (try_match && keytyped == KEY_OPEN_BACK)
8060 return TRUE;
8061 ++look;
8062 }
8063
8064 /*
8065 * 'e' means to check for "else" at start of line and just before the
8066 * cursor.
8067 */
8068 else if (*look == 'e')
8069 {
8070 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8071 {
8072 p = ml_get_curline();
8073 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8074 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8075 return TRUE;
8076 }
8077 ++look;
8078 }
8079
8080 /*
8081 * ':' only causes an indent if it is at the end of a label or case
8082 * statement, or when it was before typing the ':' (to fix
8083 * class::method for C++).
8084 */
8085 else if (*look == ':')
8086 {
8087 if (try_match && keytyped == ':')
8088 {
8089 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008090 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008092 /* Need to get the line again after cin_islabel(). */
8093 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 if (curwin->w_cursor.col > 2
8095 && p[curwin->w_cursor.col - 1] == ':'
8096 && p[curwin->w_cursor.col - 2] == ':')
8097 {
8098 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008099 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008100 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 p = ml_get_curline();
8102 p[curwin->w_cursor.col - 1] = ':';
8103 if (i)
8104 return TRUE;
8105 }
8106 }
8107 ++look;
8108 }
8109
8110
8111 /*
8112 * Is it a key in <>, maybe?
8113 */
8114 else if (*look == '<')
8115 {
8116 if (try_match)
8117 {
8118 /*
8119 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8120 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8121 * >, *, : and ! keys if they really really want to.
8122 */
8123 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8124 && keytyped == look[1])
8125 return TRUE;
8126
8127 if (keytyped == get_special_key_code(look + 1))
8128 return TRUE;
8129 }
8130 while (*look && *look != '>')
8131 look++;
8132 while (*look == '>')
8133 look++;
8134 }
8135
8136 /*
8137 * Is it a word: "=word"?
8138 */
8139 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8140 {
8141 ++look;
8142 if (*look == '~')
8143 {
8144 icase = TRUE;
8145 ++look;
8146 }
8147 else
8148 icase = FALSE;
8149 p = vim_strchr(look, ',');
8150 if (p == NULL)
8151 p = look + STRLEN(look);
8152 if ((try_match || try_match_word)
8153 && curwin->w_cursor.col >= (colnr_T)(p - look))
8154 {
8155 int match = FALSE;
8156
8157#ifdef FEAT_INS_EXPAND
8158 if (keytyped == KEY_COMPLETE)
8159 {
8160 char_u *s;
8161
8162 /* Just completed a word, check if it starts with "look".
8163 * search back for the start of a word. */
8164 line = ml_get_curline();
8165# ifdef FEAT_MBYTE
8166 if (has_mbyte)
8167 {
8168 char_u *n;
8169
8170 for (s = line + curwin->w_cursor.col; s > line; s = n)
8171 {
8172 n = mb_prevptr(line, s);
8173 if (!vim_iswordp(n))
8174 break;
8175 }
8176 }
8177 else
8178# endif
8179 for (s = line + curwin->w_cursor.col; s > line; --s)
8180 if (!vim_iswordc(s[-1]))
8181 break;
8182 if (s + (p - look) <= line + curwin->w_cursor.col
8183 && (icase
8184 ? MB_STRNICMP(s, look, p - look)
8185 : STRNCMP(s, look, p - look)) == 0)
8186 match = TRUE;
8187 }
8188 else
8189#endif
8190 /* TODO: multi-byte */
8191 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8192 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8193 {
8194 line = ml_get_cursor();
8195 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8196 || !vim_iswordc(line[-(p - look) - 1]))
8197 && (icase
8198 ? MB_STRNICMP(line - (p - look), look, p - look)
8199 : STRNCMP(line - (p - look), look, p - look))
8200 == 0)
8201 match = TRUE;
8202 }
8203 if (match && try_match_word && !try_match)
8204 {
8205 /* "0=word": Check if there are only blanks before the
8206 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008207 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 (int)(curwin->w_cursor.col - (p - look)))
8209 match = FALSE;
8210 }
8211 if (match)
8212 return TRUE;
8213 }
8214 look = p;
8215 }
8216
8217 /*
8218 * ok, it's a boring generic character.
8219 */
8220 else
8221 {
8222 if (try_match && *look == keytyped)
8223 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008224 if (*look != NUL)
8225 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 }
8227
8228 /*
8229 * Skip over ", ".
8230 */
8231 look = skip_to_option_part(look);
8232 }
8233 return FALSE;
8234}
8235#endif /* FEAT_CINDENT */
8236
8237#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8238/*
8239 * Map Hebrew keyboard when in hkmap mode.
8240 */
8241 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008242hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243{
8244 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8245 {
8246 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8247 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8248 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8249 static char_u map[26] =
8250 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8251 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8252 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8253 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8254 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8255 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8256 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8257 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8258 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8259
8260 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8261 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8262 /* '-1'='sofit' */
8263 else if (c == 'x')
8264 return 'X';
8265 else if (c == 'q')
8266 return '\''; /* {geresh}={'} */
8267 else if (c == 246)
8268 return ' '; /* \"o --> ' ' for a german keyboard */
8269 else if (c == 228)
8270 return ' '; /* \"a --> ' ' -- / -- */
8271 else if (c == 252)
8272 return ' '; /* \"u --> ' ' -- / -- */
8273#ifdef EBCDIC
8274 else if (islower(c))
8275#else
8276 /* NOTE: islower() does not do the right thing for us on Linux so we
8277 * do this the same was as 5.7 and previous, so it works correctly on
8278 * all systems. Specifically, the e.g. Delete and Arrow keys are
8279 * munged and won't work if e.g. searching for Hebrew text.
8280 */
8281 else if (c >= 'a' && c <= 'z')
8282#endif
8283 return (int)(map[CharOrdLow(c)] + p_aleph);
8284 else
8285 return c;
8286 }
8287 else
8288 {
8289 switch (c)
8290 {
8291 case '`': return ';';
8292 case '/': return '.';
8293 case '\'': return ',';
8294 case 'q': return '/';
8295 case 'w': return '\'';
8296
8297 /* Hebrew letters - set offset from 'a' */
8298 case ',': c = '{'; break;
8299 case '.': c = 'v'; break;
8300 case ';': c = 't'; break;
8301 default: {
8302 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8303
8304#ifdef EBCDIC
8305 /* see note about islower() above */
8306 if (!islower(c))
8307#else
8308 if (c < 'a' || c > 'z')
8309#endif
8310 return c;
8311 c = str[CharOrdLow(c)];
8312 break;
8313 }
8314 }
8315
8316 return (int)(CharOrdLow(c) + p_aleph);
8317 }
8318}
8319#endif
8320
8321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008322ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323{
8324 int need_redraw = FALSE;
8325 int regname;
8326 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008327 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328
8329 /*
8330 * If we are going to wait for a character, show a '"'.
8331 */
8332 pc_status = PC_STATUS_UNSET;
8333 if (redrawing() && !char_avail())
8334 {
8335 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008336 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337
8338 edit_putchar('"', TRUE);
8339#ifdef FEAT_CMDL_INFO
8340 add_to_showcmd_c(Ctrl_R);
8341#endif
8342 }
8343
8344#ifdef USE_ON_FLY_SCROLL
8345 dont_scroll = TRUE; /* disallow scrolling here */
8346#endif
8347
8348 /*
8349 * Don't map the register name. This also prevents the mode message to be
8350 * deleted when ESC is hit.
8351 */
8352 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008353 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8356 {
8357 /* Get a third key for literal register insertion */
8358 literally = regname;
8359#ifdef FEAT_CMDL_INFO
8360 add_to_showcmd_c(literally);
8361#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008362 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 }
8365 --no_mapping;
8366
8367#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008368 /* Don't call u_sync() while typing the expression or giving an error
8369 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 ++no_u_sync;
8371 if (regname == '=')
8372 {
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008373# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008375# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008376 /* Sync undo when evaluating the expression calls setline() or
8377 * append(), so that it can be undone separately. */
8378 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008379
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 regname = get_expr_register();
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008381# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 /* Restore the Input Method. */
8383 if (im_on)
8384 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008387 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8388 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008389 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 else
8393 {
8394#endif
8395 if (literally == Ctrl_O || literally == Ctrl_P)
8396 {
8397 /* Append the command to the redo buffer. */
8398 AppendCharToRedobuff(Ctrl_R);
8399 AppendCharToRedobuff(literally);
8400 AppendCharToRedobuff(regname);
8401
8402 do_put(regname, BACKWARD, 1L,
8403 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8404 }
8405 else if (insert_reg(regname, literally) == FAIL)
8406 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008407 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 need_redraw = TRUE; /* remove the '"' */
8409 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008410 else if (stop_insert_mode)
8411 /* When the '=' register was used and a function was invoked that
8412 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8413 * insert anything, need to remove the '"' */
8414 need_redraw = TRUE;
8415
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416#ifdef FEAT_EVAL
8417 }
8418 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008419 if (u_sync_once == 1)
8420 ins_need_undo = TRUE;
8421 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422#endif
8423#ifdef FEAT_CMDL_INFO
8424 clear_showcmd();
8425#endif
8426
8427 /* If the inserted register is empty, we need to remove the '"' */
8428 if (need_redraw || stuff_empty())
8429 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008430
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008431 /* Disallow starting Visual mode here, would get a weird mode. */
8432 if (!vis_active && VIsual_active)
8433 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434}
8435
8436/*
8437 * CTRL-G commands in Insert mode.
8438 */
8439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008440ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441{
8442 int c;
8443
8444#ifdef FEAT_INS_EXPAND
8445 /* Right after CTRL-X the cursor will be after the ruler. */
8446 setcursor();
8447#endif
8448
8449 /*
8450 * Don't map the second key. This also prevents the mode message to be
8451 * deleted when ESC is hit.
8452 */
8453 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008454 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 --no_mapping;
8456 switch (c)
8457 {
8458 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8459 case K_UP:
8460 case Ctrl_K:
8461 case 'k': ins_up(TRUE);
8462 break;
8463
8464 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8465 case K_DOWN:
8466 case Ctrl_J:
8467 case 'j': ins_down(TRUE);
8468 break;
8469
8470 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008471 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008473
8474 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008475 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008476 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008477 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 break;
8479
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008480 /* CTRL-G U: do not break undo with the next char */
8481 case 'U':
8482 /* Allow one left/right cursor movement with the next char,
8483 * without breaking undo. */
8484 dont_sync_undo = MAYBE;
8485 break;
8486
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008488 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 }
8490}
8491
8492/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008493 * CTRL-^ in Insert mode.
8494 */
8495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008496ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008497{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008498 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008499 {
8500 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8501 if (State & LANGMAP)
8502 {
8503 curbuf->b_p_iminsert = B_IMODE_NONE;
8504 State &= ~LANGMAP;
8505 }
8506 else
8507 {
8508 curbuf->b_p_iminsert = B_IMODE_LMAP;
8509 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008510#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008511 im_set_active(FALSE);
8512#endif
8513 }
8514 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008515#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008516 else
8517 {
8518 /* There are no ":lmap" mappings, toggle IM */
8519 if (im_get_status())
8520 {
8521 curbuf->b_p_iminsert = B_IMODE_NONE;
8522 im_set_active(FALSE);
8523 }
8524 else
8525 {
8526 curbuf->b_p_iminsert = B_IMODE_IM;
8527 State &= ~LANGMAP;
8528 im_set_active(TRUE);
8529 }
8530 }
8531#endif
8532 set_iminsert_global();
8533 showmode();
8534#ifdef FEAT_GUI
8535 /* may show different cursor shape or color */
8536 if (gui.in_use)
8537 gui_update_cursor(TRUE, FALSE);
8538#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008539#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008540 /* Show/unshow value of 'keymap' in status lines. */
8541 status_redraw_curbuf();
8542#endif
8543}
8544
8545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 * Handle ESC in insert mode.
8547 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8548 * insert.
8549 */
8550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008551ins_esc(
8552 long *count,
8553 int cmdchar,
8554 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556 int temp;
8557 static int disabled_redraw = FALSE;
8558
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008559#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008560 check_spell_redraw();
8561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562#if defined(FEAT_HANGULIN)
8563# if defined(ESC_CHG_TO_ENG_MODE)
8564 hangul_input_state_set(0);
8565# endif
8566 if (composing_hangul)
8567 {
8568 push_raw_key(composing_hangul_buffer, 2);
8569 composing_hangul = 0;
8570 }
8571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572
8573 temp = curwin->w_cursor.col;
8574 if (disabled_redraw)
8575 {
8576 --RedrawingDisabled;
8577 disabled_redraw = FALSE;
8578 }
8579 if (!arrow_used)
8580 {
8581 /*
8582 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008583 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8584 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 */
8586 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008587 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588
8589 /*
8590 * Repeating insert may take a long time. Check for
8591 * interrupt now and then.
8592 */
8593 if (*count > 0)
8594 {
8595 line_breakcheck();
8596 if (got_int)
8597 *count = 0;
8598 }
8599
8600 if (--*count > 0) /* repeat what was typed */
8601 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008602 /* Vi repeats the insert without replacing characters. */
8603 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8604 State &= ~REPLACE_FLAG;
8605
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 (void)start_redo_ins();
8607 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008608 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 ++RedrawingDisabled;
8610 disabled_redraw = TRUE;
8611 return FALSE; /* repeat the insert */
8612 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008613 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 undisplay_dollar();
8615 }
8616
8617 /* When an autoindent was removed, curswant stays after the
8618 * indent */
8619 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8620 curwin->w_set_curswant = TRUE;
8621
8622 /* Remember the last Insert position in the '^ mark. */
8623 if (!cmdmod.keepjumps)
8624 curbuf->b_last_insert = curwin->w_cursor;
8625
8626 /*
8627 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008628 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008630 if (!nomove
8631 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632#ifdef FEAT_VIRTUALEDIT
8633 || curwin->w_cursor.coladd > 0
8634#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008635 )
8636 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008637 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638#ifdef FEAT_RIGHTLEFT
8639 && !revins_on
8640#endif
8641 )
8642 {
8643#ifdef FEAT_VIRTUALEDIT
8644 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8645 {
8646 oneleft();
8647 if (restart_edit != NUL)
8648 ++curwin->w_cursor.coladd;
8649 }
8650 else
8651#endif
8652 {
8653 --curwin->w_cursor.col;
8654#ifdef FEAT_MBYTE
8655 /* Correct cursor for multi-byte character. */
8656 if (has_mbyte)
8657 mb_adjust_cursor();
8658#endif
8659 }
8660 }
8661
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008662#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 /* Disable IM to allow typing English directly for Normal mode commands.
8664 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8665 * well). */
8666 if (!(State & LANGMAP))
8667 im_save_status(&curbuf->b_p_iminsert);
8668 im_set_active(FALSE);
8669#endif
8670
8671 State = NORMAL;
8672 /* need to position cursor again (e.g. when on a TAB ) */
8673 changed_cline_bef_curs();
8674
8675#ifdef FEAT_MOUSE
8676 setmouse();
8677#endif
8678#ifdef CURSOR_SHAPE
8679 ui_cursor_shape(); /* may show different cursor shape */
8680#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008681 if (!p_ek)
8682 /* Re-enable bracketed paste mode. */
8683 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684
8685 /*
8686 * When recording or for CTRL-O, need to display the new mode.
8687 * Otherwise remove the mode message.
8688 */
8689 if (Recording || restart_edit != NUL)
8690 showmode();
8691 else if (p_smd)
8692 MSG("");
8693
8694 return TRUE; /* exit Insert mode */
8695}
8696
8697#ifdef FEAT_RIGHTLEFT
8698/*
8699 * Toggle language: hkmap and revins_on.
8700 * Move to end of reverse inserted text.
8701 */
8702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008703ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
8705 if (revins_on && revins_chars && revins_scol >= 0)
8706 {
8707 while (gchar_cursor() != NUL && revins_chars--)
8708 ++curwin->w_cursor.col;
8709 }
8710 p_ri = !p_ri;
8711 revins_on = (State == INSERT && p_ri);
8712 if (revins_on)
8713 {
8714 revins_scol = curwin->w_cursor.col;
8715 revins_legal++;
8716 revins_chars = 0;
8717 undisplay_dollar();
8718 }
8719 else
8720 revins_scol = -1;
8721#ifdef FEAT_FKMAP
8722 if (p_altkeymap)
8723 {
8724 /*
8725 * to be consistent also for redo command, using '.'
8726 * set arrow_used to true and stop it - causing to redo
8727 * characters entered in one mode (normal/reverse insert).
8728 */
8729 arrow_used = TRUE;
8730 (void)stop_arrow();
8731 p_fkmap = curwin->w_p_rl ^ p_ri;
8732 if (p_fkmap && p_ri)
8733 State = INSERT;
8734 }
8735 else
8736#endif
8737 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8738 showmode();
8739}
8740#endif
8741
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742/*
8743 * If 'keymodel' contains "startsel", may start selection.
8744 * Returns TRUE when a CTRL-O and other keys stuffed.
8745 */
8746 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008747ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
8749 if (km_startsel)
8750 switch (c)
8751 {
8752 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 case K_PAGEUP:
8755 case K_KPAGEUP:
8756 case K_PAGEDOWN:
8757 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008758# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 case K_LEFT:
8760 case K_RIGHT:
8761 case K_UP:
8762 case K_DOWN:
8763 case K_END:
8764 case K_HOME:
8765# endif
8766 if (!(mod_mask & MOD_MASK_SHIFT))
8767 break;
8768 /* FALLTHROUGH */
8769 case K_S_LEFT:
8770 case K_S_RIGHT:
8771 case K_S_UP:
8772 case K_S_DOWN:
8773 case K_S_END:
8774 case K_S_HOME:
8775 /* Start selection right away, the cursor can move with
8776 * CTRL-O when beyond the end of the line. */
8777 start_selection();
8778
8779 /* Execute the key in (insert) Select mode. */
8780 stuffcharReadbuff(Ctrl_O);
8781 if (mod_mask)
8782 {
8783 char_u buf[4];
8784
8785 buf[0] = K_SPECIAL;
8786 buf[1] = KS_MODIFIER;
8787 buf[2] = mod_mask;
8788 buf[3] = NUL;
8789 stuffReadbuff(buf);
8790 }
8791 stuffcharReadbuff(c);
8792 return TRUE;
8793 }
8794 return FALSE;
8795}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796
8797/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008798 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008799 */
8800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008801ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008802{
8803#ifdef FEAT_FKMAP
8804 if (p_fkmap && p_ri)
8805 {
8806 beep_flush();
8807 EMSG(farsi_text_3); /* encoded in Farsi */
8808 return;
8809 }
8810#endif
8811
8812#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008813# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008814 set_vim_var_string(VV_INSERTMODE,
8815 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008816# ifdef FEAT_VREPLACE
8817 replaceState == VREPLACE ? "v" :
8818# endif
8819 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008820# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008821 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8822#endif
8823 if (State & REPLACE_FLAG)
8824 State = INSERT | (State & LANGMAP);
8825 else
8826 State = replaceState | (State & LANGMAP);
8827 AppendCharToRedobuff(K_INS);
8828 showmode();
8829#ifdef CURSOR_SHAPE
8830 ui_cursor_shape(); /* may show different cursor shape */
8831#endif
8832}
8833
8834/*
8835 * Pressed CTRL-O in Insert mode.
8836 */
8837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008838ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008839{
8840#ifdef FEAT_VREPLACE
8841 if (State & VREPLACE_FLAG)
8842 restart_edit = 'V';
8843 else
8844#endif
8845 if (State & REPLACE_FLAG)
8846 restart_edit = 'R';
8847 else
8848 restart_edit = 'I';
8849#ifdef FEAT_VIRTUALEDIT
8850 if (virtual_active())
8851 ins_at_eol = FALSE; /* cursor always keeps its column */
8852 else
8853#endif
8854 ins_at_eol = (gchar_cursor() == NUL);
8855}
8856
8857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 * If the cursor is on an indent, ^T/^D insert/delete one
8859 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008860 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 * with vi. But vi only supports ^T and ^D after an
8862 * autoindent, we support it everywhere.
8863 */
8864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008865ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
8867 if (stop_arrow() == FAIL)
8868 return;
8869 AppendCharToRedobuff(c);
8870
8871 /*
8872 * 0^D and ^^D: remove all indent.
8873 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008874 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8875 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 {
8877 --curwin->w_cursor.col;
8878 (void)del_char(FALSE); /* delete the '^' or '0' */
8879 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8880 if (State & REPLACE_FLAG)
8881 replace_pop_ins();
8882 if (lastc == '^')
8883 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008884 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 }
8886 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008887 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888
8889 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8890 did_ai = FALSE;
8891#ifdef FEAT_SMARTINDENT
8892 did_si = FALSE;
8893 can_si = FALSE;
8894 can_si_back = FALSE;
8895#endif
8896#ifdef FEAT_CINDENT
8897 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8898#endif
8899}
8900
8901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008902ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903{
8904 int temp;
8905
8906 if (stop_arrow() == FAIL)
8907 return;
8908 if (gchar_cursor() == NUL) /* delete newline */
8909 {
8910 temp = curwin->w_cursor.col;
8911 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008912 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008913 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 else
8915 curwin->w_cursor.col = temp;
8916 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008917 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8918 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 did_ai = FALSE;
8920#ifdef FEAT_SMARTINDENT
8921 did_si = FALSE;
8922 can_si = FALSE;
8923 can_si_back = FALSE;
8924#endif
8925 AppendCharToRedobuff(K_DEL);
8926}
8927
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008928static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008929
8930/*
8931 * Delete one character for ins_bs().
8932 */
8933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008934ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008935{
8936 dec_cursor();
8937 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8938 if (State & REPLACE_FLAG)
8939 {
8940 /* Don't delete characters before the insert point when in
8941 * Replace mode */
8942 if (curwin->w_cursor.lnum != Insstart.lnum
8943 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008944 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008945 }
8946 else
8947 (void)del_char(FALSE);
8948}
8949
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950/*
8951 * Handle Backspace, delete-word and delete-line in Insert mode.
8952 * Return TRUE when backspace was actually used.
8953 */
8954 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008955ins_bs(
8956 int c,
8957 int mode,
8958 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959{
8960 linenr_T lnum;
8961 int cc;
8962 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008963 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 colnr_T mincol;
8965 int did_backspace = FALSE;
8966 int in_indent;
8967 int oldState;
8968#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008969 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970#endif
8971
8972 /*
8973 * can't delete anything in an empty file
8974 * can't backup past first character in buffer
8975 * can't backup past starting point unless 'backspace' > 1
8976 * can backup to a previous line if 'backspace' == 0
8977 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008978 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 || (
8980#ifdef FEAT_RIGHTLEFT
8981 !revins_on &&
8982#endif
8983 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8984 || (!can_bs(BS_START)
8985 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008986 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8987 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8989 && curwin->w_cursor.col <= ai_col)
8990 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8991 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008992 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 return FALSE;
8994 }
8995
8996 if (stop_arrow() == FAIL)
8997 return FALSE;
8998 in_indent = inindent(0);
8999#ifdef FEAT_CINDENT
9000 if (in_indent)
9001 can_cindent = FALSE;
9002#endif
9003#ifdef FEAT_COMMENTS
9004 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9005#endif
9006#ifdef FEAT_RIGHTLEFT
9007 if (revins_on) /* put cursor after last inserted char */
9008 inc_cursor();
9009#endif
9010
9011#ifdef FEAT_VIRTUALEDIT
9012 /* Virtualedit:
9013 * BACKSPACE_CHAR eats a virtual space
9014 * BACKSPACE_WORD eats all coladd
9015 * BACKSPACE_LINE eats all coladd and keeps going
9016 */
9017 if (curwin->w_cursor.coladd > 0)
9018 {
9019 if (mode == BACKSPACE_CHAR)
9020 {
9021 --curwin->w_cursor.coladd;
9022 return TRUE;
9023 }
9024 if (mode == BACKSPACE_WORD)
9025 {
9026 curwin->w_cursor.coladd = 0;
9027 return TRUE;
9028 }
9029 curwin->w_cursor.coladd = 0;
9030 }
9031#endif
9032
9033 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009034 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 */
9036 if (curwin->w_cursor.col == 0)
9037 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009038 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009039 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040#ifdef FEAT_RIGHTLEFT
9041 || revins_on
9042#endif
9043 )
9044 {
9045 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9046 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9047 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009048 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009049 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 }
9051 /*
9052 * In replace mode:
9053 * cc < 0: NL was inserted, delete it
9054 * cc >= 0: NL was replaced, put original characters back
9055 */
9056 cc = -1;
9057 if (State & REPLACE_FLAG)
9058 cc = replace_pop(); /* returns -1 if NL was inserted */
9059 /*
9060 * In replace mode, in the line we started replacing, we only move the
9061 * cursor.
9062 */
9063 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9064 {
9065 dec_cursor();
9066 }
9067 else
9068 {
9069#ifdef FEAT_VREPLACE
9070 if (!(State & VREPLACE_FLAG)
9071 || curwin->w_cursor.lnum > orig_line_count)
9072#endif
9073 {
9074 temp = gchar_cursor(); /* remember current char */
9075 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009076
9077 /* When "aw" is in 'formatoptions' we must delete the space at
9078 * the end of the line, otherwise the line will be broken
9079 * again when auto-formatting. */
9080 if (has_format_option(FO_AUTO)
9081 && has_format_option(FO_WHITE_PAR))
9082 {
9083 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9084 TRUE);
9085 int len;
9086
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009087 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009088 if (len > 0 && ptr[len - 1] == ' ')
9089 ptr[len - 1] = NUL;
9090 }
9091
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009092 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 if (temp == NUL && gchar_cursor() != NUL)
9094 inc_cursor();
9095 }
9096#ifdef FEAT_VREPLACE
9097 else
9098 dec_cursor();
9099#endif
9100
9101 /*
9102 * In REPLACE mode we have to put back the text that was replaced
9103 * by the NL. On the replace stack is first a NUL-terminated
9104 * sequence of characters that were deleted and then the
9105 * characters that NL replaced.
9106 */
9107 if (State & REPLACE_FLAG)
9108 {
9109 /*
9110 * Do the next ins_char() in NORMAL state, to
9111 * prevent ins_char() from replacing characters and
9112 * avoiding showmatch().
9113 */
9114 oldState = State;
9115 State = NORMAL;
9116 /*
9117 * restore characters (blanks) deleted after cursor
9118 */
9119 while (cc > 0)
9120 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009121 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122#ifdef FEAT_MBYTE
9123 mb_replace_pop_ins(cc);
9124#else
9125 ins_char(cc);
9126#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009127 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 cc = replace_pop();
9129 }
9130 /* restore the characters that NL replaced */
9131 replace_pop_ins();
9132 State = oldState;
9133 }
9134 }
9135 did_ai = FALSE;
9136 }
9137 else
9138 {
9139 /*
9140 * Delete character(s) before the cursor.
9141 */
9142#ifdef FEAT_RIGHTLEFT
9143 if (revins_on) /* put cursor on last inserted char */
9144 dec_cursor();
9145#endif
9146 mincol = 0;
9147 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009148 if (mode == BACKSPACE_LINE
9149 && (curbuf->b_p_ai
9150#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009151 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009152#endif
9153 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154#ifdef FEAT_RIGHTLEFT
9155 && !revins_on
9156#endif
9157 )
9158 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009159 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009161 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009163 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 }
9165
9166 /*
9167 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9168 */
9169 if ( mode == BACKSPACE_CHAR
9170 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009171 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009172 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 && (*(ml_get_cursor() - 1) == TAB
9174 || (*(ml_get_cursor() - 1) == ' '
9175 && (!*inserted_space_p
9176 || arrow_used))))))
9177 {
9178 int ts;
9179 colnr_T vcol;
9180 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009181 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182
9183 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009184 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009185 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009187 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 /* Compute the virtual column where we want to be. Since
9189 * 'showbreak' may get in the way, need to get the last column of
9190 * the previous character. */
9191 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009192 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 dec_cursor();
9194 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9195 inc_cursor();
9196 want_vcol = (want_vcol / ts) * ts;
9197
9198 /* delete characters until we are at or before want_vcol */
9199 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009200 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009201 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202
9203 /* insert extra spaces until we are at want_vcol */
9204 while (vcol < want_vcol)
9205 {
9206 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009207 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9208 && curwin->w_cursor.col < Insstart_orig.col)
9209 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210
9211#ifdef FEAT_VREPLACE
9212 if (State & VREPLACE_FLAG)
9213 ins_char(' ');
9214 else
9215#endif
9216 {
9217 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009218 if ((State & REPLACE_FLAG))
9219 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 }
9221 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9222 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009223
9224 /* If we are now back where we started delete one character. Can
9225 * happen when using 'sts' and 'linebreak'. */
9226 if (vcol >= start_vcol)
9227 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 }
9229
9230 /*
9231 * Delete upto starting point, start of line or previous word.
9232 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009233 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009235#ifdef FEAT_MBYTE
9236 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009238 if (has_mbyte)
9239 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009241 do
9242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009244 if (!revins_on) /* put cursor on char to be deleted */
9245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009247
9248 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009250 /* look multi-byte character class */
9251 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009253 prev_cclass = cclass;
9254 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009257
9258 /* start of word? */
9259 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9260 {
9261 mode = BACKSPACE_WORD_NOT_SPACE;
9262 temp = vim_iswordc(cc);
9263 }
9264 /* end of word? */
9265 else if (mode == BACKSPACE_WORD_NOT_SPACE
9266 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9267#ifdef FEAT_MBYTE
9268 || prev_cclass != cclass
9269#endif
9270 ))
9271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009273 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009275 inc_cursor();
9276#ifdef FEAT_RIGHTLEFT
9277 else if (State & REPLACE_FLAG)
9278 dec_cursor();
9279#endif
9280 break;
9281 }
9282 if (State & REPLACE_FLAG)
9283 replace_do_bs(-1);
9284 else
9285 {
9286#ifdef FEAT_MBYTE
9287 if (enc_utf8 && p_deco)
9288 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9289#endif
9290 (void)del_char(FALSE);
9291#ifdef FEAT_MBYTE
9292 /*
9293 * If there are combining characters and 'delcombine' is set
9294 * move the cursor back. Don't back up before the base
9295 * character.
9296 */
9297 if (enc_utf8 && p_deco && cpc[0] != NUL)
9298 inc_cursor();
9299#endif
9300#ifdef FEAT_RIGHTLEFT
9301 if (revins_chars)
9302 {
9303 revins_chars--;
9304 revins_legal++;
9305 }
9306 if (revins_on && gchar_cursor() == NUL)
9307 break;
9308#endif
9309 }
9310 /* Just a single backspace?: */
9311 if (mode == BACKSPACE_CHAR)
9312 break;
9313 } while (
9314#ifdef FEAT_RIGHTLEFT
9315 revins_on ||
9316#endif
9317 (curwin->w_cursor.col > mincol
9318 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9319 || curwin->w_cursor.col != Insstart_orig.col)));
9320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 did_backspace = TRUE;
9322 }
9323#ifdef FEAT_SMARTINDENT
9324 did_si = FALSE;
9325 can_si = FALSE;
9326 can_si_back = FALSE;
9327#endif
9328 if (curwin->w_cursor.col <= 1)
9329 did_ai = FALSE;
9330 /*
9331 * It's a little strange to put backspaces into the redo
9332 * buffer, but it makes auto-indent a lot easier to deal
9333 * with.
9334 */
9335 AppendCharToRedobuff(c);
9336
9337 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009338 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9339 && curwin->w_cursor.col < Insstart_orig.col)
9340 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341
9342 /* vi behaviour: the cursor moves backward but the character that
9343 * was there remains visible
9344 * Vim behaviour: the cursor moves backward and the character that
9345 * was there is erased from the screen.
9346 * We can emulate the vi behaviour by pretending there is a dollar
9347 * displayed even when there isn't.
9348 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009349 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 dollar_vcol = curwin->w_virtcol;
9351
Bram Moolenaarce3be472008-01-14 19:12:28 +00009352#ifdef FEAT_FOLDING
9353 /* When deleting a char the cursor line must never be in a closed fold.
9354 * E.g., when 'foldmethod' is indent and deleting the first non-white
9355 * char before a Tab. */
9356 if (did_backspace)
9357 foldOpenCursor();
9358#endif
9359
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 return did_backspace;
9361}
9362
9363#ifdef FEAT_MOUSE
9364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009365ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366{
9367 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009368 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
9370# ifdef FEAT_GUI
9371 /* When GUI is active, also move/paste when 'mouse' is empty */
9372 if (!gui.in_use)
9373# endif
9374 if (!mouse_has(MOUSE_INSERT))
9375 return;
9376
9377 undisplay_dollar();
9378 tpos = curwin->w_cursor;
9379 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9380 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009381 win_T *new_curwin = curwin;
9382
9383 if (curwin != old_curwin && win_valid(old_curwin))
9384 {
9385 /* Mouse took us to another window. We need to go back to the
9386 * previous one to stop insert there properly. */
9387 curwin = old_curwin;
9388 curbuf = curwin->w_buffer;
9389 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009390 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009391 if (curwin != new_curwin && win_valid(new_curwin))
9392 {
9393 curwin = new_curwin;
9394 curbuf = curwin->w_buffer;
9395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396# ifdef FEAT_CINDENT
9397 can_cindent = TRUE;
9398# endif
9399 }
9400
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 /* redraw status lines (in case another window became active) */
9402 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403}
9404
9405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009406ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
9408 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009409 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009410# ifdef FEAT_INS_EXPAND
9411 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412# endif
9413
9414 tpos = curwin->w_cursor;
9415
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009416 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 {
9418 int row, col;
9419
9420 row = mouse_row;
9421 col = mouse_col;
9422
9423 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009424 wp = mouse_find_win(&row, &col);
9425 if (wp == NULL)
9426 return;
9427 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 curbuf = curwin->w_buffer;
9429 }
9430 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 undisplay_dollar();
9432
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009433# ifdef FEAT_INS_EXPAND
9434 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009435 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009436# endif
9437 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009438 if (dir == MSCR_DOWN || dir == MSCR_UP)
9439 {
9440 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9441 scroll_redraw(dir,
9442 (long)(curwin->w_botline - curwin->w_topline));
9443 else
9444 scroll_redraw(dir, 3L);
9445 }
9446#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009447 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009448 {
9449 int val, step = 6;
9450
9451 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009452 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009453 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9454 if (val < 0)
9455 val = 0;
9456 gui_do_horiz_scroll(val, TRUE);
9457 }
9458#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009459# ifdef FEAT_INS_EXPAND
9460 did_scroll = TRUE;
9461# endif
9462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 curwin->w_redr_status = TRUE;
9465
9466 curwin = old_curwin;
9467 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009469# ifdef FEAT_INS_EXPAND
9470 /* The popup menu may overlay the window, need to redraw it.
9471 * TODO: Would be more efficient to only redraw the windows that are
9472 * overlapped by the popup menu. */
9473 if (pum_visible() && did_scroll)
9474 {
9475 redraw_all_later(NOT_VALID);
9476 ins_compl_show_pum();
9477 }
9478# endif
9479
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009480 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 {
9482 start_arrow(&tpos);
9483# ifdef FEAT_CINDENT
9484 can_cindent = TRUE;
9485# endif
9486 }
9487}
9488#endif
9489
Bram Moolenaarec2da362017-01-21 20:04:22 +01009490/*
9491 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9492 * P_PE literally.
9493 * When "drop" is TRUE then consume the text and drop it.
9494 */
9495 int
9496bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9497{
9498 int c;
9499 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9500 int idx = 0;
9501 char_u *end = find_termcode((char_u *)"PE");
9502 int ret_char = -1;
9503 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009504 int save_paste = p_paste;
9505 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009506
9507 /* If the end code is too long we can't detect it, read everything. */
9508 if (STRLEN(end) >= NUMBUFLEN)
9509 end = NULL;
9510 ++no_mapping;
9511 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009512 p_paste = TRUE;
9513 curbuf->b_p_ai = FALSE;
9514
Bram Moolenaarec2da362017-01-21 20:04:22 +01009515 for (;;)
9516 {
9517 /* When the end is not defined read everything. */
9518 if (end == NULL && vpeekc() == NUL)
9519 break;
9520 c = plain_vgetc();
9521#ifdef FEAT_MBYTE
9522 if (has_mbyte)
9523 idx += (*mb_char2bytes)(c, buf + idx);
9524 else
9525#endif
9526 buf[idx++] = c;
9527 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009528 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009529 {
9530 if (end[idx] == NUL)
9531 break; /* Found the end of paste code. */
9532 continue;
9533 }
9534 if (!drop)
9535 {
9536 switch (mode)
9537 {
9538 case PASTE_CMDLINE:
9539 put_on_cmdline(buf, idx, TRUE);
9540 break;
9541
9542 case PASTE_EX:
9543 if (gap != NULL && ga_grow(gap, idx) == OK)
9544 {
9545 mch_memmove((char *)gap->ga_data + gap->ga_len,
9546 buf, (size_t)idx);
9547 gap->ga_len += idx;
9548 }
9549 break;
9550
9551 case PASTE_INSERT:
9552 if (stop_arrow() == OK)
9553 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009554 c = buf[0];
9555 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9556 ins_eol(c);
9557 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009558 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009559 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009560 AppendToRedobuffLit(buf, idx);
9561 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009562 }
9563 break;
9564
9565 case PASTE_ONE_CHAR:
9566 if (ret_char == -1)
9567 {
9568#ifdef FEAT_MBYTE
9569 if (has_mbyte)
9570 ret_char = (*mb_ptr2char)(buf);
9571 else
9572#endif
9573 ret_char = buf[0];
9574 }
9575 break;
9576 }
9577 }
9578 idx = 0;
9579 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009580
Bram Moolenaarec2da362017-01-21 20:04:22 +01009581 --no_mapping;
9582 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009583 p_paste = save_paste;
9584 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009585
9586 return ret_char;
9587}
9588
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009589#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009591ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009592{
9593 /* We will be leaving the current window, unless closing another tab. */
9594 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9595 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9596 {
9597 undisplay_dollar();
9598 start_arrow(&curwin->w_cursor);
9599# ifdef FEAT_CINDENT
9600 can_cindent = TRUE;
9601# endif
9602 }
9603
9604 if (c == K_TABLINE)
9605 goto_tabpage(current_tab);
9606 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009607 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009608 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009609 redraw_statuslines(); /* will redraw the tabline when needed */
9610 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009611}
9612#endif
9613
9614#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009616ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617{
9618 pos_T tpos;
9619
9620 undisplay_dollar();
9621 tpos = curwin->w_cursor;
9622 if (gui_do_scroll())
9623 {
9624 start_arrow(&tpos);
9625# ifdef FEAT_CINDENT
9626 can_cindent = TRUE;
9627# endif
9628 }
9629}
9630
9631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
9634 pos_T tpos;
9635
9636 undisplay_dollar();
9637 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009638 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 {
9640 start_arrow(&tpos);
9641# ifdef FEAT_CINDENT
9642 can_cindent = TRUE;
9643# endif
9644 }
9645}
9646#endif
9647
9648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009649ins_left(
9650 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
9652 pos_T tpos;
9653
9654#ifdef FEAT_FOLDING
9655 if ((fdo_flags & FDO_HOR) && KeyTyped)
9656 foldOpenCursor();
9657#endif
9658 undisplay_dollar();
9659 tpos = curwin->w_cursor;
9660 if (oneleft() == OK)
9661 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009662#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9663 /* Only call start_arrow() when not busy with preediting, it will
9664 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009665 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009666#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009667 {
9668 start_arrow_with_change(&tpos, end_change);
9669 if (!end_change)
9670 AppendCharToRedobuff(K_LEFT);
9671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672#ifdef FEAT_RIGHTLEFT
9673 /* If exit reversed string, position is fixed */
9674 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9675 revins_legal++;
9676 revins_chars++;
9677#endif
9678 }
9679
9680 /*
9681 * if 'whichwrap' set for cursor in insert mode may go to
9682 * previous line
9683 */
9684 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9685 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009686 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 start_arrow(&tpos);
9688 --(curwin->w_cursor.lnum);
9689 coladvance((colnr_T)MAXCOL);
9690 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9691 }
9692 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009693 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009694 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695}
9696
9697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009698ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
9700 pos_T tpos;
9701
9702#ifdef FEAT_FOLDING
9703 if ((fdo_flags & FDO_HOR) && KeyTyped)
9704 foldOpenCursor();
9705#endif
9706 undisplay_dollar();
9707 tpos = curwin->w_cursor;
9708 if (c == K_C_HOME)
9709 curwin->w_cursor.lnum = 1;
9710 curwin->w_cursor.col = 0;
9711#ifdef FEAT_VIRTUALEDIT
9712 curwin->w_cursor.coladd = 0;
9713#endif
9714 curwin->w_curswant = 0;
9715 start_arrow(&tpos);
9716}
9717
9718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009719ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
9721 pos_T tpos;
9722
9723#ifdef FEAT_FOLDING
9724 if ((fdo_flags & FDO_HOR) && KeyTyped)
9725 foldOpenCursor();
9726#endif
9727 undisplay_dollar();
9728 tpos = curwin->w_cursor;
9729 if (c == K_C_END)
9730 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9731 coladvance((colnr_T)MAXCOL);
9732 curwin->w_curswant = MAXCOL;
9733
9734 start_arrow(&tpos);
9735}
9736
9737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009738ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739{
9740#ifdef FEAT_FOLDING
9741 if ((fdo_flags & FDO_HOR) && KeyTyped)
9742 foldOpenCursor();
9743#endif
9744 undisplay_dollar();
9745 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9746 {
9747 start_arrow(&curwin->w_cursor);
9748 (void)bck_word(1L, FALSE, FALSE);
9749 curwin->w_set_curswant = TRUE;
9750 }
9751 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009752 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753}
9754
9755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009756ins_right(
9757 int end_change) /* end undoable change */
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();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009764 if (gchar_cursor() != NUL
9765#ifdef FEAT_VIRTUALEDIT
9766 || virtual_active()
9767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 )
9769 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009770 start_arrow_with_change(&curwin->w_cursor, end_change);
9771 if (!end_change)
9772 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 curwin->w_set_curswant = TRUE;
9774#ifdef FEAT_VIRTUALEDIT
9775 if (virtual_active())
9776 oneright();
9777 else
9778#endif
9779 {
9780#ifdef FEAT_MBYTE
9781 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009782 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 else
9784#endif
9785 ++curwin->w_cursor.col;
9786 }
9787
9788#ifdef FEAT_RIGHTLEFT
9789 revins_legal++;
9790 if (revins_chars)
9791 revins_chars--;
9792#endif
9793 }
9794 /* if 'whichwrap' set for cursor in insert mode, may move the
9795 * cursor to the next line */
9796 else if (vim_strchr(p_ww, ']') != NULL
9797 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9798 {
9799 start_arrow(&curwin->w_cursor);
9800 curwin->w_set_curswant = TRUE;
9801 ++curwin->w_cursor.lnum;
9802 curwin->w_cursor.col = 0;
9803 }
9804 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009805 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009806 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807}
9808
9809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009810ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811{
9812#ifdef FEAT_FOLDING
9813 if ((fdo_flags & FDO_HOR) && KeyTyped)
9814 foldOpenCursor();
9815#endif
9816 undisplay_dollar();
9817 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9818 || gchar_cursor() != NUL)
9819 {
9820 start_arrow(&curwin->w_cursor);
9821 (void)fwd_word(1L, FALSE, 0);
9822 curwin->w_set_curswant = TRUE;
9823 }
9824 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009825 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826}
9827
9828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009829ins_up(
9830 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831{
9832 pos_T tpos;
9833 linenr_T old_topline = curwin->w_topline;
9834#ifdef FEAT_DIFF
9835 int old_topfill = curwin->w_topfill;
9836#endif
9837
9838 undisplay_dollar();
9839 tpos = curwin->w_cursor;
9840 if (cursor_up(1L, TRUE) == OK)
9841 {
9842 if (startcol)
9843 coladvance(getvcol_nolist(&Insstart));
9844 if (old_topline != curwin->w_topline
9845#ifdef FEAT_DIFF
9846 || old_topfill != curwin->w_topfill
9847#endif
9848 )
9849 redraw_later(VALID);
9850 start_arrow(&tpos);
9851#ifdef FEAT_CINDENT
9852 can_cindent = TRUE;
9853#endif
9854 }
9855 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009856 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857}
9858
9859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009860ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861{
9862 pos_T tpos;
9863
9864 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009865
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009866 if (mod_mask & MOD_MASK_CTRL)
9867 {
9868 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009869 if (first_tabpage->tp_next != NULL)
9870 {
9871 start_arrow(&curwin->w_cursor);
9872 goto_tabpage(-1);
9873 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009874 return;
9875 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009876
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 tpos = curwin->w_cursor;
9878 if (onepage(BACKWARD, 1L) == OK)
9879 {
9880 start_arrow(&tpos);
9881#ifdef FEAT_CINDENT
9882 can_cindent = TRUE;
9883#endif
9884 }
9885 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009886 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887}
9888
9889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009890ins_down(
9891 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892{
9893 pos_T tpos;
9894 linenr_T old_topline = curwin->w_topline;
9895#ifdef FEAT_DIFF
9896 int old_topfill = curwin->w_topfill;
9897#endif
9898
9899 undisplay_dollar();
9900 tpos = curwin->w_cursor;
9901 if (cursor_down(1L, TRUE) == OK)
9902 {
9903 if (startcol)
9904 coladvance(getvcol_nolist(&Insstart));
9905 if (old_topline != curwin->w_topline
9906#ifdef FEAT_DIFF
9907 || old_topfill != curwin->w_topfill
9908#endif
9909 )
9910 redraw_later(VALID);
9911 start_arrow(&tpos);
9912#ifdef FEAT_CINDENT
9913 can_cindent = TRUE;
9914#endif
9915 }
9916 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009917 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918}
9919
9920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009921ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922{
9923 pos_T tpos;
9924
9925 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009926
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009927 if (mod_mask & MOD_MASK_CTRL)
9928 {
9929 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009930 if (first_tabpage->tp_next != NULL)
9931 {
9932 start_arrow(&curwin->w_cursor);
9933 goto_tabpage(0);
9934 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009935 return;
9936 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009937
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 tpos = curwin->w_cursor;
9939 if (onepage(FORWARD, 1L) == OK)
9940 {
9941 start_arrow(&tpos);
9942#ifdef FEAT_CINDENT
9943 can_cindent = TRUE;
9944#endif
9945 }
9946 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009947 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948}
9949
9950#ifdef FEAT_DND
9951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009952ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953{
9954 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9955}
9956#endif
9957
9958/*
9959 * Handle TAB in Insert or Replace mode.
9960 * Return TRUE when the TAB needs to be inserted like a normal character.
9961 */
9962 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009963ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
9965 int ind;
9966 int i;
9967 int temp;
9968
9969 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9970 Insstart_blank_vcol = get_nolist_virtcol();
9971 if (echeck_abbr(TAB + ABBR_OFF))
9972 return FALSE;
9973
9974 ind = inindent(0);
9975#ifdef FEAT_CINDENT
9976 if (ind)
9977 can_cindent = FALSE;
9978#endif
9979
9980 /*
9981 * When nothing special, insert TAB like a normal character
9982 */
9983 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009984 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009985 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 return TRUE;
9987
9988 if (stop_arrow() == FAIL)
9989 return TRUE;
9990
9991 did_ai = FALSE;
9992#ifdef FEAT_SMARTINDENT
9993 did_si = FALSE;
9994 can_si = FALSE;
9995 can_si_back = FALSE;
9996#endif
9997 AppendToRedobuff((char_u *)"\t");
9998
9999 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010000 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010001 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10002 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 else /* otherwise use 'tabstop' */
10004 temp = (int)curbuf->b_p_ts;
10005 temp -= get_nolist_virtcol() % temp;
10006
10007 /*
10008 * Insert the first space with ins_char(). It will delete one char in
10009 * replace mode. Insert the rest with ins_str(); it will not delete any
10010 * chars. For VREPLACE mode, we use ins_char() for all characters.
10011 */
10012 ins_char(' ');
10013 while (--temp > 0)
10014 {
10015#ifdef FEAT_VREPLACE
10016 if (State & VREPLACE_FLAG)
10017 ins_char(' ');
10018 else
10019#endif
10020 {
10021 ins_str((char_u *)" ");
10022 if (State & REPLACE_FLAG) /* no char replaced */
10023 replace_push(NUL);
10024 }
10025 }
10026
10027 /*
10028 * When 'expandtab' not set: Replace spaces by TABs where possible.
10029 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010030 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 {
10032 char_u *ptr;
10033#ifdef FEAT_VREPLACE
10034 char_u *saved_line = NULL; /* init for GCC */
10035 pos_T pos;
10036#endif
10037 pos_T fpos;
10038 pos_T *cursor;
10039 colnr_T want_vcol, vcol;
10040 int change_col = -1;
10041 int save_list = curwin->w_p_list;
10042
10043 /*
10044 * Get the current line. For VREPLACE mode, don't make real changes
10045 * yet, just work on a copy of the line.
10046 */
10047#ifdef FEAT_VREPLACE
10048 if (State & VREPLACE_FLAG)
10049 {
10050 pos = curwin->w_cursor;
10051 cursor = &pos;
10052 saved_line = vim_strsave(ml_get_curline());
10053 if (saved_line == NULL)
10054 return FALSE;
10055 ptr = saved_line + pos.col;
10056 }
10057 else
10058#endif
10059 {
10060 ptr = ml_get_cursor();
10061 cursor = &curwin->w_cursor;
10062 }
10063
10064 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10065 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10066 curwin->w_p_list = FALSE;
10067
10068 /* Find first white before the cursor */
10069 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010070 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 {
10072 --fpos.col;
10073 --ptr;
10074 }
10075
10076 /* In Replace mode, don't change characters before the insert point. */
10077 if ((State & REPLACE_FLAG)
10078 && fpos.lnum == Insstart.lnum
10079 && fpos.col < Insstart.col)
10080 {
10081 ptr += Insstart.col - fpos.col;
10082 fpos.col = Insstart.col;
10083 }
10084
10085 /* compute virtual column numbers of first white and cursor */
10086 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10087 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10088
Bram Moolenaar597a4222014-06-25 14:39:50 +020010089 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10090 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010091 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010093 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 if (vcol + i > want_vcol)
10095 break;
10096 if (*ptr != TAB)
10097 {
10098 *ptr = TAB;
10099 if (change_col < 0)
10100 {
10101 change_col = fpos.col; /* Column of first change */
10102 /* May have to adjust Insstart */
10103 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10104 Insstart.col = fpos.col;
10105 }
10106 }
10107 ++fpos.col;
10108 ++ptr;
10109 vcol += i;
10110 }
10111
10112 if (change_col >= 0)
10113 {
10114 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010115 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116
10117 /* Skip over the spaces we need. */
10118 while (vcol < want_vcol && *ptr == ' ')
10119 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010120 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 ++ptr;
10122 ++repl_off;
10123 }
10124 if (vcol > want_vcol)
10125 {
10126 /* Must have a char with 'showbreak' just before it. */
10127 --ptr;
10128 --repl_off;
10129 }
10130 fpos.col += repl_off;
10131
10132 /* Delete following spaces. */
10133 i = cursor->col - fpos.col;
10134 if (i > 0)
10135 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010136 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 /* correct replace stack. */
10138 if ((State & REPLACE_FLAG)
10139#ifdef FEAT_VREPLACE
10140 && !(State & VREPLACE_FLAG)
10141#endif
10142 )
10143 for (temp = i; --temp >= 0; )
10144 replace_join(repl_off);
10145 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010146#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010147 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010148 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010149 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010150 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10151 (char_u *)"\t", 1);
10152 }
10153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 cursor->col -= i;
10155
10156#ifdef FEAT_VREPLACE
10157 /*
10158 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10159 * backspacing over the changed spacing and then inserting the new
10160 * spacing.
10161 */
10162 if (State & VREPLACE_FLAG)
10163 {
10164 /* Backspace from real cursor to change_col */
10165 backspace_until_column(change_col);
10166
10167 /* Insert each char in saved_line from changed_col to
10168 * ptr-cursor */
10169 ins_bytes_len(saved_line + change_col,
10170 cursor->col - change_col);
10171 }
10172#endif
10173 }
10174
10175#ifdef FEAT_VREPLACE
10176 if (State & VREPLACE_FLAG)
10177 vim_free(saved_line);
10178#endif
10179 curwin->w_p_list = save_list;
10180 }
10181
10182 return FALSE;
10183}
10184
10185/*
10186 * Handle CR or NL in insert mode.
10187 * Return TRUE when out of memory or can't undo.
10188 */
10189 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010190ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191{
10192 int i;
10193
10194 if (echeck_abbr(c + ABBR_OFF))
10195 return FALSE;
10196 if (stop_arrow() == FAIL)
10197 return TRUE;
10198 undisplay_dollar();
10199
10200 /*
10201 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10202 * character under the cursor. Only push a NUL on the replace stack,
10203 * nothing to put back when the NL is deleted.
10204 */
10205 if ((State & REPLACE_FLAG)
10206#ifdef FEAT_VREPLACE
10207 && !(State & VREPLACE_FLAG)
10208#endif
10209 )
10210 replace_push(NUL);
10211
10212 /*
10213 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10214 * replacing the next line, so we push all of the characters left on the
10215 * line onto the replace stack. This is not done here though, it is done
10216 * in open_line().
10217 */
10218
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010219#ifdef FEAT_VIRTUALEDIT
10220 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10221 * CTRL-O). */
10222 if (virtual_active() && curwin->w_cursor.coladd > 0)
10223 coladvance(getviscol());
10224#endif
10225
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226#ifdef FEAT_RIGHTLEFT
10227# ifdef FEAT_FKMAP
10228 if (p_altkeymap && p_fkmap)
10229 fkmap(NL);
10230# endif
10231 /* NL in reverse insert will always start in the end of
10232 * current line. */
10233 if (revins_on)
10234 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10235#endif
10236
10237 AppendToRedobuff(NL_STR);
10238 i = open_line(FORWARD,
10239#ifdef FEAT_COMMENTS
10240 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10241#endif
10242 0, old_indent);
10243 old_indent = 0;
10244#ifdef FEAT_CINDENT
10245 can_cindent = TRUE;
10246#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010247#ifdef FEAT_FOLDING
10248 /* When inserting a line the cursor line must never be in a closed fold. */
10249 foldOpenCursor();
10250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251
10252 return (!i);
10253}
10254
10255#ifdef FEAT_DIGRAPHS
10256/*
10257 * Handle digraph in insert mode.
10258 * Returns character still to be inserted, or NUL when nothing remaining to be
10259 * done.
10260 */
10261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010262ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263{
10264 int c;
10265 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010266 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267
10268 pc_status = PC_STATUS_UNSET;
10269 if (redrawing() && !char_avail())
10270 {
10271 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010272 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273
10274 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010275 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276#ifdef FEAT_CMDL_INFO
10277 add_to_showcmd_c(Ctrl_K);
10278#endif
10279 }
10280
10281#ifdef USE_ON_FLY_SCROLL
10282 dont_scroll = TRUE; /* disallow scrolling here */
10283#endif
10284
10285 /* don't map the digraph chars. This also prevents the
10286 * mode message to be deleted when ESC is hit */
10287 ++no_mapping;
10288 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010289 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 --no_mapping;
10291 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010292 if (did_putchar)
10293 /* when the line fits in 'columns' the '?' is at the start of the next
10294 * line and will not be removed by the redraw */
10295 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010296
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 if (IS_SPECIAL(c) || mod_mask) /* special key */
10298 {
10299#ifdef FEAT_CMDL_INFO
10300 clear_showcmd();
10301#endif
10302 insert_special(c, TRUE, FALSE);
10303 return NUL;
10304 }
10305 if (c != ESC)
10306 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010307 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 if (redrawing() && !char_avail())
10309 {
10310 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010311 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312
10313 if (char2cells(c) == 1)
10314 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010315 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010317 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 }
10319#ifdef FEAT_CMDL_INFO
10320 add_to_showcmd_c(c);
10321#endif
10322 }
10323 ++no_mapping;
10324 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010325 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 --no_mapping;
10327 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010328 if (did_putchar)
10329 /* when the line fits in 'columns' the '?' is at the start of the
10330 * next line and will not be removed by a redraw */
10331 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 if (cc != ESC)
10333 {
10334 AppendToRedobuff((char_u *)CTRL_V_STR);
10335 c = getdigraph(c, cc, TRUE);
10336#ifdef FEAT_CMDL_INFO
10337 clear_showcmd();
10338#endif
10339 return c;
10340 }
10341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342#ifdef FEAT_CMDL_INFO
10343 clear_showcmd();
10344#endif
10345 return NUL;
10346}
10347#endif
10348
10349/*
10350 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10351 * Returns the char to be inserted, or NUL if none found.
10352 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010354ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355{
10356 int c;
10357 int temp;
10358 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010359 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360
10361 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10362 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010363 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 return NUL;
10365 }
10366
10367 /* try to advance to the cursor column */
10368 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010369 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 prev_ptr = ptr;
10371 validate_virtcol();
10372 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10373 {
10374 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010375 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 }
10377 if ((colnr_T)temp > curwin->w_virtcol)
10378 ptr = prev_ptr;
10379
10380#ifdef FEAT_MBYTE
10381 c = (*mb_ptr2char)(ptr);
10382#else
10383 c = *ptr;
10384#endif
10385 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010386 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 return c;
10388}
10389
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010390/*
10391 * CTRL-Y or CTRL-E typed in Insert mode.
10392 */
10393 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010394ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010395{
10396 int c = tc;
10397
10398#ifdef FEAT_INS_EXPAND
10399 if (ctrl_x_mode == CTRL_X_SCROLL)
10400 {
10401 if (c == Ctrl_Y)
10402 scrolldown_clamp();
10403 else
10404 scrollup_clamp();
10405 redraw_later(VALID);
10406 }
10407 else
10408#endif
10409 {
10410 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10411 if (c != NUL)
10412 {
10413 long tw_save;
10414
10415 /* The character must be taken literally, insert like it
10416 * was typed after a CTRL-V, and pretend 'textwidth'
10417 * wasn't set. Digits, 'o' and 'x' are special after a
10418 * CTRL-V, don't use it for these. */
10419 if (c < 256 && !isalnum(c))
10420 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10421 tw_save = curbuf->b_p_tw;
10422 curbuf->b_p_tw = -1;
10423 insert_special(c, TRUE, FALSE);
10424 curbuf->b_p_tw = tw_save;
10425#ifdef FEAT_RIGHTLEFT
10426 revins_chars++;
10427 revins_legal++;
10428#endif
10429 c = Ctrl_V; /* pretend CTRL-V is last character */
10430 auto_format(FALSE, TRUE);
10431 }
10432 }
10433 return c;
10434}
10435
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436#ifdef FEAT_SMARTINDENT
10437/*
10438 * Try to do some very smart auto-indenting.
10439 * Used when inserting a "normal" character.
10440 */
10441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010442ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443{
10444 pos_T *pos, old_pos;
10445 char_u *ptr;
10446 int i;
10447 int temp;
10448
10449 /*
10450 * do some very smart indenting when entering '{' or '}'
10451 */
10452 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10453 {
10454 /*
10455 * for '}' set indent equal to indent of line containing matching '{'
10456 */
10457 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10458 {
10459 old_pos = curwin->w_cursor;
10460 /*
10461 * If the matching '{' has a ')' immediately before it (ignoring
10462 * white-space), then line up with the start of the line
10463 * containing the matching '(' if there is one. This handles the
10464 * case where an "if (..\n..) {" statement continues over multiple
10465 * lines -- webb
10466 */
10467 ptr = ml_get(pos->lnum);
10468 i = pos->col;
10469 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010470 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 ;
10472 curwin->w_cursor.lnum = pos->lnum;
10473 curwin->w_cursor.col = i;
10474 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10475 curwin->w_cursor = *pos;
10476 i = get_indent();
10477 curwin->w_cursor = old_pos;
10478#ifdef FEAT_VREPLACE
10479 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010480 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 else
10482#endif
10483 (void)set_indent(i, SIN_CHANGED);
10484 }
10485 else if (curwin->w_cursor.col > 0)
10486 {
10487 /*
10488 * when inserting '{' after "O" reduce indent, but not
10489 * more than indent of previous line
10490 */
10491 temp = TRUE;
10492 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10493 {
10494 old_pos = curwin->w_cursor;
10495 i = get_indent();
10496 while (curwin->w_cursor.lnum > 1)
10497 {
10498 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10499
10500 /* ignore empty lines and lines starting with '#'. */
10501 if (*ptr != '#' && *ptr != NUL)
10502 break;
10503 }
10504 if (get_indent() >= i)
10505 temp = FALSE;
10506 curwin->w_cursor = old_pos;
10507 }
10508 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010509 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 }
10511 }
10512
10513 /*
10514 * set indent of '#' always to 0
10515 */
10516 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10517 {
10518 /* remember current indent for next line */
10519 old_indent = get_indent();
10520 (void)set_indent(0, SIN_CHANGED);
10521 }
10522
10523 /* Adjust ai_col, the char at this position can be deleted. */
10524 if (ai_col > curwin->w_cursor.col)
10525 ai_col = curwin->w_cursor.col;
10526}
10527#endif
10528
10529/*
10530 * Get the value that w_virtcol would have when 'list' is off.
10531 * Unless 'cpo' contains the 'L' flag.
10532 */
10533 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010534get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535{
10536 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10537 return getvcol_nolist(&curwin->w_cursor);
10538 validate_virtcol();
10539 return curwin->w_virtcol;
10540}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010541
10542#ifdef FEAT_AUTOCMD
10543/*
10544 * Handle the InsertCharPre autocommand.
10545 * "c" is the character that was typed.
10546 * Return a pointer to allocated memory with the replacement string.
10547 * Return NULL to continue inserting "c".
10548 */
10549 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010550do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010551{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010552 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010553 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010554
10555 /* Return quickly when there is nothing to do. */
10556 if (!has_insertcharpre())
10557 return NULL;
10558
Bram Moolenaar704984a2012-06-01 14:57:51 +020010559#ifdef FEAT_MBYTE
10560 if (has_mbyte)
10561 buf[(*mb_char2bytes)(c, buf)] = NUL;
10562 else
10563#endif
10564 {
10565 buf[0] = c;
10566 buf[1] = NUL;
10567 }
10568
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010569 /* Lock the text to avoid weird things from happening. */
10570 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010571 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010572
Bram Moolenaar704984a2012-06-01 14:57:51 +020010573 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010574 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010575 {
10576 /* Get the value of v:char. It may be empty or more than one
10577 * character. Only use it when changed, otherwise continue with the
10578 * original character to avoid breaking autoindent. */
10579 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10580 res = vim_strsave(get_vim_var_str(VV_CHAR));
10581 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010582
10583 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10584 --textlock;
10585
10586 return res;
10587}
10588#endif