blob: 14201a2ec3e36db56dff7ac0788007beb642ed67 [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;
519#ifdef USE_IM_CONTROL
520 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 Moolenaard4e20a72008-01-22 16:50:03 +0000784 * Get a character for Insert mode. Ignore K_IGNORE.
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();
801 } while (c == K_IGNORE);
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;
987 /*FALLTHROUGH*/
988
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:
1180 case K_MIDDLEMOUSE:
1181 case K_MIDDLEDRAG:
1182 case K_MIDDLERELEASE:
1183 case K_RIGHTMOUSE:
1184 case K_RIGHTDRAG:
1185 case K_RIGHTRELEASE:
1186 case K_X1MOUSE:
1187 case K_X1DRAG:
1188 case K_X1RELEASE:
1189 case K_X2MOUSE:
1190 case K_X2DRAG:
1191 case K_X2RELEASE:
1192 ins_mouse(c);
1193 break;
1194
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001195 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001196 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 break;
1198
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001199 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001200 ins_mousescroll(MSCR_UP);
1201 break;
1202
1203 case K_MOUSELEFT: /* Scroll wheel left */
1204 ins_mousescroll(MSCR_LEFT);
1205 break;
1206
1207 case K_MOUSERIGHT: /* Scroll wheel right */
1208 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 break;
1210#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001211 case K_PS:
1212 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1213 if (cmdchar == K_PS)
1214 /* invoked from normal mode, bail out */
1215 goto doESCkey;
1216 break;
1217 case K_PE:
1218 /* Got K_PE without K_PS, ignore. */
1219 break;
1220
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001221#ifdef FEAT_GUI_TABLINE
1222 case K_TABLINE:
1223 case K_TABMENU:
1224 ins_tabline(c);
1225 break;
1226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001228 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 break;
1230
Bram Moolenaar754b5602006-02-09 23:53:20 +00001231#ifdef FEAT_AUTOCMD
1232 case K_CURSORHOLD: /* Didn't type something for a while. */
1233 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1234 did_cursorhold = TRUE;
1235 break;
1236#endif
1237
Bram Moolenaar4770d092006-01-12 23:22:24 +00001238#ifdef FEAT_GUI_W32
1239 /* On Win32 ignore <M-F4>, we get it when closing the window was
1240 * cancelled. */
1241 case K_F4:
1242 if (mod_mask != MOD_MASK_ALT)
1243 goto normalchar;
1244 break;
1245#endif
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247#ifdef FEAT_GUI
1248 case K_VER_SCROLLBAR:
1249 ins_scroll();
1250 break;
1251
1252 case K_HOR_SCROLLBAR:
1253 ins_horscroll();
1254 break;
1255#endif
1256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001257 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 case K_S_HOME:
1260 case K_C_HOME:
1261 ins_home(c);
1262 break;
1263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001264 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 case K_S_END:
1267 case K_C_END:
1268 ins_end(c);
1269 break;
1270
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001271 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001272 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1273 ins_s_left();
1274 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001275 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 break;
1277
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001278 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 case K_C_LEFT:
1280 ins_s_left();
1281 break;
1282
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001283 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001284 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1285 ins_s_right();
1286 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001287 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 break;
1289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001290 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 case K_C_RIGHT:
1292 ins_s_right();
1293 break;
1294
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001295 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001296#ifdef FEAT_INS_EXPAND
1297 if (pum_visible())
1298 goto docomplete;
1299#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001300 if (mod_mask & MOD_MASK_SHIFT)
1301 ins_pageup();
1302 else
1303 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 break;
1305
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001306 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 case K_PAGEUP:
1308 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001309#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001310 if (pum_visible())
1311 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 ins_pageup();
1314 break;
1315
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001316 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001317#ifdef FEAT_INS_EXPAND
1318 if (pum_visible())
1319 goto docomplete;
1320#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001321 if (mod_mask & MOD_MASK_SHIFT)
1322 ins_pagedown();
1323 else
1324 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 break;
1326
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001327 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 case K_PAGEDOWN:
1329 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001330#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001331 if (pum_visible())
1332 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 ins_pagedown();
1335 break;
1336
1337#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001338 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 ins_drop();
1340 break;
1341#endif
1342
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001343 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 c = TAB;
1345 /* FALLTHROUGH */
1346
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001347 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1349 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1350 goto docomplete;
1351#endif
1352 inserted_space = FALSE;
1353 if (ins_tab())
1354 goto normalchar; /* insert TAB as a normal char */
1355 auto_format(FALSE, TRUE);
1356 break;
1357
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001358 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 c = CAR;
1360 /* FALLTHROUGH */
1361 case CAR:
1362 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001363#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 /* In a quickfix window a <CR> jumps to the error under the
1365 * cursor. */
1366 if (bt_quickfix(curbuf) && c == CAR)
1367 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001368 if (curwin->w_llist_ref == NULL) /* quickfix window */
1369 do_cmdline_cmd((char_u *)".cc");
1370 else /* location list window */
1371 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 break;
1373 }
1374#endif
1375#ifdef FEAT_CMDWIN
1376 if (cmdwin_type != 0)
1377 {
1378 /* Execute the command in the cmdline window. */
1379 cmdwin_result = CAR;
1380 goto doESCkey;
1381 }
1382#endif
1383 if (ins_eol(c) && !p_im)
1384 goto doESCkey; /* out of memory */
1385 auto_format(FALSE, FALSE);
1386 inserted_space = FALSE;
1387 break;
1388
Bram Moolenaar860cae12010-06-05 23:22:07 +02001389#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001390 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391# ifdef FEAT_INS_EXPAND
1392 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1393 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001394 if (has_compl_option(TRUE))
1395 goto docomplete;
1396 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398# endif
1399# ifdef FEAT_DIGRAPHS
1400 c = ins_digraph();
1401 if (c == NUL)
1402 break;
1403# endif
1404 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
1407#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001408 case Ctrl_X: /* Enter CTRL-X mode */
1409 ins_ctrl_x();
1410 break;
1411
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001412 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (ctrl_x_mode != CTRL_X_TAGS)
1414 goto normalchar;
1415 goto docomplete;
1416
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001417 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (ctrl_x_mode != CTRL_X_FILES)
1419 goto normalchar;
1420 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001421
1422 case 's': /* Spelling completion after ^X */
1423 case Ctrl_S:
1424 if (ctrl_x_mode != CTRL_X_SPELL)
1425 goto normalchar;
1426 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427#endif
1428
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001429 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430#ifdef FEAT_INS_EXPAND
1431 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1432#endif
1433 {
1434 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1435 if (p_im)
1436 {
1437 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1438 break;
1439 goto doESCkey;
1440 }
1441 goto normalchar;
1442 }
1443#ifdef FEAT_INS_EXPAND
1444 /* FALLTHROUGH */
1445
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001446 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 case Ctrl_N:
1448 /* if 'complete' is empty then plain ^P is no longer special,
1449 * but it is under other ^X modes */
1450 if (*curbuf->b_p_cpt == NUL
1451 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001452 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 goto normalchar;
1454
1455docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001456 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001457#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001458 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001459#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001460 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001461 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001462#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001463 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001464#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001465 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 break;
1467#endif /* FEAT_INS_EXPAND */
1468
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001469 case Ctrl_Y: /* copy from previous line or scroll down */
1470 case Ctrl_E: /* copy from next line or scroll up */
1471 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 break;
1473
1474 default:
1475#ifdef UNIX
1476 if (c == intr_char) /* special interrupt char */
1477 goto do_intr;
1478#endif
1479
Bram Moolenaare659c952011-05-19 17:25:41 +02001480normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001482 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001484#ifdef FEAT_AUTOCMD
1485 if (!p_paste)
1486 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001487 /* Trigger InsertCharPre. */
1488 char_u *str = do_insert_char_pre(c);
1489 char_u *p;
1490
1491 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001492 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001493 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001494 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001495 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001496 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001497 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001498 c = PTR2CHAR(p);
1499 if (c == CAR || c == K_KENTER || c == NL)
1500 ins_eol(c);
1501 else
1502 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001503 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001504 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001505 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001506 vim_free(str);
1507 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001508 }
1509
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001510 /* If the new value is already inserted or an empty string
1511 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001512 if (c == NUL)
1513 break;
1514 }
1515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516#ifdef FEAT_SMARTINDENT
1517 /* Try to perform smart-indenting. */
1518 ins_try_si(c);
1519#endif
1520
1521 if (c == ' ')
1522 {
1523 inserted_space = TRUE;
1524#ifdef FEAT_CINDENT
1525 if (inindent(0))
1526 can_cindent = FALSE;
1527#endif
1528 if (Insstart_blank_vcol == MAXCOL
1529 && curwin->w_cursor.lnum == Insstart.lnum)
1530 Insstart_blank_vcol = get_nolist_virtcol();
1531 }
1532
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001533 /* Insert a normal character and check for abbreviations on a
1534 * special character. Let CTRL-] expand abbreviations without
1535 * inserting it. */
1536 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#ifdef FEAT_MBYTE
1538 /* Add ABBR_OFF for characters above 0x100, this is
1539 * what check_abbr() expects. */
1540 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1541#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001542 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
1544 insert_special(c, FALSE, FALSE);
1545#ifdef FEAT_RIGHTLEFT
1546 revins_legal++;
1547 revins_chars++;
1548#endif
1549 }
1550
1551 auto_format(FALSE, TRUE);
1552
1553#ifdef FEAT_FOLDING
1554 /* When inserting a character the cursor line must never be in a
1555 * closed fold. */
1556 foldOpenCursor();
1557#endif
1558 break;
1559 } /* end of switch (c) */
1560
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001561#ifdef FEAT_AUTOCMD
1562 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001563 if (c != K_CURSORHOLD
1564# ifdef FEAT_COMPL_FUNC
1565 /* but not in CTRL-X mode, a script can't restore the state */
1566 && ctrl_x_mode == 0
1567# endif
1568 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001569 did_cursorhold = FALSE;
1570#endif
1571
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 /* If the cursor was moved we didn't just insert a space */
1573 if (arrow_used)
1574 inserted_space = FALSE;
1575
1576#ifdef FEAT_CINDENT
1577 if (can_cindent && cindent_on()
1578# ifdef FEAT_INS_EXPAND
1579 && ctrl_x_mode == 0
1580# endif
1581 )
1582 {
1583force_cindent:
1584 /*
1585 * Indent now if a key was typed that is in 'cinkeys'.
1586 */
1587 if (in_cinkeys(c, ' ', line_is_white))
1588 {
1589 if (stop_arrow() == OK)
1590 /* re-indent the current line */
1591 do_c_expr_indent();
1592 }
1593 }
1594#endif /* FEAT_CINDENT */
1595
1596 } /* for (;;) */
1597 /* NOTREACHED */
1598}
1599
1600/*
1601 * Redraw for Insert mode.
1602 * This is postponed until getting the next character to make '$' in the 'cpo'
1603 * option work correctly.
1604 * Only redraw when there are no characters available. This speeds up
1605 * inserting sequences of characters (e.g., for CTRL-R).
1606 */
1607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001608ins_redraw(
1609 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001611#ifdef FEAT_CONCEAL
1612 linenr_T conceal_old_cursor_line = 0;
1613 linenr_T conceal_new_cursor_line = 0;
1614 int conceal_update_lines = FALSE;
1615#endif
1616
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001617 if (char_avail())
1618 return;
1619
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001620#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001621 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1622 * visible, the command might delete it. */
1623 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001624# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001625 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001626# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001627# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001628 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001629# endif
1630# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001631 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001632# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001633 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001634# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001635 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001636# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001637# ifdef FEAT_INS_EXPAND
1638 && !pum_visible()
1639# endif
1640 )
1641 {
1642# ifdef FEAT_SYN_HL
1643 /* Need to update the screen first, to make sure syntax
1644 * highlighting is correct after making a change (e.g., inserting
1645 * a "(". The autocommand may also require a redraw, so it's done
1646 * again below, unfortunately. */
1647 if (syntax_present(curwin) && must_redraw)
1648 update_screen(0);
1649# endif
1650# ifdef FEAT_AUTOCMD
1651 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001652 {
1653 /* Make sure curswant is correct, an autocommand may call
1654 * getcurpos(). */
1655 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001656 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001657 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001658# endif
1659# ifdef FEAT_CONCEAL
1660 if (curwin->w_p_cole > 0)
1661 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001662# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001663 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001664# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665 conceal_new_cursor_line = curwin->w_cursor.lnum;
1666 conceal_update_lines = TRUE;
1667 }
1668# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001669# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001670 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001671# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001672 }
1673#endif
1674
1675#ifdef FEAT_AUTOCMD
1676 /* Trigger TextChangedI if b_changedtick differs. */
1677 if (ready && has_textchangedI()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001678 && last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001679# ifdef FEAT_INS_EXPAND
1680 && !pum_visible()
1681# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001682 )
1683 {
1684 if (last_changedtick_buf == curbuf)
1685 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1686 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001687 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001689#endif
1690
1691 if (must_redraw)
1692 update_screen(0);
1693 else if (clear_cmdline || redraw_cmdline)
1694 showmode(); /* clear cmdline and show mode */
1695# if defined(FEAT_CONCEAL)
1696 if ((conceal_update_lines
1697 && (conceal_old_cursor_line != conceal_new_cursor_line
1698 || conceal_cursor_line(curwin)))
1699 || need_cursor_line_redraw)
1700 {
1701 if (conceal_old_cursor_line != conceal_new_cursor_line)
1702 update_single_line(curwin, conceal_old_cursor_line);
1703 update_single_line(curwin, conceal_new_cursor_line == 0
1704 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1705 curwin->w_valid &= ~VALID_CROW;
1706 }
1707# endif
1708 showruler(FALSE);
1709 setcursor();
1710 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711}
1712
1713/*
1714 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1715 */
1716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001717ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001720 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
1722 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001723 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
1725 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001726 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001728 did_putchar = TRUE;
1729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1731
1732#ifdef FEAT_CMDL_INFO
1733 add_to_showcmd_c(Ctrl_V);
1734#endif
1735
1736 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001737 if (did_putchar)
1738 /* when the line fits in 'columns' the '^' is at the start of the next
1739 * line and will not removed by the redraw */
1740 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741#ifdef FEAT_CMDL_INFO
1742 clear_showcmd();
1743#endif
1744 insert_special(c, FALSE, TRUE);
1745#ifdef FEAT_RIGHTLEFT
1746 revins_chars++;
1747 revins_legal++;
1748#endif
1749}
1750
1751/*
1752 * Put a character directly onto the screen. It's not stored in a buffer.
1753 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1754 */
1755static int pc_status;
1756#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1757#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1758#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1759#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761static int pc_attr;
1762static int pc_row;
1763static int pc_col;
1764
1765 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001766edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768 int attr;
1769
1770 if (ScreenLines != NULL)
1771 {
1772 update_topline(); /* just in case w_topline isn't valid */
1773 validate_cursor();
1774 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001775 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 else
1777 attr = 0;
1778 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001779 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1781 pc_status = PC_STATUS_UNSET;
1782#endif
1783#ifdef FEAT_RIGHTLEFT
1784 if (curwin->w_p_rl)
1785 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001786 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787# ifdef FEAT_MBYTE
1788 if (has_mbyte)
1789 {
1790 int fix_col = mb_fix_col(pc_col, pc_row);
1791
1792 if (fix_col != pc_col)
1793 {
1794 screen_putchar(' ', pc_row, fix_col, attr);
1795 --curwin->w_wcol;
1796 pc_status = PC_STATUS_RIGHT;
1797 }
1798 }
1799# endif
1800 }
1801 else
1802#endif
1803 {
1804 pc_col += curwin->w_wcol;
1805#ifdef FEAT_MBYTE
1806 if (mb_lefthalve(pc_row, pc_col))
1807 pc_status = PC_STATUS_LEFT;
1808#endif
1809 }
1810
1811 /* save the character to be able to put it back */
1812#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1813 if (pc_status == PC_STATUS_UNSET)
1814#endif
1815 {
1816 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1817 pc_status = PC_STATUS_SET;
1818 }
1819 screen_putchar(c, pc_row, pc_col, attr);
1820 }
1821}
1822
1823/*
1824 * Undo the previous edit_putchar().
1825 */
1826 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001827edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1830 {
1831#if defined(FEAT_MBYTE)
1832 if (pc_status == PC_STATUS_RIGHT)
1833 ++curwin->w_wcol;
1834 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1835 redrawWinline(curwin->w_cursor.lnum, FALSE);
1836 else
1837#endif
1838 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1839 }
1840}
1841
1842/*
1843 * Called when p_dollar is set: display a '$' at the end of the changed text
1844 * Only works when cursor is in the line that changes.
1845 */
1846 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001847display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
1849 colnr_T save_col;
1850
1851 if (!redrawing())
1852 return;
1853
1854 cursor_off();
1855 save_col = curwin->w_cursor.col;
1856 curwin->w_cursor.col = col;
1857#ifdef FEAT_MBYTE
1858 if (has_mbyte)
1859 {
1860 char_u *p;
1861
1862 /* If on the last byte of a multi-byte move to the first byte. */
1863 p = ml_get_curline();
1864 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1865 }
1866#endif
1867 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001868 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 {
1870 edit_putchar('$', FALSE);
1871 dollar_vcol = curwin->w_virtcol;
1872 }
1873 curwin->w_cursor.col = save_col;
1874}
1875
1876/*
1877 * Call this function before moving the cursor from the normal insert position
1878 * in insert mode.
1879 */
1880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001881undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001883 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001885 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 redrawWinline(curwin->w_cursor.lnum, FALSE);
1887 }
1888}
1889
1890/*
1891 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1892 * Keep the cursor on the same character.
1893 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1894 * type == INDENT_DEC decrease indent (for CTRL-D)
1895 * type == INDENT_SET set indent to "amount"
1896 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1897 */
1898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001899change_indent(
1900 int type,
1901 int amount,
1902 int round,
1903 int replaced, /* replaced character, put on replace stack */
1904 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905{
1906 int vcol;
1907 int last_vcol;
1908 int insstart_less; /* reduction for Insstart.col */
1909 int new_cursor_col;
1910 int i;
1911 char_u *ptr;
1912 int save_p_list;
1913 int start_col;
1914 colnr_T vc;
1915#ifdef FEAT_VREPLACE
1916 colnr_T orig_col = 0; /* init for GCC */
1917 char_u *new_line, *orig_line = NULL; /* init for GCC */
1918
1919 /* VREPLACE mode needs to know what the line was like before changing */
1920 if (State & VREPLACE_FLAG)
1921 {
1922 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1923 orig_col = curwin->w_cursor.col;
1924 }
1925#endif
1926
1927 /* for the following tricks we don't want list mode */
1928 save_p_list = curwin->w_p_list;
1929 curwin->w_p_list = FALSE;
1930 vc = getvcol_nolist(&curwin->w_cursor);
1931 vcol = vc;
1932
1933 /*
1934 * For Replace mode we need to fix the replace stack later, which is only
1935 * possible when the cursor is in the indent. Remember the number of
1936 * characters before the cursor if it's possible.
1937 */
1938 start_col = curwin->w_cursor.col;
1939
1940 /* determine offset from first non-blank */
1941 new_cursor_col = curwin->w_cursor.col;
1942 beginline(BL_WHITE);
1943 new_cursor_col -= curwin->w_cursor.col;
1944
1945 insstart_less = curwin->w_cursor.col;
1946
1947 /*
1948 * If the cursor is in the indent, compute how many screen columns the
1949 * cursor is to the left of the first non-blank.
1950 */
1951 if (new_cursor_col < 0)
1952 vcol = get_indent() - vcol;
1953
1954 if (new_cursor_col > 0) /* can't fix replace stack */
1955 start_col = -1;
1956
1957 /*
1958 * Set the new indent. The cursor will be put on the first non-blank.
1959 */
1960 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001961 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 else
1963 {
1964#ifdef FEAT_VREPLACE
1965 int save_State = State;
1966
1967 /* Avoid being called recursively. */
1968 if (State & VREPLACE_FLAG)
1969 State = INSERT;
1970#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001971 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972#ifdef FEAT_VREPLACE
1973 State = save_State;
1974#endif
1975 }
1976 insstart_less -= curwin->w_cursor.col;
1977
1978 /*
1979 * Try to put cursor on same character.
1980 * If the cursor is at or after the first non-blank in the line,
1981 * compute the cursor column relative to the column of the first
1982 * non-blank character.
1983 * If we are not in insert mode, leave the cursor on the first non-blank.
1984 * If the cursor is before the first non-blank, position it relative
1985 * to the first non-blank, counted in screen columns.
1986 */
1987 if (new_cursor_col >= 0)
1988 {
1989 /*
1990 * When changing the indent while the cursor is touching it, reset
1991 * Insstart_col to 0.
1992 */
1993 if (new_cursor_col == 0)
1994 insstart_less = MAXCOL;
1995 new_cursor_col += curwin->w_cursor.col;
1996 }
1997 else if (!(State & INSERT))
1998 new_cursor_col = curwin->w_cursor.col;
1999 else
2000 {
2001 /*
2002 * Compute the screen column where the cursor should be.
2003 */
2004 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002005 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006
2007 /*
2008 * Advance the cursor until we reach the right screen column.
2009 */
2010 vcol = last_vcol = 0;
2011 new_cursor_col = -1;
2012 ptr = ml_get_curline();
2013 while (vcol <= (int)curwin->w_virtcol)
2014 {
2015 last_vcol = vcol;
2016#ifdef FEAT_MBYTE
2017 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002018 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 else
2020#endif
2021 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002022 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
2024 vcol = last_vcol;
2025
2026 /*
2027 * May need to insert spaces to be able to position the cursor on
2028 * the right screen column.
2029 */
2030 if (vcol != (int)curwin->w_virtcol)
2031 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002032 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002034 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (ptr != NULL)
2036 {
2037 new_cursor_col += i;
2038 ptr[i] = NUL;
2039 while (--i >= 0)
2040 ptr[i] = ' ';
2041 ins_str(ptr);
2042 vim_free(ptr);
2043 }
2044 }
2045
2046 /*
2047 * When changing the indent while the cursor is in it, reset
2048 * Insstart_col to 0.
2049 */
2050 insstart_less = MAXCOL;
2051 }
2052
2053 curwin->w_p_list = save_p_list;
2054
2055 if (new_cursor_col <= 0)
2056 curwin->w_cursor.col = 0;
2057 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002058 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 curwin->w_set_curswant = TRUE;
2060 changed_cline_bef_curs();
2061
2062 /*
2063 * May have to adjust the start of the insert.
2064 */
2065 if (State & INSERT)
2066 {
2067 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2068 {
2069 if ((int)Insstart.col <= insstart_less)
2070 Insstart.col = 0;
2071 else
2072 Insstart.col -= insstart_less;
2073 }
2074 if ((int)ai_col <= insstart_less)
2075 ai_col = 0;
2076 else
2077 ai_col -= insstart_less;
2078 }
2079
2080 /*
2081 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2082 * If the number of characters before the cursor decreased, need to pop a
2083 * few characters from the replace stack.
2084 * If the number of characters before the cursor increased, need to push a
2085 * few NULs onto the replace stack.
2086 */
2087 if (REPLACE_NORMAL(State) && start_col >= 0)
2088 {
2089 while (start_col > (int)curwin->w_cursor.col)
2090 {
2091 replace_join(0); /* remove a NUL from the replace stack */
2092 --start_col;
2093 }
2094 while (start_col < (int)curwin->w_cursor.col || replaced)
2095 {
2096 replace_push(NUL);
2097 if (replaced)
2098 {
2099 replace_push(replaced);
2100 replaced = NUL;
2101 }
2102 ++start_col;
2103 }
2104 }
2105
2106#ifdef FEAT_VREPLACE
2107 /*
2108 * For VREPLACE mode, we also have to fix the replace stack. In this case
2109 * it is always possible because we backspace over the whole line and then
2110 * put it back again the way we wanted it.
2111 */
2112 if (State & VREPLACE_FLAG)
2113 {
2114 /* If orig_line didn't allocate, just return. At least we did the job,
2115 * even if you can't backspace. */
2116 if (orig_line == NULL)
2117 return;
2118
2119 /* Save new line */
2120 new_line = vim_strsave(ml_get_curline());
2121 if (new_line == NULL)
2122 return;
2123
2124 /* We only put back the new line up to the cursor */
2125 new_line[curwin->w_cursor.col] = NUL;
2126
2127 /* Put back original line */
2128 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2129 curwin->w_cursor.col = orig_col;
2130
2131 /* Backspace from cursor to start of line */
2132 backspace_until_column(0);
2133
2134 /* Insert new stuff into line again */
2135 ins_bytes(new_line);
2136
2137 vim_free(new_line);
2138 }
2139#endif
2140}
2141
2142/*
2143 * Truncate the space at the end of a line. This is to be used only in an
2144 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2145 * modes.
2146 */
2147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002148truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
2150 int i;
2151
2152 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002153 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
2155 if (State & REPLACE_FLAG)
2156 replace_join(0); /* remove a NUL from the replace stack */
2157 }
2158 line[i + 1] = NUL;
2159}
2160
2161#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2162 || defined(FEAT_COMMENTS) || defined(PROTO)
2163/*
2164 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2165 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002166 * Will attempt not to go before "col" even when there is a composing
2167 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 */
2169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002170backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
2172 while ((int)curwin->w_cursor.col > col)
2173 {
2174 curwin->w_cursor.col--;
2175 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002176 replace_do_bs(col);
2177 else if (!del_char_after_col(col))
2178 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180}
2181#endif
2182
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002183/*
2184 * Like del_char(), but make sure not to go before column "limit_col".
2185 * Only matters when there are composing characters.
2186 * Return TRUE when something was deleted.
2187 */
2188 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002189del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002190{
2191#ifdef FEAT_MBYTE
2192 if (enc_utf8 && limit_col >= 0)
2193 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002194 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002195
2196 /* Make sure the cursor is at the start of a character, but
2197 * skip forward again when going too far back because of a
2198 * composing character. */
2199 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002200 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002201 {
2202 int l = utf_ptr2len(ml_get_cursor());
2203
2204 if (l == 0) /* end of line */
2205 break;
2206 curwin->w_cursor.col += l;
2207 }
2208 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2209 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002210 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002211 }
2212 else
2213#endif
2214 (void)del_char(FALSE);
2215 return TRUE;
2216}
2217
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2219/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002220 * CTRL-X pressed in Insert mode.
2221 */
2222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002223ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002224{
2225 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2226 * CTRL-V works like CTRL-N */
2227 if (ctrl_x_mode != CTRL_X_CMDLINE)
2228 {
2229 /* if the next ^X<> won't ADD nothing, then reset
2230 * compl_cont_status */
2231 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002232 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002233 else
2234 compl_cont_status = 0;
2235 /* We're not sure which CTRL-X mode it will be yet */
2236 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2237 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2238 edit_submode_pre = NULL;
2239 showmode();
2240 }
2241}
2242
2243/*
2244 * Return TRUE if the 'dict' or 'tsr' option can be used.
2245 */
2246 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002247has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002248{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002249 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002250# ifdef FEAT_SPELL
2251 && !curwin->w_p_spell
2252# endif
2253 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002254 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2255 {
2256 ctrl_x_mode = 0;
2257 edit_submode = NULL;
2258 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2259 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002260 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002261 if (emsg_silent == 0)
2262 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002263 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002264 setcursor();
2265 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002266#ifdef FEAT_EVAL
2267 if (!get_vim_var_nr(VV_TESTING))
2268#endif
2269 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002270 }
2271 return FALSE;
2272 }
2273 return TRUE;
2274}
2275
2276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2278 * This depends on the current mode.
2279 */
2280 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002281vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
2283 /* Always allow ^R - let it's results then be checked */
2284 if (c == Ctrl_R)
2285 return TRUE;
2286
Bram Moolenaare3226be2005-12-18 22:10:00 +00002287 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002288 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002289 return TRUE;
2290
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 switch (ctrl_x_mode)
2292 {
2293 case 0: /* Not in any CTRL-X mode */
2294 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2295 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002296 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2298 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2299 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002300 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002301 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 case CTRL_X_SCROLL:
2303 return (c == Ctrl_Y || c == Ctrl_E);
2304 case CTRL_X_WHOLE_LINE:
2305 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2306 case CTRL_X_FILES:
2307 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2308 case CTRL_X_DICTIONARY:
2309 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2310 case CTRL_X_THESAURUS:
2311 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2312 case CTRL_X_TAGS:
2313 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2314#ifdef FEAT_FIND_ID
2315 case CTRL_X_PATH_PATTERNS:
2316 return (c == Ctrl_P || c == Ctrl_N);
2317 case CTRL_X_PATH_DEFINES:
2318 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2319#endif
2320 case CTRL_X_CMDLINE:
2321 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2322 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002323#ifdef FEAT_COMPL_FUNC
2324 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002325 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002326 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002327 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002328#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002329 case CTRL_X_SPELL:
2330 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002331 case CTRL_X_EVAL:
2332 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002334 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 return FALSE;
2336}
2337
2338/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002339 * Return TRUE when character "c" is part of the item currently being
2340 * completed. Used to decide whether to abandon complete mode when the menu
2341 * is visible.
2342 */
2343 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002344ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002345{
2346 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2347 /* When expanding an identifier only accept identifier chars. */
2348 return vim_isIDc(c);
2349
2350 switch (ctrl_x_mode)
2351 {
2352 case CTRL_X_FILES:
2353 /* When expanding file name only accept file name chars. But not
2354 * path separators, so that "proto/<Tab>" expands files in
2355 * "proto", not "proto/" as a whole */
2356 return vim_isfilec(c) && !vim_ispathsep(c);
2357
2358 case CTRL_X_CMDLINE:
2359 case CTRL_X_OMNI:
2360 /* Command line and Omni completion can work with just about any
2361 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002362 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002363
2364 case CTRL_X_WHOLE_LINE:
2365 /* For while line completion a space can be part of the line. */
2366 return vim_isprintc(c);
2367 }
2368 return vim_iswordc(c);
2369}
2370
2371/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002372 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002374 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 * the rest of the word to be in -- webb
2376 */
2377 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002378ins_compl_add_infercase(
2379 char_u *str,
2380 int len,
2381 int icase,
2382 char_u *fname,
2383 int dir,
2384 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002386 char_u *p;
2387 int i, c;
2388 int actual_len; /* Take multi-byte characters */
2389 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002390 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002391 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 int has_lower = FALSE;
2393 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002395 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002397 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
Bram Moolenaara2993e12007-08-12 14:38:46 +00002399 /* Find actual length of completion. */
2400#ifdef FEAT_MBYTE
2401 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002403 p = str;
2404 actual_len = 0;
2405 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002407 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002408 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002411 else
2412#endif
2413 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
Bram Moolenaara2993e12007-08-12 14:38:46 +00002415 /* Find actual length of original text. */
2416#ifdef FEAT_MBYTE
2417 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002419 p = compl_orig_text;
2420 actual_compl_length = 0;
2421 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002423 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002424 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002427 else
2428#endif
2429 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002431 /* "actual_len" may be smaller than "actual_compl_length" when using
2432 * thesaurus, only use the minimum when comparing. */
2433 min_len = actual_len < actual_compl_length
2434 ? actual_len : actual_compl_length;
2435
Bram Moolenaara2993e12007-08-12 14:38:46 +00002436 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002437 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002438 if (wca != NULL)
2439 {
2440 p = str;
2441 for (i = 0; i < actual_len; ++i)
2442#ifdef FEAT_MBYTE
2443 if (has_mbyte)
2444 wca[i] = mb_ptr2char_adv(&p);
2445 else
2446#endif
2447 wca[i] = *(p++);
2448
2449 /* Rule 1: Were any chars converted to lower? */
2450 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002451 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002452 {
2453#ifdef FEAT_MBYTE
2454 if (has_mbyte)
2455 c = mb_ptr2char_adv(&p);
2456 else
2457#endif
2458 c = *(p++);
2459 if (MB_ISLOWER(c))
2460 {
2461 has_lower = TRUE;
2462 if (MB_ISUPPER(wca[i]))
2463 {
2464 /* Rule 1 is satisfied. */
2465 for (i = actual_compl_length; i < actual_len; ++i)
2466 wca[i] = MB_TOLOWER(wca[i]);
2467 break;
2468 }
2469 }
2470 }
2471
2472 /*
2473 * Rule 2: No lower case, 2nd consecutive letter converted to
2474 * upper case.
2475 */
2476 if (!has_lower)
2477 {
2478 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002479 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002480 {
2481#ifdef FEAT_MBYTE
2482 if (has_mbyte)
2483 c = mb_ptr2char_adv(&p);
2484 else
2485#endif
2486 c = *(p++);
2487 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2488 {
2489 /* Rule 2 is satisfied. */
2490 for (i = actual_compl_length; i < actual_len; ++i)
2491 wca[i] = MB_TOUPPER(wca[i]);
2492 break;
2493 }
2494 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2495 }
2496 }
2497
2498 /* Copy the original case of the part we typed. */
2499 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002500 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002501 {
2502#ifdef FEAT_MBYTE
2503 if (has_mbyte)
2504 c = mb_ptr2char_adv(&p);
2505 else
2506#endif
2507 c = *(p++);
2508 if (MB_ISLOWER(c))
2509 wca[i] = MB_TOLOWER(wca[i]);
2510 else if (MB_ISUPPER(c))
2511 wca[i] = MB_TOUPPER(wca[i]);
2512 }
2513
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002514 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002515 * Generate encoding specific output from wide character array.
2516 * Multi-byte characters can occupy up to five bytes more than
2517 * ASCII characters, and we also need one byte for NUL, so stay
2518 * six bytes away from the edge of IObuff.
2519 */
2520 p = IObuff;
2521 i = 0;
2522 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2523#ifdef FEAT_MBYTE
2524 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002525 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002526 else
2527#endif
2528 *(p++) = wca[i++];
2529 *p = NUL;
2530
2531 vim_free(wca);
2532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002534 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2535 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002537 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538}
2539
2540/*
2541 * Add a match to the list of matches.
2542 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002543 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002544 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002547ins_compl_add(
2548 char_u *str,
2549 int len,
2550 int icase,
2551 char_u *fname,
2552 char_u **cptext, /* extra text for popup menu or NULL */
2553 int cdir,
2554 int flags,
2555 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002557 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002558 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
2560 ui_breakcheck();
2561 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002562 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (len < 0)
2564 len = (int)STRLEN(str);
2565
2566 /*
2567 * If the same match is already present, don't add it.
2568 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002569 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002571 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 do
2573 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002574 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002575 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002576 && match->cp_str[len] == NUL)
2577 return NOTDONE;
2578 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002579 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
2581
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002582 /* Remove any popup menu before changing the list of matches. */
2583 ins_compl_del_pum();
2584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 /*
2586 * Allocate a new match structure.
2587 * Copy the values to the new match structure.
2588 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002589 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002591 return FAIL;
2592 match->cp_number = -1;
2593 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002594 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002595 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
2597 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002598 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002600 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002603 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2605 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002606 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002607 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002608 && compl_curr_match->cp_fname != NULL
2609 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002610 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002611 else if (fname != NULL)
2612 {
2613 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002614 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002617 match->cp_fname = NULL;
2618 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002619
2620 if (cptext != NULL)
2621 {
2622 int i;
2623
2624 for (i = 0; i < CPT_COUNT; ++i)
2625 if (cptext[i] != NULL && *cptext[i] != NUL)
2626 match->cp_text[i] = vim_strsave(cptext[i]);
2627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
2629 /*
2630 * Link the new match structure in the list of matches.
2631 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002632 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002633 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 else if (dir == FORWARD)
2635 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002636 match->cp_next = compl_curr_match->cp_next;
2637 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639 else /* BACKWARD */
2640 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002641 match->cp_next = compl_curr_match;
2642 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002644 if (match->cp_next)
2645 match->cp_next->cp_prev = match;
2646 if (match->cp_prev)
2647 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002649 compl_first_match = match;
2650 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002652 /*
2653 * Find the longest common string if still doing that.
2654 */
2655 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2656 ins_compl_longest_match(match);
2657
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 return OK;
2659}
2660
2661/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002662 * Return TRUE if "str[len]" matches with match->cp_str, considering
2663 * match->cp_icase.
2664 */
2665 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002666ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002667{
2668 if (match->cp_icase)
2669 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2670 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2671}
2672
2673/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002674 * Reduce the longest common string for match "match".
2675 */
2676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002677ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002678{
2679 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002680 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002681 int had_match;
2682
2683 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002684 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002685 /* First match, use it as a whole. */
2686 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002687 if (compl_leader != NULL)
2688 {
2689 had_match = (curwin->w_cursor.col > compl_col);
2690 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002691 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002692 ins_redraw(FALSE);
2693
2694 /* When the match isn't there (to avoid matching itself) remove it
2695 * again after redrawing. */
2696 if (!had_match)
2697 ins_compl_delete();
2698 compl_used_match = FALSE;
2699 }
2700 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002701 else
2702 {
2703 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002704 p = compl_leader;
2705 s = match->cp_str;
2706 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002707 {
2708#ifdef FEAT_MBYTE
2709 if (has_mbyte)
2710 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002711 c1 = mb_ptr2char(p);
2712 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002713 }
2714 else
2715#endif
2716 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002717 c1 = *p;
2718 c2 = *s;
2719 }
2720 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2721 : (c1 != c2))
2722 break;
2723#ifdef FEAT_MBYTE
2724 if (has_mbyte)
2725 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002726 MB_PTR_ADV(p);
2727 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002728 }
2729 else
2730#endif
2731 {
2732 ++p;
2733 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002734 }
2735 }
2736
2737 if (*p != NUL)
2738 {
2739 /* Leader was shortened, need to change the inserted text. */
2740 *p = NUL;
2741 had_match = (curwin->w_cursor.col > compl_col);
2742 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002743 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002744 ins_redraw(FALSE);
2745
2746 /* When the match isn't there (to avoid matching itself) remove it
2747 * again after redrawing. */
2748 if (!had_match)
2749 ins_compl_delete();
2750 }
2751
2752 compl_used_match = FALSE;
2753 }
2754}
2755
2756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 * Add an array of matches to the list of matches.
2758 * Frees matches[].
2759 */
2760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002761ins_compl_add_matches(
2762 int num_matches,
2763 char_u **matches,
2764 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
2766 int i;
2767 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002768 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
Bram Moolenaar572cb562005-08-05 21:35:02 +00002770 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002771 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002772 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002774 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 FreeWild(num_matches, matches);
2776}
2777
2778/* Make the completion list cyclic.
2779 * Return the number of matches (excluding the original).
2780 */
2781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002782ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002784 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 int count = 0;
2786
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002787 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
2789 /*
2790 * Find the end of the list.
2791 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002792 match = compl_first_match;
2793 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002794 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002796 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 ++count;
2798 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002799 match->cp_next = compl_first_match;
2800 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802 return count;
2803}
2804
Bram Moolenaara94bc432006-03-10 21:42:59 +00002805/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002806 * Set variables that store noselect and noinsert behavior from the
2807 * 'completeopt' value.
2808 */
2809 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002810completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002811{
2812 compl_no_insert = FALSE;
2813 compl_no_select = FALSE;
2814 if (strstr((char *)p_cot, "noselect") != NULL)
2815 compl_no_select = TRUE;
2816 if (strstr((char *)p_cot, "noinsert") != NULL)
2817 compl_no_insert = TRUE;
2818}
2819
2820/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002821 * Start completion for the complete() function.
2822 * "startcol" is where the matched text starts (1 is first column).
2823 * "list" is the list of matches.
2824 */
2825 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002826set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002827{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002828 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002829 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002830
Bram Moolenaara94bc432006-03-10 21:42:59 +00002831 /* If already doing completions stop it. */
2832 if (ctrl_x_mode != 0)
2833 ins_compl_prep(' ');
2834 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002835 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002836
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002837 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002838 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002839 startcol = curwin->w_cursor.col;
2840 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002841 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002842 /* compl_pattern doesn't need to be set */
2843 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2844 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002845 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002846 return;
2847
Bram Moolenaare4214502015-03-05 18:08:43 +01002848 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002849
2850 ins_compl_add_list(list);
2851 compl_matches = ins_compl_make_cyclic();
2852 compl_started = TRUE;
2853 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002854 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002855
2856 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002857 if (compl_no_insert || compl_no_select)
2858 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002859 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002860 if (compl_no_select)
2861 /* Down/Up has no real effect. */
2862 ins_complete(K_UP, FALSE);
2863 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002864 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002865 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002866 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002867
2868 /* Lazily show the popup menu, unless we got interrupted. */
2869 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002870 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002871 out_flush();
2872}
2873
2874
Bram Moolenaar9372a112005-12-06 19:59:18 +00002875/* "compl_match_array" points the currently displayed list of entries in the
2876 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002877static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002878static int compl_match_arraysize;
2879
2880/*
2881 * Update the screen and when there is any scrolling remove the popup menu.
2882 */
2883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002884ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002885{
2886 int h;
2887
2888 if (compl_match_array != NULL)
2889 {
2890 h = curwin->w_cline_height;
2891 update_screen(0);
2892 if (h != curwin->w_cline_height)
2893 ins_compl_del_pum();
2894 }
2895}
2896
2897/*
2898 * Remove any popup menu.
2899 */
2900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002901ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002902{
2903 if (compl_match_array != NULL)
2904 {
2905 pum_undisplay();
2906 vim_free(compl_match_array);
2907 compl_match_array = NULL;
2908 }
2909}
2910
2911/*
2912 * Return TRUE if the popup menu should be displayed.
2913 */
2914 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002915pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002916{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002917 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002918 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002919 return FALSE;
2920
2921 /* The display looks bad on a B&W display. */
2922 if (t_colors < 8
2923#ifdef FEAT_GUI
2924 && !gui.in_use
2925#endif
2926 )
2927 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002928 return TRUE;
2929}
2930
2931/*
2932 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002933 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002934 */
2935 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002936pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002937{
2938 compl_T *compl;
2939 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002940
2941 /* Don't display the popup menu if there are no matches or there is only
2942 * one (ignoring the original text). */
2943 compl = compl_first_match;
2944 i = 0;
2945 do
2946 {
2947 if (compl == NULL
2948 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2949 break;
2950 compl = compl->cp_next;
2951 } while (compl != compl_first_match);
2952
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002953 if (strstr((char *)p_cot, "menuone") != NULL)
2954 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002955 return (i >= 2);
2956}
2957
2958/*
2959 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002960 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002961 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002963ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002964{
2965 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002966 compl_T *shown_compl = NULL;
2967 int did_find_shown_match = FALSE;
2968 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002969 int i;
2970 int cur = -1;
2971 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002972 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002973
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002974 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002975 return;
2976
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002977#if defined(FEAT_EVAL)
2978 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2979 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2980#endif
2981
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002982 /* Update the screen before drawing the popup menu over it. */
2983 update_screen(0);
2984
2985 if (compl_match_array == NULL)
2986 {
2987 /* Need to build the popup menu list. */
2988 compl_match_arraysize = 0;
2989 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002990 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002991 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002992 do
2993 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002994 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2995 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002996 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002997 ++compl_match_arraysize;
2998 compl = compl->cp_next;
2999 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003000 if (compl_match_arraysize == 0)
3001 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003002 compl_match_array = (pumitem_T *)alloc_clear(
3003 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003004 * compl_match_arraysize));
3005 if (compl_match_array != NULL)
3006 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003007 /* If the current match is the original text don't find the first
3008 * match after it, don't highlight anything. */
3009 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3010 shown_match_ok = TRUE;
3011
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003012 i = 0;
3013 compl = compl_first_match;
3014 do
3015 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003016 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3017 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003018 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003019 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003020 if (!shown_match_ok)
3021 {
3022 if (compl == compl_shown_match || did_find_shown_match)
3023 {
3024 /* This item is the shown match or this is the
3025 * first displayed item after the shown match. */
3026 compl_shown_match = compl;
3027 did_find_shown_match = TRUE;
3028 shown_match_ok = TRUE;
3029 }
3030 else
3031 /* Remember this displayed match for when the
3032 * shown match is just below it. */
3033 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003034 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003035 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003036
3037 if (compl->cp_text[CPT_ABBR] != NULL)
3038 compl_match_array[i].pum_text =
3039 compl->cp_text[CPT_ABBR];
3040 else
3041 compl_match_array[i].pum_text = compl->cp_str;
3042 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3043 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3044 if (compl->cp_text[CPT_MENU] != NULL)
3045 compl_match_array[i++].pum_extra =
3046 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003047 else
3048 compl_match_array[i++].pum_extra = compl->cp_fname;
3049 }
3050
3051 if (compl == compl_shown_match)
3052 {
3053 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003054
3055 /* When the original text is the shown match don't set
3056 * compl_shown_match. */
3057 if (compl->cp_flags & ORIGINAL_TEXT)
3058 shown_match_ok = TRUE;
3059
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003060 if (!shown_match_ok && shown_compl != NULL)
3061 {
3062 /* The shown match isn't displayed, set it to the
3063 * previously displayed match. */
3064 compl_shown_match = shown_compl;
3065 shown_match_ok = TRUE;
3066 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003067 }
3068 compl = compl->cp_next;
3069 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003070
3071 if (!shown_match_ok) /* no displayed match at all */
3072 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003073 }
3074 }
3075 else
3076 {
3077 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003078 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003079 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3080 || compl_match_array[i].pum_text
3081 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003082 {
3083 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003084 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003085 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003086 }
3087
3088 if (compl_match_array != NULL)
3089 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003090 /* In Replace mode when a $ is displayed at the end of the line only
3091 * part of the screen would be updated. We do need to redraw here. */
3092 dollar_vcol = -1;
3093
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003094 /* Compute the screen column of the start of the completed text.
3095 * Use the cursor to get all wrapping and other settings right. */
3096 col = curwin->w_cursor.col;
3097 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003098 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003099 curwin->w_cursor.col = col;
3100 }
3101}
3102
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103#define DICT_FIRST (1) /* use just first element in "dict" */
3104#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003105
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003107 * Add any identifiers that match the given pattern in the list of dictionary
3108 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 */
3110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003111ins_compl_dictionaries(
3112 char_u *dict_start,
3113 char_u *pat,
3114 int flags, /* DICT_FIRST and/or DICT_EXACT */
3115 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003117 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 char_u *ptr;
3119 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 char_u **files;
3122 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003124 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125
Bram Moolenaar0b238792006-03-02 22:49:12 +00003126 if (*dict == NUL)
3127 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003128#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003129 /* When 'dictionary' is empty and spell checking is enabled use
3130 * "spell". */
3131 if (!thesaurus && curwin->w_p_spell)
3132 dict = (char_u *)"spell";
3133 else
3134#endif
3135 return;
3136 }
3137
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003139 if (buf == NULL)
3140 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003141 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 /* If 'infercase' is set, don't use 'smartcase' here */
3144 save_p_scs = p_scs;
3145 if (curbuf->b_p_inf)
3146 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003147
3148 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3149 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003150 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003151 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003152 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003153 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003154 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003155
3156 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003157 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003158 len = STRLEN(pat_esc) + 10;
3159 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003160 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003161 {
3162 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003163 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003164 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003165 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003166 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3167 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003168 vim_free(ptr);
3169 }
3170 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003171 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003172 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003173 if (regmatch.regprog == NULL)
3174 goto theend;
3175 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003176
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3178 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003179 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
3181 /* copy one dictionary file name into buf */
3182 if (flags == DICT_EXACT)
3183 {
3184 count = 1;
3185 files = &dict;
3186 }
3187 else
3188 {
3189 /* Expand wildcards in the dictionary name, but do not allow
3190 * backticks (for security, the 'dict' option may have been set in
3191 * a modeline). */
3192 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003193# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003194 if (!thesaurus && STRCMP(buf, "spell") == 0)
3195 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003196 else
3197# endif
3198 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 || expand_wildcards(1, &buf, &count, &files,
3200 EW_FILE|EW_SILENT) != OK)
3201 count = 0;
3202 }
3203
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003204# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003205 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003207 /* Complete from active spelling. Skip "\<" in the pattern, we
3208 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003209 if (pat[0] == '\\' && pat[1] == '<')
3210 ptr = pat + 2;
3211 else
3212 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003213 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003215 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003216# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003217 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003218 {
3219 ins_compl_files(count, files, thesaurus, flags,
3220 &regmatch, buf, &dir);
3221 if (flags != DICT_EXACT)
3222 FreeWild(count, files);
3223 }
3224 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 break;
3226 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003227
3228theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003230 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 vim_free(buf);
3232}
3233
Bram Moolenaar0b238792006-03-02 22:49:12 +00003234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235ins_compl_files(
3236 int count,
3237 char_u **files,
3238 int thesaurus,
3239 int flags,
3240 regmatch_T *regmatch,
3241 char_u *buf,
3242 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003243{
3244 char_u *ptr;
3245 int i;
3246 FILE *fp;
3247 int add_r;
3248
3249 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3250 {
3251 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3252 if (flags != DICT_EXACT)
3253 {
3254 vim_snprintf((char *)IObuff, IOSIZE,
3255 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003256 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003257 }
3258
3259 if (fp != NULL)
3260 {
3261 /*
3262 * Read dictionary file line by line.
3263 * Check each line for a match.
3264 */
3265 while (!got_int && !compl_interrupted
3266 && !vim_fgets(buf, LSIZE, fp))
3267 {
3268 ptr = buf;
3269 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3270 {
3271 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003272 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003273 ptr = find_line_end(ptr);
3274 else
3275 ptr = find_word_end(ptr);
3276 add_r = ins_compl_add_infercase(regmatch->startp[0],
3277 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003278 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003279 if (thesaurus)
3280 {
3281 char_u *wstart;
3282
3283 /*
3284 * Add the other matches on the line
3285 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003286 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003287 while (!got_int)
3288 {
3289 /* Find start of the next word. Skip white
3290 * space and punctuation. */
3291 ptr = find_word_start(ptr);
3292 if (*ptr == NUL || *ptr == NL)
3293 break;
3294 wstart = ptr;
3295
Bram Moolenaara2993e12007-08-12 14:38:46 +00003296 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003297#ifdef FEAT_MBYTE
3298 if (has_mbyte)
3299 /* Japanese words may have characters in
3300 * different classes, only separate words
3301 * with single-byte non-word characters. */
3302 while (*ptr != NUL)
3303 {
3304 int l = (*mb_ptr2len)(ptr);
3305
3306 if (l < 2 && !vim_iswordc(*ptr))
3307 break;
3308 ptr += l;
3309 }
3310 else
3311#endif
3312 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003313
3314 /* Add the word. Skip the regexp match. */
3315 if (wstart != regmatch->startp[0])
3316 add_r = ins_compl_add_infercase(wstart,
3317 (int)(ptr - wstart),
3318 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003319 }
3320 }
3321 if (add_r == OK)
3322 /* if dir was BACKWARD then honor it just once */
3323 *dir = FORWARD;
3324 else if (add_r == FAIL)
3325 break;
3326 /* avoid expensive call to vim_regexec() when at end
3327 * of line */
3328 if (*ptr == '\n' || got_int)
3329 break;
3330 }
3331 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003332 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003333 }
3334 fclose(fp);
3335 }
3336 }
3337}
3338
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339/*
3340 * Find the start of the next word.
3341 * Returns a pointer to the first char of the word. Also stops at a NUL.
3342 */
3343 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003344find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346#ifdef FEAT_MBYTE
3347 if (has_mbyte)
3348 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003349 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 else
3351#endif
3352 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3353 ++ptr;
3354 return ptr;
3355}
3356
3357/*
3358 * Find the end of the word. Assumes it starts inside a word.
3359 * Returns a pointer to just after the word.
3360 */
3361 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003362find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
3364#ifdef FEAT_MBYTE
3365 int start_class;
3366
3367 if (has_mbyte)
3368 {
3369 start_class = mb_get_class(ptr);
3370 if (start_class > 1)
3371 while (*ptr != NUL)
3372 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003373 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (mb_get_class(ptr) != start_class)
3375 break;
3376 }
3377 }
3378 else
3379#endif
3380 while (vim_iswordc(*ptr))
3381 ++ptr;
3382 return ptr;
3383}
3384
3385/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003386 * Find the end of the line, omitting CR and NL at the end.
3387 * Returns a pointer to just after the line.
3388 */
3389 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003390find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003391{
3392 char_u *s;
3393
3394 s = ptr + STRLEN(ptr);
3395 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3396 --s;
3397 return s;
3398}
3399
3400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 * Free the list of completions
3402 */
3403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003404ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003406 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003407 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003409 vim_free(compl_pattern);
3410 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003411 vim_free(compl_leader);
3412 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003414 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003416
3417 ins_compl_del_pum();
3418 pum_clear();
3419
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003420 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 do
3422 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003423 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003424 compl_curr_match = compl_curr_match->cp_next;
3425 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003427 if (match->cp_flags & FREE_FNAME)
3428 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003429 for (i = 0; i < CPT_COUNT; ++i)
3430 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003432 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3433 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003434 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003435 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436}
3437
3438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003439ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003441 compl_cont_status = 0;
3442 compl_started = FALSE;
3443 compl_matches = 0;
3444 vim_free(compl_pattern);
3445 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003446 vim_free(compl_leader);
3447 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003449 vim_free(compl_orig_text);
3450 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003451 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003452 /* clear v:completed_item */
3453 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454}
3455
3456/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003457 * Return TRUE when Insert completion is active.
3458 */
3459 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003460ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003461{
3462 return compl_started;
3463}
3464
3465/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003466 * Delete one character before the cursor and show the subset of the matches
3467 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003468 * Returns the character to be used, NUL if the work is done and another char
3469 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003470 */
3471 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003472ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003473{
3474 char_u *line;
3475 char_u *p;
3476
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003477 line = ml_get_curline();
3478 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003479 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003480
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003481 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003482 * allow the word to be deleted, we won't match everything.
3483 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003484 if ((int)(p - line) - (int)compl_col < 0
3485 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003486 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3487 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3488 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003489 return K_BS;
3490
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003491 /* Deleted more than what was used to find matches or didn't finish
3492 * finding all matches: need to look for matches all over again. */
3493 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003494 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003495 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003496
Bram Moolenaara6557602006-02-04 22:43:20 +00003497 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003498 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003499 if (compl_leader != NULL)
3500 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003501 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003502 if (compl_shown_match != NULL)
3503 /* Make sure current match is not a hidden item. */
3504 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003505 return NUL;
3506 }
3507 return K_BS;
3508}
Bram Moolenaara6557602006-02-04 22:43:20 +00003509
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003510/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003511 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3512 * be called.
3513 */
3514 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003515ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003516{
3517 /* Return TRUE if we didn't complete finding matches or when the
3518 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3519 return compl_was_interrupted
3520 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3521 && compl_opt_refresh_always);
3522}
3523
3524/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003525 * Called after changing "compl_leader".
3526 * Show the popup menu with a different set of matches.
3527 * May also search for matches again if the previous search was interrupted.
3528 */
3529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003530ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003531{
3532 ins_compl_del_pum();
3533 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003534 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003535 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003536
3537 if (compl_started)
3538 ins_compl_set_original_text(compl_leader);
3539 else
3540 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003541#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003542 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003543#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003544 /*
3545 * Matches were cleared, need to search for them now. First display
3546 * the changed text before the cursor. Set "compl_restarting" to
3547 * avoid that the first match is inserted.
3548 */
3549 update_screen(0);
3550#ifdef FEAT_GUI
3551 if (gui.in_use)
3552 {
3553 /* Show the cursor after the match, not after the redrawn text. */
3554 setcursor();
3555 out_flush();
3556 gui_update_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 */
4706 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
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 } */
4726 dict = dict_alloc();
4727 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();
4938 out_flush();
4939 gui_update_cursor(FALSE, FALSE);
4940 }
4941#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004942
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 /* Delete old text to be replaced, since we're still searching and
4944 * don't want to match ourselves! */
4945 ins_compl_delete();
4946 }
4947
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004948 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004949 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004950 if (compl_no_insert && !started)
4951 compl_enter_selects = TRUE;
4952 else
4953 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004954
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 /*
4956 * Show the file name for the match (if any)
4957 * Truncate the file name to avoid a wait for return.
4958 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004959 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02004961 char *lead = _("match in file");
4962 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4963 char_u *s;
4964 char_u *e;
4965
4966 if (space > 0)
4967 {
4968 /* We need the tail that fits. With double-byte encoding going
4969 * back from the end is very slow, thus go from the start and keep
4970 * the text that fits in "space" between "s" and "e". */
4971 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
4972 {
4973 space -= ptr2cells(e);
4974 while (space < 0)
4975 {
4976 space += ptr2cells(s);
4977 MB_PTR_ADV(s);
4978 }
4979 }
4980 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
4981 s > compl_shown_match->cp_fname ? "<" : "", s);
4982 msg(IObuff);
4983 redraw_cmdline = FALSE; /* don't overwrite! */
4984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
4986
4987 return num_matches;
4988}
4989
4990/*
4991 * Call this while finding completions, to check whether the user has hit a key
4992 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004993 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004995 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004996 * "in_compl_func" is TRUE when called from complete_check(), don't set
4997 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 */
4999 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005000ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
5002 static int count = 0;
5003
5004 int c;
5005
5006 /* Don't check when reading keys from a script. That would break the test
5007 * scripts */
5008 if (using_script())
5009 return;
5010
5011 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005012 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 return;
5014 count = 0;
5015
Bram Moolenaara260a972006-06-23 19:36:29 +00005016 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5017 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (c != NUL)
5020 {
5021 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5022 {
5023 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005024 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005025 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005026 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005028 else
5029 {
5030 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005031 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005032 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005033 if (c != K_IGNORE)
5034 {
5035 /* Don't interrupt completion when the character wasn't typed,
5036 * e.g., when doing @q to replay keys. */
5037 if (c != Ctrl_R && KeyTyped)
5038 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005039
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005040 vungetc(c);
5041 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005044 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005045 {
5046 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5047
5048 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005049 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005050 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005051}
5052
5053/*
5054 * Decide the direction of Insert mode complete from the key typed.
5055 * Returns BACKWARD or FORWARD.
5056 */
5057 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005058ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005059{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005060 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005061 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005062 return BACKWARD;
5063 return FORWARD;
5064}
5065
5066/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005067 * Return TRUE for keys that are used for completion only when the popup menu
5068 * is visible.
5069 */
5070 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005071ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005072{
5073 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005074 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5075 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005076}
5077
5078/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005079 * Decide the number of completions to move forward.
5080 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5081 */
5082 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005083ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005084{
5085 int h;
5086
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005087 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005088 {
5089 h = pum_get_height();
5090 if (h > 3)
5091 h -= 2; /* keep some context */
5092 return h;
5093 }
5094 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095}
5096
5097/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005098 * Return TRUE if completion with "c" should insert the match, FALSE if only
5099 * to change the currently selected completion.
5100 */
5101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005102ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005103{
5104 switch (c)
5105 {
5106 case K_UP:
5107 case K_DOWN:
5108 case K_PAGEDOWN:
5109 case K_KPAGEDOWN:
5110 case K_S_DOWN:
5111 case K_PAGEUP:
5112 case K_KPAGEUP:
5113 case K_S_UP:
5114 return FALSE;
5115 }
5116 return TRUE;
5117}
5118
5119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 * Do Insert mode completion.
5121 * Called when character "c" was typed, which has a meaning for completion.
5122 * Returns OK if completion was done, FAIL if something failed (out of mem).
5123 */
5124 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005125ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005127 char_u *line;
5128 int startcol = 0; /* column where searched text starts */
5129 colnr_T curs_col; /* cursor column */
5130 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005131 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005132 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005133 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005134 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135
Bram Moolenaare3226be2005-12-18 22:10:00 +00005136 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005137 insert_match = ins_compl_use_match(c);
5138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005139 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
5141 /* First time we hit ^N or ^P (in a row, I mean) */
5142
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 did_ai = FALSE;
5144#ifdef FEAT_SMARTINDENT
5145 did_si = FALSE;
5146 can_si = FALSE;
5147 can_si_back = FALSE;
5148#endif
5149 if (stop_arrow() == FAIL)
5150 return FAIL;
5151
5152 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005153 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005154 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005156 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005157 * "compl_startpos" to the cursor as a pattern to add a new word
5158 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005159 * "compl_startpos" is not in the same line as the cursor then fix it
5160 * (the line has been split because it was longer than 'tw'). if SOL
5161 * is set then skip the previous pattern, a word at the beginning of
5162 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005163 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5164 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
5166 /*
5167 * it is a continued search
5168 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005169 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5171 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5172 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005173 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005175 /* line (probably) wrapped, set compl_startpos to the
5176 * first non_blank in the line, if it is not a wordchar
5177 * include it to get a better pattern, but then we don't
5178 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005179 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005180 compl_startpos.col = compl_col;
5181 compl_startpos.lnum = curwin->w_cursor.lnum;
5182 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
5184 else
5185 {
5186 /* S_IPOS was set when we inserted a word that was at the
5187 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005188 * mode but first we need to redefine compl_startpos */
5189 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005191 compl_cont_status |= CONT_SOL;
5192 compl_startpos.col = (colnr_T)(skipwhite(
5193 line + compl_length
5194 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005196 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005198 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005199 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005200 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005202 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005204 compl_cont_status &= ~CONT_SOL;
5205 compl_length = (IOSIZE - MIN_SPACE);
5206 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005208 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5209 if (compl_length < 1)
5210 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005212 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005213 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005215 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005218 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005220 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005222 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005224 compl_cont_status = 0;
5225 compl_cont_status |= CONT_N_ADDS;
5226 compl_startpos = curwin->w_cursor;
5227 startcol = (int)curs_col;
5228 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230
5231 /* Work out completion pattern and original text -- webb */
5232 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5233 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005234 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5236 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005237 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005239 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005241 compl_col += ++startcol;
5242 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005245 compl_pattern = str_foldcase(line + compl_col,
5246 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005248 compl_pattern = vim_strnsave(line + compl_col,
5249 compl_length);
5250 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 return FAIL;
5252 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005253 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 {
5255 char_u *prefix = (char_u *)"\\<";
5256
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005257 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005258 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005259 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005260 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005262 if (!vim_iswordp(line + compl_col)
5263 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 && (
5265#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005266 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005268 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269#endif
5270 )))
5271 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005272 STRCPY((char *)compl_pattern, prefix);
5273 (void)quote_meta(compl_pattern + STRLEN(prefix),
5274 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005276 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005278 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281#endif
5282 )
5283 {
5284 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005285 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5286 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 compl_col += curs_col;
5289 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
5291 else
5292 {
5293#ifdef FEAT_MBYTE
5294 /* Search the point of change class of multibyte character
5295 * or not a word single byte character backward. */
5296 if (has_mbyte)
5297 {
5298 int base_class;
5299 int head_off;
5300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 startcol -= (*mb_head_off)(line, line + startcol);
5302 base_class = mb_get_class(line + startcol);
5303 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005305 head_off = (*mb_head_off)(line, line + startcol);
5306 if (base_class != mb_get_class(line + startcol
5307 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005309 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 }
5311 }
5312 else
5313#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005314 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005316 compl_col += ++startcol;
5317 compl_length = (int)curs_col - startcol;
5318 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
5320 /* Only match word with at least two chars -- webb
5321 * there's no need to call quote_meta,
5322 * alloc(7) is enough -- Acevedo
5323 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005324 compl_pattern = alloc(7);
5325 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005327 STRCPY((char *)compl_pattern, "\\<");
5328 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5329 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331 else
5332 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005333 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005334 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 STRCPY((char *)compl_pattern, "\\<");
5338 (void)quote_meta(compl_pattern + 2, line + compl_col,
5339 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
5341 }
5342 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005343 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005345 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005346 compl_length = (int)curs_col - (int)compl_col;
5347 if (compl_length < 0) /* cursor in indent: empty pattern */
5348 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005350 compl_pattern = str_foldcase(line + compl_col, compl_length,
5351 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005353 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5354 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 return FAIL;
5356 }
5357 else if (ctrl_x_mode == CTRL_X_FILES)
5358 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005359 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005360 if (startcol > 0)
5361 {
5362 char_u *p = line + startcol;
5363
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005364 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005365 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005366 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005367 if (p == line && vim_isfilec(PTR2CHAR(p)))
5368 startcol = 0;
5369 else
5370 startcol = (int)(p - line) + 1;
5371 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005372
Bram Moolenaar0300e462013-09-08 16:03:45 +02005373 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005374 compl_length = (int)curs_col - startcol;
5375 compl_pattern = addstar(line + compl_col, compl_length,
5376 EXPAND_FILES);
5377 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 return FAIL;
5379 }
5380 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5381 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005382 compl_pattern = vim_strnsave(line, curs_col);
5383 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005385 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005386 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005387 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5388 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005389 /* No completion possible, use an empty pattern to get a
5390 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005391 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005392 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005393 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5394 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005396 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005397 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005398#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005399 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005400 * Call user defined function 'completefunc' with "a:findstart"
5401 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005402 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005403 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005404 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005405 char_u *funcname;
5406 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005407 win_T *curwin_save;
5408 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005409
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005410 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005411 * string */
5412 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5413 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5414 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005415 {
5416 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5417 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005418 /* restore did_ai, so that adding comment leader works */
5419 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005420 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005421 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005422
5423 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005424 args[1] = NULL;
5425 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005426 curwin_save = curwin;
5427 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005428 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005429 if (curwin_save != curwin || curbuf_save != curbuf)
5430 {
5431 EMSG(_(e_complwin));
5432 return FAIL;
5433 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005434 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005435 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005436 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005437 {
5438 EMSG(_(e_compldel));
5439 return FAIL;
5440 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005441
Bram Moolenaar6110a002012-01-26 18:58:38 +01005442 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005443 * cancel the complete without an error.
5444 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005445 if (col == -2)
5446 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005447 if (col == -3)
5448 {
5449 ctrl_x_mode = 0;
5450 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005451 if (!shortmess(SHM_COMPLETIONMENU))
5452 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005453 return FAIL;
5454 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005455
Bram Moolenaar82139082011-09-14 16:52:09 +02005456 /*
5457 * Reset extended parameters of completion, when start new
5458 * completion.
5459 */
5460 compl_opt_refresh_always = FALSE;
5461
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005462 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005463 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005464 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005465 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005466 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005467
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005468 /* Setup variables for completion. Need to obtain "line" again,
5469 * it may have become invalid. */
5470 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005471 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005472 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5473 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005474#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005475 return FAIL;
5476 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005477 else if (ctrl_x_mode == CTRL_X_SPELL)
5478 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005479#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005480 if (spell_bad_len > 0)
5481 compl_col = curs_col - spell_bad_len;
5482 else
5483 compl_col = spell_word_start(startcol);
5484 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005485 {
5486 compl_length = 0;
5487 compl_col = curs_col;
5488 }
5489 else
5490 {
5491 spell_expand_check_cap(compl_col);
5492 compl_length = (int)curs_col - compl_col;
5493 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005494 /* Need to obtain "line" again, it may have become invalid. */
5495 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005496 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5497 if (compl_pattern == NULL)
5498#endif
5499 return FAIL;
5500 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005501 else
5502 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005503 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005504 return FAIL;
5505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005507 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 {
5509 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005510 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 {
5512 /* Insert a new line, keep indentation but ignore 'comments' */
5513#ifdef FEAT_COMMENTS
5514 char_u *old = curbuf->b_p_com;
5515
5516 curbuf->b_p_com = (char_u *)"";
5517#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005518 compl_startpos.lnum = curwin->w_cursor.lnum;
5519 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 ins_eol('\r');
5521#ifdef FEAT_COMMENTS
5522 curbuf->b_p_com = old;
5523#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005524 compl_length = 0;
5525 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 }
5527 }
5528 else
5529 {
5530 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005531 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 }
5533
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005534 if (compl_cont_status & CONT_LOCAL)
5535 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 else
5537 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5538
Bram Moolenaar62951b12011-09-21 18:23:05 +02005539 /* If any of the original typed text has been changed we need to fix
5540 * the redo buffer. */
5541 ins_compl_fixRedoBufForLeader(NULL);
5542
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005543 /* Always add completion for the original text. */
5544 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005545 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5546 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005547 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005549 vim_free(compl_pattern);
5550 compl_pattern = NULL;
5551 vim_free(compl_orig_text);
5552 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 return FAIL;
5554 }
5555
5556 /* showmode might reset the internal line pointers, so it must
5557 * be called before line = ml_get(), or when this address is no
5558 * longer needed. -- Acevedo.
5559 */
5560 edit_submode_extra = (char_u *)_("-- Searching...");
5561 edit_submode_highl = HLF_COUNT;
5562 showmode();
5563 edit_submode_extra = NULL;
5564 out_flush();
5565 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005566 else if (insert_match && stop_arrow() == FAIL)
5567 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005569 compl_shown_match = compl_curr_match;
5570 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
5572 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005573 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005575 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005576 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005577 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005579 /* may undisplay the popup menu */
5580 ins_compl_upd_pum();
5581
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005582 if (n > 1) /* all matches have been found */
5583 compl_matches = n;
5584 compl_curr_match = compl_shown_match;
5585 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
Bram Moolenaard68071d2006-05-02 22:08:30 +00005587 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5588 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 if (got_int && !global_busy)
5590 {
5591 (void)vgetc();
5592 got_int = FALSE;
5593 }
5594
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005595 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005596 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005598 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5599 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5601 edit_submode_highl = HLF_E;
5602 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5603 * because we couldn't expand anything at first place, but if we used
5604 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5605 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005606 if ( compl_length > 1
5607 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 || (ctrl_x_mode != 0
5609 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5610 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005611 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
5613
Bram Moolenaar572cb562005-08-05 21:35:02 +00005614 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005615 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005617 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
5619 if (edit_submode_extra == NULL)
5620 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005621 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 {
5623 edit_submode_extra = (char_u *)_("Back at original");
5624 edit_submode_highl = HLF_W;
5625 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005626 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 {
5628 edit_submode_extra = (char_u *)_("Word from other line");
5629 edit_submode_highl = HLF_COUNT;
5630 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005631 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 {
5633 edit_submode_extra = (char_u *)_("The only match");
5634 edit_submode_highl = HLF_COUNT;
5635 }
5636 else
5637 {
5638 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005639 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005641 int number = 0;
5642 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005644 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
5646 /* search backwards for the first valid (!= -1) number.
5647 * This should normally succeed already at the first loop
5648 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005649 for (match = compl_curr_match->cp_prev; match != NULL
5650 && match != compl_first_match;
5651 match = match->cp_prev)
5652 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005654 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 break;
5656 }
5657 if (match != NULL)
5658 /* go up and assign all numbers which are not assigned
5659 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005660 for (match = match->cp_next;
5661 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005662 match = match->cp_next)
5663 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
5665 else /* BACKWARD */
5666 {
5667 /* search forwards (upwards) for the first valid (!= -1)
5668 * number. This should normally succeed already at the
5669 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005670 for (match = compl_curr_match->cp_next; match != NULL
5671 && match != compl_first_match;
5672 match = match->cp_next)
5673 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005675 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 break;
5677 }
5678 if (match != NULL)
5679 /* go down and assign all numbers which are not
5680 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005681 for (match = match->cp_prev; match
5682 && match->cp_number == -1;
5683 match = match->cp_prev)
5684 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 }
5687
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005688 /* The match should always have a sequence number now, this is
5689 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005690 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005692 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5693 * Translations may need more than twice that. */
5694 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005696 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005697 vim_snprintf((char *)match_ref, sizeof(match_ref),
5698 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005699 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005701 vim_snprintf((char *)match_ref, sizeof(match_ref),
5702 _("match %d"),
5703 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 edit_submode_extra = match_ref;
5705 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005706 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 curs_columns(FALSE);
5708 }
5709 }
5710 }
5711
5712 /* Show a message about what (completion) mode we're in. */
5713 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005714 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005716 if (edit_submode_extra != NULL)
5717 {
5718 if (!p_smd)
5719 msg_attr(edit_submode_extra,
5720 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005721 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005722 }
5723 else
5724 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
Bram Moolenaard68071d2006-05-02 22:08:30 +00005727 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005728 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005729 show_pum(save_w_wrow, save_w_leftcol);
5730
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005731 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005732 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005733
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 return OK;
5735}
5736
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005737 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005738show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005739{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005740 /* RedrawingDisabled may be set when invoked through complete(). */
5741 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005742
Bram Moolenaarcb036422017-03-01 12:29:10 +01005743 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005744
Bram Moolenaarcb036422017-03-01 12:29:10 +01005745 /* If the cursor moved or the display scrolled we need to remove the pum
5746 * first. */
5747 setcursor();
5748 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5749 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005750
Bram Moolenaarcb036422017-03-01 12:29:10 +01005751 ins_compl_show_pum();
5752 setcursor();
5753 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005754}
5755
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756/*
5757 * Looks in the first "len" chars. of "src" for search-metachars.
5758 * If dest is not NULL the chars. are copied there quoting (with
5759 * a backslash) the metachars, and dest would be NUL terminated.
5760 * Returns the length (needed) of dest
5761 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005762 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005763quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005765 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005767 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 {
5769 switch (*src)
5770 {
5771 case '.':
5772 case '*':
5773 case '[':
5774 if (ctrl_x_mode == CTRL_X_DICTIONARY
5775 || ctrl_x_mode == CTRL_X_THESAURUS)
5776 break;
5777 case '~':
5778 if (!p_magic) /* quote these only if magic is set */
5779 break;
5780 case '\\':
5781 if (ctrl_x_mode == CTRL_X_DICTIONARY
5782 || ctrl_x_mode == CTRL_X_THESAURUS)
5783 break;
5784 case '^': /* currently it's not needed. */
5785 case '$':
5786 m++;
5787 if (dest != NULL)
5788 *dest++ = '\\';
5789 break;
5790 }
5791 if (dest != NULL)
5792 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005793# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 /* Copy remaining bytes of a multibyte character. */
5795 if (has_mbyte)
5796 {
5797 int i, mb_len;
5798
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005799 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 if (mb_len > 0 && len >= mb_len)
5801 for (i = 0; i < mb_len; ++i)
5802 {
5803 --len;
5804 ++src;
5805 if (dest != NULL)
5806 *dest++ = *src;
5807 }
5808 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
5811 if (dest != NULL)
5812 *dest = NUL;
5813
5814 return m;
5815}
5816#endif /* FEAT_INS_EXPAND */
5817
5818/*
5819 * Next character is interpreted literally.
5820 * A one, two or three digit decimal number is interpreted as its byte value.
5821 * If one or two digits are entered, the next character is given to vungetc().
5822 * For Unicode a character > 255 may be returned.
5823 */
5824 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005825get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826{
5827 int cc;
5828 int nc;
5829 int i;
5830 int hex = FALSE;
5831 int octal = FALSE;
5832#ifdef FEAT_MBYTE
5833 int unicode = 0;
5834#endif
5835
5836 if (got_int)
5837 return Ctrl_C;
5838
5839#ifdef FEAT_GUI
5840 /*
5841 * In GUI there is no point inserting the internal code for a special key.
5842 * It is more useful to insert the string "<KEY>" instead. This would
5843 * probably be useful in a text window too, but it would not be
5844 * vi-compatible (maybe there should be an option for it?) -- webb
5845 */
5846 if (gui.in_use)
5847 ++allow_keys;
5848#endif
5849#ifdef USE_ON_FLY_SCROLL
5850 dont_scroll = TRUE; /* disallow scrolling here */
5851#endif
5852 ++no_mapping; /* don't map the next key hits */
5853 cc = 0;
5854 i = 0;
5855 for (;;)
5856 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005857 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858#ifdef FEAT_CMDL_INFO
5859 if (!(State & CMDLINE)
5860# ifdef FEAT_MBYTE
5861 && MB_BYTE2LEN_CHECK(nc) == 1
5862# endif
5863 )
5864 add_to_showcmd(nc);
5865#endif
5866 if (nc == 'x' || nc == 'X')
5867 hex = TRUE;
5868 else if (nc == 'o' || nc == 'O')
5869 octal = TRUE;
5870#ifdef FEAT_MBYTE
5871 else if (nc == 'u' || nc == 'U')
5872 unicode = nc;
5873#endif
5874 else
5875 {
5876 if (hex
5877#ifdef FEAT_MBYTE
5878 || unicode != 0
5879#endif
5880 )
5881 {
5882 if (!vim_isxdigit(nc))
5883 break;
5884 cc = cc * 16 + hex2nr(nc);
5885 }
5886 else if (octal)
5887 {
5888 if (nc < '0' || nc > '7')
5889 break;
5890 cc = cc * 8 + nc - '0';
5891 }
5892 else
5893 {
5894 if (!VIM_ISDIGIT(nc))
5895 break;
5896 cc = cc * 10 + nc - '0';
5897 }
5898
5899 ++i;
5900 }
5901
5902 if (cc > 255
5903#ifdef FEAT_MBYTE
5904 && unicode == 0
5905#endif
5906 )
5907 cc = 255; /* limit range to 0-255 */
5908 nc = 0;
5909
5910 if (hex) /* hex: up to two chars */
5911 {
5912 if (i >= 2)
5913 break;
5914 }
5915#ifdef FEAT_MBYTE
5916 else if (unicode) /* Unicode: up to four or eight chars */
5917 {
5918 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5919 break;
5920 }
5921#endif
5922 else if (i >= 3) /* decimal or octal: up to three chars */
5923 break;
5924 }
5925 if (i == 0) /* no number entered */
5926 {
5927 if (nc == K_ZERO) /* NUL is stored as NL */
5928 {
5929 cc = '\n';
5930 nc = 0;
5931 }
5932 else
5933 {
5934 cc = nc;
5935 nc = 0;
5936 }
5937 }
5938
5939 if (cc == 0) /* NUL is stored as NL */
5940 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005941#ifdef FEAT_MBYTE
5942 if (enc_dbcs && (cc & 0xff) == 0)
5943 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5944 second byte will cause trouble! */
5945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946
5947 --no_mapping;
5948#ifdef FEAT_GUI
5949 if (gui.in_use)
5950 --allow_keys;
5951#endif
5952 if (nc)
5953 vungetc(nc);
5954 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5955 return cc;
5956}
5957
5958/*
5959 * Insert character, taking care of special keys and mod_mask
5960 */
5961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005962insert_special(
5963 int c,
5964 int allow_modmask,
5965 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966{
5967 char_u *p;
5968 int len;
5969
5970 /*
5971 * Special function key, translate into "<Key>". Up to the last '>' is
5972 * inserted with ins_str(), so as not to replace characters in replace
5973 * mode.
5974 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5975 * unless 'allow_modmask' is TRUE.
5976 */
5977#ifdef MACOS
5978 /* Command-key never produces a normal key */
5979 if (mod_mask & MOD_MASK_CMD)
5980 allow_modmask = TRUE;
5981#endif
5982 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5983 {
5984 p = get_special_key_name(c, mod_mask);
5985 len = (int)STRLEN(p);
5986 c = p[len - 1];
5987 if (len > 2)
5988 {
5989 if (stop_arrow() == FAIL)
5990 return;
5991 p[len - 1] = NUL;
5992 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005993 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 ctrlv = FALSE;
5995 }
5996 }
5997 if (stop_arrow() == OK)
5998 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5999}
6000
6001/*
6002 * Special characters in this context are those that need processing other
6003 * than the simple insertion that can be performed here. This includes ESC
6004 * which terminates the insert, and CR/NL which need special processing to
6005 * open up a new line. This routine tries to optimize insertions performed by
6006 * the "redo", "undo" or "put" commands, so it needs to know when it should
6007 * stop and defer processing to the "normal" mechanism.
6008 * '0' and '^' are special, because they can be followed by CTRL-D.
6009 */
6010#ifdef EBCDIC
6011# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6012#else
6013# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6014#endif
6015
6016#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006017# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006019# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020#endif
6021
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006022/*
6023 * "flags": INSCHAR_FORMAT - force formatting
6024 * INSCHAR_CTRLV - char typed just after CTRL-V
6025 * INSCHAR_NO_FEX - don't use 'formatexpr'
6026 *
6027 * NOTE: passes the flags value straight through to internal_format() which,
6028 * beside INSCHAR_FORMAT (above), is also looking for these:
6029 * INSCHAR_DO_COM - format comments
6030 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006033insertchar(
6034 int c, /* character to insert or NUL */
6035 int flags, /* INSCHAR_FORMAT, etc. */
6036 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 int textwidth;
6039#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006043 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006045 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
6048 /*
6049 * Try to break the line in two or more pieces when:
6050 * - Always do this if we have been called to do formatting only.
6051 * - Always do this when 'formatoptions' has the 'a' flag and the line
6052 * ends in white space.
6053 * - Otherwise:
6054 * - Don't do this if inserting a blank
6055 * - Don't do this if an existing character is being replaced, unless
6056 * we're in VREPLACE mode.
6057 * - Do this if the cursor is not on the line where insert started
6058 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6059 * before the insert.
6060 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6061 * before 'textwidth'
6062 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006063 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006064 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006065 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 && !((State & REPLACE_FLAG)
6067#ifdef FEAT_VREPLACE
6068 && !(State & VREPLACE_FLAG)
6069#endif
6070 && *ml_get_cursor() != NUL)
6071 && (curwin->w_cursor.lnum != Insstart.lnum
6072 || ((!has_format_option(FO_INS_LONG)
6073 || Insstart_textlen <= (colnr_T)textwidth)
6074 && (!fo_ins_blank
6075 || Insstart_blank_vcol <= (colnr_T)textwidth
6076 ))))))
6077 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006078 /* Format with 'formatexpr' when it's set. Use internal formatting
6079 * when 'formatexpr' isn't set or it returns non-zero. */
6080#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006081 int do_internal = TRUE;
6082 colnr_T virtcol = get_nolist_virtcol()
6083 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006084
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006085 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6086 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006087 {
6088 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6089 /* It may be required to save for undo again, e.g. when setline()
6090 * was called. */
6091 ins_need_undo = TRUE;
6092 }
6093 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006095 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006097
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 if (c == NUL) /* only formatting was wanted */
6099 return;
6100
6101#ifdef FEAT_COMMENTS
6102 /* Check whether this character should end a comment. */
6103 if (did_ai && (int)c == end_comment_pending)
6104 {
6105 char_u *line;
6106 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6107 int middle_len, end_len;
6108 int i;
6109
6110 /*
6111 * Need to remove existing (middle) comment leader and insert end
6112 * comment leader. First, check what comment leader we can find.
6113 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006114 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6116 {
6117 /* Skip middle-comment string */
6118 while (*p && p[-1] != ':') /* find end of middle flags */
6119 ++p;
6120 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6121 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006122 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 --middle_len;
6124
6125 /* Find the end-comment string */
6126 while (*p && p[-1] != ':') /* find end of end flags */
6127 ++p;
6128 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6129
6130 /* Skip white space before the cursor */
6131 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006132 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 ;
6134 i++;
6135
6136 /* Skip to before the middle leader */
6137 i -= middle_len;
6138
6139 /* Check some expected things before we go on */
6140 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6141 {
6142 /* Backspace over all the stuff we want to replace */
6143 backspace_until_column(i);
6144
6145 /*
6146 * Insert the end-comment string, except for the last
6147 * character, which will get inserted as normal later.
6148 */
6149 ins_bytes_len(lead_end, end_len - 1);
6150 }
6151 }
6152 }
6153 end_comment_pending = NUL;
6154#endif
6155
6156 did_ai = FALSE;
6157#ifdef FEAT_SMARTINDENT
6158 did_si = FALSE;
6159 can_si = FALSE;
6160 can_si_back = FALSE;
6161#endif
6162
6163 /*
6164 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6165 * This speeds up normal text input considerably.
6166 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6167 * need to re-indent at a ':', or any other character (but not what
6168 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006169 * Don't do this when there an InsertCharPre autocommand is defined,
6170 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 */
6172#ifdef USE_ON_FLY_SCROLL
6173 dont_scroll = FALSE; /* allow scrolling here */
6174#endif
6175
6176 if ( !ISSPECIAL(c)
6177#ifdef FEAT_MBYTE
6178 && (!has_mbyte || (*mb_char2len)(c) == 1)
6179#endif
6180 && vpeekc() != NUL
6181 && !(State & REPLACE_FLAG)
6182#ifdef FEAT_CINDENT
6183 && !cindent_on()
6184#endif
6185#ifdef FEAT_RIGHTLEFT
6186 && !p_ri
6187#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006188#ifdef FEAT_AUTOCMD
6189 && !has_insertcharpre()
6190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 )
6192 {
6193#define INPUT_BUFLEN 100
6194 char_u buf[INPUT_BUFLEN + 1];
6195 int i;
6196 colnr_T virtcol = 0;
6197
6198 buf[0] = c;
6199 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006200 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 virtcol = get_nolist_virtcol();
6202 /*
6203 * Stop the string when:
6204 * - no more chars available
6205 * - finding a special character (command key)
6206 * - buffer is full
6207 * - running into the 'textwidth' boundary
6208 * - need to check for abbreviation: A non-word char after a word-char
6209 */
6210 while ( (c = vpeekc()) != NUL
6211 && !ISSPECIAL(c)
6212#ifdef FEAT_MBYTE
6213 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6214#endif
6215 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006216# ifdef FEAT_FKMAP
6217 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 && (textwidth == 0
6220 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6221 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6222 {
6223#ifdef FEAT_RIGHTLEFT
6224 c = vgetc();
6225 if (p_hkmap && KeyTyped)
6226 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 buf[i++] = c;
6228#else
6229 buf[i++] = vgetc();
6230#endif
6231 }
6232
6233#ifdef FEAT_DIGRAPHS
6234 do_digraph(-1); /* clear digraphs */
6235 do_digraph(buf[i-1]); /* may be the start of a digraph */
6236#endif
6237 buf[i] = NUL;
6238 ins_str(buf);
6239 if (flags & INSCHAR_CTRLV)
6240 {
6241 redo_literal(*buf);
6242 i = 1;
6243 }
6244 else
6245 i = 0;
6246 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006247 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 }
6249 else
6250 {
6251#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006252 int cc;
6253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6255 {
6256 char_u buf[MB_MAXBYTES + 1];
6257
6258 (*mb_char2bytes)(c, buf);
6259 buf[cc] = NUL;
6260 ins_char_bytes(buf, cc);
6261 AppendCharToRedobuff(c);
6262 }
6263 else
6264#endif
6265 {
6266 ins_char(c);
6267 if (flags & INSCHAR_CTRLV)
6268 redo_literal(c);
6269 else
6270 AppendCharToRedobuff(c);
6271 }
6272 }
6273}
6274
6275/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006276 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006277 *
6278 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6279 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006280 */
6281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006282internal_format(
6283 int textwidth,
6284 int second_indent,
6285 int flags,
6286 int format_only,
6287 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006288{
6289 int cc;
6290 int save_char = NUL;
6291 int haveto_redraw = FALSE;
6292 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6293#ifdef FEAT_MBYTE
6294 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6295#endif
6296 int fo_white_par = has_format_option(FO_WHITE_PAR);
6297 int first_line = TRUE;
6298#ifdef FEAT_COMMENTS
6299 colnr_T leader_len;
6300 int no_leader = FALSE;
6301 int do_comments = (flags & INSCHAR_DO_COM);
6302#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006303#ifdef FEAT_LINEBREAK
6304 int has_lbr = curwin->w_p_lbr;
6305
6306 /* make sure win_lbr_chartabsize() counts correctly */
6307 curwin->w_p_lbr = FALSE;
6308#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006309
6310 /*
6311 * When 'ai' is off we don't want a space under the cursor to be
6312 * deleted. Replace it with an 'x' temporarily.
6313 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006314 if (!curbuf->b_p_ai
6315#ifdef FEAT_VREPLACE
6316 && !(State & VREPLACE_FLAG)
6317#endif
6318 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006319 {
6320 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006321 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006322 {
6323 save_char = cc;
6324 pchar_cursor('x');
6325 }
6326 }
6327
6328 /*
6329 * Repeat breaking lines, until the current line is not too long.
6330 */
6331 while (!got_int)
6332 {
6333 int startcol; /* Cursor column at entry */
6334 int wantcol; /* column at textwidth border */
6335 int foundcol; /* column for start of spaces */
6336 int end_foundcol = 0; /* column for start of word */
6337 colnr_T len;
6338 colnr_T virtcol;
6339#ifdef FEAT_VREPLACE
6340 int orig_col = 0;
6341 char_u *saved_text = NULL;
6342#endif
6343 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006344 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006345
Bram Moolenaar97b98102009-11-17 16:41:01 +00006346 virtcol = get_nolist_virtcol()
6347 + char2cells(c != NUL ? c : gchar_cursor());
6348 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006349 break;
6350
6351#ifdef FEAT_COMMENTS
6352 if (no_leader)
6353 do_comments = FALSE;
6354 else if (!(flags & INSCHAR_FORMAT)
6355 && has_format_option(FO_WRAP_COMS))
6356 do_comments = TRUE;
6357
6358 /* Don't break until after the comment leader */
6359 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006360 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006361 else
6362 leader_len = 0;
6363
6364 /* If the line doesn't start with a comment leader, then don't
6365 * start one in a following broken line. Avoids that a %word
6366 * moved to the start of the next line causes all following lines
6367 * to start with %. */
6368 if (leader_len == 0)
6369 no_leader = TRUE;
6370#endif
6371 if (!(flags & INSCHAR_FORMAT)
6372#ifdef FEAT_COMMENTS
6373 && leader_len == 0
6374#endif
6375 && !has_format_option(FO_WRAP))
6376
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006377 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006378 if ((startcol = curwin->w_cursor.col) == 0)
6379 break;
6380
6381 /* find column of textwidth border */
6382 coladvance((colnr_T)textwidth);
6383 wantcol = curwin->w_cursor.col;
6384
Bram Moolenaar97b98102009-11-17 16:41:01 +00006385 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006386 foundcol = 0;
6387
6388 /*
6389 * Find position to break at.
6390 * Stop at first entered white when 'formatoptions' has 'v'
6391 */
6392 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006393 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006394 || curwin->w_cursor.lnum != Insstart.lnum
6395 || curwin->w_cursor.col >= Insstart.col)
6396 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006397 if (curwin->w_cursor.col == startcol && c != NUL)
6398 cc = c;
6399 else
6400 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006401 if (WHITECHAR(cc))
6402 {
6403 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006404 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006405
6406 /* find start of sequence of blanks */
6407 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6408 {
6409 dec_cursor();
6410 cc = gchar_cursor();
6411 }
6412 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6413 break; /* only spaces in front of text */
6414#ifdef FEAT_COMMENTS
6415 /* Don't break until after the comment leader */
6416 if (curwin->w_cursor.col < leader_len)
6417 break;
6418#endif
6419 if (has_format_option(FO_ONE_LETTER))
6420 {
6421 /* do not break after one-letter words */
6422 if (curwin->w_cursor.col == 0)
6423 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006424#ifdef FEAT_COMMENTS
6425 /* do not break "#a b" when 'tw' is 2 */
6426 if (curwin->w_cursor.col <= leader_len)
6427 break;
6428#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006429 col = curwin->w_cursor.col;
6430 dec_cursor();
6431 cc = gchar_cursor();
6432
6433 if (WHITECHAR(cc))
6434 continue; /* one-letter, continue */
6435 curwin->w_cursor.col = col;
6436 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006437
6438 inc_cursor();
6439
6440 end_foundcol = end_col + 1;
6441 foundcol = curwin->w_cursor.col;
6442 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006443 break;
6444 }
6445#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006446 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006447 {
6448 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006449 if (curwin->w_cursor.col != startcol)
6450 {
6451#ifdef FEAT_COMMENTS
6452 /* Don't break until after the comment leader */
6453 if (curwin->w_cursor.col < leader_len)
6454 break;
6455#endif
6456 col = curwin->w_cursor.col;
6457 inc_cursor();
6458 /* Don't change end_foundcol if already set. */
6459 if (foundcol != curwin->w_cursor.col)
6460 {
6461 foundcol = curwin->w_cursor.col;
6462 end_foundcol = foundcol;
6463 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6464 break;
6465 }
6466 curwin->w_cursor.col = col;
6467 }
6468
6469 if (curwin->w_cursor.col == 0)
6470 break;
6471
6472 col = curwin->w_cursor.col;
6473
6474 dec_cursor();
6475 cc = gchar_cursor();
6476
6477 if (WHITECHAR(cc))
6478 continue; /* break with space */
6479#ifdef FEAT_COMMENTS
6480 /* Don't break until after the comment leader */
6481 if (curwin->w_cursor.col < leader_len)
6482 break;
6483#endif
6484
6485 curwin->w_cursor.col = col;
6486
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006487 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006488 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006489 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6490 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006491 }
6492#endif
6493 if (curwin->w_cursor.col == 0)
6494 break;
6495 dec_cursor();
6496 }
6497
6498 if (foundcol == 0) /* no spaces, cannot break line */
6499 {
6500 curwin->w_cursor.col = startcol;
6501 break;
6502 }
6503
6504 /* Going to break the line, remove any "$" now. */
6505 undisplay_dollar();
6506
6507 /*
6508 * Offset between cursor position and line break is used by replace
6509 * stack functions. VREPLACE does not use this, and backspaces
6510 * over the text instead.
6511 */
6512#ifdef FEAT_VREPLACE
6513 if (State & VREPLACE_FLAG)
6514 orig_col = startcol; /* Will start backspacing from here */
6515 else
6516#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006517 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006518
6519 /*
6520 * adjust startcol for spaces that will be deleted and
6521 * characters that will remain on top line
6522 */
6523 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006524 while ((cc = gchar_cursor(), WHITECHAR(cc))
6525 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006526 inc_cursor();
6527 startcol -= curwin->w_cursor.col;
6528 if (startcol < 0)
6529 startcol = 0;
6530
6531#ifdef FEAT_VREPLACE
6532 if (State & VREPLACE_FLAG)
6533 {
6534 /*
6535 * In VREPLACE mode, we will backspace over the text to be
6536 * wrapped, so save a copy now to put on the next line.
6537 */
6538 saved_text = vim_strsave(ml_get_cursor());
6539 curwin->w_cursor.col = orig_col;
6540 if (saved_text == NULL)
6541 break; /* Can't do it, out of memory */
6542 saved_text[startcol] = NUL;
6543
6544 /* Backspace over characters that will move to the next line */
6545 if (!fo_white_par)
6546 backspace_until_column(foundcol);
6547 }
6548 else
6549#endif
6550 {
6551 /* put cursor after pos. to break line */
6552 if (!fo_white_par)
6553 curwin->w_cursor.col = foundcol;
6554 }
6555
6556 /*
6557 * Split the line just before the margin.
6558 * Only insert/delete lines, but don't really redraw the window.
6559 */
6560 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6561 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6562#ifdef FEAT_COMMENTS
6563 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006564 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006565#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006566 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6567 if (!(flags & INSCHAR_COM_LIST))
6568 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006569
6570 replace_offset = 0;
6571 if (first_line)
6572 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006573 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006574 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006575 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006576 * This section is for auto-wrap of numeric lists. When not
6577 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6578 * flag will be set and open_line() will handle it (as seen
6579 * above). The code here (and in get_number_indent()) will
6580 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006581 */
6582 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006583 second_indent =
6584 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006585 if (second_indent >= 0)
6586 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006587#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006588 if (State & VREPLACE_FLAG)
6589 change_indent(INDENT_SET, second_indent,
6590 FALSE, NUL, TRUE);
6591 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006592#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006593#ifdef FEAT_COMMENTS
6594 if (leader_len > 0 && second_indent - leader_len > 0)
6595 {
6596 int i;
6597 int padding = second_indent - leader_len;
6598
6599 /* We started at the first_line of a numbered list
6600 * that has a comment. the open_line() function has
6601 * inserted the proper comment leader and positioned
6602 * the cursor at the end of the split line. Now we
6603 * add the additional whitespace needed after the
6604 * comment leader for the numbered list. */
6605 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006606 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006607 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006608 }
6609 else
6610 {
6611#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006612 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006613#ifdef FEAT_COMMENTS
6614 }
6615#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006616 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006617 }
6618 first_line = FALSE;
6619 }
6620
6621#ifdef FEAT_VREPLACE
6622 if (State & VREPLACE_FLAG)
6623 {
6624 /*
6625 * In VREPLACE mode we have backspaced over the text to be
6626 * moved, now we re-insert it into the new line.
6627 */
6628 ins_bytes(saved_text);
6629 vim_free(saved_text);
6630 }
6631 else
6632#endif
6633 {
6634 /*
6635 * Check if cursor is not past the NUL off the line, cindent
6636 * may have added or removed indent.
6637 */
6638 curwin->w_cursor.col += startcol;
6639 len = (colnr_T)STRLEN(ml_get_curline());
6640 if (curwin->w_cursor.col > len)
6641 curwin->w_cursor.col = len;
6642 }
6643
6644 haveto_redraw = TRUE;
6645#ifdef FEAT_CINDENT
6646 can_cindent = TRUE;
6647#endif
6648 /* moved the cursor, don't autoindent or cindent now */
6649 did_ai = FALSE;
6650#ifdef FEAT_SMARTINDENT
6651 did_si = FALSE;
6652 can_si = FALSE;
6653 can_si_back = FALSE;
6654#endif
6655 line_breakcheck();
6656 }
6657
6658 if (save_char != NUL) /* put back space after cursor */
6659 pchar_cursor(save_char);
6660
Bram Moolenaar0026d472014-09-09 16:32:39 +02006661#ifdef FEAT_LINEBREAK
6662 curwin->w_p_lbr = has_lbr;
6663#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006664 if (!format_only && haveto_redraw)
6665 {
6666 update_topline();
6667 redraw_curbuf_later(VALID);
6668 }
6669}
6670
6671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 * Called after inserting or deleting text: When 'formatoptions' includes the
6673 * 'a' flag format from the current line until the end of the paragraph.
6674 * Keep the cursor at the same position relative to the text.
6675 * The caller must have saved the cursor line for undo, following ones will be
6676 * saved here.
6677 */
6678 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006679auto_format(
6680 int trailblank, /* when TRUE also format with trailing blank */
6681 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
6683 pos_T pos;
6684 colnr_T len;
6685 char_u *old;
6686 char_u *new, *pnew;
6687 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006688 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689
6690 if (!has_format_option(FO_AUTO))
6691 return;
6692
6693 pos = curwin->w_cursor;
6694 old = ml_get_curline();
6695
6696 /* may remove added space */
6697 check_auto_format(FALSE);
6698
6699 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6700 * user might insert normal text next. Also skip formatting when "1" is
6701 * in 'formatoptions' and there is a single character before the cursor.
6702 * Otherwise the line would be broken and when typing another non-white
6703 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006704 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 if (*old != NUL && !trailblank && wasatend)
6706 {
6707 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006708 cc = gchar_cursor();
6709 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6710 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006712 cc = gchar_cursor();
6713 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {
6715 curwin->w_cursor = pos;
6716 return;
6717 }
6718 curwin->w_cursor = pos;
6719 }
6720
6721#ifdef FEAT_COMMENTS
6722 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6723 * comments. */
6724 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006725 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 return;
6727#endif
6728
6729 /*
6730 * May start formatting in a previous line, so that after "x" a word is
6731 * moved to the previous line if it fits there now. Only when this is not
6732 * the start of a paragraph.
6733 */
6734 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6735 {
6736 --curwin->w_cursor.lnum;
6737 if (u_save_cursor() == FAIL)
6738 return;
6739 }
6740
6741 /*
6742 * Do the formatting and restore the cursor position. "saved_cursor" will
6743 * be adjusted for the text formatting.
6744 */
6745 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006746 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 curwin->w_cursor = saved_cursor;
6748 saved_cursor.lnum = 0;
6749
6750 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6751 {
6752 /* "cannot happen" */
6753 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6754 coladvance((colnr_T)MAXCOL);
6755 }
6756 else
6757 check_cursor_col();
6758
6759 /* Insert mode: If the cursor is now after the end of the line while it
6760 * previously wasn't, the line was broken. Because of the rule above we
6761 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6762 * formatted. */
6763 if (!wasatend && has_format_option(FO_WHITE_PAR))
6764 {
6765 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006766 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 if (curwin->w_cursor.col == len)
6768 {
6769 pnew = vim_strnsave(new, len + 2);
6770 pnew[len] = ' ';
6771 pnew[len + 1] = NUL;
6772 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6773 /* remove the space later */
6774 did_add_space = TRUE;
6775 }
6776 else
6777 /* may remove added space */
6778 check_auto_format(FALSE);
6779 }
6780
6781 check_cursor();
6782}
6783
6784/*
6785 * When an extra space was added to continue a paragraph for auto-formatting,
6786 * delete it now. The space must be under the cursor, just after the insert
6787 * position.
6788 */
6789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006790check_auto_format(
6791 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792{
6793 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006794 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
6796 if (did_add_space)
6797 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006798 cc = gchar_cursor();
6799 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 /* Somehow the space was removed already. */
6801 did_add_space = FALSE;
6802 else
6803 {
6804 if (!end_insert)
6805 {
6806 inc_cursor();
6807 c = gchar_cursor();
6808 dec_cursor();
6809 }
6810 if (c != NUL)
6811 {
6812 /* The space is no longer at the end of the line, delete it. */
6813 del_char(FALSE);
6814 did_add_space = FALSE;
6815 }
6816 }
6817 }
6818}
6819
6820/*
6821 * Find out textwidth to be used for formatting:
6822 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006823 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 * if invalid value, use 0.
6825 * Set default to window width (maximum 79) for "gq" operator.
6826 */
6827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006828comp_textwidth(
6829 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830{
6831 int textwidth;
6832
6833 textwidth = curbuf->b_p_tw;
6834 if (textwidth == 0 && curbuf->b_p_wm)
6835 {
6836 /* The width is the window width minus 'wrapmargin' minus all the
6837 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006838 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839#ifdef FEAT_CMDWIN
6840 if (cmdwin_type != 0)
6841 textwidth -= 1;
6842#endif
6843#ifdef FEAT_FOLDING
6844 textwidth -= curwin->w_p_fdc;
6845#endif
6846#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006847 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 textwidth -= 1;
6849#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006850 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 textwidth -= 8;
6852 }
6853 if (textwidth < 0)
6854 textwidth = 0;
6855 if (ff && textwidth == 0)
6856 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006857 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 if (textwidth > 79)
6859 textwidth = 79;
6860 }
6861 return textwidth;
6862}
6863
6864/*
6865 * Put a character in the redo buffer, for when just after a CTRL-V.
6866 */
6867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006868redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 char_u buf[10];
6871
6872 /* Only digits need special treatment. Translate them into a string of
6873 * three digits. */
6874 if (VIM_ISDIGIT(c))
6875 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006876 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 AppendToRedobuff(buf);
6878 }
6879 else
6880 AppendCharToRedobuff(c);
6881}
6882
6883/*
6884 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006885 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 */
6887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006888start_arrow(
6889 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006891 start_arrow_common(end_insert_pos, TRUE);
6892}
6893
6894/*
6895 * Like start_arrow() but with end_change argument.
6896 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6897 */
6898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006899start_arrow_with_change(
6900 pos_T *end_insert_pos, /* can be NULL */
6901 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006902{
6903 start_arrow_common(end_insert_pos, end_change);
6904 if (!end_change)
6905 {
6906 AppendCharToRedobuff(Ctrl_G);
6907 AppendCharToRedobuff('U');
6908 }
6909}
6910
6911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006912start_arrow_common(
6913 pos_T *end_insert_pos, /* can be NULL */
6914 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006915{
6916 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {
6918 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006919 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 arrow_used = TRUE; /* this means we stopped the current insert */
6921 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006922#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006923 check_spell_redraw();
6924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925}
6926
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006927#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006928/*
6929 * If we skipped highlighting word at cursor, do it now.
6930 * It may be skipped again, thus reset spell_redraw_lnum first.
6931 */
6932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006933check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006934{
6935 if (spell_redraw_lnum != 0)
6936 {
6937 linenr_T lnum = spell_redraw_lnum;
6938
6939 spell_redraw_lnum = 0;
6940 redrawWinline(lnum, FALSE);
6941 }
6942}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006943
6944/*
6945 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6946 * spelled word, if there is one.
6947 */
6948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006949spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006950{
6951 pos_T tpos = curwin->w_cursor;
6952
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006953 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006954 if (curwin->w_cursor.col != tpos.col)
6955 start_arrow(&tpos);
6956}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006957#endif
6958
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959/*
6960 * stop_arrow() is called before a change is made in insert mode.
6961 * If an arrow key has been used, start a new insertion.
6962 * Returns FAIL if undo is impossible, shouldn't insert then.
6963 */
6964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006965stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966{
6967 if (arrow_used)
6968 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006969 Insstart = curwin->w_cursor; /* new insertion starts here */
6970 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6971 /* Don't update the original insert position when moved to the
6972 * right, except when nothing was inserted yet. */
6973 update_Insstart_orig = FALSE;
6974 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6975
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 if (u_save_cursor() == OK)
6977 {
6978 arrow_used = FALSE;
6979 ins_need_undo = FALSE;
6980 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006981
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 ai_col = 0;
6983#ifdef FEAT_VREPLACE
6984 if (State & VREPLACE_FLAG)
6985 {
6986 orig_line_count = curbuf->b_ml.ml_line_count;
6987 vr_lines_changed = 1;
6988 }
6989#endif
6990 ResetRedobuff();
6991 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006992 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 }
6994 else if (ins_need_undo)
6995 {
6996 if (u_save_cursor() == OK)
6997 ins_need_undo = FALSE;
6998 }
6999
7000#ifdef FEAT_FOLDING
7001 /* Always open fold at the cursor line when inserting something. */
7002 foldOpenCursor();
7003#endif
7004
7005 return (arrow_used || ins_need_undo ? FAIL : OK);
7006}
7007
7008/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007009 * Do a few things to stop inserting.
7010 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7011 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 */
7013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007014stop_insert(
7015 pos_T *end_insert_pos,
7016 int esc, /* called by ins_esc() */
7017 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007019 int cc;
7020 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021
7022 stop_redo_ins();
7023 replace_flush(); /* abandon replace stack */
7024
7025 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007026 * Save the inserted text for later redo with ^@ and CTRL-A.
7027 * Don't do it when "restart_edit" was set and nothing was inserted,
7028 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007030 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007031 if (did_restart_edit == 0 || (ptr != NULL
7032 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007033 {
7034 vim_free(last_insert);
7035 last_insert = ptr;
7036 last_insert_skip = new_insert_skip;
7037 }
7038 else
7039 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007041 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {
7043 /* Auto-format now. It may seem strange to do this when stopping an
7044 * insertion (or moving the cursor), but it's required when appending
7045 * a line and having it end in a space. But only do it when something
7046 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007047 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007049 pos_T tpos = curwin->w_cursor;
7050
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 /* When the cursor is at the end of the line after a space the
7052 * formatting will move it to the following word. Avoid that by
7053 * moving the cursor onto the space. */
7054 cc = 'x';
7055 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7056 {
7057 dec_cursor();
7058 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007059 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007060 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 }
7062
7063 auto_format(TRUE, FALSE);
7064
Bram Moolenaar1c465442017-03-12 20:10:05 +01007065 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007066 {
7067 if (gchar_cursor() != NUL)
7068 inc_cursor();
7069#ifdef FEAT_VIRTUALEDIT
7070 /* If the cursor is still at the same character, also keep
7071 * the "coladd". */
7072 if (gchar_cursor() == NUL
7073 && curwin->w_cursor.lnum == tpos.lnum
7074 && curwin->w_cursor.col == tpos.col)
7075 curwin->w_cursor.coladd = tpos.coladd;
7076#endif
7077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
7079
7080 /* If a space was inserted for auto-formatting, remove it now. */
7081 check_auto_format(TRUE);
7082
7083 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007084 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007085 * Do this when ESC was used or moving the cursor up/down.
7086 * Check for the old position still being valid, just in case the text
7087 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007088 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007089 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7090 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007092 pos_T tpos = curwin->w_cursor;
7093
7094 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007095 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007096 for (;;)
7097 {
7098 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7099 --curwin->w_cursor.col;
7100 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007101 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007102 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007103 if (del_char(TRUE) == FAIL)
7104 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007105 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007106 if (curwin->w_cursor.lnum != tpos.lnum)
7107 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007108 else
7109 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007110 /* reset tpos, could have been invalidated in the loop above */
7111 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007112 tpos.col++;
7113 if (cc != NUL && gchar_pos(&tpos) == NUL)
7114 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 /* <C-S-Right> may have started Visual mode, adjust the position for
7118 * deleted characters. */
7119 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7120 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007121 int len = (int)STRLEN(ml_get_curline());
7122
7123 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007125 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007126#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 }
7130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 }
7132 }
7133 did_ai = FALSE;
7134#ifdef FEAT_SMARTINDENT
7135 did_si = FALSE;
7136 can_si = FALSE;
7137 can_si_back = FALSE;
7138#endif
7139
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007140 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7141 * now in a different buffer. */
7142 if (end_insert_pos != NULL)
7143 {
7144 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007145 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007146 curbuf->b_op_end = *end_insert_pos;
7147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148}
7149
7150/*
7151 * Set the last inserted text to a single character.
7152 * Used for the replace command.
7153 */
7154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007155set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
7157 char_u *s;
7158
7159 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 if (last_insert != NULL)
7162 {
7163 s = last_insert;
7164 /* Use the CTRL-V only when entering a special char */
7165 if (c < ' ' || c == DEL)
7166 *s++ = Ctrl_V;
7167 s = add_char2buf(c, s);
7168 *s++ = ESC;
7169 *s++ = NUL;
7170 last_insert_skip = 0;
7171 }
7172}
7173
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007174#if defined(EXITFREE) || defined(PROTO)
7175 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007176free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007177{
7178 vim_free(last_insert);
7179 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007180# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007181 vim_free(compl_orig_text);
7182 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007183# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007184}
7185#endif
7186
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187/*
7188 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7189 * and CSI. Handle multi-byte characters.
7190 * Returns a pointer to after the added bytes.
7191 */
7192 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007193add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007196 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 int i;
7198 int len;
7199
7200 len = (*mb_char2bytes)(c, temp);
7201 for (i = 0; i < len; ++i)
7202 {
7203 c = temp[i];
7204#endif
7205 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7206 if (c == K_SPECIAL)
7207 {
7208 *s++ = K_SPECIAL;
7209 *s++ = KS_SPECIAL;
7210 *s++ = KE_FILLER;
7211 }
7212#ifdef FEAT_GUI
7213 else if (c == CSI)
7214 {
7215 *s++ = CSI;
7216 *s++ = KS_EXTRA;
7217 *s++ = (int)KE_CSI;
7218 }
7219#endif
7220 else
7221 *s++ = c;
7222#ifdef FEAT_MBYTE
7223 }
7224#endif
7225 return s;
7226}
7227
7228/*
7229 * move cursor to start of line
7230 * if flags & BL_WHITE move to first non-white
7231 * if flags & BL_SOL move to first non-white if startofline is set,
7232 * otherwise keep "curswant" column
7233 * if flags & BL_FIX don't leave the cursor on a NUL.
7234 */
7235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007236beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
7238 if ((flags & BL_SOL) && !p_sol)
7239 coladvance(curwin->w_curswant);
7240 else
7241 {
7242 curwin->w_cursor.col = 0;
7243#ifdef FEAT_VIRTUALEDIT
7244 curwin->w_cursor.coladd = 0;
7245#endif
7246
7247 if (flags & (BL_WHITE | BL_SOL))
7248 {
7249 char_u *ptr;
7250
Bram Moolenaar1c465442017-03-12 20:10:05 +01007251 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7253 ++curwin->w_cursor.col;
7254 }
7255 curwin->w_set_curswant = TRUE;
7256 }
7257}
7258
7259/*
7260 * oneright oneleft cursor_down cursor_up
7261 *
7262 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007263 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 * Return OK when successful, FAIL when we hit a line of file boundary.
7265 */
7266
7267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007268oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
7270 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
7273#ifdef FEAT_VIRTUALEDIT
7274 if (virtual_active())
7275 {
7276 pos_T prevpos = curwin->w_cursor;
7277
7278 /* Adjust for multi-wide char (excluding TAB) */
7279 ptr = ml_get_cursor();
7280 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007281# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007283# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 ))
7287 ? ptr2cells(ptr) : 1));
7288 curwin->w_set_curswant = TRUE;
7289 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7290 return (prevpos.col != curwin->w_cursor.col
7291 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7292 }
7293#endif
7294
7295 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007296 if (*ptr == NUL)
7297 return FAIL; /* already at the very end */
7298
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007300 if (has_mbyte)
7301 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 else
7303#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007304 l = 1;
7305
7306 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7307 * contains "onemore". */
7308 if (ptr[l] == NUL
7309#ifdef FEAT_VIRTUALEDIT
7310 && (ve_flags & VE_ONEMORE) == 0
7311#endif
7312 )
7313 return FAIL;
7314 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315
7316 curwin->w_set_curswant = TRUE;
7317 return OK;
7318}
7319
7320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007321oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322{
7323#ifdef FEAT_VIRTUALEDIT
7324 if (virtual_active())
7325 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007326# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007328# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 int v = getviscol();
7330
7331 if (v == 0)
7332 return FAIL;
7333
7334# ifdef FEAT_LINEBREAK
7335 /* We might get stuck on 'showbreak', skip over it. */
7336 width = 1;
7337 for (;;)
7338 {
7339 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007340 /* getviscol() is slow, skip it when 'showbreak' is empty,
7341 * 'breakindent' is not set and there are no multi-byte
7342 * characters */
7343 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344# ifdef FEAT_MBYTE
7345 && !has_mbyte
7346# endif
7347 ) || getviscol() < v)
7348 break;
7349 ++width;
7350 }
7351# else
7352 coladvance(v - 1);
7353# endif
7354
7355 if (curwin->w_cursor.coladd == 1)
7356 {
7357 char_u *ptr;
7358
7359 /* Adjust for multi-wide char (not a TAB) */
7360 ptr = ml_get_cursor();
7361 if (*ptr != TAB && vim_isprintc(
7362# ifdef FEAT_MBYTE
7363 (*mb_ptr2char)(ptr)
7364# else
7365 *ptr
7366# endif
7367 ) && ptr2cells(ptr) > 1)
7368 curwin->w_cursor.coladd = 0;
7369 }
7370
7371 curwin->w_set_curswant = TRUE;
7372 return OK;
7373 }
7374#endif
7375
7376 if (curwin->w_cursor.col == 0)
7377 return FAIL;
7378
7379 curwin->w_set_curswant = TRUE;
7380 --curwin->w_cursor.col;
7381
7382#ifdef FEAT_MBYTE
7383 /* if the character on the left of the current cursor is a multi-byte
7384 * character, move to its first byte */
7385 if (has_mbyte)
7386 mb_adjust_cursor();
7387#endif
7388 return OK;
7389}
7390
7391 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007392cursor_up(
7393 long n,
7394 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 linenr_T lnum;
7397
7398 if (n > 0)
7399 {
7400 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007401 /* This fails if the cursor is already in the first line or the count
7402 * is larger than the line number and '-' is in 'cpoptions' */
7403 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 return FAIL;
7405 if (n >= lnum)
7406 lnum = 1;
7407 else
7408#ifdef FEAT_FOLDING
7409 if (hasAnyFolding(curwin))
7410 {
7411 /*
7412 * Count each sequence of folded lines as one logical line.
7413 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007414 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 (void)hasFolding(lnum, &lnum, NULL);
7416
7417 while (n--)
7418 {
7419 /* move up one line */
7420 --lnum;
7421 if (lnum <= 1)
7422 break;
7423 /* If we entered a fold, move to the beginning, unless in
7424 * Insert mode or when 'foldopen' contains "all": it will open
7425 * in a moment. */
7426 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7427 (void)hasFolding(lnum, &lnum, NULL);
7428 }
7429 if (lnum < 1)
7430 lnum = 1;
7431 }
7432 else
7433#endif
7434 lnum -= n;
7435 curwin->w_cursor.lnum = lnum;
7436 }
7437
7438 /* try to advance to the column we want to be at */
7439 coladvance(curwin->w_curswant);
7440
7441 if (upd_topline)
7442 update_topline(); /* make sure curwin->w_topline is valid */
7443
7444 return OK;
7445}
7446
7447/*
7448 * Cursor down a number of logical lines.
7449 */
7450 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007451cursor_down(
7452 long n,
7453 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
7455 linenr_T lnum;
7456
7457 if (n > 0)
7458 {
7459 lnum = curwin->w_cursor.lnum;
7460#ifdef FEAT_FOLDING
7461 /* Move to last line of fold, will fail if it's the end-of-file. */
7462 (void)hasFolding(lnum, NULL, &lnum);
7463#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007464 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007465 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007466 if (lnum >= curbuf->b_ml.ml_line_count
7467 || (lnum + n > curbuf->b_ml.ml_line_count
7468 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 return FAIL;
7470 if (lnum + n >= curbuf->b_ml.ml_line_count)
7471 lnum = curbuf->b_ml.ml_line_count;
7472 else
7473#ifdef FEAT_FOLDING
7474 if (hasAnyFolding(curwin))
7475 {
7476 linenr_T last;
7477
7478 /* count each sequence of folded lines as one logical line */
7479 while (n--)
7480 {
7481 if (hasFolding(lnum, NULL, &last))
7482 lnum = last + 1;
7483 else
7484 ++lnum;
7485 if (lnum >= curbuf->b_ml.ml_line_count)
7486 break;
7487 }
7488 if (lnum > curbuf->b_ml.ml_line_count)
7489 lnum = curbuf->b_ml.ml_line_count;
7490 }
7491 else
7492#endif
7493 lnum += n;
7494 curwin->w_cursor.lnum = lnum;
7495 }
7496
7497 /* try to advance to the column we want to be at */
7498 coladvance(curwin->w_curswant);
7499
7500 if (upd_topline)
7501 update_topline(); /* make sure curwin->w_topline is valid */
7502
7503 return OK;
7504}
7505
7506/*
7507 * Stuff the last inserted text in the read buffer.
7508 * Last_insert actually is a copy of the redo buffer, so we
7509 * first have to remove the command.
7510 */
7511 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007512stuff_inserted(
7513 int c, /* Command character to be inserted */
7514 long count, /* Repeat this many times */
7515 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516{
7517 char_u *esc_ptr;
7518 char_u *ptr;
7519 char_u *last_ptr;
7520 char_u last = NUL;
7521
7522 ptr = get_last_insert();
7523 if (ptr == NULL)
7524 {
7525 EMSG(_(e_noinstext));
7526 return FAIL;
7527 }
7528
7529 /* may want to stuff the command character, to start Insert mode */
7530 if (c != NUL)
7531 stuffcharReadbuff(c);
7532 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7533 *esc_ptr = NUL; /* remove the ESC */
7534
7535 /* when the last char is either "0" or "^" it will be quoted if no ESC
7536 * comes after it OR if it will inserted more than once and "ptr"
7537 * starts with ^D. -- Acevedo
7538 */
7539 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7540 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7541 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7542 {
7543 last = *last_ptr;
7544 *last_ptr = NUL;
7545 }
7546
7547 do
7548 {
7549 stuffReadbuff(ptr);
7550 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7551 if (last)
7552 stuffReadbuff((char_u *)(last == '0'
7553 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7554 : IF_EB("\026^", CTRL_V_STR "^")));
7555 }
7556 while (--count > 0);
7557
7558 if (last)
7559 *last_ptr = last;
7560
7561 if (esc_ptr != NULL)
7562 *esc_ptr = ESC; /* put the ESC back */
7563
7564 /* may want to stuff a trailing ESC, to get out of Insert mode */
7565 if (!no_esc)
7566 stuffcharReadbuff(ESC);
7567
7568 return OK;
7569}
7570
7571 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007572get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573{
7574 if (last_insert == NULL)
7575 return NULL;
7576 return last_insert + last_insert_skip;
7577}
7578
7579/*
7580 * Get last inserted string, and remove trailing <Esc>.
7581 * Returns pointer to allocated memory (must be freed) or NULL.
7582 */
7583 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007584get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585{
7586 char_u *s;
7587 int len;
7588
7589 if (last_insert == NULL)
7590 return NULL;
7591 s = vim_strsave(last_insert + last_insert_skip);
7592 if (s != NULL)
7593 {
7594 len = (int)STRLEN(s);
7595 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7596 s[len - 1] = NUL;
7597 }
7598 return s;
7599}
7600
7601/*
7602 * Check the word in front of the cursor for an abbreviation.
7603 * Called when the non-id character "c" has been entered.
7604 * When an abbreviation is recognized it is removed from the text and
7605 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7606 */
7607 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007608echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610 /* Don't check for abbreviation in paste mode, when disabled and just
7611 * after moving around with cursor keys. */
7612 if (p_paste || no_abbr || arrow_used)
7613 return FALSE;
7614
7615 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7616 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7617}
7618
7619/*
7620 * replace-stack functions
7621 *
7622 * When replacing characters, the replaced characters are remembered for each
7623 * new character. This is used to re-insert the old text when backspacing.
7624 *
7625 * There is a NUL headed list of characters for each character that is
7626 * currently in the file after the insertion point. When BS is used, one NUL
7627 * headed list is put back for the deleted character.
7628 *
7629 * For a newline, there are two NUL headed lists. One contains the characters
7630 * that the NL replaced. The extra one stores the characters after the cursor
7631 * that were deleted (always white space).
7632 *
7633 * Replace_offset is normally 0, in which case replace_push will add a new
7634 * character at the end of the stack. If replace_offset is not 0, that many
7635 * characters will be left on the stack above the newly inserted character.
7636 */
7637
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007638static char_u *replace_stack = NULL;
7639static long replace_stack_nr = 0; /* next entry in replace stack */
7640static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641
7642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007643replace_push(
7644 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
7646 char_u *p;
7647
7648 if (replace_stack_nr < replace_offset) /* nothing to do */
7649 return;
7650 if (replace_stack_len <= replace_stack_nr)
7651 {
7652 replace_stack_len += 50;
7653 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7654 if (p == NULL) /* out of memory */
7655 {
7656 replace_stack_len -= 50;
7657 return;
7658 }
7659 if (replace_stack != NULL)
7660 {
7661 mch_memmove(p, replace_stack,
7662 (size_t)(replace_stack_nr * sizeof(char_u)));
7663 vim_free(replace_stack);
7664 }
7665 replace_stack = p;
7666 }
7667 p = replace_stack + replace_stack_nr - replace_offset;
7668 if (replace_offset)
7669 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7670 *p = c;
7671 ++replace_stack_nr;
7672}
7673
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007674#if defined(FEAT_MBYTE) || defined(PROTO)
7675/*
7676 * Push a character onto the replace stack. Handles a multi-byte character in
7677 * reverse byte order, so that the first byte is popped off first.
7678 * Return the number of bytes done (includes composing characters).
7679 */
7680 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007681replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007682{
7683 int l = (*mb_ptr2len)(p);
7684 int j;
7685
7686 for (j = l - 1; j >= 0; --j)
7687 replace_push(p[j]);
7688 return l;
7689}
7690#endif
7691
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692/*
7693 * Pop one item from the replace stack.
7694 * return -1 if stack empty
7695 * return replaced character or NUL otherwise
7696 */
7697 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007698replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700 if (replace_stack_nr == 0)
7701 return -1;
7702 return (int)replace_stack[--replace_stack_nr];
7703}
7704
7705/*
7706 * Join the top two items on the replace stack. This removes to "off"'th NUL
7707 * encountered.
7708 */
7709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007710replace_join(
7711 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 int i;
7714
7715 for (i = replace_stack_nr; --i >= 0; )
7716 if (replace_stack[i] == NUL && off-- <= 0)
7717 {
7718 --replace_stack_nr;
7719 mch_memmove(replace_stack + i, replace_stack + i + 1,
7720 (size_t)(replace_stack_nr - i));
7721 return;
7722 }
7723}
7724
7725/*
7726 * Pop bytes from the replace stack until a NUL is found, and insert them
7727 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7728 */
7729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007730replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731{
7732 int cc;
7733 int oldState = State;
7734
7735 State = NORMAL; /* don't want REPLACE here */
7736 while ((cc = replace_pop()) > 0)
7737 {
7738#ifdef FEAT_MBYTE
7739 mb_replace_pop_ins(cc);
7740#else
7741 ins_char(cc);
7742#endif
7743 dec_cursor();
7744 }
7745 State = oldState;
7746}
7747
7748#ifdef FEAT_MBYTE
7749/*
7750 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7751 * indicates a multi-byte char, pop the other bytes too.
7752 */
7753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007754mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755{
7756 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007757 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 int i;
7759 int c;
7760
7761 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7762 {
7763 buf[0] = cc;
7764 for (i = 1; i < n; ++i)
7765 buf[i] = replace_pop();
7766 ins_bytes_len(buf, n);
7767 }
7768 else
7769 ins_char(cc);
7770
7771 if (enc_utf8)
7772 /* Handle composing chars. */
7773 for (;;)
7774 {
7775 c = replace_pop();
7776 if (c == -1) /* stack empty */
7777 break;
7778 if ((n = MB_BYTE2LEN(c)) == 1)
7779 {
7780 /* Not a multi-byte char, put it back. */
7781 replace_push(c);
7782 break;
7783 }
7784 else
7785 {
7786 buf[0] = c;
7787 for (i = 1; i < n; ++i)
7788 buf[i] = replace_pop();
7789 if (utf_iscomposing(utf_ptr2char(buf)))
7790 ins_bytes_len(buf, n);
7791 else
7792 {
7793 /* Not a composing char, put it back. */
7794 for (i = n - 1; i >= 0; --i)
7795 replace_push(buf[i]);
7796 break;
7797 }
7798 }
7799 }
7800}
7801#endif
7802
7803/*
7804 * make the replace stack empty
7805 * (called when exiting replace mode)
7806 */
7807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007808replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809{
7810 vim_free(replace_stack);
7811 replace_stack = NULL;
7812 replace_stack_len = 0;
7813 replace_stack_nr = 0;
7814}
7815
7816/*
7817 * Handle doing a BS for one character.
7818 * cc < 0: replace stack empty, just move cursor
7819 * cc == 0: character was inserted, delete it
7820 * cc > 0: character was replaced, put cc (first byte of original char) back
7821 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007822 * When "limit_col" is >= 0, don't delete before this column. Matters when
7823 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 */
7825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007826replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
7828 int cc;
7829#ifdef FEAT_VREPLACE
7830 int orig_len = 0;
7831 int ins_len;
7832 int orig_vcols = 0;
7833 colnr_T start_vcol;
7834 char_u *p;
7835 int i;
7836 int vcol;
7837#endif
7838
7839 cc = replace_pop();
7840 if (cc > 0)
7841 {
7842#ifdef FEAT_VREPLACE
7843 if (State & VREPLACE_FLAG)
7844 {
7845 /* Get the number of screen cells used by the character we are
7846 * going to delete. */
7847 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7848 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7849 }
7850#endif
7851#ifdef FEAT_MBYTE
7852 if (has_mbyte)
7853 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007854 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855# ifdef FEAT_VREPLACE
7856 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007857 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858# endif
7859 replace_push(cc);
7860 }
7861 else
7862#endif
7863 {
7864 pchar_cursor(cc);
7865#ifdef FEAT_VREPLACE
7866 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007867 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868#endif
7869 }
7870 replace_pop_ins();
7871
7872#ifdef FEAT_VREPLACE
7873 if (State & VREPLACE_FLAG)
7874 {
7875 /* Get the number of screen cells used by the inserted characters */
7876 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007877 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 vcol = start_vcol;
7879 for (i = 0; i < ins_len; ++i)
7880 {
7881 vcol += chartabsize(p + i, vcol);
7882#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007883 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884#endif
7885 }
7886 vcol -= start_vcol;
7887
7888 /* Delete spaces that were inserted after the cursor to keep the
7889 * text aligned. */
7890 curwin->w_cursor.col += ins_len;
7891 while (vcol > orig_vcols && gchar_cursor() == ' ')
7892 {
7893 del_char(FALSE);
7894 ++orig_vcols;
7895 }
7896 curwin->w_cursor.col -= ins_len;
7897 }
7898#endif
7899
7900 /* mark the buffer as changed and prepare for displaying */
7901 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7902 }
7903 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007904 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905}
7906
7907#ifdef FEAT_CINDENT
7908/*
7909 * Return TRUE if C-indenting is on.
7910 */
7911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007912cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913{
7914 return (!p_paste && (curbuf->b_p_cin
7915# ifdef FEAT_EVAL
7916 || *curbuf->b_p_inde != NUL
7917# endif
7918 ));
7919}
7920#endif
7921
7922#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7923/*
7924 * Re-indent the current line, based on the current contents of it and the
7925 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7926 * confused what all the part that handles Control-T is doing that I'm not.
7927 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7928 */
7929
7930 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007931fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007933 int amount = get_the_indent();
7934
7935 if (amount >= 0)
7936 {
7937 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7938 if (linewhite(curwin->w_cursor.lnum))
7939 did_ai = TRUE; /* delete the indent if the line stays empty */
7940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941}
7942
7943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007944fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
7946 if (p_paste)
7947 return;
7948# ifdef FEAT_LISP
7949 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7950 fixthisline(get_lisp_indent);
7951# endif
7952# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7953 else
7954# endif
7955# ifdef FEAT_CINDENT
7956 if (cindent_on())
7957 do_c_expr_indent();
7958# endif
7959}
7960
7961#endif
7962
7963#ifdef FEAT_CINDENT
7964/*
7965 * return TRUE if 'cinkeys' contains the key "keytyped",
7966 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007967 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7969 *
7970 * "keytyped" can have a few special values:
7971 * KEY_OPEN_FORW
7972 * KEY_OPEN_BACK
7973 * KEY_COMPLETE just finished completion.
7974 *
7975 * If line_is_empty is TRUE accept keys with '0' before them.
7976 */
7977 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007978in_cinkeys(
7979 int keytyped,
7980 int when,
7981 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982{
7983 char_u *look;
7984 int try_match;
7985 int try_match_word;
7986 char_u *p;
7987 char_u *line;
7988 int icase;
7989 int i;
7990
Bram Moolenaar30848942009-12-24 14:46:12 +00007991 if (keytyped == NUL)
7992 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7993 return FALSE;
7994
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995#ifdef FEAT_EVAL
7996 if (*curbuf->b_p_inde != NUL)
7997 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7998 else
7999#endif
8000 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8001 while (*look)
8002 {
8003 /*
8004 * Find out if we want to try a match with this key, depending on
8005 * 'when' and a '*' or '!' before the key.
8006 */
8007 switch (when)
8008 {
8009 case '*': try_match = (*look == '*'); break;
8010 case '!': try_match = (*look == '!'); break;
8011 default: try_match = (*look != '*'); break;
8012 }
8013 if (*look == '*' || *look == '!')
8014 ++look;
8015
8016 /*
8017 * If there is a '0', only accept a match if the line is empty.
8018 * But may still match when typing last char of a word.
8019 */
8020 if (*look == '0')
8021 {
8022 try_match_word = try_match;
8023 if (!line_is_empty)
8024 try_match = FALSE;
8025 ++look;
8026 }
8027 else
8028 try_match_word = FALSE;
8029
8030 /*
8031 * does it look like a control character?
8032 */
8033 if (*look == '^'
8034#ifdef EBCDIC
8035 && (Ctrl_chr(look[1]) != 0)
8036#else
8037 && look[1] >= '?' && look[1] <= '_'
8038#endif
8039 )
8040 {
8041 if (try_match && keytyped == Ctrl_chr(look[1]))
8042 return TRUE;
8043 look += 2;
8044 }
8045 /*
8046 * 'o' means "o" command, open forward.
8047 * 'O' means "O" command, open backward.
8048 */
8049 else if (*look == 'o')
8050 {
8051 if (try_match && keytyped == KEY_OPEN_FORW)
8052 return TRUE;
8053 ++look;
8054 }
8055 else if (*look == 'O')
8056 {
8057 if (try_match && keytyped == KEY_OPEN_BACK)
8058 return TRUE;
8059 ++look;
8060 }
8061
8062 /*
8063 * 'e' means to check for "else" at start of line and just before the
8064 * cursor.
8065 */
8066 else if (*look == 'e')
8067 {
8068 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8069 {
8070 p = ml_get_curline();
8071 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8072 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8073 return TRUE;
8074 }
8075 ++look;
8076 }
8077
8078 /*
8079 * ':' only causes an indent if it is at the end of a label or case
8080 * statement, or when it was before typing the ':' (to fix
8081 * class::method for C++).
8082 */
8083 else if (*look == ':')
8084 {
8085 if (try_match && keytyped == ':')
8086 {
8087 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008088 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008090 /* Need to get the line again after cin_islabel(). */
8091 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 if (curwin->w_cursor.col > 2
8093 && p[curwin->w_cursor.col - 1] == ':'
8094 && p[curwin->w_cursor.col - 2] == ':')
8095 {
8096 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008097 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008098 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 p = ml_get_curline();
8100 p[curwin->w_cursor.col - 1] = ':';
8101 if (i)
8102 return TRUE;
8103 }
8104 }
8105 ++look;
8106 }
8107
8108
8109 /*
8110 * Is it a key in <>, maybe?
8111 */
8112 else if (*look == '<')
8113 {
8114 if (try_match)
8115 {
8116 /*
8117 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8118 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8119 * >, *, : and ! keys if they really really want to.
8120 */
8121 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8122 && keytyped == look[1])
8123 return TRUE;
8124
8125 if (keytyped == get_special_key_code(look + 1))
8126 return TRUE;
8127 }
8128 while (*look && *look != '>')
8129 look++;
8130 while (*look == '>')
8131 look++;
8132 }
8133
8134 /*
8135 * Is it a word: "=word"?
8136 */
8137 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8138 {
8139 ++look;
8140 if (*look == '~')
8141 {
8142 icase = TRUE;
8143 ++look;
8144 }
8145 else
8146 icase = FALSE;
8147 p = vim_strchr(look, ',');
8148 if (p == NULL)
8149 p = look + STRLEN(look);
8150 if ((try_match || try_match_word)
8151 && curwin->w_cursor.col >= (colnr_T)(p - look))
8152 {
8153 int match = FALSE;
8154
8155#ifdef FEAT_INS_EXPAND
8156 if (keytyped == KEY_COMPLETE)
8157 {
8158 char_u *s;
8159
8160 /* Just completed a word, check if it starts with "look".
8161 * search back for the start of a word. */
8162 line = ml_get_curline();
8163# ifdef FEAT_MBYTE
8164 if (has_mbyte)
8165 {
8166 char_u *n;
8167
8168 for (s = line + curwin->w_cursor.col; s > line; s = n)
8169 {
8170 n = mb_prevptr(line, s);
8171 if (!vim_iswordp(n))
8172 break;
8173 }
8174 }
8175 else
8176# endif
8177 for (s = line + curwin->w_cursor.col; s > line; --s)
8178 if (!vim_iswordc(s[-1]))
8179 break;
8180 if (s + (p - look) <= line + curwin->w_cursor.col
8181 && (icase
8182 ? MB_STRNICMP(s, look, p - look)
8183 : STRNCMP(s, look, p - look)) == 0)
8184 match = TRUE;
8185 }
8186 else
8187#endif
8188 /* TODO: multi-byte */
8189 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8190 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8191 {
8192 line = ml_get_cursor();
8193 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8194 || !vim_iswordc(line[-(p - look) - 1]))
8195 && (icase
8196 ? MB_STRNICMP(line - (p - look), look, p - look)
8197 : STRNCMP(line - (p - look), look, p - look))
8198 == 0)
8199 match = TRUE;
8200 }
8201 if (match && try_match_word && !try_match)
8202 {
8203 /* "0=word": Check if there are only blanks before the
8204 * word. */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02008205 if (getwhitecols(line) !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 (int)(curwin->w_cursor.col - (p - look)))
8207 match = FALSE;
8208 }
8209 if (match)
8210 return TRUE;
8211 }
8212 look = p;
8213 }
8214
8215 /*
8216 * ok, it's a boring generic character.
8217 */
8218 else
8219 {
8220 if (try_match && *look == keytyped)
8221 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008222 if (*look != NUL)
8223 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 }
8225
8226 /*
8227 * Skip over ", ".
8228 */
8229 look = skip_to_option_part(look);
8230 }
8231 return FALSE;
8232}
8233#endif /* FEAT_CINDENT */
8234
8235#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8236/*
8237 * Map Hebrew keyboard when in hkmap mode.
8238 */
8239 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008240hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241{
8242 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8243 {
8244 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8245 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8246 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8247 static char_u map[26] =
8248 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8249 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8250 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8251 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8252 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8253 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8254 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8255 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8256 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8257
8258 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8259 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8260 /* '-1'='sofit' */
8261 else if (c == 'x')
8262 return 'X';
8263 else if (c == 'q')
8264 return '\''; /* {geresh}={'} */
8265 else if (c == 246)
8266 return ' '; /* \"o --> ' ' for a german keyboard */
8267 else if (c == 228)
8268 return ' '; /* \"a --> ' ' -- / -- */
8269 else if (c == 252)
8270 return ' '; /* \"u --> ' ' -- / -- */
8271#ifdef EBCDIC
8272 else if (islower(c))
8273#else
8274 /* NOTE: islower() does not do the right thing for us on Linux so we
8275 * do this the same was as 5.7 and previous, so it works correctly on
8276 * all systems. Specifically, the e.g. Delete and Arrow keys are
8277 * munged and won't work if e.g. searching for Hebrew text.
8278 */
8279 else if (c >= 'a' && c <= 'z')
8280#endif
8281 return (int)(map[CharOrdLow(c)] + p_aleph);
8282 else
8283 return c;
8284 }
8285 else
8286 {
8287 switch (c)
8288 {
8289 case '`': return ';';
8290 case '/': return '.';
8291 case '\'': return ',';
8292 case 'q': return '/';
8293 case 'w': return '\'';
8294
8295 /* Hebrew letters - set offset from 'a' */
8296 case ',': c = '{'; break;
8297 case '.': c = 'v'; break;
8298 case ';': c = 't'; break;
8299 default: {
8300 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8301
8302#ifdef EBCDIC
8303 /* see note about islower() above */
8304 if (!islower(c))
8305#else
8306 if (c < 'a' || c > 'z')
8307#endif
8308 return c;
8309 c = str[CharOrdLow(c)];
8310 break;
8311 }
8312 }
8313
8314 return (int)(CharOrdLow(c) + p_aleph);
8315 }
8316}
8317#endif
8318
8319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008320ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321{
8322 int need_redraw = FALSE;
8323 int regname;
8324 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008325 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326
8327 /*
8328 * If we are going to wait for a character, show a '"'.
8329 */
8330 pc_status = PC_STATUS_UNSET;
8331 if (redrawing() && !char_avail())
8332 {
8333 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008334 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335
8336 edit_putchar('"', TRUE);
8337#ifdef FEAT_CMDL_INFO
8338 add_to_showcmd_c(Ctrl_R);
8339#endif
8340 }
8341
8342#ifdef USE_ON_FLY_SCROLL
8343 dont_scroll = TRUE; /* disallow scrolling here */
8344#endif
8345
8346 /*
8347 * Don't map the register name. This also prevents the mode message to be
8348 * deleted when ESC is hit.
8349 */
8350 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008351 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8354 {
8355 /* Get a third key for literal register insertion */
8356 literally = regname;
8357#ifdef FEAT_CMDL_INFO
8358 add_to_showcmd_c(literally);
8359#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008360 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 }
8363 --no_mapping;
8364
8365#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008366 /* Don't call u_sync() while typing the expression or giving an error
8367 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 ++no_u_sync;
8369 if (regname == '=')
8370 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008371# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008373# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008374 /* Sync undo when evaluating the expression calls setline() or
8375 * append(), so that it can be undone separately. */
8376 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008377
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008379# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 /* Restore the Input Method. */
8381 if (im_on)
8382 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008385 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8386 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008387 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 else
8391 {
8392#endif
8393 if (literally == Ctrl_O || literally == Ctrl_P)
8394 {
8395 /* Append the command to the redo buffer. */
8396 AppendCharToRedobuff(Ctrl_R);
8397 AppendCharToRedobuff(literally);
8398 AppendCharToRedobuff(regname);
8399
8400 do_put(regname, BACKWARD, 1L,
8401 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8402 }
8403 else if (insert_reg(regname, literally) == FAIL)
8404 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008405 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 need_redraw = TRUE; /* remove the '"' */
8407 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008408 else if (stop_insert_mode)
8409 /* When the '=' register was used and a function was invoked that
8410 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8411 * insert anything, need to remove the '"' */
8412 need_redraw = TRUE;
8413
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414#ifdef FEAT_EVAL
8415 }
8416 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008417 if (u_sync_once == 1)
8418 ins_need_undo = TRUE;
8419 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420#endif
8421#ifdef FEAT_CMDL_INFO
8422 clear_showcmd();
8423#endif
8424
8425 /* If the inserted register is empty, we need to remove the '"' */
8426 if (need_redraw || stuff_empty())
8427 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008428
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008429 /* Disallow starting Visual mode here, would get a weird mode. */
8430 if (!vis_active && VIsual_active)
8431 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432}
8433
8434/*
8435 * CTRL-G commands in Insert mode.
8436 */
8437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008438ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439{
8440 int c;
8441
8442#ifdef FEAT_INS_EXPAND
8443 /* Right after CTRL-X the cursor will be after the ruler. */
8444 setcursor();
8445#endif
8446
8447 /*
8448 * Don't map the second key. This also prevents the mode message to be
8449 * deleted when ESC is hit.
8450 */
8451 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008452 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 --no_mapping;
8454 switch (c)
8455 {
8456 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8457 case K_UP:
8458 case Ctrl_K:
8459 case 'k': ins_up(TRUE);
8460 break;
8461
8462 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8463 case K_DOWN:
8464 case Ctrl_J:
8465 case 'j': ins_down(TRUE);
8466 break;
8467
8468 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008469 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008471
8472 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008473 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008474 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008475 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 break;
8477
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008478 /* CTRL-G U: do not break undo with the next char */
8479 case 'U':
8480 /* Allow one left/right cursor movement with the next char,
8481 * without breaking undo. */
8482 dont_sync_undo = MAYBE;
8483 break;
8484
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008486 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 }
8488}
8489
8490/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008491 * CTRL-^ in Insert mode.
8492 */
8493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008494ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008495{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008496 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008497 {
8498 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8499 if (State & LANGMAP)
8500 {
8501 curbuf->b_p_iminsert = B_IMODE_NONE;
8502 State &= ~LANGMAP;
8503 }
8504 else
8505 {
8506 curbuf->b_p_iminsert = B_IMODE_LMAP;
8507 State |= LANGMAP;
8508#ifdef USE_IM_CONTROL
8509 im_set_active(FALSE);
8510#endif
8511 }
8512 }
8513#ifdef USE_IM_CONTROL
8514 else
8515 {
8516 /* There are no ":lmap" mappings, toggle IM */
8517 if (im_get_status())
8518 {
8519 curbuf->b_p_iminsert = B_IMODE_NONE;
8520 im_set_active(FALSE);
8521 }
8522 else
8523 {
8524 curbuf->b_p_iminsert = B_IMODE_IM;
8525 State &= ~LANGMAP;
8526 im_set_active(TRUE);
8527 }
8528 }
8529#endif
8530 set_iminsert_global();
8531 showmode();
8532#ifdef FEAT_GUI
8533 /* may show different cursor shape or color */
8534 if (gui.in_use)
8535 gui_update_cursor(TRUE, FALSE);
8536#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008537#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008538 /* Show/unshow value of 'keymap' in status lines. */
8539 status_redraw_curbuf();
8540#endif
8541}
8542
8543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 * Handle ESC in insert mode.
8545 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8546 * insert.
8547 */
8548 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008549ins_esc(
8550 long *count,
8551 int cmdchar,
8552 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553{
8554 int temp;
8555 static int disabled_redraw = FALSE;
8556
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008557#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008558 check_spell_redraw();
8559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560#if defined(FEAT_HANGULIN)
8561# if defined(ESC_CHG_TO_ENG_MODE)
8562 hangul_input_state_set(0);
8563# endif
8564 if (composing_hangul)
8565 {
8566 push_raw_key(composing_hangul_buffer, 2);
8567 composing_hangul = 0;
8568 }
8569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570
8571 temp = curwin->w_cursor.col;
8572 if (disabled_redraw)
8573 {
8574 --RedrawingDisabled;
8575 disabled_redraw = FALSE;
8576 }
8577 if (!arrow_used)
8578 {
8579 /*
8580 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008581 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8582 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 */
8584 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008585 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586
8587 /*
8588 * Repeating insert may take a long time. Check for
8589 * interrupt now and then.
8590 */
8591 if (*count > 0)
8592 {
8593 line_breakcheck();
8594 if (got_int)
8595 *count = 0;
8596 }
8597
8598 if (--*count > 0) /* repeat what was typed */
8599 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008600 /* Vi repeats the insert without replacing characters. */
8601 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8602 State &= ~REPLACE_FLAG;
8603
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 (void)start_redo_ins();
8605 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008606 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 ++RedrawingDisabled;
8608 disabled_redraw = TRUE;
8609 return FALSE; /* repeat the insert */
8610 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008611 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 undisplay_dollar();
8613 }
8614
8615 /* When an autoindent was removed, curswant stays after the
8616 * indent */
8617 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8618 curwin->w_set_curswant = TRUE;
8619
8620 /* Remember the last Insert position in the '^ mark. */
8621 if (!cmdmod.keepjumps)
8622 curbuf->b_last_insert = curwin->w_cursor;
8623
8624 /*
8625 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008626 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008628 if (!nomove
8629 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630#ifdef FEAT_VIRTUALEDIT
8631 || curwin->w_cursor.coladd > 0
8632#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008633 )
8634 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008635 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636#ifdef FEAT_RIGHTLEFT
8637 && !revins_on
8638#endif
8639 )
8640 {
8641#ifdef FEAT_VIRTUALEDIT
8642 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8643 {
8644 oneleft();
8645 if (restart_edit != NUL)
8646 ++curwin->w_cursor.coladd;
8647 }
8648 else
8649#endif
8650 {
8651 --curwin->w_cursor.col;
8652#ifdef FEAT_MBYTE
8653 /* Correct cursor for multi-byte character. */
8654 if (has_mbyte)
8655 mb_adjust_cursor();
8656#endif
8657 }
8658 }
8659
8660#ifdef USE_IM_CONTROL
8661 /* Disable IM to allow typing English directly for Normal mode commands.
8662 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8663 * well). */
8664 if (!(State & LANGMAP))
8665 im_save_status(&curbuf->b_p_iminsert);
8666 im_set_active(FALSE);
8667#endif
8668
8669 State = NORMAL;
8670 /* need to position cursor again (e.g. when on a TAB ) */
8671 changed_cline_bef_curs();
8672
8673#ifdef FEAT_MOUSE
8674 setmouse();
8675#endif
8676#ifdef CURSOR_SHAPE
8677 ui_cursor_shape(); /* may show different cursor shape */
8678#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008679 if (!p_ek)
8680 /* Re-enable bracketed paste mode. */
8681 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682
8683 /*
8684 * When recording or for CTRL-O, need to display the new mode.
8685 * Otherwise remove the mode message.
8686 */
8687 if (Recording || restart_edit != NUL)
8688 showmode();
8689 else if (p_smd)
8690 MSG("");
8691
8692 return TRUE; /* exit Insert mode */
8693}
8694
8695#ifdef FEAT_RIGHTLEFT
8696/*
8697 * Toggle language: hkmap and revins_on.
8698 * Move to end of reverse inserted text.
8699 */
8700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008701ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702{
8703 if (revins_on && revins_chars && revins_scol >= 0)
8704 {
8705 while (gchar_cursor() != NUL && revins_chars--)
8706 ++curwin->w_cursor.col;
8707 }
8708 p_ri = !p_ri;
8709 revins_on = (State == INSERT && p_ri);
8710 if (revins_on)
8711 {
8712 revins_scol = curwin->w_cursor.col;
8713 revins_legal++;
8714 revins_chars = 0;
8715 undisplay_dollar();
8716 }
8717 else
8718 revins_scol = -1;
8719#ifdef FEAT_FKMAP
8720 if (p_altkeymap)
8721 {
8722 /*
8723 * to be consistent also for redo command, using '.'
8724 * set arrow_used to true and stop it - causing to redo
8725 * characters entered in one mode (normal/reverse insert).
8726 */
8727 arrow_used = TRUE;
8728 (void)stop_arrow();
8729 p_fkmap = curwin->w_p_rl ^ p_ri;
8730 if (p_fkmap && p_ri)
8731 State = INSERT;
8732 }
8733 else
8734#endif
8735 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8736 showmode();
8737}
8738#endif
8739
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740/*
8741 * If 'keymodel' contains "startsel", may start selection.
8742 * Returns TRUE when a CTRL-O and other keys stuffed.
8743 */
8744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008745ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746{
8747 if (km_startsel)
8748 switch (c)
8749 {
8750 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 case K_PAGEUP:
8753 case K_KPAGEUP:
8754 case K_PAGEDOWN:
8755 case K_KPAGEDOWN:
8756# ifdef MACOS
8757 case K_LEFT:
8758 case K_RIGHT:
8759 case K_UP:
8760 case K_DOWN:
8761 case K_END:
8762 case K_HOME:
8763# endif
8764 if (!(mod_mask & MOD_MASK_SHIFT))
8765 break;
8766 /* FALLTHROUGH */
8767 case K_S_LEFT:
8768 case K_S_RIGHT:
8769 case K_S_UP:
8770 case K_S_DOWN:
8771 case K_S_END:
8772 case K_S_HOME:
8773 /* Start selection right away, the cursor can move with
8774 * CTRL-O when beyond the end of the line. */
8775 start_selection();
8776
8777 /* Execute the key in (insert) Select mode. */
8778 stuffcharReadbuff(Ctrl_O);
8779 if (mod_mask)
8780 {
8781 char_u buf[4];
8782
8783 buf[0] = K_SPECIAL;
8784 buf[1] = KS_MODIFIER;
8785 buf[2] = mod_mask;
8786 buf[3] = NUL;
8787 stuffReadbuff(buf);
8788 }
8789 stuffcharReadbuff(c);
8790 return TRUE;
8791 }
8792 return FALSE;
8793}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794
8795/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008796 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008797 */
8798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008799ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008800{
8801#ifdef FEAT_FKMAP
8802 if (p_fkmap && p_ri)
8803 {
8804 beep_flush();
8805 EMSG(farsi_text_3); /* encoded in Farsi */
8806 return;
8807 }
8808#endif
8809
8810#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008811# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008812 set_vim_var_string(VV_INSERTMODE,
8813 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008814# ifdef FEAT_VREPLACE
8815 replaceState == VREPLACE ? "v" :
8816# endif
8817 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008818# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008819 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8820#endif
8821 if (State & REPLACE_FLAG)
8822 State = INSERT | (State & LANGMAP);
8823 else
8824 State = replaceState | (State & LANGMAP);
8825 AppendCharToRedobuff(K_INS);
8826 showmode();
8827#ifdef CURSOR_SHAPE
8828 ui_cursor_shape(); /* may show different cursor shape */
8829#endif
8830}
8831
8832/*
8833 * Pressed CTRL-O in Insert mode.
8834 */
8835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008836ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008837{
8838#ifdef FEAT_VREPLACE
8839 if (State & VREPLACE_FLAG)
8840 restart_edit = 'V';
8841 else
8842#endif
8843 if (State & REPLACE_FLAG)
8844 restart_edit = 'R';
8845 else
8846 restart_edit = 'I';
8847#ifdef FEAT_VIRTUALEDIT
8848 if (virtual_active())
8849 ins_at_eol = FALSE; /* cursor always keeps its column */
8850 else
8851#endif
8852 ins_at_eol = (gchar_cursor() == NUL);
8853}
8854
8855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 * If the cursor is on an indent, ^T/^D insert/delete one
8857 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008858 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 * with vi. But vi only supports ^T and ^D after an
8860 * autoindent, we support it everywhere.
8861 */
8862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008863ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864{
8865 if (stop_arrow() == FAIL)
8866 return;
8867 AppendCharToRedobuff(c);
8868
8869 /*
8870 * 0^D and ^^D: remove all indent.
8871 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008872 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8873 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 {
8875 --curwin->w_cursor.col;
8876 (void)del_char(FALSE); /* delete the '^' or '0' */
8877 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8878 if (State & REPLACE_FLAG)
8879 replace_pop_ins();
8880 if (lastc == '^')
8881 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008882 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 }
8884 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008885 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886
8887 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8888 did_ai = FALSE;
8889#ifdef FEAT_SMARTINDENT
8890 did_si = FALSE;
8891 can_si = FALSE;
8892 can_si_back = FALSE;
8893#endif
8894#ifdef FEAT_CINDENT
8895 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8896#endif
8897}
8898
8899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008900ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901{
8902 int temp;
8903
8904 if (stop_arrow() == FAIL)
8905 return;
8906 if (gchar_cursor() == NUL) /* delete newline */
8907 {
8908 temp = curwin->w_cursor.col;
8909 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008910 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008911 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 else
8913 curwin->w_cursor.col = temp;
8914 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008915 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8916 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 did_ai = FALSE;
8918#ifdef FEAT_SMARTINDENT
8919 did_si = FALSE;
8920 can_si = FALSE;
8921 can_si_back = FALSE;
8922#endif
8923 AppendCharToRedobuff(K_DEL);
8924}
8925
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008926static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008927
8928/*
8929 * Delete one character for ins_bs().
8930 */
8931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008932ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008933{
8934 dec_cursor();
8935 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8936 if (State & REPLACE_FLAG)
8937 {
8938 /* Don't delete characters before the insert point when in
8939 * Replace mode */
8940 if (curwin->w_cursor.lnum != Insstart.lnum
8941 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008942 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008943 }
8944 else
8945 (void)del_char(FALSE);
8946}
8947
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948/*
8949 * Handle Backspace, delete-word and delete-line in Insert mode.
8950 * Return TRUE when backspace was actually used.
8951 */
8952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008953ins_bs(
8954 int c,
8955 int mode,
8956 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957{
8958 linenr_T lnum;
8959 int cc;
8960 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008961 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 colnr_T mincol;
8963 int did_backspace = FALSE;
8964 int in_indent;
8965 int oldState;
8966#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008967 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968#endif
8969
8970 /*
8971 * can't delete anything in an empty file
8972 * can't backup past first character in buffer
8973 * can't backup past starting point unless 'backspace' > 1
8974 * can backup to a previous line if 'backspace' == 0
8975 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008976 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 || (
8978#ifdef FEAT_RIGHTLEFT
8979 !revins_on &&
8980#endif
8981 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8982 || (!can_bs(BS_START)
8983 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008984 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8985 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8987 && curwin->w_cursor.col <= ai_col)
8988 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8989 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008990 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 return FALSE;
8992 }
8993
8994 if (stop_arrow() == FAIL)
8995 return FALSE;
8996 in_indent = inindent(0);
8997#ifdef FEAT_CINDENT
8998 if (in_indent)
8999 can_cindent = FALSE;
9000#endif
9001#ifdef FEAT_COMMENTS
9002 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9003#endif
9004#ifdef FEAT_RIGHTLEFT
9005 if (revins_on) /* put cursor after last inserted char */
9006 inc_cursor();
9007#endif
9008
9009#ifdef FEAT_VIRTUALEDIT
9010 /* Virtualedit:
9011 * BACKSPACE_CHAR eats a virtual space
9012 * BACKSPACE_WORD eats all coladd
9013 * BACKSPACE_LINE eats all coladd and keeps going
9014 */
9015 if (curwin->w_cursor.coladd > 0)
9016 {
9017 if (mode == BACKSPACE_CHAR)
9018 {
9019 --curwin->w_cursor.coladd;
9020 return TRUE;
9021 }
9022 if (mode == BACKSPACE_WORD)
9023 {
9024 curwin->w_cursor.coladd = 0;
9025 return TRUE;
9026 }
9027 curwin->w_cursor.coladd = 0;
9028 }
9029#endif
9030
9031 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009032 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 */
9034 if (curwin->w_cursor.col == 0)
9035 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009036 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009037 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038#ifdef FEAT_RIGHTLEFT
9039 || revins_on
9040#endif
9041 )
9042 {
9043 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9044 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9045 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009046 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009047 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 }
9049 /*
9050 * In replace mode:
9051 * cc < 0: NL was inserted, delete it
9052 * cc >= 0: NL was replaced, put original characters back
9053 */
9054 cc = -1;
9055 if (State & REPLACE_FLAG)
9056 cc = replace_pop(); /* returns -1 if NL was inserted */
9057 /*
9058 * In replace mode, in the line we started replacing, we only move the
9059 * cursor.
9060 */
9061 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9062 {
9063 dec_cursor();
9064 }
9065 else
9066 {
9067#ifdef FEAT_VREPLACE
9068 if (!(State & VREPLACE_FLAG)
9069 || curwin->w_cursor.lnum > orig_line_count)
9070#endif
9071 {
9072 temp = gchar_cursor(); /* remember current char */
9073 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009074
9075 /* When "aw" is in 'formatoptions' we must delete the space at
9076 * the end of the line, otherwise the line will be broken
9077 * again when auto-formatting. */
9078 if (has_format_option(FO_AUTO)
9079 && has_format_option(FO_WHITE_PAR))
9080 {
9081 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9082 TRUE);
9083 int len;
9084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009085 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009086 if (len > 0 && ptr[len - 1] == ' ')
9087 ptr[len - 1] = NUL;
9088 }
9089
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009090 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 if (temp == NUL && gchar_cursor() != NUL)
9092 inc_cursor();
9093 }
9094#ifdef FEAT_VREPLACE
9095 else
9096 dec_cursor();
9097#endif
9098
9099 /*
9100 * In REPLACE mode we have to put back the text that was replaced
9101 * by the NL. On the replace stack is first a NUL-terminated
9102 * sequence of characters that were deleted and then the
9103 * characters that NL replaced.
9104 */
9105 if (State & REPLACE_FLAG)
9106 {
9107 /*
9108 * Do the next ins_char() in NORMAL state, to
9109 * prevent ins_char() from replacing characters and
9110 * avoiding showmatch().
9111 */
9112 oldState = State;
9113 State = NORMAL;
9114 /*
9115 * restore characters (blanks) deleted after cursor
9116 */
9117 while (cc > 0)
9118 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009119 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120#ifdef FEAT_MBYTE
9121 mb_replace_pop_ins(cc);
9122#else
9123 ins_char(cc);
9124#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009125 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 cc = replace_pop();
9127 }
9128 /* restore the characters that NL replaced */
9129 replace_pop_ins();
9130 State = oldState;
9131 }
9132 }
9133 did_ai = FALSE;
9134 }
9135 else
9136 {
9137 /*
9138 * Delete character(s) before the cursor.
9139 */
9140#ifdef FEAT_RIGHTLEFT
9141 if (revins_on) /* put cursor on last inserted char */
9142 dec_cursor();
9143#endif
9144 mincol = 0;
9145 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009146 if (mode == BACKSPACE_LINE
9147 && (curbuf->b_p_ai
9148#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009149 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009150#endif
9151 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152#ifdef FEAT_RIGHTLEFT
9153 && !revins_on
9154#endif
9155 )
9156 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009157 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009159 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009161 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 }
9163
9164 /*
9165 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9166 */
9167 if ( mode == BACKSPACE_CHAR
9168 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009169 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009170 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 && (*(ml_get_cursor() - 1) == TAB
9172 || (*(ml_get_cursor() - 1) == ' '
9173 && (!*inserted_space_p
9174 || arrow_used))))))
9175 {
9176 int ts;
9177 colnr_T vcol;
9178 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009179 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180
9181 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009182 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009183 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009185 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 /* Compute the virtual column where we want to be. Since
9187 * 'showbreak' may get in the way, need to get the last column of
9188 * the previous character. */
9189 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009190 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 dec_cursor();
9192 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9193 inc_cursor();
9194 want_vcol = (want_vcol / ts) * ts;
9195
9196 /* delete characters until we are at or before want_vcol */
9197 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009198 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009199 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200
9201 /* insert extra spaces until we are at want_vcol */
9202 while (vcol < want_vcol)
9203 {
9204 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009205 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9206 && curwin->w_cursor.col < Insstart_orig.col)
9207 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208
9209#ifdef FEAT_VREPLACE
9210 if (State & VREPLACE_FLAG)
9211 ins_char(' ');
9212 else
9213#endif
9214 {
9215 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009216 if ((State & REPLACE_FLAG))
9217 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 }
9219 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9220 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009221
9222 /* If we are now back where we started delete one character. Can
9223 * happen when using 'sts' and 'linebreak'. */
9224 if (vcol >= start_vcol)
9225 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 }
9227
9228 /*
9229 * Delete upto starting point, start of line or previous word.
9230 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009231 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009233#ifdef FEAT_MBYTE
9234 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009236 if (has_mbyte)
9237 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009239 do
9240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009242 if (!revins_on) /* put cursor on char to be deleted */
9243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009245
9246 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009248 /* look multi-byte character class */
9249 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009251 prev_cclass = cclass;
9252 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009255
9256 /* start of word? */
9257 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9258 {
9259 mode = BACKSPACE_WORD_NOT_SPACE;
9260 temp = vim_iswordc(cc);
9261 }
9262 /* end of word? */
9263 else if (mode == BACKSPACE_WORD_NOT_SPACE
9264 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9265#ifdef FEAT_MBYTE
9266 || prev_cclass != cclass
9267#endif
9268 ))
9269 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009271 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009273 inc_cursor();
9274#ifdef FEAT_RIGHTLEFT
9275 else if (State & REPLACE_FLAG)
9276 dec_cursor();
9277#endif
9278 break;
9279 }
9280 if (State & REPLACE_FLAG)
9281 replace_do_bs(-1);
9282 else
9283 {
9284#ifdef FEAT_MBYTE
9285 if (enc_utf8 && p_deco)
9286 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9287#endif
9288 (void)del_char(FALSE);
9289#ifdef FEAT_MBYTE
9290 /*
9291 * If there are combining characters and 'delcombine' is set
9292 * move the cursor back. Don't back up before the base
9293 * character.
9294 */
9295 if (enc_utf8 && p_deco && cpc[0] != NUL)
9296 inc_cursor();
9297#endif
9298#ifdef FEAT_RIGHTLEFT
9299 if (revins_chars)
9300 {
9301 revins_chars--;
9302 revins_legal++;
9303 }
9304 if (revins_on && gchar_cursor() == NUL)
9305 break;
9306#endif
9307 }
9308 /* Just a single backspace?: */
9309 if (mode == BACKSPACE_CHAR)
9310 break;
9311 } while (
9312#ifdef FEAT_RIGHTLEFT
9313 revins_on ||
9314#endif
9315 (curwin->w_cursor.col > mincol
9316 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9317 || curwin->w_cursor.col != Insstart_orig.col)));
9318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 did_backspace = TRUE;
9320 }
9321#ifdef FEAT_SMARTINDENT
9322 did_si = FALSE;
9323 can_si = FALSE;
9324 can_si_back = FALSE;
9325#endif
9326 if (curwin->w_cursor.col <= 1)
9327 did_ai = FALSE;
9328 /*
9329 * It's a little strange to put backspaces into the redo
9330 * buffer, but it makes auto-indent a lot easier to deal
9331 * with.
9332 */
9333 AppendCharToRedobuff(c);
9334
9335 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009336 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9337 && curwin->w_cursor.col < Insstart_orig.col)
9338 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339
9340 /* vi behaviour: the cursor moves backward but the character that
9341 * was there remains visible
9342 * Vim behaviour: the cursor moves backward and the character that
9343 * was there is erased from the screen.
9344 * We can emulate the vi behaviour by pretending there is a dollar
9345 * displayed even when there isn't.
9346 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009347 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 dollar_vcol = curwin->w_virtcol;
9349
Bram Moolenaarce3be472008-01-14 19:12:28 +00009350#ifdef FEAT_FOLDING
9351 /* When deleting a char the cursor line must never be in a closed fold.
9352 * E.g., when 'foldmethod' is indent and deleting the first non-white
9353 * char before a Tab. */
9354 if (did_backspace)
9355 foldOpenCursor();
9356#endif
9357
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 return did_backspace;
9359}
9360
9361#ifdef FEAT_MOUSE
9362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009363ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364{
9365 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009366 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367
9368# ifdef FEAT_GUI
9369 /* When GUI is active, also move/paste when 'mouse' is empty */
9370 if (!gui.in_use)
9371# endif
9372 if (!mouse_has(MOUSE_INSERT))
9373 return;
9374
9375 undisplay_dollar();
9376 tpos = curwin->w_cursor;
9377 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9378 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009379 win_T *new_curwin = curwin;
9380
9381 if (curwin != old_curwin && win_valid(old_curwin))
9382 {
9383 /* Mouse took us to another window. We need to go back to the
9384 * previous one to stop insert there properly. */
9385 curwin = old_curwin;
9386 curbuf = curwin->w_buffer;
9387 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009388 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009389 if (curwin != new_curwin && win_valid(new_curwin))
9390 {
9391 curwin = new_curwin;
9392 curbuf = curwin->w_buffer;
9393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394# ifdef FEAT_CINDENT
9395 can_cindent = TRUE;
9396# endif
9397 }
9398
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 /* redraw status lines (in case another window became active) */
9400 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401}
9402
9403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009404ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405{
9406 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009407 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009408# ifdef FEAT_INS_EXPAND
9409 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410# endif
9411
9412 tpos = curwin->w_cursor;
9413
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009414 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 {
9416 int row, col;
9417
9418 row = mouse_row;
9419 col = mouse_col;
9420
9421 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009422 wp = mouse_find_win(&row, &col);
9423 if (wp == NULL)
9424 return;
9425 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 curbuf = curwin->w_buffer;
9427 }
9428 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 undisplay_dollar();
9430
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009431# ifdef FEAT_INS_EXPAND
9432 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009433 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009434# endif
9435 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009436 if (dir == MSCR_DOWN || dir == MSCR_UP)
9437 {
9438 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9439 scroll_redraw(dir,
9440 (long)(curwin->w_botline - curwin->w_topline));
9441 else
9442 scroll_redraw(dir, 3L);
9443 }
9444#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009445 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009446 {
9447 int val, step = 6;
9448
9449 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009450 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009451 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9452 if (val < 0)
9453 val = 0;
9454 gui_do_horiz_scroll(val, TRUE);
9455 }
9456#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009457# ifdef FEAT_INS_EXPAND
9458 did_scroll = TRUE;
9459# endif
9460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 curwin->w_redr_status = TRUE;
9463
9464 curwin = old_curwin;
9465 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009467# ifdef FEAT_INS_EXPAND
9468 /* The popup menu may overlay the window, need to redraw it.
9469 * TODO: Would be more efficient to only redraw the windows that are
9470 * overlapped by the popup menu. */
9471 if (pum_visible() && did_scroll)
9472 {
9473 redraw_all_later(NOT_VALID);
9474 ins_compl_show_pum();
9475 }
9476# endif
9477
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009478 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 {
9480 start_arrow(&tpos);
9481# ifdef FEAT_CINDENT
9482 can_cindent = TRUE;
9483# endif
9484 }
9485}
9486#endif
9487
Bram Moolenaarec2da362017-01-21 20:04:22 +01009488/*
9489 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9490 * P_PE literally.
9491 * When "drop" is TRUE then consume the text and drop it.
9492 */
9493 int
9494bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9495{
9496 int c;
9497 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9498 int idx = 0;
9499 char_u *end = find_termcode((char_u *)"PE");
9500 int ret_char = -1;
9501 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009502 int save_paste = p_paste;
9503 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009504
9505 /* If the end code is too long we can't detect it, read everything. */
9506 if (STRLEN(end) >= NUMBUFLEN)
9507 end = NULL;
9508 ++no_mapping;
9509 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009510 p_paste = TRUE;
9511 curbuf->b_p_ai = FALSE;
9512
Bram Moolenaarec2da362017-01-21 20:04:22 +01009513 for (;;)
9514 {
9515 /* When the end is not defined read everything. */
9516 if (end == NULL && vpeekc() == NUL)
9517 break;
9518 c = plain_vgetc();
9519#ifdef FEAT_MBYTE
9520 if (has_mbyte)
9521 idx += (*mb_char2bytes)(c, buf + idx);
9522 else
9523#endif
9524 buf[idx++] = c;
9525 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009526 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009527 {
9528 if (end[idx] == NUL)
9529 break; /* Found the end of paste code. */
9530 continue;
9531 }
9532 if (!drop)
9533 {
9534 switch (mode)
9535 {
9536 case PASTE_CMDLINE:
9537 put_on_cmdline(buf, idx, TRUE);
9538 break;
9539
9540 case PASTE_EX:
9541 if (gap != NULL && ga_grow(gap, idx) == OK)
9542 {
9543 mch_memmove((char *)gap->ga_data + gap->ga_len,
9544 buf, (size_t)idx);
9545 gap->ga_len += idx;
9546 }
9547 break;
9548
9549 case PASTE_INSERT:
9550 if (stop_arrow() == OK)
9551 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009552 c = buf[0];
9553 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9554 ins_eol(c);
9555 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009556 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009557 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009558 AppendToRedobuffLit(buf, idx);
9559 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009560 }
9561 break;
9562
9563 case PASTE_ONE_CHAR:
9564 if (ret_char == -1)
9565 {
9566#ifdef FEAT_MBYTE
9567 if (has_mbyte)
9568 ret_char = (*mb_ptr2char)(buf);
9569 else
9570#endif
9571 ret_char = buf[0];
9572 }
9573 break;
9574 }
9575 }
9576 idx = 0;
9577 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009578
Bram Moolenaarec2da362017-01-21 20:04:22 +01009579 --no_mapping;
9580 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009581 p_paste = save_paste;
9582 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009583
9584 return ret_char;
9585}
9586
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009587#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009589ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009590{
9591 /* We will be leaving the current window, unless closing another tab. */
9592 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9593 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9594 {
9595 undisplay_dollar();
9596 start_arrow(&curwin->w_cursor);
9597# ifdef FEAT_CINDENT
9598 can_cindent = TRUE;
9599# endif
9600 }
9601
9602 if (c == K_TABLINE)
9603 goto_tabpage(current_tab);
9604 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009605 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009606 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009607 redraw_statuslines(); /* will redraw the tabline when needed */
9608 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009609}
9610#endif
9611
9612#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009614ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615{
9616 pos_T tpos;
9617
9618 undisplay_dollar();
9619 tpos = curwin->w_cursor;
9620 if (gui_do_scroll())
9621 {
9622 start_arrow(&tpos);
9623# ifdef FEAT_CINDENT
9624 can_cindent = TRUE;
9625# endif
9626 }
9627}
9628
9629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009630ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631{
9632 pos_T tpos;
9633
9634 undisplay_dollar();
9635 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009636 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 {
9638 start_arrow(&tpos);
9639# ifdef FEAT_CINDENT
9640 can_cindent = TRUE;
9641# endif
9642 }
9643}
9644#endif
9645
9646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009647ins_left(
9648 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649{
9650 pos_T tpos;
9651
9652#ifdef FEAT_FOLDING
9653 if ((fdo_flags & FDO_HOR) && KeyTyped)
9654 foldOpenCursor();
9655#endif
9656 undisplay_dollar();
9657 tpos = curwin->w_cursor;
9658 if (oneleft() == OK)
9659 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009660#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9661 /* Only call start_arrow() when not busy with preediting, it will
9662 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009663 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009664#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009665 {
9666 start_arrow_with_change(&tpos, end_change);
9667 if (!end_change)
9668 AppendCharToRedobuff(K_LEFT);
9669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670#ifdef FEAT_RIGHTLEFT
9671 /* If exit reversed string, position is fixed */
9672 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9673 revins_legal++;
9674 revins_chars++;
9675#endif
9676 }
9677
9678 /*
9679 * if 'whichwrap' set for cursor in insert mode may go to
9680 * previous line
9681 */
9682 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9683 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009684 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 start_arrow(&tpos);
9686 --(curwin->w_cursor.lnum);
9687 coladvance((colnr_T)MAXCOL);
9688 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9689 }
9690 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009691 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009692 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693}
9694
9695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009696ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697{
9698 pos_T tpos;
9699
9700#ifdef FEAT_FOLDING
9701 if ((fdo_flags & FDO_HOR) && KeyTyped)
9702 foldOpenCursor();
9703#endif
9704 undisplay_dollar();
9705 tpos = curwin->w_cursor;
9706 if (c == K_C_HOME)
9707 curwin->w_cursor.lnum = 1;
9708 curwin->w_cursor.col = 0;
9709#ifdef FEAT_VIRTUALEDIT
9710 curwin->w_cursor.coladd = 0;
9711#endif
9712 curwin->w_curswant = 0;
9713 start_arrow(&tpos);
9714}
9715
9716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009717ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
9719 pos_T tpos;
9720
9721#ifdef FEAT_FOLDING
9722 if ((fdo_flags & FDO_HOR) && KeyTyped)
9723 foldOpenCursor();
9724#endif
9725 undisplay_dollar();
9726 tpos = curwin->w_cursor;
9727 if (c == K_C_END)
9728 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9729 coladvance((colnr_T)MAXCOL);
9730 curwin->w_curswant = MAXCOL;
9731
9732 start_arrow(&tpos);
9733}
9734
9735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009736ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737{
9738#ifdef FEAT_FOLDING
9739 if ((fdo_flags & FDO_HOR) && KeyTyped)
9740 foldOpenCursor();
9741#endif
9742 undisplay_dollar();
9743 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9744 {
9745 start_arrow(&curwin->w_cursor);
9746 (void)bck_word(1L, FALSE, FALSE);
9747 curwin->w_set_curswant = TRUE;
9748 }
9749 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009750 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751}
9752
9753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009754ins_right(
9755 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756{
9757#ifdef FEAT_FOLDING
9758 if ((fdo_flags & FDO_HOR) && KeyTyped)
9759 foldOpenCursor();
9760#endif
9761 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009762 if (gchar_cursor() != NUL
9763#ifdef FEAT_VIRTUALEDIT
9764 || virtual_active()
9765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 )
9767 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009768 start_arrow_with_change(&curwin->w_cursor, end_change);
9769 if (!end_change)
9770 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 curwin->w_set_curswant = TRUE;
9772#ifdef FEAT_VIRTUALEDIT
9773 if (virtual_active())
9774 oneright();
9775 else
9776#endif
9777 {
9778#ifdef FEAT_MBYTE
9779 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009780 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 else
9782#endif
9783 ++curwin->w_cursor.col;
9784 }
9785
9786#ifdef FEAT_RIGHTLEFT
9787 revins_legal++;
9788 if (revins_chars)
9789 revins_chars--;
9790#endif
9791 }
9792 /* if 'whichwrap' set for cursor in insert mode, may move the
9793 * cursor to the next line */
9794 else if (vim_strchr(p_ww, ']') != NULL
9795 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9796 {
9797 start_arrow(&curwin->w_cursor);
9798 curwin->w_set_curswant = TRUE;
9799 ++curwin->w_cursor.lnum;
9800 curwin->w_cursor.col = 0;
9801 }
9802 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009803 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009804 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805}
9806
9807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009808ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809{
9810#ifdef FEAT_FOLDING
9811 if ((fdo_flags & FDO_HOR) && KeyTyped)
9812 foldOpenCursor();
9813#endif
9814 undisplay_dollar();
9815 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9816 || gchar_cursor() != NUL)
9817 {
9818 start_arrow(&curwin->w_cursor);
9819 (void)fwd_word(1L, FALSE, 0);
9820 curwin->w_set_curswant = TRUE;
9821 }
9822 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009823 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824}
9825
9826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009827ins_up(
9828 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829{
9830 pos_T tpos;
9831 linenr_T old_topline = curwin->w_topline;
9832#ifdef FEAT_DIFF
9833 int old_topfill = curwin->w_topfill;
9834#endif
9835
9836 undisplay_dollar();
9837 tpos = curwin->w_cursor;
9838 if (cursor_up(1L, TRUE) == OK)
9839 {
9840 if (startcol)
9841 coladvance(getvcol_nolist(&Insstart));
9842 if (old_topline != curwin->w_topline
9843#ifdef FEAT_DIFF
9844 || old_topfill != curwin->w_topfill
9845#endif
9846 )
9847 redraw_later(VALID);
9848 start_arrow(&tpos);
9849#ifdef FEAT_CINDENT
9850 can_cindent = TRUE;
9851#endif
9852 }
9853 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009854 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855}
9856
9857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009858ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859{
9860 pos_T tpos;
9861
9862 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009863
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009864 if (mod_mask & MOD_MASK_CTRL)
9865 {
9866 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009867 if (first_tabpage->tp_next != NULL)
9868 {
9869 start_arrow(&curwin->w_cursor);
9870 goto_tabpage(-1);
9871 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009872 return;
9873 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009874
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 tpos = curwin->w_cursor;
9876 if (onepage(BACKWARD, 1L) == OK)
9877 {
9878 start_arrow(&tpos);
9879#ifdef FEAT_CINDENT
9880 can_cindent = TRUE;
9881#endif
9882 }
9883 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009884 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885}
9886
9887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009888ins_down(
9889 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890{
9891 pos_T tpos;
9892 linenr_T old_topline = curwin->w_topline;
9893#ifdef FEAT_DIFF
9894 int old_topfill = curwin->w_topfill;
9895#endif
9896
9897 undisplay_dollar();
9898 tpos = curwin->w_cursor;
9899 if (cursor_down(1L, TRUE) == OK)
9900 {
9901 if (startcol)
9902 coladvance(getvcol_nolist(&Insstart));
9903 if (old_topline != curwin->w_topline
9904#ifdef FEAT_DIFF
9905 || old_topfill != curwin->w_topfill
9906#endif
9907 )
9908 redraw_later(VALID);
9909 start_arrow(&tpos);
9910#ifdef FEAT_CINDENT
9911 can_cindent = TRUE;
9912#endif
9913 }
9914 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009915 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916}
9917
9918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009919ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920{
9921 pos_T tpos;
9922
9923 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009924
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009925 if (mod_mask & MOD_MASK_CTRL)
9926 {
9927 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009928 if (first_tabpage->tp_next != NULL)
9929 {
9930 start_arrow(&curwin->w_cursor);
9931 goto_tabpage(0);
9932 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009933 return;
9934 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009935
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 tpos = curwin->w_cursor;
9937 if (onepage(FORWARD, 1L) == OK)
9938 {
9939 start_arrow(&tpos);
9940#ifdef FEAT_CINDENT
9941 can_cindent = TRUE;
9942#endif
9943 }
9944 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009945 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946}
9947
9948#ifdef FEAT_DND
9949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009950ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951{
9952 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9953}
9954#endif
9955
9956/*
9957 * Handle TAB in Insert or Replace mode.
9958 * Return TRUE when the TAB needs to be inserted like a normal character.
9959 */
9960 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009961ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962{
9963 int ind;
9964 int i;
9965 int temp;
9966
9967 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9968 Insstart_blank_vcol = get_nolist_virtcol();
9969 if (echeck_abbr(TAB + ABBR_OFF))
9970 return FALSE;
9971
9972 ind = inindent(0);
9973#ifdef FEAT_CINDENT
9974 if (ind)
9975 can_cindent = FALSE;
9976#endif
9977
9978 /*
9979 * When nothing special, insert TAB like a normal character
9980 */
9981 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009982 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009983 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 return TRUE;
9985
9986 if (stop_arrow() == FAIL)
9987 return TRUE;
9988
9989 did_ai = FALSE;
9990#ifdef FEAT_SMARTINDENT
9991 did_si = FALSE;
9992 can_si = FALSE;
9993 can_si_back = FALSE;
9994#endif
9995 AppendToRedobuff((char_u *)"\t");
9996
9997 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009998 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009999 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10000 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 else /* otherwise use 'tabstop' */
10002 temp = (int)curbuf->b_p_ts;
10003 temp -= get_nolist_virtcol() % temp;
10004
10005 /*
10006 * Insert the first space with ins_char(). It will delete one char in
10007 * replace mode. Insert the rest with ins_str(); it will not delete any
10008 * chars. For VREPLACE mode, we use ins_char() for all characters.
10009 */
10010 ins_char(' ');
10011 while (--temp > 0)
10012 {
10013#ifdef FEAT_VREPLACE
10014 if (State & VREPLACE_FLAG)
10015 ins_char(' ');
10016 else
10017#endif
10018 {
10019 ins_str((char_u *)" ");
10020 if (State & REPLACE_FLAG) /* no char replaced */
10021 replace_push(NUL);
10022 }
10023 }
10024
10025 /*
10026 * When 'expandtab' not set: Replace spaces by TABs where possible.
10027 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010028 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 {
10030 char_u *ptr;
10031#ifdef FEAT_VREPLACE
10032 char_u *saved_line = NULL; /* init for GCC */
10033 pos_T pos;
10034#endif
10035 pos_T fpos;
10036 pos_T *cursor;
10037 colnr_T want_vcol, vcol;
10038 int change_col = -1;
10039 int save_list = curwin->w_p_list;
10040
10041 /*
10042 * Get the current line. For VREPLACE mode, don't make real changes
10043 * yet, just work on a copy of the line.
10044 */
10045#ifdef FEAT_VREPLACE
10046 if (State & VREPLACE_FLAG)
10047 {
10048 pos = curwin->w_cursor;
10049 cursor = &pos;
10050 saved_line = vim_strsave(ml_get_curline());
10051 if (saved_line == NULL)
10052 return FALSE;
10053 ptr = saved_line + pos.col;
10054 }
10055 else
10056#endif
10057 {
10058 ptr = ml_get_cursor();
10059 cursor = &curwin->w_cursor;
10060 }
10061
10062 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10063 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10064 curwin->w_p_list = FALSE;
10065
10066 /* Find first white before the cursor */
10067 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010068 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 {
10070 --fpos.col;
10071 --ptr;
10072 }
10073
10074 /* In Replace mode, don't change characters before the insert point. */
10075 if ((State & REPLACE_FLAG)
10076 && fpos.lnum == Insstart.lnum
10077 && fpos.col < Insstart.col)
10078 {
10079 ptr += Insstart.col - fpos.col;
10080 fpos.col = Insstart.col;
10081 }
10082
10083 /* compute virtual column numbers of first white and cursor */
10084 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10085 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10086
Bram Moolenaar597a4222014-06-25 14:39:50 +020010087 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10088 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010089 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010091 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 if (vcol + i > want_vcol)
10093 break;
10094 if (*ptr != TAB)
10095 {
10096 *ptr = TAB;
10097 if (change_col < 0)
10098 {
10099 change_col = fpos.col; /* Column of first change */
10100 /* May have to adjust Insstart */
10101 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10102 Insstart.col = fpos.col;
10103 }
10104 }
10105 ++fpos.col;
10106 ++ptr;
10107 vcol += i;
10108 }
10109
10110 if (change_col >= 0)
10111 {
10112 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010113 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114
10115 /* Skip over the spaces we need. */
10116 while (vcol < want_vcol && *ptr == ' ')
10117 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010118 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 ++ptr;
10120 ++repl_off;
10121 }
10122 if (vcol > want_vcol)
10123 {
10124 /* Must have a char with 'showbreak' just before it. */
10125 --ptr;
10126 --repl_off;
10127 }
10128 fpos.col += repl_off;
10129
10130 /* Delete following spaces. */
10131 i = cursor->col - fpos.col;
10132 if (i > 0)
10133 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010134 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 /* correct replace stack. */
10136 if ((State & REPLACE_FLAG)
10137#ifdef FEAT_VREPLACE
10138 && !(State & VREPLACE_FLAG)
10139#endif
10140 )
10141 for (temp = i; --temp >= 0; )
10142 replace_join(repl_off);
10143 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010144#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010145 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010146 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010147 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010148 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10149 (char_u *)"\t", 1);
10150 }
10151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 cursor->col -= i;
10153
10154#ifdef FEAT_VREPLACE
10155 /*
10156 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10157 * backspacing over the changed spacing and then inserting the new
10158 * spacing.
10159 */
10160 if (State & VREPLACE_FLAG)
10161 {
10162 /* Backspace from real cursor to change_col */
10163 backspace_until_column(change_col);
10164
10165 /* Insert each char in saved_line from changed_col to
10166 * ptr-cursor */
10167 ins_bytes_len(saved_line + change_col,
10168 cursor->col - change_col);
10169 }
10170#endif
10171 }
10172
10173#ifdef FEAT_VREPLACE
10174 if (State & VREPLACE_FLAG)
10175 vim_free(saved_line);
10176#endif
10177 curwin->w_p_list = save_list;
10178 }
10179
10180 return FALSE;
10181}
10182
10183/*
10184 * Handle CR or NL in insert mode.
10185 * Return TRUE when out of memory or can't undo.
10186 */
10187 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010188ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189{
10190 int i;
10191
10192 if (echeck_abbr(c + ABBR_OFF))
10193 return FALSE;
10194 if (stop_arrow() == FAIL)
10195 return TRUE;
10196 undisplay_dollar();
10197
10198 /*
10199 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10200 * character under the cursor. Only push a NUL on the replace stack,
10201 * nothing to put back when the NL is deleted.
10202 */
10203 if ((State & REPLACE_FLAG)
10204#ifdef FEAT_VREPLACE
10205 && !(State & VREPLACE_FLAG)
10206#endif
10207 )
10208 replace_push(NUL);
10209
10210 /*
10211 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10212 * replacing the next line, so we push all of the characters left on the
10213 * line onto the replace stack. This is not done here though, it is done
10214 * in open_line().
10215 */
10216
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010217#ifdef FEAT_VIRTUALEDIT
10218 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10219 * CTRL-O). */
10220 if (virtual_active() && curwin->w_cursor.coladd > 0)
10221 coladvance(getviscol());
10222#endif
10223
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224#ifdef FEAT_RIGHTLEFT
10225# ifdef FEAT_FKMAP
10226 if (p_altkeymap && p_fkmap)
10227 fkmap(NL);
10228# endif
10229 /* NL in reverse insert will always start in the end of
10230 * current line. */
10231 if (revins_on)
10232 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10233#endif
10234
10235 AppendToRedobuff(NL_STR);
10236 i = open_line(FORWARD,
10237#ifdef FEAT_COMMENTS
10238 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10239#endif
10240 0, old_indent);
10241 old_indent = 0;
10242#ifdef FEAT_CINDENT
10243 can_cindent = TRUE;
10244#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010245#ifdef FEAT_FOLDING
10246 /* When inserting a line the cursor line must never be in a closed fold. */
10247 foldOpenCursor();
10248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249
10250 return (!i);
10251}
10252
10253#ifdef FEAT_DIGRAPHS
10254/*
10255 * Handle digraph in insert mode.
10256 * Returns character still to be inserted, or NUL when nothing remaining to be
10257 * done.
10258 */
10259 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010260ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261{
10262 int c;
10263 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010264 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265
10266 pc_status = PC_STATUS_UNSET;
10267 if (redrawing() && !char_avail())
10268 {
10269 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010270 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271
10272 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010273 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274#ifdef FEAT_CMDL_INFO
10275 add_to_showcmd_c(Ctrl_K);
10276#endif
10277 }
10278
10279#ifdef USE_ON_FLY_SCROLL
10280 dont_scroll = TRUE; /* disallow scrolling here */
10281#endif
10282
10283 /* don't map the digraph chars. This also prevents the
10284 * mode message to be deleted when ESC is hit */
10285 ++no_mapping;
10286 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010287 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 --no_mapping;
10289 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010290 if (did_putchar)
10291 /* when the line fits in 'columns' the '?' is at the start of the next
10292 * line and will not be removed by the redraw */
10293 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010294
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 if (IS_SPECIAL(c) || mod_mask) /* special key */
10296 {
10297#ifdef FEAT_CMDL_INFO
10298 clear_showcmd();
10299#endif
10300 insert_special(c, TRUE, FALSE);
10301 return NUL;
10302 }
10303 if (c != ESC)
10304 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010305 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 if (redrawing() && !char_avail())
10307 {
10308 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010309 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310
10311 if (char2cells(c) == 1)
10312 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010313 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010315 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 }
10317#ifdef FEAT_CMDL_INFO
10318 add_to_showcmd_c(c);
10319#endif
10320 }
10321 ++no_mapping;
10322 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010323 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 --no_mapping;
10325 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010326 if (did_putchar)
10327 /* when the line fits in 'columns' the '?' is at the start of the
10328 * next line and will not be removed by a redraw */
10329 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 if (cc != ESC)
10331 {
10332 AppendToRedobuff((char_u *)CTRL_V_STR);
10333 c = getdigraph(c, cc, TRUE);
10334#ifdef FEAT_CMDL_INFO
10335 clear_showcmd();
10336#endif
10337 return c;
10338 }
10339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340#ifdef FEAT_CMDL_INFO
10341 clear_showcmd();
10342#endif
10343 return NUL;
10344}
10345#endif
10346
10347/*
10348 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10349 * Returns the char to be inserted, or NUL if none found.
10350 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010351 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010352ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353{
10354 int c;
10355 int temp;
10356 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010357 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358
10359 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10360 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010361 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 return NUL;
10363 }
10364
10365 /* try to advance to the cursor column */
10366 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010367 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 prev_ptr = ptr;
10369 validate_virtcol();
10370 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10371 {
10372 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010373 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 }
10375 if ((colnr_T)temp > curwin->w_virtcol)
10376 ptr = prev_ptr;
10377
10378#ifdef FEAT_MBYTE
10379 c = (*mb_ptr2char)(ptr);
10380#else
10381 c = *ptr;
10382#endif
10383 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010384 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 return c;
10386}
10387
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010388/*
10389 * CTRL-Y or CTRL-E typed in Insert mode.
10390 */
10391 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010392ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010393{
10394 int c = tc;
10395
10396#ifdef FEAT_INS_EXPAND
10397 if (ctrl_x_mode == CTRL_X_SCROLL)
10398 {
10399 if (c == Ctrl_Y)
10400 scrolldown_clamp();
10401 else
10402 scrollup_clamp();
10403 redraw_later(VALID);
10404 }
10405 else
10406#endif
10407 {
10408 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10409 if (c != NUL)
10410 {
10411 long tw_save;
10412
10413 /* The character must be taken literally, insert like it
10414 * was typed after a CTRL-V, and pretend 'textwidth'
10415 * wasn't set. Digits, 'o' and 'x' are special after a
10416 * CTRL-V, don't use it for these. */
10417 if (c < 256 && !isalnum(c))
10418 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10419 tw_save = curbuf->b_p_tw;
10420 curbuf->b_p_tw = -1;
10421 insert_special(c, TRUE, FALSE);
10422 curbuf->b_p_tw = tw_save;
10423#ifdef FEAT_RIGHTLEFT
10424 revins_chars++;
10425 revins_legal++;
10426#endif
10427 c = Ctrl_V; /* pretend CTRL-V is last character */
10428 auto_format(FALSE, TRUE);
10429 }
10430 }
10431 return c;
10432}
10433
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434#ifdef FEAT_SMARTINDENT
10435/*
10436 * Try to do some very smart auto-indenting.
10437 * Used when inserting a "normal" character.
10438 */
10439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010440ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441{
10442 pos_T *pos, old_pos;
10443 char_u *ptr;
10444 int i;
10445 int temp;
10446
10447 /*
10448 * do some very smart indenting when entering '{' or '}'
10449 */
10450 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10451 {
10452 /*
10453 * for '}' set indent equal to indent of line containing matching '{'
10454 */
10455 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10456 {
10457 old_pos = curwin->w_cursor;
10458 /*
10459 * If the matching '{' has a ')' immediately before it (ignoring
10460 * white-space), then line up with the start of the line
10461 * containing the matching '(' if there is one. This handles the
10462 * case where an "if (..\n..) {" statement continues over multiple
10463 * lines -- webb
10464 */
10465 ptr = ml_get(pos->lnum);
10466 i = pos->col;
10467 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010468 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 ;
10470 curwin->w_cursor.lnum = pos->lnum;
10471 curwin->w_cursor.col = i;
10472 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10473 curwin->w_cursor = *pos;
10474 i = get_indent();
10475 curwin->w_cursor = old_pos;
10476#ifdef FEAT_VREPLACE
10477 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010478 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 else
10480#endif
10481 (void)set_indent(i, SIN_CHANGED);
10482 }
10483 else if (curwin->w_cursor.col > 0)
10484 {
10485 /*
10486 * when inserting '{' after "O" reduce indent, but not
10487 * more than indent of previous line
10488 */
10489 temp = TRUE;
10490 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10491 {
10492 old_pos = curwin->w_cursor;
10493 i = get_indent();
10494 while (curwin->w_cursor.lnum > 1)
10495 {
10496 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10497
10498 /* ignore empty lines and lines starting with '#'. */
10499 if (*ptr != '#' && *ptr != NUL)
10500 break;
10501 }
10502 if (get_indent() >= i)
10503 temp = FALSE;
10504 curwin->w_cursor = old_pos;
10505 }
10506 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010507 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 }
10509 }
10510
10511 /*
10512 * set indent of '#' always to 0
10513 */
10514 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10515 {
10516 /* remember current indent for next line */
10517 old_indent = get_indent();
10518 (void)set_indent(0, SIN_CHANGED);
10519 }
10520
10521 /* Adjust ai_col, the char at this position can be deleted. */
10522 if (ai_col > curwin->w_cursor.col)
10523 ai_col = curwin->w_cursor.col;
10524}
10525#endif
10526
10527/*
10528 * Get the value that w_virtcol would have when 'list' is off.
10529 * Unless 'cpo' contains the 'L' flag.
10530 */
10531 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010532get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533{
10534 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10535 return getvcol_nolist(&curwin->w_cursor);
10536 validate_virtcol();
10537 return curwin->w_virtcol;
10538}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010539
10540#ifdef FEAT_AUTOCMD
10541/*
10542 * Handle the InsertCharPre autocommand.
10543 * "c" is the character that was typed.
10544 * Return a pointer to allocated memory with the replacement string.
10545 * Return NULL to continue inserting "c".
10546 */
10547 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010548do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010549{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010550 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010551 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010552
10553 /* Return quickly when there is nothing to do. */
10554 if (!has_insertcharpre())
10555 return NULL;
10556
Bram Moolenaar704984a2012-06-01 14:57:51 +020010557#ifdef FEAT_MBYTE
10558 if (has_mbyte)
10559 buf[(*mb_char2bytes)(c, buf)] = NUL;
10560 else
10561#endif
10562 {
10563 buf[0] = c;
10564 buf[1] = NUL;
10565 }
10566
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010567 /* Lock the text to avoid weird things from happening. */
10568 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010569 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010570
Bram Moolenaar704984a2012-06-01 14:57:51 +020010571 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010572 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010573 {
10574 /* Get the value of v:char. It may be empty or more than one
10575 * character. Only use it when changed, otherwise continue with the
10576 * original character to avoid breaking autoindent. */
10577 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10578 res = vim_strsave(get_vim_var_str(VV_CHAR));
10579 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010580
10581 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10582 --textlock;
10583
10584 return res;
10585}
10586#endif