blob: 47932c916a23e0b4c98106aac02dc4597676ee98 [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()
1671 && 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;
1680 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
3470 * allow the word to be deleted, we won't match everything. */
3471 if ((int)(p - line) - (int)compl_col < 0
3472 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003473 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003474 return K_BS;
3475
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003476 /* Deleted more than what was used to find matches or didn't finish
3477 * finding all matches: need to look for matches all over again. */
3478 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003479 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003480 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003481
Bram Moolenaara6557602006-02-04 22:43:20 +00003482 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003483 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003484 if (compl_leader != NULL)
3485 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003486 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003487 if (compl_shown_match != NULL)
3488 /* Make sure current match is not a hidden item. */
3489 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003490 return NUL;
3491 }
3492 return K_BS;
3493}
Bram Moolenaara6557602006-02-04 22:43:20 +00003494
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003495/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003496 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3497 * be called.
3498 */
3499 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003500ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003501{
3502 /* Return TRUE if we didn't complete finding matches or when the
3503 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3504 return compl_was_interrupted
3505 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3506 && compl_opt_refresh_always);
3507}
3508
3509/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003510 * Called after changing "compl_leader".
3511 * Show the popup menu with a different set of matches.
3512 * May also search for matches again if the previous search was interrupted.
3513 */
3514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003515ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003516{
3517 ins_compl_del_pum();
3518 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003519 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003520 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003521
3522 if (compl_started)
3523 ins_compl_set_original_text(compl_leader);
3524 else
3525 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003526#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003527 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003528#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003529 /*
3530 * Matches were cleared, need to search for them now. First display
3531 * the changed text before the cursor. Set "compl_restarting" to
3532 * avoid that the first match is inserted.
3533 */
3534 update_screen(0);
3535#ifdef FEAT_GUI
3536 if (gui.in_use)
3537 {
3538 /* Show the cursor after the match, not after the redrawn text. */
3539 setcursor();
3540 out_flush();
3541 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003542 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003543#endif
3544 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003545 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003546 compl_cont_status = 0;
3547 compl_restarting = FALSE;
3548 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003549
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003550 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003551
3552 /* Show the popup menu with a different set of matches. */
3553 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003554
3555 /* Don't let Enter select the original text when there is no popup menu. */
3556 if (compl_match_array == NULL)
3557 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003558}
3559
3560/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003561 * Return the length of the completion, from the completion start column to
3562 * the cursor column. Making sure it never goes below zero.
3563 */
3564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003565ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003566{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003567 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003568
3569 if (off < 0)
3570 return 0;
3571 return off;
3572}
3573
3574/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003575 * Append one character to the match leader. May reduce the number of
3576 * matches.
3577 */
3578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003579ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003580{
3581#ifdef FEAT_MBYTE
3582 int cc;
3583
3584 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3585 {
3586 char_u buf[MB_MAXBYTES + 1];
3587
3588 (*mb_char2bytes)(c, buf);
3589 buf[cc] = NUL;
3590 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003591 if (compl_opt_refresh_always)
3592 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003593 }
3594 else
3595#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003596 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003597 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003598 if (compl_opt_refresh_always)
3599 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003600 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003601
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003602 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003603 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003604 ins_compl_restart();
3605
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003606 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003607 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003608 * break redo. */
3609 if (!compl_opt_refresh_always)
3610 {
3611 vim_free(compl_leader);
3612 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003613 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003614 if (compl_leader != NULL)
3615 ins_compl_new_leader();
3616 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003617}
3618
3619/*
3620 * Setup for finding completions again without leaving CTRL-X mode. Used when
3621 * BS or a key was typed while still searching for matches.
3622 */
3623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003624ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003625{
3626 ins_compl_free();
3627 compl_started = FALSE;
3628 compl_matches = 0;
3629 compl_cont_status = 0;
3630 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003631}
3632
3633/*
3634 * Set the first match, the original text.
3635 */
3636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003637ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003638{
3639 char_u *p;
3640
3641 /* Replace the original text entry. */
3642 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3643 {
3644 p = vim_strsave(str);
3645 if (p != NULL)
3646 {
3647 vim_free(compl_first_match->cp_str);
3648 compl_first_match->cp_str = p;
3649 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003650 }
3651}
3652
3653/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003654 * Append one character to the match leader. May reduce the number of
3655 * matches.
3656 */
3657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003658ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003659{
3660 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003661 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003662 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003663 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003664
3665 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003666 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003667 {
3668 /* When still at the original match use the first entry that matches
3669 * the leader. */
3670 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3671 {
3672 p = NULL;
3673 for (cp = compl_shown_match->cp_next; cp != NULL
3674 && cp != compl_first_match; cp = cp->cp_next)
3675 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003676 if (compl_leader == NULL
3677 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003678 (int)STRLEN(compl_leader)))
3679 {
3680 p = cp->cp_str;
3681 break;
3682 }
3683 }
3684 if (p == NULL || (int)STRLEN(p) <= len)
3685 return;
3686 }
3687 else
3688 return;
3689 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003690 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003691 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003692 ins_compl_addleader(c);
3693}
3694
3695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003697 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003698 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003700 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003701ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702{
3703 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003705 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
3707 /* Forget any previous 'special' messages if this is actually
3708 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3709 */
3710 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3711 edit_submode_extra = NULL;
3712
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003713 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003714 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3715 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003716 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003718 /* Set "compl_get_longest" when finding the first matches. */
3719 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3720 || (ctrl_x_mode == 0 && !compl_started))
3721 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003722 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003723 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003724
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003725 }
3726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3728 {
3729 /*
3730 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3731 * it will be yet. Now we decide.
3732 */
3733 switch (c)
3734 {
3735 case Ctrl_E:
3736 case Ctrl_Y:
3737 ctrl_x_mode = CTRL_X_SCROLL;
3738 if (!(State & REPLACE_FLAG))
3739 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3740 else
3741 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3742 edit_submode_pre = NULL;
3743 showmode();
3744 break;
3745 case Ctrl_L:
3746 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3747 break;
3748 case Ctrl_F:
3749 ctrl_x_mode = CTRL_X_FILES;
3750 break;
3751 case Ctrl_K:
3752 ctrl_x_mode = CTRL_X_DICTIONARY;
3753 break;
3754 case Ctrl_R:
3755 /* Simply allow ^R to happen without affecting ^X mode */
3756 break;
3757 case Ctrl_T:
3758 ctrl_x_mode = CTRL_X_THESAURUS;
3759 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003760#ifdef FEAT_COMPL_FUNC
3761 case Ctrl_U:
3762 ctrl_x_mode = CTRL_X_FUNCTION;
3763 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003764 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003765 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003766 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003767#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003768 case 's':
3769 case Ctrl_S:
3770 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003771#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003772 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003773 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003774 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003775#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003776 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 case Ctrl_RSB:
3778 ctrl_x_mode = CTRL_X_TAGS;
3779 break;
3780#ifdef FEAT_FIND_ID
3781 case Ctrl_I:
3782 case K_S_TAB:
3783 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3784 break;
3785 case Ctrl_D:
3786 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3787 break;
3788#endif
3789 case Ctrl_V:
3790 case Ctrl_Q:
3791 ctrl_x_mode = CTRL_X_CMDLINE;
3792 break;
3793 case Ctrl_P:
3794 case Ctrl_N:
3795 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3796 * just started ^X mode, or there were enough ^X's to cancel
3797 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3798 * do normal expansion when interrupting a different mode (say
3799 * ^X^F^X^P or ^P^X^X^P, see below)
3800 * nothing changes if interrupting mode 0, (eg, the flag
3801 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003802 if (!(compl_cont_status & CONT_INTRPT))
3803 compl_cont_status |= CONT_LOCAL;
3804 else if (compl_cont_mode != 0)
3805 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 /* FALLTHROUGH */
3807 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003808 /* If we have typed at least 2 ^X's... for modes != 0, we set
3809 * compl_cont_status = 0 (eg, as if we had just started ^X
3810 * mode).
3811 * For mode 0, we set "compl_cont_mode" to an impossible
3812 * value, in both cases ^X^X can be used to restart the same
3813 * mode (avoiding ADDING mode).
3814 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3815 * 'complete' and local ^P expansions respectively.
3816 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3817 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if (c == Ctrl_X)
3819 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003820 if (compl_cont_mode != 0)
3821 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003823 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825 ctrl_x_mode = 0;
3826 edit_submode = NULL;
3827 showmode();
3828 break;
3829 }
3830 }
3831 else if (ctrl_x_mode != 0)
3832 {
3833 /* We're already in CTRL-X mode, do we stay in it? */
3834 if (!vim_is_ctrl_x_key(c))
3835 {
3836 if (ctrl_x_mode == CTRL_X_SCROLL)
3837 ctrl_x_mode = 0;
3838 else
3839 ctrl_x_mode = CTRL_X_FINISHED;
3840 edit_submode = NULL;
3841 }
3842 showmode();
3843 }
3844
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003845 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
3847 /* Show error message from attempted keyword completion (probably
3848 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003849 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003851 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3852 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 || ctrl_x_mode == CTRL_X_FINISHED)
3854 {
3855 /* Get here when we have finished typing a sequence of ^N and
3856 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003857 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003858 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
3860 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003861 * If any of the original typed text has been changed, eg when
3862 * ignorecase is set, we must add back-spaces to the redo
3863 * buffer. We add as few as necessary to delete just the part
3864 * of the original text that has changed.
3865 * When using the longest match, edited the match or used
3866 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003868 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3869 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003870 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003871 ptr = NULL;
3872 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
3874
3875#ifdef FEAT_CINDENT
3876 want_cindent = (can_cindent && cindent_on());
3877#endif
3878 /*
3879 * When completing whole lines: fix indent for 'cindent'.
3880 * Otherwise, break line if it's too long.
3881 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003882 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
3884#ifdef FEAT_CINDENT
3885 /* re-indent the current line */
3886 if (want_cindent)
3887 {
3888 do_c_expr_indent();
3889 want_cindent = FALSE; /* don't do it again */
3890 }
3891#endif
3892 }
3893 else
3894 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003895 int prev_col = curwin->w_cursor.col;
3896
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003898 if (prev_col > 0)
3899 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003900 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003901 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003903 if (prev_col > 0
3904 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3905 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003908 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003909 * the selection without inserting anything. When
3910 * compl_enter_selects is set the Enter key does the same. */
3911 if ((c == Ctrl_Y || (compl_enter_selects
3912 && (c == CAR || c == K_KENTER || c == NL)))
3913 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003914 retval = TRUE;
3915
Bram Moolenaar47247282016-08-02 22:36:02 +02003916 /* CTRL-E means completion is Ended, go back to the typed text.
3917 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003918 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003919 {
3920 ins_compl_delete();
3921 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003922 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003923 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003924 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003925 retval = TRUE;
3926 }
3927
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003928 auto_format(FALSE, TRUE);
3929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003931 compl_started = FALSE;
3932 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003933 if (!shortmess(SHM_COMPLETIONMENU))
3934 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003936 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 if (edit_submode != NULL)
3938 {
3939 edit_submode = NULL;
3940 showmode();
3941 }
3942
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003943#ifdef FEAT_CMDWIN
3944 if (c == Ctrl_C && cmdwin_type != 0)
3945 /* Avoid the popup menu remains displayed when leaving the
3946 * command line window. */
3947 update_screen(0);
3948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949#ifdef FEAT_CINDENT
3950 /*
3951 * Indent now if a key was typed that is in 'cinkeys'.
3952 */
3953 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3954 do_c_expr_indent();
3955#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003956#ifdef FEAT_AUTOCMD
3957 /* Trigger the CompleteDone event to give scripts a chance to act
3958 * upon the completion. */
3959 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
3962 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003963#ifdef FEAT_AUTOCMD
3964 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3965 /* Trigger the CompleteDone event to give scripts a chance to act
3966 * upon the (possibly failed) completion. */
3967 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970 /* reset continue_* if we left expansion-mode, if we stay they'll be
3971 * (re)set properly in ins_complete() */
3972 if (!vim_is_ctrl_x_key(c))
3973 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003974 compl_cont_status = 0;
3975 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003977
3978 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979}
3980
3981/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003982 * Fix the redo buffer for the completion leader replacing some of the typed
3983 * text. This inserts backspaces and appends the changed text.
3984 * "ptr" is the known leader text or NUL.
3985 */
3986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003987ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003988{
3989 int len;
3990 char_u *p;
3991 char_u *ptr = ptr_arg;
3992
3993 if (ptr == NULL)
3994 {
3995 if (compl_leader != NULL)
3996 ptr = compl_leader;
3997 else
3998 return; /* nothing to do */
3999 }
4000 if (compl_orig_text != NULL)
4001 {
4002 p = compl_orig_text;
4003 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4004 ;
4005#ifdef FEAT_MBYTE
4006 if (len > 0)
4007 len -= (*mb_head_off)(p, p + len);
4008#endif
4009 for (p += len; *p != NUL; mb_ptr_adv(p))
4010 AppendCharToRedobuff(K_BS);
4011 }
4012 else
4013 len = 0;
4014 if (ptr != NULL)
4015 AppendToRedobuffLit(ptr + len, -1);
4016}
4017
4018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4020 * (depending on flag) starting from buf and looking for a non-scanned
4021 * buffer (other than curbuf). curbuf is special, if it is called with
4022 * buf=curbuf then it has to be the first call for a given flag/expansion.
4023 *
4024 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4025 */
4026 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004027ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
4029#ifdef FEAT_WINDOWS
4030 static win_T *wp;
4031#endif
4032
4033 if (flag == 'w') /* just windows */
4034 {
4035#ifdef FEAT_WINDOWS
4036 if (buf == curbuf) /* first call for this flag/expansion */
4037 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004038 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 && wp->w_buffer->b_scanned)
4040 ;
4041 buf = wp->w_buffer;
4042#else
4043 buf = curbuf;
4044#endif
4045 }
4046 else
4047 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4048 * (unlisted buffers)
4049 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004050 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 && ((flag == 'U'
4052 ? buf->b_p_bl
4053 : (!buf->b_p_bl
4054 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004055 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 ;
4057 return buf;
4058}
4059
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004060#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004061/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004062 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004063 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004064 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004066expand_by_function(
4067 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4068 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004069{
Bram Moolenaar82139082011-09-14 16:52:09 +02004070 list_T *matchlist = NULL;
4071 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004072 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004073 char_u *funcname;
4074 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004075 win_T *curwin_save;
4076 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004077 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004078
Bram Moolenaare344bea2005-09-01 20:46:49 +00004079 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4080 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004081 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004082
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004083 /* Call 'completefunc' to obtain the list of matches. */
4084 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004085 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004086
Bram Moolenaare344bea2005-09-01 20:46:49 +00004087 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004088 curwin_save = curwin;
4089 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004090
4091 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004092 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004093 {
4094 switch (rettv.v_type)
4095 {
4096 case VAR_LIST:
4097 matchlist = rettv.vval.v_list;
4098 break;
4099 case VAR_DICT:
4100 matchdict = rettv.vval.v_dict;
4101 break;
4102 default:
4103 /* TODO: Give error message? */
4104 clear_tv(&rettv);
4105 break;
4106 }
4107 }
4108
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004109 if (curwin_save != curwin || curbuf_save != curbuf)
4110 {
4111 EMSG(_(e_complwin));
4112 goto theend;
4113 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004114 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004115 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004116 if (!equalpos(curwin->w_cursor, pos))
4117 {
4118 EMSG(_(e_compldel));
4119 goto theend;
4120 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004121
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004122 if (matchlist != NULL)
4123 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004124 else if (matchdict != NULL)
4125 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004126
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004127theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004128 if (matchdict != NULL)
4129 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004130 if (matchlist != NULL)
4131 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004132}
4133#endif /* FEAT_COMPL_FUNC */
4134
Bram Moolenaar39f05632006-03-19 22:15:26 +00004135#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004136/*
4137 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004138 */
4139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004140ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004141{
4142 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004143 int dir = compl_direction;
4144
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004145 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004146 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004147 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004148 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4149 /* if dir was BACKWARD then honor it just once */
4150 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004151 else if (did_emsg)
4152 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004153 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004154}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004155
4156/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004157 * Add completions from a dict.
4158 */
4159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004160ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004161{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004162 dictitem_T *di_refresh;
4163 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004164
4165 /* Check for optional "refresh" item. */
4166 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004167 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4168 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004169 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004170 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004171
4172 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4173 compl_opt_refresh_always = TRUE;
4174 }
4175
4176 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004177 di_words = dict_find(dict, (char_u *)"words", 5);
4178 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4179 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004180}
4181
4182/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004183 * Add a match to the list of matches from a typeval_T.
4184 * If the given string is already in the list of completions, then return
4185 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4186 * maybe because alloc() returns NULL, then FAIL is returned.
4187 */
4188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004189ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004190{
4191 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004192 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004193 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004194 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004195 char_u *(cptext[CPT_COUNT]);
4196
4197 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4198 {
4199 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4200 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4201 (char_u *)"abbr", FALSE);
4202 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4203 (char_u *)"menu", FALSE);
4204 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4205 (char_u *)"kind", FALSE);
4206 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4207 (char_u *)"info", FALSE);
4208 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4209 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004210 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004211 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004212 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4213 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004214 }
4215 else
4216 {
4217 word = get_tv_string_chk(tv);
4218 vim_memset(cptext, 0, sizeof(cptext));
4219 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004220 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004221 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004222 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004223}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004224#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004225
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004226/*
4227 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004228 * The search starts at position "ini" in curbuf and in the direction
4229 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004230 * When "compl_started" is FALSE start at that position, otherwise continue
4231 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004232 * This may return before finding all the matches.
4233 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 */
4235 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004236ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
4238 static pos_T first_match_pos;
4239 static pos_T last_match_pos;
4240 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241 static int found_all = FALSE; /* Found all matches of a
4242 certain type. */
4243 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
Bram Moolenaar572cb562005-08-05 21:35:02 +00004245 pos_T *pos;
4246 char_u **matches;
4247 int save_p_scs;
4248 int save_p_ws;
4249 int save_p_ic;
4250 int i;
4251 int num_matches;
4252 int len;
4253 int found_new_match;
4254 int type = ctrl_x_mode;
4255 char_u *ptr;
4256 char_u *dict = NULL;
4257 int dict_f = 0;
4258 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004259 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004261 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004263 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 ins_buf->b_scanned = 0;
4265 found_all = FALSE;
4266 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004267 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004268 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 last_match_pos = first_match_pos = *ini;
4270 }
4271
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004273 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4275 for (;;)
4276 {
4277 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004278 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004280 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 * or if found_all says this entry is done. For ^X^L only use the
4282 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004283 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004284 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
4286 found_all = FALSE;
4287 while (*e_cpt == ',' || *e_cpt == ' ')
4288 e_cpt++;
4289 if (*e_cpt == '.' && !curbuf->b_scanned)
4290 {
4291 ins_buf = curbuf;
4292 first_match_pos = *ini;
4293 /* So that ^N can match word immediately after cursor */
4294 if (ctrl_x_mode == 0)
4295 dec(&first_match_pos);
4296 last_match_pos = first_match_pos;
4297 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004298
4299 /* Remember the first match so that the loop stops when we
4300 * wrap and come back there a second time. */
4301 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4304 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4305 {
4306 /* Scan a buffer, but not the current one. */
4307 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4308 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004309 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 first_match_pos.col = last_match_pos.col = 0;
4311 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4312 last_match_pos.lnum = 0;
4313 type = 0;
4314 }
4315 else /* unloaded buffer, scan like dictionary */
4316 {
4317 found_all = TRUE;
4318 if (ins_buf->b_fname == NULL)
4319 continue;
4320 type = CTRL_X_DICTIONARY;
4321 dict = ins_buf->b_fname;
4322 dict_f = DICT_EXACT;
4323 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004324 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 ins_buf->b_fname == NULL
4326 ? buf_spname(ins_buf)
4327 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004328 ? ins_buf->b_fname
4329 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004330 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332 else if (*e_cpt == NUL)
4333 break;
4334 else
4335 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004336 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 type = -1;
4338 else if (*e_cpt == 'k' || *e_cpt == 's')
4339 {
4340 if (*e_cpt == 'k')
4341 type = CTRL_X_DICTIONARY;
4342 else
4343 type = CTRL_X_THESAURUS;
4344 if (*++e_cpt != ',' && *e_cpt != NUL)
4345 {
4346 dict = e_cpt;
4347 dict_f = DICT_FIRST;
4348 }
4349 }
4350#ifdef FEAT_FIND_ID
4351 else if (*e_cpt == 'i')
4352 type = CTRL_X_PATH_PATTERNS;
4353 else if (*e_cpt == 'd')
4354 type = CTRL_X_PATH_DEFINES;
4355#endif
4356 else if (*e_cpt == ']' || *e_cpt == 't')
4357 {
4358 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004359 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004360 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 else
4363 type = -1;
4364
4365 /* in any case e_cpt is advanced to the next entry */
4366 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4367
4368 found_all = TRUE;
4369 if (type == -1)
4370 continue;
4371 }
4372 }
4373
4374 switch (type)
4375 {
4376 case -1:
4377 break;
4378#ifdef FEAT_FIND_ID
4379 case CTRL_X_PATH_PATTERNS:
4380 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004381 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004382 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004384 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4386 (linenr_T)1, (linenr_T)MAXLNUM);
4387 break;
4388#endif
4389
4390 case CTRL_X_DICTIONARY:
4391 case CTRL_X_THESAURUS:
4392 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004393 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 : (type == CTRL_X_THESAURUS
4395 ? (*curbuf->b_p_tsr == NUL
4396 ? p_tsr
4397 : curbuf->b_p_tsr)
4398 : (*curbuf->b_p_dict == NUL
4399 ? p_dict
4400 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004401 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004402 dict != NULL ? dict_f
4403 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 dict = NULL;
4405 break;
4406
4407 case CTRL_X_TAGS:
4408 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4409 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004410 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004412 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413 * of matches is found when compl_pattern is empty */
4414 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4416 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4417 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4418 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004419 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 }
4421 p_ic = save_p_ic;
4422 break;
4423
4424 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4427 {
4428
4429 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004431 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
4433 break;
4434
4435 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004436 if (expand_cmdline(&compl_xp, compl_pattern,
4437 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004439 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 break;
4441
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004442#ifdef FEAT_COMPL_FUNC
4443 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004444 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004445 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004446 break;
4447#endif
4448
Bram Moolenaar488c6512005-08-11 20:09:58 +00004449 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004450#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004451 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004452 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004453 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004454 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004455#endif
4456 break;
4457
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 default: /* normal ^P/^N and ^X^L */
4459 /*
4460 * If 'infercase' is set, don't use 'smartcase' here
4461 */
4462 save_p_scs = p_scs;
4463 if (ins_buf->b_p_inf)
4464 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004465
Bram Moolenaar361aa502014-01-23 22:45:58 +01004466 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 * end but never from the middle, thus setting nowrapscan in this
4468 * buffers is a good idea, on the other hand, we always set
4469 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4470 save_p_ws = p_ws;
4471 if (ins_buf != curbuf)
4472 p_ws = FALSE;
4473 else if (*e_cpt == '.')
4474 p_ws = TRUE;
4475 for (;;)
4476 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004477 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004479 ++msg_silent; /* Don't want messages for wrapscan. */
4480
Bram Moolenaare4214502015-03-05 18:08:43 +01004481 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4482 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004483 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004484 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004485 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004487 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004489 found_new_match = searchit(NULL, ins_buf, pos,
4490 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004491 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004492 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004493 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004494 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004496 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004497 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 first_match_pos = *pos;
4499 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004500 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004503 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 found_new_match = FAIL;
4505 if (found_new_match == FAIL)
4506 {
4507 if (ins_buf == curbuf)
4508 found_all = TRUE;
4509 break;
4510 }
4511
4512 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004513 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 && ini->lnum == pos->lnum
4515 && ini->col == pos->col)
4516 continue;
4517 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004518 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004520 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 {
4522 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4523 continue;
4524 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4525 if (!p_paste)
4526 ptr = skipwhite(ptr);
4527 }
4528 len = (int)STRLEN(ptr);
4529 }
4530 else
4531 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004532 char_u *tmp_ptr = ptr;
4533
4534 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 /* Skip if already inside a word. */
4538 if (vim_iswordp(tmp_ptr))
4539 continue;
4540 /* Find start of next word. */
4541 tmp_ptr = find_word_start(tmp_ptr);
4542 }
4543 /* Find end of this word. */
4544 tmp_ptr = find_word_end(tmp_ptr);
4545 len = (int)(tmp_ptr - ptr);
4546
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004547 if ((compl_cont_status & CONT_ADDING)
4548 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 {
4550 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4551 {
4552 /* Try next line, if any. the new word will be
4553 * "join" as if the normal command "J" was used.
4554 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 * works -- Acevedo */
4557 STRNCPY(IObuff, ptr, len);
4558 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4559 tmp_ptr = ptr = skipwhite(ptr);
4560 /* Find start of next word. */
4561 tmp_ptr = find_word_start(tmp_ptr);
4562 /* Find end of next word. */
4563 tmp_ptr = find_word_end(tmp_ptr);
4564 if (tmp_ptr > ptr)
4565 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004566 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004568 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 IObuff[len++] = ' ';
4570 /* IObuf =~ "\k.* ", thus len >= 2 */
4571 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004572 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 || (vim_strchr(p_cpo, CPO_JOINSP)
4574 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004575 && (IObuff[len - 2] == '?'
4576 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 IObuff[len++] = ' ';
4578 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004579 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (tmp_ptr - ptr >= IOSIZE - len)
4581 tmp_ptr = ptr + IOSIZE - len - 1;
4582 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4583 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004584 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 IObuff[len] = NUL;
4587 ptr = IObuff;
4588 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004589 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 continue;
4591 }
4592 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004593 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004594 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004595 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
4597 found_new_match = OK;
4598 break;
4599 }
4600 }
4601 p_scs = save_p_scs;
4602 p_ws = save_p_ws;
4603 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004604
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004605 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004606 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004607 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 found_new_match = OK;
4609
4610 /* break the loop for specialized modes (use 'complete' just for the
4611 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004612 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004613 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004614 {
4615 if (got_int)
4616 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004617 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004618 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004619 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004620
Bram Moolenaare4214502015-03-05 18:08:43 +01004621 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004622 || compl_interrupted)
4623 break;
4624 compl_started = TRUE;
4625 }
4626 else
4627 {
4628 /* Mark a buffer scanned when it has been scanned completely */
4629 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4630 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004632 compl_started = FALSE;
4633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004635 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636
Bram Moolenaare4214502015-03-05 18:08:43 +01004637 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 && *e_cpt == NUL) /* Got to end of 'complete' */
4639 found_new_match = FAIL;
4640
4641 i = -1; /* total of matches, unknown */
4642 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004643 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 i = ins_compl_make_cyclic();
4645
4646 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004647 * just been made cyclic then we have to move compl_curr_match to the next
4648 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004649 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4650 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004651 if (compl_curr_match == NULL)
4652 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 return i;
4654}
4655
4656/* Delete the old text being completed. */
4657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004658ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004660 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662 /*
4663 * In insert mode: Delete the typed part.
4664 * In replace mode: Put the old characters back, if any.
4665 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004666 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4667 if ((int)curwin->w_cursor.col > col)
4668 {
4669 if (stop_arrow() == FAIL)
4670 return;
4671 backspace_until_column(col);
4672 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004673
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004674 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4675 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004677 /* clear v:completed_item */
4678 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679}
4680
Bram Moolenaar472e8592016-10-15 17:06:47 +02004681/*
4682 * Insert the new text being completed.
4683 * "in_compl_func" is TRUE when called from complete_check().
4684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004686ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004688 dict_T *dict;
4689
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004690 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004691 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4692 compl_used_match = FALSE;
4693 else
4694 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004695
4696 /* Set completed item. */
4697 /* { word, abbr, menu, kind, info } */
4698 dict = dict_alloc();
4699 if (dict != NULL)
4700 {
4701 dict_add_nr_str(dict, "word", 0L,
4702 EMPTY_IF_NULL(compl_shown_match->cp_str));
4703 dict_add_nr_str(dict, "abbr", 0L,
4704 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4705 dict_add_nr_str(dict, "menu", 0L,
4706 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4707 dict_add_nr_str(dict, "kind", 0L,
4708 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4709 dict_add_nr_str(dict, "info", 0L,
4710 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4711 }
4712 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004713 if (!in_compl_func)
4714 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715}
4716
4717/*
4718 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004719 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4720 * get more completions. If it is FALSE, then we just do nothing when there
4721 * are no more completions in a given direction. The latter case is used when
4722 * we are still in the middle of finding completions, to allow browsing
4723 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 * Return the total number of matches, or -1 if still unknown -- webb.
4725 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004726 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4727 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 *
4729 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004730 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4731 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 */
4733 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004734ins_compl_next(
4735 int allow_get_expansion,
4736 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004737 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004738 int insert_match, /* Insert the newly selected match */
4739 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
4741 int num_matches = -1;
4742 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004743 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004744 compl_T *found_compl = NULL;
4745 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004746 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004747 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004749 /* When user complete function return -1 for findstart which is next
4750 * time of 'always', compl_shown_match become NULL. */
4751 if (compl_shown_match == NULL)
4752 return -1;
4753
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004754 if (compl_leader != NULL
4755 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004757 /* Set "compl_shown_match" to the actually shown match, it may differ
4758 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004759 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004760 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004761 && compl_shown_match->cp_next != NULL
4762 && compl_shown_match->cp_next != compl_first_match)
4763 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004764
4765 /* If we didn't find it searching forward, and compl_shows_dir is
4766 * backward, find the last match. */
4767 if (compl_shows_dir == BACKWARD
4768 && !ins_compl_equal(compl_shown_match,
4769 compl_leader, (int)STRLEN(compl_leader))
4770 && (compl_shown_match->cp_next == NULL
4771 || compl_shown_match->cp_next == compl_first_match))
4772 {
4773 while (!ins_compl_equal(compl_shown_match,
4774 compl_leader, (int)STRLEN(compl_leader))
4775 && compl_shown_match->cp_prev != NULL
4776 && compl_shown_match->cp_prev != compl_first_match)
4777 compl_shown_match = compl_shown_match->cp_prev;
4778 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004779 }
4780
4781 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004782 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 /* Delete old text to be replaced */
4784 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004785
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004786 /* When finding the longest common text we stick at the original text,
4787 * don't let CTRL-N or CTRL-P move to the first match. */
4788 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4789
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004790 /* When restarting the search don't insert the first match either. */
4791 if (compl_restarting)
4792 {
4793 advance = FALSE;
4794 compl_restarting = FALSE;
4795 }
4796
Bram Moolenaare3226be2005-12-18 22:10:00 +00004797 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4798 * around. */
4799 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004801 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004803 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004804 found_end = (compl_first_match != NULL
4805 && (compl_shown_match->cp_next == compl_first_match
4806 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004807 }
4808 else if (compl_shows_dir == BACKWARD
4809 && compl_shown_match->cp_prev != NULL)
4810 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004811 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004812 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004813 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
4815 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004816 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004817 if (!allow_get_expansion)
4818 {
4819 if (advance)
4820 {
4821 if (compl_shows_dir == BACKWARD)
4822 compl_pending -= todo + 1;
4823 else
4824 compl_pending += todo + 1;
4825 }
4826 return -1;
4827 }
4828
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004829 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004830 {
4831 if (compl_shows_dir == BACKWARD)
4832 --compl_pending;
4833 else
4834 ++compl_pending;
4835 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004836
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004837 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004838 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004839
4840 /* handle any pending completions */
4841 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004842 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004843 {
4844 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4845 {
4846 compl_shown_match = compl_shown_match->cp_next;
4847 --compl_pending;
4848 }
4849 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4850 {
4851 compl_shown_match = compl_shown_match->cp_prev;
4852 ++compl_pending;
4853 }
4854 else
4855 break;
4856 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004857 found_end = FALSE;
4858 }
4859 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4860 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004861 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004862 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004863 ++todo;
4864 else
4865 /* Remember a matching item. */
4866 found_compl = compl_shown_match;
4867
4868 /* Stop at the end of the list when we found a usable match. */
4869 if (found_end)
4870 {
4871 if (found_compl != NULL)
4872 {
4873 compl_shown_match = found_compl;
4874 break;
4875 }
4876 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004880 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004881 if (compl_no_insert && !started)
4882 {
4883 ins_bytes(compl_orig_text + ins_compl_len());
4884 compl_used_match = FALSE;
4885 }
4886 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004887 {
4888 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004889 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004890 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004891 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004892 }
4893 else
4894 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
4896 if (!allow_get_expansion)
4897 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004898 /* may undisplay the popup menu first */
4899 ins_compl_upd_pum();
4900
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004901 /* redraw to show the user what was inserted */
4902 update_screen(0);
4903
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004904 /* display the updated popup menu */
4905 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004906#ifdef FEAT_GUI
4907 if (gui.in_use)
4908 {
4909 /* Show the cursor after the match, not after the redrawn text. */
4910 setcursor();
4911 out_flush();
4912 gui_update_cursor(FALSE, FALSE);
4913 }
4914#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004915
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 /* Delete old text to be replaced, since we're still searching and
4917 * don't want to match ourselves! */
4918 ins_compl_delete();
4919 }
4920
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004921 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004922 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004923 if (compl_no_insert && !started)
4924 compl_enter_selects = TRUE;
4925 else
4926 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004927
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 /*
4929 * Show the file name for the match (if any)
4930 * Truncate the file name to avoid a wait for return.
4931 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004932 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 {
4934 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004935 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (i <= 0)
4937 i = 0;
4938 else
4939 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004940 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 msg(IObuff);
4942 redraw_cmdline = FALSE; /* don't overwrite! */
4943 }
4944
4945 return num_matches;
4946}
4947
4948/*
4949 * Call this while finding completions, to check whether the user has hit a key
4950 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004951 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004953 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004954 * "in_compl_func" is TRUE when called from complete_check(), don't set
4955 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 */
4957 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004958ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959{
4960 static int count = 0;
4961
4962 int c;
4963
4964 /* Don't check when reading keys from a script. That would break the test
4965 * scripts */
4966 if (using_script())
4967 return;
4968
4969 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004970 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 return;
4972 count = 0;
4973
Bram Moolenaara260a972006-06-23 19:36:29 +00004974 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4975 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (c != NUL)
4978 {
4979 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4980 {
4981 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004982 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004983 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02004984 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004986 else
4987 {
4988 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004989 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004990 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004991 if (c != K_IGNORE)
4992 {
4993 /* Don't interrupt completion when the character wasn't typed,
4994 * e.g., when doing @q to replay keys. */
4995 if (c != Ctrl_R && KeyTyped)
4996 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004997
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004998 vungetc(c);
4999 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005002 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005003 {
5004 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5005
5006 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005007 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005008 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005009}
5010
5011/*
5012 * Decide the direction of Insert mode complete from the key typed.
5013 * Returns BACKWARD or FORWARD.
5014 */
5015 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005016ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005017{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005018 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005019 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005020 return BACKWARD;
5021 return FORWARD;
5022}
5023
5024/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005025 * Return TRUE for keys that are used for completion only when the popup menu
5026 * is visible.
5027 */
5028 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005029ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005030{
5031 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005032 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5033 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005034}
5035
5036/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005037 * Decide the number of completions to move forward.
5038 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5039 */
5040 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005041ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005042{
5043 int h;
5044
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005045 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005046 {
5047 h = pum_get_height();
5048 if (h > 3)
5049 h -= 2; /* keep some context */
5050 return h;
5051 }
5052 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053}
5054
5055/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005056 * Return TRUE if completion with "c" should insert the match, FALSE if only
5057 * to change the currently selected completion.
5058 */
5059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005060ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005061{
5062 switch (c)
5063 {
5064 case K_UP:
5065 case K_DOWN:
5066 case K_PAGEDOWN:
5067 case K_KPAGEDOWN:
5068 case K_S_DOWN:
5069 case K_PAGEUP:
5070 case K_KPAGEUP:
5071 case K_S_UP:
5072 return FALSE;
5073 }
5074 return TRUE;
5075}
5076
5077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 * Do Insert mode completion.
5079 * Called when character "c" was typed, which has a meaning for completion.
5080 * Returns OK if completion was done, FAIL if something failed (out of mem).
5081 */
5082 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005083ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005085 char_u *line;
5086 int startcol = 0; /* column where searched text starts */
5087 colnr_T curs_col; /* cursor column */
5088 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005089 int save_w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005090 int insert_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
Bram Moolenaare3226be2005-12-18 22:10:00 +00005092 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005093 insert_match = ins_compl_use_match(c);
5094
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005095 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 /* First time we hit ^N or ^P (in a row, I mean) */
5098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 did_ai = FALSE;
5100#ifdef FEAT_SMARTINDENT
5101 did_si = FALSE;
5102 can_si = FALSE;
5103 can_si_back = FALSE;
5104#endif
5105 if (stop_arrow() == FAIL)
5106 return FAIL;
5107
5108 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005109 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005110 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005112 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005113 * "compl_startpos" to the cursor as a pattern to add a new word
5114 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005115 * "compl_startpos" is not in the same line as the cursor then fix it
5116 * (the line has been split because it was longer than 'tw'). if SOL
5117 * is set then skip the previous pattern, a word at the beginning of
5118 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005119 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5120 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 /*
5123 * it is a continued search
5124 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5127 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5128 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005131 /* line (probably) wrapped, set compl_startpos to the
5132 * first non_blank in the line, if it is not a wordchar
5133 * include it to get a better pattern, but then we don't
5134 * want the "\\<" prefix, check it bellow */
5135 compl_col = (colnr_T)(skipwhite(line) - line);
5136 compl_startpos.col = compl_col;
5137 compl_startpos.lnum = curwin->w_cursor.lnum;
5138 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 else
5141 {
5142 /* S_IPOS was set when we inserted a word that was at the
5143 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005144 * mode but first we need to redefine compl_startpos */
5145 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005147 compl_cont_status |= CONT_SOL;
5148 compl_startpos.col = (colnr_T)(skipwhite(
5149 line + compl_length
5150 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005154 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005155 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005156 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005158 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005160 compl_cont_status &= ~CONT_SOL;
5161 compl_length = (IOSIZE - MIN_SPACE);
5162 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005164 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5165 if (compl_length < 1)
5166 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005168 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005169 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005171 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005174 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005176 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005178 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005180 compl_cont_status = 0;
5181 compl_cont_status |= CONT_N_ADDS;
5182 compl_startpos = curwin->w_cursor;
5183 startcol = (int)curs_col;
5184 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186
5187 /* Work out completion pattern and original text -- webb */
5188 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5189 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5192 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005193 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005195 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005197 compl_col += ++startcol;
5198 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005201 compl_pattern = str_foldcase(line + compl_col,
5202 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005204 compl_pattern = vim_strnsave(line + compl_col,
5205 compl_length);
5206 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 return FAIL;
5208 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005209 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
5211 char_u *prefix = (char_u *)"\\<";
5212
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005213 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005214 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005215 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005216 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005218 if (!vim_iswordp(line + compl_col)
5219 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 && (
5221#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005222 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005224 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225#endif
5226 )))
5227 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 STRCPY((char *)compl_pattern, prefix);
5229 (void)quote_meta(compl_pattern + STRLEN(prefix),
5230 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005232 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005234 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237#endif
5238 )
5239 {
5240 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005241 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5242 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 compl_col += curs_col;
5245 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 }
5247 else
5248 {
5249#ifdef FEAT_MBYTE
5250 /* Search the point of change class of multibyte character
5251 * or not a word single byte character backward. */
5252 if (has_mbyte)
5253 {
5254 int base_class;
5255 int head_off;
5256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 startcol -= (*mb_head_off)(line, line + startcol);
5258 base_class = mb_get_class(line + startcol);
5259 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005261 head_off = (*mb_head_off)(line, line + startcol);
5262 if (base_class != mb_get_class(line + startcol
5263 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005265 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
5267 }
5268 else
5269#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005270 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005272 compl_col += ++startcol;
5273 compl_length = (int)curs_col - startcol;
5274 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
5276 /* Only match word with at least two chars -- webb
5277 * there's no need to call quote_meta,
5278 * alloc(7) is enough -- Acevedo
5279 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 compl_pattern = alloc(7);
5281 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005283 STRCPY((char *)compl_pattern, "\\<");
5284 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5285 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 }
5287 else
5288 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005289 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005290 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005293 STRCPY((char *)compl_pattern, "\\<");
5294 (void)quote_meta(compl_pattern + 2, line + compl_col,
5295 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
5297 }
5298 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005299 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005301 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005302 compl_length = (int)curs_col - (int)compl_col;
5303 if (compl_length < 0) /* cursor in indent: empty pattern */
5304 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005306 compl_pattern = str_foldcase(line + compl_col, compl_length,
5307 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005309 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5310 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 return FAIL;
5312 }
5313 else if (ctrl_x_mode == CTRL_X_FILES)
5314 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005315 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005316 if (startcol > 0)
5317 {
5318 char_u *p = line + startcol;
5319
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005320 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005321 while (p > line && vim_isfilec(PTR2CHAR(p)))
5322 mb_ptr_back(line, p);
5323 if (p == line && vim_isfilec(PTR2CHAR(p)))
5324 startcol = 0;
5325 else
5326 startcol = (int)(p - line) + 1;
5327 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005328
Bram Moolenaar0300e462013-09-08 16:03:45 +02005329 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005330 compl_length = (int)curs_col - startcol;
5331 compl_pattern = addstar(line + compl_col, compl_length,
5332 EXPAND_FILES);
5333 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 return FAIL;
5335 }
5336 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5337 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 compl_pattern = vim_strnsave(line, curs_col);
5339 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005341 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005342 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005343 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5344 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005345 /* No completion possible, use an empty pattern to get a
5346 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005347 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005348 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005349 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5350 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005352 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005353 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005354#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005355 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005356 * Call user defined function 'completefunc' with "a:findstart"
5357 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005358 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005359 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005360 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005361 char_u *funcname;
5362 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005363 win_T *curwin_save;
5364 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005365
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005366 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005367 * string */
5368 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5369 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5370 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005371 {
5372 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5373 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005374 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005375 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005376
5377 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005378 args[1] = NULL;
5379 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005380 curwin_save = curwin;
5381 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005382 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005383 if (curwin_save != curwin || curbuf_save != curbuf)
5384 {
5385 EMSG(_(e_complwin));
5386 return FAIL;
5387 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005388 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005389 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005390 if (!equalpos(curwin->w_cursor, pos))
5391 {
5392 EMSG(_(e_compldel));
5393 return FAIL;
5394 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005395
Bram Moolenaar6110a002012-01-26 18:58:38 +01005396 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005397 * cancel the complete without an error.
5398 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005399 if (col == -2)
5400 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005401 if (col == -3)
5402 {
5403 ctrl_x_mode = 0;
5404 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005405 if (!shortmess(SHM_COMPLETIONMENU))
5406 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005407 return FAIL;
5408 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005409
Bram Moolenaar82139082011-09-14 16:52:09 +02005410 /*
5411 * Reset extended parameters of completion, when start new
5412 * completion.
5413 */
5414 compl_opt_refresh_always = FALSE;
5415
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005416 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005417 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005418 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005419 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005420 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005421
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005422 /* Setup variables for completion. Need to obtain "line" again,
5423 * it may have become invalid. */
5424 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005425 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005426 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5427 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005428#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005429 return FAIL;
5430 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005431 else if (ctrl_x_mode == CTRL_X_SPELL)
5432 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005433#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005434 if (spell_bad_len > 0)
5435 compl_col = curs_col - spell_bad_len;
5436 else
5437 compl_col = spell_word_start(startcol);
5438 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005439 {
5440 compl_length = 0;
5441 compl_col = curs_col;
5442 }
5443 else
5444 {
5445 spell_expand_check_cap(compl_col);
5446 compl_length = (int)curs_col - compl_col;
5447 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005448 /* Need to obtain "line" again, it may have become invalid. */
5449 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005450 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5451 if (compl_pattern == NULL)
5452#endif
5453 return FAIL;
5454 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005455 else
5456 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005457 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005458 return FAIL;
5459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005461 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
5463 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005464 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
5466 /* Insert a new line, keep indentation but ignore 'comments' */
5467#ifdef FEAT_COMMENTS
5468 char_u *old = curbuf->b_p_com;
5469
5470 curbuf->b_p_com = (char_u *)"";
5471#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005472 compl_startpos.lnum = curwin->w_cursor.lnum;
5473 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 ins_eol('\r');
5475#ifdef FEAT_COMMENTS
5476 curbuf->b_p_com = old;
5477#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005478 compl_length = 0;
5479 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
5481 }
5482 else
5483 {
5484 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005485 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005488 if (compl_cont_status & CONT_LOCAL)
5489 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 else
5491 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5492
Bram Moolenaar62951b12011-09-21 18:23:05 +02005493 /* If any of the original typed text has been changed we need to fix
5494 * the redo buffer. */
5495 ins_compl_fixRedoBufForLeader(NULL);
5496
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005497 /* Always add completion for the original text. */
5498 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005499 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5500 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005501 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005503 vim_free(compl_pattern);
5504 compl_pattern = NULL;
5505 vim_free(compl_orig_text);
5506 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 return FAIL;
5508 }
5509
5510 /* showmode might reset the internal line pointers, so it must
5511 * be called before line = ml_get(), or when this address is no
5512 * longer needed. -- Acevedo.
5513 */
5514 edit_submode_extra = (char_u *)_("-- Searching...");
5515 edit_submode_highl = HLF_COUNT;
5516 showmode();
5517 edit_submode_extra = NULL;
5518 out_flush();
5519 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005520 else if (insert_match && stop_arrow() == FAIL)
5521 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005523 compl_shown_match = compl_curr_match;
5524 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
5526 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005527 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005529 save_w_wrow = curwin->w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005530 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005532 /* may undisplay the popup menu */
5533 ins_compl_upd_pum();
5534
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005535 if (n > 1) /* all matches have been found */
5536 compl_matches = n;
5537 compl_curr_match = compl_shown_match;
5538 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
Bram Moolenaard68071d2006-05-02 22:08:30 +00005540 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5541 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 if (got_int && !global_busy)
5543 {
5544 (void)vgetc();
5545 got_int = FALSE;
5546 }
5547
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005548 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005549 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005551 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5552 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5554 edit_submode_highl = HLF_E;
5555 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5556 * because we couldn't expand anything at first place, but if we used
5557 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5558 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005559 if ( compl_length > 1
5560 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 || (ctrl_x_mode != 0
5562 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5563 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005564 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566
Bram Moolenaar572cb562005-08-05 21:35:02 +00005567 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005568 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005570 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
5572 if (edit_submode_extra == NULL)
5573 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005574 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
5576 edit_submode_extra = (char_u *)_("Back at original");
5577 edit_submode_highl = HLF_W;
5578 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005579 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 {
5581 edit_submode_extra = (char_u *)_("Word from other line");
5582 edit_submode_highl = HLF_COUNT;
5583 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005584 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
5586 edit_submode_extra = (char_u *)_("The only match");
5587 edit_submode_highl = HLF_COUNT;
5588 }
5589 else
5590 {
5591 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005592 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005594 int number = 0;
5595 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005597 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
5599 /* search backwards for the first valid (!= -1) number.
5600 * This should normally succeed already at the first loop
5601 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005602 for (match = compl_curr_match->cp_prev; match != NULL
5603 && match != compl_first_match;
5604 match = match->cp_prev)
5605 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005607 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 break;
5609 }
5610 if (match != NULL)
5611 /* go up and assign all numbers which are not assigned
5612 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005613 for (match = match->cp_next;
5614 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005615 match = match->cp_next)
5616 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 }
5618 else /* BACKWARD */
5619 {
5620 /* search forwards (upwards) for the first valid (!= -1)
5621 * number. This should normally succeed already at the
5622 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005623 for (match = compl_curr_match->cp_next; match != NULL
5624 && match != compl_first_match;
5625 match = match->cp_next)
5626 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005628 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 break;
5630 }
5631 if (match != NULL)
5632 /* go down and assign all numbers which are not
5633 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005634 for (match = match->cp_prev; match
5635 && match->cp_number == -1;
5636 match = match->cp_prev)
5637 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 }
5639 }
5640
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005641 /* The match should always have a sequence number now, this is
5642 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005643 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005645 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5646 * Translations may need more than twice that. */
5647 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005649 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005650 vim_snprintf((char *)match_ref, sizeof(match_ref),
5651 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005652 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005654 vim_snprintf((char *)match_ref, sizeof(match_ref),
5655 _("match %d"),
5656 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 edit_submode_extra = match_ref;
5658 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005659 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 curs_columns(FALSE);
5661 }
5662 }
5663 }
5664
5665 /* Show a message about what (completion) mode we're in. */
5666 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005667 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005669 if (edit_submode_extra != NULL)
5670 {
5671 if (!p_smd)
5672 msg_attr(edit_submode_extra,
5673 edit_submode_highl < HLF_COUNT
5674 ? hl_attr(edit_submode_highl) : 0);
5675 }
5676 else
5677 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
Bram Moolenaard68071d2006-05-02 22:08:30 +00005680 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005681 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005682 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005683 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005684 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005685 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005686 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005687
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 return OK;
5689}
5690
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005691 static void
5692show_pum(int save_w_wrow)
5693{
5694 /* RedrawingDisabled may be set when invoked through complete(). */
5695 int n = RedrawingDisabled;
5696
5697 RedrawingDisabled = 0;
5698
5699 /* If the cursor moved we need to remove the pum first. */
5700 setcursor();
5701 if (save_w_wrow != curwin->w_wrow)
5702 ins_compl_del_pum();
5703
5704 ins_compl_show_pum();
5705 setcursor();
5706 RedrawingDisabled = n;
5707}
5708
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709/*
5710 * Looks in the first "len" chars. of "src" for search-metachars.
5711 * If dest is not NULL the chars. are copied there quoting (with
5712 * a backslash) the metachars, and dest would be NUL terminated.
5713 * Returns the length (needed) of dest
5714 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005715 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005716quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005718 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005720 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 switch (*src)
5723 {
5724 case '.':
5725 case '*':
5726 case '[':
5727 if (ctrl_x_mode == CTRL_X_DICTIONARY
5728 || ctrl_x_mode == CTRL_X_THESAURUS)
5729 break;
5730 case '~':
5731 if (!p_magic) /* quote these only if magic is set */
5732 break;
5733 case '\\':
5734 if (ctrl_x_mode == CTRL_X_DICTIONARY
5735 || ctrl_x_mode == CTRL_X_THESAURUS)
5736 break;
5737 case '^': /* currently it's not needed. */
5738 case '$':
5739 m++;
5740 if (dest != NULL)
5741 *dest++ = '\\';
5742 break;
5743 }
5744 if (dest != NULL)
5745 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005746# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 /* Copy remaining bytes of a multibyte character. */
5748 if (has_mbyte)
5749 {
5750 int i, mb_len;
5751
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005752 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (mb_len > 0 && len >= mb_len)
5754 for (i = 0; i < mb_len; ++i)
5755 {
5756 --len;
5757 ++src;
5758 if (dest != NULL)
5759 *dest++ = *src;
5760 }
5761 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 }
5764 if (dest != NULL)
5765 *dest = NUL;
5766
5767 return m;
5768}
5769#endif /* FEAT_INS_EXPAND */
5770
5771/*
5772 * Next character is interpreted literally.
5773 * A one, two or three digit decimal number is interpreted as its byte value.
5774 * If one or two digits are entered, the next character is given to vungetc().
5775 * For Unicode a character > 255 may be returned.
5776 */
5777 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005778get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
5780 int cc;
5781 int nc;
5782 int i;
5783 int hex = FALSE;
5784 int octal = FALSE;
5785#ifdef FEAT_MBYTE
5786 int unicode = 0;
5787#endif
5788
5789 if (got_int)
5790 return Ctrl_C;
5791
5792#ifdef FEAT_GUI
5793 /*
5794 * In GUI there is no point inserting the internal code for a special key.
5795 * It is more useful to insert the string "<KEY>" instead. This would
5796 * probably be useful in a text window too, but it would not be
5797 * vi-compatible (maybe there should be an option for it?) -- webb
5798 */
5799 if (gui.in_use)
5800 ++allow_keys;
5801#endif
5802#ifdef USE_ON_FLY_SCROLL
5803 dont_scroll = TRUE; /* disallow scrolling here */
5804#endif
5805 ++no_mapping; /* don't map the next key hits */
5806 cc = 0;
5807 i = 0;
5808 for (;;)
5809 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005810 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811#ifdef FEAT_CMDL_INFO
5812 if (!(State & CMDLINE)
5813# ifdef FEAT_MBYTE
5814 && MB_BYTE2LEN_CHECK(nc) == 1
5815# endif
5816 )
5817 add_to_showcmd(nc);
5818#endif
5819 if (nc == 'x' || nc == 'X')
5820 hex = TRUE;
5821 else if (nc == 'o' || nc == 'O')
5822 octal = TRUE;
5823#ifdef FEAT_MBYTE
5824 else if (nc == 'u' || nc == 'U')
5825 unicode = nc;
5826#endif
5827 else
5828 {
5829 if (hex
5830#ifdef FEAT_MBYTE
5831 || unicode != 0
5832#endif
5833 )
5834 {
5835 if (!vim_isxdigit(nc))
5836 break;
5837 cc = cc * 16 + hex2nr(nc);
5838 }
5839 else if (octal)
5840 {
5841 if (nc < '0' || nc > '7')
5842 break;
5843 cc = cc * 8 + nc - '0';
5844 }
5845 else
5846 {
5847 if (!VIM_ISDIGIT(nc))
5848 break;
5849 cc = cc * 10 + nc - '0';
5850 }
5851
5852 ++i;
5853 }
5854
5855 if (cc > 255
5856#ifdef FEAT_MBYTE
5857 && unicode == 0
5858#endif
5859 )
5860 cc = 255; /* limit range to 0-255 */
5861 nc = 0;
5862
5863 if (hex) /* hex: up to two chars */
5864 {
5865 if (i >= 2)
5866 break;
5867 }
5868#ifdef FEAT_MBYTE
5869 else if (unicode) /* Unicode: up to four or eight chars */
5870 {
5871 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5872 break;
5873 }
5874#endif
5875 else if (i >= 3) /* decimal or octal: up to three chars */
5876 break;
5877 }
5878 if (i == 0) /* no number entered */
5879 {
5880 if (nc == K_ZERO) /* NUL is stored as NL */
5881 {
5882 cc = '\n';
5883 nc = 0;
5884 }
5885 else
5886 {
5887 cc = nc;
5888 nc = 0;
5889 }
5890 }
5891
5892 if (cc == 0) /* NUL is stored as NL */
5893 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005894#ifdef FEAT_MBYTE
5895 if (enc_dbcs && (cc & 0xff) == 0)
5896 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5897 second byte will cause trouble! */
5898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
5900 --no_mapping;
5901#ifdef FEAT_GUI
5902 if (gui.in_use)
5903 --allow_keys;
5904#endif
5905 if (nc)
5906 vungetc(nc);
5907 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5908 return cc;
5909}
5910
5911/*
5912 * Insert character, taking care of special keys and mod_mask
5913 */
5914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005915insert_special(
5916 int c,
5917 int allow_modmask,
5918 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919{
5920 char_u *p;
5921 int len;
5922
5923 /*
5924 * Special function key, translate into "<Key>". Up to the last '>' is
5925 * inserted with ins_str(), so as not to replace characters in replace
5926 * mode.
5927 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5928 * unless 'allow_modmask' is TRUE.
5929 */
5930#ifdef MACOS
5931 /* Command-key never produces a normal key */
5932 if (mod_mask & MOD_MASK_CMD)
5933 allow_modmask = TRUE;
5934#endif
5935 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5936 {
5937 p = get_special_key_name(c, mod_mask);
5938 len = (int)STRLEN(p);
5939 c = p[len - 1];
5940 if (len > 2)
5941 {
5942 if (stop_arrow() == FAIL)
5943 return;
5944 p[len - 1] = NUL;
5945 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005946 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 ctrlv = FALSE;
5948 }
5949 }
5950 if (stop_arrow() == OK)
5951 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5952}
5953
5954/*
5955 * Special characters in this context are those that need processing other
5956 * than the simple insertion that can be performed here. This includes ESC
5957 * which terminates the insert, and CR/NL which need special processing to
5958 * open up a new line. This routine tries to optimize insertions performed by
5959 * the "redo", "undo" or "put" commands, so it needs to know when it should
5960 * stop and defer processing to the "normal" mechanism.
5961 * '0' and '^' are special, because they can be followed by CTRL-D.
5962 */
5963#ifdef EBCDIC
5964# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5965#else
5966# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5967#endif
5968
5969#ifdef FEAT_MBYTE
5970# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5971#else
5972# define WHITECHAR(cc) vim_iswhite(cc)
5973#endif
5974
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005975/*
5976 * "flags": INSCHAR_FORMAT - force formatting
5977 * INSCHAR_CTRLV - char typed just after CTRL-V
5978 * INSCHAR_NO_FEX - don't use 'formatexpr'
5979 *
5980 * NOTE: passes the flags value straight through to internal_format() which,
5981 * beside INSCHAR_FORMAT (above), is also looking for these:
5982 * INSCHAR_DO_COM - format comments
5983 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005986insertchar(
5987 int c, /* character to insert or NUL */
5988 int flags, /* INSCHAR_FORMAT, etc. */
5989 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 int textwidth;
5992#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005996 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005998 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000
6001 /*
6002 * Try to break the line in two or more pieces when:
6003 * - Always do this if we have been called to do formatting only.
6004 * - Always do this when 'formatoptions' has the 'a' flag and the line
6005 * ends in white space.
6006 * - Otherwise:
6007 * - Don't do this if inserting a blank
6008 * - Don't do this if an existing character is being replaced, unless
6009 * we're in VREPLACE mode.
6010 * - Do this if the cursor is not on the line where insert started
6011 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6012 * before the insert.
6013 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6014 * before 'textwidth'
6015 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006016 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006017 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 || (!vim_iswhite(c)
6019 && !((State & REPLACE_FLAG)
6020#ifdef FEAT_VREPLACE
6021 && !(State & VREPLACE_FLAG)
6022#endif
6023 && *ml_get_cursor() != NUL)
6024 && (curwin->w_cursor.lnum != Insstart.lnum
6025 || ((!has_format_option(FO_INS_LONG)
6026 || Insstart_textlen <= (colnr_T)textwidth)
6027 && (!fo_ins_blank
6028 || Insstart_blank_vcol <= (colnr_T)textwidth
6029 ))))))
6030 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006031 /* Format with 'formatexpr' when it's set. Use internal formatting
6032 * when 'formatexpr' isn't set or it returns non-zero. */
6033#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006034 int do_internal = TRUE;
6035 colnr_T virtcol = get_nolist_virtcol()
6036 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006037
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006038 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6039 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006040 {
6041 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6042 /* It may be required to save for undo again, e.g. when setline()
6043 * was called. */
6044 ins_need_undo = TRUE;
6045 }
6046 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006048 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 if (c == NUL) /* only formatting was wanted */
6052 return;
6053
6054#ifdef FEAT_COMMENTS
6055 /* Check whether this character should end a comment. */
6056 if (did_ai && (int)c == end_comment_pending)
6057 {
6058 char_u *line;
6059 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6060 int middle_len, end_len;
6061 int i;
6062
6063 /*
6064 * Need to remove existing (middle) comment leader and insert end
6065 * comment leader. First, check what comment leader we can find.
6066 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006067 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6069 {
6070 /* Skip middle-comment string */
6071 while (*p && p[-1] != ':') /* find end of middle flags */
6072 ++p;
6073 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6074 /* Don't count trailing white space for middle_len */
6075 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6076 --middle_len;
6077
6078 /* Find the end-comment string */
6079 while (*p && p[-1] != ':') /* find end of end flags */
6080 ++p;
6081 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6082
6083 /* Skip white space before the cursor */
6084 i = curwin->w_cursor.col;
6085 while (--i >= 0 && vim_iswhite(line[i]))
6086 ;
6087 i++;
6088
6089 /* Skip to before the middle leader */
6090 i -= middle_len;
6091
6092 /* Check some expected things before we go on */
6093 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6094 {
6095 /* Backspace over all the stuff we want to replace */
6096 backspace_until_column(i);
6097
6098 /*
6099 * Insert the end-comment string, except for the last
6100 * character, which will get inserted as normal later.
6101 */
6102 ins_bytes_len(lead_end, end_len - 1);
6103 }
6104 }
6105 }
6106 end_comment_pending = NUL;
6107#endif
6108
6109 did_ai = FALSE;
6110#ifdef FEAT_SMARTINDENT
6111 did_si = FALSE;
6112 can_si = FALSE;
6113 can_si_back = FALSE;
6114#endif
6115
6116 /*
6117 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6118 * This speeds up normal text input considerably.
6119 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6120 * need to re-indent at a ':', or any other character (but not what
6121 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006122 * Don't do this when there an InsertCharPre autocommand is defined,
6123 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 */
6125#ifdef USE_ON_FLY_SCROLL
6126 dont_scroll = FALSE; /* allow scrolling here */
6127#endif
6128
6129 if ( !ISSPECIAL(c)
6130#ifdef FEAT_MBYTE
6131 && (!has_mbyte || (*mb_char2len)(c) == 1)
6132#endif
6133 && vpeekc() != NUL
6134 && !(State & REPLACE_FLAG)
6135#ifdef FEAT_CINDENT
6136 && !cindent_on()
6137#endif
6138#ifdef FEAT_RIGHTLEFT
6139 && !p_ri
6140#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006141#ifdef FEAT_AUTOCMD
6142 && !has_insertcharpre()
6143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 )
6145 {
6146#define INPUT_BUFLEN 100
6147 char_u buf[INPUT_BUFLEN + 1];
6148 int i;
6149 colnr_T virtcol = 0;
6150
6151 buf[0] = c;
6152 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006153 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 virtcol = get_nolist_virtcol();
6155 /*
6156 * Stop the string when:
6157 * - no more chars available
6158 * - finding a special character (command key)
6159 * - buffer is full
6160 * - running into the 'textwidth' boundary
6161 * - need to check for abbreviation: A non-word char after a word-char
6162 */
6163 while ( (c = vpeekc()) != NUL
6164 && !ISSPECIAL(c)
6165#ifdef FEAT_MBYTE
6166 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6167#endif
6168 && i < INPUT_BUFLEN
6169 && (textwidth == 0
6170 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6171 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6172 {
6173#ifdef FEAT_RIGHTLEFT
6174 c = vgetc();
6175 if (p_hkmap && KeyTyped)
6176 c = hkmap(c); /* Hebrew mode mapping */
6177# ifdef FEAT_FKMAP
6178 if (p_fkmap && KeyTyped)
6179 c = fkmap(c); /* Farsi mode mapping */
6180# endif
6181 buf[i++] = c;
6182#else
6183 buf[i++] = vgetc();
6184#endif
6185 }
6186
6187#ifdef FEAT_DIGRAPHS
6188 do_digraph(-1); /* clear digraphs */
6189 do_digraph(buf[i-1]); /* may be the start of a digraph */
6190#endif
6191 buf[i] = NUL;
6192 ins_str(buf);
6193 if (flags & INSCHAR_CTRLV)
6194 {
6195 redo_literal(*buf);
6196 i = 1;
6197 }
6198 else
6199 i = 0;
6200 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006201 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 }
6203 else
6204 {
6205#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206 int cc;
6207
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6209 {
6210 char_u buf[MB_MAXBYTES + 1];
6211
6212 (*mb_char2bytes)(c, buf);
6213 buf[cc] = NUL;
6214 ins_char_bytes(buf, cc);
6215 AppendCharToRedobuff(c);
6216 }
6217 else
6218#endif
6219 {
6220 ins_char(c);
6221 if (flags & INSCHAR_CTRLV)
6222 redo_literal(c);
6223 else
6224 AppendCharToRedobuff(c);
6225 }
6226 }
6227}
6228
6229/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006230 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006231 *
6232 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6233 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006234 */
6235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006236internal_format(
6237 int textwidth,
6238 int second_indent,
6239 int flags,
6240 int format_only,
6241 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006242{
6243 int cc;
6244 int save_char = NUL;
6245 int haveto_redraw = FALSE;
6246 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6247#ifdef FEAT_MBYTE
6248 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6249#endif
6250 int fo_white_par = has_format_option(FO_WHITE_PAR);
6251 int first_line = TRUE;
6252#ifdef FEAT_COMMENTS
6253 colnr_T leader_len;
6254 int no_leader = FALSE;
6255 int do_comments = (flags & INSCHAR_DO_COM);
6256#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006257#ifdef FEAT_LINEBREAK
6258 int has_lbr = curwin->w_p_lbr;
6259
6260 /* make sure win_lbr_chartabsize() counts correctly */
6261 curwin->w_p_lbr = FALSE;
6262#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006263
6264 /*
6265 * When 'ai' is off we don't want a space under the cursor to be
6266 * deleted. Replace it with an 'x' temporarily.
6267 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006268 if (!curbuf->b_p_ai
6269#ifdef FEAT_VREPLACE
6270 && !(State & VREPLACE_FLAG)
6271#endif
6272 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006273 {
6274 cc = gchar_cursor();
6275 if (vim_iswhite(cc))
6276 {
6277 save_char = cc;
6278 pchar_cursor('x');
6279 }
6280 }
6281
6282 /*
6283 * Repeat breaking lines, until the current line is not too long.
6284 */
6285 while (!got_int)
6286 {
6287 int startcol; /* Cursor column at entry */
6288 int wantcol; /* column at textwidth border */
6289 int foundcol; /* column for start of spaces */
6290 int end_foundcol = 0; /* column for start of word */
6291 colnr_T len;
6292 colnr_T virtcol;
6293#ifdef FEAT_VREPLACE
6294 int orig_col = 0;
6295 char_u *saved_text = NULL;
6296#endif
6297 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006298 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006299
Bram Moolenaar97b98102009-11-17 16:41:01 +00006300 virtcol = get_nolist_virtcol()
6301 + char2cells(c != NUL ? c : gchar_cursor());
6302 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006303 break;
6304
6305#ifdef FEAT_COMMENTS
6306 if (no_leader)
6307 do_comments = FALSE;
6308 else if (!(flags & INSCHAR_FORMAT)
6309 && has_format_option(FO_WRAP_COMS))
6310 do_comments = TRUE;
6311
6312 /* Don't break until after the comment leader */
6313 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006314 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006315 else
6316 leader_len = 0;
6317
6318 /* If the line doesn't start with a comment leader, then don't
6319 * start one in a following broken line. Avoids that a %word
6320 * moved to the start of the next line causes all following lines
6321 * to start with %. */
6322 if (leader_len == 0)
6323 no_leader = TRUE;
6324#endif
6325 if (!(flags & INSCHAR_FORMAT)
6326#ifdef FEAT_COMMENTS
6327 && leader_len == 0
6328#endif
6329 && !has_format_option(FO_WRAP))
6330
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006331 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006332 if ((startcol = curwin->w_cursor.col) == 0)
6333 break;
6334
6335 /* find column of textwidth border */
6336 coladvance((colnr_T)textwidth);
6337 wantcol = curwin->w_cursor.col;
6338
Bram Moolenaar97b98102009-11-17 16:41:01 +00006339 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006340 foundcol = 0;
6341
6342 /*
6343 * Find position to break at.
6344 * Stop at first entered white when 'formatoptions' has 'v'
6345 */
6346 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006347 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006348 || curwin->w_cursor.lnum != Insstart.lnum
6349 || curwin->w_cursor.col >= Insstart.col)
6350 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006351 if (curwin->w_cursor.col == startcol && c != NUL)
6352 cc = c;
6353 else
6354 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006355 if (WHITECHAR(cc))
6356 {
6357 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006358 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006359
6360 /* find start of sequence of blanks */
6361 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6362 {
6363 dec_cursor();
6364 cc = gchar_cursor();
6365 }
6366 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6367 break; /* only spaces in front of text */
6368#ifdef FEAT_COMMENTS
6369 /* Don't break until after the comment leader */
6370 if (curwin->w_cursor.col < leader_len)
6371 break;
6372#endif
6373 if (has_format_option(FO_ONE_LETTER))
6374 {
6375 /* do not break after one-letter words */
6376 if (curwin->w_cursor.col == 0)
6377 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006378#ifdef FEAT_COMMENTS
6379 /* do not break "#a b" when 'tw' is 2 */
6380 if (curwin->w_cursor.col <= leader_len)
6381 break;
6382#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006383 col = curwin->w_cursor.col;
6384 dec_cursor();
6385 cc = gchar_cursor();
6386
6387 if (WHITECHAR(cc))
6388 continue; /* one-letter, continue */
6389 curwin->w_cursor.col = col;
6390 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006391
6392 inc_cursor();
6393
6394 end_foundcol = end_col + 1;
6395 foundcol = curwin->w_cursor.col;
6396 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006397 break;
6398 }
6399#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006400 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006401 {
6402 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006403 if (curwin->w_cursor.col != startcol)
6404 {
6405#ifdef FEAT_COMMENTS
6406 /* Don't break until after the comment leader */
6407 if (curwin->w_cursor.col < leader_len)
6408 break;
6409#endif
6410 col = curwin->w_cursor.col;
6411 inc_cursor();
6412 /* Don't change end_foundcol if already set. */
6413 if (foundcol != curwin->w_cursor.col)
6414 {
6415 foundcol = curwin->w_cursor.col;
6416 end_foundcol = foundcol;
6417 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6418 break;
6419 }
6420 curwin->w_cursor.col = col;
6421 }
6422
6423 if (curwin->w_cursor.col == 0)
6424 break;
6425
6426 col = curwin->w_cursor.col;
6427
6428 dec_cursor();
6429 cc = gchar_cursor();
6430
6431 if (WHITECHAR(cc))
6432 continue; /* break with space */
6433#ifdef FEAT_COMMENTS
6434 /* Don't break until after the comment leader */
6435 if (curwin->w_cursor.col < leader_len)
6436 break;
6437#endif
6438
6439 curwin->w_cursor.col = col;
6440
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006441 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006442 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006443 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6444 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006445 }
6446#endif
6447 if (curwin->w_cursor.col == 0)
6448 break;
6449 dec_cursor();
6450 }
6451
6452 if (foundcol == 0) /* no spaces, cannot break line */
6453 {
6454 curwin->w_cursor.col = startcol;
6455 break;
6456 }
6457
6458 /* Going to break the line, remove any "$" now. */
6459 undisplay_dollar();
6460
6461 /*
6462 * Offset between cursor position and line break is used by replace
6463 * stack functions. VREPLACE does not use this, and backspaces
6464 * over the text instead.
6465 */
6466#ifdef FEAT_VREPLACE
6467 if (State & VREPLACE_FLAG)
6468 orig_col = startcol; /* Will start backspacing from here */
6469 else
6470#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006471 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006472
6473 /*
6474 * adjust startcol for spaces that will be deleted and
6475 * characters that will remain on top line
6476 */
6477 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006478 while ((cc = gchar_cursor(), WHITECHAR(cc))
6479 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006480 inc_cursor();
6481 startcol -= curwin->w_cursor.col;
6482 if (startcol < 0)
6483 startcol = 0;
6484
6485#ifdef FEAT_VREPLACE
6486 if (State & VREPLACE_FLAG)
6487 {
6488 /*
6489 * In VREPLACE mode, we will backspace over the text to be
6490 * wrapped, so save a copy now to put on the next line.
6491 */
6492 saved_text = vim_strsave(ml_get_cursor());
6493 curwin->w_cursor.col = orig_col;
6494 if (saved_text == NULL)
6495 break; /* Can't do it, out of memory */
6496 saved_text[startcol] = NUL;
6497
6498 /* Backspace over characters that will move to the next line */
6499 if (!fo_white_par)
6500 backspace_until_column(foundcol);
6501 }
6502 else
6503#endif
6504 {
6505 /* put cursor after pos. to break line */
6506 if (!fo_white_par)
6507 curwin->w_cursor.col = foundcol;
6508 }
6509
6510 /*
6511 * Split the line just before the margin.
6512 * Only insert/delete lines, but don't really redraw the window.
6513 */
6514 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6515 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6516#ifdef FEAT_COMMENTS
6517 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006518 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006519#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006520 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6521 if (!(flags & INSCHAR_COM_LIST))
6522 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006523
6524 replace_offset = 0;
6525 if (first_line)
6526 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006527 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006528 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006529 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006530 * This section is for auto-wrap of numeric lists. When not
6531 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6532 * flag will be set and open_line() will handle it (as seen
6533 * above). The code here (and in get_number_indent()) will
6534 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006535 */
6536 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006537 second_indent =
6538 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006539 if (second_indent >= 0)
6540 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006541#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006542 if (State & VREPLACE_FLAG)
6543 change_indent(INDENT_SET, second_indent,
6544 FALSE, NUL, TRUE);
6545 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006546#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006547#ifdef FEAT_COMMENTS
6548 if (leader_len > 0 && second_indent - leader_len > 0)
6549 {
6550 int i;
6551 int padding = second_indent - leader_len;
6552
6553 /* We started at the first_line of a numbered list
6554 * that has a comment. the open_line() function has
6555 * inserted the proper comment leader and positioned
6556 * the cursor at the end of the split line. Now we
6557 * add the additional whitespace needed after the
6558 * comment leader for the numbered list. */
6559 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006560 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006561 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006562 }
6563 else
6564 {
6565#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006566 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006567#ifdef FEAT_COMMENTS
6568 }
6569#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006570 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006571 }
6572 first_line = FALSE;
6573 }
6574
6575#ifdef FEAT_VREPLACE
6576 if (State & VREPLACE_FLAG)
6577 {
6578 /*
6579 * In VREPLACE mode we have backspaced over the text to be
6580 * moved, now we re-insert it into the new line.
6581 */
6582 ins_bytes(saved_text);
6583 vim_free(saved_text);
6584 }
6585 else
6586#endif
6587 {
6588 /*
6589 * Check if cursor is not past the NUL off the line, cindent
6590 * may have added or removed indent.
6591 */
6592 curwin->w_cursor.col += startcol;
6593 len = (colnr_T)STRLEN(ml_get_curline());
6594 if (curwin->w_cursor.col > len)
6595 curwin->w_cursor.col = len;
6596 }
6597
6598 haveto_redraw = TRUE;
6599#ifdef FEAT_CINDENT
6600 can_cindent = TRUE;
6601#endif
6602 /* moved the cursor, don't autoindent or cindent now */
6603 did_ai = FALSE;
6604#ifdef FEAT_SMARTINDENT
6605 did_si = FALSE;
6606 can_si = FALSE;
6607 can_si_back = FALSE;
6608#endif
6609 line_breakcheck();
6610 }
6611
6612 if (save_char != NUL) /* put back space after cursor */
6613 pchar_cursor(save_char);
6614
Bram Moolenaar0026d472014-09-09 16:32:39 +02006615#ifdef FEAT_LINEBREAK
6616 curwin->w_p_lbr = has_lbr;
6617#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006618 if (!format_only && haveto_redraw)
6619 {
6620 update_topline();
6621 redraw_curbuf_later(VALID);
6622 }
6623}
6624
6625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 * Called after inserting or deleting text: When 'formatoptions' includes the
6627 * 'a' flag format from the current line until the end of the paragraph.
6628 * Keep the cursor at the same position relative to the text.
6629 * The caller must have saved the cursor line for undo, following ones will be
6630 * saved here.
6631 */
6632 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006633auto_format(
6634 int trailblank, /* when TRUE also format with trailing blank */
6635 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636{
6637 pos_T pos;
6638 colnr_T len;
6639 char_u *old;
6640 char_u *new, *pnew;
6641 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006642 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643
6644 if (!has_format_option(FO_AUTO))
6645 return;
6646
6647 pos = curwin->w_cursor;
6648 old = ml_get_curline();
6649
6650 /* may remove added space */
6651 check_auto_format(FALSE);
6652
6653 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6654 * user might insert normal text next. Also skip formatting when "1" is
6655 * in 'formatoptions' and there is a single character before the cursor.
6656 * Otherwise the line would be broken and when typing another non-white
6657 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006658 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 if (*old != NUL && !trailblank && wasatend)
6660 {
6661 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006662 cc = gchar_cursor();
6663 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6664 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006666 cc = gchar_cursor();
6667 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 {
6669 curwin->w_cursor = pos;
6670 return;
6671 }
6672 curwin->w_cursor = pos;
6673 }
6674
6675#ifdef FEAT_COMMENTS
6676 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6677 * comments. */
6678 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006679 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 return;
6681#endif
6682
6683 /*
6684 * May start formatting in a previous line, so that after "x" a word is
6685 * moved to the previous line if it fits there now. Only when this is not
6686 * the start of a paragraph.
6687 */
6688 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6689 {
6690 --curwin->w_cursor.lnum;
6691 if (u_save_cursor() == FAIL)
6692 return;
6693 }
6694
6695 /*
6696 * Do the formatting and restore the cursor position. "saved_cursor" will
6697 * be adjusted for the text formatting.
6698 */
6699 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006700 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 curwin->w_cursor = saved_cursor;
6702 saved_cursor.lnum = 0;
6703
6704 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6705 {
6706 /* "cannot happen" */
6707 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6708 coladvance((colnr_T)MAXCOL);
6709 }
6710 else
6711 check_cursor_col();
6712
6713 /* Insert mode: If the cursor is now after the end of the line while it
6714 * previously wasn't, the line was broken. Because of the rule above we
6715 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6716 * formatted. */
6717 if (!wasatend && has_format_option(FO_WHITE_PAR))
6718 {
6719 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006720 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 if (curwin->w_cursor.col == len)
6722 {
6723 pnew = vim_strnsave(new, len + 2);
6724 pnew[len] = ' ';
6725 pnew[len + 1] = NUL;
6726 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6727 /* remove the space later */
6728 did_add_space = TRUE;
6729 }
6730 else
6731 /* may remove added space */
6732 check_auto_format(FALSE);
6733 }
6734
6735 check_cursor();
6736}
6737
6738/*
6739 * When an extra space was added to continue a paragraph for auto-formatting,
6740 * delete it now. The space must be under the cursor, just after the insert
6741 * position.
6742 */
6743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006744check_auto_format(
6745 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
6747 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006748 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749
6750 if (did_add_space)
6751 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006752 cc = gchar_cursor();
6753 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 /* Somehow the space was removed already. */
6755 did_add_space = FALSE;
6756 else
6757 {
6758 if (!end_insert)
6759 {
6760 inc_cursor();
6761 c = gchar_cursor();
6762 dec_cursor();
6763 }
6764 if (c != NUL)
6765 {
6766 /* The space is no longer at the end of the line, delete it. */
6767 del_char(FALSE);
6768 did_add_space = FALSE;
6769 }
6770 }
6771 }
6772}
6773
6774/*
6775 * Find out textwidth to be used for formatting:
6776 * if 'textwidth' option is set, use it
6777 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6778 * if invalid value, use 0.
6779 * Set default to window width (maximum 79) for "gq" operator.
6780 */
6781 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006782comp_textwidth(
6783 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 int textwidth;
6786
6787 textwidth = curbuf->b_p_tw;
6788 if (textwidth == 0 && curbuf->b_p_wm)
6789 {
6790 /* The width is the window width minus 'wrapmargin' minus all the
6791 * things that add to the margin. */
6792 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6793#ifdef FEAT_CMDWIN
6794 if (cmdwin_type != 0)
6795 textwidth -= 1;
6796#endif
6797#ifdef FEAT_FOLDING
6798 textwidth -= curwin->w_p_fdc;
6799#endif
6800#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006801 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 textwidth -= 1;
6803#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006804 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 textwidth -= 8;
6806 }
6807 if (textwidth < 0)
6808 textwidth = 0;
6809 if (ff && textwidth == 0)
6810 {
6811 textwidth = W_WIDTH(curwin) - 1;
6812 if (textwidth > 79)
6813 textwidth = 79;
6814 }
6815 return textwidth;
6816}
6817
6818/*
6819 * Put a character in the redo buffer, for when just after a CTRL-V.
6820 */
6821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006822redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823{
6824 char_u buf[10];
6825
6826 /* Only digits need special treatment. Translate them into a string of
6827 * three digits. */
6828 if (VIM_ISDIGIT(c))
6829 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006830 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 AppendToRedobuff(buf);
6832 }
6833 else
6834 AppendCharToRedobuff(c);
6835}
6836
6837/*
6838 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006839 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 */
6841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006842start_arrow(
6843 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006845 start_arrow_common(end_insert_pos, TRUE);
6846}
6847
6848/*
6849 * Like start_arrow() but with end_change argument.
6850 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6851 */
6852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006853start_arrow_with_change(
6854 pos_T *end_insert_pos, /* can be NULL */
6855 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006856{
6857 start_arrow_common(end_insert_pos, end_change);
6858 if (!end_change)
6859 {
6860 AppendCharToRedobuff(Ctrl_G);
6861 AppendCharToRedobuff('U');
6862 }
6863}
6864
6865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006866start_arrow_common(
6867 pos_T *end_insert_pos, /* can be NULL */
6868 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006869{
6870 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {
6872 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006873 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 arrow_used = TRUE; /* this means we stopped the current insert */
6875 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006876#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006877 check_spell_redraw();
6878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879}
6880
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006881#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006882/*
6883 * If we skipped highlighting word at cursor, do it now.
6884 * It may be skipped again, thus reset spell_redraw_lnum first.
6885 */
6886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006887check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006888{
6889 if (spell_redraw_lnum != 0)
6890 {
6891 linenr_T lnum = spell_redraw_lnum;
6892
6893 spell_redraw_lnum = 0;
6894 redrawWinline(lnum, FALSE);
6895 }
6896}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006897
6898/*
6899 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6900 * spelled word, if there is one.
6901 */
6902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006903spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006904{
6905 pos_T tpos = curwin->w_cursor;
6906
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006907 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006908 if (curwin->w_cursor.col != tpos.col)
6909 start_arrow(&tpos);
6910}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006911#endif
6912
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913/*
6914 * stop_arrow() is called before a change is made in insert mode.
6915 * If an arrow key has been used, start a new insertion.
6916 * Returns FAIL if undo is impossible, shouldn't insert then.
6917 */
6918 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006919stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
6921 if (arrow_used)
6922 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006923 Insstart = curwin->w_cursor; /* new insertion starts here */
6924 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6925 /* Don't update the original insert position when moved to the
6926 * right, except when nothing was inserted yet. */
6927 update_Insstart_orig = FALSE;
6928 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 if (u_save_cursor() == OK)
6931 {
6932 arrow_used = FALSE;
6933 ins_need_undo = FALSE;
6934 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 ai_col = 0;
6937#ifdef FEAT_VREPLACE
6938 if (State & VREPLACE_FLAG)
6939 {
6940 orig_line_count = curbuf->b_ml.ml_line_count;
6941 vr_lines_changed = 1;
6942 }
6943#endif
6944 ResetRedobuff();
6945 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006946 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 }
6948 else if (ins_need_undo)
6949 {
6950 if (u_save_cursor() == OK)
6951 ins_need_undo = FALSE;
6952 }
6953
6954#ifdef FEAT_FOLDING
6955 /* Always open fold at the cursor line when inserting something. */
6956 foldOpenCursor();
6957#endif
6958
6959 return (arrow_used || ins_need_undo ? FAIL : OK);
6960}
6961
6962/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006963 * Do a few things to stop inserting.
6964 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6965 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 */
6967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006968stop_insert(
6969 pos_T *end_insert_pos,
6970 int esc, /* called by ins_esc() */
6971 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006973 int cc;
6974 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975
6976 stop_redo_ins();
6977 replace_flush(); /* abandon replace stack */
6978
6979 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006980 * Save the inserted text for later redo with ^@ and CTRL-A.
6981 * Don't do it when "restart_edit" was set and nothing was inserted,
6982 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006984 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006985 if (did_restart_edit == 0 || (ptr != NULL
6986 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006987 {
6988 vim_free(last_insert);
6989 last_insert = ptr;
6990 last_insert_skip = new_insert_skip;
6991 }
6992 else
6993 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006995 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {
6997 /* Auto-format now. It may seem strange to do this when stopping an
6998 * insertion (or moving the cursor), but it's required when appending
6999 * a line and having it end in a space. But only do it when something
7000 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007001 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007003 pos_T tpos = curwin->w_cursor;
7004
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 /* When the cursor is at the end of the line after a space the
7006 * formatting will move it to the following word. Avoid that by
7007 * moving the cursor onto the space. */
7008 cc = 'x';
7009 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7010 {
7011 dec_cursor();
7012 cc = gchar_cursor();
7013 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007014 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 }
7016
7017 auto_format(TRUE, FALSE);
7018
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007019 if (vim_iswhite(cc))
7020 {
7021 if (gchar_cursor() != NUL)
7022 inc_cursor();
7023#ifdef FEAT_VIRTUALEDIT
7024 /* If the cursor is still at the same character, also keep
7025 * the "coladd". */
7026 if (gchar_cursor() == NUL
7027 && curwin->w_cursor.lnum == tpos.lnum
7028 && curwin->w_cursor.col == tpos.col)
7029 curwin->w_cursor.coladd = tpos.coladd;
7030#endif
7031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
7033
7034 /* If a space was inserted for auto-formatting, remove it now. */
7035 check_auto_format(TRUE);
7036
7037 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007038 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007039 * Do this when ESC was used or moving the cursor up/down.
7040 * Check for the old position still being valid, just in case the text
7041 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007042 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007043 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7044 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007046 pos_T tpos = curwin->w_cursor;
7047
7048 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007049 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007050 for (;;)
7051 {
7052 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7053 --curwin->w_cursor.col;
7054 cc = gchar_cursor();
7055 if (!vim_iswhite(cc))
7056 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007057 if (del_char(TRUE) == FAIL)
7058 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007059 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007060 if (curwin->w_cursor.lnum != tpos.lnum)
7061 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007062 else
7063 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007064 /* reset tpos, could have been invalidated in the loop above */
7065 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007066 tpos.col++;
7067 if (cc != NUL && gchar_pos(&tpos) == NUL)
7068 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 /* <C-S-Right> may have started Visual mode, adjust the position for
7072 * deleted characters. */
7073 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7074 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007075 int len = (int)STRLEN(ml_get_curline());
7076
7077 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007079 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007080#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 }
7084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 }
7086 }
7087 did_ai = FALSE;
7088#ifdef FEAT_SMARTINDENT
7089 did_si = FALSE;
7090 can_si = FALSE;
7091 can_si_back = FALSE;
7092#endif
7093
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007094 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7095 * now in a different buffer. */
7096 if (end_insert_pos != NULL)
7097 {
7098 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007099 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007100 curbuf->b_op_end = *end_insert_pos;
7101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102}
7103
7104/*
7105 * Set the last inserted text to a single character.
7106 * Used for the replace command.
7107 */
7108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007109set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110{
7111 char_u *s;
7112
7113 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 if (last_insert != NULL)
7116 {
7117 s = last_insert;
7118 /* Use the CTRL-V only when entering a special char */
7119 if (c < ' ' || c == DEL)
7120 *s++ = Ctrl_V;
7121 s = add_char2buf(c, s);
7122 *s++ = ESC;
7123 *s++ = NUL;
7124 last_insert_skip = 0;
7125 }
7126}
7127
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007128#if defined(EXITFREE) || defined(PROTO)
7129 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007130free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007131{
7132 vim_free(last_insert);
7133 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007134# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007135 vim_free(compl_orig_text);
7136 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007137# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007138}
7139#endif
7140
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141/*
7142 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7143 * and CSI. Handle multi-byte characters.
7144 * Returns a pointer to after the added bytes.
7145 */
7146 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007147add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148{
7149#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007150 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 int i;
7152 int len;
7153
7154 len = (*mb_char2bytes)(c, temp);
7155 for (i = 0; i < len; ++i)
7156 {
7157 c = temp[i];
7158#endif
7159 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7160 if (c == K_SPECIAL)
7161 {
7162 *s++ = K_SPECIAL;
7163 *s++ = KS_SPECIAL;
7164 *s++ = KE_FILLER;
7165 }
7166#ifdef FEAT_GUI
7167 else if (c == CSI)
7168 {
7169 *s++ = CSI;
7170 *s++ = KS_EXTRA;
7171 *s++ = (int)KE_CSI;
7172 }
7173#endif
7174 else
7175 *s++ = c;
7176#ifdef FEAT_MBYTE
7177 }
7178#endif
7179 return s;
7180}
7181
7182/*
7183 * move cursor to start of line
7184 * if flags & BL_WHITE move to first non-white
7185 * if flags & BL_SOL move to first non-white if startofline is set,
7186 * otherwise keep "curswant" column
7187 * if flags & BL_FIX don't leave the cursor on a NUL.
7188 */
7189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007190beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
7192 if ((flags & BL_SOL) && !p_sol)
7193 coladvance(curwin->w_curswant);
7194 else
7195 {
7196 curwin->w_cursor.col = 0;
7197#ifdef FEAT_VIRTUALEDIT
7198 curwin->w_cursor.coladd = 0;
7199#endif
7200
7201 if (flags & (BL_WHITE | BL_SOL))
7202 {
7203 char_u *ptr;
7204
7205 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7206 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7207 ++curwin->w_cursor.col;
7208 }
7209 curwin->w_set_curswant = TRUE;
7210 }
7211}
7212
7213/*
7214 * oneright oneleft cursor_down cursor_up
7215 *
7216 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007217 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 * Return OK when successful, FAIL when we hit a line of file boundary.
7219 */
7220
7221 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007222oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
7224 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
7227#ifdef FEAT_VIRTUALEDIT
7228 if (virtual_active())
7229 {
7230 pos_T prevpos = curwin->w_cursor;
7231
7232 /* Adjust for multi-wide char (excluding TAB) */
7233 ptr = ml_get_cursor();
7234 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007235# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007237# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 ))
7241 ? ptr2cells(ptr) : 1));
7242 curwin->w_set_curswant = TRUE;
7243 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7244 return (prevpos.col != curwin->w_cursor.col
7245 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7246 }
7247#endif
7248
7249 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007250 if (*ptr == NUL)
7251 return FAIL; /* already at the very end */
7252
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007254 if (has_mbyte)
7255 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 else
7257#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007258 l = 1;
7259
7260 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7261 * contains "onemore". */
7262 if (ptr[l] == NUL
7263#ifdef FEAT_VIRTUALEDIT
7264 && (ve_flags & VE_ONEMORE) == 0
7265#endif
7266 )
7267 return FAIL;
7268 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
7270 curwin->w_set_curswant = TRUE;
7271 return OK;
7272}
7273
7274 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007275oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276{
7277#ifdef FEAT_VIRTUALEDIT
7278 if (virtual_active())
7279 {
7280 int width;
7281 int v = getviscol();
7282
7283 if (v == 0)
7284 return FAIL;
7285
7286# ifdef FEAT_LINEBREAK
7287 /* We might get stuck on 'showbreak', skip over it. */
7288 width = 1;
7289 for (;;)
7290 {
7291 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007292 /* getviscol() is slow, skip it when 'showbreak' is empty,
7293 * 'breakindent' is not set and there are no multi-byte
7294 * characters */
7295 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296# ifdef FEAT_MBYTE
7297 && !has_mbyte
7298# endif
7299 ) || getviscol() < v)
7300 break;
7301 ++width;
7302 }
7303# else
7304 coladvance(v - 1);
7305# endif
7306
7307 if (curwin->w_cursor.coladd == 1)
7308 {
7309 char_u *ptr;
7310
7311 /* Adjust for multi-wide char (not a TAB) */
7312 ptr = ml_get_cursor();
7313 if (*ptr != TAB && vim_isprintc(
7314# ifdef FEAT_MBYTE
7315 (*mb_ptr2char)(ptr)
7316# else
7317 *ptr
7318# endif
7319 ) && ptr2cells(ptr) > 1)
7320 curwin->w_cursor.coladd = 0;
7321 }
7322
7323 curwin->w_set_curswant = TRUE;
7324 return OK;
7325 }
7326#endif
7327
7328 if (curwin->w_cursor.col == 0)
7329 return FAIL;
7330
7331 curwin->w_set_curswant = TRUE;
7332 --curwin->w_cursor.col;
7333
7334#ifdef FEAT_MBYTE
7335 /* if the character on the left of the current cursor is a multi-byte
7336 * character, move to its first byte */
7337 if (has_mbyte)
7338 mb_adjust_cursor();
7339#endif
7340 return OK;
7341}
7342
7343 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007344cursor_up(
7345 long n,
7346 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347{
7348 linenr_T lnum;
7349
7350 if (n > 0)
7351 {
7352 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007353 /* This fails if the cursor is already in the first line or the count
7354 * is larger than the line number and '-' is in 'cpoptions' */
7355 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 return FAIL;
7357 if (n >= lnum)
7358 lnum = 1;
7359 else
7360#ifdef FEAT_FOLDING
7361 if (hasAnyFolding(curwin))
7362 {
7363 /*
7364 * Count each sequence of folded lines as one logical line.
7365 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007366 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 (void)hasFolding(lnum, &lnum, NULL);
7368
7369 while (n--)
7370 {
7371 /* move up one line */
7372 --lnum;
7373 if (lnum <= 1)
7374 break;
7375 /* If we entered a fold, move to the beginning, unless in
7376 * Insert mode or when 'foldopen' contains "all": it will open
7377 * in a moment. */
7378 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7379 (void)hasFolding(lnum, &lnum, NULL);
7380 }
7381 if (lnum < 1)
7382 lnum = 1;
7383 }
7384 else
7385#endif
7386 lnum -= n;
7387 curwin->w_cursor.lnum = lnum;
7388 }
7389
7390 /* try to advance to the column we want to be at */
7391 coladvance(curwin->w_curswant);
7392
7393 if (upd_topline)
7394 update_topline(); /* make sure curwin->w_topline is valid */
7395
7396 return OK;
7397}
7398
7399/*
7400 * Cursor down a number of logical lines.
7401 */
7402 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007403cursor_down(
7404 long n,
7405 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406{
7407 linenr_T lnum;
7408
7409 if (n > 0)
7410 {
7411 lnum = curwin->w_cursor.lnum;
7412#ifdef FEAT_FOLDING
7413 /* Move to last line of fold, will fail if it's the end-of-file. */
7414 (void)hasFolding(lnum, NULL, &lnum);
7415#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007416 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007417 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007418 if (lnum >= curbuf->b_ml.ml_line_count
7419 || (lnum + n > curbuf->b_ml.ml_line_count
7420 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 return FAIL;
7422 if (lnum + n >= curbuf->b_ml.ml_line_count)
7423 lnum = curbuf->b_ml.ml_line_count;
7424 else
7425#ifdef FEAT_FOLDING
7426 if (hasAnyFolding(curwin))
7427 {
7428 linenr_T last;
7429
7430 /* count each sequence of folded lines as one logical line */
7431 while (n--)
7432 {
7433 if (hasFolding(lnum, NULL, &last))
7434 lnum = last + 1;
7435 else
7436 ++lnum;
7437 if (lnum >= curbuf->b_ml.ml_line_count)
7438 break;
7439 }
7440 if (lnum > curbuf->b_ml.ml_line_count)
7441 lnum = curbuf->b_ml.ml_line_count;
7442 }
7443 else
7444#endif
7445 lnum += n;
7446 curwin->w_cursor.lnum = lnum;
7447 }
7448
7449 /* try to advance to the column we want to be at */
7450 coladvance(curwin->w_curswant);
7451
7452 if (upd_topline)
7453 update_topline(); /* make sure curwin->w_topline is valid */
7454
7455 return OK;
7456}
7457
7458/*
7459 * Stuff the last inserted text in the read buffer.
7460 * Last_insert actually is a copy of the redo buffer, so we
7461 * first have to remove the command.
7462 */
7463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007464stuff_inserted(
7465 int c, /* Command character to be inserted */
7466 long count, /* Repeat this many times */
7467 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468{
7469 char_u *esc_ptr;
7470 char_u *ptr;
7471 char_u *last_ptr;
7472 char_u last = NUL;
7473
7474 ptr = get_last_insert();
7475 if (ptr == NULL)
7476 {
7477 EMSG(_(e_noinstext));
7478 return FAIL;
7479 }
7480
7481 /* may want to stuff the command character, to start Insert mode */
7482 if (c != NUL)
7483 stuffcharReadbuff(c);
7484 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7485 *esc_ptr = NUL; /* remove the ESC */
7486
7487 /* when the last char is either "0" or "^" it will be quoted if no ESC
7488 * comes after it OR if it will inserted more than once and "ptr"
7489 * starts with ^D. -- Acevedo
7490 */
7491 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7492 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7493 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7494 {
7495 last = *last_ptr;
7496 *last_ptr = NUL;
7497 }
7498
7499 do
7500 {
7501 stuffReadbuff(ptr);
7502 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7503 if (last)
7504 stuffReadbuff((char_u *)(last == '0'
7505 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7506 : IF_EB("\026^", CTRL_V_STR "^")));
7507 }
7508 while (--count > 0);
7509
7510 if (last)
7511 *last_ptr = last;
7512
7513 if (esc_ptr != NULL)
7514 *esc_ptr = ESC; /* put the ESC back */
7515
7516 /* may want to stuff a trailing ESC, to get out of Insert mode */
7517 if (!no_esc)
7518 stuffcharReadbuff(ESC);
7519
7520 return OK;
7521}
7522
7523 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007524get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525{
7526 if (last_insert == NULL)
7527 return NULL;
7528 return last_insert + last_insert_skip;
7529}
7530
7531/*
7532 * Get last inserted string, and remove trailing <Esc>.
7533 * Returns pointer to allocated memory (must be freed) or NULL.
7534 */
7535 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007536get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 char_u *s;
7539 int len;
7540
7541 if (last_insert == NULL)
7542 return NULL;
7543 s = vim_strsave(last_insert + last_insert_skip);
7544 if (s != NULL)
7545 {
7546 len = (int)STRLEN(s);
7547 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7548 s[len - 1] = NUL;
7549 }
7550 return s;
7551}
7552
7553/*
7554 * Check the word in front of the cursor for an abbreviation.
7555 * Called when the non-id character "c" has been entered.
7556 * When an abbreviation is recognized it is removed from the text and
7557 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7558 */
7559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007560echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561{
7562 /* Don't check for abbreviation in paste mode, when disabled and just
7563 * after moving around with cursor keys. */
7564 if (p_paste || no_abbr || arrow_used)
7565 return FALSE;
7566
7567 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7568 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7569}
7570
7571/*
7572 * replace-stack functions
7573 *
7574 * When replacing characters, the replaced characters are remembered for each
7575 * new character. This is used to re-insert the old text when backspacing.
7576 *
7577 * There is a NUL headed list of characters for each character that is
7578 * currently in the file after the insertion point. When BS is used, one NUL
7579 * headed list is put back for the deleted character.
7580 *
7581 * For a newline, there are two NUL headed lists. One contains the characters
7582 * that the NL replaced. The extra one stores the characters after the cursor
7583 * that were deleted (always white space).
7584 *
7585 * Replace_offset is normally 0, in which case replace_push will add a new
7586 * character at the end of the stack. If replace_offset is not 0, that many
7587 * characters will be left on the stack above the newly inserted character.
7588 */
7589
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007590static char_u *replace_stack = NULL;
7591static long replace_stack_nr = 0; /* next entry in replace stack */
7592static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593
7594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007595replace_push(
7596 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
7598 char_u *p;
7599
7600 if (replace_stack_nr < replace_offset) /* nothing to do */
7601 return;
7602 if (replace_stack_len <= replace_stack_nr)
7603 {
7604 replace_stack_len += 50;
7605 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7606 if (p == NULL) /* out of memory */
7607 {
7608 replace_stack_len -= 50;
7609 return;
7610 }
7611 if (replace_stack != NULL)
7612 {
7613 mch_memmove(p, replace_stack,
7614 (size_t)(replace_stack_nr * sizeof(char_u)));
7615 vim_free(replace_stack);
7616 }
7617 replace_stack = p;
7618 }
7619 p = replace_stack + replace_stack_nr - replace_offset;
7620 if (replace_offset)
7621 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7622 *p = c;
7623 ++replace_stack_nr;
7624}
7625
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007626#if defined(FEAT_MBYTE) || defined(PROTO)
7627/*
7628 * Push a character onto the replace stack. Handles a multi-byte character in
7629 * reverse byte order, so that the first byte is popped off first.
7630 * Return the number of bytes done (includes composing characters).
7631 */
7632 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007633replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007634{
7635 int l = (*mb_ptr2len)(p);
7636 int j;
7637
7638 for (j = l - 1; j >= 0; --j)
7639 replace_push(p[j]);
7640 return l;
7641}
7642#endif
7643
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644/*
7645 * Pop one item from the replace stack.
7646 * return -1 if stack empty
7647 * return replaced character or NUL otherwise
7648 */
7649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007650replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651{
7652 if (replace_stack_nr == 0)
7653 return -1;
7654 return (int)replace_stack[--replace_stack_nr];
7655}
7656
7657/*
7658 * Join the top two items on the replace stack. This removes to "off"'th NUL
7659 * encountered.
7660 */
7661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007662replace_join(
7663 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664{
7665 int i;
7666
7667 for (i = replace_stack_nr; --i >= 0; )
7668 if (replace_stack[i] == NUL && off-- <= 0)
7669 {
7670 --replace_stack_nr;
7671 mch_memmove(replace_stack + i, replace_stack + i + 1,
7672 (size_t)(replace_stack_nr - i));
7673 return;
7674 }
7675}
7676
7677/*
7678 * Pop bytes from the replace stack until a NUL is found, and insert them
7679 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7680 */
7681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007682replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
7684 int cc;
7685 int oldState = State;
7686
7687 State = NORMAL; /* don't want REPLACE here */
7688 while ((cc = replace_pop()) > 0)
7689 {
7690#ifdef FEAT_MBYTE
7691 mb_replace_pop_ins(cc);
7692#else
7693 ins_char(cc);
7694#endif
7695 dec_cursor();
7696 }
7697 State = oldState;
7698}
7699
7700#ifdef FEAT_MBYTE
7701/*
7702 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7703 * indicates a multi-byte char, pop the other bytes too.
7704 */
7705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007706mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707{
7708 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007709 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 int i;
7711 int c;
7712
7713 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7714 {
7715 buf[0] = cc;
7716 for (i = 1; i < n; ++i)
7717 buf[i] = replace_pop();
7718 ins_bytes_len(buf, n);
7719 }
7720 else
7721 ins_char(cc);
7722
7723 if (enc_utf8)
7724 /* Handle composing chars. */
7725 for (;;)
7726 {
7727 c = replace_pop();
7728 if (c == -1) /* stack empty */
7729 break;
7730 if ((n = MB_BYTE2LEN(c)) == 1)
7731 {
7732 /* Not a multi-byte char, put it back. */
7733 replace_push(c);
7734 break;
7735 }
7736 else
7737 {
7738 buf[0] = c;
7739 for (i = 1; i < n; ++i)
7740 buf[i] = replace_pop();
7741 if (utf_iscomposing(utf_ptr2char(buf)))
7742 ins_bytes_len(buf, n);
7743 else
7744 {
7745 /* Not a composing char, put it back. */
7746 for (i = n - 1; i >= 0; --i)
7747 replace_push(buf[i]);
7748 break;
7749 }
7750 }
7751 }
7752}
7753#endif
7754
7755/*
7756 * make the replace stack empty
7757 * (called when exiting replace mode)
7758 */
7759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007760replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761{
7762 vim_free(replace_stack);
7763 replace_stack = NULL;
7764 replace_stack_len = 0;
7765 replace_stack_nr = 0;
7766}
7767
7768/*
7769 * Handle doing a BS for one character.
7770 * cc < 0: replace stack empty, just move cursor
7771 * cc == 0: character was inserted, delete it
7772 * cc > 0: character was replaced, put cc (first byte of original char) back
7773 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007774 * When "limit_col" is >= 0, don't delete before this column. Matters when
7775 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 */
7777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007778replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780 int cc;
7781#ifdef FEAT_VREPLACE
7782 int orig_len = 0;
7783 int ins_len;
7784 int orig_vcols = 0;
7785 colnr_T start_vcol;
7786 char_u *p;
7787 int i;
7788 int vcol;
7789#endif
7790
7791 cc = replace_pop();
7792 if (cc > 0)
7793 {
7794#ifdef FEAT_VREPLACE
7795 if (State & VREPLACE_FLAG)
7796 {
7797 /* Get the number of screen cells used by the character we are
7798 * going to delete. */
7799 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7800 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7801 }
7802#endif
7803#ifdef FEAT_MBYTE
7804 if (has_mbyte)
7805 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007806 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807# ifdef FEAT_VREPLACE
7808 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007809 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810# endif
7811 replace_push(cc);
7812 }
7813 else
7814#endif
7815 {
7816 pchar_cursor(cc);
7817#ifdef FEAT_VREPLACE
7818 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007819 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820#endif
7821 }
7822 replace_pop_ins();
7823
7824#ifdef FEAT_VREPLACE
7825 if (State & VREPLACE_FLAG)
7826 {
7827 /* Get the number of screen cells used by the inserted characters */
7828 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007829 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 vcol = start_vcol;
7831 for (i = 0; i < ins_len; ++i)
7832 {
7833 vcol += chartabsize(p + i, vcol);
7834#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007835 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836#endif
7837 }
7838 vcol -= start_vcol;
7839
7840 /* Delete spaces that were inserted after the cursor to keep the
7841 * text aligned. */
7842 curwin->w_cursor.col += ins_len;
7843 while (vcol > orig_vcols && gchar_cursor() == ' ')
7844 {
7845 del_char(FALSE);
7846 ++orig_vcols;
7847 }
7848 curwin->w_cursor.col -= ins_len;
7849 }
7850#endif
7851
7852 /* mark the buffer as changed and prepare for displaying */
7853 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7854 }
7855 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007856 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857}
7858
7859#ifdef FEAT_CINDENT
7860/*
7861 * Return TRUE if C-indenting is on.
7862 */
7863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007864cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
7866 return (!p_paste && (curbuf->b_p_cin
7867# ifdef FEAT_EVAL
7868 || *curbuf->b_p_inde != NUL
7869# endif
7870 ));
7871}
7872#endif
7873
7874#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7875/*
7876 * Re-indent the current line, based on the current contents of it and the
7877 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7878 * confused what all the part that handles Control-T is doing that I'm not.
7879 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7880 */
7881
7882 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007883fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007885 int amount = get_the_indent();
7886
7887 if (amount >= 0)
7888 {
7889 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7890 if (linewhite(curwin->w_cursor.lnum))
7891 did_ai = TRUE; /* delete the indent if the line stays empty */
7892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893}
7894
7895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007896fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 if (p_paste)
7899 return;
7900# ifdef FEAT_LISP
7901 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7902 fixthisline(get_lisp_indent);
7903# endif
7904# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7905 else
7906# endif
7907# ifdef FEAT_CINDENT
7908 if (cindent_on())
7909 do_c_expr_indent();
7910# endif
7911}
7912
7913#endif
7914
7915#ifdef FEAT_CINDENT
7916/*
7917 * return TRUE if 'cinkeys' contains the key "keytyped",
7918 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007919 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7921 *
7922 * "keytyped" can have a few special values:
7923 * KEY_OPEN_FORW
7924 * KEY_OPEN_BACK
7925 * KEY_COMPLETE just finished completion.
7926 *
7927 * If line_is_empty is TRUE accept keys with '0' before them.
7928 */
7929 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007930in_cinkeys(
7931 int keytyped,
7932 int when,
7933 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
7935 char_u *look;
7936 int try_match;
7937 int try_match_word;
7938 char_u *p;
7939 char_u *line;
7940 int icase;
7941 int i;
7942
Bram Moolenaar30848942009-12-24 14:46:12 +00007943 if (keytyped == NUL)
7944 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7945 return FALSE;
7946
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947#ifdef FEAT_EVAL
7948 if (*curbuf->b_p_inde != NUL)
7949 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7950 else
7951#endif
7952 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7953 while (*look)
7954 {
7955 /*
7956 * Find out if we want to try a match with this key, depending on
7957 * 'when' and a '*' or '!' before the key.
7958 */
7959 switch (when)
7960 {
7961 case '*': try_match = (*look == '*'); break;
7962 case '!': try_match = (*look == '!'); break;
7963 default: try_match = (*look != '*'); break;
7964 }
7965 if (*look == '*' || *look == '!')
7966 ++look;
7967
7968 /*
7969 * If there is a '0', only accept a match if the line is empty.
7970 * But may still match when typing last char of a word.
7971 */
7972 if (*look == '0')
7973 {
7974 try_match_word = try_match;
7975 if (!line_is_empty)
7976 try_match = FALSE;
7977 ++look;
7978 }
7979 else
7980 try_match_word = FALSE;
7981
7982 /*
7983 * does it look like a control character?
7984 */
7985 if (*look == '^'
7986#ifdef EBCDIC
7987 && (Ctrl_chr(look[1]) != 0)
7988#else
7989 && look[1] >= '?' && look[1] <= '_'
7990#endif
7991 )
7992 {
7993 if (try_match && keytyped == Ctrl_chr(look[1]))
7994 return TRUE;
7995 look += 2;
7996 }
7997 /*
7998 * 'o' means "o" command, open forward.
7999 * 'O' means "O" command, open backward.
8000 */
8001 else if (*look == 'o')
8002 {
8003 if (try_match && keytyped == KEY_OPEN_FORW)
8004 return TRUE;
8005 ++look;
8006 }
8007 else if (*look == 'O')
8008 {
8009 if (try_match && keytyped == KEY_OPEN_BACK)
8010 return TRUE;
8011 ++look;
8012 }
8013
8014 /*
8015 * 'e' means to check for "else" at start of line and just before the
8016 * cursor.
8017 */
8018 else if (*look == 'e')
8019 {
8020 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8021 {
8022 p = ml_get_curline();
8023 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8024 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8025 return TRUE;
8026 }
8027 ++look;
8028 }
8029
8030 /*
8031 * ':' only causes an indent if it is at the end of a label or case
8032 * statement, or when it was before typing the ':' (to fix
8033 * class::method for C++).
8034 */
8035 else if (*look == ':')
8036 {
8037 if (try_match && keytyped == ':')
8038 {
8039 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008040 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008042 /* Need to get the line again after cin_islabel(). */
8043 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 if (curwin->w_cursor.col > 2
8045 && p[curwin->w_cursor.col - 1] == ':'
8046 && p[curwin->w_cursor.col - 2] == ':')
8047 {
8048 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008049 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008050 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 p = ml_get_curline();
8052 p[curwin->w_cursor.col - 1] = ':';
8053 if (i)
8054 return TRUE;
8055 }
8056 }
8057 ++look;
8058 }
8059
8060
8061 /*
8062 * Is it a key in <>, maybe?
8063 */
8064 else if (*look == '<')
8065 {
8066 if (try_match)
8067 {
8068 /*
8069 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8070 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8071 * >, *, : and ! keys if they really really want to.
8072 */
8073 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8074 && keytyped == look[1])
8075 return TRUE;
8076
8077 if (keytyped == get_special_key_code(look + 1))
8078 return TRUE;
8079 }
8080 while (*look && *look != '>')
8081 look++;
8082 while (*look == '>')
8083 look++;
8084 }
8085
8086 /*
8087 * Is it a word: "=word"?
8088 */
8089 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8090 {
8091 ++look;
8092 if (*look == '~')
8093 {
8094 icase = TRUE;
8095 ++look;
8096 }
8097 else
8098 icase = FALSE;
8099 p = vim_strchr(look, ',');
8100 if (p == NULL)
8101 p = look + STRLEN(look);
8102 if ((try_match || try_match_word)
8103 && curwin->w_cursor.col >= (colnr_T)(p - look))
8104 {
8105 int match = FALSE;
8106
8107#ifdef FEAT_INS_EXPAND
8108 if (keytyped == KEY_COMPLETE)
8109 {
8110 char_u *s;
8111
8112 /* Just completed a word, check if it starts with "look".
8113 * search back for the start of a word. */
8114 line = ml_get_curline();
8115# ifdef FEAT_MBYTE
8116 if (has_mbyte)
8117 {
8118 char_u *n;
8119
8120 for (s = line + curwin->w_cursor.col; s > line; s = n)
8121 {
8122 n = mb_prevptr(line, s);
8123 if (!vim_iswordp(n))
8124 break;
8125 }
8126 }
8127 else
8128# endif
8129 for (s = line + curwin->w_cursor.col; s > line; --s)
8130 if (!vim_iswordc(s[-1]))
8131 break;
8132 if (s + (p - look) <= line + curwin->w_cursor.col
8133 && (icase
8134 ? MB_STRNICMP(s, look, p - look)
8135 : STRNCMP(s, look, p - look)) == 0)
8136 match = TRUE;
8137 }
8138 else
8139#endif
8140 /* TODO: multi-byte */
8141 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8142 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8143 {
8144 line = ml_get_cursor();
8145 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8146 || !vim_iswordc(line[-(p - look) - 1]))
8147 && (icase
8148 ? MB_STRNICMP(line - (p - look), look, p - look)
8149 : STRNCMP(line - (p - look), look, p - look))
8150 == 0)
8151 match = TRUE;
8152 }
8153 if (match && try_match_word && !try_match)
8154 {
8155 /* "0=word": Check if there are only blanks before the
8156 * word. */
8157 line = ml_get_curline();
8158 if ((int)(skipwhite(line) - line) !=
8159 (int)(curwin->w_cursor.col - (p - look)))
8160 match = FALSE;
8161 }
8162 if (match)
8163 return TRUE;
8164 }
8165 look = p;
8166 }
8167
8168 /*
8169 * ok, it's a boring generic character.
8170 */
8171 else
8172 {
8173 if (try_match && *look == keytyped)
8174 return TRUE;
8175 ++look;
8176 }
8177
8178 /*
8179 * Skip over ", ".
8180 */
8181 look = skip_to_option_part(look);
8182 }
8183 return FALSE;
8184}
8185#endif /* FEAT_CINDENT */
8186
8187#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8188/*
8189 * Map Hebrew keyboard when in hkmap mode.
8190 */
8191 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008192hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193{
8194 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8195 {
8196 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8197 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8198 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8199 static char_u map[26] =
8200 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8201 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8202 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8203 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8204 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8205 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8206 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8207 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8208 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8209
8210 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8211 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8212 /* '-1'='sofit' */
8213 else if (c == 'x')
8214 return 'X';
8215 else if (c == 'q')
8216 return '\''; /* {geresh}={'} */
8217 else if (c == 246)
8218 return ' '; /* \"o --> ' ' for a german keyboard */
8219 else if (c == 228)
8220 return ' '; /* \"a --> ' ' -- / -- */
8221 else if (c == 252)
8222 return ' '; /* \"u --> ' ' -- / -- */
8223#ifdef EBCDIC
8224 else if (islower(c))
8225#else
8226 /* NOTE: islower() does not do the right thing for us on Linux so we
8227 * do this the same was as 5.7 and previous, so it works correctly on
8228 * all systems. Specifically, the e.g. Delete and Arrow keys are
8229 * munged and won't work if e.g. searching for Hebrew text.
8230 */
8231 else if (c >= 'a' && c <= 'z')
8232#endif
8233 return (int)(map[CharOrdLow(c)] + p_aleph);
8234 else
8235 return c;
8236 }
8237 else
8238 {
8239 switch (c)
8240 {
8241 case '`': return ';';
8242 case '/': return '.';
8243 case '\'': return ',';
8244 case 'q': return '/';
8245 case 'w': return '\'';
8246
8247 /* Hebrew letters - set offset from 'a' */
8248 case ',': c = '{'; break;
8249 case '.': c = 'v'; break;
8250 case ';': c = 't'; break;
8251 default: {
8252 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8253
8254#ifdef EBCDIC
8255 /* see note about islower() above */
8256 if (!islower(c))
8257#else
8258 if (c < 'a' || c > 'z')
8259#endif
8260 return c;
8261 c = str[CharOrdLow(c)];
8262 break;
8263 }
8264 }
8265
8266 return (int)(CharOrdLow(c) + p_aleph);
8267 }
8268}
8269#endif
8270
8271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008272ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273{
8274 int need_redraw = FALSE;
8275 int regname;
8276 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008277 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278
8279 /*
8280 * If we are going to wait for a character, show a '"'.
8281 */
8282 pc_status = PC_STATUS_UNSET;
8283 if (redrawing() && !char_avail())
8284 {
8285 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008286 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287
8288 edit_putchar('"', TRUE);
8289#ifdef FEAT_CMDL_INFO
8290 add_to_showcmd_c(Ctrl_R);
8291#endif
8292 }
8293
8294#ifdef USE_ON_FLY_SCROLL
8295 dont_scroll = TRUE; /* disallow scrolling here */
8296#endif
8297
8298 /*
8299 * Don't map the register name. This also prevents the mode message to be
8300 * deleted when ESC is hit.
8301 */
8302 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008303 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8306 {
8307 /* Get a third key for literal register insertion */
8308 literally = regname;
8309#ifdef FEAT_CMDL_INFO
8310 add_to_showcmd_c(literally);
8311#endif
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 }
8315 --no_mapping;
8316
8317#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008318 /* Don't call u_sync() while typing the expression or giving an error
8319 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 ++no_u_sync;
8321 if (regname == '=')
8322 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008323# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008325# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008326 /* Sync undo when evaluating the expression calls setline() or
8327 * append(), so that it can be undone separately. */
8328 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008329
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008331# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 /* Restore the Input Method. */
8333 if (im_on)
8334 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008337 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8338 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008339 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 else
8343 {
8344#endif
8345 if (literally == Ctrl_O || literally == Ctrl_P)
8346 {
8347 /* Append the command to the redo buffer. */
8348 AppendCharToRedobuff(Ctrl_R);
8349 AppendCharToRedobuff(literally);
8350 AppendCharToRedobuff(regname);
8351
8352 do_put(regname, BACKWARD, 1L,
8353 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8354 }
8355 else if (insert_reg(regname, literally) == FAIL)
8356 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008357 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 need_redraw = TRUE; /* remove the '"' */
8359 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008360 else if (stop_insert_mode)
8361 /* When the '=' register was used and a function was invoked that
8362 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8363 * insert anything, need to remove the '"' */
8364 need_redraw = TRUE;
8365
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366#ifdef FEAT_EVAL
8367 }
8368 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008369 if (u_sync_once == 1)
8370 ins_need_undo = TRUE;
8371 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372#endif
8373#ifdef FEAT_CMDL_INFO
8374 clear_showcmd();
8375#endif
8376
8377 /* If the inserted register is empty, we need to remove the '"' */
8378 if (need_redraw || stuff_empty())
8379 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008380
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008381 /* Disallow starting Visual mode here, would get a weird mode. */
8382 if (!vis_active && VIsual_active)
8383 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384}
8385
8386/*
8387 * CTRL-G commands in Insert mode.
8388 */
8389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008390ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391{
8392 int c;
8393
8394#ifdef FEAT_INS_EXPAND
8395 /* Right after CTRL-X the cursor will be after the ruler. */
8396 setcursor();
8397#endif
8398
8399 /*
8400 * Don't map the second key. This also prevents the mode message to be
8401 * deleted when ESC is hit.
8402 */
8403 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008404 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 --no_mapping;
8406 switch (c)
8407 {
8408 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8409 case K_UP:
8410 case Ctrl_K:
8411 case 'k': ins_up(TRUE);
8412 break;
8413
8414 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8415 case K_DOWN:
8416 case Ctrl_J:
8417 case 'j': ins_down(TRUE);
8418 break;
8419
8420 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008421 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008423
8424 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008425 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008426 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008427 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 break;
8429
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008430 /* CTRL-G U: do not break undo with the next char */
8431 case 'U':
8432 /* Allow one left/right cursor movement with the next char,
8433 * without breaking undo. */
8434 dont_sync_undo = MAYBE;
8435 break;
8436
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008438 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 }
8440}
8441
8442/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008443 * CTRL-^ in Insert mode.
8444 */
8445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008446ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008447{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008448 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008449 {
8450 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8451 if (State & LANGMAP)
8452 {
8453 curbuf->b_p_iminsert = B_IMODE_NONE;
8454 State &= ~LANGMAP;
8455 }
8456 else
8457 {
8458 curbuf->b_p_iminsert = B_IMODE_LMAP;
8459 State |= LANGMAP;
8460#ifdef USE_IM_CONTROL
8461 im_set_active(FALSE);
8462#endif
8463 }
8464 }
8465#ifdef USE_IM_CONTROL
8466 else
8467 {
8468 /* There are no ":lmap" mappings, toggle IM */
8469 if (im_get_status())
8470 {
8471 curbuf->b_p_iminsert = B_IMODE_NONE;
8472 im_set_active(FALSE);
8473 }
8474 else
8475 {
8476 curbuf->b_p_iminsert = B_IMODE_IM;
8477 State &= ~LANGMAP;
8478 im_set_active(TRUE);
8479 }
8480 }
8481#endif
8482 set_iminsert_global();
8483 showmode();
8484#ifdef FEAT_GUI
8485 /* may show different cursor shape or color */
8486 if (gui.in_use)
8487 gui_update_cursor(TRUE, FALSE);
8488#endif
8489#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8490 /* Show/unshow value of 'keymap' in status lines. */
8491 status_redraw_curbuf();
8492#endif
8493}
8494
8495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 * Handle ESC in insert mode.
8497 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8498 * insert.
8499 */
8500 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008501ins_esc(
8502 long *count,
8503 int cmdchar,
8504 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
8506 int temp;
8507 static int disabled_redraw = FALSE;
8508
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008509#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008510 check_spell_redraw();
8511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512#if defined(FEAT_HANGULIN)
8513# if defined(ESC_CHG_TO_ENG_MODE)
8514 hangul_input_state_set(0);
8515# endif
8516 if (composing_hangul)
8517 {
8518 push_raw_key(composing_hangul_buffer, 2);
8519 composing_hangul = 0;
8520 }
8521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522
8523 temp = curwin->w_cursor.col;
8524 if (disabled_redraw)
8525 {
8526 --RedrawingDisabled;
8527 disabled_redraw = FALSE;
8528 }
8529 if (!arrow_used)
8530 {
8531 /*
8532 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008533 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8534 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 */
8536 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008537 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538
8539 /*
8540 * Repeating insert may take a long time. Check for
8541 * interrupt now and then.
8542 */
8543 if (*count > 0)
8544 {
8545 line_breakcheck();
8546 if (got_int)
8547 *count = 0;
8548 }
8549
8550 if (--*count > 0) /* repeat what was typed */
8551 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008552 /* Vi repeats the insert without replacing characters. */
8553 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8554 State &= ~REPLACE_FLAG;
8555
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 (void)start_redo_ins();
8557 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008558 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 ++RedrawingDisabled;
8560 disabled_redraw = TRUE;
8561 return FALSE; /* repeat the insert */
8562 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008563 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 undisplay_dollar();
8565 }
8566
8567 /* When an autoindent was removed, curswant stays after the
8568 * indent */
8569 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8570 curwin->w_set_curswant = TRUE;
8571
8572 /* Remember the last Insert position in the '^ mark. */
8573 if (!cmdmod.keepjumps)
8574 curbuf->b_last_insert = curwin->w_cursor;
8575
8576 /*
8577 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008578 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008580 if (!nomove
8581 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582#ifdef FEAT_VIRTUALEDIT
8583 || curwin->w_cursor.coladd > 0
8584#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008585 )
8586 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008587 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588#ifdef FEAT_RIGHTLEFT
8589 && !revins_on
8590#endif
8591 )
8592 {
8593#ifdef FEAT_VIRTUALEDIT
8594 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8595 {
8596 oneleft();
8597 if (restart_edit != NUL)
8598 ++curwin->w_cursor.coladd;
8599 }
8600 else
8601#endif
8602 {
8603 --curwin->w_cursor.col;
8604#ifdef FEAT_MBYTE
8605 /* Correct cursor for multi-byte character. */
8606 if (has_mbyte)
8607 mb_adjust_cursor();
8608#endif
8609 }
8610 }
8611
8612#ifdef USE_IM_CONTROL
8613 /* Disable IM to allow typing English directly for Normal mode commands.
8614 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8615 * well). */
8616 if (!(State & LANGMAP))
8617 im_save_status(&curbuf->b_p_iminsert);
8618 im_set_active(FALSE);
8619#endif
8620
8621 State = NORMAL;
8622 /* need to position cursor again (e.g. when on a TAB ) */
8623 changed_cline_bef_curs();
8624
8625#ifdef FEAT_MOUSE
8626 setmouse();
8627#endif
8628#ifdef CURSOR_SHAPE
8629 ui_cursor_shape(); /* may show different cursor shape */
8630#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008631 if (!p_ek)
8632 /* Re-enable bracketed paste mode. */
8633 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634
8635 /*
8636 * When recording or for CTRL-O, need to display the new mode.
8637 * Otherwise remove the mode message.
8638 */
8639 if (Recording || restart_edit != NUL)
8640 showmode();
8641 else if (p_smd)
8642 MSG("");
8643
8644 return TRUE; /* exit Insert mode */
8645}
8646
8647#ifdef FEAT_RIGHTLEFT
8648/*
8649 * Toggle language: hkmap and revins_on.
8650 * Move to end of reverse inserted text.
8651 */
8652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008653ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654{
8655 if (revins_on && revins_chars && revins_scol >= 0)
8656 {
8657 while (gchar_cursor() != NUL && revins_chars--)
8658 ++curwin->w_cursor.col;
8659 }
8660 p_ri = !p_ri;
8661 revins_on = (State == INSERT && p_ri);
8662 if (revins_on)
8663 {
8664 revins_scol = curwin->w_cursor.col;
8665 revins_legal++;
8666 revins_chars = 0;
8667 undisplay_dollar();
8668 }
8669 else
8670 revins_scol = -1;
8671#ifdef FEAT_FKMAP
8672 if (p_altkeymap)
8673 {
8674 /*
8675 * to be consistent also for redo command, using '.'
8676 * set arrow_used to true and stop it - causing to redo
8677 * characters entered in one mode (normal/reverse insert).
8678 */
8679 arrow_used = TRUE;
8680 (void)stop_arrow();
8681 p_fkmap = curwin->w_p_rl ^ p_ri;
8682 if (p_fkmap && p_ri)
8683 State = INSERT;
8684 }
8685 else
8686#endif
8687 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8688 showmode();
8689}
8690#endif
8691
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692/*
8693 * If 'keymodel' contains "startsel", may start selection.
8694 * Returns TRUE when a CTRL-O and other keys stuffed.
8695 */
8696 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008697ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699 if (km_startsel)
8700 switch (c)
8701 {
8702 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 case K_PAGEUP:
8705 case K_KPAGEUP:
8706 case K_PAGEDOWN:
8707 case K_KPAGEDOWN:
8708# ifdef MACOS
8709 case K_LEFT:
8710 case K_RIGHT:
8711 case K_UP:
8712 case K_DOWN:
8713 case K_END:
8714 case K_HOME:
8715# endif
8716 if (!(mod_mask & MOD_MASK_SHIFT))
8717 break;
8718 /* FALLTHROUGH */
8719 case K_S_LEFT:
8720 case K_S_RIGHT:
8721 case K_S_UP:
8722 case K_S_DOWN:
8723 case K_S_END:
8724 case K_S_HOME:
8725 /* Start selection right away, the cursor can move with
8726 * CTRL-O when beyond the end of the line. */
8727 start_selection();
8728
8729 /* Execute the key in (insert) Select mode. */
8730 stuffcharReadbuff(Ctrl_O);
8731 if (mod_mask)
8732 {
8733 char_u buf[4];
8734
8735 buf[0] = K_SPECIAL;
8736 buf[1] = KS_MODIFIER;
8737 buf[2] = mod_mask;
8738 buf[3] = NUL;
8739 stuffReadbuff(buf);
8740 }
8741 stuffcharReadbuff(c);
8742 return TRUE;
8743 }
8744 return FALSE;
8745}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746
8747/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008748 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008749 */
8750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008751ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008752{
8753#ifdef FEAT_FKMAP
8754 if (p_fkmap && p_ri)
8755 {
8756 beep_flush();
8757 EMSG(farsi_text_3); /* encoded in Farsi */
8758 return;
8759 }
8760#endif
8761
8762#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008763# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008764 set_vim_var_string(VV_INSERTMODE,
8765 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008766# ifdef FEAT_VREPLACE
8767 replaceState == VREPLACE ? "v" :
8768# endif
8769 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008770# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008771 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8772#endif
8773 if (State & REPLACE_FLAG)
8774 State = INSERT | (State & LANGMAP);
8775 else
8776 State = replaceState | (State & LANGMAP);
8777 AppendCharToRedobuff(K_INS);
8778 showmode();
8779#ifdef CURSOR_SHAPE
8780 ui_cursor_shape(); /* may show different cursor shape */
8781#endif
8782}
8783
8784/*
8785 * Pressed CTRL-O in Insert mode.
8786 */
8787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008788ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008789{
8790#ifdef FEAT_VREPLACE
8791 if (State & VREPLACE_FLAG)
8792 restart_edit = 'V';
8793 else
8794#endif
8795 if (State & REPLACE_FLAG)
8796 restart_edit = 'R';
8797 else
8798 restart_edit = 'I';
8799#ifdef FEAT_VIRTUALEDIT
8800 if (virtual_active())
8801 ins_at_eol = FALSE; /* cursor always keeps its column */
8802 else
8803#endif
8804 ins_at_eol = (gchar_cursor() == NUL);
8805}
8806
8807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 * If the cursor is on an indent, ^T/^D insert/delete one
8809 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008810 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 * with vi. But vi only supports ^T and ^D after an
8812 * autoindent, we support it everywhere.
8813 */
8814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008815ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816{
8817 if (stop_arrow() == FAIL)
8818 return;
8819 AppendCharToRedobuff(c);
8820
8821 /*
8822 * 0^D and ^^D: remove all indent.
8823 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008824 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8825 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 {
8827 --curwin->w_cursor.col;
8828 (void)del_char(FALSE); /* delete the '^' or '0' */
8829 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8830 if (State & REPLACE_FLAG)
8831 replace_pop_ins();
8832 if (lastc == '^')
8833 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008834 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 }
8836 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008837 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838
8839 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8840 did_ai = FALSE;
8841#ifdef FEAT_SMARTINDENT
8842 did_si = FALSE;
8843 can_si = FALSE;
8844 can_si_back = FALSE;
8845#endif
8846#ifdef FEAT_CINDENT
8847 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8848#endif
8849}
8850
8851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008852ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853{
8854 int temp;
8855
8856 if (stop_arrow() == FAIL)
8857 return;
8858 if (gchar_cursor() == NUL) /* delete newline */
8859 {
8860 temp = curwin->w_cursor.col;
8861 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008862 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008863 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 else
8865 curwin->w_cursor.col = temp;
8866 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008867 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8868 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 did_ai = FALSE;
8870#ifdef FEAT_SMARTINDENT
8871 did_si = FALSE;
8872 can_si = FALSE;
8873 can_si_back = FALSE;
8874#endif
8875 AppendCharToRedobuff(K_DEL);
8876}
8877
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008878static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008879
8880/*
8881 * Delete one character for ins_bs().
8882 */
8883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008884ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008885{
8886 dec_cursor();
8887 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8888 if (State & REPLACE_FLAG)
8889 {
8890 /* Don't delete characters before the insert point when in
8891 * Replace mode */
8892 if (curwin->w_cursor.lnum != Insstart.lnum
8893 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008894 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008895 }
8896 else
8897 (void)del_char(FALSE);
8898}
8899
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900/*
8901 * Handle Backspace, delete-word and delete-line in Insert mode.
8902 * Return TRUE when backspace was actually used.
8903 */
8904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008905ins_bs(
8906 int c,
8907 int mode,
8908 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
8910 linenr_T lnum;
8911 int cc;
8912 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008913 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 colnr_T mincol;
8915 int did_backspace = FALSE;
8916 int in_indent;
8917 int oldState;
8918#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008919 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920#endif
8921
8922 /*
8923 * can't delete anything in an empty file
8924 * can't backup past first character in buffer
8925 * can't backup past starting point unless 'backspace' > 1
8926 * can backup to a previous line if 'backspace' == 0
8927 */
8928 if ( bufempty()
8929 || (
8930#ifdef FEAT_RIGHTLEFT
8931 !revins_on &&
8932#endif
8933 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8934 || (!can_bs(BS_START)
8935 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008936 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8937 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8939 && curwin->w_cursor.col <= ai_col)
8940 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8941 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008942 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 return FALSE;
8944 }
8945
8946 if (stop_arrow() == FAIL)
8947 return FALSE;
8948 in_indent = inindent(0);
8949#ifdef FEAT_CINDENT
8950 if (in_indent)
8951 can_cindent = FALSE;
8952#endif
8953#ifdef FEAT_COMMENTS
8954 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8955#endif
8956#ifdef FEAT_RIGHTLEFT
8957 if (revins_on) /* put cursor after last inserted char */
8958 inc_cursor();
8959#endif
8960
8961#ifdef FEAT_VIRTUALEDIT
8962 /* Virtualedit:
8963 * BACKSPACE_CHAR eats a virtual space
8964 * BACKSPACE_WORD eats all coladd
8965 * BACKSPACE_LINE eats all coladd and keeps going
8966 */
8967 if (curwin->w_cursor.coladd > 0)
8968 {
8969 if (mode == BACKSPACE_CHAR)
8970 {
8971 --curwin->w_cursor.coladd;
8972 return TRUE;
8973 }
8974 if (mode == BACKSPACE_WORD)
8975 {
8976 curwin->w_cursor.coladd = 0;
8977 return TRUE;
8978 }
8979 curwin->w_cursor.coladd = 0;
8980 }
8981#endif
8982
8983 /*
8984 * delete newline!
8985 */
8986 if (curwin->w_cursor.col == 0)
8987 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008988 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008989 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990#ifdef FEAT_RIGHTLEFT
8991 || revins_on
8992#endif
8993 )
8994 {
8995 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8996 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8997 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008998 --Insstart.lnum;
8999 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 }
9001 /*
9002 * In replace mode:
9003 * cc < 0: NL was inserted, delete it
9004 * cc >= 0: NL was replaced, put original characters back
9005 */
9006 cc = -1;
9007 if (State & REPLACE_FLAG)
9008 cc = replace_pop(); /* returns -1 if NL was inserted */
9009 /*
9010 * In replace mode, in the line we started replacing, we only move the
9011 * cursor.
9012 */
9013 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9014 {
9015 dec_cursor();
9016 }
9017 else
9018 {
9019#ifdef FEAT_VREPLACE
9020 if (!(State & VREPLACE_FLAG)
9021 || curwin->w_cursor.lnum > orig_line_count)
9022#endif
9023 {
9024 temp = gchar_cursor(); /* remember current char */
9025 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009026
9027 /* When "aw" is in 'formatoptions' we must delete the space at
9028 * the end of the line, otherwise the line will be broken
9029 * again when auto-formatting. */
9030 if (has_format_option(FO_AUTO)
9031 && has_format_option(FO_WHITE_PAR))
9032 {
9033 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9034 TRUE);
9035 int len;
9036
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009037 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009038 if (len > 0 && ptr[len - 1] == ' ')
9039 ptr[len - 1] = NUL;
9040 }
9041
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009042 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 if (temp == NUL && gchar_cursor() != NUL)
9044 inc_cursor();
9045 }
9046#ifdef FEAT_VREPLACE
9047 else
9048 dec_cursor();
9049#endif
9050
9051 /*
9052 * In REPLACE mode we have to put back the text that was replaced
9053 * by the NL. On the replace stack is first a NUL-terminated
9054 * sequence of characters that were deleted and then the
9055 * characters that NL replaced.
9056 */
9057 if (State & REPLACE_FLAG)
9058 {
9059 /*
9060 * Do the next ins_char() in NORMAL state, to
9061 * prevent ins_char() from replacing characters and
9062 * avoiding showmatch().
9063 */
9064 oldState = State;
9065 State = NORMAL;
9066 /*
9067 * restore characters (blanks) deleted after cursor
9068 */
9069 while (cc > 0)
9070 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009071 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072#ifdef FEAT_MBYTE
9073 mb_replace_pop_ins(cc);
9074#else
9075 ins_char(cc);
9076#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009077 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 cc = replace_pop();
9079 }
9080 /* restore the characters that NL replaced */
9081 replace_pop_ins();
9082 State = oldState;
9083 }
9084 }
9085 did_ai = FALSE;
9086 }
9087 else
9088 {
9089 /*
9090 * Delete character(s) before the cursor.
9091 */
9092#ifdef FEAT_RIGHTLEFT
9093 if (revins_on) /* put cursor on last inserted char */
9094 dec_cursor();
9095#endif
9096 mincol = 0;
9097 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009098 if (mode == BACKSPACE_LINE
9099 && (curbuf->b_p_ai
9100#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009101 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009102#endif
9103 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104#ifdef FEAT_RIGHTLEFT
9105 && !revins_on
9106#endif
9107 )
9108 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009109 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009111 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009113 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 }
9115
9116 /*
9117 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9118 */
9119 if ( mode == BACKSPACE_CHAR
9120 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009121 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009122 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 && (*(ml_get_cursor() - 1) == TAB
9124 || (*(ml_get_cursor() - 1) == ' '
9125 && (!*inserted_space_p
9126 || arrow_used))))))
9127 {
9128 int ts;
9129 colnr_T vcol;
9130 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009131 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132
9133 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009134 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009135 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009137 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 /* Compute the virtual column where we want to be. Since
9139 * 'showbreak' may get in the way, need to get the last column of
9140 * the previous character. */
9141 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009142 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 dec_cursor();
9144 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9145 inc_cursor();
9146 want_vcol = (want_vcol / ts) * ts;
9147
9148 /* delete characters until we are at or before want_vcol */
9149 while (vcol > want_vcol
9150 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009151 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152
9153 /* insert extra spaces until we are at want_vcol */
9154 while (vcol < want_vcol)
9155 {
9156 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009157 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9158 && curwin->w_cursor.col < Insstart_orig.col)
9159 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160
9161#ifdef FEAT_VREPLACE
9162 if (State & VREPLACE_FLAG)
9163 ins_char(' ');
9164 else
9165#endif
9166 {
9167 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009168 if ((State & REPLACE_FLAG))
9169 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 }
9171 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9172 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009173
9174 /* If we are now back where we started delete one character. Can
9175 * happen when using 'sts' and 'linebreak'. */
9176 if (vcol >= start_vcol)
9177 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 }
9179
9180 /*
9181 * Delete upto starting point, start of line or previous word.
9182 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009183 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009185#ifdef FEAT_MBYTE
9186 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009188 if (has_mbyte)
9189 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009191 do
9192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009194 if (!revins_on) /* put cursor on char to be deleted */
9195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009197
9198 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009200 /* look multi-byte character class */
9201 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009203 prev_cclass = cclass;
9204 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009207
9208 /* start of word? */
9209 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9210 {
9211 mode = BACKSPACE_WORD_NOT_SPACE;
9212 temp = vim_iswordc(cc);
9213 }
9214 /* end of word? */
9215 else if (mode == BACKSPACE_WORD_NOT_SPACE
9216 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9217#ifdef FEAT_MBYTE
9218 || prev_cclass != cclass
9219#endif
9220 ))
9221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009223 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009225 inc_cursor();
9226#ifdef FEAT_RIGHTLEFT
9227 else if (State & REPLACE_FLAG)
9228 dec_cursor();
9229#endif
9230 break;
9231 }
9232 if (State & REPLACE_FLAG)
9233 replace_do_bs(-1);
9234 else
9235 {
9236#ifdef FEAT_MBYTE
9237 if (enc_utf8 && p_deco)
9238 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9239#endif
9240 (void)del_char(FALSE);
9241#ifdef FEAT_MBYTE
9242 /*
9243 * If there are combining characters and 'delcombine' is set
9244 * move the cursor back. Don't back up before the base
9245 * character.
9246 */
9247 if (enc_utf8 && p_deco && cpc[0] != NUL)
9248 inc_cursor();
9249#endif
9250#ifdef FEAT_RIGHTLEFT
9251 if (revins_chars)
9252 {
9253 revins_chars--;
9254 revins_legal++;
9255 }
9256 if (revins_on && gchar_cursor() == NUL)
9257 break;
9258#endif
9259 }
9260 /* Just a single backspace?: */
9261 if (mode == BACKSPACE_CHAR)
9262 break;
9263 } while (
9264#ifdef FEAT_RIGHTLEFT
9265 revins_on ||
9266#endif
9267 (curwin->w_cursor.col > mincol
9268 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9269 || curwin->w_cursor.col != Insstart_orig.col)));
9270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 did_backspace = TRUE;
9272 }
9273#ifdef FEAT_SMARTINDENT
9274 did_si = FALSE;
9275 can_si = FALSE;
9276 can_si_back = FALSE;
9277#endif
9278 if (curwin->w_cursor.col <= 1)
9279 did_ai = FALSE;
9280 /*
9281 * It's a little strange to put backspaces into the redo
9282 * buffer, but it makes auto-indent a lot easier to deal
9283 * with.
9284 */
9285 AppendCharToRedobuff(c);
9286
9287 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009288 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9289 && curwin->w_cursor.col < Insstart_orig.col)
9290 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291
9292 /* vi behaviour: the cursor moves backward but the character that
9293 * was there remains visible
9294 * Vim behaviour: the cursor moves backward and the character that
9295 * was there is erased from the screen.
9296 * We can emulate the vi behaviour by pretending there is a dollar
9297 * displayed even when there isn't.
9298 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009299 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 dollar_vcol = curwin->w_virtcol;
9301
Bram Moolenaarce3be472008-01-14 19:12:28 +00009302#ifdef FEAT_FOLDING
9303 /* When deleting a char the cursor line must never be in a closed fold.
9304 * E.g., when 'foldmethod' is indent and deleting the first non-white
9305 * char before a Tab. */
9306 if (did_backspace)
9307 foldOpenCursor();
9308#endif
9309
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 return did_backspace;
9311}
9312
9313#ifdef FEAT_MOUSE
9314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009315ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316{
9317 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009318 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319
9320# ifdef FEAT_GUI
9321 /* When GUI is active, also move/paste when 'mouse' is empty */
9322 if (!gui.in_use)
9323# endif
9324 if (!mouse_has(MOUSE_INSERT))
9325 return;
9326
9327 undisplay_dollar();
9328 tpos = curwin->w_cursor;
9329 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9330 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009331#ifdef FEAT_WINDOWS
9332 win_T *new_curwin = curwin;
9333
9334 if (curwin != old_curwin && win_valid(old_curwin))
9335 {
9336 /* Mouse took us to another window. We need to go back to the
9337 * previous one to stop insert there properly. */
9338 curwin = old_curwin;
9339 curbuf = curwin->w_buffer;
9340 }
9341#endif
9342 start_arrow(curwin == old_curwin ? &tpos : NULL);
9343#ifdef FEAT_WINDOWS
9344 if (curwin != new_curwin && win_valid(new_curwin))
9345 {
9346 curwin = new_curwin;
9347 curbuf = curwin->w_buffer;
9348 }
9349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350# ifdef FEAT_CINDENT
9351 can_cindent = TRUE;
9352# endif
9353 }
9354
9355#ifdef FEAT_WINDOWS
9356 /* redraw status lines (in case another window became active) */
9357 redraw_statuslines();
9358#endif
9359}
9360
9361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009362ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363{
9364 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009365# if defined(FEAT_WINDOWS)
9366 win_T *old_curwin = curwin;
9367# endif
9368# ifdef FEAT_INS_EXPAND
9369 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370# endif
9371
9372 tpos = curwin->w_cursor;
9373
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009374# ifdef FEAT_WINDOWS
9375 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 {
9377 int row, col;
9378
9379 row = mouse_row;
9380 col = mouse_col;
9381
9382 /* find the window at the pointer coordinates */
9383 curwin = mouse_find_win(&row, &col);
9384 curbuf = curwin->w_buffer;
9385 }
9386 if (curwin == old_curwin)
9387# endif
9388 undisplay_dollar();
9389
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009390# ifdef FEAT_INS_EXPAND
9391 /* Don't scroll the window in which completion is being done. */
9392 if (!pum_visible()
9393# if defined(FEAT_WINDOWS)
9394 || curwin != old_curwin
9395# endif
9396 )
9397# endif
9398 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009399 if (dir == MSCR_DOWN || dir == MSCR_UP)
9400 {
9401 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9402 scroll_redraw(dir,
9403 (long)(curwin->w_botline - curwin->w_topline));
9404 else
9405 scroll_redraw(dir, 3L);
9406 }
9407#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009408 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009409 {
9410 int val, step = 6;
9411
9412 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9413 step = W_WIDTH(curwin);
9414 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9415 if (val < 0)
9416 val = 0;
9417 gui_do_horiz_scroll(val, TRUE);
9418 }
9419#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009420# ifdef FEAT_INS_EXPAND
9421 did_scroll = TRUE;
9422# endif
9423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009425# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 curwin->w_redr_status = TRUE;
9427
9428 curwin = old_curwin;
9429 curbuf = curwin->w_buffer;
9430# endif
9431
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009432# ifdef FEAT_INS_EXPAND
9433 /* The popup menu may overlay the window, need to redraw it.
9434 * TODO: Would be more efficient to only redraw the windows that are
9435 * overlapped by the popup menu. */
9436 if (pum_visible() && did_scroll)
9437 {
9438 redraw_all_later(NOT_VALID);
9439 ins_compl_show_pum();
9440 }
9441# endif
9442
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 if (!equalpos(curwin->w_cursor, tpos))
9444 {
9445 start_arrow(&tpos);
9446# ifdef FEAT_CINDENT
9447 can_cindent = TRUE;
9448# endif
9449 }
9450}
9451#endif
9452
Bram Moolenaarec2da362017-01-21 20:04:22 +01009453/*
9454 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9455 * P_PE literally.
9456 * When "drop" is TRUE then consume the text and drop it.
9457 */
9458 int
9459bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9460{
9461 int c;
9462 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9463 int idx = 0;
9464 char_u *end = find_termcode((char_u *)"PE");
9465 int ret_char = -1;
9466 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009467 int save_paste = p_paste;
9468 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009469
9470 /* If the end code is too long we can't detect it, read everything. */
9471 if (STRLEN(end) >= NUMBUFLEN)
9472 end = NULL;
9473 ++no_mapping;
9474 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009475 p_paste = TRUE;
9476 curbuf->b_p_ai = FALSE;
9477
Bram Moolenaarec2da362017-01-21 20:04:22 +01009478 for (;;)
9479 {
9480 /* When the end is not defined read everything. */
9481 if (end == NULL && vpeekc() == NUL)
9482 break;
9483 c = plain_vgetc();
9484#ifdef FEAT_MBYTE
9485 if (has_mbyte)
9486 idx += (*mb_char2bytes)(c, buf + idx);
9487 else
9488#endif
9489 buf[idx++] = c;
9490 buf[idx] = NUL;
9491 if (end != NUL && STRNCMP(buf, end, idx) == 0)
9492 {
9493 if (end[idx] == NUL)
9494 break; /* Found the end of paste code. */
9495 continue;
9496 }
9497 if (!drop)
9498 {
9499 switch (mode)
9500 {
9501 case PASTE_CMDLINE:
9502 put_on_cmdline(buf, idx, TRUE);
9503 break;
9504
9505 case PASTE_EX:
9506 if (gap != NULL && ga_grow(gap, idx) == OK)
9507 {
9508 mch_memmove((char *)gap->ga_data + gap->ga_len,
9509 buf, (size_t)idx);
9510 gap->ga_len += idx;
9511 }
9512 break;
9513
9514 case PASTE_INSERT:
9515 if (stop_arrow() == OK)
9516 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009517 c = buf[0];
9518 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9519 ins_eol(c);
9520 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009521 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009522 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009523 AppendToRedobuffLit(buf, idx);
9524 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009525 }
9526 break;
9527
9528 case PASTE_ONE_CHAR:
9529 if (ret_char == -1)
9530 {
9531#ifdef FEAT_MBYTE
9532 if (has_mbyte)
9533 ret_char = (*mb_ptr2char)(buf);
9534 else
9535#endif
9536 ret_char = buf[0];
9537 }
9538 break;
9539 }
9540 }
9541 idx = 0;
9542 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009543
Bram Moolenaarec2da362017-01-21 20:04:22 +01009544 --no_mapping;
9545 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009546 p_paste = save_paste;
9547 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009548
9549 return ret_char;
9550}
9551
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009552#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009554ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009555{
9556 /* We will be leaving the current window, unless closing another tab. */
9557 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9558 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9559 {
9560 undisplay_dollar();
9561 start_arrow(&curwin->w_cursor);
9562# ifdef FEAT_CINDENT
9563 can_cindent = TRUE;
9564# endif
9565 }
9566
9567 if (c == K_TABLINE)
9568 goto_tabpage(current_tab);
9569 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009570 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009571 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009572 redraw_statuslines(); /* will redraw the tabline when needed */
9573 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009574}
9575#endif
9576
9577#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009579ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
9581 pos_T tpos;
9582
9583 undisplay_dollar();
9584 tpos = curwin->w_cursor;
9585 if (gui_do_scroll())
9586 {
9587 start_arrow(&tpos);
9588# ifdef FEAT_CINDENT
9589 can_cindent = TRUE;
9590# endif
9591 }
9592}
9593
9594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009595ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
9597 pos_T tpos;
9598
9599 undisplay_dollar();
9600 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009601 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 {
9603 start_arrow(&tpos);
9604# ifdef FEAT_CINDENT
9605 can_cindent = TRUE;
9606# endif
9607 }
9608}
9609#endif
9610
9611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009612ins_left(
9613 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615 pos_T tpos;
9616
9617#ifdef FEAT_FOLDING
9618 if ((fdo_flags & FDO_HOR) && KeyTyped)
9619 foldOpenCursor();
9620#endif
9621 undisplay_dollar();
9622 tpos = curwin->w_cursor;
9623 if (oneleft() == OK)
9624 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009625#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9626 /* Only call start_arrow() when not busy with preediting, it will
9627 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9628 if (!im_is_preediting())
9629#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009630 {
9631 start_arrow_with_change(&tpos, end_change);
9632 if (!end_change)
9633 AppendCharToRedobuff(K_LEFT);
9634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635#ifdef FEAT_RIGHTLEFT
9636 /* If exit reversed string, position is fixed */
9637 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9638 revins_legal++;
9639 revins_chars++;
9640#endif
9641 }
9642
9643 /*
9644 * if 'whichwrap' set for cursor in insert mode may go to
9645 * previous line
9646 */
9647 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9648 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009649 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 start_arrow(&tpos);
9651 --(curwin->w_cursor.lnum);
9652 coladvance((colnr_T)MAXCOL);
9653 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9654 }
9655 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009656 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009657 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658}
9659
9660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009661ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662{
9663 pos_T tpos;
9664
9665#ifdef FEAT_FOLDING
9666 if ((fdo_flags & FDO_HOR) && KeyTyped)
9667 foldOpenCursor();
9668#endif
9669 undisplay_dollar();
9670 tpos = curwin->w_cursor;
9671 if (c == K_C_HOME)
9672 curwin->w_cursor.lnum = 1;
9673 curwin->w_cursor.col = 0;
9674#ifdef FEAT_VIRTUALEDIT
9675 curwin->w_cursor.coladd = 0;
9676#endif
9677 curwin->w_curswant = 0;
9678 start_arrow(&tpos);
9679}
9680
9681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009682ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683{
9684 pos_T tpos;
9685
9686#ifdef FEAT_FOLDING
9687 if ((fdo_flags & FDO_HOR) && KeyTyped)
9688 foldOpenCursor();
9689#endif
9690 undisplay_dollar();
9691 tpos = curwin->w_cursor;
9692 if (c == K_C_END)
9693 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9694 coladvance((colnr_T)MAXCOL);
9695 curwin->w_curswant = MAXCOL;
9696
9697 start_arrow(&tpos);
9698}
9699
9700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009701ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
9703#ifdef FEAT_FOLDING
9704 if ((fdo_flags & FDO_HOR) && KeyTyped)
9705 foldOpenCursor();
9706#endif
9707 undisplay_dollar();
9708 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9709 {
9710 start_arrow(&curwin->w_cursor);
9711 (void)bck_word(1L, FALSE, FALSE);
9712 curwin->w_set_curswant = TRUE;
9713 }
9714 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009715 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716}
9717
9718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009719ins_right(
9720 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
9722#ifdef FEAT_FOLDING
9723 if ((fdo_flags & FDO_HOR) && KeyTyped)
9724 foldOpenCursor();
9725#endif
9726 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009727 if (gchar_cursor() != NUL
9728#ifdef FEAT_VIRTUALEDIT
9729 || virtual_active()
9730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 )
9732 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009733 start_arrow_with_change(&curwin->w_cursor, end_change);
9734 if (!end_change)
9735 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 curwin->w_set_curswant = TRUE;
9737#ifdef FEAT_VIRTUALEDIT
9738 if (virtual_active())
9739 oneright();
9740 else
9741#endif
9742 {
9743#ifdef FEAT_MBYTE
9744 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009745 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 else
9747#endif
9748 ++curwin->w_cursor.col;
9749 }
9750
9751#ifdef FEAT_RIGHTLEFT
9752 revins_legal++;
9753 if (revins_chars)
9754 revins_chars--;
9755#endif
9756 }
9757 /* if 'whichwrap' set for cursor in insert mode, may move the
9758 * cursor to the next line */
9759 else if (vim_strchr(p_ww, ']') != NULL
9760 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9761 {
9762 start_arrow(&curwin->w_cursor);
9763 curwin->w_set_curswant = TRUE;
9764 ++curwin->w_cursor.lnum;
9765 curwin->w_cursor.col = 0;
9766 }
9767 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009768 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009769 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770}
9771
9772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009773ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774{
9775#ifdef FEAT_FOLDING
9776 if ((fdo_flags & FDO_HOR) && KeyTyped)
9777 foldOpenCursor();
9778#endif
9779 undisplay_dollar();
9780 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9781 || gchar_cursor() != NUL)
9782 {
9783 start_arrow(&curwin->w_cursor);
9784 (void)fwd_word(1L, FALSE, 0);
9785 curwin->w_set_curswant = TRUE;
9786 }
9787 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009788 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789}
9790
9791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009792ins_up(
9793 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794{
9795 pos_T tpos;
9796 linenr_T old_topline = curwin->w_topline;
9797#ifdef FEAT_DIFF
9798 int old_topfill = curwin->w_topfill;
9799#endif
9800
9801 undisplay_dollar();
9802 tpos = curwin->w_cursor;
9803 if (cursor_up(1L, TRUE) == OK)
9804 {
9805 if (startcol)
9806 coladvance(getvcol_nolist(&Insstart));
9807 if (old_topline != curwin->w_topline
9808#ifdef FEAT_DIFF
9809 || old_topfill != curwin->w_topfill
9810#endif
9811 )
9812 redraw_later(VALID);
9813 start_arrow(&tpos);
9814#ifdef FEAT_CINDENT
9815 can_cindent = TRUE;
9816#endif
9817 }
9818 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009819 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820}
9821
9822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009823ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824{
9825 pos_T tpos;
9826
9827 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009828
9829#ifdef FEAT_WINDOWS
9830 if (mod_mask & MOD_MASK_CTRL)
9831 {
9832 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009833 if (first_tabpage->tp_next != NULL)
9834 {
9835 start_arrow(&curwin->w_cursor);
9836 goto_tabpage(-1);
9837 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009838 return;
9839 }
9840#endif
9841
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 tpos = curwin->w_cursor;
9843 if (onepage(BACKWARD, 1L) == OK)
9844 {
9845 start_arrow(&tpos);
9846#ifdef FEAT_CINDENT
9847 can_cindent = TRUE;
9848#endif
9849 }
9850 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009851 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852}
9853
9854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009855ins_down(
9856 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857{
9858 pos_T tpos;
9859 linenr_T old_topline = curwin->w_topline;
9860#ifdef FEAT_DIFF
9861 int old_topfill = curwin->w_topfill;
9862#endif
9863
9864 undisplay_dollar();
9865 tpos = curwin->w_cursor;
9866 if (cursor_down(1L, TRUE) == OK)
9867 {
9868 if (startcol)
9869 coladvance(getvcol_nolist(&Insstart));
9870 if (old_topline != curwin->w_topline
9871#ifdef FEAT_DIFF
9872 || old_topfill != curwin->w_topfill
9873#endif
9874 )
9875 redraw_later(VALID);
9876 start_arrow(&tpos);
9877#ifdef FEAT_CINDENT
9878 can_cindent = TRUE;
9879#endif
9880 }
9881 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009882 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883}
9884
9885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009886ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887{
9888 pos_T tpos;
9889
9890 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009891
9892#ifdef FEAT_WINDOWS
9893 if (mod_mask & MOD_MASK_CTRL)
9894 {
9895 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009896 if (first_tabpage->tp_next != NULL)
9897 {
9898 start_arrow(&curwin->w_cursor);
9899 goto_tabpage(0);
9900 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009901 return;
9902 }
9903#endif
9904
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 tpos = curwin->w_cursor;
9906 if (onepage(FORWARD, 1L) == OK)
9907 {
9908 start_arrow(&tpos);
9909#ifdef FEAT_CINDENT
9910 can_cindent = TRUE;
9911#endif
9912 }
9913 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009914 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915}
9916
9917#ifdef FEAT_DND
9918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009919ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920{
9921 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9922}
9923#endif
9924
9925/*
9926 * Handle TAB in Insert or Replace mode.
9927 * Return TRUE when the TAB needs to be inserted like a normal character.
9928 */
9929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009930ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931{
9932 int ind;
9933 int i;
9934 int temp;
9935
9936 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9937 Insstart_blank_vcol = get_nolist_virtcol();
9938 if (echeck_abbr(TAB + ABBR_OFF))
9939 return FALSE;
9940
9941 ind = inindent(0);
9942#ifdef FEAT_CINDENT
9943 if (ind)
9944 can_cindent = FALSE;
9945#endif
9946
9947 /*
9948 * When nothing special, insert TAB like a normal character
9949 */
9950 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009951 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009952 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 return TRUE;
9954
9955 if (stop_arrow() == FAIL)
9956 return TRUE;
9957
9958 did_ai = FALSE;
9959#ifdef FEAT_SMARTINDENT
9960 did_si = FALSE;
9961 can_si = FALSE;
9962 can_si_back = FALSE;
9963#endif
9964 AppendToRedobuff((char_u *)"\t");
9965
9966 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009967 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009968 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9969 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 else /* otherwise use 'tabstop' */
9971 temp = (int)curbuf->b_p_ts;
9972 temp -= get_nolist_virtcol() % temp;
9973
9974 /*
9975 * Insert the first space with ins_char(). It will delete one char in
9976 * replace mode. Insert the rest with ins_str(); it will not delete any
9977 * chars. For VREPLACE mode, we use ins_char() for all characters.
9978 */
9979 ins_char(' ');
9980 while (--temp > 0)
9981 {
9982#ifdef FEAT_VREPLACE
9983 if (State & VREPLACE_FLAG)
9984 ins_char(' ');
9985 else
9986#endif
9987 {
9988 ins_str((char_u *)" ");
9989 if (State & REPLACE_FLAG) /* no char replaced */
9990 replace_push(NUL);
9991 }
9992 }
9993
9994 /*
9995 * When 'expandtab' not set: Replace spaces by TABs where possible.
9996 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009997 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 {
9999 char_u *ptr;
10000#ifdef FEAT_VREPLACE
10001 char_u *saved_line = NULL; /* init for GCC */
10002 pos_T pos;
10003#endif
10004 pos_T fpos;
10005 pos_T *cursor;
10006 colnr_T want_vcol, vcol;
10007 int change_col = -1;
10008 int save_list = curwin->w_p_list;
10009
10010 /*
10011 * Get the current line. For VREPLACE mode, don't make real changes
10012 * yet, just work on a copy of the line.
10013 */
10014#ifdef FEAT_VREPLACE
10015 if (State & VREPLACE_FLAG)
10016 {
10017 pos = curwin->w_cursor;
10018 cursor = &pos;
10019 saved_line = vim_strsave(ml_get_curline());
10020 if (saved_line == NULL)
10021 return FALSE;
10022 ptr = saved_line + pos.col;
10023 }
10024 else
10025#endif
10026 {
10027 ptr = ml_get_cursor();
10028 cursor = &curwin->w_cursor;
10029 }
10030
10031 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10032 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10033 curwin->w_p_list = FALSE;
10034
10035 /* Find first white before the cursor */
10036 fpos = curwin->w_cursor;
10037 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
10038 {
10039 --fpos.col;
10040 --ptr;
10041 }
10042
10043 /* In Replace mode, don't change characters before the insert point. */
10044 if ((State & REPLACE_FLAG)
10045 && fpos.lnum == Insstart.lnum
10046 && fpos.col < Insstart.col)
10047 {
10048 ptr += Insstart.col - fpos.col;
10049 fpos.col = Insstart.col;
10050 }
10051
10052 /* compute virtual column numbers of first white and cursor */
10053 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10054 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10055
Bram Moolenaar597a4222014-06-25 14:39:50 +020010056 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10057 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 while (vim_iswhite(*ptr))
10059 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010060 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 if (vcol + i > want_vcol)
10062 break;
10063 if (*ptr != TAB)
10064 {
10065 *ptr = TAB;
10066 if (change_col < 0)
10067 {
10068 change_col = fpos.col; /* Column of first change */
10069 /* May have to adjust Insstart */
10070 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10071 Insstart.col = fpos.col;
10072 }
10073 }
10074 ++fpos.col;
10075 ++ptr;
10076 vcol += i;
10077 }
10078
10079 if (change_col >= 0)
10080 {
10081 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010082 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083
10084 /* Skip over the spaces we need. */
10085 while (vcol < want_vcol && *ptr == ' ')
10086 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010087 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 ++ptr;
10089 ++repl_off;
10090 }
10091 if (vcol > want_vcol)
10092 {
10093 /* Must have a char with 'showbreak' just before it. */
10094 --ptr;
10095 --repl_off;
10096 }
10097 fpos.col += repl_off;
10098
10099 /* Delete following spaces. */
10100 i = cursor->col - fpos.col;
10101 if (i > 0)
10102 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010103 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 /* correct replace stack. */
10105 if ((State & REPLACE_FLAG)
10106#ifdef FEAT_VREPLACE
10107 && !(State & VREPLACE_FLAG)
10108#endif
10109 )
10110 for (temp = i; --temp >= 0; )
10111 replace_join(repl_off);
10112 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010113#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010114 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010115 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010116 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010117 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10118 (char_u *)"\t", 1);
10119 }
10120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 cursor->col -= i;
10122
10123#ifdef FEAT_VREPLACE
10124 /*
10125 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10126 * backspacing over the changed spacing and then inserting the new
10127 * spacing.
10128 */
10129 if (State & VREPLACE_FLAG)
10130 {
10131 /* Backspace from real cursor to change_col */
10132 backspace_until_column(change_col);
10133
10134 /* Insert each char in saved_line from changed_col to
10135 * ptr-cursor */
10136 ins_bytes_len(saved_line + change_col,
10137 cursor->col - change_col);
10138 }
10139#endif
10140 }
10141
10142#ifdef FEAT_VREPLACE
10143 if (State & VREPLACE_FLAG)
10144 vim_free(saved_line);
10145#endif
10146 curwin->w_p_list = save_list;
10147 }
10148
10149 return FALSE;
10150}
10151
10152/*
10153 * Handle CR or NL in insert mode.
10154 * Return TRUE when out of memory or can't undo.
10155 */
10156 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010157ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158{
10159 int i;
10160
10161 if (echeck_abbr(c + ABBR_OFF))
10162 return FALSE;
10163 if (stop_arrow() == FAIL)
10164 return TRUE;
10165 undisplay_dollar();
10166
10167 /*
10168 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10169 * character under the cursor. Only push a NUL on the replace stack,
10170 * nothing to put back when the NL is deleted.
10171 */
10172 if ((State & REPLACE_FLAG)
10173#ifdef FEAT_VREPLACE
10174 && !(State & VREPLACE_FLAG)
10175#endif
10176 )
10177 replace_push(NUL);
10178
10179 /*
10180 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10181 * replacing the next line, so we push all of the characters left on the
10182 * line onto the replace stack. This is not done here though, it is done
10183 * in open_line().
10184 */
10185
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010186#ifdef FEAT_VIRTUALEDIT
10187 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10188 * CTRL-O). */
10189 if (virtual_active() && curwin->w_cursor.coladd > 0)
10190 coladvance(getviscol());
10191#endif
10192
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193#ifdef FEAT_RIGHTLEFT
10194# ifdef FEAT_FKMAP
10195 if (p_altkeymap && p_fkmap)
10196 fkmap(NL);
10197# endif
10198 /* NL in reverse insert will always start in the end of
10199 * current line. */
10200 if (revins_on)
10201 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10202#endif
10203
10204 AppendToRedobuff(NL_STR);
10205 i = open_line(FORWARD,
10206#ifdef FEAT_COMMENTS
10207 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10208#endif
10209 0, old_indent);
10210 old_indent = 0;
10211#ifdef FEAT_CINDENT
10212 can_cindent = TRUE;
10213#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010214#ifdef FEAT_FOLDING
10215 /* When inserting a line the cursor line must never be in a closed fold. */
10216 foldOpenCursor();
10217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218
10219 return (!i);
10220}
10221
10222#ifdef FEAT_DIGRAPHS
10223/*
10224 * Handle digraph in insert mode.
10225 * Returns character still to be inserted, or NUL when nothing remaining to be
10226 * done.
10227 */
10228 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010229ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230{
10231 int c;
10232 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010233 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234
10235 pc_status = PC_STATUS_UNSET;
10236 if (redrawing() && !char_avail())
10237 {
10238 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010239 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240
10241 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010242 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243#ifdef FEAT_CMDL_INFO
10244 add_to_showcmd_c(Ctrl_K);
10245#endif
10246 }
10247
10248#ifdef USE_ON_FLY_SCROLL
10249 dont_scroll = TRUE; /* disallow scrolling here */
10250#endif
10251
10252 /* don't map the digraph chars. This also prevents the
10253 * mode message to be deleted when ESC is hit */
10254 ++no_mapping;
10255 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010256 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 --no_mapping;
10258 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010259 if (did_putchar)
10260 /* when the line fits in 'columns' the '?' is at the start of the next
10261 * line and will not be removed by the redraw */
10262 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010263
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 if (IS_SPECIAL(c) || mod_mask) /* special key */
10265 {
10266#ifdef FEAT_CMDL_INFO
10267 clear_showcmd();
10268#endif
10269 insert_special(c, TRUE, FALSE);
10270 return NUL;
10271 }
10272 if (c != ESC)
10273 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010274 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 if (redrawing() && !char_avail())
10276 {
10277 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010278 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279
10280 if (char2cells(c) == 1)
10281 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010282 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010284 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 }
10286#ifdef FEAT_CMDL_INFO
10287 add_to_showcmd_c(c);
10288#endif
10289 }
10290 ++no_mapping;
10291 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010292 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 --no_mapping;
10294 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010295 if (did_putchar)
10296 /* when the line fits in 'columns' the '?' is at the start of the
10297 * next line and will not be removed by a redraw */
10298 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 if (cc != ESC)
10300 {
10301 AppendToRedobuff((char_u *)CTRL_V_STR);
10302 c = getdigraph(c, cc, TRUE);
10303#ifdef FEAT_CMDL_INFO
10304 clear_showcmd();
10305#endif
10306 return c;
10307 }
10308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309#ifdef FEAT_CMDL_INFO
10310 clear_showcmd();
10311#endif
10312 return NUL;
10313}
10314#endif
10315
10316/*
10317 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10318 * Returns the char to be inserted, or NUL if none found.
10319 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010321ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322{
10323 int c;
10324 int temp;
10325 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010326 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327
10328 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10329 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010330 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 return NUL;
10332 }
10333
10334 /* try to advance to the cursor column */
10335 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010336 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 prev_ptr = ptr;
10338 validate_virtcol();
10339 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10340 {
10341 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010342 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 }
10344 if ((colnr_T)temp > curwin->w_virtcol)
10345 ptr = prev_ptr;
10346
10347#ifdef FEAT_MBYTE
10348 c = (*mb_ptr2char)(ptr);
10349#else
10350 c = *ptr;
10351#endif
10352 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010353 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 return c;
10355}
10356
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010357/*
10358 * CTRL-Y or CTRL-E typed in Insert mode.
10359 */
10360 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010361ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010362{
10363 int c = tc;
10364
10365#ifdef FEAT_INS_EXPAND
10366 if (ctrl_x_mode == CTRL_X_SCROLL)
10367 {
10368 if (c == Ctrl_Y)
10369 scrolldown_clamp();
10370 else
10371 scrollup_clamp();
10372 redraw_later(VALID);
10373 }
10374 else
10375#endif
10376 {
10377 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10378 if (c != NUL)
10379 {
10380 long tw_save;
10381
10382 /* The character must be taken literally, insert like it
10383 * was typed after a CTRL-V, and pretend 'textwidth'
10384 * wasn't set. Digits, 'o' and 'x' are special after a
10385 * CTRL-V, don't use it for these. */
10386 if (c < 256 && !isalnum(c))
10387 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10388 tw_save = curbuf->b_p_tw;
10389 curbuf->b_p_tw = -1;
10390 insert_special(c, TRUE, FALSE);
10391 curbuf->b_p_tw = tw_save;
10392#ifdef FEAT_RIGHTLEFT
10393 revins_chars++;
10394 revins_legal++;
10395#endif
10396 c = Ctrl_V; /* pretend CTRL-V is last character */
10397 auto_format(FALSE, TRUE);
10398 }
10399 }
10400 return c;
10401}
10402
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403#ifdef FEAT_SMARTINDENT
10404/*
10405 * Try to do some very smart auto-indenting.
10406 * Used when inserting a "normal" character.
10407 */
10408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010409ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410{
10411 pos_T *pos, old_pos;
10412 char_u *ptr;
10413 int i;
10414 int temp;
10415
10416 /*
10417 * do some very smart indenting when entering '{' or '}'
10418 */
10419 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10420 {
10421 /*
10422 * for '}' set indent equal to indent of line containing matching '{'
10423 */
10424 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10425 {
10426 old_pos = curwin->w_cursor;
10427 /*
10428 * If the matching '{' has a ')' immediately before it (ignoring
10429 * white-space), then line up with the start of the line
10430 * containing the matching '(' if there is one. This handles the
10431 * case where an "if (..\n..) {" statement continues over multiple
10432 * lines -- webb
10433 */
10434 ptr = ml_get(pos->lnum);
10435 i = pos->col;
10436 if (i > 0) /* skip blanks before '{' */
10437 while (--i > 0 && vim_iswhite(ptr[i]))
10438 ;
10439 curwin->w_cursor.lnum = pos->lnum;
10440 curwin->w_cursor.col = i;
10441 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10442 curwin->w_cursor = *pos;
10443 i = get_indent();
10444 curwin->w_cursor = old_pos;
10445#ifdef FEAT_VREPLACE
10446 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010447 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 else
10449#endif
10450 (void)set_indent(i, SIN_CHANGED);
10451 }
10452 else if (curwin->w_cursor.col > 0)
10453 {
10454 /*
10455 * when inserting '{' after "O" reduce indent, but not
10456 * more than indent of previous line
10457 */
10458 temp = TRUE;
10459 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10460 {
10461 old_pos = curwin->w_cursor;
10462 i = get_indent();
10463 while (curwin->w_cursor.lnum > 1)
10464 {
10465 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10466
10467 /* ignore empty lines and lines starting with '#'. */
10468 if (*ptr != '#' && *ptr != NUL)
10469 break;
10470 }
10471 if (get_indent() >= i)
10472 temp = FALSE;
10473 curwin->w_cursor = old_pos;
10474 }
10475 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010476 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 }
10478 }
10479
10480 /*
10481 * set indent of '#' always to 0
10482 */
10483 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10484 {
10485 /* remember current indent for next line */
10486 old_indent = get_indent();
10487 (void)set_indent(0, SIN_CHANGED);
10488 }
10489
10490 /* Adjust ai_col, the char at this position can be deleted. */
10491 if (ai_col > curwin->w_cursor.col)
10492 ai_col = curwin->w_cursor.col;
10493}
10494#endif
10495
10496/*
10497 * Get the value that w_virtcol would have when 'list' is off.
10498 * Unless 'cpo' contains the 'L' flag.
10499 */
10500 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010501get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502{
10503 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10504 return getvcol_nolist(&curwin->w_cursor);
10505 validate_virtcol();
10506 return curwin->w_virtcol;
10507}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010508
10509#ifdef FEAT_AUTOCMD
10510/*
10511 * Handle the InsertCharPre autocommand.
10512 * "c" is the character that was typed.
10513 * Return a pointer to allocated memory with the replacement string.
10514 * Return NULL to continue inserting "c".
10515 */
10516 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010517do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010518{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010519 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010520 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010521
10522 /* Return quickly when there is nothing to do. */
10523 if (!has_insertcharpre())
10524 return NULL;
10525
Bram Moolenaar704984a2012-06-01 14:57:51 +020010526#ifdef FEAT_MBYTE
10527 if (has_mbyte)
10528 buf[(*mb_char2bytes)(c, buf)] = NUL;
10529 else
10530#endif
10531 {
10532 buf[0] = c;
10533 buf[1] = NUL;
10534 }
10535
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010536 /* Lock the text to avoid weird things from happening. */
10537 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010538 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010539
Bram Moolenaar704984a2012-06-01 14:57:51 +020010540 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010541 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010542 {
10543 /* Get the value of v:char. It may be empty or more than one
10544 * character. Only use it when changed, otherwise continue with the
10545 * original character to avoid breaking autoindent. */
10546 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10547 res = vim_strsave(get_vim_var_str(VV_CHAR));
10548 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010549
10550 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10551 --textlock;
10552
10553 return res;
10554}
10555#endif