blob: 25a123e8891ccb29e317258f4e2dccfb7d11bf78 [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 Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000100/* After using a cursor key <Enter> selects a match in the popup menu,
101 * otherwise it inserts a line break. */
102static int compl_enter_selects = FALSE;
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104/* When "compl_leader" is not NULL only matches that start with this string
105 * are used. */
106static char_u *compl_leader = NULL;
107
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000108static int compl_get_longest = FALSE; /* put longest common string
109 in compl_leader */
110
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200111static int compl_no_insert = FALSE; /* FALSE: select & insert
112 TRUE: noinsert */
113static int compl_no_select = FALSE; /* FALSE: select & insert
114 TRUE: noselect */
115
Bram Moolenaara6557602006-02-04 22:43:20 +0000116static int compl_used_match; /* Selected one of the matches. When
117 FALSE the match was edited or using
118 the longest common string. */
119
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000120static int compl_was_interrupted = FALSE; /* didn't finish finding
121 completions. */
122
123static int compl_restarting = FALSE; /* don't insert match */
124
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000125/* When the first completion is done "compl_started" is set. When it's
126 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000127static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000129/* Set when doing something for completion that may call edit() recursively,
130 * which is not allowed. */
131static int compl_busy = FALSE;
132
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static int compl_matches = 0;
134static char_u *compl_pattern = NULL;
135static int compl_direction = FORWARD;
136static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000137static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000138static pos_T compl_startpos;
139static colnr_T compl_col = 0; /* column where the text starts
140 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000141static char_u *compl_orig_text = NULL; /* text as it was before
142 * completion started */
143static int compl_cont_mode = 0;
144static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar82139082011-09-14 16:52:09 +0200146static int compl_opt_refresh_always = FALSE;
147
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100148static void ins_ctrl_x(void);
149static int has_compl_option(int dict_opt);
150static int ins_compl_accept_char(int c);
151static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
152static int ins_compl_equal(compl_T *match, char_u *str, int len);
153static void ins_compl_longest_match(compl_T *match);
154static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
155static int ins_compl_make_cyclic(void);
156static void ins_compl_upd_pum(void);
157static void ins_compl_del_pum(void);
158static int pum_wanted(void);
159static int pum_enough_matches(void);
160static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
161static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
162static char_u *find_line_end(char_u *ptr);
163static void ins_compl_free(void);
164static void ins_compl_clear(void);
165static int ins_compl_bs(void);
166static int ins_compl_need_restart(void);
167static void ins_compl_new_leader(void);
168static void ins_compl_addleader(int c);
169static int ins_compl_len(void);
170static void ins_compl_restart(void);
171static void ins_compl_set_original_text(char_u *str);
172static void ins_compl_addfrommatch(void);
173static int ins_compl_prep(int c);
174static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
175static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000176#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100177static void ins_compl_add_list(list_T *list);
178static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000179#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static int ins_compl_get_exp(pos_T *ini);
181static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200182static void ins_compl_insert(int in_compl_func);
183static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100184static int ins_compl_key2dir(int c);
185static int ins_compl_pum_key(int c);
186static int ins_compl_key2count(int c);
187static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100188static int ins_complete(int c, int enable_pum);
189static void show_pum(int save_w_wrow);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#endif /* FEAT_INS_EXPAND */
192
193#define BACKSPACE_CHAR 1
194#define BACKSPACE_WORD 2
195#define BACKSPACE_WORD_NOT_SPACE 3
196#define BACKSPACE_LINE 4
197
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100198static void ins_redraw(int ready);
199static void ins_ctrl_v(void);
200static void undisplay_dollar(void);
201static void insert_special(int, int, int);
202static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
203static void check_auto_format(int);
204static void redo_literal(int c);
205static void start_arrow(pos_T *end_insert_pos);
206static void start_arrow_with_change(pos_T *end_insert_pos, int change);
207static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000208#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void check_spell_redraw(void);
210static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000211static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
214static int echeck_abbr(int);
215static int replace_pop(void);
216static void replace_join(int off);
217static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void replace_flush(void);
222static void replace_do_bs(int limit_col);
223static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ins_reg(void);
228static void ins_ctrl_g(void);
229static void ins_ctrl_hat(void);
230static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100234static int ins_start_select(int c);
235static void ins_insert(int replaceState);
236static void ins_ctrl_o(void);
237static void ins_shift(int c, int lastc);
238static void ins_del(void);
239static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_mouse(int c);
242static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000244#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000246#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ins_left(int end_change);
248static void ins_home(int c);
249static void ins_end(int c);
250static void ins_s_left(void);
251static void ins_right(int end_change);
252static void ins_s_right(void);
253static void ins_up(int startcol);
254static void ins_pageup(void);
255static void ins_down(int startcol);
256static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static int ins_tab(void);
261static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100270#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274static colnr_T Insstart_textlen; /* length of line when insert started */
275static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100276static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
278static char_u *last_insert = NULL; /* the text of the previous insert,
279 K_SPECIAL and CSI are escaped */
280static int last_insert_skip; /* nr of chars in front of previous insert */
281static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000282static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284#ifdef FEAT_CINDENT
285static int can_cindent; /* may do cindenting on this line */
286#endif
287
288static int old_indent = 0; /* for ^^D command in insert mode */
289
290#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000291static int revins_on; /* reverse insert mode on */
292static int revins_chars; /* how much to skip after edit */
293static int revins_legal; /* was the last char 'legal'? */
294static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static int ins_need_undo; /* call u_save() before inserting a
298 char. Set when edit() is called.
299 after that arrow_used is used. */
300
301static int did_add_space = FALSE; /* auto_format() added an extra space
302 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200303static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
304 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
306/*
307 * edit(): Start inserting text.
308 *
309 * "cmdchar" can be:
310 * 'i' normal insert command
311 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100312 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 * 'R' replace command
314 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
315 * but still only one <CR> is inserted. The <Esc> is not used for redo.
316 * 'g' "gI" command.
317 * 'V' "gR" command for Virtual Replace mode.
318 * 'v' "gr" command for single character Virtual Replace mode.
319 *
320 * This function is not called recursively. For CTRL-O commands, it returns
321 * and lets the caller handle the Normal-mode command.
322 *
323 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
324 */
325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100326edit(
327 int cmdchar,
328 int startln, /* if set, insert at start of line */
329 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330{
331 int c = 0;
332 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100333 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000334 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 int i;
337 int did_backspace = TRUE; /* previous char was backspace */
338#ifdef FEAT_CINDENT
339 int line_is_white = FALSE; /* line is empty before insert */
340#endif
341 linenr_T old_topline = 0; /* topline before insertion */
342#ifdef FEAT_DIFF
343 int old_topfill = -1;
344#endif
345 int inserted_space = FALSE; /* just inserted a space */
346 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000347 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000349 /* Remember whether editing was restarted after CTRL-O. */
350 did_restart_edit = restart_edit;
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 /* sleep before redrawing, needed for "CTRL-O :" that results in an
353 * error message */
354 check_for_delay(TRUE);
355
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100356 /* set Insstart_orig to Insstart */
357 update_Insstart_orig = TRUE;
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef HAVE_SANDBOX
360 /* Don't allow inserting in the sandbox. */
361 if (sandbox != 0)
362 {
363 EMSG(_(e_sandbox));
364 return FALSE;
365 }
366#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000367 /* Don't allow changes in the buffer while editing the cmdline. The
368 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000369 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000370 {
371 EMSG(_(e_secure));
372 return FALSE;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000376 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000377 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000378 {
379 EMSG(_(e_secure));
380 return FALSE;
381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 ins_compl_clear(); /* clear stuff for CTRL-X mode */
383#endif
384
Bram Moolenaar843ee412004-06-30 16:16:41 +0000385#ifdef FEAT_AUTOCMD
386 /*
387 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
388 */
389 if (cmdchar != 'r' && cmdchar != 'v')
390 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100391 pos_T save_cursor = curwin->w_cursor;
392
Bram Moolenaar1e015462005-09-25 22:16:38 +0000393# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000394 if (cmdchar == 'R')
395 ptr = (char_u *)"r";
396 else if (cmdchar == 'V')
397 ptr = (char_u *)"v";
398 else
399 ptr = (char_u *)"i";
400 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200401 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000402# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000403 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100404
Bram Moolenaar097c9922013-05-19 21:15:15 +0200405 /* Make sure the cursor didn't move. Do call check_cursor_col() in
406 * case the text was modified. Since Insert mode was not started yet
407 * a call to check_cursor_col() may move the cursor, especially with
408 * the "A" command, thus set State to avoid that. Also check that the
409 * line number is still valid (lines may have been deleted).
410 * Do not restore if v:char was set to a non-empty string. */
411 if (!equalpos(curwin->w_cursor, save_cursor)
412# ifdef FEAT_EVAL
413 && *get_vim_var_str(VV_CHAR) == NUL
414# endif
415 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100416 {
417 int save_state = State;
418
419 curwin->w_cursor = save_cursor;
420 State = INSERT;
421 check_cursor_col();
422 State = save_state;
423 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000424 }
425#endif
426
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200427#ifdef FEAT_CONCEAL
428 /* Check if the cursor line needs redrawing before changing State. If
429 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200430 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200431#endif
432
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433#ifdef FEAT_MOUSE
434 /*
435 * When doing a paste with the middle mouse button, Insstart is set to
436 * where the paste started.
437 */
438 if (where_paste_started.lnum != 0)
439 Insstart = where_paste_started;
440 else
441#endif
442 {
443 Insstart = curwin->w_cursor;
444 if (startln)
445 Insstart.col = 0;
446 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000447 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 Insstart_blank_vcol = MAXCOL;
449 if (!did_ai)
450 ai_col = 0;
451
452 if (cmdchar != NUL && restart_edit == 0)
453 {
454 ResetRedobuff();
455 AppendNumberToRedobuff(count);
456#ifdef FEAT_VREPLACE
457 if (cmdchar == 'V' || cmdchar == 'v')
458 {
459 /* "gR" or "gr" command */
460 AppendCharToRedobuff('g');
461 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
462 }
463 else
464#endif
465 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100466 if (cmdchar == K_PS)
467 AppendCharToRedobuff('a');
468 else
469 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 if (cmdchar == 'g') /* "gI" command */
471 AppendCharToRedobuff('I');
472 else if (cmdchar == 'r') /* "r<CR>" command */
473 count = 1; /* insert only one <CR> */
474 }
475 }
476
477 if (cmdchar == 'R')
478 {
479#ifdef FEAT_FKMAP
480 if (p_fkmap && p_ri)
481 {
482 beep_flush();
483 EMSG(farsi_text_3); /* encoded in Farsi */
484 State = INSERT;
485 }
486 else
487#endif
488 State = REPLACE;
489 }
490#ifdef FEAT_VREPLACE
491 else if (cmdchar == 'V' || cmdchar == 'v')
492 {
493 State = VREPLACE;
494 replaceState = VREPLACE;
495 orig_line_count = curbuf->b_ml.ml_line_count;
496 vr_lines_changed = 1;
497 }
498#endif
499 else
500 State = INSERT;
501
502 stop_insert_mode = FALSE;
503
504 /*
505 * Need to recompute the cursor position, it might move when the cursor is
506 * on a TAB or special character.
507 */
508 curs_columns(TRUE);
509
510 /*
511 * Enable langmap or IME, indicated by 'iminsert'.
512 * Note that IME may enabled/disabled without us noticing here, thus the
513 * 'iminsert' value may not reflect what is actually used. It is updated
514 * when hitting <Esc>.
515 */
516 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
517 State |= LANGMAP;
518#ifdef USE_IM_CONTROL
519 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
520#endif
521
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522#ifdef FEAT_MOUSE
523 setmouse();
524#endif
525#ifdef FEAT_CMDL_INFO
526 clear_showcmd();
527#endif
528#ifdef FEAT_RIGHTLEFT
529 /* there is no reverse replace mode */
530 revins_on = (State == INSERT && p_ri);
531 if (revins_on)
532 undisplay_dollar();
533 revins_chars = 0;
534 revins_legal = 0;
535 revins_scol = -1;
536#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100537 if (!p_ek)
538 /* Disable bracketed paste mode, we won't recognize the escape
539 * sequences. */
540 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542 /*
543 * Handle restarting Insert mode.
544 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
545 * restart_edit non-zero, and something in the stuff buffer.
546 */
547 if (restart_edit != 0 && stuff_empty())
548 {
549#ifdef FEAT_MOUSE
550 /*
551 * After a paste we consider text typed to be part of the insert for
552 * the pasted text. You can backspace over the pasted text too.
553 */
554 if (where_paste_started.lnum)
555 arrow_used = FALSE;
556 else
557#endif
558 arrow_used = TRUE;
559 restart_edit = 0;
560
561 /*
562 * If the cursor was after the end-of-line before the CTRL-O and it is
563 * now at the end-of-line, put it after the end-of-line (this is not
564 * correct in very rare cases).
565 * Also do this if curswant is greater than the current virtual
566 * column. Eg after "^O$" or "^O80|".
567 */
568 validate_virtcol();
569 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000570 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 || curwin->w_curswant > curwin->w_virtcol)
572 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
573 {
574 if (ptr[1] == NUL)
575 ++curwin->w_cursor.col;
576#ifdef FEAT_MBYTE
577 else if (has_mbyte)
578 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000579 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (ptr[i] == NUL)
581 curwin->w_cursor.col += i;
582 }
583#endif
584 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000585 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 else
588 arrow_used = FALSE;
589
590 /* we are in insert mode now, don't need to start it anymore */
591 need_start_insertmode = FALSE;
592
593 /* Need to save the line for undo before inserting the first char. */
594 ins_need_undo = TRUE;
595
596#ifdef FEAT_MOUSE
597 where_paste_started.lnum = 0;
598#endif
599#ifdef FEAT_CINDENT
600 can_cindent = TRUE;
601#endif
602#ifdef FEAT_FOLDING
603 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
604 * restarting. */
605 if (!p_im && did_restart_edit == 0)
606 foldOpenCursor();
607#endif
608
609 /*
610 * If 'showmode' is set, show the current (insert/replace/..) mode.
611 * A warning message for changing a readonly file is given here, before
612 * actually changing anything. It's put after the mode, if any.
613 */
614 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000615 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 i = showmode();
617
618 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000619 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621#ifdef CURSOR_SHAPE
622 ui_cursor_shape(); /* may show different cursor shape */
623#endif
624#ifdef FEAT_DIGRAPHS
625 do_digraph(-1); /* clear digraphs */
626#endif
627
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000628 /*
629 * Get the current length of the redo buffer, those characters have to be
630 * skipped if we want to get to the inserted characters.
631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 ptr = get_inserted();
633 if (ptr == NULL)
634 new_insert_skip = 0;
635 else
636 {
637 new_insert_skip = (int)STRLEN(ptr);
638 vim_free(ptr);
639 }
640
641 old_indent = 0;
642
643 /*
644 * Main loop in Insert mode: repeat until Insert mode is left.
645 */
646 for (;;)
647 {
648#ifdef FEAT_RIGHTLEFT
649 if (!revins_legal)
650 revins_scol = -1; /* reset on illegal motions */
651 else
652 revins_legal = 0;
653#endif
654 if (arrow_used) /* don't repeat insert when arrow key used */
655 count = 0;
656
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100657 if (update_Insstart_orig)
658 Insstart_orig = Insstart;
659
Bram Moolenaar00672e12016-06-26 18:38:13 +0200660 if (stop_insert_mode
661#ifdef FEAT_INS_EXPAND
662 && !pum_visible()
663#endif
664 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 {
666 /* ":stopinsert" used or 'insertmode' reset */
667 count = 0;
668 goto doESCkey;
669 }
670
671 /* set curwin->w_curswant for next K_DOWN or K_UP */
672 if (!arrow_used)
673 curwin->w_set_curswant = TRUE;
674
675 /* If there is no typeahead may check for timestamps (e.g., for when a
676 * menu invoked a shell command). */
677 if (stuff_empty())
678 {
679 did_check_timestamps = FALSE;
680 if (need_check_timestamps)
681 check_timestamps(FALSE);
682 }
683
684 /*
685 * When emsg() was called msg_scroll will have been set.
686 */
687 msg_scroll = FALSE;
688
689#ifdef FEAT_GUI
690 /* When 'mousefocus' is set a mouse movement may have taken us to
691 * another window. "need_mouse_correct" may then be set because of an
692 * autocommand. */
693 if (need_mouse_correct)
694 gui_mouse_correct();
695#endif
696
697#ifdef FEAT_FOLDING
698 /* Open fold at the cursor line, according to 'foldopen'. */
699 if (fdo_flags & FDO_INSERT)
700 foldOpenCursor();
701 /* Close folds where the cursor isn't, according to 'foldclose' */
702 if (!char_avail())
703 foldCheckClose();
704#endif
705
706 /*
707 * If we inserted a character at the last position of the last line in
708 * the window, scroll the window one line up. This avoids an extra
709 * redraw.
710 * This is detected when the cursor column is smaller after inserting
711 * something.
712 * Don't do this when the topline changed already, it has
713 * already been adjusted (by insertchar() calling open_line())).
714 */
715 if (curbuf->b_mod_set
716 && curwin->w_p_wrap
717 && !did_backspace
718 && curwin->w_topline == old_topline
719#ifdef FEAT_DIFF
720 && curwin->w_topfill == old_topfill
721#endif
722 )
723 {
724 mincol = curwin->w_wcol;
725 validate_cursor_col();
726
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000727 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 && curwin->w_wrow == W_WINROW(curwin)
729 + curwin->w_height - 1 - p_so
730 && (curwin->w_cursor.lnum != curwin->w_topline
731#ifdef FEAT_DIFF
732 || curwin->w_topfill > 0
733#endif
734 ))
735 {
736#ifdef FEAT_DIFF
737 if (curwin->w_topfill > 0)
738 --curwin->w_topfill;
739 else
740#endif
741#ifdef FEAT_FOLDING
742 if (hasFolding(curwin->w_topline, NULL, &old_topline))
743 set_topline(curwin, old_topline + 1);
744 else
745#endif
746 set_topline(curwin, curwin->w_topline + 1);
747 }
748 }
749
750 /* May need to adjust w_topline to show the cursor. */
751 update_topline();
752
753 did_backspace = FALSE;
754
755 validate_cursor(); /* may set must_redraw */
756
757 /*
758 * Redraw the display when no characters are waiting.
759 * Also shows mode, ruler and positions cursor.
760 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000761 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
763#ifdef FEAT_SCROLLBIND
764 if (curwin->w_p_scb)
765 do_check_scrollbind(TRUE);
766#endif
767
Bram Moolenaar860cae12010-06-05 23:22:07 +0200768#ifdef FEAT_CURSORBIND
769 if (curwin->w_p_crb)
770 do_check_cursorbind();
771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 update_curswant();
773 old_topline = curwin->w_topline;
774#ifdef FEAT_DIFF
775 old_topfill = curwin->w_topfill;
776#endif
777
778#ifdef USE_ON_FLY_SCROLL
779 dont_scroll = FALSE; /* allow scrolling here */
780#endif
781
782 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000783 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100785 if (c != K_CURSORHOLD)
786 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200787
788 /* After using CTRL-G U the next cursor key will not break undo. */
789 if (dont_sync_undo == MAYBE)
790 dont_sync_undo = TRUE;
791 else
792 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100793 if (cmdchar == K_PS)
794 /* Got here from normal mode when bracketed paste started. */
795 c = K_PS;
796 else
797 do
798 {
799 c = safe_vgetc();
800 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000802#ifdef FEAT_AUTOCMD
803 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
804 did_cursorhold = TRUE;
805#endif
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#ifdef FEAT_RIGHTLEFT
808 if (p_hkmap && KeyTyped)
809 c = hkmap(c); /* Hebrew mode mapping */
810#endif
811#ifdef FEAT_FKMAP
812 if (p_fkmap && KeyTyped)
813 c = fkmap(c); /* Farsi mode mapping */
814#endif
815
816#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000817 /*
818 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000819 * and the cursor is still in the completed word. Only when there is
820 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000821 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000822 if (compl_started
823 && pum_wanted()
824 && curwin->w_cursor.col >= compl_col
825 && (compl_shown_match == NULL
826 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000827 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000828 /* BS: Delete one character from "compl_leader". */
829 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000830 && curwin->w_cursor.col > compl_col
831 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000832 continue;
833
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000834 /* When no match was selected or it was edited. */
835 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000836 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000837 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000838 * "compl_leader". Except when at the original match and
839 * there is nothing to add, CTRL-L works like CTRL-P then. */
840 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100841 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000842 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000843 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 {
845 ins_compl_addfrommatch();
846 continue;
847 }
848
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000849 /* A non-white character that fits in with the current
850 * completion: Add to "compl_leader". */
851 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000852 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100853#ifdef FEAT_AUTOCMD
854 /* Trigger InsertCharPre. */
855 char_u *str = do_insert_char_pre(c);
856 char_u *p;
857
858 if (str != NULL)
859 {
860 for (p = str; *p != NUL; mb_ptr_adv(p))
861 ins_compl_addleader(PTR2CHAR(p));
862 vim_free(str);
863 }
864 else
865#endif
866 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000867 continue;
868 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000869
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000870 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000871 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200872 if ((c == Ctrl_Y || (compl_enter_selects
873 && (c == CAR || c == K_KENTER || c == NL)))
874 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000875 {
876 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200877 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000878 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000879 }
880 }
881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
883 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000884 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000885 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000886 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#endif
888
Bram Moolenaar488c6512005-08-11 20:09:58 +0000889 /* CTRL-\ CTRL-N goes to Normal mode,
890 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
891 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 if (c == Ctrl_BSL)
893 {
894 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000895 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 ++no_mapping;
897 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000898 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 --no_mapping;
900 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000901 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000903 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 vungetc(c);
905 c = Ctrl_BSL;
906 }
907 else if (c == Ctrl_G && p_im)
908 continue;
909 else
910 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000911 if (c == Ctrl_O)
912 {
913 ins_ctrl_o();
914 ins_at_eol = FALSE; /* cursor keeps its column */
915 nomove = TRUE;
916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 count = 0;
918 goto doESCkey;
919 }
920 }
921
922#ifdef FEAT_DIGRAPHS
923 c = do_digraph(c);
924#endif
925
926#ifdef FEAT_INS_EXPAND
927 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
928 goto docomplete;
929#endif
930 if (c == Ctrl_V || c == Ctrl_Q)
931 {
932 ins_ctrl_v();
933 c = Ctrl_V; /* pretend CTRL-V is last typed character */
934 continue;
935 }
936
937#ifdef FEAT_CINDENT
938 if (cindent_on()
939# ifdef FEAT_INS_EXPAND
940 && ctrl_x_mode == 0
941# endif
942 )
943 {
944 /* A key name preceded by a bang means this key is not to be
945 * inserted. Skip ahead to the re-indenting below.
946 * A key name preceded by a star means that indenting has to be
947 * done before inserting the key. */
948 line_is_white = inindent(0);
949 if (in_cinkeys(c, '!', line_is_white))
950 goto force_cindent;
951 if (can_cindent && in_cinkeys(c, '*', line_is_white)
952 && stop_arrow() == OK)
953 do_c_expr_indent();
954 }
955#endif
956
957#ifdef FEAT_RIGHTLEFT
958 if (curwin->w_p_rl)
959 switch (c)
960 {
961 case K_LEFT: c = K_RIGHT; break;
962 case K_S_LEFT: c = K_S_RIGHT; break;
963 case K_C_LEFT: c = K_C_RIGHT; break;
964 case K_RIGHT: c = K_LEFT; break;
965 case K_S_RIGHT: c = K_S_LEFT; break;
966 case K_C_RIGHT: c = K_C_LEFT; break;
967 }
968#endif
969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 /*
971 * If 'keymodel' contains "startsel", may start selection. If it
972 * does, a CTRL-O and c will be stuffed, we need to get these
973 * characters.
974 */
975 if (ins_start_select(c))
976 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 /*
979 * The big switch to handle a character in insert mode.
980 */
981 switch (c)
982 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000983 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (echeck_abbr(ESC + ABBR_OFF))
985 break;
986 /*FALLTHROUGH*/
987
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#ifdef FEAT_CMDWIN
990 if (c == Ctrl_C && cmdwin_type != 0)
991 {
992 /* Close the cmdline window. */
993 cmdwin_result = K_IGNORE;
994 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000995 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 goto doESCkey;
997 }
998#endif
999
1000#ifdef UNIX
1001do_intr:
1002#endif
1003 /* when 'insertmode' set, and not halfway a mapping, don't leave
1004 * Insert mode */
1005 if (goto_im())
1006 {
1007 if (got_int)
1008 {
1009 (void)vgetc(); /* flush all buffers */
1010 got_int = FALSE;
1011 }
1012 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001013 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 break;
1015 }
1016doESCkey:
1017 /*
1018 * This is the ONLY return from edit()!
1019 */
1020 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1021 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001022 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 o_lnum = curwin->w_cursor.lnum;
1024
Bram Moolenaar488c6512005-08-11 20:09:58 +00001025 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001026 {
1027#ifdef FEAT_AUTOCMD
1028 if (cmdchar != 'r' && cmdchar != 'v')
1029 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1030 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001031 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 continue;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case Ctrl_Z: /* suspend when 'insertmode' set */
1038 if (!p_im)
1039 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001040 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001041 c = Ctrl_O;
1042 /*FALLTHROUGH*/
1043
1044 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001045#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001046 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001047 goto docomplete;
1048#endif
1049 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1050 break;
1051 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001052
1053#ifdef FEAT_VIRTUALEDIT
1054 /* don't move the cursor left when 'virtualedit' has "onemore". */
1055 if (ve_flags & VE_ONEMORE)
1056 {
1057 ins_at_eol = FALSE;
1058 nomove = TRUE;
1059 }
1060#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 count = 0;
1062 goto doESCkey;
1063
Bram Moolenaar572cb562005-08-05 21:35:02 +00001064 case K_INS: /* toggle insert/replace mode */
1065 case K_KINS:
1066 ins_insert(replaceState);
1067 break;
1068
1069 case K_SELECT: /* end of Select mode mapping - ignore */
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case K_HELP: /* Help key works like <ESC> <Help> */
1073 case K_F1:
1074 case K_XF1:
1075 stuffcharReadbuff(K_HELP);
1076 if (p_im)
1077 need_start_insertmode = TRUE;
1078 goto doESCkey;
1079
1080#ifdef FEAT_NETBEANS_INTG
1081 case K_F21: /* NetBeans command */
1082 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001083 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001084 --no_mapping;
1085 netbeans_keycommand(i);
1086 break;
1087#endif
1088
1089 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 case NUL:
1091 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 /* For ^@ the trailing ESC will end the insert, unless there is an
1093 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1095 && c != Ctrl_A && !p_im)
1096 goto doESCkey; /* quit insert mode */
1097 inserted_space = FALSE;
1098 break;
1099
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 ins_reg();
1102 auto_format(FALSE, TRUE);
1103 inserted_space = FALSE;
1104 break;
1105
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001106 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 ins_ctrl_g();
1108 break;
1109
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001110 case Ctrl_HAT: /* switch input mode and/or langmap */
1111 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 break;
1113
1114#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (!p_ari)
1117 goto normalchar;
1118 ins_ctrl_();
1119 break;
1120#endif
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1124 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1125 goto docomplete;
1126#endif
1127 /* FALLTHROUGH */
1128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001129 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130# ifdef FEAT_INS_EXPAND
1131 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1132 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001133 if (has_compl_option(FALSE))
1134 goto docomplete;
1135 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
1137# endif
1138 ins_shift(c, lastc);
1139 auto_format(FALSE, TRUE);
1140 inserted_space = FALSE;
1141 break;
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 case K_KDEL:
1145 ins_del();
1146 auto_format(FALSE, TRUE);
1147 break;
1148
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001149 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 case Ctrl_H:
1151 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1152 auto_format(FALSE, TRUE);
1153 break;
1154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1157 auto_format(FALSE, TRUE);
1158 break;
1159
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001160 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001161# ifdef FEAT_COMPL_FUNC
1162 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001164 goto docomplete;
1165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1167 auto_format(FALSE, TRUE);
1168 inserted_space = FALSE;
1169 break;
1170
1171#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001172 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 case K_LEFTMOUSE_NM:
1174 case K_LEFTDRAG:
1175 case K_LEFTRELEASE:
1176 case K_LEFTRELEASE_NM:
1177 case K_MIDDLEMOUSE:
1178 case K_MIDDLEDRAG:
1179 case K_MIDDLERELEASE:
1180 case K_RIGHTMOUSE:
1181 case K_RIGHTDRAG:
1182 case K_RIGHTRELEASE:
1183 case K_X1MOUSE:
1184 case K_X1DRAG:
1185 case K_X1RELEASE:
1186 case K_X2MOUSE:
1187 case K_X2DRAG:
1188 case K_X2RELEASE:
1189 ins_mouse(c);
1190 break;
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001193 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 break;
1195
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001196 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001197 ins_mousescroll(MSCR_UP);
1198 break;
1199
1200 case K_MOUSELEFT: /* Scroll wheel left */
1201 ins_mousescroll(MSCR_LEFT);
1202 break;
1203
1204 case K_MOUSERIGHT: /* Scroll wheel right */
1205 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 break;
1207#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001208 case K_PS:
1209 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1210 if (cmdchar == K_PS)
1211 /* invoked from normal mode, bail out */
1212 goto doESCkey;
1213 break;
1214 case K_PE:
1215 /* Got K_PE without K_PS, ignore. */
1216 break;
1217
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001218#ifdef FEAT_GUI_TABLINE
1219 case K_TABLINE:
1220 case K_TABMENU:
1221 ins_tabline(c);
1222 break;
1223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 break;
1227
Bram Moolenaar754b5602006-02-09 23:53:20 +00001228#ifdef FEAT_AUTOCMD
1229 case K_CURSORHOLD: /* Didn't type something for a while. */
1230 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1231 did_cursorhold = TRUE;
1232 break;
1233#endif
1234
Bram Moolenaar4770d092006-01-12 23:22:24 +00001235#ifdef FEAT_GUI_W32
1236 /* On Win32 ignore <M-F4>, we get it when closing the window was
1237 * cancelled. */
1238 case K_F4:
1239 if (mod_mask != MOD_MASK_ALT)
1240 goto normalchar;
1241 break;
1242#endif
1243
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244#ifdef FEAT_GUI
1245 case K_VER_SCROLLBAR:
1246 ins_scroll();
1247 break;
1248
1249 case K_HOR_SCROLLBAR:
1250 ins_horscroll();
1251 break;
1252#endif
1253
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001254 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 case K_S_HOME:
1257 case K_C_HOME:
1258 ins_home(c);
1259 break;
1260
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001261 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 case K_S_END:
1264 case K_C_END:
1265 ins_end(c);
1266 break;
1267
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001268 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001269 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1270 ins_s_left();
1271 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001272 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 break;
1274
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001275 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 case K_C_LEFT:
1277 ins_s_left();
1278 break;
1279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001280 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001281 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1282 ins_s_right();
1283 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001284 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 break;
1286
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001287 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 case K_C_RIGHT:
1289 ins_s_right();
1290 break;
1291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001292 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001293#ifdef FEAT_INS_EXPAND
1294 if (pum_visible())
1295 goto docomplete;
1296#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001297 if (mod_mask & MOD_MASK_SHIFT)
1298 ins_pageup();
1299 else
1300 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 break;
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 case K_PAGEUP:
1305 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001306#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001307 if (pum_visible())
1308 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 ins_pageup();
1311 break;
1312
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001313 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001314#ifdef FEAT_INS_EXPAND
1315 if (pum_visible())
1316 goto docomplete;
1317#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001318 if (mod_mask & MOD_MASK_SHIFT)
1319 ins_pagedown();
1320 else
1321 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 break;
1323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001324 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 case K_PAGEDOWN:
1326 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001327#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001328 if (pum_visible())
1329 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 ins_pagedown();
1332 break;
1333
1334#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001335 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 ins_drop();
1337 break;
1338#endif
1339
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001340 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 c = TAB;
1342 /* FALLTHROUGH */
1343
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1346 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1347 goto docomplete;
1348#endif
1349 inserted_space = FALSE;
1350 if (ins_tab())
1351 goto normalchar; /* insert TAB as a normal char */
1352 auto_format(FALSE, TRUE);
1353 break;
1354
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001355 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 c = CAR;
1357 /* FALLTHROUGH */
1358 case CAR:
1359 case NL:
1360#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1361 /* In a quickfix window a <CR> jumps to the error under the
1362 * cursor. */
1363 if (bt_quickfix(curbuf) && c == CAR)
1364 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001365 if (curwin->w_llist_ref == NULL) /* quickfix window */
1366 do_cmdline_cmd((char_u *)".cc");
1367 else /* location list window */
1368 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 break;
1370 }
1371#endif
1372#ifdef FEAT_CMDWIN
1373 if (cmdwin_type != 0)
1374 {
1375 /* Execute the command in the cmdline window. */
1376 cmdwin_result = CAR;
1377 goto doESCkey;
1378 }
1379#endif
1380 if (ins_eol(c) && !p_im)
1381 goto doESCkey; /* out of memory */
1382 auto_format(FALSE, FALSE);
1383 inserted_space = FALSE;
1384 break;
1385
Bram Moolenaar860cae12010-06-05 23:22:07 +02001386#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001387 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388# ifdef FEAT_INS_EXPAND
1389 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1390 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001391 if (has_compl_option(TRUE))
1392 goto docomplete;
1393 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395# endif
1396# ifdef FEAT_DIGRAPHS
1397 c = ins_digraph();
1398 if (c == NUL)
1399 break;
1400# endif
1401 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
1404#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001405 case Ctrl_X: /* Enter CTRL-X mode */
1406 ins_ctrl_x();
1407 break;
1408
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001409 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (ctrl_x_mode != CTRL_X_TAGS)
1411 goto normalchar;
1412 goto docomplete;
1413
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001414 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (ctrl_x_mode != CTRL_X_FILES)
1416 goto normalchar;
1417 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001418
1419 case 's': /* Spelling completion after ^X */
1420 case Ctrl_S:
1421 if (ctrl_x_mode != CTRL_X_SPELL)
1422 goto normalchar;
1423 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424#endif
1425
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001426 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427#ifdef FEAT_INS_EXPAND
1428 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1429#endif
1430 {
1431 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1432 if (p_im)
1433 {
1434 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1435 break;
1436 goto doESCkey;
1437 }
1438 goto normalchar;
1439 }
1440#ifdef FEAT_INS_EXPAND
1441 /* FALLTHROUGH */
1442
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001443 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 case Ctrl_N:
1445 /* if 'complete' is empty then plain ^P is no longer special,
1446 * but it is under other ^X modes */
1447 if (*curbuf->b_p_cpt == NUL
1448 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001449 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 goto normalchar;
1451
1452docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001453 compl_busy = TRUE;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001454 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001455 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001456 compl_cont_status = 0;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001457 disable_fold_update--;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001458 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 break;
1460#endif /* FEAT_INS_EXPAND */
1461
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001462 case Ctrl_Y: /* copy from previous line or scroll down */
1463 case Ctrl_E: /* copy from next line or scroll up */
1464 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 break;
1466
1467 default:
1468#ifdef UNIX
1469 if (c == intr_char) /* special interrupt char */
1470 goto do_intr;
1471#endif
1472
Bram Moolenaare659c952011-05-19 17:25:41 +02001473normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001475 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001477#ifdef FEAT_AUTOCMD
1478 if (!p_paste)
1479 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001480 /* Trigger InsertCharPre. */
1481 char_u *str = do_insert_char_pre(c);
1482 char_u *p;
1483
1484 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001485 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001486 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001487 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001488 /* Insert the new value of v:char literally. */
1489 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001490 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001491 c = PTR2CHAR(p);
1492 if (c == CAR || c == K_KENTER || c == NL)
1493 ins_eol(c);
1494 else
1495 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001496 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001497 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001498 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001499 vim_free(str);
1500 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001501 }
1502
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001503 /* If the new value is already inserted or an empty string
1504 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001505 if (c == NUL)
1506 break;
1507 }
1508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509#ifdef FEAT_SMARTINDENT
1510 /* Try to perform smart-indenting. */
1511 ins_try_si(c);
1512#endif
1513
1514 if (c == ' ')
1515 {
1516 inserted_space = TRUE;
1517#ifdef FEAT_CINDENT
1518 if (inindent(0))
1519 can_cindent = FALSE;
1520#endif
1521 if (Insstart_blank_vcol == MAXCOL
1522 && curwin->w_cursor.lnum == Insstart.lnum)
1523 Insstart_blank_vcol = get_nolist_virtcol();
1524 }
1525
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001526 /* Insert a normal character and check for abbreviations on a
1527 * special character. Let CTRL-] expand abbreviations without
1528 * inserting it. */
1529 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530#ifdef FEAT_MBYTE
1531 /* Add ABBR_OFF for characters above 0x100, this is
1532 * what check_abbr() expects. */
1533 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1534#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001535 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 insert_special(c, FALSE, FALSE);
1538#ifdef FEAT_RIGHTLEFT
1539 revins_legal++;
1540 revins_chars++;
1541#endif
1542 }
1543
1544 auto_format(FALSE, TRUE);
1545
1546#ifdef FEAT_FOLDING
1547 /* When inserting a character the cursor line must never be in a
1548 * closed fold. */
1549 foldOpenCursor();
1550#endif
1551 break;
1552 } /* end of switch (c) */
1553
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001554#ifdef FEAT_AUTOCMD
1555 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001556 if (c != K_CURSORHOLD
1557# ifdef FEAT_COMPL_FUNC
1558 /* but not in CTRL-X mode, a script can't restore the state */
1559 && ctrl_x_mode == 0
1560# endif
1561 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001562 did_cursorhold = FALSE;
1563#endif
1564
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 /* If the cursor was moved we didn't just insert a space */
1566 if (arrow_used)
1567 inserted_space = FALSE;
1568
1569#ifdef FEAT_CINDENT
1570 if (can_cindent && cindent_on()
1571# ifdef FEAT_INS_EXPAND
1572 && ctrl_x_mode == 0
1573# endif
1574 )
1575 {
1576force_cindent:
1577 /*
1578 * Indent now if a key was typed that is in 'cinkeys'.
1579 */
1580 if (in_cinkeys(c, ' ', line_is_white))
1581 {
1582 if (stop_arrow() == OK)
1583 /* re-indent the current line */
1584 do_c_expr_indent();
1585 }
1586 }
1587#endif /* FEAT_CINDENT */
1588
1589 } /* for (;;) */
1590 /* NOTREACHED */
1591}
1592
1593/*
1594 * Redraw for Insert mode.
1595 * This is postponed until getting the next character to make '$' in the 'cpo'
1596 * option work correctly.
1597 * Only redraw when there are no characters available. This speeds up
1598 * inserting sequences of characters (e.g., for CTRL-R).
1599 */
1600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601ins_redraw(
1602 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001604#ifdef FEAT_CONCEAL
1605 linenr_T conceal_old_cursor_line = 0;
1606 linenr_T conceal_new_cursor_line = 0;
1607 int conceal_update_lines = FALSE;
1608#endif
1609
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001610 if (char_avail())
1611 return;
1612
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001613#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001614 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1615 * visible, the command might delete it. */
1616 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001617# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001618 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001619# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001620# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001621 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001622# endif
1623# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001625# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001626 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001627# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001628 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001629# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001630# ifdef FEAT_INS_EXPAND
1631 && !pum_visible()
1632# endif
1633 )
1634 {
1635# ifdef FEAT_SYN_HL
1636 /* Need to update the screen first, to make sure syntax
1637 * highlighting is correct after making a change (e.g., inserting
1638 * a "(". The autocommand may also require a redraw, so it's done
1639 * again below, unfortunately. */
1640 if (syntax_present(curwin) && must_redraw)
1641 update_screen(0);
1642# endif
1643# ifdef FEAT_AUTOCMD
1644 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001645 {
1646 /* Make sure curswant is correct, an autocommand may call
1647 * getcurpos(). */
1648 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001649 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001650 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001651# endif
1652# ifdef FEAT_CONCEAL
1653 if (curwin->w_p_cole > 0)
1654 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001655# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001656 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001657# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001658 conceal_new_cursor_line = curwin->w_cursor.lnum;
1659 conceal_update_lines = TRUE;
1660 }
1661# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001662# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001663 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001664# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665 }
1666#endif
1667
1668#ifdef FEAT_AUTOCMD
1669 /* Trigger TextChangedI if b_changedtick differs. */
1670 if (ready && has_textchangedI()
Bram Moolenaar79518e22017-02-17 16:31:35 +01001671 && last_changedtick != *curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001672# ifdef FEAT_INS_EXPAND
1673 && !pum_visible()
1674# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001675 )
1676 {
1677 if (last_changedtick_buf == curbuf)
1678 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1679 last_changedtick_buf = curbuf;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001680 last_changedtick = *curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001682#endif
1683
1684 if (must_redraw)
1685 update_screen(0);
1686 else if (clear_cmdline || redraw_cmdline)
1687 showmode(); /* clear cmdline and show mode */
1688# if defined(FEAT_CONCEAL)
1689 if ((conceal_update_lines
1690 && (conceal_old_cursor_line != conceal_new_cursor_line
1691 || conceal_cursor_line(curwin)))
1692 || need_cursor_line_redraw)
1693 {
1694 if (conceal_old_cursor_line != conceal_new_cursor_line)
1695 update_single_line(curwin, conceal_old_cursor_line);
1696 update_single_line(curwin, conceal_new_cursor_line == 0
1697 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1698 curwin->w_valid &= ~VALID_CROW;
1699 }
1700# endif
1701 showruler(FALSE);
1702 setcursor();
1703 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704}
1705
1706/*
1707 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1708 */
1709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001710ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
1712 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001713 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001716 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
1718 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001721 did_putchar = TRUE;
1722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1724
1725#ifdef FEAT_CMDL_INFO
1726 add_to_showcmd_c(Ctrl_V);
1727#endif
1728
1729 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001730 if (did_putchar)
1731 /* when the line fits in 'columns' the '^' is at the start of the next
1732 * line and will not removed by the redraw */
1733 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734#ifdef FEAT_CMDL_INFO
1735 clear_showcmd();
1736#endif
1737 insert_special(c, FALSE, TRUE);
1738#ifdef FEAT_RIGHTLEFT
1739 revins_chars++;
1740 revins_legal++;
1741#endif
1742}
1743
1744/*
1745 * Put a character directly onto the screen. It's not stored in a buffer.
1746 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1747 */
1748static int pc_status;
1749#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1750#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1751#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1752#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754static int pc_attr;
1755static int pc_row;
1756static int pc_col;
1757
1758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001759edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 int attr;
1762
1763 if (ScreenLines != NULL)
1764 {
1765 update_topline(); /* just in case w_topline isn't valid */
1766 validate_cursor();
1767 if (highlight)
1768 attr = hl_attr(HLF_8);
1769 else
1770 attr = 0;
1771 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1772 pc_col = W_WINCOL(curwin);
1773#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1774 pc_status = PC_STATUS_UNSET;
1775#endif
1776#ifdef FEAT_RIGHTLEFT
1777 if (curwin->w_p_rl)
1778 {
1779 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1780# ifdef FEAT_MBYTE
1781 if (has_mbyte)
1782 {
1783 int fix_col = mb_fix_col(pc_col, pc_row);
1784
1785 if (fix_col != pc_col)
1786 {
1787 screen_putchar(' ', pc_row, fix_col, attr);
1788 --curwin->w_wcol;
1789 pc_status = PC_STATUS_RIGHT;
1790 }
1791 }
1792# endif
1793 }
1794 else
1795#endif
1796 {
1797 pc_col += curwin->w_wcol;
1798#ifdef FEAT_MBYTE
1799 if (mb_lefthalve(pc_row, pc_col))
1800 pc_status = PC_STATUS_LEFT;
1801#endif
1802 }
1803
1804 /* save the character to be able to put it back */
1805#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1806 if (pc_status == PC_STATUS_UNSET)
1807#endif
1808 {
1809 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1810 pc_status = PC_STATUS_SET;
1811 }
1812 screen_putchar(c, pc_row, pc_col, attr);
1813 }
1814}
1815
1816/*
1817 * Undo the previous edit_putchar().
1818 */
1819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001820edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821{
1822 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1823 {
1824#if defined(FEAT_MBYTE)
1825 if (pc_status == PC_STATUS_RIGHT)
1826 ++curwin->w_wcol;
1827 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1828 redrawWinline(curwin->w_cursor.lnum, FALSE);
1829 else
1830#endif
1831 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1832 }
1833}
1834
1835/*
1836 * Called when p_dollar is set: display a '$' at the end of the changed text
1837 * Only works when cursor is in the line that changes.
1838 */
1839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
1842 colnr_T save_col;
1843
1844 if (!redrawing())
1845 return;
1846
1847 cursor_off();
1848 save_col = curwin->w_cursor.col;
1849 curwin->w_cursor.col = col;
1850#ifdef FEAT_MBYTE
1851 if (has_mbyte)
1852 {
1853 char_u *p;
1854
1855 /* If on the last byte of a multi-byte move to the first byte. */
1856 p = ml_get_curline();
1857 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1858 }
1859#endif
1860 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1861 if (curwin->w_wcol < W_WIDTH(curwin))
1862 {
1863 edit_putchar('$', FALSE);
1864 dollar_vcol = curwin->w_virtcol;
1865 }
1866 curwin->w_cursor.col = save_col;
1867}
1868
1869/*
1870 * Call this function before moving the cursor from the normal insert position
1871 * in insert mode.
1872 */
1873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001874undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001876 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001878 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 redrawWinline(curwin->w_cursor.lnum, FALSE);
1880 }
1881}
1882
1883/*
1884 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1885 * Keep the cursor on the same character.
1886 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1887 * type == INDENT_DEC decrease indent (for CTRL-D)
1888 * type == INDENT_SET set indent to "amount"
1889 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1890 */
1891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001892change_indent(
1893 int type,
1894 int amount,
1895 int round,
1896 int replaced, /* replaced character, put on replace stack */
1897 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898{
1899 int vcol;
1900 int last_vcol;
1901 int insstart_less; /* reduction for Insstart.col */
1902 int new_cursor_col;
1903 int i;
1904 char_u *ptr;
1905 int save_p_list;
1906 int start_col;
1907 colnr_T vc;
1908#ifdef FEAT_VREPLACE
1909 colnr_T orig_col = 0; /* init for GCC */
1910 char_u *new_line, *orig_line = NULL; /* init for GCC */
1911
1912 /* VREPLACE mode needs to know what the line was like before changing */
1913 if (State & VREPLACE_FLAG)
1914 {
1915 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1916 orig_col = curwin->w_cursor.col;
1917 }
1918#endif
1919
1920 /* for the following tricks we don't want list mode */
1921 save_p_list = curwin->w_p_list;
1922 curwin->w_p_list = FALSE;
1923 vc = getvcol_nolist(&curwin->w_cursor);
1924 vcol = vc;
1925
1926 /*
1927 * For Replace mode we need to fix the replace stack later, which is only
1928 * possible when the cursor is in the indent. Remember the number of
1929 * characters before the cursor if it's possible.
1930 */
1931 start_col = curwin->w_cursor.col;
1932
1933 /* determine offset from first non-blank */
1934 new_cursor_col = curwin->w_cursor.col;
1935 beginline(BL_WHITE);
1936 new_cursor_col -= curwin->w_cursor.col;
1937
1938 insstart_less = curwin->w_cursor.col;
1939
1940 /*
1941 * If the cursor is in the indent, compute how many screen columns the
1942 * cursor is to the left of the first non-blank.
1943 */
1944 if (new_cursor_col < 0)
1945 vcol = get_indent() - vcol;
1946
1947 if (new_cursor_col > 0) /* can't fix replace stack */
1948 start_col = -1;
1949
1950 /*
1951 * Set the new indent. The cursor will be put on the first non-blank.
1952 */
1953 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001954 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 else
1956 {
1957#ifdef FEAT_VREPLACE
1958 int save_State = State;
1959
1960 /* Avoid being called recursively. */
1961 if (State & VREPLACE_FLAG)
1962 State = INSERT;
1963#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001964 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965#ifdef FEAT_VREPLACE
1966 State = save_State;
1967#endif
1968 }
1969 insstart_less -= curwin->w_cursor.col;
1970
1971 /*
1972 * Try to put cursor on same character.
1973 * If the cursor is at or after the first non-blank in the line,
1974 * compute the cursor column relative to the column of the first
1975 * non-blank character.
1976 * If we are not in insert mode, leave the cursor on the first non-blank.
1977 * If the cursor is before the first non-blank, position it relative
1978 * to the first non-blank, counted in screen columns.
1979 */
1980 if (new_cursor_col >= 0)
1981 {
1982 /*
1983 * When changing the indent while the cursor is touching it, reset
1984 * Insstart_col to 0.
1985 */
1986 if (new_cursor_col == 0)
1987 insstart_less = MAXCOL;
1988 new_cursor_col += curwin->w_cursor.col;
1989 }
1990 else if (!(State & INSERT))
1991 new_cursor_col = curwin->w_cursor.col;
1992 else
1993 {
1994 /*
1995 * Compute the screen column where the cursor should be.
1996 */
1997 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001998 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000 /*
2001 * Advance the cursor until we reach the right screen column.
2002 */
2003 vcol = last_vcol = 0;
2004 new_cursor_col = -1;
2005 ptr = ml_get_curline();
2006 while (vcol <= (int)curwin->w_virtcol)
2007 {
2008 last_vcol = vcol;
2009#ifdef FEAT_MBYTE
2010 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002011 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 else
2013#endif
2014 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002015 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
2017 vcol = last_vcol;
2018
2019 /*
2020 * May need to insert spaces to be able to position the cursor on
2021 * the right screen column.
2022 */
2023 if (vcol != (int)curwin->w_virtcol)
2024 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002025 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002027 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 if (ptr != NULL)
2029 {
2030 new_cursor_col += i;
2031 ptr[i] = NUL;
2032 while (--i >= 0)
2033 ptr[i] = ' ';
2034 ins_str(ptr);
2035 vim_free(ptr);
2036 }
2037 }
2038
2039 /*
2040 * When changing the indent while the cursor is in it, reset
2041 * Insstart_col to 0.
2042 */
2043 insstart_less = MAXCOL;
2044 }
2045
2046 curwin->w_p_list = save_p_list;
2047
2048 if (new_cursor_col <= 0)
2049 curwin->w_cursor.col = 0;
2050 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002051 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 curwin->w_set_curswant = TRUE;
2053 changed_cline_bef_curs();
2054
2055 /*
2056 * May have to adjust the start of the insert.
2057 */
2058 if (State & INSERT)
2059 {
2060 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2061 {
2062 if ((int)Insstart.col <= insstart_less)
2063 Insstart.col = 0;
2064 else
2065 Insstart.col -= insstart_less;
2066 }
2067 if ((int)ai_col <= insstart_less)
2068 ai_col = 0;
2069 else
2070 ai_col -= insstart_less;
2071 }
2072
2073 /*
2074 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2075 * If the number of characters before the cursor decreased, need to pop a
2076 * few characters from the replace stack.
2077 * If the number of characters before the cursor increased, need to push a
2078 * few NULs onto the replace stack.
2079 */
2080 if (REPLACE_NORMAL(State) && start_col >= 0)
2081 {
2082 while (start_col > (int)curwin->w_cursor.col)
2083 {
2084 replace_join(0); /* remove a NUL from the replace stack */
2085 --start_col;
2086 }
2087 while (start_col < (int)curwin->w_cursor.col || replaced)
2088 {
2089 replace_push(NUL);
2090 if (replaced)
2091 {
2092 replace_push(replaced);
2093 replaced = NUL;
2094 }
2095 ++start_col;
2096 }
2097 }
2098
2099#ifdef FEAT_VREPLACE
2100 /*
2101 * For VREPLACE mode, we also have to fix the replace stack. In this case
2102 * it is always possible because we backspace over the whole line and then
2103 * put it back again the way we wanted it.
2104 */
2105 if (State & VREPLACE_FLAG)
2106 {
2107 /* If orig_line didn't allocate, just return. At least we did the job,
2108 * even if you can't backspace. */
2109 if (orig_line == NULL)
2110 return;
2111
2112 /* Save new line */
2113 new_line = vim_strsave(ml_get_curline());
2114 if (new_line == NULL)
2115 return;
2116
2117 /* We only put back the new line up to the cursor */
2118 new_line[curwin->w_cursor.col] = NUL;
2119
2120 /* Put back original line */
2121 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2122 curwin->w_cursor.col = orig_col;
2123
2124 /* Backspace from cursor to start of line */
2125 backspace_until_column(0);
2126
2127 /* Insert new stuff into line again */
2128 ins_bytes(new_line);
2129
2130 vim_free(new_line);
2131 }
2132#endif
2133}
2134
2135/*
2136 * Truncate the space at the end of a line. This is to be used only in an
2137 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2138 * modes.
2139 */
2140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002141truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142{
2143 int i;
2144
2145 /* find start of trailing white space */
2146 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2147 {
2148 if (State & REPLACE_FLAG)
2149 replace_join(0); /* remove a NUL from the replace stack */
2150 }
2151 line[i + 1] = NUL;
2152}
2153
2154#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2155 || defined(FEAT_COMMENTS) || defined(PROTO)
2156/*
2157 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2158 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002159 * Will attempt not to go before "col" even when there is a composing
2160 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 */
2162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002163backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164{
2165 while ((int)curwin->w_cursor.col > col)
2166 {
2167 curwin->w_cursor.col--;
2168 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002169 replace_do_bs(col);
2170 else if (!del_char_after_col(col))
2171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
2173}
2174#endif
2175
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002176/*
2177 * Like del_char(), but make sure not to go before column "limit_col".
2178 * Only matters when there are composing characters.
2179 * Return TRUE when something was deleted.
2180 */
2181 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002182del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002183{
2184#ifdef FEAT_MBYTE
2185 if (enc_utf8 && limit_col >= 0)
2186 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002187 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002188
2189 /* Make sure the cursor is at the start of a character, but
2190 * skip forward again when going too far back because of a
2191 * composing character. */
2192 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002193 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002194 {
2195 int l = utf_ptr2len(ml_get_cursor());
2196
2197 if (l == 0) /* end of line */
2198 break;
2199 curwin->w_cursor.col += l;
2200 }
2201 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2202 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002203 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002204 }
2205 else
2206#endif
2207 (void)del_char(FALSE);
2208 return TRUE;
2209}
2210
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2212/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002213 * CTRL-X pressed in Insert mode.
2214 */
2215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002216ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002217{
2218 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2219 * CTRL-V works like CTRL-N */
2220 if (ctrl_x_mode != CTRL_X_CMDLINE)
2221 {
2222 /* if the next ^X<> won't ADD nothing, then reset
2223 * compl_cont_status */
2224 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002225 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002226 else
2227 compl_cont_status = 0;
2228 /* We're not sure which CTRL-X mode it will be yet */
2229 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2230 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2231 edit_submode_pre = NULL;
2232 showmode();
2233 }
2234}
2235
2236/*
2237 * Return TRUE if the 'dict' or 'tsr' option can be used.
2238 */
2239 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002240has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002241{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002242 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002243# ifdef FEAT_SPELL
2244 && !curwin->w_p_spell
2245# endif
2246 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002247 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2248 {
2249 ctrl_x_mode = 0;
2250 edit_submode = NULL;
2251 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2252 : (char_u *)_("'thesaurus' option is empty"),
2253 hl_attr(HLF_E));
2254 if (emsg_silent == 0)
2255 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002256 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002257 setcursor();
2258 out_flush();
2259 ui_delay(2000L, FALSE);
2260 }
2261 return FALSE;
2262 }
2263 return TRUE;
2264}
2265
2266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2268 * This depends on the current mode.
2269 */
2270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002271vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
2273 /* Always allow ^R - let it's results then be checked */
2274 if (c == Ctrl_R)
2275 return TRUE;
2276
Bram Moolenaare3226be2005-12-18 22:10:00 +00002277 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002278 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002279 return TRUE;
2280
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 switch (ctrl_x_mode)
2282 {
2283 case 0: /* Not in any CTRL-X mode */
2284 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2285 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002286 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2288 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2289 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002290 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002291 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 case CTRL_X_SCROLL:
2293 return (c == Ctrl_Y || c == Ctrl_E);
2294 case CTRL_X_WHOLE_LINE:
2295 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2296 case CTRL_X_FILES:
2297 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2298 case CTRL_X_DICTIONARY:
2299 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2300 case CTRL_X_THESAURUS:
2301 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2302 case CTRL_X_TAGS:
2303 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2304#ifdef FEAT_FIND_ID
2305 case CTRL_X_PATH_PATTERNS:
2306 return (c == Ctrl_P || c == Ctrl_N);
2307 case CTRL_X_PATH_DEFINES:
2308 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2309#endif
2310 case CTRL_X_CMDLINE:
2311 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2312 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002313#ifdef FEAT_COMPL_FUNC
2314 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002315 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002316 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002317 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002318#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002319 case CTRL_X_SPELL:
2320 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002321 case CTRL_X_EVAL:
2322 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002324 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 return FALSE;
2326}
2327
2328/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002329 * Return TRUE when character "c" is part of the item currently being
2330 * completed. Used to decide whether to abandon complete mode when the menu
2331 * is visible.
2332 */
2333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002334ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002335{
2336 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2337 /* When expanding an identifier only accept identifier chars. */
2338 return vim_isIDc(c);
2339
2340 switch (ctrl_x_mode)
2341 {
2342 case CTRL_X_FILES:
2343 /* When expanding file name only accept file name chars. But not
2344 * path separators, so that "proto/<Tab>" expands files in
2345 * "proto", not "proto/" as a whole */
2346 return vim_isfilec(c) && !vim_ispathsep(c);
2347
2348 case CTRL_X_CMDLINE:
2349 case CTRL_X_OMNI:
2350 /* Command line and Omni completion can work with just about any
2351 * printable character, but do stop at white space. */
2352 return vim_isprintc(c) && !vim_iswhite(c);
2353
2354 case CTRL_X_WHOLE_LINE:
2355 /* For while line completion a space can be part of the line. */
2356 return vim_isprintc(c);
2357 }
2358 return vim_iswordc(c);
2359}
2360
2361/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002362 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002364 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 * the rest of the word to be in -- webb
2366 */
2367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002368ins_compl_add_infercase(
2369 char_u *str,
2370 int len,
2371 int icase,
2372 char_u *fname,
2373 int dir,
2374 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002376 char_u *p;
2377 int i, c;
2378 int actual_len; /* Take multi-byte characters */
2379 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002380 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002381 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 int has_lower = FALSE;
2383 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002385 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002387 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaara2993e12007-08-12 14:38:46 +00002389 /* Find actual length of completion. */
2390#ifdef FEAT_MBYTE
2391 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002393 p = str;
2394 actual_len = 0;
2395 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002397 mb_ptr_adv(p);
2398 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002401 else
2402#endif
2403 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
Bram Moolenaara2993e12007-08-12 14:38:46 +00002405 /* Find actual length of original text. */
2406#ifdef FEAT_MBYTE
2407 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002409 p = compl_orig_text;
2410 actual_compl_length = 0;
2411 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002413 mb_ptr_adv(p);
2414 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002417 else
2418#endif
2419 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002421 /* "actual_len" may be smaller than "actual_compl_length" when using
2422 * thesaurus, only use the minimum when comparing. */
2423 min_len = actual_len < actual_compl_length
2424 ? actual_len : actual_compl_length;
2425
Bram Moolenaara2993e12007-08-12 14:38:46 +00002426 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002427 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002428 if (wca != NULL)
2429 {
2430 p = str;
2431 for (i = 0; i < actual_len; ++i)
2432#ifdef FEAT_MBYTE
2433 if (has_mbyte)
2434 wca[i] = mb_ptr2char_adv(&p);
2435 else
2436#endif
2437 wca[i] = *(p++);
2438
2439 /* Rule 1: Were any chars converted to lower? */
2440 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002441 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002442 {
2443#ifdef FEAT_MBYTE
2444 if (has_mbyte)
2445 c = mb_ptr2char_adv(&p);
2446 else
2447#endif
2448 c = *(p++);
2449 if (MB_ISLOWER(c))
2450 {
2451 has_lower = TRUE;
2452 if (MB_ISUPPER(wca[i]))
2453 {
2454 /* Rule 1 is satisfied. */
2455 for (i = actual_compl_length; i < actual_len; ++i)
2456 wca[i] = MB_TOLOWER(wca[i]);
2457 break;
2458 }
2459 }
2460 }
2461
2462 /*
2463 * Rule 2: No lower case, 2nd consecutive letter converted to
2464 * upper case.
2465 */
2466 if (!has_lower)
2467 {
2468 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002469 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002470 {
2471#ifdef FEAT_MBYTE
2472 if (has_mbyte)
2473 c = mb_ptr2char_adv(&p);
2474 else
2475#endif
2476 c = *(p++);
2477 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2478 {
2479 /* Rule 2 is satisfied. */
2480 for (i = actual_compl_length; i < actual_len; ++i)
2481 wca[i] = MB_TOUPPER(wca[i]);
2482 break;
2483 }
2484 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2485 }
2486 }
2487
2488 /* Copy the original case of the part we typed. */
2489 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002490 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002491 {
2492#ifdef FEAT_MBYTE
2493 if (has_mbyte)
2494 c = mb_ptr2char_adv(&p);
2495 else
2496#endif
2497 c = *(p++);
2498 if (MB_ISLOWER(c))
2499 wca[i] = MB_TOLOWER(wca[i]);
2500 else if (MB_ISUPPER(c))
2501 wca[i] = MB_TOUPPER(wca[i]);
2502 }
2503
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002504 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002505 * Generate encoding specific output from wide character array.
2506 * Multi-byte characters can occupy up to five bytes more than
2507 * ASCII characters, and we also need one byte for NUL, so stay
2508 * six bytes away from the edge of IObuff.
2509 */
2510 p = IObuff;
2511 i = 0;
2512 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2513#ifdef FEAT_MBYTE
2514 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002515 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002516 else
2517#endif
2518 *(p++) = wca[i++];
2519 *p = NUL;
2520
2521 vim_free(wca);
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002524 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2525 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002527 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528}
2529
2530/*
2531 * Add a match to the list of matches.
2532 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002533 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002537ins_compl_add(
2538 char_u *str,
2539 int len,
2540 int icase,
2541 char_u *fname,
2542 char_u **cptext, /* extra text for popup menu or NULL */
2543 int cdir,
2544 int flags,
2545 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002547 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002548 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
2550 ui_breakcheck();
2551 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002552 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (len < 0)
2554 len = (int)STRLEN(str);
2555
2556 /*
2557 * If the same match is already present, don't add it.
2558 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002559 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002561 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 do
2563 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002564 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002565 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002566 && match->cp_str[len] == NUL)
2567 return NOTDONE;
2568 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002569 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002572 /* Remove any popup menu before changing the list of matches. */
2573 ins_compl_del_pum();
2574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 /*
2576 * Allocate a new match structure.
2577 * Copy the values to the new match structure.
2578 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002579 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002581 return FAIL;
2582 match->cp_number = -1;
2583 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002584 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002585 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
2587 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002590 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002593 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2595 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002596 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002597 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002598 && compl_curr_match->cp_fname != NULL
2599 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002600 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002601 else if (fname != NULL)
2602 {
2603 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002604 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002607 match->cp_fname = NULL;
2608 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002609
2610 if (cptext != NULL)
2611 {
2612 int i;
2613
2614 for (i = 0; i < CPT_COUNT; ++i)
2615 if (cptext[i] != NULL && *cptext[i] != NUL)
2616 match->cp_text[i] = vim_strsave(cptext[i]);
2617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 /*
2620 * Link the new match structure in the list of matches.
2621 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002622 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002623 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 else if (dir == FORWARD)
2625 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002626 match->cp_next = compl_curr_match->cp_next;
2627 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
2629 else /* BACKWARD */
2630 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002631 match->cp_next = compl_curr_match;
2632 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002634 if (match->cp_next)
2635 match->cp_next->cp_prev = match;
2636 if (match->cp_prev)
2637 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002639 compl_first_match = match;
2640 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002642 /*
2643 * Find the longest common string if still doing that.
2644 */
2645 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2646 ins_compl_longest_match(match);
2647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 return OK;
2649}
2650
2651/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002652 * Return TRUE if "str[len]" matches with match->cp_str, considering
2653 * match->cp_icase.
2654 */
2655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002656ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002657{
2658 if (match->cp_icase)
2659 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2660 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2661}
2662
2663/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002664 * Reduce the longest common string for match "match".
2665 */
2666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002667ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002668{
2669 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002670 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002671 int had_match;
2672
2673 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002674 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002675 /* First match, use it as a whole. */
2676 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002677 if (compl_leader != NULL)
2678 {
2679 had_match = (curwin->w_cursor.col > compl_col);
2680 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002681 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002682 ins_redraw(FALSE);
2683
2684 /* When the match isn't there (to avoid matching itself) remove it
2685 * again after redrawing. */
2686 if (!had_match)
2687 ins_compl_delete();
2688 compl_used_match = FALSE;
2689 }
2690 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002691 else
2692 {
2693 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002694 p = compl_leader;
2695 s = match->cp_str;
2696 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002697 {
2698#ifdef FEAT_MBYTE
2699 if (has_mbyte)
2700 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002701 c1 = mb_ptr2char(p);
2702 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002703 }
2704 else
2705#endif
2706 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002707 c1 = *p;
2708 c2 = *s;
2709 }
2710 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2711 : (c1 != c2))
2712 break;
2713#ifdef FEAT_MBYTE
2714 if (has_mbyte)
2715 {
2716 mb_ptr_adv(p);
2717 mb_ptr_adv(s);
2718 }
2719 else
2720#endif
2721 {
2722 ++p;
2723 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002724 }
2725 }
2726
2727 if (*p != NUL)
2728 {
2729 /* Leader was shortened, need to change the inserted text. */
2730 *p = NUL;
2731 had_match = (curwin->w_cursor.col > compl_col);
2732 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002733 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002734 ins_redraw(FALSE);
2735
2736 /* When the match isn't there (to avoid matching itself) remove it
2737 * again after redrawing. */
2738 if (!had_match)
2739 ins_compl_delete();
2740 }
2741
2742 compl_used_match = FALSE;
2743 }
2744}
2745
2746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 * Add an array of matches to the list of matches.
2748 * Frees matches[].
2749 */
2750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002751ins_compl_add_matches(
2752 int num_matches,
2753 char_u **matches,
2754 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 int i;
2757 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002758 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
Bram Moolenaar572cb562005-08-05 21:35:02 +00002760 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002761 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002762 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002764 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 FreeWild(num_matches, matches);
2766}
2767
2768/* Make the completion list cyclic.
2769 * Return the number of matches (excluding the original).
2770 */
2771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002772ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002774 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 int count = 0;
2776
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002777 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
2779 /*
2780 * Find the end of the list.
2781 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002782 match = compl_first_match;
2783 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002784 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002786 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 ++count;
2788 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002789 match->cp_next = compl_first_match;
2790 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
2792 return count;
2793}
2794
Bram Moolenaara94bc432006-03-10 21:42:59 +00002795/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002796 * Set variables that store noselect and noinsert behavior from the
2797 * 'completeopt' value.
2798 */
2799 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002800completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002801{
2802 compl_no_insert = FALSE;
2803 compl_no_select = FALSE;
2804 if (strstr((char *)p_cot, "noselect") != NULL)
2805 compl_no_select = TRUE;
2806 if (strstr((char *)p_cot, "noinsert") != NULL)
2807 compl_no_insert = TRUE;
2808}
2809
2810/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002811 * Start completion for the complete() function.
2812 * "startcol" is where the matched text starts (1 is first column).
2813 * "list" is the list of matches.
2814 */
2815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002816set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002817{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002818 int save_w_wrow = curwin->w_wrow;
2819
Bram Moolenaara94bc432006-03-10 21:42:59 +00002820 /* If already doing completions stop it. */
2821 if (ctrl_x_mode != 0)
2822 ins_compl_prep(' ');
2823 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002824 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002825
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002826 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002827 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002828 startcol = curwin->w_cursor.col;
2829 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002830 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002831 /* compl_pattern doesn't need to be set */
2832 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2833 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002834 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002835 return;
2836
Bram Moolenaare4214502015-03-05 18:08:43 +01002837 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002838
2839 ins_compl_add_list(list);
2840 compl_matches = ins_compl_make_cyclic();
2841 compl_started = TRUE;
2842 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002843 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002844
2845 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002846 if (compl_no_insert || compl_no_select)
2847 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002848 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002849 if (compl_no_select)
2850 /* Down/Up has no real effect. */
2851 ins_complete(K_UP, FALSE);
2852 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002853 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002854 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002855 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002856
2857 /* Lazily show the popup menu, unless we got interrupted. */
2858 if (!compl_interrupted)
2859 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002860 out_flush();
2861}
2862
2863
Bram Moolenaar9372a112005-12-06 19:59:18 +00002864/* "compl_match_array" points the currently displayed list of entries in the
2865 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002866static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002867static int compl_match_arraysize;
2868
2869/*
2870 * Update the screen and when there is any scrolling remove the popup menu.
2871 */
2872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002873ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002874{
2875 int h;
2876
2877 if (compl_match_array != NULL)
2878 {
2879 h = curwin->w_cline_height;
2880 update_screen(0);
2881 if (h != curwin->w_cline_height)
2882 ins_compl_del_pum();
2883 }
2884}
2885
2886/*
2887 * Remove any popup menu.
2888 */
2889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002890ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002891{
2892 if (compl_match_array != NULL)
2893 {
2894 pum_undisplay();
2895 vim_free(compl_match_array);
2896 compl_match_array = NULL;
2897 }
2898}
2899
2900/*
2901 * Return TRUE if the popup menu should be displayed.
2902 */
2903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002904pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002905{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002906 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002907 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002908 return FALSE;
2909
2910 /* The display looks bad on a B&W display. */
2911 if (t_colors < 8
2912#ifdef FEAT_GUI
2913 && !gui.in_use
2914#endif
2915 )
2916 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002917 return TRUE;
2918}
2919
2920/*
2921 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002922 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002923 */
2924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002925pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002926{
2927 compl_T *compl;
2928 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002929
2930 /* Don't display the popup menu if there are no matches or there is only
2931 * one (ignoring the original text). */
2932 compl = compl_first_match;
2933 i = 0;
2934 do
2935 {
2936 if (compl == NULL
2937 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2938 break;
2939 compl = compl->cp_next;
2940 } while (compl != compl_first_match);
2941
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002942 if (strstr((char *)p_cot, "menuone") != NULL)
2943 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002944 return (i >= 2);
2945}
2946
2947/*
2948 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002949 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002950 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002951 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002952ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002953{
2954 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002955 compl_T *shown_compl = NULL;
2956 int did_find_shown_match = FALSE;
2957 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002958 int i;
2959 int cur = -1;
2960 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002961 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002962
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002963 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002964 return;
2965
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002966#if defined(FEAT_EVAL)
2967 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2968 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2969#endif
2970
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002971 /* Update the screen before drawing the popup menu over it. */
2972 update_screen(0);
2973
2974 if (compl_match_array == NULL)
2975 {
2976 /* Need to build the popup menu list. */
2977 compl_match_arraysize = 0;
2978 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002979 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002980 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002981 do
2982 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002983 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2984 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002985 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002986 ++compl_match_arraysize;
2987 compl = compl->cp_next;
2988 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002989 if (compl_match_arraysize == 0)
2990 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002991 compl_match_array = (pumitem_T *)alloc_clear(
2992 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002993 * compl_match_arraysize));
2994 if (compl_match_array != NULL)
2995 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002996 /* If the current match is the original text don't find the first
2997 * match after it, don't highlight anything. */
2998 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2999 shown_match_ok = TRUE;
3000
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003001 i = 0;
3002 compl = compl_first_match;
3003 do
3004 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003005 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3006 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003007 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003008 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003009 if (!shown_match_ok)
3010 {
3011 if (compl == compl_shown_match || did_find_shown_match)
3012 {
3013 /* This item is the shown match or this is the
3014 * first displayed item after the shown match. */
3015 compl_shown_match = compl;
3016 did_find_shown_match = TRUE;
3017 shown_match_ok = TRUE;
3018 }
3019 else
3020 /* Remember this displayed match for when the
3021 * shown match is just below it. */
3022 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003023 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003024 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003025
3026 if (compl->cp_text[CPT_ABBR] != NULL)
3027 compl_match_array[i].pum_text =
3028 compl->cp_text[CPT_ABBR];
3029 else
3030 compl_match_array[i].pum_text = compl->cp_str;
3031 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3032 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3033 if (compl->cp_text[CPT_MENU] != NULL)
3034 compl_match_array[i++].pum_extra =
3035 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003036 else
3037 compl_match_array[i++].pum_extra = compl->cp_fname;
3038 }
3039
3040 if (compl == compl_shown_match)
3041 {
3042 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003043
3044 /* When the original text is the shown match don't set
3045 * compl_shown_match. */
3046 if (compl->cp_flags & ORIGINAL_TEXT)
3047 shown_match_ok = TRUE;
3048
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003049 if (!shown_match_ok && shown_compl != NULL)
3050 {
3051 /* The shown match isn't displayed, set it to the
3052 * previously displayed match. */
3053 compl_shown_match = shown_compl;
3054 shown_match_ok = TRUE;
3055 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003056 }
3057 compl = compl->cp_next;
3058 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003059
3060 if (!shown_match_ok) /* no displayed match at all */
3061 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003062 }
3063 }
3064 else
3065 {
3066 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003067 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003068 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3069 || compl_match_array[i].pum_text
3070 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003071 {
3072 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003073 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003074 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003075 }
3076
3077 if (compl_match_array != NULL)
3078 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003079 /* In Replace mode when a $ is displayed at the end of the line only
3080 * part of the screen would be updated. We do need to redraw here. */
3081 dollar_vcol = -1;
3082
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003083 /* Compute the screen column of the start of the completed text.
3084 * Use the cursor to get all wrapping and other settings right. */
3085 col = curwin->w_cursor.col;
3086 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003087 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003088 curwin->w_cursor.col = col;
3089 }
3090}
3091
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092#define DICT_FIRST (1) /* use just first element in "dict" */
3093#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003094
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003096 * Add any identifiers that match the given pattern in the list of dictionary
3097 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 */
3099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003100ins_compl_dictionaries(
3101 char_u *dict_start,
3102 char_u *pat,
3103 int flags, /* DICT_FIRST and/or DICT_EXACT */
3104 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003106 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 char_u *ptr;
3108 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 char_u **files;
3111 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003113 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
Bram Moolenaar0b238792006-03-02 22:49:12 +00003115 if (*dict == NUL)
3116 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003117#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003118 /* When 'dictionary' is empty and spell checking is enabled use
3119 * "spell". */
3120 if (!thesaurus && curwin->w_p_spell)
3121 dict = (char_u *)"spell";
3122 else
3123#endif
3124 return;
3125 }
3126
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003128 if (buf == NULL)
3129 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003130 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 /* If 'infercase' is set, don't use 'smartcase' here */
3133 save_p_scs = p_scs;
3134 if (curbuf->b_p_inf)
3135 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003136
3137 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3138 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003139 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003140 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003141 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003142 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003143 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003144
3145 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003146 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003147 len = STRLEN(pat_esc) + 10;
3148 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003149 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003150 {
3151 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003152 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003153 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003154 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003155 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3156 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003157 vim_free(ptr);
3158 }
3159 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003160 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003161 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003162 if (regmatch.regprog == NULL)
3163 goto theend;
3164 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3167 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003168 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
3170 /* copy one dictionary file name into buf */
3171 if (flags == DICT_EXACT)
3172 {
3173 count = 1;
3174 files = &dict;
3175 }
3176 else
3177 {
3178 /* Expand wildcards in the dictionary name, but do not allow
3179 * backticks (for security, the 'dict' option may have been set in
3180 * a modeline). */
3181 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003182# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003183 if (!thesaurus && STRCMP(buf, "spell") == 0)
3184 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003185 else
3186# endif
3187 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 || expand_wildcards(1, &buf, &count, &files,
3189 EW_FILE|EW_SILENT) != OK)
3190 count = 0;
3191 }
3192
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003193# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003194 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003196 /* Complete from active spelling. Skip "\<" in the pattern, we
3197 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003198 if (pat[0] == '\\' && pat[1] == '<')
3199 ptr = pat + 2;
3200 else
3201 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003202 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003204 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003205# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003206 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003207 {
3208 ins_compl_files(count, files, thesaurus, flags,
3209 &regmatch, buf, &dir);
3210 if (flags != DICT_EXACT)
3211 FreeWild(count, files);
3212 }
3213 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 break;
3215 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003216
3217theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003219 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 vim_free(buf);
3221}
3222
Bram Moolenaar0b238792006-03-02 22:49:12 +00003223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003224ins_compl_files(
3225 int count,
3226 char_u **files,
3227 int thesaurus,
3228 int flags,
3229 regmatch_T *regmatch,
3230 char_u *buf,
3231 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003232{
3233 char_u *ptr;
3234 int i;
3235 FILE *fp;
3236 int add_r;
3237
3238 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3239 {
3240 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3241 if (flags != DICT_EXACT)
3242 {
3243 vim_snprintf((char *)IObuff, IOSIZE,
3244 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003245 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003246 }
3247
3248 if (fp != NULL)
3249 {
3250 /*
3251 * Read dictionary file line by line.
3252 * Check each line for a match.
3253 */
3254 while (!got_int && !compl_interrupted
3255 && !vim_fgets(buf, LSIZE, fp))
3256 {
3257 ptr = buf;
3258 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3259 {
3260 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003261 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003262 ptr = find_line_end(ptr);
3263 else
3264 ptr = find_word_end(ptr);
3265 add_r = ins_compl_add_infercase(regmatch->startp[0],
3266 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003267 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003268 if (thesaurus)
3269 {
3270 char_u *wstart;
3271
3272 /*
3273 * Add the other matches on the line
3274 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003275 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003276 while (!got_int)
3277 {
3278 /* Find start of the next word. Skip white
3279 * space and punctuation. */
3280 ptr = find_word_start(ptr);
3281 if (*ptr == NUL || *ptr == NL)
3282 break;
3283 wstart = ptr;
3284
Bram Moolenaara2993e12007-08-12 14:38:46 +00003285 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003286#ifdef FEAT_MBYTE
3287 if (has_mbyte)
3288 /* Japanese words may have characters in
3289 * different classes, only separate words
3290 * with single-byte non-word characters. */
3291 while (*ptr != NUL)
3292 {
3293 int l = (*mb_ptr2len)(ptr);
3294
3295 if (l < 2 && !vim_iswordc(*ptr))
3296 break;
3297 ptr += l;
3298 }
3299 else
3300#endif
3301 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003302
3303 /* Add the word. Skip the regexp match. */
3304 if (wstart != regmatch->startp[0])
3305 add_r = ins_compl_add_infercase(wstart,
3306 (int)(ptr - wstart),
3307 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003308 }
3309 }
3310 if (add_r == OK)
3311 /* if dir was BACKWARD then honor it just once */
3312 *dir = FORWARD;
3313 else if (add_r == FAIL)
3314 break;
3315 /* avoid expensive call to vim_regexec() when at end
3316 * of line */
3317 if (*ptr == '\n' || got_int)
3318 break;
3319 }
3320 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003321 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003322 }
3323 fclose(fp);
3324 }
3325 }
3326}
3327
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328/*
3329 * Find the start of the next word.
3330 * Returns a pointer to the first char of the word. Also stops at a NUL.
3331 */
3332 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003333find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335#ifdef FEAT_MBYTE
3336 if (has_mbyte)
3337 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003338 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 else
3340#endif
3341 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3342 ++ptr;
3343 return ptr;
3344}
3345
3346/*
3347 * Find the end of the word. Assumes it starts inside a word.
3348 * Returns a pointer to just after the word.
3349 */
3350 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003351find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353#ifdef FEAT_MBYTE
3354 int start_class;
3355
3356 if (has_mbyte)
3357 {
3358 start_class = mb_get_class(ptr);
3359 if (start_class > 1)
3360 while (*ptr != NUL)
3361 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003362 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (mb_get_class(ptr) != start_class)
3364 break;
3365 }
3366 }
3367 else
3368#endif
3369 while (vim_iswordc(*ptr))
3370 ++ptr;
3371 return ptr;
3372}
3373
3374/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003375 * Find the end of the line, omitting CR and NL at the end.
3376 * Returns a pointer to just after the line.
3377 */
3378 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003379find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003380{
3381 char_u *s;
3382
3383 s = ptr + STRLEN(ptr);
3384 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3385 --s;
3386 return s;
3387}
3388
3389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 * Free the list of completions
3391 */
3392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003393ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003395 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003396 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003398 vim_free(compl_pattern);
3399 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003400 vim_free(compl_leader);
3401 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003403 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003405
3406 ins_compl_del_pum();
3407 pum_clear();
3408
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003409 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 do
3411 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003412 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003413 compl_curr_match = compl_curr_match->cp_next;
3414 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003416 if (match->cp_flags & FREE_FNAME)
3417 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003418 for (i = 0; i < CPT_COUNT; ++i)
3419 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003421 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3422 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003423 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424}
3425
3426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003427ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003429 compl_cont_status = 0;
3430 compl_started = FALSE;
3431 compl_matches = 0;
3432 vim_free(compl_pattern);
3433 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003434 vim_free(compl_leader);
3435 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003437 vim_free(compl_orig_text);
3438 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003439 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003440 /* clear v:completed_item */
3441 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442}
3443
3444/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003445 * Return TRUE when Insert completion is active.
3446 */
3447 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003448ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003449{
3450 return compl_started;
3451}
3452
3453/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003454 * Delete one character before the cursor and show the subset of the matches
3455 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003456 * Returns the character to be used, NUL if the work is done and another char
3457 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003458 */
3459 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003460ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003461{
3462 char_u *line;
3463 char_u *p;
3464
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003465 line = ml_get_curline();
3466 p = line + curwin->w_cursor.col;
3467 mb_ptr_back(line, p);
3468
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003469 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003470 * allow the word to be deleted, we won't match everything.
3471 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003472 if ((int)(p - line) - (int)compl_col < 0
3473 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003474 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3475 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3476 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003477 return K_BS;
3478
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003479 /* Deleted more than what was used to find matches or didn't finish
3480 * finding all matches: need to look for matches all over again. */
3481 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003482 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003483 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003484
Bram Moolenaara6557602006-02-04 22:43:20 +00003485 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003486 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003487 if (compl_leader != NULL)
3488 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003489 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003490 if (compl_shown_match != NULL)
3491 /* Make sure current match is not a hidden item. */
3492 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003493 return NUL;
3494 }
3495 return K_BS;
3496}
Bram Moolenaara6557602006-02-04 22:43:20 +00003497
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003498/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003499 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3500 * be called.
3501 */
3502 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003503ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003504{
3505 /* Return TRUE if we didn't complete finding matches or when the
3506 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3507 return compl_was_interrupted
3508 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3509 && compl_opt_refresh_always);
3510}
3511
3512/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003513 * Called after changing "compl_leader".
3514 * Show the popup menu with a different set of matches.
3515 * May also search for matches again if the previous search was interrupted.
3516 */
3517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003518ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003519{
3520 ins_compl_del_pum();
3521 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003522 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003523 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003524
3525 if (compl_started)
3526 ins_compl_set_original_text(compl_leader);
3527 else
3528 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003529#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003530 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003531#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003532 /*
3533 * Matches were cleared, need to search for them now. First display
3534 * the changed text before the cursor. Set "compl_restarting" to
3535 * avoid that the first match is inserted.
3536 */
3537 update_screen(0);
3538#ifdef FEAT_GUI
3539 if (gui.in_use)
3540 {
3541 /* Show the cursor after the match, not after the redrawn text. */
3542 setcursor();
3543 out_flush();
3544 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003545 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003546#endif
3547 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003548 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003549 compl_cont_status = 0;
3550 compl_restarting = FALSE;
3551 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003552
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003553 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003554
3555 /* Show the popup menu with a different set of matches. */
3556 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003557
3558 /* Don't let Enter select the original text when there is no popup menu. */
3559 if (compl_match_array == NULL)
3560 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003561}
3562
3563/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003564 * Return the length of the completion, from the completion start column to
3565 * the cursor column. Making sure it never goes below zero.
3566 */
3567 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003568ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003569{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003570 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003571
3572 if (off < 0)
3573 return 0;
3574 return off;
3575}
3576
3577/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003578 * Append one character to the match leader. May reduce the number of
3579 * matches.
3580 */
3581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003582ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003583{
3584#ifdef FEAT_MBYTE
3585 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003586#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003587
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003588 if (stop_arrow() == FAIL)
3589 return;
3590#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003591 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3592 {
3593 char_u buf[MB_MAXBYTES + 1];
3594
3595 (*mb_char2bytes)(c, buf);
3596 buf[cc] = NUL;
3597 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003598 if (compl_opt_refresh_always)
3599 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003600 }
3601 else
3602#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003603 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003604 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003605 if (compl_opt_refresh_always)
3606 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003607 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003608
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003609 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003610 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003611 ins_compl_restart();
3612
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003613 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003614 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003615 * break redo. */
3616 if (!compl_opt_refresh_always)
3617 {
3618 vim_free(compl_leader);
3619 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003620 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003621 if (compl_leader != NULL)
3622 ins_compl_new_leader();
3623 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003624}
3625
3626/*
3627 * Setup for finding completions again without leaving CTRL-X mode. Used when
3628 * BS or a key was typed while still searching for matches.
3629 */
3630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003631ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003632{
3633 ins_compl_free();
3634 compl_started = FALSE;
3635 compl_matches = 0;
3636 compl_cont_status = 0;
3637 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003638}
3639
3640/*
3641 * Set the first match, the original text.
3642 */
3643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003644ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003645{
3646 char_u *p;
3647
3648 /* Replace the original text entry. */
3649 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3650 {
3651 p = vim_strsave(str);
3652 if (p != NULL)
3653 {
3654 vim_free(compl_first_match->cp_str);
3655 compl_first_match->cp_str = p;
3656 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003657 }
3658}
3659
3660/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003661 * Append one character to the match leader. May reduce the number of
3662 * matches.
3663 */
3664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003665ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003666{
3667 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003668 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003669 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003670 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003671
3672 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003673 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003674 {
3675 /* When still at the original match use the first entry that matches
3676 * the leader. */
3677 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3678 {
3679 p = NULL;
3680 for (cp = compl_shown_match->cp_next; cp != NULL
3681 && cp != compl_first_match; cp = cp->cp_next)
3682 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003683 if (compl_leader == NULL
3684 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003685 (int)STRLEN(compl_leader)))
3686 {
3687 p = cp->cp_str;
3688 break;
3689 }
3690 }
3691 if (p == NULL || (int)STRLEN(p) <= len)
3692 return;
3693 }
3694 else
3695 return;
3696 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003697 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003698 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003699 ins_compl_addleader(c);
3700}
3701
3702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003704 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003705 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003707 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003708ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
3710 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003712 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
3714 /* Forget any previous 'special' messages if this is actually
3715 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3716 */
3717 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3718 edit_submode_extra = NULL;
3719
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003720 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003721 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3722 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003723 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003725 /* Set "compl_get_longest" when finding the first matches. */
3726 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3727 || (ctrl_x_mode == 0 && !compl_started))
3728 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003729 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003730 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003731
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003732 }
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3735 {
3736 /*
3737 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3738 * it will be yet. Now we decide.
3739 */
3740 switch (c)
3741 {
3742 case Ctrl_E:
3743 case Ctrl_Y:
3744 ctrl_x_mode = CTRL_X_SCROLL;
3745 if (!(State & REPLACE_FLAG))
3746 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3747 else
3748 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3749 edit_submode_pre = NULL;
3750 showmode();
3751 break;
3752 case Ctrl_L:
3753 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3754 break;
3755 case Ctrl_F:
3756 ctrl_x_mode = CTRL_X_FILES;
3757 break;
3758 case Ctrl_K:
3759 ctrl_x_mode = CTRL_X_DICTIONARY;
3760 break;
3761 case Ctrl_R:
3762 /* Simply allow ^R to happen without affecting ^X mode */
3763 break;
3764 case Ctrl_T:
3765 ctrl_x_mode = CTRL_X_THESAURUS;
3766 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003767#ifdef FEAT_COMPL_FUNC
3768 case Ctrl_U:
3769 ctrl_x_mode = CTRL_X_FUNCTION;
3770 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003771 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003772 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003773 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003774#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003775 case 's':
3776 case Ctrl_S:
3777 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003778#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003779 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003780 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003781 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003782#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003783 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 case Ctrl_RSB:
3785 ctrl_x_mode = CTRL_X_TAGS;
3786 break;
3787#ifdef FEAT_FIND_ID
3788 case Ctrl_I:
3789 case K_S_TAB:
3790 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3791 break;
3792 case Ctrl_D:
3793 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3794 break;
3795#endif
3796 case Ctrl_V:
3797 case Ctrl_Q:
3798 ctrl_x_mode = CTRL_X_CMDLINE;
3799 break;
3800 case Ctrl_P:
3801 case Ctrl_N:
3802 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3803 * just started ^X mode, or there were enough ^X's to cancel
3804 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3805 * do normal expansion when interrupting a different mode (say
3806 * ^X^F^X^P or ^P^X^X^P, see below)
3807 * nothing changes if interrupting mode 0, (eg, the flag
3808 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003809 if (!(compl_cont_status & CONT_INTRPT))
3810 compl_cont_status |= CONT_LOCAL;
3811 else if (compl_cont_mode != 0)
3812 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 /* FALLTHROUGH */
3814 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003815 /* If we have typed at least 2 ^X's... for modes != 0, we set
3816 * compl_cont_status = 0 (eg, as if we had just started ^X
3817 * mode).
3818 * For mode 0, we set "compl_cont_mode" to an impossible
3819 * value, in both cases ^X^X can be used to restart the same
3820 * mode (avoiding ADDING mode).
3821 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3822 * 'complete' and local ^P expansions respectively.
3823 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3824 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (c == Ctrl_X)
3826 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003827 if (compl_cont_mode != 0)
3828 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003830 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832 ctrl_x_mode = 0;
3833 edit_submode = NULL;
3834 showmode();
3835 break;
3836 }
3837 }
3838 else if (ctrl_x_mode != 0)
3839 {
3840 /* We're already in CTRL-X mode, do we stay in it? */
3841 if (!vim_is_ctrl_x_key(c))
3842 {
3843 if (ctrl_x_mode == CTRL_X_SCROLL)
3844 ctrl_x_mode = 0;
3845 else
3846 ctrl_x_mode = CTRL_X_FINISHED;
3847 edit_submode = NULL;
3848 }
3849 showmode();
3850 }
3851
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003852 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
3854 /* Show error message from attempted keyword completion (probably
3855 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003856 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003858 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3859 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 || ctrl_x_mode == CTRL_X_FINISHED)
3861 {
3862 /* Get here when we have finished typing a sequence of ^N and
3863 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003864 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003865 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
3867 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003868 * If any of the original typed text has been changed, eg when
3869 * ignorecase is set, we must add back-spaces to the redo
3870 * buffer. We add as few as necessary to delete just the part
3871 * of the original text that has changed.
3872 * When using the longest match, edited the match or used
3873 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003875 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3876 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003877 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003878 ptr = NULL;
3879 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881
3882#ifdef FEAT_CINDENT
3883 want_cindent = (can_cindent && cindent_on());
3884#endif
3885 /*
3886 * When completing whole lines: fix indent for 'cindent'.
3887 * Otherwise, break line if it's too long.
3888 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003889 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
3891#ifdef FEAT_CINDENT
3892 /* re-indent the current line */
3893 if (want_cindent)
3894 {
3895 do_c_expr_indent();
3896 want_cindent = FALSE; /* don't do it again */
3897 }
3898#endif
3899 }
3900 else
3901 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003902 int prev_col = curwin->w_cursor.col;
3903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003905 if (prev_col > 0)
3906 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003907 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003908 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003910 if (prev_col > 0
3911 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3912 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003915 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003916 * the selection without inserting anything. When
3917 * compl_enter_selects is set the Enter key does the same. */
3918 if ((c == Ctrl_Y || (compl_enter_selects
3919 && (c == CAR || c == K_KENTER || c == NL)))
3920 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003921 retval = TRUE;
3922
Bram Moolenaar47247282016-08-02 22:36:02 +02003923 /* CTRL-E means completion is Ended, go back to the typed text.
3924 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003925 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003926 {
3927 ins_compl_delete();
3928 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003929 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003930 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003931 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003932 retval = TRUE;
3933 }
3934
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003935 auto_format(FALSE, TRUE);
3936
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003938 compl_started = FALSE;
3939 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003940 if (!shortmess(SHM_COMPLETIONMENU))
3941 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003943 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 if (edit_submode != NULL)
3945 {
3946 edit_submode = NULL;
3947 showmode();
3948 }
3949
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003950#ifdef FEAT_CMDWIN
3951 if (c == Ctrl_C && cmdwin_type != 0)
3952 /* Avoid the popup menu remains displayed when leaving the
3953 * command line window. */
3954 update_screen(0);
3955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956#ifdef FEAT_CINDENT
3957 /*
3958 * Indent now if a key was typed that is in 'cinkeys'.
3959 */
3960 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3961 do_c_expr_indent();
3962#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003963#ifdef FEAT_AUTOCMD
3964 /* Trigger the CompleteDone event to give scripts a chance to act
3965 * upon the completion. */
3966 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003970#ifdef FEAT_AUTOCMD
3971 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3972 /* Trigger the CompleteDone event to give scripts a chance to act
3973 * upon the (possibly failed) completion. */
3974 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
3977 /* reset continue_* if we left expansion-mode, if we stay they'll be
3978 * (re)set properly in ins_complete() */
3979 if (!vim_is_ctrl_x_key(c))
3980 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003981 compl_cont_status = 0;
3982 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003984
3985 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986}
3987
3988/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003989 * Fix the redo buffer for the completion leader replacing some of the typed
3990 * text. This inserts backspaces and appends the changed text.
3991 * "ptr" is the known leader text or NUL.
3992 */
3993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003994ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003995{
3996 int len;
3997 char_u *p;
3998 char_u *ptr = ptr_arg;
3999
4000 if (ptr == NULL)
4001 {
4002 if (compl_leader != NULL)
4003 ptr = compl_leader;
4004 else
4005 return; /* nothing to do */
4006 }
4007 if (compl_orig_text != NULL)
4008 {
4009 p = compl_orig_text;
4010 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4011 ;
4012#ifdef FEAT_MBYTE
4013 if (len > 0)
4014 len -= (*mb_head_off)(p, p + len);
4015#endif
4016 for (p += len; *p != NUL; mb_ptr_adv(p))
4017 AppendCharToRedobuff(K_BS);
4018 }
4019 else
4020 len = 0;
4021 if (ptr != NULL)
4022 AppendToRedobuffLit(ptr + len, -1);
4023}
4024
4025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4027 * (depending on flag) starting from buf and looking for a non-scanned
4028 * buffer (other than curbuf). curbuf is special, if it is called with
4029 * buf=curbuf then it has to be the first call for a given flag/expansion.
4030 *
4031 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4032 */
4033 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004034ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
4036#ifdef FEAT_WINDOWS
4037 static win_T *wp;
4038#endif
4039
4040 if (flag == 'w') /* just windows */
4041 {
4042#ifdef FEAT_WINDOWS
4043 if (buf == curbuf) /* first call for this flag/expansion */
4044 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004045 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 && wp->w_buffer->b_scanned)
4047 ;
4048 buf = wp->w_buffer;
4049#else
4050 buf = curbuf;
4051#endif
4052 }
4053 else
4054 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4055 * (unlisted buffers)
4056 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004057 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 && ((flag == 'U'
4059 ? buf->b_p_bl
4060 : (!buf->b_p_bl
4061 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004062 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 ;
4064 return buf;
4065}
4066
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004067#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004068/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004069 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004070 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004071 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004073expand_by_function(
4074 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4075 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004076{
Bram Moolenaar82139082011-09-14 16:52:09 +02004077 list_T *matchlist = NULL;
4078 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004079 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004080 char_u *funcname;
4081 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004082 win_T *curwin_save;
4083 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004084 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004085
Bram Moolenaare344bea2005-09-01 20:46:49 +00004086 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4087 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004088 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004089
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004090 /* Call 'completefunc' to obtain the list of matches. */
4091 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004092 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004093
Bram Moolenaare344bea2005-09-01 20:46:49 +00004094 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004095 curwin_save = curwin;
4096 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004097
4098 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004099 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004100 {
4101 switch (rettv.v_type)
4102 {
4103 case VAR_LIST:
4104 matchlist = rettv.vval.v_list;
4105 break;
4106 case VAR_DICT:
4107 matchdict = rettv.vval.v_dict;
4108 break;
4109 default:
4110 /* TODO: Give error message? */
4111 clear_tv(&rettv);
4112 break;
4113 }
4114 }
4115
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004116 if (curwin_save != curwin || curbuf_save != curbuf)
4117 {
4118 EMSG(_(e_complwin));
4119 goto theend;
4120 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004121 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004122 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004123 if (!equalpos(curwin->w_cursor, pos))
4124 {
4125 EMSG(_(e_compldel));
4126 goto theend;
4127 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004128
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004129 if (matchlist != NULL)
4130 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004131 else if (matchdict != NULL)
4132 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004133
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004134theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004135 if (matchdict != NULL)
4136 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004137 if (matchlist != NULL)
4138 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004139}
4140#endif /* FEAT_COMPL_FUNC */
4141
Bram Moolenaar39f05632006-03-19 22:15:26 +00004142#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004143/*
4144 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004145 */
4146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004147ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004148{
4149 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004150 int dir = compl_direction;
4151
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004152 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004153 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004154 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004155 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4156 /* if dir was BACKWARD then honor it just once */
4157 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004158 else if (did_emsg)
4159 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004160 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004161}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004162
4163/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004164 * Add completions from a dict.
4165 */
4166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004167ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004168{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004169 dictitem_T *di_refresh;
4170 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004171
4172 /* Check for optional "refresh" item. */
4173 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004174 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4175 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004176 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004177 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004178
4179 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4180 compl_opt_refresh_always = TRUE;
4181 }
4182
4183 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004184 di_words = dict_find(dict, (char_u *)"words", 5);
4185 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4186 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004187}
4188
4189/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004190 * Add a match to the list of matches from a typeval_T.
4191 * If the given string is already in the list of completions, then return
4192 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4193 * maybe because alloc() returns NULL, then FAIL is returned.
4194 */
4195 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004196ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004197{
4198 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004199 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004200 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004201 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004202 char_u *(cptext[CPT_COUNT]);
4203
4204 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4205 {
4206 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4207 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4208 (char_u *)"abbr", FALSE);
4209 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4210 (char_u *)"menu", FALSE);
4211 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4212 (char_u *)"kind", FALSE);
4213 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4214 (char_u *)"info", FALSE);
4215 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4216 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004217 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004218 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004219 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4220 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004221 }
4222 else
4223 {
4224 word = get_tv_string_chk(tv);
4225 vim_memset(cptext, 0, sizeof(cptext));
4226 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004227 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004228 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004229 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004230}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004231#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004232
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004233/*
4234 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004235 * The search starts at position "ini" in curbuf and in the direction
4236 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004237 * When "compl_started" is FALSE start at that position, otherwise continue
4238 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004239 * This may return before finding all the matches.
4240 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 */
4242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004243ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
4245 static pos_T first_match_pos;
4246 static pos_T last_match_pos;
4247 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004248 static int found_all = FALSE; /* Found all matches of a
4249 certain type. */
4250 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Bram Moolenaar572cb562005-08-05 21:35:02 +00004252 pos_T *pos;
4253 char_u **matches;
4254 int save_p_scs;
4255 int save_p_ws;
4256 int save_p_ic;
4257 int i;
4258 int num_matches;
4259 int len;
4260 int found_new_match;
4261 int type = ctrl_x_mode;
4262 char_u *ptr;
4263 char_u *dict = NULL;
4264 int dict_f = 0;
4265 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004266 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004268 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004270 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 ins_buf->b_scanned = 0;
4272 found_all = FALSE;
4273 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004274 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004275 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 last_match_pos = first_match_pos = *ini;
4277 }
4278
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004279 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004280 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4282 for (;;)
4283 {
4284 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004285 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004287 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 * or if found_all says this entry is done. For ^X^L only use the
4289 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004290 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004291 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
4293 found_all = FALSE;
4294 while (*e_cpt == ',' || *e_cpt == ' ')
4295 e_cpt++;
4296 if (*e_cpt == '.' && !curbuf->b_scanned)
4297 {
4298 ins_buf = curbuf;
4299 first_match_pos = *ini;
4300 /* So that ^N can match word immediately after cursor */
4301 if (ctrl_x_mode == 0)
4302 dec(&first_match_pos);
4303 last_match_pos = first_match_pos;
4304 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004305
4306 /* Remember the first match so that the loop stops when we
4307 * wrap and come back there a second time. */
4308 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
4310 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4311 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4312 {
4313 /* Scan a buffer, but not the current one. */
4314 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4315 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004316 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 first_match_pos.col = last_match_pos.col = 0;
4318 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4319 last_match_pos.lnum = 0;
4320 type = 0;
4321 }
4322 else /* unloaded buffer, scan like dictionary */
4323 {
4324 found_all = TRUE;
4325 if (ins_buf->b_fname == NULL)
4326 continue;
4327 type = CTRL_X_DICTIONARY;
4328 dict = ins_buf->b_fname;
4329 dict_f = DICT_EXACT;
4330 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004331 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 ins_buf->b_fname == NULL
4333 ? buf_spname(ins_buf)
4334 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004335 ? ins_buf->b_fname
4336 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004337 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 else if (*e_cpt == NUL)
4340 break;
4341 else
4342 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004343 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 type = -1;
4345 else if (*e_cpt == 'k' || *e_cpt == 's')
4346 {
4347 if (*e_cpt == 'k')
4348 type = CTRL_X_DICTIONARY;
4349 else
4350 type = CTRL_X_THESAURUS;
4351 if (*++e_cpt != ',' && *e_cpt != NUL)
4352 {
4353 dict = e_cpt;
4354 dict_f = DICT_FIRST;
4355 }
4356 }
4357#ifdef FEAT_FIND_ID
4358 else if (*e_cpt == 'i')
4359 type = CTRL_X_PATH_PATTERNS;
4360 else if (*e_cpt == 'd')
4361 type = CTRL_X_PATH_DEFINES;
4362#endif
4363 else if (*e_cpt == ']' || *e_cpt == 't')
4364 {
4365 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004366 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004367 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 else
4370 type = -1;
4371
4372 /* in any case e_cpt is advanced to the next entry */
4373 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4374
4375 found_all = TRUE;
4376 if (type == -1)
4377 continue;
4378 }
4379 }
4380
4381 switch (type)
4382 {
4383 case -1:
4384 break;
4385#ifdef FEAT_FIND_ID
4386 case CTRL_X_PATH_PATTERNS:
4387 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004388 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004389 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4393 (linenr_T)1, (linenr_T)MAXLNUM);
4394 break;
4395#endif
4396
4397 case CTRL_X_DICTIONARY:
4398 case CTRL_X_THESAURUS:
4399 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004400 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 : (type == CTRL_X_THESAURUS
4402 ? (*curbuf->b_p_tsr == NUL
4403 ? p_tsr
4404 : curbuf->b_p_tsr)
4405 : (*curbuf->b_p_dict == NUL
4406 ? p_dict
4407 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004408 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004409 dict != NULL ? dict_f
4410 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 dict = NULL;
4412 break;
4413
4414 case CTRL_X_TAGS:
4415 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4416 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004419 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 * of matches is found when compl_pattern is empty */
4421 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4423 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4424 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4425 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004426 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 }
4428 p_ic = save_p_ic;
4429 break;
4430
4431 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004432 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4434 {
4435
4436 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004438 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440 break;
4441
4442 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004443 if (expand_cmdline(&compl_xp, compl_pattern,
4444 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004446 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 break;
4448
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004449#ifdef FEAT_COMPL_FUNC
4450 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004451 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004452 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004453 break;
4454#endif
4455
Bram Moolenaar488c6512005-08-11 20:09:58 +00004456 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004457#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004458 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004459 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004460 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004461 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004462#endif
4463 break;
4464
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 default: /* normal ^P/^N and ^X^L */
4466 /*
4467 * If 'infercase' is set, don't use 'smartcase' here
4468 */
4469 save_p_scs = p_scs;
4470 if (ins_buf->b_p_inf)
4471 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004472
Bram Moolenaar361aa502014-01-23 22:45:58 +01004473 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 * end but never from the middle, thus setting nowrapscan in this
4475 * buffers is a good idea, on the other hand, we always set
4476 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4477 save_p_ws = p_ws;
4478 if (ins_buf != curbuf)
4479 p_ws = FALSE;
4480 else if (*e_cpt == '.')
4481 p_ws = TRUE;
4482 for (;;)
4483 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004484 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004486 ++msg_silent; /* Don't want messages for wrapscan. */
4487
Bram Moolenaare4214502015-03-05 18:08:43 +01004488 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4489 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004490 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004491 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004492 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004494 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004496 found_new_match = searchit(NULL, ins_buf, pos,
4497 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004498 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004499 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004500 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004501 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004503 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004504 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 first_match_pos = *pos;
4506 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004507 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004510 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 found_new_match = FAIL;
4512 if (found_new_match == FAIL)
4513 {
4514 if (ins_buf == curbuf)
4515 found_all = TRUE;
4516 break;
4517 }
4518
4519 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004520 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 && ini->lnum == pos->lnum
4522 && ini->col == pos->col)
4523 continue;
4524 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004525 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004527 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
4529 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4530 continue;
4531 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4532 if (!p_paste)
4533 ptr = skipwhite(ptr);
4534 }
4535 len = (int)STRLEN(ptr);
4536 }
4537 else
4538 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539 char_u *tmp_ptr = ptr;
4540
4541 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004543 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 /* Skip if already inside a word. */
4545 if (vim_iswordp(tmp_ptr))
4546 continue;
4547 /* Find start of next word. */
4548 tmp_ptr = find_word_start(tmp_ptr);
4549 }
4550 /* Find end of this word. */
4551 tmp_ptr = find_word_end(tmp_ptr);
4552 len = (int)(tmp_ptr - ptr);
4553
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 if ((compl_cont_status & CONT_ADDING)
4555 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
4557 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4558 {
4559 /* Try next line, if any. the new word will be
4560 * "join" as if the normal command "J" was used.
4561 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 * works -- Acevedo */
4564 STRNCPY(IObuff, ptr, len);
4565 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4566 tmp_ptr = ptr = skipwhite(ptr);
4567 /* Find start of next word. */
4568 tmp_ptr = find_word_start(tmp_ptr);
4569 /* Find end of next word. */
4570 tmp_ptr = find_word_end(tmp_ptr);
4571 if (tmp_ptr > ptr)
4572 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004573 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004575 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 IObuff[len++] = ' ';
4577 /* IObuf =~ "\k.* ", thus len >= 2 */
4578 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004579 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 || (vim_strchr(p_cpo, CPO_JOINSP)
4581 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004582 && (IObuff[len - 2] == '?'
4583 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 IObuff[len++] = ' ';
4585 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004586 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 if (tmp_ptr - ptr >= IOSIZE - len)
4588 tmp_ptr = ptr + IOSIZE - len - 1;
4589 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4590 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004591 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 IObuff[len] = NUL;
4594 ptr = IObuff;
4595 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 continue;
4598 }
4599 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004600 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004601 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004602 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
4604 found_new_match = OK;
4605 break;
4606 }
4607 }
4608 p_scs = save_p_scs;
4609 p_ws = save_p_ws;
4610 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004611
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004613 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004614 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 found_new_match = OK;
4616
4617 /* break the loop for specialized modes (use 'complete' just for the
4618 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004619 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004620 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004621 {
4622 if (got_int)
4623 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004624 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004625 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004626 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004627
Bram Moolenaare4214502015-03-05 18:08:43 +01004628 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004629 || compl_interrupted)
4630 break;
4631 compl_started = TRUE;
4632 }
4633 else
4634 {
4635 /* Mark a buffer scanned when it has been scanned completely */
4636 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4637 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004639 compl_started = FALSE;
4640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004642 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
Bram Moolenaare4214502015-03-05 18:08:43 +01004644 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 && *e_cpt == NUL) /* Got to end of 'complete' */
4646 found_new_match = FAIL;
4647
4648 i = -1; /* total of matches, unknown */
4649 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004650 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 i = ins_compl_make_cyclic();
4652
4653 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004654 * just been made cyclic then we have to move compl_curr_match to the next
4655 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004656 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4657 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004658 if (compl_curr_match == NULL)
4659 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 return i;
4661}
4662
4663/* Delete the old text being completed. */
4664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004665ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004667 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668
4669 /*
4670 * In insert mode: Delete the typed part.
4671 * In replace mode: Put the old characters back, if any.
4672 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004673 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4674 if ((int)curwin->w_cursor.col > col)
4675 {
4676 if (stop_arrow() == FAIL)
4677 return;
4678 backspace_until_column(col);
4679 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004680
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004681 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4682 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004684 /* clear v:completed_item */
4685 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686}
4687
Bram Moolenaar472e8592016-10-15 17:06:47 +02004688/*
4689 * Insert the new text being completed.
4690 * "in_compl_func" is TRUE when called from complete_check().
4691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004693ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004695 dict_T *dict;
4696
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004697 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004698 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4699 compl_used_match = FALSE;
4700 else
4701 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004702
4703 /* Set completed item. */
4704 /* { word, abbr, menu, kind, info } */
4705 dict = dict_alloc();
4706 if (dict != NULL)
4707 {
4708 dict_add_nr_str(dict, "word", 0L,
4709 EMPTY_IF_NULL(compl_shown_match->cp_str));
4710 dict_add_nr_str(dict, "abbr", 0L,
4711 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4712 dict_add_nr_str(dict, "menu", 0L,
4713 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4714 dict_add_nr_str(dict, "kind", 0L,
4715 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4716 dict_add_nr_str(dict, "info", 0L,
4717 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4718 }
4719 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004720 if (!in_compl_func)
4721 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722}
4723
4724/*
4725 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004726 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4727 * get more completions. If it is FALSE, then we just do nothing when there
4728 * are no more completions in a given direction. The latter case is used when
4729 * we are still in the middle of finding completions, to allow browsing
4730 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 * Return the total number of matches, or -1 if still unknown -- webb.
4732 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004733 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4734 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 *
4736 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004737 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4738 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 */
4740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004741ins_compl_next(
4742 int allow_get_expansion,
4743 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004744 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004745 int insert_match, /* Insert the newly selected match */
4746 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 int num_matches = -1;
4749 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004750 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004751 compl_T *found_compl = NULL;
4752 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004753 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004754 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004756 /* When user complete function return -1 for findstart which is next
4757 * time of 'always', compl_shown_match become NULL. */
4758 if (compl_shown_match == NULL)
4759 return -1;
4760
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004761 if (compl_leader != NULL
4762 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004764 /* Set "compl_shown_match" to the actually shown match, it may differ
4765 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004766 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004767 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004768 && compl_shown_match->cp_next != NULL
4769 && compl_shown_match->cp_next != compl_first_match)
4770 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004771
4772 /* If we didn't find it searching forward, and compl_shows_dir is
4773 * backward, find the last match. */
4774 if (compl_shows_dir == BACKWARD
4775 && !ins_compl_equal(compl_shown_match,
4776 compl_leader, (int)STRLEN(compl_leader))
4777 && (compl_shown_match->cp_next == NULL
4778 || compl_shown_match->cp_next == compl_first_match))
4779 {
4780 while (!ins_compl_equal(compl_shown_match,
4781 compl_leader, (int)STRLEN(compl_leader))
4782 && compl_shown_match->cp_prev != NULL
4783 && compl_shown_match->cp_prev != compl_first_match)
4784 compl_shown_match = compl_shown_match->cp_prev;
4785 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004786 }
4787
4788 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004789 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 /* Delete old text to be replaced */
4791 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004792
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004793 /* When finding the longest common text we stick at the original text,
4794 * don't let CTRL-N or CTRL-P move to the first match. */
4795 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4796
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004797 /* When restarting the search don't insert the first match either. */
4798 if (compl_restarting)
4799 {
4800 advance = FALSE;
4801 compl_restarting = FALSE;
4802 }
4803
Bram Moolenaare3226be2005-12-18 22:10:00 +00004804 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4805 * around. */
4806 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004808 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004810 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004811 found_end = (compl_first_match != NULL
4812 && (compl_shown_match->cp_next == compl_first_match
4813 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004814 }
4815 else if (compl_shows_dir == BACKWARD
4816 && compl_shown_match->cp_prev != NULL)
4817 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004818 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004819 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004820 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
4822 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004823 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004824 if (!allow_get_expansion)
4825 {
4826 if (advance)
4827 {
4828 if (compl_shows_dir == BACKWARD)
4829 compl_pending -= todo + 1;
4830 else
4831 compl_pending += todo + 1;
4832 }
4833 return -1;
4834 }
4835
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004836 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004837 {
4838 if (compl_shows_dir == BACKWARD)
4839 --compl_pending;
4840 else
4841 ++compl_pending;
4842 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004843
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004844 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004845 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004846
4847 /* handle any pending completions */
4848 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004849 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004850 {
4851 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4852 {
4853 compl_shown_match = compl_shown_match->cp_next;
4854 --compl_pending;
4855 }
4856 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4857 {
4858 compl_shown_match = compl_shown_match->cp_prev;
4859 ++compl_pending;
4860 }
4861 else
4862 break;
4863 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004864 found_end = FALSE;
4865 }
4866 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4867 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004868 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004869 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004870 ++todo;
4871 else
4872 /* Remember a matching item. */
4873 found_compl = compl_shown_match;
4874
4875 /* Stop at the end of the list when we found a usable match. */
4876 if (found_end)
4877 {
4878 if (found_compl != NULL)
4879 {
4880 compl_shown_match = found_compl;
4881 break;
4882 }
4883 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
4886
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004887 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004888 if (compl_no_insert && !started)
4889 {
4890 ins_bytes(compl_orig_text + ins_compl_len());
4891 compl_used_match = FALSE;
4892 }
4893 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004894 {
4895 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004896 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004897 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004898 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004899 }
4900 else
4901 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
4903 if (!allow_get_expansion)
4904 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004905 /* may undisplay the popup menu first */
4906 ins_compl_upd_pum();
4907
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004908 /* redraw to show the user what was inserted */
4909 update_screen(0);
4910
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004911 /* display the updated popup menu */
4912 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004913#ifdef FEAT_GUI
4914 if (gui.in_use)
4915 {
4916 /* Show the cursor after the match, not after the redrawn text. */
4917 setcursor();
4918 out_flush();
4919 gui_update_cursor(FALSE, FALSE);
4920 }
4921#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004922
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 /* Delete old text to be replaced, since we're still searching and
4924 * don't want to match ourselves! */
4925 ins_compl_delete();
4926 }
4927
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004928 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004929 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004930 if (compl_no_insert && !started)
4931 compl_enter_selects = TRUE;
4932 else
4933 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 /*
4936 * Show the file name for the match (if any)
4937 * Truncate the file name to avoid a wait for return.
4938 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004939 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
4941 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004942 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 if (i <= 0)
4944 i = 0;
4945 else
4946 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004947 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 msg(IObuff);
4949 redraw_cmdline = FALSE; /* don't overwrite! */
4950 }
4951
4952 return num_matches;
4953}
4954
4955/*
4956 * Call this while finding completions, to check whether the user has hit a key
4957 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004958 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004960 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004961 * "in_compl_func" is TRUE when called from complete_check(), don't set
4962 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 */
4964 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004965ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966{
4967 static int count = 0;
4968
4969 int c;
4970
4971 /* Don't check when reading keys from a script. That would break the test
4972 * scripts */
4973 if (using_script())
4974 return;
4975
4976 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004977 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 return;
4979 count = 0;
4980
Bram Moolenaara260a972006-06-23 19:36:29 +00004981 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4982 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 if (c != NUL)
4985 {
4986 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4987 {
4988 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004989 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004990 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02004991 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004993 else
4994 {
4995 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004996 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004997 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004998 if (c != K_IGNORE)
4999 {
5000 /* Don't interrupt completion when the character wasn't typed,
5001 * e.g., when doing @q to replay keys. */
5002 if (c != Ctrl_R && KeyTyped)
5003 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005004
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005005 vungetc(c);
5006 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005009 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005010 {
5011 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5012
5013 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005014 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005015 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005016}
5017
5018/*
5019 * Decide the direction of Insert mode complete from the key typed.
5020 * Returns BACKWARD or FORWARD.
5021 */
5022 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005023ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005024{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005025 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005026 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005027 return BACKWARD;
5028 return FORWARD;
5029}
5030
5031/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005032 * Return TRUE for keys that are used for completion only when the popup menu
5033 * is visible.
5034 */
5035 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005036ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005037{
5038 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005039 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5040 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005041}
5042
5043/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005044 * Decide the number of completions to move forward.
5045 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5046 */
5047 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005048ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005049{
5050 int h;
5051
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005052 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005053 {
5054 h = pum_get_height();
5055 if (h > 3)
5056 h -= 2; /* keep some context */
5057 return h;
5058 }
5059 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060}
5061
5062/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005063 * Return TRUE if completion with "c" should insert the match, FALSE if only
5064 * to change the currently selected completion.
5065 */
5066 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005067ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005068{
5069 switch (c)
5070 {
5071 case K_UP:
5072 case K_DOWN:
5073 case K_PAGEDOWN:
5074 case K_KPAGEDOWN:
5075 case K_S_DOWN:
5076 case K_PAGEUP:
5077 case K_KPAGEUP:
5078 case K_S_UP:
5079 return FALSE;
5080 }
5081 return TRUE;
5082}
5083
5084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 * Do Insert mode completion.
5086 * Called when character "c" was typed, which has a meaning for completion.
5087 * Returns OK if completion was done, FAIL if something failed (out of mem).
5088 */
5089 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005090ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005092 char_u *line;
5093 int startcol = 0; /* column where searched text starts */
5094 colnr_T curs_col; /* cursor column */
5095 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005096 int save_w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005097 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005098 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
Bram Moolenaare3226be2005-12-18 22:10:00 +00005100 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005101 insert_match = ins_compl_use_match(c);
5102
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005103 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 /* First time we hit ^N or ^P (in a row, I mean) */
5106
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 did_ai = FALSE;
5108#ifdef FEAT_SMARTINDENT
5109 did_si = FALSE;
5110 can_si = FALSE;
5111 can_si_back = FALSE;
5112#endif
5113 if (stop_arrow() == FAIL)
5114 return FAIL;
5115
5116 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005117 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005118 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005120 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005121 * "compl_startpos" to the cursor as a pattern to add a new word
5122 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005123 * "compl_startpos" is not in the same line as the cursor then fix it
5124 * (the line has been split because it was longer than 'tw'). if SOL
5125 * is set then skip the previous pattern, a word at the beginning of
5126 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005127 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5128 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
5130 /*
5131 * it is a continued search
5132 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005133 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5135 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5136 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005137 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005139 /* line (probably) wrapped, set compl_startpos to the
5140 * first non_blank in the line, if it is not a wordchar
5141 * include it to get a better pattern, but then we don't
5142 * want the "\\<" prefix, check it bellow */
5143 compl_col = (colnr_T)(skipwhite(line) - line);
5144 compl_startpos.col = compl_col;
5145 compl_startpos.lnum = curwin->w_cursor.lnum;
5146 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 }
5148 else
5149 {
5150 /* S_IPOS was set when we inserted a word that was at the
5151 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 * mode but first we need to redefine compl_startpos */
5153 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005155 compl_cont_status |= CONT_SOL;
5156 compl_startpos.col = (colnr_T)(skipwhite(
5157 line + compl_length
5158 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005160 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005162 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005163 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005164 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005166 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005168 compl_cont_status &= ~CONT_SOL;
5169 compl_length = (IOSIZE - MIN_SPACE);
5170 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005172 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5173 if (compl_length < 1)
5174 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005176 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005177 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005179 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
5181 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005182 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005184 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005186 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005188 compl_cont_status = 0;
5189 compl_cont_status |= CONT_N_ADDS;
5190 compl_startpos = curwin->w_cursor;
5191 startcol = (int)curs_col;
5192 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194
5195 /* Work out completion pattern and original text -- webb */
5196 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5197 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005198 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5200 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005201 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005203 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005205 compl_col += ++startcol;
5206 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
5208 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005209 compl_pattern = str_foldcase(line + compl_col,
5210 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005212 compl_pattern = vim_strnsave(line + compl_col,
5213 compl_length);
5214 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 return FAIL;
5216 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005217 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
5219 char_u *prefix = (char_u *)"\\<";
5220
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005221 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005222 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005223 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005224 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005226 if (!vim_iswordp(line + compl_col)
5227 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 && (
5229#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005230 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005232 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#endif
5234 )))
5235 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236 STRCPY((char *)compl_pattern, prefix);
5237 (void)quote_meta(compl_pattern + STRLEN(prefix),
5238 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005240 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005242 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245#endif
5246 )
5247 {
5248 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005249 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5250 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005252 compl_col += curs_col;
5253 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255 else
5256 {
5257#ifdef FEAT_MBYTE
5258 /* Search the point of change class of multibyte character
5259 * or not a word single byte character backward. */
5260 if (has_mbyte)
5261 {
5262 int base_class;
5263 int head_off;
5264
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005265 startcol -= (*mb_head_off)(line, line + startcol);
5266 base_class = mb_get_class(line + startcol);
5267 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005269 head_off = (*mb_head_off)(line, line + startcol);
5270 if (base_class != mb_get_class(line + startcol
5271 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005273 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
5275 }
5276 else
5277#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005278 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 compl_col += ++startcol;
5281 compl_length = (int)curs_col - startcol;
5282 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {
5284 /* Only match word with at least two chars -- webb
5285 * there's no need to call quote_meta,
5286 * alloc(7) is enough -- Acevedo
5287 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 compl_pattern = alloc(7);
5289 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 STRCPY((char *)compl_pattern, "\\<");
5292 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5293 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 }
5295 else
5296 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005297 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005298 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005299 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 STRCPY((char *)compl_pattern, "\\<");
5302 (void)quote_meta(compl_pattern + 2, line + compl_col,
5303 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305 }
5306 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005307 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005309 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005310 compl_length = (int)curs_col - (int)compl_col;
5311 if (compl_length < 0) /* cursor in indent: empty pattern */
5312 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005314 compl_pattern = str_foldcase(line + compl_col, compl_length,
5315 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005317 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5318 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 return FAIL;
5320 }
5321 else if (ctrl_x_mode == CTRL_X_FILES)
5322 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005323 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005324 if (startcol > 0)
5325 {
5326 char_u *p = line + startcol;
5327
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005328 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005329 while (p > line && vim_isfilec(PTR2CHAR(p)))
5330 mb_ptr_back(line, p);
5331 if (p == line && vim_isfilec(PTR2CHAR(p)))
5332 startcol = 0;
5333 else
5334 startcol = (int)(p - line) + 1;
5335 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005336
Bram Moolenaar0300e462013-09-08 16:03:45 +02005337 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 compl_length = (int)curs_col - startcol;
5339 compl_pattern = addstar(line + compl_col, compl_length,
5340 EXPAND_FILES);
5341 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 return FAIL;
5343 }
5344 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5345 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005346 compl_pattern = vim_strnsave(line, curs_col);
5347 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005349 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005350 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005351 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5352 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005353 /* No completion possible, use an empty pattern to get a
5354 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005355 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005356 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005357 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5358 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005360 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005361 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005362#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005363 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005364 * Call user defined function 'completefunc' with "a:findstart"
5365 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005366 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005367 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005368 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005369 char_u *funcname;
5370 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005371 win_T *curwin_save;
5372 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005373
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005374 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005375 * string */
5376 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5377 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5378 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005379 {
5380 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5381 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005382 /* restore did_ai, so that adding comment leader works */
5383 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005384 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005385 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005386
5387 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005388 args[1] = NULL;
5389 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005390 curwin_save = curwin;
5391 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005392 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005393 if (curwin_save != curwin || curbuf_save != curbuf)
5394 {
5395 EMSG(_(e_complwin));
5396 return FAIL;
5397 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005398 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005399 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005400 if (!equalpos(curwin->w_cursor, pos))
5401 {
5402 EMSG(_(e_compldel));
5403 return FAIL;
5404 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005405
Bram Moolenaar6110a002012-01-26 18:58:38 +01005406 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005407 * cancel the complete without an error.
5408 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005409 if (col == -2)
5410 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005411 if (col == -3)
5412 {
5413 ctrl_x_mode = 0;
5414 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005415 if (!shortmess(SHM_COMPLETIONMENU))
5416 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005417 return FAIL;
5418 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005419
Bram Moolenaar82139082011-09-14 16:52:09 +02005420 /*
5421 * Reset extended parameters of completion, when start new
5422 * completion.
5423 */
5424 compl_opt_refresh_always = FALSE;
5425
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005426 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005427 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005428 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005429 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005430 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005431
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005432 /* Setup variables for completion. Need to obtain "line" again,
5433 * it may have become invalid. */
5434 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005435 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5437 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005438#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005439 return FAIL;
5440 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005441 else if (ctrl_x_mode == CTRL_X_SPELL)
5442 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005443#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005444 if (spell_bad_len > 0)
5445 compl_col = curs_col - spell_bad_len;
5446 else
5447 compl_col = spell_word_start(startcol);
5448 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005449 {
5450 compl_length = 0;
5451 compl_col = curs_col;
5452 }
5453 else
5454 {
5455 spell_expand_check_cap(compl_col);
5456 compl_length = (int)curs_col - compl_col;
5457 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005458 /* Need to obtain "line" again, it may have become invalid. */
5459 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005460 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5461 if (compl_pattern == NULL)
5462#endif
5463 return FAIL;
5464 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005465 else
5466 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005467 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005468 return FAIL;
5469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005471 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 {
5473 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005474 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
5476 /* Insert a new line, keep indentation but ignore 'comments' */
5477#ifdef FEAT_COMMENTS
5478 char_u *old = curbuf->b_p_com;
5479
5480 curbuf->b_p_com = (char_u *)"";
5481#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005482 compl_startpos.lnum = curwin->w_cursor.lnum;
5483 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 ins_eol('\r');
5485#ifdef FEAT_COMMENTS
5486 curbuf->b_p_com = old;
5487#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005488 compl_length = 0;
5489 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
5491 }
5492 else
5493 {
5494 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005495 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005498 if (compl_cont_status & CONT_LOCAL)
5499 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 else
5501 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5502
Bram Moolenaar62951b12011-09-21 18:23:05 +02005503 /* If any of the original typed text has been changed we need to fix
5504 * the redo buffer. */
5505 ins_compl_fixRedoBufForLeader(NULL);
5506
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005507 /* Always add completion for the original text. */
5508 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005509 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5510 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005511 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005513 vim_free(compl_pattern);
5514 compl_pattern = NULL;
5515 vim_free(compl_orig_text);
5516 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 return FAIL;
5518 }
5519
5520 /* showmode might reset the internal line pointers, so it must
5521 * be called before line = ml_get(), or when this address is no
5522 * longer needed. -- Acevedo.
5523 */
5524 edit_submode_extra = (char_u *)_("-- Searching...");
5525 edit_submode_highl = HLF_COUNT;
5526 showmode();
5527 edit_submode_extra = NULL;
5528 out_flush();
5529 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005530 else if (insert_match && stop_arrow() == FAIL)
5531 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005533 compl_shown_match = compl_curr_match;
5534 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
5536 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005537 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005539 save_w_wrow = curwin->w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005540 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005542 /* may undisplay the popup menu */
5543 ins_compl_upd_pum();
5544
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005545 if (n > 1) /* all matches have been found */
5546 compl_matches = n;
5547 compl_curr_match = compl_shown_match;
5548 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
Bram Moolenaard68071d2006-05-02 22:08:30 +00005550 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5551 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 if (got_int && !global_busy)
5553 {
5554 (void)vgetc();
5555 got_int = FALSE;
5556 }
5557
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005558 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005559 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005561 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5562 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5564 edit_submode_highl = HLF_E;
5565 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5566 * because we couldn't expand anything at first place, but if we used
5567 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5568 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005569 if ( compl_length > 1
5570 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 || (ctrl_x_mode != 0
5572 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5573 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005574 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 }
5576
Bram Moolenaar572cb562005-08-05 21:35:02 +00005577 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005578 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005580 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581
5582 if (edit_submode_extra == NULL)
5583 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005584 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
5586 edit_submode_extra = (char_u *)_("Back at original");
5587 edit_submode_highl = HLF_W;
5588 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005589 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
5591 edit_submode_extra = (char_u *)_("Word from other line");
5592 edit_submode_highl = HLF_COUNT;
5593 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005594 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 {
5596 edit_submode_extra = (char_u *)_("The only match");
5597 edit_submode_highl = HLF_COUNT;
5598 }
5599 else
5600 {
5601 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005602 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005604 int number = 0;
5605 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005607 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
5609 /* search backwards for the first valid (!= -1) number.
5610 * This should normally succeed already at the first loop
5611 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005612 for (match = compl_curr_match->cp_prev; match != NULL
5613 && match != compl_first_match;
5614 match = match->cp_prev)
5615 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005617 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 break;
5619 }
5620 if (match != NULL)
5621 /* go up and assign all numbers which are not assigned
5622 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005623 for (match = match->cp_next;
5624 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005625 match = match->cp_next)
5626 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 }
5628 else /* BACKWARD */
5629 {
5630 /* search forwards (upwards) for the first valid (!= -1)
5631 * number. This should normally succeed already at the
5632 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005633 for (match = compl_curr_match->cp_next; match != NULL
5634 && match != compl_first_match;
5635 match = match->cp_next)
5636 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005638 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 break;
5640 }
5641 if (match != NULL)
5642 /* go down and assign all numbers which are not
5643 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005644 for (match = match->cp_prev; match
5645 && match->cp_number == -1;
5646 match = match->cp_prev)
5647 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
5649 }
5650
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005651 /* The match should always have a sequence number now, this is
5652 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005653 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005655 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5656 * Translations may need more than twice that. */
5657 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005659 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005660 vim_snprintf((char *)match_ref, sizeof(match_ref),
5661 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005662 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005664 vim_snprintf((char *)match_ref, sizeof(match_ref),
5665 _("match %d"),
5666 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 edit_submode_extra = match_ref;
5668 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005669 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 curs_columns(FALSE);
5671 }
5672 }
5673 }
5674
5675 /* Show a message about what (completion) mode we're in. */
5676 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005677 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005679 if (edit_submode_extra != NULL)
5680 {
5681 if (!p_smd)
5682 msg_attr(edit_submode_extra,
5683 edit_submode_highl < HLF_COUNT
5684 ? hl_attr(edit_submode_highl) : 0);
5685 }
5686 else
5687 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
Bram Moolenaard68071d2006-05-02 22:08:30 +00005690 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005691 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005692 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005693 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005694 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005695 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005696 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005697
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 return OK;
5699}
5700
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005701 static void
5702show_pum(int save_w_wrow)
5703{
5704 /* RedrawingDisabled may be set when invoked through complete(). */
5705 int n = RedrawingDisabled;
5706
5707 RedrawingDisabled = 0;
5708
5709 /* If the cursor moved we need to remove the pum first. */
5710 setcursor();
5711 if (save_w_wrow != curwin->w_wrow)
5712 ins_compl_del_pum();
5713
5714 ins_compl_show_pum();
5715 setcursor();
5716 RedrawingDisabled = n;
5717}
5718
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719/*
5720 * Looks in the first "len" chars. of "src" for search-metachars.
5721 * If dest is not NULL the chars. are copied there quoting (with
5722 * a backslash) the metachars, and dest would be NUL terminated.
5723 * Returns the length (needed) of dest
5724 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005725 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005726quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005728 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005730 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 {
5732 switch (*src)
5733 {
5734 case '.':
5735 case '*':
5736 case '[':
5737 if (ctrl_x_mode == CTRL_X_DICTIONARY
5738 || ctrl_x_mode == CTRL_X_THESAURUS)
5739 break;
5740 case '~':
5741 if (!p_magic) /* quote these only if magic is set */
5742 break;
5743 case '\\':
5744 if (ctrl_x_mode == CTRL_X_DICTIONARY
5745 || ctrl_x_mode == CTRL_X_THESAURUS)
5746 break;
5747 case '^': /* currently it's not needed. */
5748 case '$':
5749 m++;
5750 if (dest != NULL)
5751 *dest++ = '\\';
5752 break;
5753 }
5754 if (dest != NULL)
5755 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005756# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 /* Copy remaining bytes of a multibyte character. */
5758 if (has_mbyte)
5759 {
5760 int i, mb_len;
5761
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005762 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 if (mb_len > 0 && len >= mb_len)
5764 for (i = 0; i < mb_len; ++i)
5765 {
5766 --len;
5767 ++src;
5768 if (dest != NULL)
5769 *dest++ = *src;
5770 }
5771 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
5774 if (dest != NULL)
5775 *dest = NUL;
5776
5777 return m;
5778}
5779#endif /* FEAT_INS_EXPAND */
5780
5781/*
5782 * Next character is interpreted literally.
5783 * A one, two or three digit decimal number is interpreted as its byte value.
5784 * If one or two digits are entered, the next character is given to vungetc().
5785 * For Unicode a character > 255 may be returned.
5786 */
5787 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005788get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790 int cc;
5791 int nc;
5792 int i;
5793 int hex = FALSE;
5794 int octal = FALSE;
5795#ifdef FEAT_MBYTE
5796 int unicode = 0;
5797#endif
5798
5799 if (got_int)
5800 return Ctrl_C;
5801
5802#ifdef FEAT_GUI
5803 /*
5804 * In GUI there is no point inserting the internal code for a special key.
5805 * It is more useful to insert the string "<KEY>" instead. This would
5806 * probably be useful in a text window too, but it would not be
5807 * vi-compatible (maybe there should be an option for it?) -- webb
5808 */
5809 if (gui.in_use)
5810 ++allow_keys;
5811#endif
5812#ifdef USE_ON_FLY_SCROLL
5813 dont_scroll = TRUE; /* disallow scrolling here */
5814#endif
5815 ++no_mapping; /* don't map the next key hits */
5816 cc = 0;
5817 i = 0;
5818 for (;;)
5819 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005820 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821#ifdef FEAT_CMDL_INFO
5822 if (!(State & CMDLINE)
5823# ifdef FEAT_MBYTE
5824 && MB_BYTE2LEN_CHECK(nc) == 1
5825# endif
5826 )
5827 add_to_showcmd(nc);
5828#endif
5829 if (nc == 'x' || nc == 'X')
5830 hex = TRUE;
5831 else if (nc == 'o' || nc == 'O')
5832 octal = TRUE;
5833#ifdef FEAT_MBYTE
5834 else if (nc == 'u' || nc == 'U')
5835 unicode = nc;
5836#endif
5837 else
5838 {
5839 if (hex
5840#ifdef FEAT_MBYTE
5841 || unicode != 0
5842#endif
5843 )
5844 {
5845 if (!vim_isxdigit(nc))
5846 break;
5847 cc = cc * 16 + hex2nr(nc);
5848 }
5849 else if (octal)
5850 {
5851 if (nc < '0' || nc > '7')
5852 break;
5853 cc = cc * 8 + nc - '0';
5854 }
5855 else
5856 {
5857 if (!VIM_ISDIGIT(nc))
5858 break;
5859 cc = cc * 10 + nc - '0';
5860 }
5861
5862 ++i;
5863 }
5864
5865 if (cc > 255
5866#ifdef FEAT_MBYTE
5867 && unicode == 0
5868#endif
5869 )
5870 cc = 255; /* limit range to 0-255 */
5871 nc = 0;
5872
5873 if (hex) /* hex: up to two chars */
5874 {
5875 if (i >= 2)
5876 break;
5877 }
5878#ifdef FEAT_MBYTE
5879 else if (unicode) /* Unicode: up to four or eight chars */
5880 {
5881 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5882 break;
5883 }
5884#endif
5885 else if (i >= 3) /* decimal or octal: up to three chars */
5886 break;
5887 }
5888 if (i == 0) /* no number entered */
5889 {
5890 if (nc == K_ZERO) /* NUL is stored as NL */
5891 {
5892 cc = '\n';
5893 nc = 0;
5894 }
5895 else
5896 {
5897 cc = nc;
5898 nc = 0;
5899 }
5900 }
5901
5902 if (cc == 0) /* NUL is stored as NL */
5903 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005904#ifdef FEAT_MBYTE
5905 if (enc_dbcs && (cc & 0xff) == 0)
5906 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5907 second byte will cause trouble! */
5908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
5910 --no_mapping;
5911#ifdef FEAT_GUI
5912 if (gui.in_use)
5913 --allow_keys;
5914#endif
5915 if (nc)
5916 vungetc(nc);
5917 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5918 return cc;
5919}
5920
5921/*
5922 * Insert character, taking care of special keys and mod_mask
5923 */
5924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005925insert_special(
5926 int c,
5927 int allow_modmask,
5928 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
5930 char_u *p;
5931 int len;
5932
5933 /*
5934 * Special function key, translate into "<Key>". Up to the last '>' is
5935 * inserted with ins_str(), so as not to replace characters in replace
5936 * mode.
5937 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5938 * unless 'allow_modmask' is TRUE.
5939 */
5940#ifdef MACOS
5941 /* Command-key never produces a normal key */
5942 if (mod_mask & MOD_MASK_CMD)
5943 allow_modmask = TRUE;
5944#endif
5945 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5946 {
5947 p = get_special_key_name(c, mod_mask);
5948 len = (int)STRLEN(p);
5949 c = p[len - 1];
5950 if (len > 2)
5951 {
5952 if (stop_arrow() == FAIL)
5953 return;
5954 p[len - 1] = NUL;
5955 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005956 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 ctrlv = FALSE;
5958 }
5959 }
5960 if (stop_arrow() == OK)
5961 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5962}
5963
5964/*
5965 * Special characters in this context are those that need processing other
5966 * than the simple insertion that can be performed here. This includes ESC
5967 * which terminates the insert, and CR/NL which need special processing to
5968 * open up a new line. This routine tries to optimize insertions performed by
5969 * the "redo", "undo" or "put" commands, so it needs to know when it should
5970 * stop and defer processing to the "normal" mechanism.
5971 * '0' and '^' are special, because they can be followed by CTRL-D.
5972 */
5973#ifdef EBCDIC
5974# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5975#else
5976# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5977#endif
5978
5979#ifdef FEAT_MBYTE
5980# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5981#else
5982# define WHITECHAR(cc) vim_iswhite(cc)
5983#endif
5984
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005985/*
5986 * "flags": INSCHAR_FORMAT - force formatting
5987 * INSCHAR_CTRLV - char typed just after CTRL-V
5988 * INSCHAR_NO_FEX - don't use 'formatexpr'
5989 *
5990 * NOTE: passes the flags value straight through to internal_format() which,
5991 * beside INSCHAR_FORMAT (above), is also looking for these:
5992 * INSCHAR_DO_COM - format comments
5993 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005996insertchar(
5997 int c, /* character to insert or NUL */
5998 int flags, /* INSCHAR_FORMAT, etc. */
5999 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 int textwidth;
6002#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006006 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006008 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010
6011 /*
6012 * Try to break the line in two or more pieces when:
6013 * - Always do this if we have been called to do formatting only.
6014 * - Always do this when 'formatoptions' has the 'a' flag and the line
6015 * ends in white space.
6016 * - Otherwise:
6017 * - Don't do this if inserting a blank
6018 * - Don't do this if an existing character is being replaced, unless
6019 * we're in VREPLACE mode.
6020 * - Do this if the cursor is not on the line where insert started
6021 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6022 * before the insert.
6023 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6024 * before 'textwidth'
6025 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006026 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006027 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 || (!vim_iswhite(c)
6029 && !((State & REPLACE_FLAG)
6030#ifdef FEAT_VREPLACE
6031 && !(State & VREPLACE_FLAG)
6032#endif
6033 && *ml_get_cursor() != NUL)
6034 && (curwin->w_cursor.lnum != Insstart.lnum
6035 || ((!has_format_option(FO_INS_LONG)
6036 || Insstart_textlen <= (colnr_T)textwidth)
6037 && (!fo_ins_blank
6038 || Insstart_blank_vcol <= (colnr_T)textwidth
6039 ))))))
6040 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006041 /* Format with 'formatexpr' when it's set. Use internal formatting
6042 * when 'formatexpr' isn't set or it returns non-zero. */
6043#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006044 int do_internal = TRUE;
6045 colnr_T virtcol = get_nolist_virtcol()
6046 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006047
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006048 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6049 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006050 {
6051 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6052 /* It may be required to save for undo again, e.g. when setline()
6053 * was called. */
6054 ins_need_undo = TRUE;
6055 }
6056 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006058 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006060
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 if (c == NUL) /* only formatting was wanted */
6062 return;
6063
6064#ifdef FEAT_COMMENTS
6065 /* Check whether this character should end a comment. */
6066 if (did_ai && (int)c == end_comment_pending)
6067 {
6068 char_u *line;
6069 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6070 int middle_len, end_len;
6071 int i;
6072
6073 /*
6074 * Need to remove existing (middle) comment leader and insert end
6075 * comment leader. First, check what comment leader we can find.
6076 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006077 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6079 {
6080 /* Skip middle-comment string */
6081 while (*p && p[-1] != ':') /* find end of middle flags */
6082 ++p;
6083 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6084 /* Don't count trailing white space for middle_len */
6085 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6086 --middle_len;
6087
6088 /* Find the end-comment string */
6089 while (*p && p[-1] != ':') /* find end of end flags */
6090 ++p;
6091 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6092
6093 /* Skip white space before the cursor */
6094 i = curwin->w_cursor.col;
6095 while (--i >= 0 && vim_iswhite(line[i]))
6096 ;
6097 i++;
6098
6099 /* Skip to before the middle leader */
6100 i -= middle_len;
6101
6102 /* Check some expected things before we go on */
6103 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6104 {
6105 /* Backspace over all the stuff we want to replace */
6106 backspace_until_column(i);
6107
6108 /*
6109 * Insert the end-comment string, except for the last
6110 * character, which will get inserted as normal later.
6111 */
6112 ins_bytes_len(lead_end, end_len - 1);
6113 }
6114 }
6115 }
6116 end_comment_pending = NUL;
6117#endif
6118
6119 did_ai = FALSE;
6120#ifdef FEAT_SMARTINDENT
6121 did_si = FALSE;
6122 can_si = FALSE;
6123 can_si_back = FALSE;
6124#endif
6125
6126 /*
6127 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6128 * This speeds up normal text input considerably.
6129 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6130 * need to re-indent at a ':', or any other character (but not what
6131 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006132 * Don't do this when there an InsertCharPre autocommand is defined,
6133 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 */
6135#ifdef USE_ON_FLY_SCROLL
6136 dont_scroll = FALSE; /* allow scrolling here */
6137#endif
6138
6139 if ( !ISSPECIAL(c)
6140#ifdef FEAT_MBYTE
6141 && (!has_mbyte || (*mb_char2len)(c) == 1)
6142#endif
6143 && vpeekc() != NUL
6144 && !(State & REPLACE_FLAG)
6145#ifdef FEAT_CINDENT
6146 && !cindent_on()
6147#endif
6148#ifdef FEAT_RIGHTLEFT
6149 && !p_ri
6150#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006151#ifdef FEAT_AUTOCMD
6152 && !has_insertcharpre()
6153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 )
6155 {
6156#define INPUT_BUFLEN 100
6157 char_u buf[INPUT_BUFLEN + 1];
6158 int i;
6159 colnr_T virtcol = 0;
6160
6161 buf[0] = c;
6162 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006163 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 virtcol = get_nolist_virtcol();
6165 /*
6166 * Stop the string when:
6167 * - no more chars available
6168 * - finding a special character (command key)
6169 * - buffer is full
6170 * - running into the 'textwidth' boundary
6171 * - need to check for abbreviation: A non-word char after a word-char
6172 */
6173 while ( (c = vpeekc()) != NUL
6174 && !ISSPECIAL(c)
6175#ifdef FEAT_MBYTE
6176 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6177#endif
6178 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006179# ifdef FEAT_FKMAP
6180 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 && (textwidth == 0
6183 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6184 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6185 {
6186#ifdef FEAT_RIGHTLEFT
6187 c = vgetc();
6188 if (p_hkmap && KeyTyped)
6189 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 buf[i++] = c;
6191#else
6192 buf[i++] = vgetc();
6193#endif
6194 }
6195
6196#ifdef FEAT_DIGRAPHS
6197 do_digraph(-1); /* clear digraphs */
6198 do_digraph(buf[i-1]); /* may be the start of a digraph */
6199#endif
6200 buf[i] = NUL;
6201 ins_str(buf);
6202 if (flags & INSCHAR_CTRLV)
6203 {
6204 redo_literal(*buf);
6205 i = 1;
6206 }
6207 else
6208 i = 0;
6209 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006210 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 }
6212 else
6213 {
6214#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006215 int cc;
6216
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6218 {
6219 char_u buf[MB_MAXBYTES + 1];
6220
6221 (*mb_char2bytes)(c, buf);
6222 buf[cc] = NUL;
6223 ins_char_bytes(buf, cc);
6224 AppendCharToRedobuff(c);
6225 }
6226 else
6227#endif
6228 {
6229 ins_char(c);
6230 if (flags & INSCHAR_CTRLV)
6231 redo_literal(c);
6232 else
6233 AppendCharToRedobuff(c);
6234 }
6235 }
6236}
6237
6238/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006239 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006240 *
6241 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6242 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006243 */
6244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006245internal_format(
6246 int textwidth,
6247 int second_indent,
6248 int flags,
6249 int format_only,
6250 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006251{
6252 int cc;
6253 int save_char = NUL;
6254 int haveto_redraw = FALSE;
6255 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6256#ifdef FEAT_MBYTE
6257 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6258#endif
6259 int fo_white_par = has_format_option(FO_WHITE_PAR);
6260 int first_line = TRUE;
6261#ifdef FEAT_COMMENTS
6262 colnr_T leader_len;
6263 int no_leader = FALSE;
6264 int do_comments = (flags & INSCHAR_DO_COM);
6265#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006266#ifdef FEAT_LINEBREAK
6267 int has_lbr = curwin->w_p_lbr;
6268
6269 /* make sure win_lbr_chartabsize() counts correctly */
6270 curwin->w_p_lbr = FALSE;
6271#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006272
6273 /*
6274 * When 'ai' is off we don't want a space under the cursor to be
6275 * deleted. Replace it with an 'x' temporarily.
6276 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006277 if (!curbuf->b_p_ai
6278#ifdef FEAT_VREPLACE
6279 && !(State & VREPLACE_FLAG)
6280#endif
6281 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006282 {
6283 cc = gchar_cursor();
6284 if (vim_iswhite(cc))
6285 {
6286 save_char = cc;
6287 pchar_cursor('x');
6288 }
6289 }
6290
6291 /*
6292 * Repeat breaking lines, until the current line is not too long.
6293 */
6294 while (!got_int)
6295 {
6296 int startcol; /* Cursor column at entry */
6297 int wantcol; /* column at textwidth border */
6298 int foundcol; /* column for start of spaces */
6299 int end_foundcol = 0; /* column for start of word */
6300 colnr_T len;
6301 colnr_T virtcol;
6302#ifdef FEAT_VREPLACE
6303 int orig_col = 0;
6304 char_u *saved_text = NULL;
6305#endif
6306 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006307 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006308
Bram Moolenaar97b98102009-11-17 16:41:01 +00006309 virtcol = get_nolist_virtcol()
6310 + char2cells(c != NUL ? c : gchar_cursor());
6311 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006312 break;
6313
6314#ifdef FEAT_COMMENTS
6315 if (no_leader)
6316 do_comments = FALSE;
6317 else if (!(flags & INSCHAR_FORMAT)
6318 && has_format_option(FO_WRAP_COMS))
6319 do_comments = TRUE;
6320
6321 /* Don't break until after the comment leader */
6322 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006323 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006324 else
6325 leader_len = 0;
6326
6327 /* If the line doesn't start with a comment leader, then don't
6328 * start one in a following broken line. Avoids that a %word
6329 * moved to the start of the next line causes all following lines
6330 * to start with %. */
6331 if (leader_len == 0)
6332 no_leader = TRUE;
6333#endif
6334 if (!(flags & INSCHAR_FORMAT)
6335#ifdef FEAT_COMMENTS
6336 && leader_len == 0
6337#endif
6338 && !has_format_option(FO_WRAP))
6339
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006340 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006341 if ((startcol = curwin->w_cursor.col) == 0)
6342 break;
6343
6344 /* find column of textwidth border */
6345 coladvance((colnr_T)textwidth);
6346 wantcol = curwin->w_cursor.col;
6347
Bram Moolenaar97b98102009-11-17 16:41:01 +00006348 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006349 foundcol = 0;
6350
6351 /*
6352 * Find position to break at.
6353 * Stop at first entered white when 'formatoptions' has 'v'
6354 */
6355 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006356 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006357 || curwin->w_cursor.lnum != Insstart.lnum
6358 || curwin->w_cursor.col >= Insstart.col)
6359 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006360 if (curwin->w_cursor.col == startcol && c != NUL)
6361 cc = c;
6362 else
6363 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006364 if (WHITECHAR(cc))
6365 {
6366 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006367 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006368
6369 /* find start of sequence of blanks */
6370 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6371 {
6372 dec_cursor();
6373 cc = gchar_cursor();
6374 }
6375 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6376 break; /* only spaces in front of text */
6377#ifdef FEAT_COMMENTS
6378 /* Don't break until after the comment leader */
6379 if (curwin->w_cursor.col < leader_len)
6380 break;
6381#endif
6382 if (has_format_option(FO_ONE_LETTER))
6383 {
6384 /* do not break after one-letter words */
6385 if (curwin->w_cursor.col == 0)
6386 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006387#ifdef FEAT_COMMENTS
6388 /* do not break "#a b" when 'tw' is 2 */
6389 if (curwin->w_cursor.col <= leader_len)
6390 break;
6391#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006392 col = curwin->w_cursor.col;
6393 dec_cursor();
6394 cc = gchar_cursor();
6395
6396 if (WHITECHAR(cc))
6397 continue; /* one-letter, continue */
6398 curwin->w_cursor.col = col;
6399 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006400
6401 inc_cursor();
6402
6403 end_foundcol = end_col + 1;
6404 foundcol = curwin->w_cursor.col;
6405 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006406 break;
6407 }
6408#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006409 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006410 {
6411 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006412 if (curwin->w_cursor.col != startcol)
6413 {
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 col = curwin->w_cursor.col;
6420 inc_cursor();
6421 /* Don't change end_foundcol if already set. */
6422 if (foundcol != curwin->w_cursor.col)
6423 {
6424 foundcol = curwin->w_cursor.col;
6425 end_foundcol = foundcol;
6426 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6427 break;
6428 }
6429 curwin->w_cursor.col = col;
6430 }
6431
6432 if (curwin->w_cursor.col == 0)
6433 break;
6434
6435 col = curwin->w_cursor.col;
6436
6437 dec_cursor();
6438 cc = gchar_cursor();
6439
6440 if (WHITECHAR(cc))
6441 continue; /* break with space */
6442#ifdef FEAT_COMMENTS
6443 /* Don't break until after the comment leader */
6444 if (curwin->w_cursor.col < leader_len)
6445 break;
6446#endif
6447
6448 curwin->w_cursor.col = col;
6449
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006450 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006451 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006452 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6453 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006454 }
6455#endif
6456 if (curwin->w_cursor.col == 0)
6457 break;
6458 dec_cursor();
6459 }
6460
6461 if (foundcol == 0) /* no spaces, cannot break line */
6462 {
6463 curwin->w_cursor.col = startcol;
6464 break;
6465 }
6466
6467 /* Going to break the line, remove any "$" now. */
6468 undisplay_dollar();
6469
6470 /*
6471 * Offset between cursor position and line break is used by replace
6472 * stack functions. VREPLACE does not use this, and backspaces
6473 * over the text instead.
6474 */
6475#ifdef FEAT_VREPLACE
6476 if (State & VREPLACE_FLAG)
6477 orig_col = startcol; /* Will start backspacing from here */
6478 else
6479#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006480 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006481
6482 /*
6483 * adjust startcol for spaces that will be deleted and
6484 * characters that will remain on top line
6485 */
6486 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006487 while ((cc = gchar_cursor(), WHITECHAR(cc))
6488 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006489 inc_cursor();
6490 startcol -= curwin->w_cursor.col;
6491 if (startcol < 0)
6492 startcol = 0;
6493
6494#ifdef FEAT_VREPLACE
6495 if (State & VREPLACE_FLAG)
6496 {
6497 /*
6498 * In VREPLACE mode, we will backspace over the text to be
6499 * wrapped, so save a copy now to put on the next line.
6500 */
6501 saved_text = vim_strsave(ml_get_cursor());
6502 curwin->w_cursor.col = orig_col;
6503 if (saved_text == NULL)
6504 break; /* Can't do it, out of memory */
6505 saved_text[startcol] = NUL;
6506
6507 /* Backspace over characters that will move to the next line */
6508 if (!fo_white_par)
6509 backspace_until_column(foundcol);
6510 }
6511 else
6512#endif
6513 {
6514 /* put cursor after pos. to break line */
6515 if (!fo_white_par)
6516 curwin->w_cursor.col = foundcol;
6517 }
6518
6519 /*
6520 * Split the line just before the margin.
6521 * Only insert/delete lines, but don't really redraw the window.
6522 */
6523 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6524 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6525#ifdef FEAT_COMMENTS
6526 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006527 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006528#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006529 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6530 if (!(flags & INSCHAR_COM_LIST))
6531 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006532
6533 replace_offset = 0;
6534 if (first_line)
6535 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006536 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006537 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006538 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006539 * This section is for auto-wrap of numeric lists. When not
6540 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6541 * flag will be set and open_line() will handle it (as seen
6542 * above). The code here (and in get_number_indent()) will
6543 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006544 */
6545 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006546 second_indent =
6547 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006548 if (second_indent >= 0)
6549 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006550#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006551 if (State & VREPLACE_FLAG)
6552 change_indent(INDENT_SET, second_indent,
6553 FALSE, NUL, TRUE);
6554 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006555#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006556#ifdef FEAT_COMMENTS
6557 if (leader_len > 0 && second_indent - leader_len > 0)
6558 {
6559 int i;
6560 int padding = second_indent - leader_len;
6561
6562 /* We started at the first_line of a numbered list
6563 * that has a comment. the open_line() function has
6564 * inserted the proper comment leader and positioned
6565 * the cursor at the end of the split line. Now we
6566 * add the additional whitespace needed after the
6567 * comment leader for the numbered list. */
6568 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006569 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006570 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006571 }
6572 else
6573 {
6574#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006575 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006576#ifdef FEAT_COMMENTS
6577 }
6578#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006579 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006580 }
6581 first_line = FALSE;
6582 }
6583
6584#ifdef FEAT_VREPLACE
6585 if (State & VREPLACE_FLAG)
6586 {
6587 /*
6588 * In VREPLACE mode we have backspaced over the text to be
6589 * moved, now we re-insert it into the new line.
6590 */
6591 ins_bytes(saved_text);
6592 vim_free(saved_text);
6593 }
6594 else
6595#endif
6596 {
6597 /*
6598 * Check if cursor is not past the NUL off the line, cindent
6599 * may have added or removed indent.
6600 */
6601 curwin->w_cursor.col += startcol;
6602 len = (colnr_T)STRLEN(ml_get_curline());
6603 if (curwin->w_cursor.col > len)
6604 curwin->w_cursor.col = len;
6605 }
6606
6607 haveto_redraw = TRUE;
6608#ifdef FEAT_CINDENT
6609 can_cindent = TRUE;
6610#endif
6611 /* moved the cursor, don't autoindent or cindent now */
6612 did_ai = FALSE;
6613#ifdef FEAT_SMARTINDENT
6614 did_si = FALSE;
6615 can_si = FALSE;
6616 can_si_back = FALSE;
6617#endif
6618 line_breakcheck();
6619 }
6620
6621 if (save_char != NUL) /* put back space after cursor */
6622 pchar_cursor(save_char);
6623
Bram Moolenaar0026d472014-09-09 16:32:39 +02006624#ifdef FEAT_LINEBREAK
6625 curwin->w_p_lbr = has_lbr;
6626#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006627 if (!format_only && haveto_redraw)
6628 {
6629 update_topline();
6630 redraw_curbuf_later(VALID);
6631 }
6632}
6633
6634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 * Called after inserting or deleting text: When 'formatoptions' includes the
6636 * 'a' flag format from the current line until the end of the paragraph.
6637 * Keep the cursor at the same position relative to the text.
6638 * The caller must have saved the cursor line for undo, following ones will be
6639 * saved here.
6640 */
6641 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006642auto_format(
6643 int trailblank, /* when TRUE also format with trailing blank */
6644 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
6646 pos_T pos;
6647 colnr_T len;
6648 char_u *old;
6649 char_u *new, *pnew;
6650 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006651 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 if (!has_format_option(FO_AUTO))
6654 return;
6655
6656 pos = curwin->w_cursor;
6657 old = ml_get_curline();
6658
6659 /* may remove added space */
6660 check_auto_format(FALSE);
6661
6662 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6663 * user might insert normal text next. Also skip formatting when "1" is
6664 * in 'formatoptions' and there is a single character before the cursor.
6665 * Otherwise the line would be broken and when typing another non-white
6666 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006667 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 if (*old != NUL && !trailblank && wasatend)
6669 {
6670 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006671 cc = gchar_cursor();
6672 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6673 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006675 cc = gchar_cursor();
6676 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 {
6678 curwin->w_cursor = pos;
6679 return;
6680 }
6681 curwin->w_cursor = pos;
6682 }
6683
6684#ifdef FEAT_COMMENTS
6685 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6686 * comments. */
6687 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006688 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 return;
6690#endif
6691
6692 /*
6693 * May start formatting in a previous line, so that after "x" a word is
6694 * moved to the previous line if it fits there now. Only when this is not
6695 * the start of a paragraph.
6696 */
6697 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6698 {
6699 --curwin->w_cursor.lnum;
6700 if (u_save_cursor() == FAIL)
6701 return;
6702 }
6703
6704 /*
6705 * Do the formatting and restore the cursor position. "saved_cursor" will
6706 * be adjusted for the text formatting.
6707 */
6708 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006709 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 curwin->w_cursor = saved_cursor;
6711 saved_cursor.lnum = 0;
6712
6713 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6714 {
6715 /* "cannot happen" */
6716 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6717 coladvance((colnr_T)MAXCOL);
6718 }
6719 else
6720 check_cursor_col();
6721
6722 /* Insert mode: If the cursor is now after the end of the line while it
6723 * previously wasn't, the line was broken. Because of the rule above we
6724 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6725 * formatted. */
6726 if (!wasatend && has_format_option(FO_WHITE_PAR))
6727 {
6728 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006729 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 if (curwin->w_cursor.col == len)
6731 {
6732 pnew = vim_strnsave(new, len + 2);
6733 pnew[len] = ' ';
6734 pnew[len + 1] = NUL;
6735 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6736 /* remove the space later */
6737 did_add_space = TRUE;
6738 }
6739 else
6740 /* may remove added space */
6741 check_auto_format(FALSE);
6742 }
6743
6744 check_cursor();
6745}
6746
6747/*
6748 * When an extra space was added to continue a paragraph for auto-formatting,
6749 * delete it now. The space must be under the cursor, just after the insert
6750 * position.
6751 */
6752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006753check_auto_format(
6754 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755{
6756 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006757 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
6759 if (did_add_space)
6760 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006761 cc = gchar_cursor();
6762 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 /* Somehow the space was removed already. */
6764 did_add_space = FALSE;
6765 else
6766 {
6767 if (!end_insert)
6768 {
6769 inc_cursor();
6770 c = gchar_cursor();
6771 dec_cursor();
6772 }
6773 if (c != NUL)
6774 {
6775 /* The space is no longer at the end of the line, delete it. */
6776 del_char(FALSE);
6777 did_add_space = FALSE;
6778 }
6779 }
6780 }
6781}
6782
6783/*
6784 * Find out textwidth to be used for formatting:
6785 * if 'textwidth' option is set, use it
6786 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6787 * if invalid value, use 0.
6788 * Set default to window width (maximum 79) for "gq" operator.
6789 */
6790 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006791comp_textwidth(
6792 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
6794 int textwidth;
6795
6796 textwidth = curbuf->b_p_tw;
6797 if (textwidth == 0 && curbuf->b_p_wm)
6798 {
6799 /* The width is the window width minus 'wrapmargin' minus all the
6800 * things that add to the margin. */
6801 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6802#ifdef FEAT_CMDWIN
6803 if (cmdwin_type != 0)
6804 textwidth -= 1;
6805#endif
6806#ifdef FEAT_FOLDING
6807 textwidth -= curwin->w_p_fdc;
6808#endif
6809#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006810 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 textwidth -= 1;
6812#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006813 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 textwidth -= 8;
6815 }
6816 if (textwidth < 0)
6817 textwidth = 0;
6818 if (ff && textwidth == 0)
6819 {
6820 textwidth = W_WIDTH(curwin) - 1;
6821 if (textwidth > 79)
6822 textwidth = 79;
6823 }
6824 return textwidth;
6825}
6826
6827/*
6828 * Put a character in the redo buffer, for when just after a CTRL-V.
6829 */
6830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006831redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832{
6833 char_u buf[10];
6834
6835 /* Only digits need special treatment. Translate them into a string of
6836 * three digits. */
6837 if (VIM_ISDIGIT(c))
6838 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006839 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 AppendToRedobuff(buf);
6841 }
6842 else
6843 AppendCharToRedobuff(c);
6844}
6845
6846/*
6847 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006848 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 */
6850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006851start_arrow(
6852 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006854 start_arrow_common(end_insert_pos, TRUE);
6855}
6856
6857/*
6858 * Like start_arrow() but with end_change argument.
6859 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6860 */
6861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006862start_arrow_with_change(
6863 pos_T *end_insert_pos, /* can be NULL */
6864 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006865{
6866 start_arrow_common(end_insert_pos, end_change);
6867 if (!end_change)
6868 {
6869 AppendCharToRedobuff(Ctrl_G);
6870 AppendCharToRedobuff('U');
6871 }
6872}
6873
6874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006875start_arrow_common(
6876 pos_T *end_insert_pos, /* can be NULL */
6877 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006878{
6879 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
6881 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006882 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 arrow_used = TRUE; /* this means we stopped the current insert */
6884 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006885#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006886 check_spell_redraw();
6887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888}
6889
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006890#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006891/*
6892 * If we skipped highlighting word at cursor, do it now.
6893 * It may be skipped again, thus reset spell_redraw_lnum first.
6894 */
6895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006896check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006897{
6898 if (spell_redraw_lnum != 0)
6899 {
6900 linenr_T lnum = spell_redraw_lnum;
6901
6902 spell_redraw_lnum = 0;
6903 redrawWinline(lnum, FALSE);
6904 }
6905}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006906
6907/*
6908 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6909 * spelled word, if there is one.
6910 */
6911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006912spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006913{
6914 pos_T tpos = curwin->w_cursor;
6915
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006916 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006917 if (curwin->w_cursor.col != tpos.col)
6918 start_arrow(&tpos);
6919}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006920#endif
6921
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922/*
6923 * stop_arrow() is called before a change is made in insert mode.
6924 * If an arrow key has been used, start a new insertion.
6925 * Returns FAIL if undo is impossible, shouldn't insert then.
6926 */
6927 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006928stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
6930 if (arrow_used)
6931 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006932 Insstart = curwin->w_cursor; /* new insertion starts here */
6933 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6934 /* Don't update the original insert position when moved to the
6935 * right, except when nothing was inserted yet. */
6936 update_Insstart_orig = FALSE;
6937 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6938
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 if (u_save_cursor() == OK)
6940 {
6941 arrow_used = FALSE;
6942 ins_need_undo = FALSE;
6943 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006944
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 ai_col = 0;
6946#ifdef FEAT_VREPLACE
6947 if (State & VREPLACE_FLAG)
6948 {
6949 orig_line_count = curbuf->b_ml.ml_line_count;
6950 vr_lines_changed = 1;
6951 }
6952#endif
6953 ResetRedobuff();
6954 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006955 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 }
6957 else if (ins_need_undo)
6958 {
6959 if (u_save_cursor() == OK)
6960 ins_need_undo = FALSE;
6961 }
6962
6963#ifdef FEAT_FOLDING
6964 /* Always open fold at the cursor line when inserting something. */
6965 foldOpenCursor();
6966#endif
6967
6968 return (arrow_used || ins_need_undo ? FAIL : OK);
6969}
6970
6971/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006972 * Do a few things to stop inserting.
6973 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6974 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 */
6976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006977stop_insert(
6978 pos_T *end_insert_pos,
6979 int esc, /* called by ins_esc() */
6980 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006982 int cc;
6983 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
6985 stop_redo_ins();
6986 replace_flush(); /* abandon replace stack */
6987
6988 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006989 * Save the inserted text for later redo with ^@ and CTRL-A.
6990 * Don't do it when "restart_edit" was set and nothing was inserted,
6991 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006993 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006994 if (did_restart_edit == 0 || (ptr != NULL
6995 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006996 {
6997 vim_free(last_insert);
6998 last_insert = ptr;
6999 last_insert_skip = new_insert_skip;
7000 }
7001 else
7002 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007004 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 {
7006 /* Auto-format now. It may seem strange to do this when stopping an
7007 * insertion (or moving the cursor), but it's required when appending
7008 * a line and having it end in a space. But only do it when something
7009 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007010 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007012 pos_T tpos = curwin->w_cursor;
7013
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 /* When the cursor is at the end of the line after a space the
7015 * formatting will move it to the following word. Avoid that by
7016 * moving the cursor onto the space. */
7017 cc = 'x';
7018 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7019 {
7020 dec_cursor();
7021 cc = gchar_cursor();
7022 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007023 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 }
7025
7026 auto_format(TRUE, FALSE);
7027
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007028 if (vim_iswhite(cc))
7029 {
7030 if (gchar_cursor() != NUL)
7031 inc_cursor();
7032#ifdef FEAT_VIRTUALEDIT
7033 /* If the cursor is still at the same character, also keep
7034 * the "coladd". */
7035 if (gchar_cursor() == NUL
7036 && curwin->w_cursor.lnum == tpos.lnum
7037 && curwin->w_cursor.col == tpos.col)
7038 curwin->w_cursor.coladd = tpos.coladd;
7039#endif
7040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 }
7042
7043 /* If a space was inserted for auto-formatting, remove it now. */
7044 check_auto_format(TRUE);
7045
7046 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007047 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007048 * Do this when ESC was used or moving the cursor up/down.
7049 * Check for the old position still being valid, just in case the text
7050 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007051 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007052 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7053 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007055 pos_T tpos = curwin->w_cursor;
7056
7057 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007058 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007059 for (;;)
7060 {
7061 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7062 --curwin->w_cursor.col;
7063 cc = gchar_cursor();
7064 if (!vim_iswhite(cc))
7065 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007066 if (del_char(TRUE) == FAIL)
7067 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007068 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007069 if (curwin->w_cursor.lnum != tpos.lnum)
7070 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007071 else
7072 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007073 /* reset tpos, could have been invalidated in the loop above */
7074 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007075 tpos.col++;
7076 if (cc != NUL && gchar_pos(&tpos) == NUL)
7077 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 /* <C-S-Right> may have started Visual mode, adjust the position for
7081 * deleted characters. */
7082 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7083 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007084 int len = (int)STRLEN(ml_get_curline());
7085
7086 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007088 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007089#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 }
7093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 }
7095 }
7096 did_ai = FALSE;
7097#ifdef FEAT_SMARTINDENT
7098 did_si = FALSE;
7099 can_si = FALSE;
7100 can_si_back = FALSE;
7101#endif
7102
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007103 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7104 * now in a different buffer. */
7105 if (end_insert_pos != NULL)
7106 {
7107 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007108 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007109 curbuf->b_op_end = *end_insert_pos;
7110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111}
7112
7113/*
7114 * Set the last inserted text to a single character.
7115 * Used for the replace command.
7116 */
7117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007118set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119{
7120 char_u *s;
7121
7122 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 if (last_insert != NULL)
7125 {
7126 s = last_insert;
7127 /* Use the CTRL-V only when entering a special char */
7128 if (c < ' ' || c == DEL)
7129 *s++ = Ctrl_V;
7130 s = add_char2buf(c, s);
7131 *s++ = ESC;
7132 *s++ = NUL;
7133 last_insert_skip = 0;
7134 }
7135}
7136
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007137#if defined(EXITFREE) || defined(PROTO)
7138 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007139free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007140{
7141 vim_free(last_insert);
7142 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007143# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007144 vim_free(compl_orig_text);
7145 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007146# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007147}
7148#endif
7149
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150/*
7151 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7152 * and CSI. Handle multi-byte characters.
7153 * Returns a pointer to after the added bytes.
7154 */
7155 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007156add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157{
7158#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007159 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 int i;
7161 int len;
7162
7163 len = (*mb_char2bytes)(c, temp);
7164 for (i = 0; i < len; ++i)
7165 {
7166 c = temp[i];
7167#endif
7168 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7169 if (c == K_SPECIAL)
7170 {
7171 *s++ = K_SPECIAL;
7172 *s++ = KS_SPECIAL;
7173 *s++ = KE_FILLER;
7174 }
7175#ifdef FEAT_GUI
7176 else if (c == CSI)
7177 {
7178 *s++ = CSI;
7179 *s++ = KS_EXTRA;
7180 *s++ = (int)KE_CSI;
7181 }
7182#endif
7183 else
7184 *s++ = c;
7185#ifdef FEAT_MBYTE
7186 }
7187#endif
7188 return s;
7189}
7190
7191/*
7192 * move cursor to start of line
7193 * if flags & BL_WHITE move to first non-white
7194 * if flags & BL_SOL move to first non-white if startofline is set,
7195 * otherwise keep "curswant" column
7196 * if flags & BL_FIX don't leave the cursor on a NUL.
7197 */
7198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007199beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 if ((flags & BL_SOL) && !p_sol)
7202 coladvance(curwin->w_curswant);
7203 else
7204 {
7205 curwin->w_cursor.col = 0;
7206#ifdef FEAT_VIRTUALEDIT
7207 curwin->w_cursor.coladd = 0;
7208#endif
7209
7210 if (flags & (BL_WHITE | BL_SOL))
7211 {
7212 char_u *ptr;
7213
7214 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7215 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7216 ++curwin->w_cursor.col;
7217 }
7218 curwin->w_set_curswant = TRUE;
7219 }
7220}
7221
7222/*
7223 * oneright oneleft cursor_down cursor_up
7224 *
7225 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007226 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 * Return OK when successful, FAIL when we hit a line of file boundary.
7228 */
7229
7230 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007231oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
7233 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235
7236#ifdef FEAT_VIRTUALEDIT
7237 if (virtual_active())
7238 {
7239 pos_T prevpos = curwin->w_cursor;
7240
7241 /* Adjust for multi-wide char (excluding TAB) */
7242 ptr = ml_get_cursor();
7243 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007244# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007246# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 ))
7250 ? ptr2cells(ptr) : 1));
7251 curwin->w_set_curswant = TRUE;
7252 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7253 return (prevpos.col != curwin->w_cursor.col
7254 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7255 }
7256#endif
7257
7258 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007259 if (*ptr == NUL)
7260 return FAIL; /* already at the very end */
7261
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007263 if (has_mbyte)
7264 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 else
7266#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007267 l = 1;
7268
7269 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7270 * contains "onemore". */
7271 if (ptr[l] == NUL
7272#ifdef FEAT_VIRTUALEDIT
7273 && (ve_flags & VE_ONEMORE) == 0
7274#endif
7275 )
7276 return FAIL;
7277 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278
7279 curwin->w_set_curswant = TRUE;
7280 return OK;
7281}
7282
7283 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007284oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285{
7286#ifdef FEAT_VIRTUALEDIT
7287 if (virtual_active())
7288 {
7289 int width;
7290 int v = getviscol();
7291
7292 if (v == 0)
7293 return FAIL;
7294
7295# ifdef FEAT_LINEBREAK
7296 /* We might get stuck on 'showbreak', skip over it. */
7297 width = 1;
7298 for (;;)
7299 {
7300 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007301 /* getviscol() is slow, skip it when 'showbreak' is empty,
7302 * 'breakindent' is not set and there are no multi-byte
7303 * characters */
7304 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305# ifdef FEAT_MBYTE
7306 && !has_mbyte
7307# endif
7308 ) || getviscol() < v)
7309 break;
7310 ++width;
7311 }
7312# else
7313 coladvance(v - 1);
7314# endif
7315
7316 if (curwin->w_cursor.coladd == 1)
7317 {
7318 char_u *ptr;
7319
7320 /* Adjust for multi-wide char (not a TAB) */
7321 ptr = ml_get_cursor();
7322 if (*ptr != TAB && vim_isprintc(
7323# ifdef FEAT_MBYTE
7324 (*mb_ptr2char)(ptr)
7325# else
7326 *ptr
7327# endif
7328 ) && ptr2cells(ptr) > 1)
7329 curwin->w_cursor.coladd = 0;
7330 }
7331
7332 curwin->w_set_curswant = TRUE;
7333 return OK;
7334 }
7335#endif
7336
7337 if (curwin->w_cursor.col == 0)
7338 return FAIL;
7339
7340 curwin->w_set_curswant = TRUE;
7341 --curwin->w_cursor.col;
7342
7343#ifdef FEAT_MBYTE
7344 /* if the character on the left of the current cursor is a multi-byte
7345 * character, move to its first byte */
7346 if (has_mbyte)
7347 mb_adjust_cursor();
7348#endif
7349 return OK;
7350}
7351
7352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007353cursor_up(
7354 long n,
7355 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356{
7357 linenr_T lnum;
7358
7359 if (n > 0)
7360 {
7361 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007362 /* This fails if the cursor is already in the first line or the count
7363 * is larger than the line number and '-' is in 'cpoptions' */
7364 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 return FAIL;
7366 if (n >= lnum)
7367 lnum = 1;
7368 else
7369#ifdef FEAT_FOLDING
7370 if (hasAnyFolding(curwin))
7371 {
7372 /*
7373 * Count each sequence of folded lines as one logical line.
7374 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007375 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 (void)hasFolding(lnum, &lnum, NULL);
7377
7378 while (n--)
7379 {
7380 /* move up one line */
7381 --lnum;
7382 if (lnum <= 1)
7383 break;
7384 /* If we entered a fold, move to the beginning, unless in
7385 * Insert mode or when 'foldopen' contains "all": it will open
7386 * in a moment. */
7387 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7388 (void)hasFolding(lnum, &lnum, NULL);
7389 }
7390 if (lnum < 1)
7391 lnum = 1;
7392 }
7393 else
7394#endif
7395 lnum -= n;
7396 curwin->w_cursor.lnum = lnum;
7397 }
7398
7399 /* try to advance to the column we want to be at */
7400 coladvance(curwin->w_curswant);
7401
7402 if (upd_topline)
7403 update_topline(); /* make sure curwin->w_topline is valid */
7404
7405 return OK;
7406}
7407
7408/*
7409 * Cursor down a number of logical lines.
7410 */
7411 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007412cursor_down(
7413 long n,
7414 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 linenr_T lnum;
7417
7418 if (n > 0)
7419 {
7420 lnum = curwin->w_cursor.lnum;
7421#ifdef FEAT_FOLDING
7422 /* Move to last line of fold, will fail if it's the end-of-file. */
7423 (void)hasFolding(lnum, NULL, &lnum);
7424#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007425 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007426 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007427 if (lnum >= curbuf->b_ml.ml_line_count
7428 || (lnum + n > curbuf->b_ml.ml_line_count
7429 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 return FAIL;
7431 if (lnum + n >= curbuf->b_ml.ml_line_count)
7432 lnum = curbuf->b_ml.ml_line_count;
7433 else
7434#ifdef FEAT_FOLDING
7435 if (hasAnyFolding(curwin))
7436 {
7437 linenr_T last;
7438
7439 /* count each sequence of folded lines as one logical line */
7440 while (n--)
7441 {
7442 if (hasFolding(lnum, NULL, &last))
7443 lnum = last + 1;
7444 else
7445 ++lnum;
7446 if (lnum >= curbuf->b_ml.ml_line_count)
7447 break;
7448 }
7449 if (lnum > curbuf->b_ml.ml_line_count)
7450 lnum = curbuf->b_ml.ml_line_count;
7451 }
7452 else
7453#endif
7454 lnum += n;
7455 curwin->w_cursor.lnum = lnum;
7456 }
7457
7458 /* try to advance to the column we want to be at */
7459 coladvance(curwin->w_curswant);
7460
7461 if (upd_topline)
7462 update_topline(); /* make sure curwin->w_topline is valid */
7463
7464 return OK;
7465}
7466
7467/*
7468 * Stuff the last inserted text in the read buffer.
7469 * Last_insert actually is a copy of the redo buffer, so we
7470 * first have to remove the command.
7471 */
7472 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007473stuff_inserted(
7474 int c, /* Command character to be inserted */
7475 long count, /* Repeat this many times */
7476 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477{
7478 char_u *esc_ptr;
7479 char_u *ptr;
7480 char_u *last_ptr;
7481 char_u last = NUL;
7482
7483 ptr = get_last_insert();
7484 if (ptr == NULL)
7485 {
7486 EMSG(_(e_noinstext));
7487 return FAIL;
7488 }
7489
7490 /* may want to stuff the command character, to start Insert mode */
7491 if (c != NUL)
7492 stuffcharReadbuff(c);
7493 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7494 *esc_ptr = NUL; /* remove the ESC */
7495
7496 /* when the last char is either "0" or "^" it will be quoted if no ESC
7497 * comes after it OR if it will inserted more than once and "ptr"
7498 * starts with ^D. -- Acevedo
7499 */
7500 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7501 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7502 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7503 {
7504 last = *last_ptr;
7505 *last_ptr = NUL;
7506 }
7507
7508 do
7509 {
7510 stuffReadbuff(ptr);
7511 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7512 if (last)
7513 stuffReadbuff((char_u *)(last == '0'
7514 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7515 : IF_EB("\026^", CTRL_V_STR "^")));
7516 }
7517 while (--count > 0);
7518
7519 if (last)
7520 *last_ptr = last;
7521
7522 if (esc_ptr != NULL)
7523 *esc_ptr = ESC; /* put the ESC back */
7524
7525 /* may want to stuff a trailing ESC, to get out of Insert mode */
7526 if (!no_esc)
7527 stuffcharReadbuff(ESC);
7528
7529 return OK;
7530}
7531
7532 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007533get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534{
7535 if (last_insert == NULL)
7536 return NULL;
7537 return last_insert + last_insert_skip;
7538}
7539
7540/*
7541 * Get last inserted string, and remove trailing <Esc>.
7542 * Returns pointer to allocated memory (must be freed) or NULL.
7543 */
7544 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007545get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546{
7547 char_u *s;
7548 int len;
7549
7550 if (last_insert == NULL)
7551 return NULL;
7552 s = vim_strsave(last_insert + last_insert_skip);
7553 if (s != NULL)
7554 {
7555 len = (int)STRLEN(s);
7556 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7557 s[len - 1] = NUL;
7558 }
7559 return s;
7560}
7561
7562/*
7563 * Check the word in front of the cursor for an abbreviation.
7564 * Called when the non-id character "c" has been entered.
7565 * When an abbreviation is recognized it is removed from the text and
7566 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7567 */
7568 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007569echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570{
7571 /* Don't check for abbreviation in paste mode, when disabled and just
7572 * after moving around with cursor keys. */
7573 if (p_paste || no_abbr || arrow_used)
7574 return FALSE;
7575
7576 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7577 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7578}
7579
7580/*
7581 * replace-stack functions
7582 *
7583 * When replacing characters, the replaced characters are remembered for each
7584 * new character. This is used to re-insert the old text when backspacing.
7585 *
7586 * There is a NUL headed list of characters for each character that is
7587 * currently in the file after the insertion point. When BS is used, one NUL
7588 * headed list is put back for the deleted character.
7589 *
7590 * For a newline, there are two NUL headed lists. One contains the characters
7591 * that the NL replaced. The extra one stores the characters after the cursor
7592 * that were deleted (always white space).
7593 *
7594 * Replace_offset is normally 0, in which case replace_push will add a new
7595 * character at the end of the stack. If replace_offset is not 0, that many
7596 * characters will be left on the stack above the newly inserted character.
7597 */
7598
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007599static char_u *replace_stack = NULL;
7600static long replace_stack_nr = 0; /* next entry in replace stack */
7601static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602
7603 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007604replace_push(
7605 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
7607 char_u *p;
7608
7609 if (replace_stack_nr < replace_offset) /* nothing to do */
7610 return;
7611 if (replace_stack_len <= replace_stack_nr)
7612 {
7613 replace_stack_len += 50;
7614 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7615 if (p == NULL) /* out of memory */
7616 {
7617 replace_stack_len -= 50;
7618 return;
7619 }
7620 if (replace_stack != NULL)
7621 {
7622 mch_memmove(p, replace_stack,
7623 (size_t)(replace_stack_nr * sizeof(char_u)));
7624 vim_free(replace_stack);
7625 }
7626 replace_stack = p;
7627 }
7628 p = replace_stack + replace_stack_nr - replace_offset;
7629 if (replace_offset)
7630 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7631 *p = c;
7632 ++replace_stack_nr;
7633}
7634
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007635#if defined(FEAT_MBYTE) || defined(PROTO)
7636/*
7637 * Push a character onto the replace stack. Handles a multi-byte character in
7638 * reverse byte order, so that the first byte is popped off first.
7639 * Return the number of bytes done (includes composing characters).
7640 */
7641 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007642replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007643{
7644 int l = (*mb_ptr2len)(p);
7645 int j;
7646
7647 for (j = l - 1; j >= 0; --j)
7648 replace_push(p[j]);
7649 return l;
7650}
7651#endif
7652
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653/*
7654 * Pop one item from the replace stack.
7655 * return -1 if stack empty
7656 * return replaced character or NUL otherwise
7657 */
7658 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007659replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660{
7661 if (replace_stack_nr == 0)
7662 return -1;
7663 return (int)replace_stack[--replace_stack_nr];
7664}
7665
7666/*
7667 * Join the top two items on the replace stack. This removes to "off"'th NUL
7668 * encountered.
7669 */
7670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007671replace_join(
7672 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673{
7674 int i;
7675
7676 for (i = replace_stack_nr; --i >= 0; )
7677 if (replace_stack[i] == NUL && off-- <= 0)
7678 {
7679 --replace_stack_nr;
7680 mch_memmove(replace_stack + i, replace_stack + i + 1,
7681 (size_t)(replace_stack_nr - i));
7682 return;
7683 }
7684}
7685
7686/*
7687 * Pop bytes from the replace stack until a NUL is found, and insert them
7688 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7689 */
7690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007691replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 int cc;
7694 int oldState = State;
7695
7696 State = NORMAL; /* don't want REPLACE here */
7697 while ((cc = replace_pop()) > 0)
7698 {
7699#ifdef FEAT_MBYTE
7700 mb_replace_pop_ins(cc);
7701#else
7702 ins_char(cc);
7703#endif
7704 dec_cursor();
7705 }
7706 State = oldState;
7707}
7708
7709#ifdef FEAT_MBYTE
7710/*
7711 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7712 * indicates a multi-byte char, pop the other bytes too.
7713 */
7714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007715mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
7717 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007718 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 int i;
7720 int c;
7721
7722 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7723 {
7724 buf[0] = cc;
7725 for (i = 1; i < n; ++i)
7726 buf[i] = replace_pop();
7727 ins_bytes_len(buf, n);
7728 }
7729 else
7730 ins_char(cc);
7731
7732 if (enc_utf8)
7733 /* Handle composing chars. */
7734 for (;;)
7735 {
7736 c = replace_pop();
7737 if (c == -1) /* stack empty */
7738 break;
7739 if ((n = MB_BYTE2LEN(c)) == 1)
7740 {
7741 /* Not a multi-byte char, put it back. */
7742 replace_push(c);
7743 break;
7744 }
7745 else
7746 {
7747 buf[0] = c;
7748 for (i = 1; i < n; ++i)
7749 buf[i] = replace_pop();
7750 if (utf_iscomposing(utf_ptr2char(buf)))
7751 ins_bytes_len(buf, n);
7752 else
7753 {
7754 /* Not a composing char, put it back. */
7755 for (i = n - 1; i >= 0; --i)
7756 replace_push(buf[i]);
7757 break;
7758 }
7759 }
7760 }
7761}
7762#endif
7763
7764/*
7765 * make the replace stack empty
7766 * (called when exiting replace mode)
7767 */
7768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007769replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770{
7771 vim_free(replace_stack);
7772 replace_stack = NULL;
7773 replace_stack_len = 0;
7774 replace_stack_nr = 0;
7775}
7776
7777/*
7778 * Handle doing a BS for one character.
7779 * cc < 0: replace stack empty, just move cursor
7780 * cc == 0: character was inserted, delete it
7781 * cc > 0: character was replaced, put cc (first byte of original char) back
7782 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007783 * When "limit_col" is >= 0, don't delete before this column. Matters when
7784 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 */
7786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007787replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789 int cc;
7790#ifdef FEAT_VREPLACE
7791 int orig_len = 0;
7792 int ins_len;
7793 int orig_vcols = 0;
7794 colnr_T start_vcol;
7795 char_u *p;
7796 int i;
7797 int vcol;
7798#endif
7799
7800 cc = replace_pop();
7801 if (cc > 0)
7802 {
7803#ifdef FEAT_VREPLACE
7804 if (State & VREPLACE_FLAG)
7805 {
7806 /* Get the number of screen cells used by the character we are
7807 * going to delete. */
7808 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7809 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7810 }
7811#endif
7812#ifdef FEAT_MBYTE
7813 if (has_mbyte)
7814 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007815 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816# ifdef FEAT_VREPLACE
7817 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007818 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819# endif
7820 replace_push(cc);
7821 }
7822 else
7823#endif
7824 {
7825 pchar_cursor(cc);
7826#ifdef FEAT_VREPLACE
7827 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007828 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829#endif
7830 }
7831 replace_pop_ins();
7832
7833#ifdef FEAT_VREPLACE
7834 if (State & VREPLACE_FLAG)
7835 {
7836 /* Get the number of screen cells used by the inserted characters */
7837 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007838 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 vcol = start_vcol;
7840 for (i = 0; i < ins_len; ++i)
7841 {
7842 vcol += chartabsize(p + i, vcol);
7843#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007844 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845#endif
7846 }
7847 vcol -= start_vcol;
7848
7849 /* Delete spaces that were inserted after the cursor to keep the
7850 * text aligned. */
7851 curwin->w_cursor.col += ins_len;
7852 while (vcol > orig_vcols && gchar_cursor() == ' ')
7853 {
7854 del_char(FALSE);
7855 ++orig_vcols;
7856 }
7857 curwin->w_cursor.col -= ins_len;
7858 }
7859#endif
7860
7861 /* mark the buffer as changed and prepare for displaying */
7862 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7863 }
7864 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007865 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866}
7867
7868#ifdef FEAT_CINDENT
7869/*
7870 * Return TRUE if C-indenting is on.
7871 */
7872 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007873cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 return (!p_paste && (curbuf->b_p_cin
7876# ifdef FEAT_EVAL
7877 || *curbuf->b_p_inde != NUL
7878# endif
7879 ));
7880}
7881#endif
7882
7883#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7884/*
7885 * Re-indent the current line, based on the current contents of it and the
7886 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7887 * confused what all the part that handles Control-T is doing that I'm not.
7888 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7889 */
7890
7891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007892fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007894 int amount = get_the_indent();
7895
7896 if (amount >= 0)
7897 {
7898 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7899 if (linewhite(curwin->w_cursor.lnum))
7900 did_ai = TRUE; /* delete the indent if the line stays empty */
7901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902}
7903
7904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007905fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
7907 if (p_paste)
7908 return;
7909# ifdef FEAT_LISP
7910 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7911 fixthisline(get_lisp_indent);
7912# endif
7913# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7914 else
7915# endif
7916# ifdef FEAT_CINDENT
7917 if (cindent_on())
7918 do_c_expr_indent();
7919# endif
7920}
7921
7922#endif
7923
7924#ifdef FEAT_CINDENT
7925/*
7926 * return TRUE if 'cinkeys' contains the key "keytyped",
7927 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007928 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7930 *
7931 * "keytyped" can have a few special values:
7932 * KEY_OPEN_FORW
7933 * KEY_OPEN_BACK
7934 * KEY_COMPLETE just finished completion.
7935 *
7936 * If line_is_empty is TRUE accept keys with '0' before them.
7937 */
7938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007939in_cinkeys(
7940 int keytyped,
7941 int when,
7942 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
7944 char_u *look;
7945 int try_match;
7946 int try_match_word;
7947 char_u *p;
7948 char_u *line;
7949 int icase;
7950 int i;
7951
Bram Moolenaar30848942009-12-24 14:46:12 +00007952 if (keytyped == NUL)
7953 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7954 return FALSE;
7955
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956#ifdef FEAT_EVAL
7957 if (*curbuf->b_p_inde != NUL)
7958 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7959 else
7960#endif
7961 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7962 while (*look)
7963 {
7964 /*
7965 * Find out if we want to try a match with this key, depending on
7966 * 'when' and a '*' or '!' before the key.
7967 */
7968 switch (when)
7969 {
7970 case '*': try_match = (*look == '*'); break;
7971 case '!': try_match = (*look == '!'); break;
7972 default: try_match = (*look != '*'); break;
7973 }
7974 if (*look == '*' || *look == '!')
7975 ++look;
7976
7977 /*
7978 * If there is a '0', only accept a match if the line is empty.
7979 * But may still match when typing last char of a word.
7980 */
7981 if (*look == '0')
7982 {
7983 try_match_word = try_match;
7984 if (!line_is_empty)
7985 try_match = FALSE;
7986 ++look;
7987 }
7988 else
7989 try_match_word = FALSE;
7990
7991 /*
7992 * does it look like a control character?
7993 */
7994 if (*look == '^'
7995#ifdef EBCDIC
7996 && (Ctrl_chr(look[1]) != 0)
7997#else
7998 && look[1] >= '?' && look[1] <= '_'
7999#endif
8000 )
8001 {
8002 if (try_match && keytyped == Ctrl_chr(look[1]))
8003 return TRUE;
8004 look += 2;
8005 }
8006 /*
8007 * 'o' means "o" command, open forward.
8008 * 'O' means "O" command, open backward.
8009 */
8010 else if (*look == 'o')
8011 {
8012 if (try_match && keytyped == KEY_OPEN_FORW)
8013 return TRUE;
8014 ++look;
8015 }
8016 else if (*look == 'O')
8017 {
8018 if (try_match && keytyped == KEY_OPEN_BACK)
8019 return TRUE;
8020 ++look;
8021 }
8022
8023 /*
8024 * 'e' means to check for "else" at start of line and just before the
8025 * cursor.
8026 */
8027 else if (*look == 'e')
8028 {
8029 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8030 {
8031 p = ml_get_curline();
8032 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8033 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8034 return TRUE;
8035 }
8036 ++look;
8037 }
8038
8039 /*
8040 * ':' only causes an indent if it is at the end of a label or case
8041 * statement, or when it was before typing the ':' (to fix
8042 * class::method for C++).
8043 */
8044 else if (*look == ':')
8045 {
8046 if (try_match && keytyped == ':')
8047 {
8048 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008049 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008051 /* Need to get the line again after cin_islabel(). */
8052 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 if (curwin->w_cursor.col > 2
8054 && p[curwin->w_cursor.col - 1] == ':'
8055 && p[curwin->w_cursor.col - 2] == ':')
8056 {
8057 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008058 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008059 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 p = ml_get_curline();
8061 p[curwin->w_cursor.col - 1] = ':';
8062 if (i)
8063 return TRUE;
8064 }
8065 }
8066 ++look;
8067 }
8068
8069
8070 /*
8071 * Is it a key in <>, maybe?
8072 */
8073 else if (*look == '<')
8074 {
8075 if (try_match)
8076 {
8077 /*
8078 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8079 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8080 * >, *, : and ! keys if they really really want to.
8081 */
8082 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8083 && keytyped == look[1])
8084 return TRUE;
8085
8086 if (keytyped == get_special_key_code(look + 1))
8087 return TRUE;
8088 }
8089 while (*look && *look != '>')
8090 look++;
8091 while (*look == '>')
8092 look++;
8093 }
8094
8095 /*
8096 * Is it a word: "=word"?
8097 */
8098 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8099 {
8100 ++look;
8101 if (*look == '~')
8102 {
8103 icase = TRUE;
8104 ++look;
8105 }
8106 else
8107 icase = FALSE;
8108 p = vim_strchr(look, ',');
8109 if (p == NULL)
8110 p = look + STRLEN(look);
8111 if ((try_match || try_match_word)
8112 && curwin->w_cursor.col >= (colnr_T)(p - look))
8113 {
8114 int match = FALSE;
8115
8116#ifdef FEAT_INS_EXPAND
8117 if (keytyped == KEY_COMPLETE)
8118 {
8119 char_u *s;
8120
8121 /* Just completed a word, check if it starts with "look".
8122 * search back for the start of a word. */
8123 line = ml_get_curline();
8124# ifdef FEAT_MBYTE
8125 if (has_mbyte)
8126 {
8127 char_u *n;
8128
8129 for (s = line + curwin->w_cursor.col; s > line; s = n)
8130 {
8131 n = mb_prevptr(line, s);
8132 if (!vim_iswordp(n))
8133 break;
8134 }
8135 }
8136 else
8137# endif
8138 for (s = line + curwin->w_cursor.col; s > line; --s)
8139 if (!vim_iswordc(s[-1]))
8140 break;
8141 if (s + (p - look) <= line + curwin->w_cursor.col
8142 && (icase
8143 ? MB_STRNICMP(s, look, p - look)
8144 : STRNCMP(s, look, p - look)) == 0)
8145 match = TRUE;
8146 }
8147 else
8148#endif
8149 /* TODO: multi-byte */
8150 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8151 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8152 {
8153 line = ml_get_cursor();
8154 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8155 || !vim_iswordc(line[-(p - look) - 1]))
8156 && (icase
8157 ? MB_STRNICMP(line - (p - look), look, p - look)
8158 : STRNCMP(line - (p - look), look, p - look))
8159 == 0)
8160 match = TRUE;
8161 }
8162 if (match && try_match_word && !try_match)
8163 {
8164 /* "0=word": Check if there are only blanks before the
8165 * word. */
8166 line = ml_get_curline();
8167 if ((int)(skipwhite(line) - line) !=
8168 (int)(curwin->w_cursor.col - (p - look)))
8169 match = FALSE;
8170 }
8171 if (match)
8172 return TRUE;
8173 }
8174 look = p;
8175 }
8176
8177 /*
8178 * ok, it's a boring generic character.
8179 */
8180 else
8181 {
8182 if (try_match && *look == keytyped)
8183 return TRUE;
8184 ++look;
8185 }
8186
8187 /*
8188 * Skip over ", ".
8189 */
8190 look = skip_to_option_part(look);
8191 }
8192 return FALSE;
8193}
8194#endif /* FEAT_CINDENT */
8195
8196#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8197/*
8198 * Map Hebrew keyboard when in hkmap mode.
8199 */
8200 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008201hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202{
8203 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8204 {
8205 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8206 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8207 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8208 static char_u map[26] =
8209 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8210 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8211 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8212 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8213 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8214 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8215 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8216 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8217 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8218
8219 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8220 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8221 /* '-1'='sofit' */
8222 else if (c == 'x')
8223 return 'X';
8224 else if (c == 'q')
8225 return '\''; /* {geresh}={'} */
8226 else if (c == 246)
8227 return ' '; /* \"o --> ' ' for a german keyboard */
8228 else if (c == 228)
8229 return ' '; /* \"a --> ' ' -- / -- */
8230 else if (c == 252)
8231 return ' '; /* \"u --> ' ' -- / -- */
8232#ifdef EBCDIC
8233 else if (islower(c))
8234#else
8235 /* NOTE: islower() does not do the right thing for us on Linux so we
8236 * do this the same was as 5.7 and previous, so it works correctly on
8237 * all systems. Specifically, the e.g. Delete and Arrow keys are
8238 * munged and won't work if e.g. searching for Hebrew text.
8239 */
8240 else if (c >= 'a' && c <= 'z')
8241#endif
8242 return (int)(map[CharOrdLow(c)] + p_aleph);
8243 else
8244 return c;
8245 }
8246 else
8247 {
8248 switch (c)
8249 {
8250 case '`': return ';';
8251 case '/': return '.';
8252 case '\'': return ',';
8253 case 'q': return '/';
8254 case 'w': return '\'';
8255
8256 /* Hebrew letters - set offset from 'a' */
8257 case ',': c = '{'; break;
8258 case '.': c = 'v'; break;
8259 case ';': c = 't'; break;
8260 default: {
8261 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8262
8263#ifdef EBCDIC
8264 /* see note about islower() above */
8265 if (!islower(c))
8266#else
8267 if (c < 'a' || c > 'z')
8268#endif
8269 return c;
8270 c = str[CharOrdLow(c)];
8271 break;
8272 }
8273 }
8274
8275 return (int)(CharOrdLow(c) + p_aleph);
8276 }
8277}
8278#endif
8279
8280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008281ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282{
8283 int need_redraw = FALSE;
8284 int regname;
8285 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008286 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287
8288 /*
8289 * If we are going to wait for a character, show a '"'.
8290 */
8291 pc_status = PC_STATUS_UNSET;
8292 if (redrawing() && !char_avail())
8293 {
8294 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008295 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296
8297 edit_putchar('"', TRUE);
8298#ifdef FEAT_CMDL_INFO
8299 add_to_showcmd_c(Ctrl_R);
8300#endif
8301 }
8302
8303#ifdef USE_ON_FLY_SCROLL
8304 dont_scroll = TRUE; /* disallow scrolling here */
8305#endif
8306
8307 /*
8308 * Don't map the register name. This also prevents the mode message to be
8309 * deleted when ESC is hit.
8310 */
8311 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008312 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8315 {
8316 /* Get a third key for literal register insertion */
8317 literally = regname;
8318#ifdef FEAT_CMDL_INFO
8319 add_to_showcmd_c(literally);
8320#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008321 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 }
8324 --no_mapping;
8325
8326#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008327 /* Don't call u_sync() while typing the expression or giving an error
8328 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 ++no_u_sync;
8330 if (regname == '=')
8331 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008332# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008334# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008335 /* Sync undo when evaluating the expression calls setline() or
8336 * append(), so that it can be undone separately. */
8337 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008338
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008340# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 /* Restore the Input Method. */
8342 if (im_on)
8343 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008346 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8347 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008348 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 else
8352 {
8353#endif
8354 if (literally == Ctrl_O || literally == Ctrl_P)
8355 {
8356 /* Append the command to the redo buffer. */
8357 AppendCharToRedobuff(Ctrl_R);
8358 AppendCharToRedobuff(literally);
8359 AppendCharToRedobuff(regname);
8360
8361 do_put(regname, BACKWARD, 1L,
8362 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8363 }
8364 else if (insert_reg(regname, literally) == FAIL)
8365 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008366 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 need_redraw = TRUE; /* remove the '"' */
8368 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008369 else if (stop_insert_mode)
8370 /* When the '=' register was used and a function was invoked that
8371 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8372 * insert anything, need to remove the '"' */
8373 need_redraw = TRUE;
8374
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375#ifdef FEAT_EVAL
8376 }
8377 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008378 if (u_sync_once == 1)
8379 ins_need_undo = TRUE;
8380 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381#endif
8382#ifdef FEAT_CMDL_INFO
8383 clear_showcmd();
8384#endif
8385
8386 /* If the inserted register is empty, we need to remove the '"' */
8387 if (need_redraw || stuff_empty())
8388 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008389
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008390 /* Disallow starting Visual mode here, would get a weird mode. */
8391 if (!vis_active && VIsual_active)
8392 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393}
8394
8395/*
8396 * CTRL-G commands in Insert mode.
8397 */
8398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008399ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400{
8401 int c;
8402
8403#ifdef FEAT_INS_EXPAND
8404 /* Right after CTRL-X the cursor will be after the ruler. */
8405 setcursor();
8406#endif
8407
8408 /*
8409 * Don't map the second key. This also prevents the mode message to be
8410 * deleted when ESC is hit.
8411 */
8412 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008413 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 --no_mapping;
8415 switch (c)
8416 {
8417 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8418 case K_UP:
8419 case Ctrl_K:
8420 case 'k': ins_up(TRUE);
8421 break;
8422
8423 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8424 case K_DOWN:
8425 case Ctrl_J:
8426 case 'j': ins_down(TRUE);
8427 break;
8428
8429 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008430 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008432
8433 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008434 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008435 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008436 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 break;
8438
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008439 /* CTRL-G U: do not break undo with the next char */
8440 case 'U':
8441 /* Allow one left/right cursor movement with the next char,
8442 * without breaking undo. */
8443 dont_sync_undo = MAYBE;
8444 break;
8445
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008447 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 }
8449}
8450
8451/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008452 * CTRL-^ in Insert mode.
8453 */
8454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008455ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008456{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008457 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008458 {
8459 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8460 if (State & LANGMAP)
8461 {
8462 curbuf->b_p_iminsert = B_IMODE_NONE;
8463 State &= ~LANGMAP;
8464 }
8465 else
8466 {
8467 curbuf->b_p_iminsert = B_IMODE_LMAP;
8468 State |= LANGMAP;
8469#ifdef USE_IM_CONTROL
8470 im_set_active(FALSE);
8471#endif
8472 }
8473 }
8474#ifdef USE_IM_CONTROL
8475 else
8476 {
8477 /* There are no ":lmap" mappings, toggle IM */
8478 if (im_get_status())
8479 {
8480 curbuf->b_p_iminsert = B_IMODE_NONE;
8481 im_set_active(FALSE);
8482 }
8483 else
8484 {
8485 curbuf->b_p_iminsert = B_IMODE_IM;
8486 State &= ~LANGMAP;
8487 im_set_active(TRUE);
8488 }
8489 }
8490#endif
8491 set_iminsert_global();
8492 showmode();
8493#ifdef FEAT_GUI
8494 /* may show different cursor shape or color */
8495 if (gui.in_use)
8496 gui_update_cursor(TRUE, FALSE);
8497#endif
8498#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8499 /* Show/unshow value of 'keymap' in status lines. */
8500 status_redraw_curbuf();
8501#endif
8502}
8503
8504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 * Handle ESC in insert mode.
8506 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8507 * insert.
8508 */
8509 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008510ins_esc(
8511 long *count,
8512 int cmdchar,
8513 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
8515 int temp;
8516 static int disabled_redraw = FALSE;
8517
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008518#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008519 check_spell_redraw();
8520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521#if defined(FEAT_HANGULIN)
8522# if defined(ESC_CHG_TO_ENG_MODE)
8523 hangul_input_state_set(0);
8524# endif
8525 if (composing_hangul)
8526 {
8527 push_raw_key(composing_hangul_buffer, 2);
8528 composing_hangul = 0;
8529 }
8530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531
8532 temp = curwin->w_cursor.col;
8533 if (disabled_redraw)
8534 {
8535 --RedrawingDisabled;
8536 disabled_redraw = FALSE;
8537 }
8538 if (!arrow_used)
8539 {
8540 /*
8541 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008542 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8543 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 */
8545 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008546 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547
8548 /*
8549 * Repeating insert may take a long time. Check for
8550 * interrupt now and then.
8551 */
8552 if (*count > 0)
8553 {
8554 line_breakcheck();
8555 if (got_int)
8556 *count = 0;
8557 }
8558
8559 if (--*count > 0) /* repeat what was typed */
8560 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008561 /* Vi repeats the insert without replacing characters. */
8562 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8563 State &= ~REPLACE_FLAG;
8564
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 (void)start_redo_ins();
8566 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008567 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 ++RedrawingDisabled;
8569 disabled_redraw = TRUE;
8570 return FALSE; /* repeat the insert */
8571 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008572 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 undisplay_dollar();
8574 }
8575
8576 /* When an autoindent was removed, curswant stays after the
8577 * indent */
8578 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8579 curwin->w_set_curswant = TRUE;
8580
8581 /* Remember the last Insert position in the '^ mark. */
8582 if (!cmdmod.keepjumps)
8583 curbuf->b_last_insert = curwin->w_cursor;
8584
8585 /*
8586 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008587 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008589 if (!nomove
8590 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591#ifdef FEAT_VIRTUALEDIT
8592 || curwin->w_cursor.coladd > 0
8593#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008594 )
8595 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008596 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597#ifdef FEAT_RIGHTLEFT
8598 && !revins_on
8599#endif
8600 )
8601 {
8602#ifdef FEAT_VIRTUALEDIT
8603 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8604 {
8605 oneleft();
8606 if (restart_edit != NUL)
8607 ++curwin->w_cursor.coladd;
8608 }
8609 else
8610#endif
8611 {
8612 --curwin->w_cursor.col;
8613#ifdef FEAT_MBYTE
8614 /* Correct cursor for multi-byte character. */
8615 if (has_mbyte)
8616 mb_adjust_cursor();
8617#endif
8618 }
8619 }
8620
8621#ifdef USE_IM_CONTROL
8622 /* Disable IM to allow typing English directly for Normal mode commands.
8623 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8624 * well). */
8625 if (!(State & LANGMAP))
8626 im_save_status(&curbuf->b_p_iminsert);
8627 im_set_active(FALSE);
8628#endif
8629
8630 State = NORMAL;
8631 /* need to position cursor again (e.g. when on a TAB ) */
8632 changed_cline_bef_curs();
8633
8634#ifdef FEAT_MOUSE
8635 setmouse();
8636#endif
8637#ifdef CURSOR_SHAPE
8638 ui_cursor_shape(); /* may show different cursor shape */
8639#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008640 if (!p_ek)
8641 /* Re-enable bracketed paste mode. */
8642 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643
8644 /*
8645 * When recording or for CTRL-O, need to display the new mode.
8646 * Otherwise remove the mode message.
8647 */
8648 if (Recording || restart_edit != NUL)
8649 showmode();
8650 else if (p_smd)
8651 MSG("");
8652
8653 return TRUE; /* exit Insert mode */
8654}
8655
8656#ifdef FEAT_RIGHTLEFT
8657/*
8658 * Toggle language: hkmap and revins_on.
8659 * Move to end of reverse inserted text.
8660 */
8661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008662ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663{
8664 if (revins_on && revins_chars && revins_scol >= 0)
8665 {
8666 while (gchar_cursor() != NUL && revins_chars--)
8667 ++curwin->w_cursor.col;
8668 }
8669 p_ri = !p_ri;
8670 revins_on = (State == INSERT && p_ri);
8671 if (revins_on)
8672 {
8673 revins_scol = curwin->w_cursor.col;
8674 revins_legal++;
8675 revins_chars = 0;
8676 undisplay_dollar();
8677 }
8678 else
8679 revins_scol = -1;
8680#ifdef FEAT_FKMAP
8681 if (p_altkeymap)
8682 {
8683 /*
8684 * to be consistent also for redo command, using '.'
8685 * set arrow_used to true and stop it - causing to redo
8686 * characters entered in one mode (normal/reverse insert).
8687 */
8688 arrow_used = TRUE;
8689 (void)stop_arrow();
8690 p_fkmap = curwin->w_p_rl ^ p_ri;
8691 if (p_fkmap && p_ri)
8692 State = INSERT;
8693 }
8694 else
8695#endif
8696 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8697 showmode();
8698}
8699#endif
8700
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701/*
8702 * If 'keymodel' contains "startsel", may start selection.
8703 * Returns TRUE when a CTRL-O and other keys stuffed.
8704 */
8705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008706ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707{
8708 if (km_startsel)
8709 switch (c)
8710 {
8711 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 case K_PAGEUP:
8714 case K_KPAGEUP:
8715 case K_PAGEDOWN:
8716 case K_KPAGEDOWN:
8717# ifdef MACOS
8718 case K_LEFT:
8719 case K_RIGHT:
8720 case K_UP:
8721 case K_DOWN:
8722 case K_END:
8723 case K_HOME:
8724# endif
8725 if (!(mod_mask & MOD_MASK_SHIFT))
8726 break;
8727 /* FALLTHROUGH */
8728 case K_S_LEFT:
8729 case K_S_RIGHT:
8730 case K_S_UP:
8731 case K_S_DOWN:
8732 case K_S_END:
8733 case K_S_HOME:
8734 /* Start selection right away, the cursor can move with
8735 * CTRL-O when beyond the end of the line. */
8736 start_selection();
8737
8738 /* Execute the key in (insert) Select mode. */
8739 stuffcharReadbuff(Ctrl_O);
8740 if (mod_mask)
8741 {
8742 char_u buf[4];
8743
8744 buf[0] = K_SPECIAL;
8745 buf[1] = KS_MODIFIER;
8746 buf[2] = mod_mask;
8747 buf[3] = NUL;
8748 stuffReadbuff(buf);
8749 }
8750 stuffcharReadbuff(c);
8751 return TRUE;
8752 }
8753 return FALSE;
8754}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755
8756/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008757 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008758 */
8759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008760ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008761{
8762#ifdef FEAT_FKMAP
8763 if (p_fkmap && p_ri)
8764 {
8765 beep_flush();
8766 EMSG(farsi_text_3); /* encoded in Farsi */
8767 return;
8768 }
8769#endif
8770
8771#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008772# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008773 set_vim_var_string(VV_INSERTMODE,
8774 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008775# ifdef FEAT_VREPLACE
8776 replaceState == VREPLACE ? "v" :
8777# endif
8778 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008779# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008780 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8781#endif
8782 if (State & REPLACE_FLAG)
8783 State = INSERT | (State & LANGMAP);
8784 else
8785 State = replaceState | (State & LANGMAP);
8786 AppendCharToRedobuff(K_INS);
8787 showmode();
8788#ifdef CURSOR_SHAPE
8789 ui_cursor_shape(); /* may show different cursor shape */
8790#endif
8791}
8792
8793/*
8794 * Pressed CTRL-O in Insert mode.
8795 */
8796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008797ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008798{
8799#ifdef FEAT_VREPLACE
8800 if (State & VREPLACE_FLAG)
8801 restart_edit = 'V';
8802 else
8803#endif
8804 if (State & REPLACE_FLAG)
8805 restart_edit = 'R';
8806 else
8807 restart_edit = 'I';
8808#ifdef FEAT_VIRTUALEDIT
8809 if (virtual_active())
8810 ins_at_eol = FALSE; /* cursor always keeps its column */
8811 else
8812#endif
8813 ins_at_eol = (gchar_cursor() == NUL);
8814}
8815
8816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 * If the cursor is on an indent, ^T/^D insert/delete one
8818 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008819 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 * with vi. But vi only supports ^T and ^D after an
8821 * autoindent, we support it everywhere.
8822 */
8823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008824ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825{
8826 if (stop_arrow() == FAIL)
8827 return;
8828 AppendCharToRedobuff(c);
8829
8830 /*
8831 * 0^D and ^^D: remove all indent.
8832 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008833 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8834 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 {
8836 --curwin->w_cursor.col;
8837 (void)del_char(FALSE); /* delete the '^' or '0' */
8838 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8839 if (State & REPLACE_FLAG)
8840 replace_pop_ins();
8841 if (lastc == '^')
8842 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008843 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 }
8845 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008846 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847
8848 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8849 did_ai = FALSE;
8850#ifdef FEAT_SMARTINDENT
8851 did_si = FALSE;
8852 can_si = FALSE;
8853 can_si_back = FALSE;
8854#endif
8855#ifdef FEAT_CINDENT
8856 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8857#endif
8858}
8859
8860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008861ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
8863 int temp;
8864
8865 if (stop_arrow() == FAIL)
8866 return;
8867 if (gchar_cursor() == NUL) /* delete newline */
8868 {
8869 temp = curwin->w_cursor.col;
8870 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008871 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008872 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 else
8874 curwin->w_cursor.col = temp;
8875 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008876 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8877 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 did_ai = FALSE;
8879#ifdef FEAT_SMARTINDENT
8880 did_si = FALSE;
8881 can_si = FALSE;
8882 can_si_back = FALSE;
8883#endif
8884 AppendCharToRedobuff(K_DEL);
8885}
8886
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008887static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008888
8889/*
8890 * Delete one character for ins_bs().
8891 */
8892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008893ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008894{
8895 dec_cursor();
8896 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8897 if (State & REPLACE_FLAG)
8898 {
8899 /* Don't delete characters before the insert point when in
8900 * Replace mode */
8901 if (curwin->w_cursor.lnum != Insstart.lnum
8902 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008903 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008904 }
8905 else
8906 (void)del_char(FALSE);
8907}
8908
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909/*
8910 * Handle Backspace, delete-word and delete-line in Insert mode.
8911 * Return TRUE when backspace was actually used.
8912 */
8913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008914ins_bs(
8915 int c,
8916 int mode,
8917 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918{
8919 linenr_T lnum;
8920 int cc;
8921 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008922 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 colnr_T mincol;
8924 int did_backspace = FALSE;
8925 int in_indent;
8926 int oldState;
8927#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008928 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929#endif
8930
8931 /*
8932 * can't delete anything in an empty file
8933 * can't backup past first character in buffer
8934 * can't backup past starting point unless 'backspace' > 1
8935 * can backup to a previous line if 'backspace' == 0
8936 */
8937 if ( bufempty()
8938 || (
8939#ifdef FEAT_RIGHTLEFT
8940 !revins_on &&
8941#endif
8942 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8943 || (!can_bs(BS_START)
8944 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008945 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8946 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8948 && curwin->w_cursor.col <= ai_col)
8949 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8950 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008951 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 return FALSE;
8953 }
8954
8955 if (stop_arrow() == FAIL)
8956 return FALSE;
8957 in_indent = inindent(0);
8958#ifdef FEAT_CINDENT
8959 if (in_indent)
8960 can_cindent = FALSE;
8961#endif
8962#ifdef FEAT_COMMENTS
8963 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8964#endif
8965#ifdef FEAT_RIGHTLEFT
8966 if (revins_on) /* put cursor after last inserted char */
8967 inc_cursor();
8968#endif
8969
8970#ifdef FEAT_VIRTUALEDIT
8971 /* Virtualedit:
8972 * BACKSPACE_CHAR eats a virtual space
8973 * BACKSPACE_WORD eats all coladd
8974 * BACKSPACE_LINE eats all coladd and keeps going
8975 */
8976 if (curwin->w_cursor.coladd > 0)
8977 {
8978 if (mode == BACKSPACE_CHAR)
8979 {
8980 --curwin->w_cursor.coladd;
8981 return TRUE;
8982 }
8983 if (mode == BACKSPACE_WORD)
8984 {
8985 curwin->w_cursor.coladd = 0;
8986 return TRUE;
8987 }
8988 curwin->w_cursor.coladd = 0;
8989 }
8990#endif
8991
8992 /*
8993 * delete newline!
8994 */
8995 if (curwin->w_cursor.col == 0)
8996 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008997 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008998 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999#ifdef FEAT_RIGHTLEFT
9000 || revins_on
9001#endif
9002 )
9003 {
9004 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9005 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9006 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009007 --Insstart.lnum;
9008 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 }
9010 /*
9011 * In replace mode:
9012 * cc < 0: NL was inserted, delete it
9013 * cc >= 0: NL was replaced, put original characters back
9014 */
9015 cc = -1;
9016 if (State & REPLACE_FLAG)
9017 cc = replace_pop(); /* returns -1 if NL was inserted */
9018 /*
9019 * In replace mode, in the line we started replacing, we only move the
9020 * cursor.
9021 */
9022 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9023 {
9024 dec_cursor();
9025 }
9026 else
9027 {
9028#ifdef FEAT_VREPLACE
9029 if (!(State & VREPLACE_FLAG)
9030 || curwin->w_cursor.lnum > orig_line_count)
9031#endif
9032 {
9033 temp = gchar_cursor(); /* remember current char */
9034 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009035
9036 /* When "aw" is in 'formatoptions' we must delete the space at
9037 * the end of the line, otherwise the line will be broken
9038 * again when auto-formatting. */
9039 if (has_format_option(FO_AUTO)
9040 && has_format_option(FO_WHITE_PAR))
9041 {
9042 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9043 TRUE);
9044 int len;
9045
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009046 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009047 if (len > 0 && ptr[len - 1] == ' ')
9048 ptr[len - 1] = NUL;
9049 }
9050
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009051 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 if (temp == NUL && gchar_cursor() != NUL)
9053 inc_cursor();
9054 }
9055#ifdef FEAT_VREPLACE
9056 else
9057 dec_cursor();
9058#endif
9059
9060 /*
9061 * In REPLACE mode we have to put back the text that was replaced
9062 * by the NL. On the replace stack is first a NUL-terminated
9063 * sequence of characters that were deleted and then the
9064 * characters that NL replaced.
9065 */
9066 if (State & REPLACE_FLAG)
9067 {
9068 /*
9069 * Do the next ins_char() in NORMAL state, to
9070 * prevent ins_char() from replacing characters and
9071 * avoiding showmatch().
9072 */
9073 oldState = State;
9074 State = NORMAL;
9075 /*
9076 * restore characters (blanks) deleted after cursor
9077 */
9078 while (cc > 0)
9079 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009080 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081#ifdef FEAT_MBYTE
9082 mb_replace_pop_ins(cc);
9083#else
9084 ins_char(cc);
9085#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009086 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 cc = replace_pop();
9088 }
9089 /* restore the characters that NL replaced */
9090 replace_pop_ins();
9091 State = oldState;
9092 }
9093 }
9094 did_ai = FALSE;
9095 }
9096 else
9097 {
9098 /*
9099 * Delete character(s) before the cursor.
9100 */
9101#ifdef FEAT_RIGHTLEFT
9102 if (revins_on) /* put cursor on last inserted char */
9103 dec_cursor();
9104#endif
9105 mincol = 0;
9106 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009107 if (mode == BACKSPACE_LINE
9108 && (curbuf->b_p_ai
9109#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009110 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009111#endif
9112 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113#ifdef FEAT_RIGHTLEFT
9114 && !revins_on
9115#endif
9116 )
9117 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009118 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009120 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009122 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 }
9124
9125 /*
9126 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9127 */
9128 if ( mode == BACKSPACE_CHAR
9129 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009130 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009131 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 && (*(ml_get_cursor() - 1) == TAB
9133 || (*(ml_get_cursor() - 1) == ' '
9134 && (!*inserted_space_p
9135 || arrow_used))))))
9136 {
9137 int ts;
9138 colnr_T vcol;
9139 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009140 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141
9142 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009143 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009144 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009146 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 /* Compute the virtual column where we want to be. Since
9148 * 'showbreak' may get in the way, need to get the last column of
9149 * the previous character. */
9150 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009151 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 dec_cursor();
9153 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9154 inc_cursor();
9155 want_vcol = (want_vcol / ts) * ts;
9156
9157 /* delete characters until we are at or before want_vcol */
9158 while (vcol > want_vcol
9159 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009160 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161
9162 /* insert extra spaces until we are at want_vcol */
9163 while (vcol < want_vcol)
9164 {
9165 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009166 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9167 && curwin->w_cursor.col < Insstart_orig.col)
9168 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169
9170#ifdef FEAT_VREPLACE
9171 if (State & VREPLACE_FLAG)
9172 ins_char(' ');
9173 else
9174#endif
9175 {
9176 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009177 if ((State & REPLACE_FLAG))
9178 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 }
9180 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9181 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009182
9183 /* If we are now back where we started delete one character. Can
9184 * happen when using 'sts' and 'linebreak'. */
9185 if (vcol >= start_vcol)
9186 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 }
9188
9189 /*
9190 * Delete upto starting point, start of line or previous word.
9191 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009192 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009194#ifdef FEAT_MBYTE
9195 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009197 if (has_mbyte)
9198 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009200 do
9201 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009203 if (!revins_on) /* put cursor on char to be deleted */
9204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009206
9207 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009209 /* look multi-byte character class */
9210 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009212 prev_cclass = cclass;
9213 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009216
9217 /* start of word? */
9218 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9219 {
9220 mode = BACKSPACE_WORD_NOT_SPACE;
9221 temp = vim_iswordc(cc);
9222 }
9223 /* end of word? */
9224 else if (mode == BACKSPACE_WORD_NOT_SPACE
9225 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9226#ifdef FEAT_MBYTE
9227 || prev_cclass != cclass
9228#endif
9229 ))
9230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009232 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009234 inc_cursor();
9235#ifdef FEAT_RIGHTLEFT
9236 else if (State & REPLACE_FLAG)
9237 dec_cursor();
9238#endif
9239 break;
9240 }
9241 if (State & REPLACE_FLAG)
9242 replace_do_bs(-1);
9243 else
9244 {
9245#ifdef FEAT_MBYTE
9246 if (enc_utf8 && p_deco)
9247 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9248#endif
9249 (void)del_char(FALSE);
9250#ifdef FEAT_MBYTE
9251 /*
9252 * If there are combining characters and 'delcombine' is set
9253 * move the cursor back. Don't back up before the base
9254 * character.
9255 */
9256 if (enc_utf8 && p_deco && cpc[0] != NUL)
9257 inc_cursor();
9258#endif
9259#ifdef FEAT_RIGHTLEFT
9260 if (revins_chars)
9261 {
9262 revins_chars--;
9263 revins_legal++;
9264 }
9265 if (revins_on && gchar_cursor() == NUL)
9266 break;
9267#endif
9268 }
9269 /* Just a single backspace?: */
9270 if (mode == BACKSPACE_CHAR)
9271 break;
9272 } while (
9273#ifdef FEAT_RIGHTLEFT
9274 revins_on ||
9275#endif
9276 (curwin->w_cursor.col > mincol
9277 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9278 || curwin->w_cursor.col != Insstart_orig.col)));
9279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 did_backspace = TRUE;
9281 }
9282#ifdef FEAT_SMARTINDENT
9283 did_si = FALSE;
9284 can_si = FALSE;
9285 can_si_back = FALSE;
9286#endif
9287 if (curwin->w_cursor.col <= 1)
9288 did_ai = FALSE;
9289 /*
9290 * It's a little strange to put backspaces into the redo
9291 * buffer, but it makes auto-indent a lot easier to deal
9292 * with.
9293 */
9294 AppendCharToRedobuff(c);
9295
9296 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009297 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9298 && curwin->w_cursor.col < Insstart_orig.col)
9299 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300
9301 /* vi behaviour: the cursor moves backward but the character that
9302 * was there remains visible
9303 * Vim behaviour: the cursor moves backward and the character that
9304 * was there is erased from the screen.
9305 * We can emulate the vi behaviour by pretending there is a dollar
9306 * displayed even when there isn't.
9307 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009308 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 dollar_vcol = curwin->w_virtcol;
9310
Bram Moolenaarce3be472008-01-14 19:12:28 +00009311#ifdef FEAT_FOLDING
9312 /* When deleting a char the cursor line must never be in a closed fold.
9313 * E.g., when 'foldmethod' is indent and deleting the first non-white
9314 * char before a Tab. */
9315 if (did_backspace)
9316 foldOpenCursor();
9317#endif
9318
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 return did_backspace;
9320}
9321
9322#ifdef FEAT_MOUSE
9323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009324ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325{
9326 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009327 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328
9329# ifdef FEAT_GUI
9330 /* When GUI is active, also move/paste when 'mouse' is empty */
9331 if (!gui.in_use)
9332# endif
9333 if (!mouse_has(MOUSE_INSERT))
9334 return;
9335
9336 undisplay_dollar();
9337 tpos = curwin->w_cursor;
9338 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9339 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009340#ifdef FEAT_WINDOWS
9341 win_T *new_curwin = curwin;
9342
9343 if (curwin != old_curwin && win_valid(old_curwin))
9344 {
9345 /* Mouse took us to another window. We need to go back to the
9346 * previous one to stop insert there properly. */
9347 curwin = old_curwin;
9348 curbuf = curwin->w_buffer;
9349 }
9350#endif
9351 start_arrow(curwin == old_curwin ? &tpos : NULL);
9352#ifdef FEAT_WINDOWS
9353 if (curwin != new_curwin && win_valid(new_curwin))
9354 {
9355 curwin = new_curwin;
9356 curbuf = curwin->w_buffer;
9357 }
9358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359# ifdef FEAT_CINDENT
9360 can_cindent = TRUE;
9361# endif
9362 }
9363
9364#ifdef FEAT_WINDOWS
9365 /* redraw status lines (in case another window became active) */
9366 redraw_statuslines();
9367#endif
9368}
9369
9370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009371ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372{
9373 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009374# if defined(FEAT_WINDOWS)
9375 win_T *old_curwin = curwin;
9376# endif
9377# ifdef FEAT_INS_EXPAND
9378 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379# endif
9380
9381 tpos = curwin->w_cursor;
9382
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009383# ifdef FEAT_WINDOWS
9384 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 {
9386 int row, col;
9387
9388 row = mouse_row;
9389 col = mouse_col;
9390
9391 /* find the window at the pointer coordinates */
9392 curwin = mouse_find_win(&row, &col);
9393 curbuf = curwin->w_buffer;
9394 }
9395 if (curwin == old_curwin)
9396# endif
9397 undisplay_dollar();
9398
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009399# ifdef FEAT_INS_EXPAND
9400 /* Don't scroll the window in which completion is being done. */
9401 if (!pum_visible()
9402# if defined(FEAT_WINDOWS)
9403 || curwin != old_curwin
9404# endif
9405 )
9406# endif
9407 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009408 if (dir == MSCR_DOWN || dir == MSCR_UP)
9409 {
9410 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9411 scroll_redraw(dir,
9412 (long)(curwin->w_botline - curwin->w_topline));
9413 else
9414 scroll_redraw(dir, 3L);
9415 }
9416#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009417 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009418 {
9419 int val, step = 6;
9420
9421 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9422 step = W_WIDTH(curwin);
9423 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9424 if (val < 0)
9425 val = 0;
9426 gui_do_horiz_scroll(val, TRUE);
9427 }
9428#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009429# ifdef FEAT_INS_EXPAND
9430 did_scroll = TRUE;
9431# endif
9432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009434# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 curwin->w_redr_status = TRUE;
9436
9437 curwin = old_curwin;
9438 curbuf = curwin->w_buffer;
9439# endif
9440
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009441# ifdef FEAT_INS_EXPAND
9442 /* The popup menu may overlay the window, need to redraw it.
9443 * TODO: Would be more efficient to only redraw the windows that are
9444 * overlapped by the popup menu. */
9445 if (pum_visible() && did_scroll)
9446 {
9447 redraw_all_later(NOT_VALID);
9448 ins_compl_show_pum();
9449 }
9450# endif
9451
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 if (!equalpos(curwin->w_cursor, tpos))
9453 {
9454 start_arrow(&tpos);
9455# ifdef FEAT_CINDENT
9456 can_cindent = TRUE;
9457# endif
9458 }
9459}
9460#endif
9461
Bram Moolenaarec2da362017-01-21 20:04:22 +01009462/*
9463 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9464 * P_PE literally.
9465 * When "drop" is TRUE then consume the text and drop it.
9466 */
9467 int
9468bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9469{
9470 int c;
9471 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9472 int idx = 0;
9473 char_u *end = find_termcode((char_u *)"PE");
9474 int ret_char = -1;
9475 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009476 int save_paste = p_paste;
9477 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009478
9479 /* If the end code is too long we can't detect it, read everything. */
9480 if (STRLEN(end) >= NUMBUFLEN)
9481 end = NULL;
9482 ++no_mapping;
9483 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009484 p_paste = TRUE;
9485 curbuf->b_p_ai = FALSE;
9486
Bram Moolenaarec2da362017-01-21 20:04:22 +01009487 for (;;)
9488 {
9489 /* When the end is not defined read everything. */
9490 if (end == NULL && vpeekc() == NUL)
9491 break;
9492 c = plain_vgetc();
9493#ifdef FEAT_MBYTE
9494 if (has_mbyte)
9495 idx += (*mb_char2bytes)(c, buf + idx);
9496 else
9497#endif
9498 buf[idx++] = c;
9499 buf[idx] = NUL;
9500 if (end != NUL && STRNCMP(buf, end, idx) == 0)
9501 {
9502 if (end[idx] == NUL)
9503 break; /* Found the end of paste code. */
9504 continue;
9505 }
9506 if (!drop)
9507 {
9508 switch (mode)
9509 {
9510 case PASTE_CMDLINE:
9511 put_on_cmdline(buf, idx, TRUE);
9512 break;
9513
9514 case PASTE_EX:
9515 if (gap != NULL && ga_grow(gap, idx) == OK)
9516 {
9517 mch_memmove((char *)gap->ga_data + gap->ga_len,
9518 buf, (size_t)idx);
9519 gap->ga_len += idx;
9520 }
9521 break;
9522
9523 case PASTE_INSERT:
9524 if (stop_arrow() == OK)
9525 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009526 c = buf[0];
9527 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9528 ins_eol(c);
9529 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009530 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009531 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009532 AppendToRedobuffLit(buf, idx);
9533 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009534 }
9535 break;
9536
9537 case PASTE_ONE_CHAR:
9538 if (ret_char == -1)
9539 {
9540#ifdef FEAT_MBYTE
9541 if (has_mbyte)
9542 ret_char = (*mb_ptr2char)(buf);
9543 else
9544#endif
9545 ret_char = buf[0];
9546 }
9547 break;
9548 }
9549 }
9550 idx = 0;
9551 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009552
Bram Moolenaarec2da362017-01-21 20:04:22 +01009553 --no_mapping;
9554 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009555 p_paste = save_paste;
9556 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009557
9558 return ret_char;
9559}
9560
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009561#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009563ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009564{
9565 /* We will be leaving the current window, unless closing another tab. */
9566 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9567 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9568 {
9569 undisplay_dollar();
9570 start_arrow(&curwin->w_cursor);
9571# ifdef FEAT_CINDENT
9572 can_cindent = TRUE;
9573# endif
9574 }
9575
9576 if (c == K_TABLINE)
9577 goto_tabpage(current_tab);
9578 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009579 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009580 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009581 redraw_statuslines(); /* will redraw the tabline when needed */
9582 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009583}
9584#endif
9585
9586#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009588ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589{
9590 pos_T tpos;
9591
9592 undisplay_dollar();
9593 tpos = curwin->w_cursor;
9594 if (gui_do_scroll())
9595 {
9596 start_arrow(&tpos);
9597# ifdef FEAT_CINDENT
9598 can_cindent = TRUE;
9599# endif
9600 }
9601}
9602
9603 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009604ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605{
9606 pos_T tpos;
9607
9608 undisplay_dollar();
9609 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009610 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 {
9612 start_arrow(&tpos);
9613# ifdef FEAT_CINDENT
9614 can_cindent = TRUE;
9615# endif
9616 }
9617}
9618#endif
9619
9620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009621ins_left(
9622 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624 pos_T tpos;
9625
9626#ifdef FEAT_FOLDING
9627 if ((fdo_flags & FDO_HOR) && KeyTyped)
9628 foldOpenCursor();
9629#endif
9630 undisplay_dollar();
9631 tpos = curwin->w_cursor;
9632 if (oneleft() == OK)
9633 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009634#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9635 /* Only call start_arrow() when not busy with preediting, it will
9636 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9637 if (!im_is_preediting())
9638#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009639 {
9640 start_arrow_with_change(&tpos, end_change);
9641 if (!end_change)
9642 AppendCharToRedobuff(K_LEFT);
9643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644#ifdef FEAT_RIGHTLEFT
9645 /* If exit reversed string, position is fixed */
9646 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9647 revins_legal++;
9648 revins_chars++;
9649#endif
9650 }
9651
9652 /*
9653 * if 'whichwrap' set for cursor in insert mode may go to
9654 * previous line
9655 */
9656 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9657 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009658 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 start_arrow(&tpos);
9660 --(curwin->w_cursor.lnum);
9661 coladvance((colnr_T)MAXCOL);
9662 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9663 }
9664 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009665 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009666 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667}
9668
9669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009670ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671{
9672 pos_T tpos;
9673
9674#ifdef FEAT_FOLDING
9675 if ((fdo_flags & FDO_HOR) && KeyTyped)
9676 foldOpenCursor();
9677#endif
9678 undisplay_dollar();
9679 tpos = curwin->w_cursor;
9680 if (c == K_C_HOME)
9681 curwin->w_cursor.lnum = 1;
9682 curwin->w_cursor.col = 0;
9683#ifdef FEAT_VIRTUALEDIT
9684 curwin->w_cursor.coladd = 0;
9685#endif
9686 curwin->w_curswant = 0;
9687 start_arrow(&tpos);
9688}
9689
9690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009691ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692{
9693 pos_T tpos;
9694
9695#ifdef FEAT_FOLDING
9696 if ((fdo_flags & FDO_HOR) && KeyTyped)
9697 foldOpenCursor();
9698#endif
9699 undisplay_dollar();
9700 tpos = curwin->w_cursor;
9701 if (c == K_C_END)
9702 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9703 coladvance((colnr_T)MAXCOL);
9704 curwin->w_curswant = MAXCOL;
9705
9706 start_arrow(&tpos);
9707}
9708
9709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009710ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711{
9712#ifdef FEAT_FOLDING
9713 if ((fdo_flags & FDO_HOR) && KeyTyped)
9714 foldOpenCursor();
9715#endif
9716 undisplay_dollar();
9717 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9718 {
9719 start_arrow(&curwin->w_cursor);
9720 (void)bck_word(1L, FALSE, FALSE);
9721 curwin->w_set_curswant = TRUE;
9722 }
9723 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009724 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725}
9726
9727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009728ins_right(
9729 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731#ifdef FEAT_FOLDING
9732 if ((fdo_flags & FDO_HOR) && KeyTyped)
9733 foldOpenCursor();
9734#endif
9735 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009736 if (gchar_cursor() != NUL
9737#ifdef FEAT_VIRTUALEDIT
9738 || virtual_active()
9739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 )
9741 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009742 start_arrow_with_change(&curwin->w_cursor, end_change);
9743 if (!end_change)
9744 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 curwin->w_set_curswant = TRUE;
9746#ifdef FEAT_VIRTUALEDIT
9747 if (virtual_active())
9748 oneright();
9749 else
9750#endif
9751 {
9752#ifdef FEAT_MBYTE
9753 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009754 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 else
9756#endif
9757 ++curwin->w_cursor.col;
9758 }
9759
9760#ifdef FEAT_RIGHTLEFT
9761 revins_legal++;
9762 if (revins_chars)
9763 revins_chars--;
9764#endif
9765 }
9766 /* if 'whichwrap' set for cursor in insert mode, may move the
9767 * cursor to the next line */
9768 else if (vim_strchr(p_ww, ']') != NULL
9769 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9770 {
9771 start_arrow(&curwin->w_cursor);
9772 curwin->w_set_curswant = TRUE;
9773 ++curwin->w_cursor.lnum;
9774 curwin->w_cursor.col = 0;
9775 }
9776 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009777 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009778 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779}
9780
9781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009782ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
9784#ifdef FEAT_FOLDING
9785 if ((fdo_flags & FDO_HOR) && KeyTyped)
9786 foldOpenCursor();
9787#endif
9788 undisplay_dollar();
9789 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9790 || gchar_cursor() != NUL)
9791 {
9792 start_arrow(&curwin->w_cursor);
9793 (void)fwd_word(1L, FALSE, 0);
9794 curwin->w_set_curswant = TRUE;
9795 }
9796 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009797 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798}
9799
9800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009801ins_up(
9802 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803{
9804 pos_T tpos;
9805 linenr_T old_topline = curwin->w_topline;
9806#ifdef FEAT_DIFF
9807 int old_topfill = curwin->w_topfill;
9808#endif
9809
9810 undisplay_dollar();
9811 tpos = curwin->w_cursor;
9812 if (cursor_up(1L, TRUE) == OK)
9813 {
9814 if (startcol)
9815 coladvance(getvcol_nolist(&Insstart));
9816 if (old_topline != curwin->w_topline
9817#ifdef FEAT_DIFF
9818 || old_topfill != curwin->w_topfill
9819#endif
9820 )
9821 redraw_later(VALID);
9822 start_arrow(&tpos);
9823#ifdef FEAT_CINDENT
9824 can_cindent = TRUE;
9825#endif
9826 }
9827 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009828 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829}
9830
9831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009832ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833{
9834 pos_T tpos;
9835
9836 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009837
9838#ifdef FEAT_WINDOWS
9839 if (mod_mask & MOD_MASK_CTRL)
9840 {
9841 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009842 if (first_tabpage->tp_next != NULL)
9843 {
9844 start_arrow(&curwin->w_cursor);
9845 goto_tabpage(-1);
9846 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009847 return;
9848 }
9849#endif
9850
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 tpos = curwin->w_cursor;
9852 if (onepage(BACKWARD, 1L) == OK)
9853 {
9854 start_arrow(&tpos);
9855#ifdef FEAT_CINDENT
9856 can_cindent = TRUE;
9857#endif
9858 }
9859 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009860 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861}
9862
9863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009864ins_down(
9865 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
9867 pos_T tpos;
9868 linenr_T old_topline = curwin->w_topline;
9869#ifdef FEAT_DIFF
9870 int old_topfill = curwin->w_topfill;
9871#endif
9872
9873 undisplay_dollar();
9874 tpos = curwin->w_cursor;
9875 if (cursor_down(1L, TRUE) == OK)
9876 {
9877 if (startcol)
9878 coladvance(getvcol_nolist(&Insstart));
9879 if (old_topline != curwin->w_topline
9880#ifdef FEAT_DIFF
9881 || old_topfill != curwin->w_topfill
9882#endif
9883 )
9884 redraw_later(VALID);
9885 start_arrow(&tpos);
9886#ifdef FEAT_CINDENT
9887 can_cindent = TRUE;
9888#endif
9889 }
9890 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009891 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892}
9893
9894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009895ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896{
9897 pos_T tpos;
9898
9899 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009900
9901#ifdef FEAT_WINDOWS
9902 if (mod_mask & MOD_MASK_CTRL)
9903 {
9904 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009905 if (first_tabpage->tp_next != NULL)
9906 {
9907 start_arrow(&curwin->w_cursor);
9908 goto_tabpage(0);
9909 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009910 return;
9911 }
9912#endif
9913
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 tpos = curwin->w_cursor;
9915 if (onepage(FORWARD, 1L) == OK)
9916 {
9917 start_arrow(&tpos);
9918#ifdef FEAT_CINDENT
9919 can_cindent = TRUE;
9920#endif
9921 }
9922 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009923 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924}
9925
9926#ifdef FEAT_DND
9927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009928ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929{
9930 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9931}
9932#endif
9933
9934/*
9935 * Handle TAB in Insert or Replace mode.
9936 * Return TRUE when the TAB needs to be inserted like a normal character.
9937 */
9938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009939ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940{
9941 int ind;
9942 int i;
9943 int temp;
9944
9945 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9946 Insstart_blank_vcol = get_nolist_virtcol();
9947 if (echeck_abbr(TAB + ABBR_OFF))
9948 return FALSE;
9949
9950 ind = inindent(0);
9951#ifdef FEAT_CINDENT
9952 if (ind)
9953 can_cindent = FALSE;
9954#endif
9955
9956 /*
9957 * When nothing special, insert TAB like a normal character
9958 */
9959 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009960 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009961 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 return TRUE;
9963
9964 if (stop_arrow() == FAIL)
9965 return TRUE;
9966
9967 did_ai = FALSE;
9968#ifdef FEAT_SMARTINDENT
9969 did_si = FALSE;
9970 can_si = FALSE;
9971 can_si_back = FALSE;
9972#endif
9973 AppendToRedobuff((char_u *)"\t");
9974
9975 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009976 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009977 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9978 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 else /* otherwise use 'tabstop' */
9980 temp = (int)curbuf->b_p_ts;
9981 temp -= get_nolist_virtcol() % temp;
9982
9983 /*
9984 * Insert the first space with ins_char(). It will delete one char in
9985 * replace mode. Insert the rest with ins_str(); it will not delete any
9986 * chars. For VREPLACE mode, we use ins_char() for all characters.
9987 */
9988 ins_char(' ');
9989 while (--temp > 0)
9990 {
9991#ifdef FEAT_VREPLACE
9992 if (State & VREPLACE_FLAG)
9993 ins_char(' ');
9994 else
9995#endif
9996 {
9997 ins_str((char_u *)" ");
9998 if (State & REPLACE_FLAG) /* no char replaced */
9999 replace_push(NUL);
10000 }
10001 }
10002
10003 /*
10004 * When 'expandtab' not set: Replace spaces by TABs where possible.
10005 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010006 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 {
10008 char_u *ptr;
10009#ifdef FEAT_VREPLACE
10010 char_u *saved_line = NULL; /* init for GCC */
10011 pos_T pos;
10012#endif
10013 pos_T fpos;
10014 pos_T *cursor;
10015 colnr_T want_vcol, vcol;
10016 int change_col = -1;
10017 int save_list = curwin->w_p_list;
10018
10019 /*
10020 * Get the current line. For VREPLACE mode, don't make real changes
10021 * yet, just work on a copy of the line.
10022 */
10023#ifdef FEAT_VREPLACE
10024 if (State & VREPLACE_FLAG)
10025 {
10026 pos = curwin->w_cursor;
10027 cursor = &pos;
10028 saved_line = vim_strsave(ml_get_curline());
10029 if (saved_line == NULL)
10030 return FALSE;
10031 ptr = saved_line + pos.col;
10032 }
10033 else
10034#endif
10035 {
10036 ptr = ml_get_cursor();
10037 cursor = &curwin->w_cursor;
10038 }
10039
10040 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10041 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10042 curwin->w_p_list = FALSE;
10043
10044 /* Find first white before the cursor */
10045 fpos = curwin->w_cursor;
10046 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
10047 {
10048 --fpos.col;
10049 --ptr;
10050 }
10051
10052 /* In Replace mode, don't change characters before the insert point. */
10053 if ((State & REPLACE_FLAG)
10054 && fpos.lnum == Insstart.lnum
10055 && fpos.col < Insstart.col)
10056 {
10057 ptr += Insstart.col - fpos.col;
10058 fpos.col = Insstart.col;
10059 }
10060
10061 /* compute virtual column numbers of first white and cursor */
10062 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10063 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10064
Bram Moolenaar597a4222014-06-25 14:39:50 +020010065 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10066 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 while (vim_iswhite(*ptr))
10068 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010069 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 if (vcol + i > want_vcol)
10071 break;
10072 if (*ptr != TAB)
10073 {
10074 *ptr = TAB;
10075 if (change_col < 0)
10076 {
10077 change_col = fpos.col; /* Column of first change */
10078 /* May have to adjust Insstart */
10079 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10080 Insstart.col = fpos.col;
10081 }
10082 }
10083 ++fpos.col;
10084 ++ptr;
10085 vcol += i;
10086 }
10087
10088 if (change_col >= 0)
10089 {
10090 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010091 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092
10093 /* Skip over the spaces we need. */
10094 while (vcol < want_vcol && *ptr == ' ')
10095 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010096 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 ++ptr;
10098 ++repl_off;
10099 }
10100 if (vcol > want_vcol)
10101 {
10102 /* Must have a char with 'showbreak' just before it. */
10103 --ptr;
10104 --repl_off;
10105 }
10106 fpos.col += repl_off;
10107
10108 /* Delete following spaces. */
10109 i = cursor->col - fpos.col;
10110 if (i > 0)
10111 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010112 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 /* correct replace stack. */
10114 if ((State & REPLACE_FLAG)
10115#ifdef FEAT_VREPLACE
10116 && !(State & VREPLACE_FLAG)
10117#endif
10118 )
10119 for (temp = i; --temp >= 0; )
10120 replace_join(repl_off);
10121 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010122#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010123 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010124 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010125 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010126 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10127 (char_u *)"\t", 1);
10128 }
10129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 cursor->col -= i;
10131
10132#ifdef FEAT_VREPLACE
10133 /*
10134 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10135 * backspacing over the changed spacing and then inserting the new
10136 * spacing.
10137 */
10138 if (State & VREPLACE_FLAG)
10139 {
10140 /* Backspace from real cursor to change_col */
10141 backspace_until_column(change_col);
10142
10143 /* Insert each char in saved_line from changed_col to
10144 * ptr-cursor */
10145 ins_bytes_len(saved_line + change_col,
10146 cursor->col - change_col);
10147 }
10148#endif
10149 }
10150
10151#ifdef FEAT_VREPLACE
10152 if (State & VREPLACE_FLAG)
10153 vim_free(saved_line);
10154#endif
10155 curwin->w_p_list = save_list;
10156 }
10157
10158 return FALSE;
10159}
10160
10161/*
10162 * Handle CR or NL in insert mode.
10163 * Return TRUE when out of memory or can't undo.
10164 */
10165 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010166ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167{
10168 int i;
10169
10170 if (echeck_abbr(c + ABBR_OFF))
10171 return FALSE;
10172 if (stop_arrow() == FAIL)
10173 return TRUE;
10174 undisplay_dollar();
10175
10176 /*
10177 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10178 * character under the cursor. Only push a NUL on the replace stack,
10179 * nothing to put back when the NL is deleted.
10180 */
10181 if ((State & REPLACE_FLAG)
10182#ifdef FEAT_VREPLACE
10183 && !(State & VREPLACE_FLAG)
10184#endif
10185 )
10186 replace_push(NUL);
10187
10188 /*
10189 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10190 * replacing the next line, so we push all of the characters left on the
10191 * line onto the replace stack. This is not done here though, it is done
10192 * in open_line().
10193 */
10194
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010195#ifdef FEAT_VIRTUALEDIT
10196 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10197 * CTRL-O). */
10198 if (virtual_active() && curwin->w_cursor.coladd > 0)
10199 coladvance(getviscol());
10200#endif
10201
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202#ifdef FEAT_RIGHTLEFT
10203# ifdef FEAT_FKMAP
10204 if (p_altkeymap && p_fkmap)
10205 fkmap(NL);
10206# endif
10207 /* NL in reverse insert will always start in the end of
10208 * current line. */
10209 if (revins_on)
10210 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10211#endif
10212
10213 AppendToRedobuff(NL_STR);
10214 i = open_line(FORWARD,
10215#ifdef FEAT_COMMENTS
10216 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10217#endif
10218 0, old_indent);
10219 old_indent = 0;
10220#ifdef FEAT_CINDENT
10221 can_cindent = TRUE;
10222#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010223#ifdef FEAT_FOLDING
10224 /* When inserting a line the cursor line must never be in a closed fold. */
10225 foldOpenCursor();
10226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227
10228 return (!i);
10229}
10230
10231#ifdef FEAT_DIGRAPHS
10232/*
10233 * Handle digraph in insert mode.
10234 * Returns character still to be inserted, or NUL when nothing remaining to be
10235 * done.
10236 */
10237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010238ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239{
10240 int c;
10241 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010242 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243
10244 pc_status = PC_STATUS_UNSET;
10245 if (redrawing() && !char_avail())
10246 {
10247 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010248 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249
10250 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010251 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252#ifdef FEAT_CMDL_INFO
10253 add_to_showcmd_c(Ctrl_K);
10254#endif
10255 }
10256
10257#ifdef USE_ON_FLY_SCROLL
10258 dont_scroll = TRUE; /* disallow scrolling here */
10259#endif
10260
10261 /* don't map the digraph chars. This also prevents the
10262 * mode message to be deleted when ESC is hit */
10263 ++no_mapping;
10264 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010265 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 --no_mapping;
10267 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010268 if (did_putchar)
10269 /* when the line fits in 'columns' the '?' is at the start of the next
10270 * line and will not be removed by the redraw */
10271 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010272
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 if (IS_SPECIAL(c) || mod_mask) /* special key */
10274 {
10275#ifdef FEAT_CMDL_INFO
10276 clear_showcmd();
10277#endif
10278 insert_special(c, TRUE, FALSE);
10279 return NUL;
10280 }
10281 if (c != ESC)
10282 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010283 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 if (redrawing() && !char_avail())
10285 {
10286 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010287 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288
10289 if (char2cells(c) == 1)
10290 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010291 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010293 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 }
10295#ifdef FEAT_CMDL_INFO
10296 add_to_showcmd_c(c);
10297#endif
10298 }
10299 ++no_mapping;
10300 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010301 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 --no_mapping;
10303 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010304 if (did_putchar)
10305 /* when the line fits in 'columns' the '?' is at the start of the
10306 * next line and will not be removed by a redraw */
10307 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 if (cc != ESC)
10309 {
10310 AppendToRedobuff((char_u *)CTRL_V_STR);
10311 c = getdigraph(c, cc, TRUE);
10312#ifdef FEAT_CMDL_INFO
10313 clear_showcmd();
10314#endif
10315 return c;
10316 }
10317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318#ifdef FEAT_CMDL_INFO
10319 clear_showcmd();
10320#endif
10321 return NUL;
10322}
10323#endif
10324
10325/*
10326 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10327 * Returns the char to be inserted, or NUL if none found.
10328 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010329 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010330ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331{
10332 int c;
10333 int temp;
10334 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010335 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336
10337 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10338 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010339 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 return NUL;
10341 }
10342
10343 /* try to advance to the cursor column */
10344 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010345 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 prev_ptr = ptr;
10347 validate_virtcol();
10348 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10349 {
10350 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010351 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 }
10353 if ((colnr_T)temp > curwin->w_virtcol)
10354 ptr = prev_ptr;
10355
10356#ifdef FEAT_MBYTE
10357 c = (*mb_ptr2char)(ptr);
10358#else
10359 c = *ptr;
10360#endif
10361 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010362 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 return c;
10364}
10365
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010366/*
10367 * CTRL-Y or CTRL-E typed in Insert mode.
10368 */
10369 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010370ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010371{
10372 int c = tc;
10373
10374#ifdef FEAT_INS_EXPAND
10375 if (ctrl_x_mode == CTRL_X_SCROLL)
10376 {
10377 if (c == Ctrl_Y)
10378 scrolldown_clamp();
10379 else
10380 scrollup_clamp();
10381 redraw_later(VALID);
10382 }
10383 else
10384#endif
10385 {
10386 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10387 if (c != NUL)
10388 {
10389 long tw_save;
10390
10391 /* The character must be taken literally, insert like it
10392 * was typed after a CTRL-V, and pretend 'textwidth'
10393 * wasn't set. Digits, 'o' and 'x' are special after a
10394 * CTRL-V, don't use it for these. */
10395 if (c < 256 && !isalnum(c))
10396 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10397 tw_save = curbuf->b_p_tw;
10398 curbuf->b_p_tw = -1;
10399 insert_special(c, TRUE, FALSE);
10400 curbuf->b_p_tw = tw_save;
10401#ifdef FEAT_RIGHTLEFT
10402 revins_chars++;
10403 revins_legal++;
10404#endif
10405 c = Ctrl_V; /* pretend CTRL-V is last character */
10406 auto_format(FALSE, TRUE);
10407 }
10408 }
10409 return c;
10410}
10411
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412#ifdef FEAT_SMARTINDENT
10413/*
10414 * Try to do some very smart auto-indenting.
10415 * Used when inserting a "normal" character.
10416 */
10417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010418ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419{
10420 pos_T *pos, old_pos;
10421 char_u *ptr;
10422 int i;
10423 int temp;
10424
10425 /*
10426 * do some very smart indenting when entering '{' or '}'
10427 */
10428 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10429 {
10430 /*
10431 * for '}' set indent equal to indent of line containing matching '{'
10432 */
10433 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10434 {
10435 old_pos = curwin->w_cursor;
10436 /*
10437 * If the matching '{' has a ')' immediately before it (ignoring
10438 * white-space), then line up with the start of the line
10439 * containing the matching '(' if there is one. This handles the
10440 * case where an "if (..\n..) {" statement continues over multiple
10441 * lines -- webb
10442 */
10443 ptr = ml_get(pos->lnum);
10444 i = pos->col;
10445 if (i > 0) /* skip blanks before '{' */
10446 while (--i > 0 && vim_iswhite(ptr[i]))
10447 ;
10448 curwin->w_cursor.lnum = pos->lnum;
10449 curwin->w_cursor.col = i;
10450 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10451 curwin->w_cursor = *pos;
10452 i = get_indent();
10453 curwin->w_cursor = old_pos;
10454#ifdef FEAT_VREPLACE
10455 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010456 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 else
10458#endif
10459 (void)set_indent(i, SIN_CHANGED);
10460 }
10461 else if (curwin->w_cursor.col > 0)
10462 {
10463 /*
10464 * when inserting '{' after "O" reduce indent, but not
10465 * more than indent of previous line
10466 */
10467 temp = TRUE;
10468 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10469 {
10470 old_pos = curwin->w_cursor;
10471 i = get_indent();
10472 while (curwin->w_cursor.lnum > 1)
10473 {
10474 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10475
10476 /* ignore empty lines and lines starting with '#'. */
10477 if (*ptr != '#' && *ptr != NUL)
10478 break;
10479 }
10480 if (get_indent() >= i)
10481 temp = FALSE;
10482 curwin->w_cursor = old_pos;
10483 }
10484 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010485 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 }
10487 }
10488
10489 /*
10490 * set indent of '#' always to 0
10491 */
10492 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10493 {
10494 /* remember current indent for next line */
10495 old_indent = get_indent();
10496 (void)set_indent(0, SIN_CHANGED);
10497 }
10498
10499 /* Adjust ai_col, the char at this position can be deleted. */
10500 if (ai_col > curwin->w_cursor.col)
10501 ai_col = curwin->w_cursor.col;
10502}
10503#endif
10504
10505/*
10506 * Get the value that w_virtcol would have when 'list' is off.
10507 * Unless 'cpo' contains the 'L' flag.
10508 */
10509 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010510get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511{
10512 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10513 return getvcol_nolist(&curwin->w_cursor);
10514 validate_virtcol();
10515 return curwin->w_virtcol;
10516}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010517
10518#ifdef FEAT_AUTOCMD
10519/*
10520 * Handle the InsertCharPre autocommand.
10521 * "c" is the character that was typed.
10522 * Return a pointer to allocated memory with the replacement string.
10523 * Return NULL to continue inserting "c".
10524 */
10525 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010526do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010527{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010528 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010529 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010530
10531 /* Return quickly when there is nothing to do. */
10532 if (!has_insertcharpre())
10533 return NULL;
10534
Bram Moolenaar704984a2012-06-01 14:57:51 +020010535#ifdef FEAT_MBYTE
10536 if (has_mbyte)
10537 buf[(*mb_char2bytes)(c, buf)] = NUL;
10538 else
10539#endif
10540 {
10541 buf[0] = c;
10542 buf[1] = NUL;
10543 }
10544
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010545 /* Lock the text to avoid weird things from happening. */
10546 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010547 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010548
Bram Moolenaar704984a2012-06-01 14:57:51 +020010549 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010550 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010551 {
10552 /* Get the value of v:char. It may be empty or more than one
10553 * character. Only use it when changed, otherwise continue with the
10554 * original character to avoid breaking autoindent. */
10555 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10556 res = vim_strsave(get_vim_var_str(VV_CHAR));
10557 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010558
10559 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10560 --textlock;
10561
10562 return res;
10563}
10564#endif