blob: af1d00b85d866d9f509d557bb382066179727049 [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();
2824
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002825 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002826 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002827 startcol = curwin->w_cursor.col;
2828 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002829 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002830 /* compl_pattern doesn't need to be set */
2831 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2832 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002833 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002834 return;
2835
Bram Moolenaare4214502015-03-05 18:08:43 +01002836 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002837
2838 ins_compl_add_list(list);
2839 compl_matches = ins_compl_make_cyclic();
2840 compl_started = TRUE;
2841 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002842 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002843
2844 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002845 if (compl_no_insert || compl_no_select)
2846 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002847 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002848 if (compl_no_select)
2849 /* Down/Up has no real effect. */
2850 ins_complete(K_UP, FALSE);
2851 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002852 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002853 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002854 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002855
2856 /* Lazily show the popup menu, unless we got interrupted. */
2857 if (!compl_interrupted)
2858 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002859 out_flush();
2860}
2861
2862
Bram Moolenaar9372a112005-12-06 19:59:18 +00002863/* "compl_match_array" points the currently displayed list of entries in the
2864 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002865static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002866static int compl_match_arraysize;
2867
2868/*
2869 * Update the screen and when there is any scrolling remove the popup menu.
2870 */
2871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002872ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002873{
2874 int h;
2875
2876 if (compl_match_array != NULL)
2877 {
2878 h = curwin->w_cline_height;
2879 update_screen(0);
2880 if (h != curwin->w_cline_height)
2881 ins_compl_del_pum();
2882 }
2883}
2884
2885/*
2886 * Remove any popup menu.
2887 */
2888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002889ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002890{
2891 if (compl_match_array != NULL)
2892 {
2893 pum_undisplay();
2894 vim_free(compl_match_array);
2895 compl_match_array = NULL;
2896 }
2897}
2898
2899/*
2900 * Return TRUE if the popup menu should be displayed.
2901 */
2902 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002903pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002904{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002905 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002906 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002907 return FALSE;
2908
2909 /* The display looks bad on a B&W display. */
2910 if (t_colors < 8
2911#ifdef FEAT_GUI
2912 && !gui.in_use
2913#endif
2914 )
2915 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002916 return TRUE;
2917}
2918
2919/*
2920 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002921 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002922 */
2923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002924pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002925{
2926 compl_T *compl;
2927 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002928
2929 /* Don't display the popup menu if there are no matches or there is only
2930 * one (ignoring the original text). */
2931 compl = compl_first_match;
2932 i = 0;
2933 do
2934 {
2935 if (compl == NULL
2936 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2937 break;
2938 compl = compl->cp_next;
2939 } while (compl != compl_first_match);
2940
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002941 if (strstr((char *)p_cot, "menuone") != NULL)
2942 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002943 return (i >= 2);
2944}
2945
2946/*
2947 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002948 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002949 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002951ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002952{
2953 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002954 compl_T *shown_compl = NULL;
2955 int did_find_shown_match = FALSE;
2956 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002957 int i;
2958 int cur = -1;
2959 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002960 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002961
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002962 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002963 return;
2964
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002965#if defined(FEAT_EVAL)
2966 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2967 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2968#endif
2969
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002970 /* Update the screen before drawing the popup menu over it. */
2971 update_screen(0);
2972
2973 if (compl_match_array == NULL)
2974 {
2975 /* Need to build the popup menu list. */
2976 compl_match_arraysize = 0;
2977 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002978 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002979 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002980 do
2981 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002982 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2983 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002984 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002985 ++compl_match_arraysize;
2986 compl = compl->cp_next;
2987 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002988 if (compl_match_arraysize == 0)
2989 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002990 compl_match_array = (pumitem_T *)alloc_clear(
2991 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002992 * compl_match_arraysize));
2993 if (compl_match_array != NULL)
2994 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002995 /* If the current match is the original text don't find the first
2996 * match after it, don't highlight anything. */
2997 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2998 shown_match_ok = TRUE;
2999
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003000 i = 0;
3001 compl = compl_first_match;
3002 do
3003 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003004 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3005 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003006 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003007 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003008 if (!shown_match_ok)
3009 {
3010 if (compl == compl_shown_match || did_find_shown_match)
3011 {
3012 /* This item is the shown match or this is the
3013 * first displayed item after the shown match. */
3014 compl_shown_match = compl;
3015 did_find_shown_match = TRUE;
3016 shown_match_ok = TRUE;
3017 }
3018 else
3019 /* Remember this displayed match for when the
3020 * shown match is just below it. */
3021 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003022 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003023 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003024
3025 if (compl->cp_text[CPT_ABBR] != NULL)
3026 compl_match_array[i].pum_text =
3027 compl->cp_text[CPT_ABBR];
3028 else
3029 compl_match_array[i].pum_text = compl->cp_str;
3030 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3031 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3032 if (compl->cp_text[CPT_MENU] != NULL)
3033 compl_match_array[i++].pum_extra =
3034 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003035 else
3036 compl_match_array[i++].pum_extra = compl->cp_fname;
3037 }
3038
3039 if (compl == compl_shown_match)
3040 {
3041 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003042
3043 /* When the original text is the shown match don't set
3044 * compl_shown_match. */
3045 if (compl->cp_flags & ORIGINAL_TEXT)
3046 shown_match_ok = TRUE;
3047
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003048 if (!shown_match_ok && shown_compl != NULL)
3049 {
3050 /* The shown match isn't displayed, set it to the
3051 * previously displayed match. */
3052 compl_shown_match = shown_compl;
3053 shown_match_ok = TRUE;
3054 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003055 }
3056 compl = compl->cp_next;
3057 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003058
3059 if (!shown_match_ok) /* no displayed match at all */
3060 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003061 }
3062 }
3063 else
3064 {
3065 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003066 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003067 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3068 || compl_match_array[i].pum_text
3069 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003070 {
3071 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003072 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003073 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003074 }
3075
3076 if (compl_match_array != NULL)
3077 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003078 /* In Replace mode when a $ is displayed at the end of the line only
3079 * part of the screen would be updated. We do need to redraw here. */
3080 dollar_vcol = -1;
3081
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003082 /* Compute the screen column of the start of the completed text.
3083 * Use the cursor to get all wrapping and other settings right. */
3084 col = curwin->w_cursor.col;
3085 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003086 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003087 curwin->w_cursor.col = col;
3088 }
3089}
3090
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091#define DICT_FIRST (1) /* use just first element in "dict" */
3092#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003093
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003095 * Add any identifiers that match the given pattern in the list of dictionary
3096 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 */
3098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003099ins_compl_dictionaries(
3100 char_u *dict_start,
3101 char_u *pat,
3102 int flags, /* DICT_FIRST and/or DICT_EXACT */
3103 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003105 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 char_u *ptr;
3107 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 char_u **files;
3110 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003112 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
Bram Moolenaar0b238792006-03-02 22:49:12 +00003114 if (*dict == NUL)
3115 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003116#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003117 /* When 'dictionary' is empty and spell checking is enabled use
3118 * "spell". */
3119 if (!thesaurus && curwin->w_p_spell)
3120 dict = (char_u *)"spell";
3121 else
3122#endif
3123 return;
3124 }
3125
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003127 if (buf == NULL)
3128 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003129 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003130
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 /* If 'infercase' is set, don't use 'smartcase' here */
3132 save_p_scs = p_scs;
3133 if (curbuf->b_p_inf)
3134 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003135
3136 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3137 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003138 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003139 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003140 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003141 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003142 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003143
3144 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003145 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003146 len = STRLEN(pat_esc) + 10;
3147 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003148 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003149 {
3150 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003151 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003152 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003153 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003154 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3155 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003156 vim_free(ptr);
3157 }
3158 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003159 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003160 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003161 if (regmatch.regprog == NULL)
3162 goto theend;
3163 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003164
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3166 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003167 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 {
3169 /* copy one dictionary file name into buf */
3170 if (flags == DICT_EXACT)
3171 {
3172 count = 1;
3173 files = &dict;
3174 }
3175 else
3176 {
3177 /* Expand wildcards in the dictionary name, but do not allow
3178 * backticks (for security, the 'dict' option may have been set in
3179 * a modeline). */
3180 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003181# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003182 if (!thesaurus && STRCMP(buf, "spell") == 0)
3183 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003184 else
3185# endif
3186 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 || expand_wildcards(1, &buf, &count, &files,
3188 EW_FILE|EW_SILENT) != OK)
3189 count = 0;
3190 }
3191
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003192# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003193 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003195 /* Complete from active spelling. Skip "\<" in the pattern, we
3196 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003197 if (pat[0] == '\\' && pat[1] == '<')
3198 ptr = pat + 2;
3199 else
3200 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003201 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003203 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003204# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003205 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003206 {
3207 ins_compl_files(count, files, thesaurus, flags,
3208 &regmatch, buf, &dir);
3209 if (flags != DICT_EXACT)
3210 FreeWild(count, files);
3211 }
3212 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 break;
3214 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003215
3216theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003218 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 vim_free(buf);
3220}
3221
Bram Moolenaar0b238792006-03-02 22:49:12 +00003222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003223ins_compl_files(
3224 int count,
3225 char_u **files,
3226 int thesaurus,
3227 int flags,
3228 regmatch_T *regmatch,
3229 char_u *buf,
3230 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003231{
3232 char_u *ptr;
3233 int i;
3234 FILE *fp;
3235 int add_r;
3236
3237 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3238 {
3239 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3240 if (flags != DICT_EXACT)
3241 {
3242 vim_snprintf((char *)IObuff, IOSIZE,
3243 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003244 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003245 }
3246
3247 if (fp != NULL)
3248 {
3249 /*
3250 * Read dictionary file line by line.
3251 * Check each line for a match.
3252 */
3253 while (!got_int && !compl_interrupted
3254 && !vim_fgets(buf, LSIZE, fp))
3255 {
3256 ptr = buf;
3257 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3258 {
3259 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003260 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003261 ptr = find_line_end(ptr);
3262 else
3263 ptr = find_word_end(ptr);
3264 add_r = ins_compl_add_infercase(regmatch->startp[0],
3265 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003266 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003267 if (thesaurus)
3268 {
3269 char_u *wstart;
3270
3271 /*
3272 * Add the other matches on the line
3273 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003274 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003275 while (!got_int)
3276 {
3277 /* Find start of the next word. Skip white
3278 * space and punctuation. */
3279 ptr = find_word_start(ptr);
3280 if (*ptr == NUL || *ptr == NL)
3281 break;
3282 wstart = ptr;
3283
Bram Moolenaara2993e12007-08-12 14:38:46 +00003284 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003285#ifdef FEAT_MBYTE
3286 if (has_mbyte)
3287 /* Japanese words may have characters in
3288 * different classes, only separate words
3289 * with single-byte non-word characters. */
3290 while (*ptr != NUL)
3291 {
3292 int l = (*mb_ptr2len)(ptr);
3293
3294 if (l < 2 && !vim_iswordc(*ptr))
3295 break;
3296 ptr += l;
3297 }
3298 else
3299#endif
3300 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003301
3302 /* Add the word. Skip the regexp match. */
3303 if (wstart != regmatch->startp[0])
3304 add_r = ins_compl_add_infercase(wstart,
3305 (int)(ptr - wstart),
3306 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003307 }
3308 }
3309 if (add_r == OK)
3310 /* if dir was BACKWARD then honor it just once */
3311 *dir = FORWARD;
3312 else if (add_r == FAIL)
3313 break;
3314 /* avoid expensive call to vim_regexec() when at end
3315 * of line */
3316 if (*ptr == '\n' || got_int)
3317 break;
3318 }
3319 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003320 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003321 }
3322 fclose(fp);
3323 }
3324 }
3325}
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327/*
3328 * Find the start of the next word.
3329 * Returns a pointer to the first char of the word. Also stops at a NUL.
3330 */
3331 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003332find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334#ifdef FEAT_MBYTE
3335 if (has_mbyte)
3336 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003337 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 else
3339#endif
3340 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3341 ++ptr;
3342 return ptr;
3343}
3344
3345/*
3346 * Find the end of the word. Assumes it starts inside a word.
3347 * Returns a pointer to just after the word.
3348 */
3349 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003350find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
3352#ifdef FEAT_MBYTE
3353 int start_class;
3354
3355 if (has_mbyte)
3356 {
3357 start_class = mb_get_class(ptr);
3358 if (start_class > 1)
3359 while (*ptr != NUL)
3360 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003361 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (mb_get_class(ptr) != start_class)
3363 break;
3364 }
3365 }
3366 else
3367#endif
3368 while (vim_iswordc(*ptr))
3369 ++ptr;
3370 return ptr;
3371}
3372
3373/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003374 * Find the end of the line, omitting CR and NL at the end.
3375 * Returns a pointer to just after the line.
3376 */
3377 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003378find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003379{
3380 char_u *s;
3381
3382 s = ptr + STRLEN(ptr);
3383 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3384 --s;
3385 return s;
3386}
3387
3388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 * Free the list of completions
3390 */
3391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003392ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003394 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003395 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003397 vim_free(compl_pattern);
3398 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003399 vim_free(compl_leader);
3400 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003402 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003404
3405 ins_compl_del_pum();
3406 pum_clear();
3407
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003408 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 do
3410 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003411 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003412 compl_curr_match = compl_curr_match->cp_next;
3413 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003415 if (match->cp_flags & FREE_FNAME)
3416 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003417 for (i = 0; i < CPT_COUNT; ++i)
3418 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003420 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3421 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003422 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423}
3424
3425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003426ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003428 compl_cont_status = 0;
3429 compl_started = FALSE;
3430 compl_matches = 0;
3431 vim_free(compl_pattern);
3432 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003433 vim_free(compl_leader);
3434 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003436 vim_free(compl_orig_text);
3437 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003438 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003439 /* clear v:completed_item */
3440 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441}
3442
3443/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003444 * Return TRUE when Insert completion is active.
3445 */
3446 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003447ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003448{
3449 return compl_started;
3450}
3451
3452/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003453 * Delete one character before the cursor and show the subset of the matches
3454 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003455 * Returns the character to be used, NUL if the work is done and another char
3456 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003457 */
3458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003459ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003460{
3461 char_u *line;
3462 char_u *p;
3463
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003464 line = ml_get_curline();
3465 p = line + curwin->w_cursor.col;
3466 mb_ptr_back(line, p);
3467
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003468 /* Stop completion when the whole word was deleted. For Omni completion
3469 * allow the word to be deleted, we won't match everything. */
3470 if ((int)(p - line) - (int)compl_col < 0
3471 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003472 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003473 return K_BS;
3474
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003475 /* Deleted more than what was used to find matches or didn't finish
3476 * finding all matches: need to look for matches all over again. */
3477 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003478 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003479 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003480
Bram Moolenaara6557602006-02-04 22:43:20 +00003481 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003482 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003483 if (compl_leader != NULL)
3484 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003485 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003486 if (compl_shown_match != NULL)
3487 /* Make sure current match is not a hidden item. */
3488 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003489 return NUL;
3490 }
3491 return K_BS;
3492}
Bram Moolenaara6557602006-02-04 22:43:20 +00003493
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003494/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003495 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3496 * be called.
3497 */
3498 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003499ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003500{
3501 /* Return TRUE if we didn't complete finding matches or when the
3502 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3503 return compl_was_interrupted
3504 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3505 && compl_opt_refresh_always);
3506}
3507
3508/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003509 * Called after changing "compl_leader".
3510 * Show the popup menu with a different set of matches.
3511 * May also search for matches again if the previous search was interrupted.
3512 */
3513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003514ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003515{
3516 ins_compl_del_pum();
3517 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003518 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003519 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003520
3521 if (compl_started)
3522 ins_compl_set_original_text(compl_leader);
3523 else
3524 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003525#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003526 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003527#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003528 /*
3529 * Matches were cleared, need to search for them now. First display
3530 * the changed text before the cursor. Set "compl_restarting" to
3531 * avoid that the first match is inserted.
3532 */
3533 update_screen(0);
3534#ifdef FEAT_GUI
3535 if (gui.in_use)
3536 {
3537 /* Show the cursor after the match, not after the redrawn text. */
3538 setcursor();
3539 out_flush();
3540 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003541 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003542#endif
3543 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003544 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003545 compl_cont_status = 0;
3546 compl_restarting = FALSE;
3547 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003548
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003549 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003550
3551 /* Show the popup menu with a different set of matches. */
3552 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003553
3554 /* Don't let Enter select the original text when there is no popup menu. */
3555 if (compl_match_array == NULL)
3556 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003557}
3558
3559/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003560 * Return the length of the completion, from the completion start column to
3561 * the cursor column. Making sure it never goes below zero.
3562 */
3563 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003564ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003565{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003566 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003567
3568 if (off < 0)
3569 return 0;
3570 return off;
3571}
3572
3573/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003574 * Append one character to the match leader. May reduce the number of
3575 * matches.
3576 */
3577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003578ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003579{
3580#ifdef FEAT_MBYTE
3581 int cc;
3582
3583 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3584 {
3585 char_u buf[MB_MAXBYTES + 1];
3586
3587 (*mb_char2bytes)(c, buf);
3588 buf[cc] = NUL;
3589 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003590 if (compl_opt_refresh_always)
3591 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003592 }
3593 else
3594#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003595 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003596 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003597 if (compl_opt_refresh_always)
3598 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003599 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003600
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003601 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003602 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003603 ins_compl_restart();
3604
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003605 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003606 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003607 * break redo. */
3608 if (!compl_opt_refresh_always)
3609 {
3610 vim_free(compl_leader);
3611 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003612 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003613 if (compl_leader != NULL)
3614 ins_compl_new_leader();
3615 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003616}
3617
3618/*
3619 * Setup for finding completions again without leaving CTRL-X mode. Used when
3620 * BS or a key was typed while still searching for matches.
3621 */
3622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003623ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003624{
3625 ins_compl_free();
3626 compl_started = FALSE;
3627 compl_matches = 0;
3628 compl_cont_status = 0;
3629 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003630}
3631
3632/*
3633 * Set the first match, the original text.
3634 */
3635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003636ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003637{
3638 char_u *p;
3639
3640 /* Replace the original text entry. */
3641 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3642 {
3643 p = vim_strsave(str);
3644 if (p != NULL)
3645 {
3646 vim_free(compl_first_match->cp_str);
3647 compl_first_match->cp_str = p;
3648 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003649 }
3650}
3651
3652/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003653 * Append one character to the match leader. May reduce the number of
3654 * matches.
3655 */
3656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003657ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003658{
3659 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003660 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003661 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003662 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003663
3664 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003665 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003666 {
3667 /* When still at the original match use the first entry that matches
3668 * the leader. */
3669 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3670 {
3671 p = NULL;
3672 for (cp = compl_shown_match->cp_next; cp != NULL
3673 && cp != compl_first_match; cp = cp->cp_next)
3674 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003675 if (compl_leader == NULL
3676 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003677 (int)STRLEN(compl_leader)))
3678 {
3679 p = cp->cp_str;
3680 break;
3681 }
3682 }
3683 if (p == NULL || (int)STRLEN(p) <= len)
3684 return;
3685 }
3686 else
3687 return;
3688 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003689 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003690 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003691 ins_compl_addleader(c);
3692}
3693
3694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003696 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003697 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003699 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003700ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701{
3702 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003704 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
3706 /* Forget any previous 'special' messages if this is actually
3707 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3708 */
3709 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3710 edit_submode_extra = NULL;
3711
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003712 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003713 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3714 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003715 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003717 /* Set "compl_get_longest" when finding the first matches. */
3718 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3719 || (ctrl_x_mode == 0 && !compl_started))
3720 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003721 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003722 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003723
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003724 }
3725
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3727 {
3728 /*
3729 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3730 * it will be yet. Now we decide.
3731 */
3732 switch (c)
3733 {
3734 case Ctrl_E:
3735 case Ctrl_Y:
3736 ctrl_x_mode = CTRL_X_SCROLL;
3737 if (!(State & REPLACE_FLAG))
3738 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3739 else
3740 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3741 edit_submode_pre = NULL;
3742 showmode();
3743 break;
3744 case Ctrl_L:
3745 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3746 break;
3747 case Ctrl_F:
3748 ctrl_x_mode = CTRL_X_FILES;
3749 break;
3750 case Ctrl_K:
3751 ctrl_x_mode = CTRL_X_DICTIONARY;
3752 break;
3753 case Ctrl_R:
3754 /* Simply allow ^R to happen without affecting ^X mode */
3755 break;
3756 case Ctrl_T:
3757 ctrl_x_mode = CTRL_X_THESAURUS;
3758 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003759#ifdef FEAT_COMPL_FUNC
3760 case Ctrl_U:
3761 ctrl_x_mode = CTRL_X_FUNCTION;
3762 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003763 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003764 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003765 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003766#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003767 case 's':
3768 case Ctrl_S:
3769 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003770#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003771 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003772 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003773 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003774#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003775 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 case Ctrl_RSB:
3777 ctrl_x_mode = CTRL_X_TAGS;
3778 break;
3779#ifdef FEAT_FIND_ID
3780 case Ctrl_I:
3781 case K_S_TAB:
3782 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3783 break;
3784 case Ctrl_D:
3785 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3786 break;
3787#endif
3788 case Ctrl_V:
3789 case Ctrl_Q:
3790 ctrl_x_mode = CTRL_X_CMDLINE;
3791 break;
3792 case Ctrl_P:
3793 case Ctrl_N:
3794 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3795 * just started ^X mode, or there were enough ^X's to cancel
3796 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3797 * do normal expansion when interrupting a different mode (say
3798 * ^X^F^X^P or ^P^X^X^P, see below)
3799 * nothing changes if interrupting mode 0, (eg, the flag
3800 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003801 if (!(compl_cont_status & CONT_INTRPT))
3802 compl_cont_status |= CONT_LOCAL;
3803 else if (compl_cont_mode != 0)
3804 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 /* FALLTHROUGH */
3806 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807 /* If we have typed at least 2 ^X's... for modes != 0, we set
3808 * compl_cont_status = 0 (eg, as if we had just started ^X
3809 * mode).
3810 * For mode 0, we set "compl_cont_mode" to an impossible
3811 * value, in both cases ^X^X can be used to restart the same
3812 * mode (avoiding ADDING mode).
3813 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3814 * 'complete' and local ^P expansions respectively.
3815 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3816 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (c == Ctrl_X)
3818 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003819 if (compl_cont_mode != 0)
3820 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003822 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
3824 ctrl_x_mode = 0;
3825 edit_submode = NULL;
3826 showmode();
3827 break;
3828 }
3829 }
3830 else if (ctrl_x_mode != 0)
3831 {
3832 /* We're already in CTRL-X mode, do we stay in it? */
3833 if (!vim_is_ctrl_x_key(c))
3834 {
3835 if (ctrl_x_mode == CTRL_X_SCROLL)
3836 ctrl_x_mode = 0;
3837 else
3838 ctrl_x_mode = CTRL_X_FINISHED;
3839 edit_submode = NULL;
3840 }
3841 showmode();
3842 }
3843
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003844 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
3846 /* Show error message from attempted keyword completion (probably
3847 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003848 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003850 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3851 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 || ctrl_x_mode == CTRL_X_FINISHED)
3853 {
3854 /* Get here when we have finished typing a sequence of ^N and
3855 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003856 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003857 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
3859 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003860 * If any of the original typed text has been changed, eg when
3861 * ignorecase is set, we must add back-spaces to the redo
3862 * buffer. We add as few as necessary to delete just the part
3863 * of the original text that has changed.
3864 * When using the longest match, edited the match or used
3865 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003867 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3868 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003869 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003870 ptr = NULL;
3871 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873
3874#ifdef FEAT_CINDENT
3875 want_cindent = (can_cindent && cindent_on());
3876#endif
3877 /*
3878 * When completing whole lines: fix indent for 'cindent'.
3879 * Otherwise, break line if it's too long.
3880 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003881 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
3883#ifdef FEAT_CINDENT
3884 /* re-indent the current line */
3885 if (want_cindent)
3886 {
3887 do_c_expr_indent();
3888 want_cindent = FALSE; /* don't do it again */
3889 }
3890#endif
3891 }
3892 else
3893 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003894 int prev_col = curwin->w_cursor.col;
3895
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003897 if (prev_col > 0)
3898 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003899 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003900 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003902 if (prev_col > 0
3903 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3904 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 }
3906
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003907 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003908 * the selection without inserting anything. When
3909 * compl_enter_selects is set the Enter key does the same. */
3910 if ((c == Ctrl_Y || (compl_enter_selects
3911 && (c == CAR || c == K_KENTER || c == NL)))
3912 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003913 retval = TRUE;
3914
Bram Moolenaar47247282016-08-02 22:36:02 +02003915 /* CTRL-E means completion is Ended, go back to the typed text.
3916 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003917 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003918 {
3919 ins_compl_delete();
3920 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003921 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003922 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003923 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003924 retval = TRUE;
3925 }
3926
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003927 auto_format(FALSE, TRUE);
3928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003930 compl_started = FALSE;
3931 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003932 if (!shortmess(SHM_COMPLETIONMENU))
3933 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003935 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 if (edit_submode != NULL)
3937 {
3938 edit_submode = NULL;
3939 showmode();
3940 }
3941
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003942#ifdef FEAT_CMDWIN
3943 if (c == Ctrl_C && cmdwin_type != 0)
3944 /* Avoid the popup menu remains displayed when leaving the
3945 * command line window. */
3946 update_screen(0);
3947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948#ifdef FEAT_CINDENT
3949 /*
3950 * Indent now if a key was typed that is in 'cinkeys'.
3951 */
3952 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3953 do_c_expr_indent();
3954#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003955#ifdef FEAT_AUTOCMD
3956 /* Trigger the CompleteDone event to give scripts a chance to act
3957 * upon the completion. */
3958 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003962#ifdef FEAT_AUTOCMD
3963 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3964 /* Trigger the CompleteDone event to give scripts a chance to act
3965 * upon the (possibly failed) completion. */
3966 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
3969 /* reset continue_* if we left expansion-mode, if we stay they'll be
3970 * (re)set properly in ins_complete() */
3971 if (!vim_is_ctrl_x_key(c))
3972 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003973 compl_cont_status = 0;
3974 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003976
3977 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978}
3979
3980/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003981 * Fix the redo buffer for the completion leader replacing some of the typed
3982 * text. This inserts backspaces and appends the changed text.
3983 * "ptr" is the known leader text or NUL.
3984 */
3985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003986ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003987{
3988 int len;
3989 char_u *p;
3990 char_u *ptr = ptr_arg;
3991
3992 if (ptr == NULL)
3993 {
3994 if (compl_leader != NULL)
3995 ptr = compl_leader;
3996 else
3997 return; /* nothing to do */
3998 }
3999 if (compl_orig_text != NULL)
4000 {
4001 p = compl_orig_text;
4002 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4003 ;
4004#ifdef FEAT_MBYTE
4005 if (len > 0)
4006 len -= (*mb_head_off)(p, p + len);
4007#endif
4008 for (p += len; *p != NUL; mb_ptr_adv(p))
4009 AppendCharToRedobuff(K_BS);
4010 }
4011 else
4012 len = 0;
4013 if (ptr != NULL)
4014 AppendToRedobuffLit(ptr + len, -1);
4015}
4016
4017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4019 * (depending on flag) starting from buf and looking for a non-scanned
4020 * buffer (other than curbuf). curbuf is special, if it is called with
4021 * buf=curbuf then it has to be the first call for a given flag/expansion.
4022 *
4023 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4024 */
4025 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004026ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
4028#ifdef FEAT_WINDOWS
4029 static win_T *wp;
4030#endif
4031
4032 if (flag == 'w') /* just windows */
4033 {
4034#ifdef FEAT_WINDOWS
4035 if (buf == curbuf) /* first call for this flag/expansion */
4036 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004037 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 && wp->w_buffer->b_scanned)
4039 ;
4040 buf = wp->w_buffer;
4041#else
4042 buf = curbuf;
4043#endif
4044 }
4045 else
4046 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4047 * (unlisted buffers)
4048 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004049 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 && ((flag == 'U'
4051 ? buf->b_p_bl
4052 : (!buf->b_p_bl
4053 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004054 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 ;
4056 return buf;
4057}
4058
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004059#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004060/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004061 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004062 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004063 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004065expand_by_function(
4066 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4067 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004068{
Bram Moolenaar82139082011-09-14 16:52:09 +02004069 list_T *matchlist = NULL;
4070 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004071 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004072 char_u *funcname;
4073 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004074 win_T *curwin_save;
4075 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004076 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004077
Bram Moolenaare344bea2005-09-01 20:46:49 +00004078 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4079 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004080 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004081
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004082 /* Call 'completefunc' to obtain the list of matches. */
4083 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004084 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004085
Bram Moolenaare344bea2005-09-01 20:46:49 +00004086 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004087 curwin_save = curwin;
4088 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004089
4090 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004091 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004092 {
4093 switch (rettv.v_type)
4094 {
4095 case VAR_LIST:
4096 matchlist = rettv.vval.v_list;
4097 break;
4098 case VAR_DICT:
4099 matchdict = rettv.vval.v_dict;
4100 break;
4101 default:
4102 /* TODO: Give error message? */
4103 clear_tv(&rettv);
4104 break;
4105 }
4106 }
4107
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004108 if (curwin_save != curwin || curbuf_save != curbuf)
4109 {
4110 EMSG(_(e_complwin));
4111 goto theend;
4112 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004113 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004114 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004115 if (!equalpos(curwin->w_cursor, pos))
4116 {
4117 EMSG(_(e_compldel));
4118 goto theend;
4119 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004120
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004121 if (matchlist != NULL)
4122 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004123 else if (matchdict != NULL)
4124 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004125
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004126theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004127 if (matchdict != NULL)
4128 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004129 if (matchlist != NULL)
4130 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004131}
4132#endif /* FEAT_COMPL_FUNC */
4133
Bram Moolenaar39f05632006-03-19 22:15:26 +00004134#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004135/*
4136 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004137 */
4138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004139ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004140{
4141 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004142 int dir = compl_direction;
4143
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004144 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004145 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004146 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004147 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4148 /* if dir was BACKWARD then honor it just once */
4149 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004150 else if (did_emsg)
4151 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004152 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004153}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004154
4155/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004156 * Add completions from a dict.
4157 */
4158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004159ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004160{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004161 dictitem_T *di_refresh;
4162 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004163
4164 /* Check for optional "refresh" item. */
4165 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004166 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4167 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004168 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004169 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004170
4171 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4172 compl_opt_refresh_always = TRUE;
4173 }
4174
4175 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004176 di_words = dict_find(dict, (char_u *)"words", 5);
4177 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4178 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004179}
4180
4181/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004182 * Add a match to the list of matches from a typeval_T.
4183 * If the given string is already in the list of completions, then return
4184 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4185 * maybe because alloc() returns NULL, then FAIL is returned.
4186 */
4187 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004188ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004189{
4190 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004191 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004192 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004193 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004194 char_u *(cptext[CPT_COUNT]);
4195
4196 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4197 {
4198 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4199 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4200 (char_u *)"abbr", FALSE);
4201 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4202 (char_u *)"menu", FALSE);
4203 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4204 (char_u *)"kind", FALSE);
4205 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4206 (char_u *)"info", FALSE);
4207 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4208 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004209 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004210 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004211 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4212 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004213 }
4214 else
4215 {
4216 word = get_tv_string_chk(tv);
4217 vim_memset(cptext, 0, sizeof(cptext));
4218 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004219 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004220 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004221 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004222}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004223#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004225/*
4226 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004227 * The search starts at position "ini" in curbuf and in the direction
4228 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004229 * When "compl_started" is FALSE start at that position, otherwise continue
4230 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004231 * This may return before finding all the matches.
4232 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 */
4234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004235ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
4237 static pos_T first_match_pos;
4238 static pos_T last_match_pos;
4239 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004240 static int found_all = FALSE; /* Found all matches of a
4241 certain type. */
4242 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
Bram Moolenaar572cb562005-08-05 21:35:02 +00004244 pos_T *pos;
4245 char_u **matches;
4246 int save_p_scs;
4247 int save_p_ws;
4248 int save_p_ic;
4249 int i;
4250 int num_matches;
4251 int len;
4252 int found_new_match;
4253 int type = ctrl_x_mode;
4254 char_u *ptr;
4255 char_u *dict = NULL;
4256 int dict_f = 0;
4257 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004258 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004260 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004262 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 ins_buf->b_scanned = 0;
4264 found_all = FALSE;
4265 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004266 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004267 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 last_match_pos = first_match_pos = *ini;
4269 }
4270
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004271 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004272 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4274 for (;;)
4275 {
4276 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004277 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004279 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 * or if found_all says this entry is done. For ^X^L only use the
4281 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004282 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004283 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
4285 found_all = FALSE;
4286 while (*e_cpt == ',' || *e_cpt == ' ')
4287 e_cpt++;
4288 if (*e_cpt == '.' && !curbuf->b_scanned)
4289 {
4290 ins_buf = curbuf;
4291 first_match_pos = *ini;
4292 /* So that ^N can match word immediately after cursor */
4293 if (ctrl_x_mode == 0)
4294 dec(&first_match_pos);
4295 last_match_pos = first_match_pos;
4296 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004297
4298 /* Remember the first match so that the loop stops when we
4299 * wrap and come back there a second time. */
4300 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4303 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4304 {
4305 /* Scan a buffer, but not the current one. */
4306 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4307 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004308 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 first_match_pos.col = last_match_pos.col = 0;
4310 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4311 last_match_pos.lnum = 0;
4312 type = 0;
4313 }
4314 else /* unloaded buffer, scan like dictionary */
4315 {
4316 found_all = TRUE;
4317 if (ins_buf->b_fname == NULL)
4318 continue;
4319 type = CTRL_X_DICTIONARY;
4320 dict = ins_buf->b_fname;
4321 dict_f = DICT_EXACT;
4322 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004323 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 ins_buf->b_fname == NULL
4325 ? buf_spname(ins_buf)
4326 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004327 ? ins_buf->b_fname
4328 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004329 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 }
4331 else if (*e_cpt == NUL)
4332 break;
4333 else
4334 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004335 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 type = -1;
4337 else if (*e_cpt == 'k' || *e_cpt == 's')
4338 {
4339 if (*e_cpt == 'k')
4340 type = CTRL_X_DICTIONARY;
4341 else
4342 type = CTRL_X_THESAURUS;
4343 if (*++e_cpt != ',' && *e_cpt != NUL)
4344 {
4345 dict = e_cpt;
4346 dict_f = DICT_FIRST;
4347 }
4348 }
4349#ifdef FEAT_FIND_ID
4350 else if (*e_cpt == 'i')
4351 type = CTRL_X_PATH_PATTERNS;
4352 else if (*e_cpt == 'd')
4353 type = CTRL_X_PATH_DEFINES;
4354#endif
4355 else if (*e_cpt == ']' || *e_cpt == 't')
4356 {
4357 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004358 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004359 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 else
4362 type = -1;
4363
4364 /* in any case e_cpt is advanced to the next entry */
4365 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4366
4367 found_all = TRUE;
4368 if (type == -1)
4369 continue;
4370 }
4371 }
4372
4373 switch (type)
4374 {
4375 case -1:
4376 break;
4377#ifdef FEAT_FIND_ID
4378 case CTRL_X_PATH_PATTERNS:
4379 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004380 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004381 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004383 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4385 (linenr_T)1, (linenr_T)MAXLNUM);
4386 break;
4387#endif
4388
4389 case CTRL_X_DICTIONARY:
4390 case CTRL_X_THESAURUS:
4391 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004392 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 : (type == CTRL_X_THESAURUS
4394 ? (*curbuf->b_p_tsr == NUL
4395 ? p_tsr
4396 : curbuf->b_p_tsr)
4397 : (*curbuf->b_p_dict == NUL
4398 ? p_dict
4399 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004400 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004401 dict != NULL ? dict_f
4402 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 dict = NULL;
4404 break;
4405
4406 case CTRL_X_TAGS:
4407 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4408 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004411 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004412 * of matches is found when compl_pattern is empty */
4413 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4415 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4416 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4417 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004418 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 p_ic = save_p_ic;
4421 break;
4422
4423 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4426 {
4427
4428 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004429 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004430 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432 break;
4433
4434 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004435 if (expand_cmdline(&compl_xp, compl_pattern,
4436 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004438 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 break;
4440
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004441#ifdef FEAT_COMPL_FUNC
4442 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004443 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004444 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004445 break;
4446#endif
4447
Bram Moolenaar488c6512005-08-11 20:09:58 +00004448 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004449#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004450 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004451 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004452 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004453 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004454#endif
4455 break;
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 default: /* normal ^P/^N and ^X^L */
4458 /*
4459 * If 'infercase' is set, don't use 'smartcase' here
4460 */
4461 save_p_scs = p_scs;
4462 if (ins_buf->b_p_inf)
4463 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004464
Bram Moolenaar361aa502014-01-23 22:45:58 +01004465 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 * end but never from the middle, thus setting nowrapscan in this
4467 * buffers is a good idea, on the other hand, we always set
4468 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4469 save_p_ws = p_ws;
4470 if (ins_buf != curbuf)
4471 p_ws = FALSE;
4472 else if (*e_cpt == '.')
4473 p_ws = TRUE;
4474 for (;;)
4475 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004476 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004478 ++msg_silent; /* Don't want messages for wrapscan. */
4479
Bram Moolenaare4214502015-03-05 18:08:43 +01004480 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4481 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004482 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004483 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004484 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004486 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004488 found_new_match = searchit(NULL, ins_buf, pos,
4489 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004490 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004491 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004492 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004493 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004495 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004496 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 first_match_pos = *pos;
4498 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004499 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004502 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 found_new_match = FAIL;
4504 if (found_new_match == FAIL)
4505 {
4506 if (ins_buf == curbuf)
4507 found_all = TRUE;
4508 break;
4509 }
4510
4511 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004512 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 && ini->lnum == pos->lnum
4514 && ini->col == pos->col)
4515 continue;
4516 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004517 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004519 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
4521 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4522 continue;
4523 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4524 if (!p_paste)
4525 ptr = skipwhite(ptr);
4526 }
4527 len = (int)STRLEN(ptr);
4528 }
4529 else
4530 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004531 char_u *tmp_ptr = ptr;
4532
4533 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004535 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 /* Skip if already inside a word. */
4537 if (vim_iswordp(tmp_ptr))
4538 continue;
4539 /* Find start of next word. */
4540 tmp_ptr = find_word_start(tmp_ptr);
4541 }
4542 /* Find end of this word. */
4543 tmp_ptr = find_word_end(tmp_ptr);
4544 len = (int)(tmp_ptr - ptr);
4545
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004546 if ((compl_cont_status & CONT_ADDING)
4547 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
4549 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4550 {
4551 /* Try next line, if any. the new word will be
4552 * "join" as if the normal command "J" was used.
4553 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 * works -- Acevedo */
4556 STRNCPY(IObuff, ptr, len);
4557 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4558 tmp_ptr = ptr = skipwhite(ptr);
4559 /* Find start of next word. */
4560 tmp_ptr = find_word_start(tmp_ptr);
4561 /* Find end of next word. */
4562 tmp_ptr = find_word_end(tmp_ptr);
4563 if (tmp_ptr > ptr)
4564 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004565 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004567 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 IObuff[len++] = ' ';
4569 /* IObuf =~ "\k.* ", thus len >= 2 */
4570 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004571 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 || (vim_strchr(p_cpo, CPO_JOINSP)
4573 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004574 && (IObuff[len - 2] == '?'
4575 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 IObuff[len++] = ' ';
4577 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004578 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 if (tmp_ptr - ptr >= IOSIZE - len)
4580 tmp_ptr = ptr + IOSIZE - len - 1;
4581 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4582 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004583 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585 IObuff[len] = NUL;
4586 ptr = IObuff;
4587 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004588 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 continue;
4590 }
4591 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004592 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004593 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004594 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
4596 found_new_match = OK;
4597 break;
4598 }
4599 }
4600 p_scs = save_p_scs;
4601 p_ws = save_p_ws;
4602 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004603
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004604 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004605 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004606 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 found_new_match = OK;
4608
4609 /* break the loop for specialized modes (use 'complete' just for the
4610 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004611 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004613 {
4614 if (got_int)
4615 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004616 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004617 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004618 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004619
Bram Moolenaare4214502015-03-05 18:08:43 +01004620 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004621 || compl_interrupted)
4622 break;
4623 compl_started = TRUE;
4624 }
4625 else
4626 {
4627 /* Mark a buffer scanned when it has been scanned completely */
4628 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4629 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004631 compl_started = FALSE;
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004634 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
Bram Moolenaare4214502015-03-05 18:08:43 +01004636 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 && *e_cpt == NUL) /* Got to end of 'complete' */
4638 found_new_match = FAIL;
4639
4640 i = -1; /* total of matches, unknown */
4641 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004642 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 i = ins_compl_make_cyclic();
4644
4645 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004646 * just been made cyclic then we have to move compl_curr_match to the next
4647 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004648 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4649 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004650 if (compl_curr_match == NULL)
4651 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 return i;
4653}
4654
4655/* Delete the old text being completed. */
4656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004657ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004659 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
4661 /*
4662 * In insert mode: Delete the typed part.
4663 * In replace mode: Put the old characters back, if any.
4664 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004665 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4666 if ((int)curwin->w_cursor.col > col)
4667 {
4668 if (stop_arrow() == FAIL)
4669 return;
4670 backspace_until_column(col);
4671 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004672
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004673 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4674 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004676 /* clear v:completed_item */
4677 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678}
4679
Bram Moolenaar472e8592016-10-15 17:06:47 +02004680/*
4681 * Insert the new text being completed.
4682 * "in_compl_func" is TRUE when called from complete_check().
4683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004685ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004687 dict_T *dict;
4688
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004689 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004690 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4691 compl_used_match = FALSE;
4692 else
4693 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004694
4695 /* Set completed item. */
4696 /* { word, abbr, menu, kind, info } */
4697 dict = dict_alloc();
4698 if (dict != NULL)
4699 {
4700 dict_add_nr_str(dict, "word", 0L,
4701 EMPTY_IF_NULL(compl_shown_match->cp_str));
4702 dict_add_nr_str(dict, "abbr", 0L,
4703 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4704 dict_add_nr_str(dict, "menu", 0L,
4705 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4706 dict_add_nr_str(dict, "kind", 0L,
4707 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4708 dict_add_nr_str(dict, "info", 0L,
4709 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4710 }
4711 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004712 if (!in_compl_func)
4713 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714}
4715
4716/*
4717 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004718 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4719 * get more completions. If it is FALSE, then we just do nothing when there
4720 * are no more completions in a given direction. The latter case is used when
4721 * we are still in the middle of finding completions, to allow browsing
4722 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 * Return the total number of matches, or -1 if still unknown -- webb.
4724 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004725 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4726 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 *
4728 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004729 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4730 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 */
4732 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004733ins_compl_next(
4734 int allow_get_expansion,
4735 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004736 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004737 int insert_match, /* Insert the newly selected match */
4738 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739{
4740 int num_matches = -1;
4741 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004742 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004743 compl_T *found_compl = NULL;
4744 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004745 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004746 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004748 /* When user complete function return -1 for findstart which is next
4749 * time of 'always', compl_shown_match become NULL. */
4750 if (compl_shown_match == NULL)
4751 return -1;
4752
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004753 if (compl_leader != NULL
4754 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004756 /* Set "compl_shown_match" to the actually shown match, it may differ
4757 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004758 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004759 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004760 && compl_shown_match->cp_next != NULL
4761 && compl_shown_match->cp_next != compl_first_match)
4762 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004763
4764 /* If we didn't find it searching forward, and compl_shows_dir is
4765 * backward, find the last match. */
4766 if (compl_shows_dir == BACKWARD
4767 && !ins_compl_equal(compl_shown_match,
4768 compl_leader, (int)STRLEN(compl_leader))
4769 && (compl_shown_match->cp_next == NULL
4770 || compl_shown_match->cp_next == compl_first_match))
4771 {
4772 while (!ins_compl_equal(compl_shown_match,
4773 compl_leader, (int)STRLEN(compl_leader))
4774 && compl_shown_match->cp_prev != NULL
4775 && compl_shown_match->cp_prev != compl_first_match)
4776 compl_shown_match = compl_shown_match->cp_prev;
4777 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004778 }
4779
4780 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004781 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 /* Delete old text to be replaced */
4783 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004784
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004785 /* When finding the longest common text we stick at the original text,
4786 * don't let CTRL-N or CTRL-P move to the first match. */
4787 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4788
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004789 /* When restarting the search don't insert the first match either. */
4790 if (compl_restarting)
4791 {
4792 advance = FALSE;
4793 compl_restarting = FALSE;
4794 }
4795
Bram Moolenaare3226be2005-12-18 22:10:00 +00004796 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4797 * around. */
4798 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004800 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004802 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004803 found_end = (compl_first_match != NULL
4804 && (compl_shown_match->cp_next == compl_first_match
4805 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004806 }
4807 else if (compl_shows_dir == BACKWARD
4808 && compl_shown_match->cp_prev != NULL)
4809 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004810 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004811 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004812 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
4814 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004815 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004816 if (!allow_get_expansion)
4817 {
4818 if (advance)
4819 {
4820 if (compl_shows_dir == BACKWARD)
4821 compl_pending -= todo + 1;
4822 else
4823 compl_pending += todo + 1;
4824 }
4825 return -1;
4826 }
4827
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004828 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004829 {
4830 if (compl_shows_dir == BACKWARD)
4831 --compl_pending;
4832 else
4833 ++compl_pending;
4834 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004835
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004836 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004837 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004838
4839 /* handle any pending completions */
4840 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004841 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004842 {
4843 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4844 {
4845 compl_shown_match = compl_shown_match->cp_next;
4846 --compl_pending;
4847 }
4848 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4849 {
4850 compl_shown_match = compl_shown_match->cp_prev;
4851 ++compl_pending;
4852 }
4853 else
4854 break;
4855 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004856 found_end = FALSE;
4857 }
4858 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4859 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004860 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004861 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004862 ++todo;
4863 else
4864 /* Remember a matching item. */
4865 found_compl = compl_shown_match;
4866
4867 /* Stop at the end of the list when we found a usable match. */
4868 if (found_end)
4869 {
4870 if (found_compl != NULL)
4871 {
4872 compl_shown_match = found_compl;
4873 break;
4874 }
4875 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004879 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004880 if (compl_no_insert && !started)
4881 {
4882 ins_bytes(compl_orig_text + ins_compl_len());
4883 compl_used_match = FALSE;
4884 }
4885 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004886 {
4887 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004888 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004889 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004890 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004891 }
4892 else
4893 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
4895 if (!allow_get_expansion)
4896 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004897 /* may undisplay the popup menu first */
4898 ins_compl_upd_pum();
4899
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004900 /* redraw to show the user what was inserted */
4901 update_screen(0);
4902
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004903 /* display the updated popup menu */
4904 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004905#ifdef FEAT_GUI
4906 if (gui.in_use)
4907 {
4908 /* Show the cursor after the match, not after the redrawn text. */
4909 setcursor();
4910 out_flush();
4911 gui_update_cursor(FALSE, FALSE);
4912 }
4913#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 /* Delete old text to be replaced, since we're still searching and
4916 * don't want to match ourselves! */
4917 ins_compl_delete();
4918 }
4919
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004920 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004921 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004922 if (compl_no_insert && !started)
4923 compl_enter_selects = TRUE;
4924 else
4925 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004926
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 /*
4928 * Show the file name for the match (if any)
4929 * Truncate the file name to avoid a wait for return.
4930 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004931 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 {
4933 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004934 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 if (i <= 0)
4936 i = 0;
4937 else
4938 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004939 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 msg(IObuff);
4941 redraw_cmdline = FALSE; /* don't overwrite! */
4942 }
4943
4944 return num_matches;
4945}
4946
4947/*
4948 * Call this while finding completions, to check whether the user has hit a key
4949 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004950 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004952 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004953 * "in_compl_func" is TRUE when called from complete_check(), don't set
4954 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 */
4956 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004957ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
4959 static int count = 0;
4960
4961 int c;
4962
4963 /* Don't check when reading keys from a script. That would break the test
4964 * scripts */
4965 if (using_script())
4966 return;
4967
4968 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004969 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 return;
4971 count = 0;
4972
Bram Moolenaara260a972006-06-23 19:36:29 +00004973 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4974 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 if (c != NUL)
4977 {
4978 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4979 {
4980 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004981 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004982 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02004983 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004985 else
4986 {
4987 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004988 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004989 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004990 if (c != K_IGNORE)
4991 {
4992 /* Don't interrupt completion when the character wasn't typed,
4993 * e.g., when doing @q to replay keys. */
4994 if (c != Ctrl_R && KeyTyped)
4995 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004996
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004997 vungetc(c);
4998 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005001 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005002 {
5003 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5004
5005 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005006 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005007 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005008}
5009
5010/*
5011 * Decide the direction of Insert mode complete from the key typed.
5012 * Returns BACKWARD or FORWARD.
5013 */
5014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005015ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005016{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005017 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005018 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005019 return BACKWARD;
5020 return FORWARD;
5021}
5022
5023/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005024 * Return TRUE for keys that are used for completion only when the popup menu
5025 * is visible.
5026 */
5027 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005028ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005029{
5030 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005031 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5032 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005033}
5034
5035/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005036 * Decide the number of completions to move forward.
5037 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5038 */
5039 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005040ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005041{
5042 int h;
5043
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005044 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005045 {
5046 h = pum_get_height();
5047 if (h > 3)
5048 h -= 2; /* keep some context */
5049 return h;
5050 }
5051 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052}
5053
5054/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005055 * Return TRUE if completion with "c" should insert the match, FALSE if only
5056 * to change the currently selected completion.
5057 */
5058 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005059ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005060{
5061 switch (c)
5062 {
5063 case K_UP:
5064 case K_DOWN:
5065 case K_PAGEDOWN:
5066 case K_KPAGEDOWN:
5067 case K_S_DOWN:
5068 case K_PAGEUP:
5069 case K_KPAGEUP:
5070 case K_S_UP:
5071 return FALSE;
5072 }
5073 return TRUE;
5074}
5075
5076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 * Do Insert mode completion.
5078 * Called when character "c" was typed, which has a meaning for completion.
5079 * Returns OK if completion was done, FAIL if something failed (out of mem).
5080 */
5081 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005082ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005084 char_u *line;
5085 int startcol = 0; /* column where searched text starts */
5086 colnr_T curs_col; /* cursor column */
5087 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005088 int save_w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005089 int insert_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090
Bram Moolenaare3226be2005-12-18 22:10:00 +00005091 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005092 insert_match = ins_compl_use_match(c);
5093
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005094 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
5096 /* First time we hit ^N or ^P (in a row, I mean) */
5097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 did_ai = FALSE;
5099#ifdef FEAT_SMARTINDENT
5100 did_si = FALSE;
5101 can_si = FALSE;
5102 can_si_back = FALSE;
5103#endif
5104 if (stop_arrow() == FAIL)
5105 return FAIL;
5106
5107 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005108 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005109 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005111 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005112 * "compl_startpos" to the cursor as a pattern to add a new word
5113 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005114 * "compl_startpos" is not in the same line as the cursor then fix it
5115 * (the line has been split because it was longer than 'tw'). if SOL
5116 * is set then skip the previous pattern, a word at the beginning of
5117 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005118 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5119 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
5121 /*
5122 * it is a continued search
5123 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005124 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5126 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5127 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005128 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005130 /* line (probably) wrapped, set compl_startpos to the
5131 * first non_blank in the line, if it is not a wordchar
5132 * include it to get a better pattern, but then we don't
5133 * want the "\\<" prefix, check it bellow */
5134 compl_col = (colnr_T)(skipwhite(line) - line);
5135 compl_startpos.col = compl_col;
5136 compl_startpos.lnum = curwin->w_cursor.lnum;
5137 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
5139 else
5140 {
5141 /* S_IPOS was set when we inserted a word that was at the
5142 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005143 * mode but first we need to redefine compl_startpos */
5144 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005146 compl_cont_status |= CONT_SOL;
5147 compl_startpos.col = (colnr_T)(skipwhite(
5148 line + compl_length
5149 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005151 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005153 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005154 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005155 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005157 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005159 compl_cont_status &= ~CONT_SOL;
5160 compl_length = (IOSIZE - MIN_SPACE);
5161 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005163 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5164 if (compl_length < 1)
5165 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005167 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005168 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005170 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005173 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005175 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005177 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005179 compl_cont_status = 0;
5180 compl_cont_status |= CONT_N_ADDS;
5181 compl_startpos = curwin->w_cursor;
5182 startcol = (int)curs_col;
5183 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
5185
5186 /* Work out completion pattern and original text -- webb */
5187 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5188 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005189 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5191 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005192 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005194 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005196 compl_col += ++startcol;
5197 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
5199 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005200 compl_pattern = str_foldcase(line + compl_col,
5201 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005203 compl_pattern = vim_strnsave(line + compl_col,
5204 compl_length);
5205 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 return FAIL;
5207 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005208 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 {
5210 char_u *prefix = (char_u *)"\\<";
5211
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005212 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005213 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005214 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005215 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005217 if (!vim_iswordp(line + compl_col)
5218 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 && (
5220#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005221 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224#endif
5225 )))
5226 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005227 STRCPY((char *)compl_pattern, prefix);
5228 (void)quote_meta(compl_pattern + STRLEN(prefix),
5229 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005231 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005233 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005235 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236#endif
5237 )
5238 {
5239 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005240 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5241 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005243 compl_col += curs_col;
5244 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 }
5246 else
5247 {
5248#ifdef FEAT_MBYTE
5249 /* Search the point of change class of multibyte character
5250 * or not a word single byte character backward. */
5251 if (has_mbyte)
5252 {
5253 int base_class;
5254 int head_off;
5255
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005256 startcol -= (*mb_head_off)(line, line + startcol);
5257 base_class = mb_get_class(line + startcol);
5258 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005260 head_off = (*mb_head_off)(line, line + startcol);
5261 if (base_class != mb_get_class(line + startcol
5262 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005264 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
5266 }
5267 else
5268#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005269 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005271 compl_col += ++startcol;
5272 compl_length = (int)curs_col - startcol;
5273 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
5275 /* Only match word with at least two chars -- webb
5276 * there's no need to call quote_meta,
5277 * alloc(7) is enough -- Acevedo
5278 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005279 compl_pattern = alloc(7);
5280 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005282 STRCPY((char *)compl_pattern, "\\<");
5283 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5284 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286 else
5287 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005289 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005290 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005292 STRCPY((char *)compl_pattern, "\\<");
5293 (void)quote_meta(compl_pattern + 2, line + compl_col,
5294 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296 }
5297 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005298 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005300 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 compl_length = (int)curs_col - (int)compl_col;
5302 if (compl_length < 0) /* cursor in indent: empty pattern */
5303 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005305 compl_pattern = str_foldcase(line + compl_col, compl_length,
5306 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5309 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 return FAIL;
5311 }
5312 else if (ctrl_x_mode == CTRL_X_FILES)
5313 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005314 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005315 if (startcol > 0)
5316 {
5317 char_u *p = line + startcol;
5318
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005319 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005320 while (p > line && vim_isfilec(PTR2CHAR(p)))
5321 mb_ptr_back(line, p);
5322 if (p == line && vim_isfilec(PTR2CHAR(p)))
5323 startcol = 0;
5324 else
5325 startcol = (int)(p - line) + 1;
5326 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005327
Bram Moolenaar0300e462013-09-08 16:03:45 +02005328 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005329 compl_length = (int)curs_col - startcol;
5330 compl_pattern = addstar(line + compl_col, compl_length,
5331 EXPAND_FILES);
5332 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 return FAIL;
5334 }
5335 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5336 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 compl_pattern = vim_strnsave(line, curs_col);
5338 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005340 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005341 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5343 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005344 /* No completion possible, use an empty pattern to get a
5345 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005346 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005347 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005348 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5349 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005351 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005352 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005353#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005354 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005355 * Call user defined function 'completefunc' with "a:findstart"
5356 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005357 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005358 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005359 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005360 char_u *funcname;
5361 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005362 win_T *curwin_save;
5363 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005364
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005365 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005366 * string */
5367 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5368 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5369 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005370 {
5371 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5372 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005373 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005374 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005375
5376 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005377 args[1] = NULL;
5378 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005379 curwin_save = curwin;
5380 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005381 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005382 if (curwin_save != curwin || curbuf_save != curbuf)
5383 {
5384 EMSG(_(e_complwin));
5385 return FAIL;
5386 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005387 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005388 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005389 if (!equalpos(curwin->w_cursor, pos))
5390 {
5391 EMSG(_(e_compldel));
5392 return FAIL;
5393 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005394
Bram Moolenaar6110a002012-01-26 18:58:38 +01005395 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005396 * cancel the complete without an error.
5397 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005398 if (col == -2)
5399 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005400 if (col == -3)
5401 {
5402 ctrl_x_mode = 0;
5403 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005404 if (!shortmess(SHM_COMPLETIONMENU))
5405 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005406 return FAIL;
5407 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005408
Bram Moolenaar82139082011-09-14 16:52:09 +02005409 /*
5410 * Reset extended parameters of completion, when start new
5411 * completion.
5412 */
5413 compl_opt_refresh_always = FALSE;
5414
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005415 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005416 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005417 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005418 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005419 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005420
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 /* Setup variables for completion. Need to obtain "line" again,
5422 * it may have become invalid. */
5423 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005424 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005425 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5426 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005427#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005428 return FAIL;
5429 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005430 else if (ctrl_x_mode == CTRL_X_SPELL)
5431 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005432#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005433 if (spell_bad_len > 0)
5434 compl_col = curs_col - spell_bad_len;
5435 else
5436 compl_col = spell_word_start(startcol);
5437 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005438 {
5439 compl_length = 0;
5440 compl_col = curs_col;
5441 }
5442 else
5443 {
5444 spell_expand_check_cap(compl_col);
5445 compl_length = (int)curs_col - compl_col;
5446 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005447 /* Need to obtain "line" again, it may have become invalid. */
5448 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005449 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5450 if (compl_pattern == NULL)
5451#endif
5452 return FAIL;
5453 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005454 else
5455 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005456 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005457 return FAIL;
5458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005460 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 {
5462 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005463 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 {
5465 /* Insert a new line, keep indentation but ignore 'comments' */
5466#ifdef FEAT_COMMENTS
5467 char_u *old = curbuf->b_p_com;
5468
5469 curbuf->b_p_com = (char_u *)"";
5470#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005471 compl_startpos.lnum = curwin->w_cursor.lnum;
5472 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 ins_eol('\r');
5474#ifdef FEAT_COMMENTS
5475 curbuf->b_p_com = old;
5476#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005477 compl_length = 0;
5478 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
5480 }
5481 else
5482 {
5483 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005484 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
5486
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005487 if (compl_cont_status & CONT_LOCAL)
5488 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 else
5490 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5491
Bram Moolenaar62951b12011-09-21 18:23:05 +02005492 /* If any of the original typed text has been changed we need to fix
5493 * the redo buffer. */
5494 ins_compl_fixRedoBufForLeader(NULL);
5495
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005496 /* Always add completion for the original text. */
5497 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005498 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5499 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005500 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005502 vim_free(compl_pattern);
5503 compl_pattern = NULL;
5504 vim_free(compl_orig_text);
5505 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 return FAIL;
5507 }
5508
5509 /* showmode might reset the internal line pointers, so it must
5510 * be called before line = ml_get(), or when this address is no
5511 * longer needed. -- Acevedo.
5512 */
5513 edit_submode_extra = (char_u *)_("-- Searching...");
5514 edit_submode_highl = HLF_COUNT;
5515 showmode();
5516 edit_submode_extra = NULL;
5517 out_flush();
5518 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005519 else if (insert_match && stop_arrow() == FAIL)
5520 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005522 compl_shown_match = compl_curr_match;
5523 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
5525 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005526 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005528 save_w_wrow = curwin->w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005529 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005531 /* may undisplay the popup menu */
5532 ins_compl_upd_pum();
5533
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005534 if (n > 1) /* all matches have been found */
5535 compl_matches = n;
5536 compl_curr_match = compl_shown_match;
5537 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
Bram Moolenaard68071d2006-05-02 22:08:30 +00005539 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5540 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 if (got_int && !global_busy)
5542 {
5543 (void)vgetc();
5544 got_int = FALSE;
5545 }
5546
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005547 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005548 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005550 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5551 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5553 edit_submode_highl = HLF_E;
5554 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5555 * because we couldn't expand anything at first place, but if we used
5556 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5557 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005558 if ( compl_length > 1
5559 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 || (ctrl_x_mode != 0
5561 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5562 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005563 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 }
5565
Bram Moolenaar572cb562005-08-05 21:35:02 +00005566 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005567 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005569 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
5571 if (edit_submode_extra == NULL)
5572 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005573 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 {
5575 edit_submode_extra = (char_u *)_("Back at original");
5576 edit_submode_highl = HLF_W;
5577 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005578 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
5580 edit_submode_extra = (char_u *)_("Word from other line");
5581 edit_submode_highl = HLF_COUNT;
5582 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005583 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 {
5585 edit_submode_extra = (char_u *)_("The only match");
5586 edit_submode_highl = HLF_COUNT;
5587 }
5588 else
5589 {
5590 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005591 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005593 int number = 0;
5594 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005596 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 {
5598 /* search backwards for the first valid (!= -1) number.
5599 * This should normally succeed already at the first loop
5600 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005601 for (match = compl_curr_match->cp_prev; match != NULL
5602 && match != compl_first_match;
5603 match = match->cp_prev)
5604 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005606 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 break;
5608 }
5609 if (match != NULL)
5610 /* go up and assign all numbers which are not assigned
5611 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005612 for (match = match->cp_next;
5613 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005614 match = match->cp_next)
5615 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 }
5617 else /* BACKWARD */
5618 {
5619 /* search forwards (upwards) for the first valid (!= -1)
5620 * number. This should normally succeed already at the
5621 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005622 for (match = compl_curr_match->cp_next; match != NULL
5623 && match != compl_first_match;
5624 match = match->cp_next)
5625 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005627 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 break;
5629 }
5630 if (match != NULL)
5631 /* go down and assign all numbers which are not
5632 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005633 for (match = match->cp_prev; match
5634 && match->cp_number == -1;
5635 match = match->cp_prev)
5636 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
5638 }
5639
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005640 /* The match should always have a sequence number now, this is
5641 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005642 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005644 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5645 * Translations may need more than twice that. */
5646 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005648 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005649 vim_snprintf((char *)match_ref, sizeof(match_ref),
5650 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005651 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005653 vim_snprintf((char *)match_ref, sizeof(match_ref),
5654 _("match %d"),
5655 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 edit_submode_extra = match_ref;
5657 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005658 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 curs_columns(FALSE);
5660 }
5661 }
5662 }
5663
5664 /* Show a message about what (completion) mode we're in. */
5665 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005666 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005668 if (edit_submode_extra != NULL)
5669 {
5670 if (!p_smd)
5671 msg_attr(edit_submode_extra,
5672 edit_submode_highl < HLF_COUNT
5673 ? hl_attr(edit_submode_highl) : 0);
5674 }
5675 else
5676 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678
Bram Moolenaard68071d2006-05-02 22:08:30 +00005679 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005680 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005681 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005682 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005683 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005684 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005685 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005686
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 return OK;
5688}
5689
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005690 static void
5691show_pum(int save_w_wrow)
5692{
5693 /* RedrawingDisabled may be set when invoked through complete(). */
5694 int n = RedrawingDisabled;
5695
5696 RedrawingDisabled = 0;
5697
5698 /* If the cursor moved we need to remove the pum first. */
5699 setcursor();
5700 if (save_w_wrow != curwin->w_wrow)
5701 ins_compl_del_pum();
5702
5703 ins_compl_show_pum();
5704 setcursor();
5705 RedrawingDisabled = n;
5706}
5707
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708/*
5709 * Looks in the first "len" chars. of "src" for search-metachars.
5710 * If dest is not NULL the chars. are copied there quoting (with
5711 * a backslash) the metachars, and dest would be NUL terminated.
5712 * Returns the length (needed) of dest
5713 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005714 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005715quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005717 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005719 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 switch (*src)
5722 {
5723 case '.':
5724 case '*':
5725 case '[':
5726 if (ctrl_x_mode == CTRL_X_DICTIONARY
5727 || ctrl_x_mode == CTRL_X_THESAURUS)
5728 break;
5729 case '~':
5730 if (!p_magic) /* quote these only if magic is set */
5731 break;
5732 case '\\':
5733 if (ctrl_x_mode == CTRL_X_DICTIONARY
5734 || ctrl_x_mode == CTRL_X_THESAURUS)
5735 break;
5736 case '^': /* currently it's not needed. */
5737 case '$':
5738 m++;
5739 if (dest != NULL)
5740 *dest++ = '\\';
5741 break;
5742 }
5743 if (dest != NULL)
5744 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005745# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 /* Copy remaining bytes of a multibyte character. */
5747 if (has_mbyte)
5748 {
5749 int i, mb_len;
5750
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005751 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 if (mb_len > 0 && len >= mb_len)
5753 for (i = 0; i < mb_len; ++i)
5754 {
5755 --len;
5756 ++src;
5757 if (dest != NULL)
5758 *dest++ = *src;
5759 }
5760 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 }
5763 if (dest != NULL)
5764 *dest = NUL;
5765
5766 return m;
5767}
5768#endif /* FEAT_INS_EXPAND */
5769
5770/*
5771 * Next character is interpreted literally.
5772 * A one, two or three digit decimal number is interpreted as its byte value.
5773 * If one or two digits are entered, the next character is given to vungetc().
5774 * For Unicode a character > 255 may be returned.
5775 */
5776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005777get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
5779 int cc;
5780 int nc;
5781 int i;
5782 int hex = FALSE;
5783 int octal = FALSE;
5784#ifdef FEAT_MBYTE
5785 int unicode = 0;
5786#endif
5787
5788 if (got_int)
5789 return Ctrl_C;
5790
5791#ifdef FEAT_GUI
5792 /*
5793 * In GUI there is no point inserting the internal code for a special key.
5794 * It is more useful to insert the string "<KEY>" instead. This would
5795 * probably be useful in a text window too, but it would not be
5796 * vi-compatible (maybe there should be an option for it?) -- webb
5797 */
5798 if (gui.in_use)
5799 ++allow_keys;
5800#endif
5801#ifdef USE_ON_FLY_SCROLL
5802 dont_scroll = TRUE; /* disallow scrolling here */
5803#endif
5804 ++no_mapping; /* don't map the next key hits */
5805 cc = 0;
5806 i = 0;
5807 for (;;)
5808 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005809 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810#ifdef FEAT_CMDL_INFO
5811 if (!(State & CMDLINE)
5812# ifdef FEAT_MBYTE
5813 && MB_BYTE2LEN_CHECK(nc) == 1
5814# endif
5815 )
5816 add_to_showcmd(nc);
5817#endif
5818 if (nc == 'x' || nc == 'X')
5819 hex = TRUE;
5820 else if (nc == 'o' || nc == 'O')
5821 octal = TRUE;
5822#ifdef FEAT_MBYTE
5823 else if (nc == 'u' || nc == 'U')
5824 unicode = nc;
5825#endif
5826 else
5827 {
5828 if (hex
5829#ifdef FEAT_MBYTE
5830 || unicode != 0
5831#endif
5832 )
5833 {
5834 if (!vim_isxdigit(nc))
5835 break;
5836 cc = cc * 16 + hex2nr(nc);
5837 }
5838 else if (octal)
5839 {
5840 if (nc < '0' || nc > '7')
5841 break;
5842 cc = cc * 8 + nc - '0';
5843 }
5844 else
5845 {
5846 if (!VIM_ISDIGIT(nc))
5847 break;
5848 cc = cc * 10 + nc - '0';
5849 }
5850
5851 ++i;
5852 }
5853
5854 if (cc > 255
5855#ifdef FEAT_MBYTE
5856 && unicode == 0
5857#endif
5858 )
5859 cc = 255; /* limit range to 0-255 */
5860 nc = 0;
5861
5862 if (hex) /* hex: up to two chars */
5863 {
5864 if (i >= 2)
5865 break;
5866 }
5867#ifdef FEAT_MBYTE
5868 else if (unicode) /* Unicode: up to four or eight chars */
5869 {
5870 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5871 break;
5872 }
5873#endif
5874 else if (i >= 3) /* decimal or octal: up to three chars */
5875 break;
5876 }
5877 if (i == 0) /* no number entered */
5878 {
5879 if (nc == K_ZERO) /* NUL is stored as NL */
5880 {
5881 cc = '\n';
5882 nc = 0;
5883 }
5884 else
5885 {
5886 cc = nc;
5887 nc = 0;
5888 }
5889 }
5890
5891 if (cc == 0) /* NUL is stored as NL */
5892 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005893#ifdef FEAT_MBYTE
5894 if (enc_dbcs && (cc & 0xff) == 0)
5895 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5896 second byte will cause trouble! */
5897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
5899 --no_mapping;
5900#ifdef FEAT_GUI
5901 if (gui.in_use)
5902 --allow_keys;
5903#endif
5904 if (nc)
5905 vungetc(nc);
5906 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5907 return cc;
5908}
5909
5910/*
5911 * Insert character, taking care of special keys and mod_mask
5912 */
5913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005914insert_special(
5915 int c,
5916 int allow_modmask,
5917 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918{
5919 char_u *p;
5920 int len;
5921
5922 /*
5923 * Special function key, translate into "<Key>". Up to the last '>' is
5924 * inserted with ins_str(), so as not to replace characters in replace
5925 * mode.
5926 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5927 * unless 'allow_modmask' is TRUE.
5928 */
5929#ifdef MACOS
5930 /* Command-key never produces a normal key */
5931 if (mod_mask & MOD_MASK_CMD)
5932 allow_modmask = TRUE;
5933#endif
5934 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5935 {
5936 p = get_special_key_name(c, mod_mask);
5937 len = (int)STRLEN(p);
5938 c = p[len - 1];
5939 if (len > 2)
5940 {
5941 if (stop_arrow() == FAIL)
5942 return;
5943 p[len - 1] = NUL;
5944 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005945 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 ctrlv = FALSE;
5947 }
5948 }
5949 if (stop_arrow() == OK)
5950 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5951}
5952
5953/*
5954 * Special characters in this context are those that need processing other
5955 * than the simple insertion that can be performed here. This includes ESC
5956 * which terminates the insert, and CR/NL which need special processing to
5957 * open up a new line. This routine tries to optimize insertions performed by
5958 * the "redo", "undo" or "put" commands, so it needs to know when it should
5959 * stop and defer processing to the "normal" mechanism.
5960 * '0' and '^' are special, because they can be followed by CTRL-D.
5961 */
5962#ifdef EBCDIC
5963# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5964#else
5965# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5966#endif
5967
5968#ifdef FEAT_MBYTE
5969# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5970#else
5971# define WHITECHAR(cc) vim_iswhite(cc)
5972#endif
5973
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005974/*
5975 * "flags": INSCHAR_FORMAT - force formatting
5976 * INSCHAR_CTRLV - char typed just after CTRL-V
5977 * INSCHAR_NO_FEX - don't use 'formatexpr'
5978 *
5979 * NOTE: passes the flags value straight through to internal_format() which,
5980 * beside INSCHAR_FORMAT (above), is also looking for these:
5981 * INSCHAR_DO_COM - format comments
5982 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005985insertchar(
5986 int c, /* character to insert or NUL */
5987 int flags, /* INSCHAR_FORMAT, etc. */
5988 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 int textwidth;
5991#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005995 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005997 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
6000 /*
6001 * Try to break the line in two or more pieces when:
6002 * - Always do this if we have been called to do formatting only.
6003 * - Always do this when 'formatoptions' has the 'a' flag and the line
6004 * ends in white space.
6005 * - Otherwise:
6006 * - Don't do this if inserting a blank
6007 * - Don't do this if an existing character is being replaced, unless
6008 * we're in VREPLACE mode.
6009 * - Do this if the cursor is not on the line where insert started
6010 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6011 * before the insert.
6012 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6013 * before 'textwidth'
6014 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006015 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006016 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 || (!vim_iswhite(c)
6018 && !((State & REPLACE_FLAG)
6019#ifdef FEAT_VREPLACE
6020 && !(State & VREPLACE_FLAG)
6021#endif
6022 && *ml_get_cursor() != NUL)
6023 && (curwin->w_cursor.lnum != Insstart.lnum
6024 || ((!has_format_option(FO_INS_LONG)
6025 || Insstart_textlen <= (colnr_T)textwidth)
6026 && (!fo_ins_blank
6027 || Insstart_blank_vcol <= (colnr_T)textwidth
6028 ))))))
6029 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006030 /* Format with 'formatexpr' when it's set. Use internal formatting
6031 * when 'formatexpr' isn't set or it returns non-zero. */
6032#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006033 int do_internal = TRUE;
6034 colnr_T virtcol = get_nolist_virtcol()
6035 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006036
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006037 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6038 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006039 {
6040 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6041 /* It may be required to save for undo again, e.g. when setline()
6042 * was called. */
6043 ins_need_undo = TRUE;
6044 }
6045 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006047 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 if (c == NUL) /* only formatting was wanted */
6051 return;
6052
6053#ifdef FEAT_COMMENTS
6054 /* Check whether this character should end a comment. */
6055 if (did_ai && (int)c == end_comment_pending)
6056 {
6057 char_u *line;
6058 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6059 int middle_len, end_len;
6060 int i;
6061
6062 /*
6063 * Need to remove existing (middle) comment leader and insert end
6064 * comment leader. First, check what comment leader we can find.
6065 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006066 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6068 {
6069 /* Skip middle-comment string */
6070 while (*p && p[-1] != ':') /* find end of middle flags */
6071 ++p;
6072 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6073 /* Don't count trailing white space for middle_len */
6074 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6075 --middle_len;
6076
6077 /* Find the end-comment string */
6078 while (*p && p[-1] != ':') /* find end of end flags */
6079 ++p;
6080 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6081
6082 /* Skip white space before the cursor */
6083 i = curwin->w_cursor.col;
6084 while (--i >= 0 && vim_iswhite(line[i]))
6085 ;
6086 i++;
6087
6088 /* Skip to before the middle leader */
6089 i -= middle_len;
6090
6091 /* Check some expected things before we go on */
6092 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6093 {
6094 /* Backspace over all the stuff we want to replace */
6095 backspace_until_column(i);
6096
6097 /*
6098 * Insert the end-comment string, except for the last
6099 * character, which will get inserted as normal later.
6100 */
6101 ins_bytes_len(lead_end, end_len - 1);
6102 }
6103 }
6104 }
6105 end_comment_pending = NUL;
6106#endif
6107
6108 did_ai = FALSE;
6109#ifdef FEAT_SMARTINDENT
6110 did_si = FALSE;
6111 can_si = FALSE;
6112 can_si_back = FALSE;
6113#endif
6114
6115 /*
6116 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6117 * This speeds up normal text input considerably.
6118 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6119 * need to re-indent at a ':', or any other character (but not what
6120 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006121 * Don't do this when there an InsertCharPre autocommand is defined,
6122 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 */
6124#ifdef USE_ON_FLY_SCROLL
6125 dont_scroll = FALSE; /* allow scrolling here */
6126#endif
6127
6128 if ( !ISSPECIAL(c)
6129#ifdef FEAT_MBYTE
6130 && (!has_mbyte || (*mb_char2len)(c) == 1)
6131#endif
6132 && vpeekc() != NUL
6133 && !(State & REPLACE_FLAG)
6134#ifdef FEAT_CINDENT
6135 && !cindent_on()
6136#endif
6137#ifdef FEAT_RIGHTLEFT
6138 && !p_ri
6139#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006140#ifdef FEAT_AUTOCMD
6141 && !has_insertcharpre()
6142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 )
6144 {
6145#define INPUT_BUFLEN 100
6146 char_u buf[INPUT_BUFLEN + 1];
6147 int i;
6148 colnr_T virtcol = 0;
6149
6150 buf[0] = c;
6151 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006152 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 virtcol = get_nolist_virtcol();
6154 /*
6155 * Stop the string when:
6156 * - no more chars available
6157 * - finding a special character (command key)
6158 * - buffer is full
6159 * - running into the 'textwidth' boundary
6160 * - need to check for abbreviation: A non-word char after a word-char
6161 */
6162 while ( (c = vpeekc()) != NUL
6163 && !ISSPECIAL(c)
6164#ifdef FEAT_MBYTE
6165 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6166#endif
6167 && i < INPUT_BUFLEN
6168 && (textwidth == 0
6169 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6170 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6171 {
6172#ifdef FEAT_RIGHTLEFT
6173 c = vgetc();
6174 if (p_hkmap && KeyTyped)
6175 c = hkmap(c); /* Hebrew mode mapping */
6176# ifdef FEAT_FKMAP
6177 if (p_fkmap && KeyTyped)
6178 c = fkmap(c); /* Farsi mode mapping */
6179# endif
6180 buf[i++] = c;
6181#else
6182 buf[i++] = vgetc();
6183#endif
6184 }
6185
6186#ifdef FEAT_DIGRAPHS
6187 do_digraph(-1); /* clear digraphs */
6188 do_digraph(buf[i-1]); /* may be the start of a digraph */
6189#endif
6190 buf[i] = NUL;
6191 ins_str(buf);
6192 if (flags & INSCHAR_CTRLV)
6193 {
6194 redo_literal(*buf);
6195 i = 1;
6196 }
6197 else
6198 i = 0;
6199 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006200 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 }
6202 else
6203 {
6204#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006205 int cc;
6206
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6208 {
6209 char_u buf[MB_MAXBYTES + 1];
6210
6211 (*mb_char2bytes)(c, buf);
6212 buf[cc] = NUL;
6213 ins_char_bytes(buf, cc);
6214 AppendCharToRedobuff(c);
6215 }
6216 else
6217#endif
6218 {
6219 ins_char(c);
6220 if (flags & INSCHAR_CTRLV)
6221 redo_literal(c);
6222 else
6223 AppendCharToRedobuff(c);
6224 }
6225 }
6226}
6227
6228/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006229 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006230 *
6231 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6232 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006233 */
6234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006235internal_format(
6236 int textwidth,
6237 int second_indent,
6238 int flags,
6239 int format_only,
6240 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006241{
6242 int cc;
6243 int save_char = NUL;
6244 int haveto_redraw = FALSE;
6245 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6246#ifdef FEAT_MBYTE
6247 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6248#endif
6249 int fo_white_par = has_format_option(FO_WHITE_PAR);
6250 int first_line = TRUE;
6251#ifdef FEAT_COMMENTS
6252 colnr_T leader_len;
6253 int no_leader = FALSE;
6254 int do_comments = (flags & INSCHAR_DO_COM);
6255#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006256#ifdef FEAT_LINEBREAK
6257 int has_lbr = curwin->w_p_lbr;
6258
6259 /* make sure win_lbr_chartabsize() counts correctly */
6260 curwin->w_p_lbr = FALSE;
6261#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006262
6263 /*
6264 * When 'ai' is off we don't want a space under the cursor to be
6265 * deleted. Replace it with an 'x' temporarily.
6266 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006267 if (!curbuf->b_p_ai
6268#ifdef FEAT_VREPLACE
6269 && !(State & VREPLACE_FLAG)
6270#endif
6271 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006272 {
6273 cc = gchar_cursor();
6274 if (vim_iswhite(cc))
6275 {
6276 save_char = cc;
6277 pchar_cursor('x');
6278 }
6279 }
6280
6281 /*
6282 * Repeat breaking lines, until the current line is not too long.
6283 */
6284 while (!got_int)
6285 {
6286 int startcol; /* Cursor column at entry */
6287 int wantcol; /* column at textwidth border */
6288 int foundcol; /* column for start of spaces */
6289 int end_foundcol = 0; /* column for start of word */
6290 colnr_T len;
6291 colnr_T virtcol;
6292#ifdef FEAT_VREPLACE
6293 int orig_col = 0;
6294 char_u *saved_text = NULL;
6295#endif
6296 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006297 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006298
Bram Moolenaar97b98102009-11-17 16:41:01 +00006299 virtcol = get_nolist_virtcol()
6300 + char2cells(c != NUL ? c : gchar_cursor());
6301 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006302 break;
6303
6304#ifdef FEAT_COMMENTS
6305 if (no_leader)
6306 do_comments = FALSE;
6307 else if (!(flags & INSCHAR_FORMAT)
6308 && has_format_option(FO_WRAP_COMS))
6309 do_comments = TRUE;
6310
6311 /* Don't break until after the comment leader */
6312 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006313 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006314 else
6315 leader_len = 0;
6316
6317 /* If the line doesn't start with a comment leader, then don't
6318 * start one in a following broken line. Avoids that a %word
6319 * moved to the start of the next line causes all following lines
6320 * to start with %. */
6321 if (leader_len == 0)
6322 no_leader = TRUE;
6323#endif
6324 if (!(flags & INSCHAR_FORMAT)
6325#ifdef FEAT_COMMENTS
6326 && leader_len == 0
6327#endif
6328 && !has_format_option(FO_WRAP))
6329
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006330 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006331 if ((startcol = curwin->w_cursor.col) == 0)
6332 break;
6333
6334 /* find column of textwidth border */
6335 coladvance((colnr_T)textwidth);
6336 wantcol = curwin->w_cursor.col;
6337
Bram Moolenaar97b98102009-11-17 16:41:01 +00006338 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006339 foundcol = 0;
6340
6341 /*
6342 * Find position to break at.
6343 * Stop at first entered white when 'formatoptions' has 'v'
6344 */
6345 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006346 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006347 || curwin->w_cursor.lnum != Insstart.lnum
6348 || curwin->w_cursor.col >= Insstart.col)
6349 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006350 if (curwin->w_cursor.col == startcol && c != NUL)
6351 cc = c;
6352 else
6353 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006354 if (WHITECHAR(cc))
6355 {
6356 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006357 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006358
6359 /* find start of sequence of blanks */
6360 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6361 {
6362 dec_cursor();
6363 cc = gchar_cursor();
6364 }
6365 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6366 break; /* only spaces in front of text */
6367#ifdef FEAT_COMMENTS
6368 /* Don't break until after the comment leader */
6369 if (curwin->w_cursor.col < leader_len)
6370 break;
6371#endif
6372 if (has_format_option(FO_ONE_LETTER))
6373 {
6374 /* do not break after one-letter words */
6375 if (curwin->w_cursor.col == 0)
6376 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006377#ifdef FEAT_COMMENTS
6378 /* do not break "#a b" when 'tw' is 2 */
6379 if (curwin->w_cursor.col <= leader_len)
6380 break;
6381#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006382 col = curwin->w_cursor.col;
6383 dec_cursor();
6384 cc = gchar_cursor();
6385
6386 if (WHITECHAR(cc))
6387 continue; /* one-letter, continue */
6388 curwin->w_cursor.col = col;
6389 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006390
6391 inc_cursor();
6392
6393 end_foundcol = end_col + 1;
6394 foundcol = curwin->w_cursor.col;
6395 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006396 break;
6397 }
6398#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006399 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006400 {
6401 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006402 if (curwin->w_cursor.col != startcol)
6403 {
6404#ifdef FEAT_COMMENTS
6405 /* Don't break until after the comment leader */
6406 if (curwin->w_cursor.col < leader_len)
6407 break;
6408#endif
6409 col = curwin->w_cursor.col;
6410 inc_cursor();
6411 /* Don't change end_foundcol if already set. */
6412 if (foundcol != curwin->w_cursor.col)
6413 {
6414 foundcol = curwin->w_cursor.col;
6415 end_foundcol = foundcol;
6416 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6417 break;
6418 }
6419 curwin->w_cursor.col = col;
6420 }
6421
6422 if (curwin->w_cursor.col == 0)
6423 break;
6424
6425 col = curwin->w_cursor.col;
6426
6427 dec_cursor();
6428 cc = gchar_cursor();
6429
6430 if (WHITECHAR(cc))
6431 continue; /* break with space */
6432#ifdef FEAT_COMMENTS
6433 /* Don't break until after the comment leader */
6434 if (curwin->w_cursor.col < leader_len)
6435 break;
6436#endif
6437
6438 curwin->w_cursor.col = col;
6439
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006441 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006442 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6443 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006444 }
6445#endif
6446 if (curwin->w_cursor.col == 0)
6447 break;
6448 dec_cursor();
6449 }
6450
6451 if (foundcol == 0) /* no spaces, cannot break line */
6452 {
6453 curwin->w_cursor.col = startcol;
6454 break;
6455 }
6456
6457 /* Going to break the line, remove any "$" now. */
6458 undisplay_dollar();
6459
6460 /*
6461 * Offset between cursor position and line break is used by replace
6462 * stack functions. VREPLACE does not use this, and backspaces
6463 * over the text instead.
6464 */
6465#ifdef FEAT_VREPLACE
6466 if (State & VREPLACE_FLAG)
6467 orig_col = startcol; /* Will start backspacing from here */
6468 else
6469#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006470 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006471
6472 /*
6473 * adjust startcol for spaces that will be deleted and
6474 * characters that will remain on top line
6475 */
6476 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006477 while ((cc = gchar_cursor(), WHITECHAR(cc))
6478 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006479 inc_cursor();
6480 startcol -= curwin->w_cursor.col;
6481 if (startcol < 0)
6482 startcol = 0;
6483
6484#ifdef FEAT_VREPLACE
6485 if (State & VREPLACE_FLAG)
6486 {
6487 /*
6488 * In VREPLACE mode, we will backspace over the text to be
6489 * wrapped, so save a copy now to put on the next line.
6490 */
6491 saved_text = vim_strsave(ml_get_cursor());
6492 curwin->w_cursor.col = orig_col;
6493 if (saved_text == NULL)
6494 break; /* Can't do it, out of memory */
6495 saved_text[startcol] = NUL;
6496
6497 /* Backspace over characters that will move to the next line */
6498 if (!fo_white_par)
6499 backspace_until_column(foundcol);
6500 }
6501 else
6502#endif
6503 {
6504 /* put cursor after pos. to break line */
6505 if (!fo_white_par)
6506 curwin->w_cursor.col = foundcol;
6507 }
6508
6509 /*
6510 * Split the line just before the margin.
6511 * Only insert/delete lines, but don't really redraw the window.
6512 */
6513 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6514 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6515#ifdef FEAT_COMMENTS
6516 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006517 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006518#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006519 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6520 if (!(flags & INSCHAR_COM_LIST))
6521 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006522
6523 replace_offset = 0;
6524 if (first_line)
6525 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006526 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006527 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006528 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006529 * This section is for auto-wrap of numeric lists. When not
6530 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6531 * flag will be set and open_line() will handle it (as seen
6532 * above). The code here (and in get_number_indent()) will
6533 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006534 */
6535 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006536 second_indent =
6537 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006538 if (second_indent >= 0)
6539 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006540#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006541 if (State & VREPLACE_FLAG)
6542 change_indent(INDENT_SET, second_indent,
6543 FALSE, NUL, TRUE);
6544 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006545#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006546#ifdef FEAT_COMMENTS
6547 if (leader_len > 0 && second_indent - leader_len > 0)
6548 {
6549 int i;
6550 int padding = second_indent - leader_len;
6551
6552 /* We started at the first_line of a numbered list
6553 * that has a comment. the open_line() function has
6554 * inserted the proper comment leader and positioned
6555 * the cursor at the end of the split line. Now we
6556 * add the additional whitespace needed after the
6557 * comment leader for the numbered list. */
6558 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006559 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006560 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006561 }
6562 else
6563 {
6564#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006565 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006566#ifdef FEAT_COMMENTS
6567 }
6568#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006569 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006570 }
6571 first_line = FALSE;
6572 }
6573
6574#ifdef FEAT_VREPLACE
6575 if (State & VREPLACE_FLAG)
6576 {
6577 /*
6578 * In VREPLACE mode we have backspaced over the text to be
6579 * moved, now we re-insert it into the new line.
6580 */
6581 ins_bytes(saved_text);
6582 vim_free(saved_text);
6583 }
6584 else
6585#endif
6586 {
6587 /*
6588 * Check if cursor is not past the NUL off the line, cindent
6589 * may have added or removed indent.
6590 */
6591 curwin->w_cursor.col += startcol;
6592 len = (colnr_T)STRLEN(ml_get_curline());
6593 if (curwin->w_cursor.col > len)
6594 curwin->w_cursor.col = len;
6595 }
6596
6597 haveto_redraw = TRUE;
6598#ifdef FEAT_CINDENT
6599 can_cindent = TRUE;
6600#endif
6601 /* moved the cursor, don't autoindent or cindent now */
6602 did_ai = FALSE;
6603#ifdef FEAT_SMARTINDENT
6604 did_si = FALSE;
6605 can_si = FALSE;
6606 can_si_back = FALSE;
6607#endif
6608 line_breakcheck();
6609 }
6610
6611 if (save_char != NUL) /* put back space after cursor */
6612 pchar_cursor(save_char);
6613
Bram Moolenaar0026d472014-09-09 16:32:39 +02006614#ifdef FEAT_LINEBREAK
6615 curwin->w_p_lbr = has_lbr;
6616#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006617 if (!format_only && haveto_redraw)
6618 {
6619 update_topline();
6620 redraw_curbuf_later(VALID);
6621 }
6622}
6623
6624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 * Called after inserting or deleting text: When 'formatoptions' includes the
6626 * 'a' flag format from the current line until the end of the paragraph.
6627 * Keep the cursor at the same position relative to the text.
6628 * The caller must have saved the cursor line for undo, following ones will be
6629 * saved here.
6630 */
6631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632auto_format(
6633 int trailblank, /* when TRUE also format with trailing blank */
6634 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
6636 pos_T pos;
6637 colnr_T len;
6638 char_u *old;
6639 char_u *new, *pnew;
6640 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006641 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642
6643 if (!has_format_option(FO_AUTO))
6644 return;
6645
6646 pos = curwin->w_cursor;
6647 old = ml_get_curline();
6648
6649 /* may remove added space */
6650 check_auto_format(FALSE);
6651
6652 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6653 * user might insert normal text next. Also skip formatting when "1" is
6654 * in 'formatoptions' and there is a single character before the cursor.
6655 * Otherwise the line would be broken and when typing another non-white
6656 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006657 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 if (*old != NUL && !trailblank && wasatend)
6659 {
6660 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006661 cc = gchar_cursor();
6662 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6663 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006665 cc = gchar_cursor();
6666 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {
6668 curwin->w_cursor = pos;
6669 return;
6670 }
6671 curwin->w_cursor = pos;
6672 }
6673
6674#ifdef FEAT_COMMENTS
6675 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6676 * comments. */
6677 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006678 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 return;
6680#endif
6681
6682 /*
6683 * May start formatting in a previous line, so that after "x" a word is
6684 * moved to the previous line if it fits there now. Only when this is not
6685 * the start of a paragraph.
6686 */
6687 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6688 {
6689 --curwin->w_cursor.lnum;
6690 if (u_save_cursor() == FAIL)
6691 return;
6692 }
6693
6694 /*
6695 * Do the formatting and restore the cursor position. "saved_cursor" will
6696 * be adjusted for the text formatting.
6697 */
6698 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006699 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 curwin->w_cursor = saved_cursor;
6701 saved_cursor.lnum = 0;
6702
6703 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6704 {
6705 /* "cannot happen" */
6706 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6707 coladvance((colnr_T)MAXCOL);
6708 }
6709 else
6710 check_cursor_col();
6711
6712 /* Insert mode: If the cursor is now after the end of the line while it
6713 * previously wasn't, the line was broken. Because of the rule above we
6714 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6715 * formatted. */
6716 if (!wasatend && has_format_option(FO_WHITE_PAR))
6717 {
6718 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006719 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 if (curwin->w_cursor.col == len)
6721 {
6722 pnew = vim_strnsave(new, len + 2);
6723 pnew[len] = ' ';
6724 pnew[len + 1] = NUL;
6725 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6726 /* remove the space later */
6727 did_add_space = TRUE;
6728 }
6729 else
6730 /* may remove added space */
6731 check_auto_format(FALSE);
6732 }
6733
6734 check_cursor();
6735}
6736
6737/*
6738 * When an extra space was added to continue a paragraph for auto-formatting,
6739 * delete it now. The space must be under the cursor, just after the insert
6740 * position.
6741 */
6742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006743check_auto_format(
6744 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745{
6746 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006747 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
6749 if (did_add_space)
6750 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006751 cc = gchar_cursor();
6752 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 /* Somehow the space was removed already. */
6754 did_add_space = FALSE;
6755 else
6756 {
6757 if (!end_insert)
6758 {
6759 inc_cursor();
6760 c = gchar_cursor();
6761 dec_cursor();
6762 }
6763 if (c != NUL)
6764 {
6765 /* The space is no longer at the end of the line, delete it. */
6766 del_char(FALSE);
6767 did_add_space = FALSE;
6768 }
6769 }
6770 }
6771}
6772
6773/*
6774 * Find out textwidth to be used for formatting:
6775 * if 'textwidth' option is set, use it
6776 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6777 * if invalid value, use 0.
6778 * Set default to window width (maximum 79) for "gq" operator.
6779 */
6780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006781comp_textwidth(
6782 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
6784 int textwidth;
6785
6786 textwidth = curbuf->b_p_tw;
6787 if (textwidth == 0 && curbuf->b_p_wm)
6788 {
6789 /* The width is the window width minus 'wrapmargin' minus all the
6790 * things that add to the margin. */
6791 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6792#ifdef FEAT_CMDWIN
6793 if (cmdwin_type != 0)
6794 textwidth -= 1;
6795#endif
6796#ifdef FEAT_FOLDING
6797 textwidth -= curwin->w_p_fdc;
6798#endif
6799#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006800 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 textwidth -= 1;
6802#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006803 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 textwidth -= 8;
6805 }
6806 if (textwidth < 0)
6807 textwidth = 0;
6808 if (ff && textwidth == 0)
6809 {
6810 textwidth = W_WIDTH(curwin) - 1;
6811 if (textwidth > 79)
6812 textwidth = 79;
6813 }
6814 return textwidth;
6815}
6816
6817/*
6818 * Put a character in the redo buffer, for when just after a CTRL-V.
6819 */
6820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006821redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
6823 char_u buf[10];
6824
6825 /* Only digits need special treatment. Translate them into a string of
6826 * three digits. */
6827 if (VIM_ISDIGIT(c))
6828 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006829 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 AppendToRedobuff(buf);
6831 }
6832 else
6833 AppendCharToRedobuff(c);
6834}
6835
6836/*
6837 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006838 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 */
6840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006841start_arrow(
6842 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006844 start_arrow_common(end_insert_pos, TRUE);
6845}
6846
6847/*
6848 * Like start_arrow() but with end_change argument.
6849 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6850 */
6851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006852start_arrow_with_change(
6853 pos_T *end_insert_pos, /* can be NULL */
6854 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006855{
6856 start_arrow_common(end_insert_pos, end_change);
6857 if (!end_change)
6858 {
6859 AppendCharToRedobuff(Ctrl_G);
6860 AppendCharToRedobuff('U');
6861 }
6862}
6863
6864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006865start_arrow_common(
6866 pos_T *end_insert_pos, /* can be NULL */
6867 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006868{
6869 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 {
6871 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006872 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 arrow_used = TRUE; /* this means we stopped the current insert */
6874 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006875#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006876 check_spell_redraw();
6877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878}
6879
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006880#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006881/*
6882 * If we skipped highlighting word at cursor, do it now.
6883 * It may be skipped again, thus reset spell_redraw_lnum first.
6884 */
6885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006886check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006887{
6888 if (spell_redraw_lnum != 0)
6889 {
6890 linenr_T lnum = spell_redraw_lnum;
6891
6892 spell_redraw_lnum = 0;
6893 redrawWinline(lnum, FALSE);
6894 }
6895}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006896
6897/*
6898 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6899 * spelled word, if there is one.
6900 */
6901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006902spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006903{
6904 pos_T tpos = curwin->w_cursor;
6905
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006906 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006907 if (curwin->w_cursor.col != tpos.col)
6908 start_arrow(&tpos);
6909}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006910#endif
6911
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912/*
6913 * stop_arrow() is called before a change is made in insert mode.
6914 * If an arrow key has been used, start a new insertion.
6915 * Returns FAIL if undo is impossible, shouldn't insert then.
6916 */
6917 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006918stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919{
6920 if (arrow_used)
6921 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006922 Insstart = curwin->w_cursor; /* new insertion starts here */
6923 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6924 /* Don't update the original insert position when moved to the
6925 * right, except when nothing was inserted yet. */
6926 update_Insstart_orig = FALSE;
6927 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6928
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 if (u_save_cursor() == OK)
6930 {
6931 arrow_used = FALSE;
6932 ins_need_undo = FALSE;
6933 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006934
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 ai_col = 0;
6936#ifdef FEAT_VREPLACE
6937 if (State & VREPLACE_FLAG)
6938 {
6939 orig_line_count = curbuf->b_ml.ml_line_count;
6940 vr_lines_changed = 1;
6941 }
6942#endif
6943 ResetRedobuff();
6944 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006945 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 }
6947 else if (ins_need_undo)
6948 {
6949 if (u_save_cursor() == OK)
6950 ins_need_undo = FALSE;
6951 }
6952
6953#ifdef FEAT_FOLDING
6954 /* Always open fold at the cursor line when inserting something. */
6955 foldOpenCursor();
6956#endif
6957
6958 return (arrow_used || ins_need_undo ? FAIL : OK);
6959}
6960
6961/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006962 * Do a few things to stop inserting.
6963 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6964 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 */
6966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006967stop_insert(
6968 pos_T *end_insert_pos,
6969 int esc, /* called by ins_esc() */
6970 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006972 int cc;
6973 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
6975 stop_redo_ins();
6976 replace_flush(); /* abandon replace stack */
6977
6978 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006979 * Save the inserted text for later redo with ^@ and CTRL-A.
6980 * Don't do it when "restart_edit" was set and nothing was inserted,
6981 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006983 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006984 if (did_restart_edit == 0 || (ptr != NULL
6985 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006986 {
6987 vim_free(last_insert);
6988 last_insert = ptr;
6989 last_insert_skip = new_insert_skip;
6990 }
6991 else
6992 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006994 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {
6996 /* Auto-format now. It may seem strange to do this when stopping an
6997 * insertion (or moving the cursor), but it's required when appending
6998 * a line and having it end in a space. But only do it when something
6999 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007000 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007002 pos_T tpos = curwin->w_cursor;
7003
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 /* When the cursor is at the end of the line after a space the
7005 * formatting will move it to the following word. Avoid that by
7006 * moving the cursor onto the space. */
7007 cc = 'x';
7008 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7009 {
7010 dec_cursor();
7011 cc = gchar_cursor();
7012 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007013 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 }
7015
7016 auto_format(TRUE, FALSE);
7017
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007018 if (vim_iswhite(cc))
7019 {
7020 if (gchar_cursor() != NUL)
7021 inc_cursor();
7022#ifdef FEAT_VIRTUALEDIT
7023 /* If the cursor is still at the same character, also keep
7024 * the "coladd". */
7025 if (gchar_cursor() == NUL
7026 && curwin->w_cursor.lnum == tpos.lnum
7027 && curwin->w_cursor.col == tpos.col)
7028 curwin->w_cursor.coladd = tpos.coladd;
7029#endif
7030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 }
7032
7033 /* If a space was inserted for auto-formatting, remove it now. */
7034 check_auto_format(TRUE);
7035
7036 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007037 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007038 * Do this when ESC was used or moving the cursor up/down.
7039 * Check for the old position still being valid, just in case the text
7040 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007041 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007042 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7043 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007045 pos_T tpos = curwin->w_cursor;
7046
7047 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007048 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007049 for (;;)
7050 {
7051 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7052 --curwin->w_cursor.col;
7053 cc = gchar_cursor();
7054 if (!vim_iswhite(cc))
7055 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007056 if (del_char(TRUE) == FAIL)
7057 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007058 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007059 if (curwin->w_cursor.lnum != tpos.lnum)
7060 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007061 else
7062 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007063 /* reset tpos, could have been invalidated in the loop above */
7064 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007065 tpos.col++;
7066 if (cc != NUL && gchar_pos(&tpos) == NUL)
7067 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 /* <C-S-Right> may have started Visual mode, adjust the position for
7071 * deleted characters. */
7072 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7073 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007074 int len = (int)STRLEN(ml_get_curline());
7075
7076 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007078 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007079#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 }
7083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
7085 }
7086 did_ai = FALSE;
7087#ifdef FEAT_SMARTINDENT
7088 did_si = FALSE;
7089 can_si = FALSE;
7090 can_si_back = FALSE;
7091#endif
7092
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007093 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7094 * now in a different buffer. */
7095 if (end_insert_pos != NULL)
7096 {
7097 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007098 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007099 curbuf->b_op_end = *end_insert_pos;
7100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101}
7102
7103/*
7104 * Set the last inserted text to a single character.
7105 * Used for the replace command.
7106 */
7107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007108set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
7110 char_u *s;
7111
7112 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (last_insert != NULL)
7115 {
7116 s = last_insert;
7117 /* Use the CTRL-V only when entering a special char */
7118 if (c < ' ' || c == DEL)
7119 *s++ = Ctrl_V;
7120 s = add_char2buf(c, s);
7121 *s++ = ESC;
7122 *s++ = NUL;
7123 last_insert_skip = 0;
7124 }
7125}
7126
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007127#if defined(EXITFREE) || defined(PROTO)
7128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007129free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007130{
7131 vim_free(last_insert);
7132 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007133# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007134 vim_free(compl_orig_text);
7135 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007136# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007137}
7138#endif
7139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140/*
7141 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7142 * and CSI. Handle multi-byte characters.
7143 * Returns a pointer to after the added bytes.
7144 */
7145 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007146add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
7148#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007149 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 int i;
7151 int len;
7152
7153 len = (*mb_char2bytes)(c, temp);
7154 for (i = 0; i < len; ++i)
7155 {
7156 c = temp[i];
7157#endif
7158 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7159 if (c == K_SPECIAL)
7160 {
7161 *s++ = K_SPECIAL;
7162 *s++ = KS_SPECIAL;
7163 *s++ = KE_FILLER;
7164 }
7165#ifdef FEAT_GUI
7166 else if (c == CSI)
7167 {
7168 *s++ = CSI;
7169 *s++ = KS_EXTRA;
7170 *s++ = (int)KE_CSI;
7171 }
7172#endif
7173 else
7174 *s++ = c;
7175#ifdef FEAT_MBYTE
7176 }
7177#endif
7178 return s;
7179}
7180
7181/*
7182 * move cursor to start of line
7183 * if flags & BL_WHITE move to first non-white
7184 * if flags & BL_SOL move to first non-white if startofline is set,
7185 * otherwise keep "curswant" column
7186 * if flags & BL_FIX don't leave the cursor on a NUL.
7187 */
7188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007189beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190{
7191 if ((flags & BL_SOL) && !p_sol)
7192 coladvance(curwin->w_curswant);
7193 else
7194 {
7195 curwin->w_cursor.col = 0;
7196#ifdef FEAT_VIRTUALEDIT
7197 curwin->w_cursor.coladd = 0;
7198#endif
7199
7200 if (flags & (BL_WHITE | BL_SOL))
7201 {
7202 char_u *ptr;
7203
7204 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7205 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7206 ++curwin->w_cursor.col;
7207 }
7208 curwin->w_set_curswant = TRUE;
7209 }
7210}
7211
7212/*
7213 * oneright oneleft cursor_down cursor_up
7214 *
7215 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007216 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 * Return OK when successful, FAIL when we hit a line of file boundary.
7218 */
7219
7220 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007221oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
7226#ifdef FEAT_VIRTUALEDIT
7227 if (virtual_active())
7228 {
7229 pos_T prevpos = curwin->w_cursor;
7230
7231 /* Adjust for multi-wide char (excluding TAB) */
7232 ptr = ml_get_cursor();
7233 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007234# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007236# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 ))
7240 ? ptr2cells(ptr) : 1));
7241 curwin->w_set_curswant = TRUE;
7242 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7243 return (prevpos.col != curwin->w_cursor.col
7244 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7245 }
7246#endif
7247
7248 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007249 if (*ptr == NUL)
7250 return FAIL; /* already at the very end */
7251
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007253 if (has_mbyte)
7254 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 else
7256#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007257 l = 1;
7258
7259 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7260 * contains "onemore". */
7261 if (ptr[l] == NUL
7262#ifdef FEAT_VIRTUALEDIT
7263 && (ve_flags & VE_ONEMORE) == 0
7264#endif
7265 )
7266 return FAIL;
7267 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
7269 curwin->w_set_curswant = TRUE;
7270 return OK;
7271}
7272
7273 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007274oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
7276#ifdef FEAT_VIRTUALEDIT
7277 if (virtual_active())
7278 {
7279 int width;
7280 int v = getviscol();
7281
7282 if (v == 0)
7283 return FAIL;
7284
7285# ifdef FEAT_LINEBREAK
7286 /* We might get stuck on 'showbreak', skip over it. */
7287 width = 1;
7288 for (;;)
7289 {
7290 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007291 /* getviscol() is slow, skip it when 'showbreak' is empty,
7292 * 'breakindent' is not set and there are no multi-byte
7293 * characters */
7294 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295# ifdef FEAT_MBYTE
7296 && !has_mbyte
7297# endif
7298 ) || getviscol() < v)
7299 break;
7300 ++width;
7301 }
7302# else
7303 coladvance(v - 1);
7304# endif
7305
7306 if (curwin->w_cursor.coladd == 1)
7307 {
7308 char_u *ptr;
7309
7310 /* Adjust for multi-wide char (not a TAB) */
7311 ptr = ml_get_cursor();
7312 if (*ptr != TAB && vim_isprintc(
7313# ifdef FEAT_MBYTE
7314 (*mb_ptr2char)(ptr)
7315# else
7316 *ptr
7317# endif
7318 ) && ptr2cells(ptr) > 1)
7319 curwin->w_cursor.coladd = 0;
7320 }
7321
7322 curwin->w_set_curswant = TRUE;
7323 return OK;
7324 }
7325#endif
7326
7327 if (curwin->w_cursor.col == 0)
7328 return FAIL;
7329
7330 curwin->w_set_curswant = TRUE;
7331 --curwin->w_cursor.col;
7332
7333#ifdef FEAT_MBYTE
7334 /* if the character on the left of the current cursor is a multi-byte
7335 * character, move to its first byte */
7336 if (has_mbyte)
7337 mb_adjust_cursor();
7338#endif
7339 return OK;
7340}
7341
7342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007343cursor_up(
7344 long n,
7345 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346{
7347 linenr_T lnum;
7348
7349 if (n > 0)
7350 {
7351 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007352 /* This fails if the cursor is already in the first line or the count
7353 * is larger than the line number and '-' is in 'cpoptions' */
7354 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 return FAIL;
7356 if (n >= lnum)
7357 lnum = 1;
7358 else
7359#ifdef FEAT_FOLDING
7360 if (hasAnyFolding(curwin))
7361 {
7362 /*
7363 * Count each sequence of folded lines as one logical line.
7364 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007365 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 (void)hasFolding(lnum, &lnum, NULL);
7367
7368 while (n--)
7369 {
7370 /* move up one line */
7371 --lnum;
7372 if (lnum <= 1)
7373 break;
7374 /* If we entered a fold, move to the beginning, unless in
7375 * Insert mode or when 'foldopen' contains "all": it will open
7376 * in a moment. */
7377 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7378 (void)hasFolding(lnum, &lnum, NULL);
7379 }
7380 if (lnum < 1)
7381 lnum = 1;
7382 }
7383 else
7384#endif
7385 lnum -= n;
7386 curwin->w_cursor.lnum = lnum;
7387 }
7388
7389 /* try to advance to the column we want to be at */
7390 coladvance(curwin->w_curswant);
7391
7392 if (upd_topline)
7393 update_topline(); /* make sure curwin->w_topline is valid */
7394
7395 return OK;
7396}
7397
7398/*
7399 * Cursor down a number of logical lines.
7400 */
7401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007402cursor_down(
7403 long n,
7404 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405{
7406 linenr_T lnum;
7407
7408 if (n > 0)
7409 {
7410 lnum = curwin->w_cursor.lnum;
7411#ifdef FEAT_FOLDING
7412 /* Move to last line of fold, will fail if it's the end-of-file. */
7413 (void)hasFolding(lnum, NULL, &lnum);
7414#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007415 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007416 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007417 if (lnum >= curbuf->b_ml.ml_line_count
7418 || (lnum + n > curbuf->b_ml.ml_line_count
7419 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 return FAIL;
7421 if (lnum + n >= curbuf->b_ml.ml_line_count)
7422 lnum = curbuf->b_ml.ml_line_count;
7423 else
7424#ifdef FEAT_FOLDING
7425 if (hasAnyFolding(curwin))
7426 {
7427 linenr_T last;
7428
7429 /* count each sequence of folded lines as one logical line */
7430 while (n--)
7431 {
7432 if (hasFolding(lnum, NULL, &last))
7433 lnum = last + 1;
7434 else
7435 ++lnum;
7436 if (lnum >= curbuf->b_ml.ml_line_count)
7437 break;
7438 }
7439 if (lnum > curbuf->b_ml.ml_line_count)
7440 lnum = curbuf->b_ml.ml_line_count;
7441 }
7442 else
7443#endif
7444 lnum += n;
7445 curwin->w_cursor.lnum = lnum;
7446 }
7447
7448 /* try to advance to the column we want to be at */
7449 coladvance(curwin->w_curswant);
7450
7451 if (upd_topline)
7452 update_topline(); /* make sure curwin->w_topline is valid */
7453
7454 return OK;
7455}
7456
7457/*
7458 * Stuff the last inserted text in the read buffer.
7459 * Last_insert actually is a copy of the redo buffer, so we
7460 * first have to remove the command.
7461 */
7462 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007463stuff_inserted(
7464 int c, /* Command character to be inserted */
7465 long count, /* Repeat this many times */
7466 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467{
7468 char_u *esc_ptr;
7469 char_u *ptr;
7470 char_u *last_ptr;
7471 char_u last = NUL;
7472
7473 ptr = get_last_insert();
7474 if (ptr == NULL)
7475 {
7476 EMSG(_(e_noinstext));
7477 return FAIL;
7478 }
7479
7480 /* may want to stuff the command character, to start Insert mode */
7481 if (c != NUL)
7482 stuffcharReadbuff(c);
7483 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7484 *esc_ptr = NUL; /* remove the ESC */
7485
7486 /* when the last char is either "0" or "^" it will be quoted if no ESC
7487 * comes after it OR if it will inserted more than once and "ptr"
7488 * starts with ^D. -- Acevedo
7489 */
7490 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7491 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7492 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7493 {
7494 last = *last_ptr;
7495 *last_ptr = NUL;
7496 }
7497
7498 do
7499 {
7500 stuffReadbuff(ptr);
7501 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7502 if (last)
7503 stuffReadbuff((char_u *)(last == '0'
7504 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7505 : IF_EB("\026^", CTRL_V_STR "^")));
7506 }
7507 while (--count > 0);
7508
7509 if (last)
7510 *last_ptr = last;
7511
7512 if (esc_ptr != NULL)
7513 *esc_ptr = ESC; /* put the ESC back */
7514
7515 /* may want to stuff a trailing ESC, to get out of Insert mode */
7516 if (!no_esc)
7517 stuffcharReadbuff(ESC);
7518
7519 return OK;
7520}
7521
7522 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007523get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524{
7525 if (last_insert == NULL)
7526 return NULL;
7527 return last_insert + last_insert_skip;
7528}
7529
7530/*
7531 * Get last inserted string, and remove trailing <Esc>.
7532 * Returns pointer to allocated memory (must be freed) or NULL.
7533 */
7534 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007535get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536{
7537 char_u *s;
7538 int len;
7539
7540 if (last_insert == NULL)
7541 return NULL;
7542 s = vim_strsave(last_insert + last_insert_skip);
7543 if (s != NULL)
7544 {
7545 len = (int)STRLEN(s);
7546 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7547 s[len - 1] = NUL;
7548 }
7549 return s;
7550}
7551
7552/*
7553 * Check the word in front of the cursor for an abbreviation.
7554 * Called when the non-id character "c" has been entered.
7555 * When an abbreviation is recognized it is removed from the text and
7556 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7557 */
7558 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007559echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560{
7561 /* Don't check for abbreviation in paste mode, when disabled and just
7562 * after moving around with cursor keys. */
7563 if (p_paste || no_abbr || arrow_used)
7564 return FALSE;
7565
7566 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7567 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7568}
7569
7570/*
7571 * replace-stack functions
7572 *
7573 * When replacing characters, the replaced characters are remembered for each
7574 * new character. This is used to re-insert the old text when backspacing.
7575 *
7576 * There is a NUL headed list of characters for each character that is
7577 * currently in the file after the insertion point. When BS is used, one NUL
7578 * headed list is put back for the deleted character.
7579 *
7580 * For a newline, there are two NUL headed lists. One contains the characters
7581 * that the NL replaced. The extra one stores the characters after the cursor
7582 * that were deleted (always white space).
7583 *
7584 * Replace_offset is normally 0, in which case replace_push will add a new
7585 * character at the end of the stack. If replace_offset is not 0, that many
7586 * characters will be left on the stack above the newly inserted character.
7587 */
7588
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007589static char_u *replace_stack = NULL;
7590static long replace_stack_nr = 0; /* next entry in replace stack */
7591static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592
7593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007594replace_push(
7595 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 char_u *p;
7598
7599 if (replace_stack_nr < replace_offset) /* nothing to do */
7600 return;
7601 if (replace_stack_len <= replace_stack_nr)
7602 {
7603 replace_stack_len += 50;
7604 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7605 if (p == NULL) /* out of memory */
7606 {
7607 replace_stack_len -= 50;
7608 return;
7609 }
7610 if (replace_stack != NULL)
7611 {
7612 mch_memmove(p, replace_stack,
7613 (size_t)(replace_stack_nr * sizeof(char_u)));
7614 vim_free(replace_stack);
7615 }
7616 replace_stack = p;
7617 }
7618 p = replace_stack + replace_stack_nr - replace_offset;
7619 if (replace_offset)
7620 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7621 *p = c;
7622 ++replace_stack_nr;
7623}
7624
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007625#if defined(FEAT_MBYTE) || defined(PROTO)
7626/*
7627 * Push a character onto the replace stack. Handles a multi-byte character in
7628 * reverse byte order, so that the first byte is popped off first.
7629 * Return the number of bytes done (includes composing characters).
7630 */
7631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007632replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007633{
7634 int l = (*mb_ptr2len)(p);
7635 int j;
7636
7637 for (j = l - 1; j >= 0; --j)
7638 replace_push(p[j]);
7639 return l;
7640}
7641#endif
7642
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643/*
7644 * Pop one item from the replace stack.
7645 * return -1 if stack empty
7646 * return replaced character or NUL otherwise
7647 */
7648 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007649replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650{
7651 if (replace_stack_nr == 0)
7652 return -1;
7653 return (int)replace_stack[--replace_stack_nr];
7654}
7655
7656/*
7657 * Join the top two items on the replace stack. This removes to "off"'th NUL
7658 * encountered.
7659 */
7660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007661replace_join(
7662 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
7664 int i;
7665
7666 for (i = replace_stack_nr; --i >= 0; )
7667 if (replace_stack[i] == NUL && off-- <= 0)
7668 {
7669 --replace_stack_nr;
7670 mch_memmove(replace_stack + i, replace_stack + i + 1,
7671 (size_t)(replace_stack_nr - i));
7672 return;
7673 }
7674}
7675
7676/*
7677 * Pop bytes from the replace stack until a NUL is found, and insert them
7678 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7679 */
7680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007681replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
7683 int cc;
7684 int oldState = State;
7685
7686 State = NORMAL; /* don't want REPLACE here */
7687 while ((cc = replace_pop()) > 0)
7688 {
7689#ifdef FEAT_MBYTE
7690 mb_replace_pop_ins(cc);
7691#else
7692 ins_char(cc);
7693#endif
7694 dec_cursor();
7695 }
7696 State = oldState;
7697}
7698
7699#ifdef FEAT_MBYTE
7700/*
7701 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7702 * indicates a multi-byte char, pop the other bytes too.
7703 */
7704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007705mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
7707 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007708 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 int i;
7710 int c;
7711
7712 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7713 {
7714 buf[0] = cc;
7715 for (i = 1; i < n; ++i)
7716 buf[i] = replace_pop();
7717 ins_bytes_len(buf, n);
7718 }
7719 else
7720 ins_char(cc);
7721
7722 if (enc_utf8)
7723 /* Handle composing chars. */
7724 for (;;)
7725 {
7726 c = replace_pop();
7727 if (c == -1) /* stack empty */
7728 break;
7729 if ((n = MB_BYTE2LEN(c)) == 1)
7730 {
7731 /* Not a multi-byte char, put it back. */
7732 replace_push(c);
7733 break;
7734 }
7735 else
7736 {
7737 buf[0] = c;
7738 for (i = 1; i < n; ++i)
7739 buf[i] = replace_pop();
7740 if (utf_iscomposing(utf_ptr2char(buf)))
7741 ins_bytes_len(buf, n);
7742 else
7743 {
7744 /* Not a composing char, put it back. */
7745 for (i = n - 1; i >= 0; --i)
7746 replace_push(buf[i]);
7747 break;
7748 }
7749 }
7750 }
7751}
7752#endif
7753
7754/*
7755 * make the replace stack empty
7756 * (called when exiting replace mode)
7757 */
7758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007759replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
7761 vim_free(replace_stack);
7762 replace_stack = NULL;
7763 replace_stack_len = 0;
7764 replace_stack_nr = 0;
7765}
7766
7767/*
7768 * Handle doing a BS for one character.
7769 * cc < 0: replace stack empty, just move cursor
7770 * cc == 0: character was inserted, delete it
7771 * cc > 0: character was replaced, put cc (first byte of original char) back
7772 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007773 * When "limit_col" is >= 0, don't delete before this column. Matters when
7774 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 */
7776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007777replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 int cc;
7780#ifdef FEAT_VREPLACE
7781 int orig_len = 0;
7782 int ins_len;
7783 int orig_vcols = 0;
7784 colnr_T start_vcol;
7785 char_u *p;
7786 int i;
7787 int vcol;
7788#endif
7789
7790 cc = replace_pop();
7791 if (cc > 0)
7792 {
7793#ifdef FEAT_VREPLACE
7794 if (State & VREPLACE_FLAG)
7795 {
7796 /* Get the number of screen cells used by the character we are
7797 * going to delete. */
7798 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7799 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7800 }
7801#endif
7802#ifdef FEAT_MBYTE
7803 if (has_mbyte)
7804 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007805 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806# ifdef FEAT_VREPLACE
7807 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007808 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809# endif
7810 replace_push(cc);
7811 }
7812 else
7813#endif
7814 {
7815 pchar_cursor(cc);
7816#ifdef FEAT_VREPLACE
7817 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007818 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819#endif
7820 }
7821 replace_pop_ins();
7822
7823#ifdef FEAT_VREPLACE
7824 if (State & VREPLACE_FLAG)
7825 {
7826 /* Get the number of screen cells used by the inserted characters */
7827 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007828 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 vcol = start_vcol;
7830 for (i = 0; i < ins_len; ++i)
7831 {
7832 vcol += chartabsize(p + i, vcol);
7833#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007834 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835#endif
7836 }
7837 vcol -= start_vcol;
7838
7839 /* Delete spaces that were inserted after the cursor to keep the
7840 * text aligned. */
7841 curwin->w_cursor.col += ins_len;
7842 while (vcol > orig_vcols && gchar_cursor() == ' ')
7843 {
7844 del_char(FALSE);
7845 ++orig_vcols;
7846 }
7847 curwin->w_cursor.col -= ins_len;
7848 }
7849#endif
7850
7851 /* mark the buffer as changed and prepare for displaying */
7852 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7853 }
7854 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007855 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856}
7857
7858#ifdef FEAT_CINDENT
7859/*
7860 * Return TRUE if C-indenting is on.
7861 */
7862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007863cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864{
7865 return (!p_paste && (curbuf->b_p_cin
7866# ifdef FEAT_EVAL
7867 || *curbuf->b_p_inde != NUL
7868# endif
7869 ));
7870}
7871#endif
7872
7873#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7874/*
7875 * Re-indent the current line, based on the current contents of it and the
7876 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7877 * confused what all the part that handles Control-T is doing that I'm not.
7878 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7879 */
7880
7881 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007882fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007884 int amount = get_the_indent();
7885
7886 if (amount >= 0)
7887 {
7888 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7889 if (linewhite(curwin->w_cursor.lnum))
7890 did_ai = TRUE; /* delete the indent if the line stays empty */
7891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892}
7893
7894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007895fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
7897 if (p_paste)
7898 return;
7899# ifdef FEAT_LISP
7900 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7901 fixthisline(get_lisp_indent);
7902# endif
7903# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7904 else
7905# endif
7906# ifdef FEAT_CINDENT
7907 if (cindent_on())
7908 do_c_expr_indent();
7909# endif
7910}
7911
7912#endif
7913
7914#ifdef FEAT_CINDENT
7915/*
7916 * return TRUE if 'cinkeys' contains the key "keytyped",
7917 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007918 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7920 *
7921 * "keytyped" can have a few special values:
7922 * KEY_OPEN_FORW
7923 * KEY_OPEN_BACK
7924 * KEY_COMPLETE just finished completion.
7925 *
7926 * If line_is_empty is TRUE accept keys with '0' before them.
7927 */
7928 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007929in_cinkeys(
7930 int keytyped,
7931 int when,
7932 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934 char_u *look;
7935 int try_match;
7936 int try_match_word;
7937 char_u *p;
7938 char_u *line;
7939 int icase;
7940 int i;
7941
Bram Moolenaar30848942009-12-24 14:46:12 +00007942 if (keytyped == NUL)
7943 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7944 return FALSE;
7945
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946#ifdef FEAT_EVAL
7947 if (*curbuf->b_p_inde != NUL)
7948 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7949 else
7950#endif
7951 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7952 while (*look)
7953 {
7954 /*
7955 * Find out if we want to try a match with this key, depending on
7956 * 'when' and a '*' or '!' before the key.
7957 */
7958 switch (when)
7959 {
7960 case '*': try_match = (*look == '*'); break;
7961 case '!': try_match = (*look == '!'); break;
7962 default: try_match = (*look != '*'); break;
7963 }
7964 if (*look == '*' || *look == '!')
7965 ++look;
7966
7967 /*
7968 * If there is a '0', only accept a match if the line is empty.
7969 * But may still match when typing last char of a word.
7970 */
7971 if (*look == '0')
7972 {
7973 try_match_word = try_match;
7974 if (!line_is_empty)
7975 try_match = FALSE;
7976 ++look;
7977 }
7978 else
7979 try_match_word = FALSE;
7980
7981 /*
7982 * does it look like a control character?
7983 */
7984 if (*look == '^'
7985#ifdef EBCDIC
7986 && (Ctrl_chr(look[1]) != 0)
7987#else
7988 && look[1] >= '?' && look[1] <= '_'
7989#endif
7990 )
7991 {
7992 if (try_match && keytyped == Ctrl_chr(look[1]))
7993 return TRUE;
7994 look += 2;
7995 }
7996 /*
7997 * 'o' means "o" command, open forward.
7998 * 'O' means "O" command, open backward.
7999 */
8000 else if (*look == 'o')
8001 {
8002 if (try_match && keytyped == KEY_OPEN_FORW)
8003 return TRUE;
8004 ++look;
8005 }
8006 else if (*look == 'O')
8007 {
8008 if (try_match && keytyped == KEY_OPEN_BACK)
8009 return TRUE;
8010 ++look;
8011 }
8012
8013 /*
8014 * 'e' means to check for "else" at start of line and just before the
8015 * cursor.
8016 */
8017 else if (*look == 'e')
8018 {
8019 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8020 {
8021 p = ml_get_curline();
8022 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8023 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8024 return TRUE;
8025 }
8026 ++look;
8027 }
8028
8029 /*
8030 * ':' only causes an indent if it is at the end of a label or case
8031 * statement, or when it was before typing the ':' (to fix
8032 * class::method for C++).
8033 */
8034 else if (*look == ':')
8035 {
8036 if (try_match && keytyped == ':')
8037 {
8038 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008039 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008041 /* Need to get the line again after cin_islabel(). */
8042 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 if (curwin->w_cursor.col > 2
8044 && p[curwin->w_cursor.col - 1] == ':'
8045 && p[curwin->w_cursor.col - 2] == ':')
8046 {
8047 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008048 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008049 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 p = ml_get_curline();
8051 p[curwin->w_cursor.col - 1] = ':';
8052 if (i)
8053 return TRUE;
8054 }
8055 }
8056 ++look;
8057 }
8058
8059
8060 /*
8061 * Is it a key in <>, maybe?
8062 */
8063 else if (*look == '<')
8064 {
8065 if (try_match)
8066 {
8067 /*
8068 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8069 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8070 * >, *, : and ! keys if they really really want to.
8071 */
8072 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8073 && keytyped == look[1])
8074 return TRUE;
8075
8076 if (keytyped == get_special_key_code(look + 1))
8077 return TRUE;
8078 }
8079 while (*look && *look != '>')
8080 look++;
8081 while (*look == '>')
8082 look++;
8083 }
8084
8085 /*
8086 * Is it a word: "=word"?
8087 */
8088 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8089 {
8090 ++look;
8091 if (*look == '~')
8092 {
8093 icase = TRUE;
8094 ++look;
8095 }
8096 else
8097 icase = FALSE;
8098 p = vim_strchr(look, ',');
8099 if (p == NULL)
8100 p = look + STRLEN(look);
8101 if ((try_match || try_match_word)
8102 && curwin->w_cursor.col >= (colnr_T)(p - look))
8103 {
8104 int match = FALSE;
8105
8106#ifdef FEAT_INS_EXPAND
8107 if (keytyped == KEY_COMPLETE)
8108 {
8109 char_u *s;
8110
8111 /* Just completed a word, check if it starts with "look".
8112 * search back for the start of a word. */
8113 line = ml_get_curline();
8114# ifdef FEAT_MBYTE
8115 if (has_mbyte)
8116 {
8117 char_u *n;
8118
8119 for (s = line + curwin->w_cursor.col; s > line; s = n)
8120 {
8121 n = mb_prevptr(line, s);
8122 if (!vim_iswordp(n))
8123 break;
8124 }
8125 }
8126 else
8127# endif
8128 for (s = line + curwin->w_cursor.col; s > line; --s)
8129 if (!vim_iswordc(s[-1]))
8130 break;
8131 if (s + (p - look) <= line + curwin->w_cursor.col
8132 && (icase
8133 ? MB_STRNICMP(s, look, p - look)
8134 : STRNCMP(s, look, p - look)) == 0)
8135 match = TRUE;
8136 }
8137 else
8138#endif
8139 /* TODO: multi-byte */
8140 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8141 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8142 {
8143 line = ml_get_cursor();
8144 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8145 || !vim_iswordc(line[-(p - look) - 1]))
8146 && (icase
8147 ? MB_STRNICMP(line - (p - look), look, p - look)
8148 : STRNCMP(line - (p - look), look, p - look))
8149 == 0)
8150 match = TRUE;
8151 }
8152 if (match && try_match_word && !try_match)
8153 {
8154 /* "0=word": Check if there are only blanks before the
8155 * word. */
8156 line = ml_get_curline();
8157 if ((int)(skipwhite(line) - line) !=
8158 (int)(curwin->w_cursor.col - (p - look)))
8159 match = FALSE;
8160 }
8161 if (match)
8162 return TRUE;
8163 }
8164 look = p;
8165 }
8166
8167 /*
8168 * ok, it's a boring generic character.
8169 */
8170 else
8171 {
8172 if (try_match && *look == keytyped)
8173 return TRUE;
8174 ++look;
8175 }
8176
8177 /*
8178 * Skip over ", ".
8179 */
8180 look = skip_to_option_part(look);
8181 }
8182 return FALSE;
8183}
8184#endif /* FEAT_CINDENT */
8185
8186#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8187/*
8188 * Map Hebrew keyboard when in hkmap mode.
8189 */
8190 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008191hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
8193 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8194 {
8195 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8196 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8197 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8198 static char_u map[26] =
8199 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8200 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8201 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8202 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8203 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8204 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8205 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8206 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8207 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8208
8209 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8210 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8211 /* '-1'='sofit' */
8212 else if (c == 'x')
8213 return 'X';
8214 else if (c == 'q')
8215 return '\''; /* {geresh}={'} */
8216 else if (c == 246)
8217 return ' '; /* \"o --> ' ' for a german keyboard */
8218 else if (c == 228)
8219 return ' '; /* \"a --> ' ' -- / -- */
8220 else if (c == 252)
8221 return ' '; /* \"u --> ' ' -- / -- */
8222#ifdef EBCDIC
8223 else if (islower(c))
8224#else
8225 /* NOTE: islower() does not do the right thing for us on Linux so we
8226 * do this the same was as 5.7 and previous, so it works correctly on
8227 * all systems. Specifically, the e.g. Delete and Arrow keys are
8228 * munged and won't work if e.g. searching for Hebrew text.
8229 */
8230 else if (c >= 'a' && c <= 'z')
8231#endif
8232 return (int)(map[CharOrdLow(c)] + p_aleph);
8233 else
8234 return c;
8235 }
8236 else
8237 {
8238 switch (c)
8239 {
8240 case '`': return ';';
8241 case '/': return '.';
8242 case '\'': return ',';
8243 case 'q': return '/';
8244 case 'w': return '\'';
8245
8246 /* Hebrew letters - set offset from 'a' */
8247 case ',': c = '{'; break;
8248 case '.': c = 'v'; break;
8249 case ';': c = 't'; break;
8250 default: {
8251 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8252
8253#ifdef EBCDIC
8254 /* see note about islower() above */
8255 if (!islower(c))
8256#else
8257 if (c < 'a' || c > 'z')
8258#endif
8259 return c;
8260 c = str[CharOrdLow(c)];
8261 break;
8262 }
8263 }
8264
8265 return (int)(CharOrdLow(c) + p_aleph);
8266 }
8267}
8268#endif
8269
8270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008271ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272{
8273 int need_redraw = FALSE;
8274 int regname;
8275 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008276 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277
8278 /*
8279 * If we are going to wait for a character, show a '"'.
8280 */
8281 pc_status = PC_STATUS_UNSET;
8282 if (redrawing() && !char_avail())
8283 {
8284 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008285 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286
8287 edit_putchar('"', TRUE);
8288#ifdef FEAT_CMDL_INFO
8289 add_to_showcmd_c(Ctrl_R);
8290#endif
8291 }
8292
8293#ifdef USE_ON_FLY_SCROLL
8294 dont_scroll = TRUE; /* disallow scrolling here */
8295#endif
8296
8297 /*
8298 * Don't map the register name. This also prevents the mode message to be
8299 * deleted when ESC is hit.
8300 */
8301 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008302 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8305 {
8306 /* Get a third key for literal register insertion */
8307 literally = regname;
8308#ifdef FEAT_CMDL_INFO
8309 add_to_showcmd_c(literally);
8310#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008311 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 }
8314 --no_mapping;
8315
8316#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008317 /* Don't call u_sync() while typing the expression or giving an error
8318 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 ++no_u_sync;
8320 if (regname == '=')
8321 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008322# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008324# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008325 /* Sync undo when evaluating the expression calls setline() or
8326 * append(), so that it can be undone separately. */
8327 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008328
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008330# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 /* Restore the Input Method. */
8332 if (im_on)
8333 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008334# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008336 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8337 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008338 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 else
8342 {
8343#endif
8344 if (literally == Ctrl_O || literally == Ctrl_P)
8345 {
8346 /* Append the command to the redo buffer. */
8347 AppendCharToRedobuff(Ctrl_R);
8348 AppendCharToRedobuff(literally);
8349 AppendCharToRedobuff(regname);
8350
8351 do_put(regname, BACKWARD, 1L,
8352 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8353 }
8354 else if (insert_reg(regname, literally) == FAIL)
8355 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008356 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 need_redraw = TRUE; /* remove the '"' */
8358 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008359 else if (stop_insert_mode)
8360 /* When the '=' register was used and a function was invoked that
8361 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8362 * insert anything, need to remove the '"' */
8363 need_redraw = TRUE;
8364
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365#ifdef FEAT_EVAL
8366 }
8367 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008368 if (u_sync_once == 1)
8369 ins_need_undo = TRUE;
8370 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371#endif
8372#ifdef FEAT_CMDL_INFO
8373 clear_showcmd();
8374#endif
8375
8376 /* If the inserted register is empty, we need to remove the '"' */
8377 if (need_redraw || stuff_empty())
8378 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008379
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008380 /* Disallow starting Visual mode here, would get a weird mode. */
8381 if (!vis_active && VIsual_active)
8382 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383}
8384
8385/*
8386 * CTRL-G commands in Insert mode.
8387 */
8388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008389ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390{
8391 int c;
8392
8393#ifdef FEAT_INS_EXPAND
8394 /* Right after CTRL-X the cursor will be after the ruler. */
8395 setcursor();
8396#endif
8397
8398 /*
8399 * Don't map the second key. This also prevents the mode message to be
8400 * deleted when ESC is hit.
8401 */
8402 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008403 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 --no_mapping;
8405 switch (c)
8406 {
8407 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8408 case K_UP:
8409 case Ctrl_K:
8410 case 'k': ins_up(TRUE);
8411 break;
8412
8413 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8414 case K_DOWN:
8415 case Ctrl_J:
8416 case 'j': ins_down(TRUE);
8417 break;
8418
8419 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008420 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008422
8423 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008424 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008425 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008426 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 break;
8428
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008429 /* CTRL-G U: do not break undo with the next char */
8430 case 'U':
8431 /* Allow one left/right cursor movement with the next char,
8432 * without breaking undo. */
8433 dont_sync_undo = MAYBE;
8434 break;
8435
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008437 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 }
8439}
8440
8441/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008442 * CTRL-^ in Insert mode.
8443 */
8444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008445ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008446{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008447 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008448 {
8449 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8450 if (State & LANGMAP)
8451 {
8452 curbuf->b_p_iminsert = B_IMODE_NONE;
8453 State &= ~LANGMAP;
8454 }
8455 else
8456 {
8457 curbuf->b_p_iminsert = B_IMODE_LMAP;
8458 State |= LANGMAP;
8459#ifdef USE_IM_CONTROL
8460 im_set_active(FALSE);
8461#endif
8462 }
8463 }
8464#ifdef USE_IM_CONTROL
8465 else
8466 {
8467 /* There are no ":lmap" mappings, toggle IM */
8468 if (im_get_status())
8469 {
8470 curbuf->b_p_iminsert = B_IMODE_NONE;
8471 im_set_active(FALSE);
8472 }
8473 else
8474 {
8475 curbuf->b_p_iminsert = B_IMODE_IM;
8476 State &= ~LANGMAP;
8477 im_set_active(TRUE);
8478 }
8479 }
8480#endif
8481 set_iminsert_global();
8482 showmode();
8483#ifdef FEAT_GUI
8484 /* may show different cursor shape or color */
8485 if (gui.in_use)
8486 gui_update_cursor(TRUE, FALSE);
8487#endif
8488#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8489 /* Show/unshow value of 'keymap' in status lines. */
8490 status_redraw_curbuf();
8491#endif
8492}
8493
8494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 * Handle ESC in insert mode.
8496 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8497 * insert.
8498 */
8499 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008500ins_esc(
8501 long *count,
8502 int cmdchar,
8503 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504{
8505 int temp;
8506 static int disabled_redraw = FALSE;
8507
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008508#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008509 check_spell_redraw();
8510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511#if defined(FEAT_HANGULIN)
8512# if defined(ESC_CHG_TO_ENG_MODE)
8513 hangul_input_state_set(0);
8514# endif
8515 if (composing_hangul)
8516 {
8517 push_raw_key(composing_hangul_buffer, 2);
8518 composing_hangul = 0;
8519 }
8520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521
8522 temp = curwin->w_cursor.col;
8523 if (disabled_redraw)
8524 {
8525 --RedrawingDisabled;
8526 disabled_redraw = FALSE;
8527 }
8528 if (!arrow_used)
8529 {
8530 /*
8531 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008532 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8533 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 */
8535 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008536 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537
8538 /*
8539 * Repeating insert may take a long time. Check for
8540 * interrupt now and then.
8541 */
8542 if (*count > 0)
8543 {
8544 line_breakcheck();
8545 if (got_int)
8546 *count = 0;
8547 }
8548
8549 if (--*count > 0) /* repeat what was typed */
8550 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008551 /* Vi repeats the insert without replacing characters. */
8552 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8553 State &= ~REPLACE_FLAG;
8554
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 (void)start_redo_ins();
8556 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008557 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 ++RedrawingDisabled;
8559 disabled_redraw = TRUE;
8560 return FALSE; /* repeat the insert */
8561 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008562 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 undisplay_dollar();
8564 }
8565
8566 /* When an autoindent was removed, curswant stays after the
8567 * indent */
8568 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8569 curwin->w_set_curswant = TRUE;
8570
8571 /* Remember the last Insert position in the '^ mark. */
8572 if (!cmdmod.keepjumps)
8573 curbuf->b_last_insert = curwin->w_cursor;
8574
8575 /*
8576 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008577 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008579 if (!nomove
8580 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581#ifdef FEAT_VIRTUALEDIT
8582 || curwin->w_cursor.coladd > 0
8583#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008584 )
8585 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008586 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587#ifdef FEAT_RIGHTLEFT
8588 && !revins_on
8589#endif
8590 )
8591 {
8592#ifdef FEAT_VIRTUALEDIT
8593 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8594 {
8595 oneleft();
8596 if (restart_edit != NUL)
8597 ++curwin->w_cursor.coladd;
8598 }
8599 else
8600#endif
8601 {
8602 --curwin->w_cursor.col;
8603#ifdef FEAT_MBYTE
8604 /* Correct cursor for multi-byte character. */
8605 if (has_mbyte)
8606 mb_adjust_cursor();
8607#endif
8608 }
8609 }
8610
8611#ifdef USE_IM_CONTROL
8612 /* Disable IM to allow typing English directly for Normal mode commands.
8613 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8614 * well). */
8615 if (!(State & LANGMAP))
8616 im_save_status(&curbuf->b_p_iminsert);
8617 im_set_active(FALSE);
8618#endif
8619
8620 State = NORMAL;
8621 /* need to position cursor again (e.g. when on a TAB ) */
8622 changed_cline_bef_curs();
8623
8624#ifdef FEAT_MOUSE
8625 setmouse();
8626#endif
8627#ifdef CURSOR_SHAPE
8628 ui_cursor_shape(); /* may show different cursor shape */
8629#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008630 if (!p_ek)
8631 /* Re-enable bracketed paste mode. */
8632 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633
8634 /*
8635 * When recording or for CTRL-O, need to display the new mode.
8636 * Otherwise remove the mode message.
8637 */
8638 if (Recording || restart_edit != NUL)
8639 showmode();
8640 else if (p_smd)
8641 MSG("");
8642
8643 return TRUE; /* exit Insert mode */
8644}
8645
8646#ifdef FEAT_RIGHTLEFT
8647/*
8648 * Toggle language: hkmap and revins_on.
8649 * Move to end of reverse inserted text.
8650 */
8651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008652ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653{
8654 if (revins_on && revins_chars && revins_scol >= 0)
8655 {
8656 while (gchar_cursor() != NUL && revins_chars--)
8657 ++curwin->w_cursor.col;
8658 }
8659 p_ri = !p_ri;
8660 revins_on = (State == INSERT && p_ri);
8661 if (revins_on)
8662 {
8663 revins_scol = curwin->w_cursor.col;
8664 revins_legal++;
8665 revins_chars = 0;
8666 undisplay_dollar();
8667 }
8668 else
8669 revins_scol = -1;
8670#ifdef FEAT_FKMAP
8671 if (p_altkeymap)
8672 {
8673 /*
8674 * to be consistent also for redo command, using '.'
8675 * set arrow_used to true and stop it - causing to redo
8676 * characters entered in one mode (normal/reverse insert).
8677 */
8678 arrow_used = TRUE;
8679 (void)stop_arrow();
8680 p_fkmap = curwin->w_p_rl ^ p_ri;
8681 if (p_fkmap && p_ri)
8682 State = INSERT;
8683 }
8684 else
8685#endif
8686 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8687 showmode();
8688}
8689#endif
8690
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691/*
8692 * If 'keymodel' contains "startsel", may start selection.
8693 * Returns TRUE when a CTRL-O and other keys stuffed.
8694 */
8695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008696ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697{
8698 if (km_startsel)
8699 switch (c)
8700 {
8701 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 case K_PAGEUP:
8704 case K_KPAGEUP:
8705 case K_PAGEDOWN:
8706 case K_KPAGEDOWN:
8707# ifdef MACOS
8708 case K_LEFT:
8709 case K_RIGHT:
8710 case K_UP:
8711 case K_DOWN:
8712 case K_END:
8713 case K_HOME:
8714# endif
8715 if (!(mod_mask & MOD_MASK_SHIFT))
8716 break;
8717 /* FALLTHROUGH */
8718 case K_S_LEFT:
8719 case K_S_RIGHT:
8720 case K_S_UP:
8721 case K_S_DOWN:
8722 case K_S_END:
8723 case K_S_HOME:
8724 /* Start selection right away, the cursor can move with
8725 * CTRL-O when beyond the end of the line. */
8726 start_selection();
8727
8728 /* Execute the key in (insert) Select mode. */
8729 stuffcharReadbuff(Ctrl_O);
8730 if (mod_mask)
8731 {
8732 char_u buf[4];
8733
8734 buf[0] = K_SPECIAL;
8735 buf[1] = KS_MODIFIER;
8736 buf[2] = mod_mask;
8737 buf[3] = NUL;
8738 stuffReadbuff(buf);
8739 }
8740 stuffcharReadbuff(c);
8741 return TRUE;
8742 }
8743 return FALSE;
8744}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745
8746/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008747 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008748 */
8749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008750ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008751{
8752#ifdef FEAT_FKMAP
8753 if (p_fkmap && p_ri)
8754 {
8755 beep_flush();
8756 EMSG(farsi_text_3); /* encoded in Farsi */
8757 return;
8758 }
8759#endif
8760
8761#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008762# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008763 set_vim_var_string(VV_INSERTMODE,
8764 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008765# ifdef FEAT_VREPLACE
8766 replaceState == VREPLACE ? "v" :
8767# endif
8768 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008769# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008770 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8771#endif
8772 if (State & REPLACE_FLAG)
8773 State = INSERT | (State & LANGMAP);
8774 else
8775 State = replaceState | (State & LANGMAP);
8776 AppendCharToRedobuff(K_INS);
8777 showmode();
8778#ifdef CURSOR_SHAPE
8779 ui_cursor_shape(); /* may show different cursor shape */
8780#endif
8781}
8782
8783/*
8784 * Pressed CTRL-O in Insert mode.
8785 */
8786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008787ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008788{
8789#ifdef FEAT_VREPLACE
8790 if (State & VREPLACE_FLAG)
8791 restart_edit = 'V';
8792 else
8793#endif
8794 if (State & REPLACE_FLAG)
8795 restart_edit = 'R';
8796 else
8797 restart_edit = 'I';
8798#ifdef FEAT_VIRTUALEDIT
8799 if (virtual_active())
8800 ins_at_eol = FALSE; /* cursor always keeps its column */
8801 else
8802#endif
8803 ins_at_eol = (gchar_cursor() == NUL);
8804}
8805
8806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 * If the cursor is on an indent, ^T/^D insert/delete one
8808 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008809 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 * with vi. But vi only supports ^T and ^D after an
8811 * autoindent, we support it everywhere.
8812 */
8813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008814ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
8816 if (stop_arrow() == FAIL)
8817 return;
8818 AppendCharToRedobuff(c);
8819
8820 /*
8821 * 0^D and ^^D: remove all indent.
8822 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008823 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8824 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 {
8826 --curwin->w_cursor.col;
8827 (void)del_char(FALSE); /* delete the '^' or '0' */
8828 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8829 if (State & REPLACE_FLAG)
8830 replace_pop_ins();
8831 if (lastc == '^')
8832 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008833 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 }
8835 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008836 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837
8838 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8839 did_ai = FALSE;
8840#ifdef FEAT_SMARTINDENT
8841 did_si = FALSE;
8842 can_si = FALSE;
8843 can_si_back = FALSE;
8844#endif
8845#ifdef FEAT_CINDENT
8846 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8847#endif
8848}
8849
8850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008851ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
8853 int temp;
8854
8855 if (stop_arrow() == FAIL)
8856 return;
8857 if (gchar_cursor() == NUL) /* delete newline */
8858 {
8859 temp = curwin->w_cursor.col;
8860 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008861 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008862 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 else
8864 curwin->w_cursor.col = temp;
8865 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008866 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8867 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 did_ai = FALSE;
8869#ifdef FEAT_SMARTINDENT
8870 did_si = FALSE;
8871 can_si = FALSE;
8872 can_si_back = FALSE;
8873#endif
8874 AppendCharToRedobuff(K_DEL);
8875}
8876
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008877static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008878
8879/*
8880 * Delete one character for ins_bs().
8881 */
8882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008883ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008884{
8885 dec_cursor();
8886 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8887 if (State & REPLACE_FLAG)
8888 {
8889 /* Don't delete characters before the insert point when in
8890 * Replace mode */
8891 if (curwin->w_cursor.lnum != Insstart.lnum
8892 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008893 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008894 }
8895 else
8896 (void)del_char(FALSE);
8897}
8898
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899/*
8900 * Handle Backspace, delete-word and delete-line in Insert mode.
8901 * Return TRUE when backspace was actually used.
8902 */
8903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008904ins_bs(
8905 int c,
8906 int mode,
8907 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909 linenr_T lnum;
8910 int cc;
8911 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008912 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 colnr_T mincol;
8914 int did_backspace = FALSE;
8915 int in_indent;
8916 int oldState;
8917#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008918 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919#endif
8920
8921 /*
8922 * can't delete anything in an empty file
8923 * can't backup past first character in buffer
8924 * can't backup past starting point unless 'backspace' > 1
8925 * can backup to a previous line if 'backspace' == 0
8926 */
8927 if ( bufempty()
8928 || (
8929#ifdef FEAT_RIGHTLEFT
8930 !revins_on &&
8931#endif
8932 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8933 || (!can_bs(BS_START)
8934 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008935 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8936 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8938 && curwin->w_cursor.col <= ai_col)
8939 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8940 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008941 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 return FALSE;
8943 }
8944
8945 if (stop_arrow() == FAIL)
8946 return FALSE;
8947 in_indent = inindent(0);
8948#ifdef FEAT_CINDENT
8949 if (in_indent)
8950 can_cindent = FALSE;
8951#endif
8952#ifdef FEAT_COMMENTS
8953 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8954#endif
8955#ifdef FEAT_RIGHTLEFT
8956 if (revins_on) /* put cursor after last inserted char */
8957 inc_cursor();
8958#endif
8959
8960#ifdef FEAT_VIRTUALEDIT
8961 /* Virtualedit:
8962 * BACKSPACE_CHAR eats a virtual space
8963 * BACKSPACE_WORD eats all coladd
8964 * BACKSPACE_LINE eats all coladd and keeps going
8965 */
8966 if (curwin->w_cursor.coladd > 0)
8967 {
8968 if (mode == BACKSPACE_CHAR)
8969 {
8970 --curwin->w_cursor.coladd;
8971 return TRUE;
8972 }
8973 if (mode == BACKSPACE_WORD)
8974 {
8975 curwin->w_cursor.coladd = 0;
8976 return TRUE;
8977 }
8978 curwin->w_cursor.coladd = 0;
8979 }
8980#endif
8981
8982 /*
8983 * delete newline!
8984 */
8985 if (curwin->w_cursor.col == 0)
8986 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008987 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008988 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989#ifdef FEAT_RIGHTLEFT
8990 || revins_on
8991#endif
8992 )
8993 {
8994 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8995 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8996 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008997 --Insstart.lnum;
8998 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 }
9000 /*
9001 * In replace mode:
9002 * cc < 0: NL was inserted, delete it
9003 * cc >= 0: NL was replaced, put original characters back
9004 */
9005 cc = -1;
9006 if (State & REPLACE_FLAG)
9007 cc = replace_pop(); /* returns -1 if NL was inserted */
9008 /*
9009 * In replace mode, in the line we started replacing, we only move the
9010 * cursor.
9011 */
9012 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9013 {
9014 dec_cursor();
9015 }
9016 else
9017 {
9018#ifdef FEAT_VREPLACE
9019 if (!(State & VREPLACE_FLAG)
9020 || curwin->w_cursor.lnum > orig_line_count)
9021#endif
9022 {
9023 temp = gchar_cursor(); /* remember current char */
9024 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009025
9026 /* When "aw" is in 'formatoptions' we must delete the space at
9027 * the end of the line, otherwise the line will be broken
9028 * again when auto-formatting. */
9029 if (has_format_option(FO_AUTO)
9030 && has_format_option(FO_WHITE_PAR))
9031 {
9032 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9033 TRUE);
9034 int len;
9035
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009036 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009037 if (len > 0 && ptr[len - 1] == ' ')
9038 ptr[len - 1] = NUL;
9039 }
9040
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009041 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 if (temp == NUL && gchar_cursor() != NUL)
9043 inc_cursor();
9044 }
9045#ifdef FEAT_VREPLACE
9046 else
9047 dec_cursor();
9048#endif
9049
9050 /*
9051 * In REPLACE mode we have to put back the text that was replaced
9052 * by the NL. On the replace stack is first a NUL-terminated
9053 * sequence of characters that were deleted and then the
9054 * characters that NL replaced.
9055 */
9056 if (State & REPLACE_FLAG)
9057 {
9058 /*
9059 * Do the next ins_char() in NORMAL state, to
9060 * prevent ins_char() from replacing characters and
9061 * avoiding showmatch().
9062 */
9063 oldState = State;
9064 State = NORMAL;
9065 /*
9066 * restore characters (blanks) deleted after cursor
9067 */
9068 while (cc > 0)
9069 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009070 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071#ifdef FEAT_MBYTE
9072 mb_replace_pop_ins(cc);
9073#else
9074 ins_char(cc);
9075#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009076 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 cc = replace_pop();
9078 }
9079 /* restore the characters that NL replaced */
9080 replace_pop_ins();
9081 State = oldState;
9082 }
9083 }
9084 did_ai = FALSE;
9085 }
9086 else
9087 {
9088 /*
9089 * Delete character(s) before the cursor.
9090 */
9091#ifdef FEAT_RIGHTLEFT
9092 if (revins_on) /* put cursor on last inserted char */
9093 dec_cursor();
9094#endif
9095 mincol = 0;
9096 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009097 if (mode == BACKSPACE_LINE
9098 && (curbuf->b_p_ai
9099#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009100 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009101#endif
9102 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103#ifdef FEAT_RIGHTLEFT
9104 && !revins_on
9105#endif
9106 )
9107 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009108 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009110 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009112 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 }
9114
9115 /*
9116 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9117 */
9118 if ( mode == BACKSPACE_CHAR
9119 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009120 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009121 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 && (*(ml_get_cursor() - 1) == TAB
9123 || (*(ml_get_cursor() - 1) == ' '
9124 && (!*inserted_space_p
9125 || arrow_used))))))
9126 {
9127 int ts;
9128 colnr_T vcol;
9129 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009130 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131
9132 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009133 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009134 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009136 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 /* Compute the virtual column where we want to be. Since
9138 * 'showbreak' may get in the way, need to get the last column of
9139 * the previous character. */
9140 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009141 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 dec_cursor();
9143 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9144 inc_cursor();
9145 want_vcol = (want_vcol / ts) * ts;
9146
9147 /* delete characters until we are at or before want_vcol */
9148 while (vcol > want_vcol
9149 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009150 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151
9152 /* insert extra spaces until we are at want_vcol */
9153 while (vcol < want_vcol)
9154 {
9155 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009156 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9157 && curwin->w_cursor.col < Insstart_orig.col)
9158 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159
9160#ifdef FEAT_VREPLACE
9161 if (State & VREPLACE_FLAG)
9162 ins_char(' ');
9163 else
9164#endif
9165 {
9166 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009167 if ((State & REPLACE_FLAG))
9168 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 }
9170 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9171 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009172
9173 /* If we are now back where we started delete one character. Can
9174 * happen when using 'sts' and 'linebreak'. */
9175 if (vcol >= start_vcol)
9176 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 }
9178
9179 /*
9180 * Delete upto starting point, start of line or previous word.
9181 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009182 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009184#ifdef FEAT_MBYTE
9185 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009187 if (has_mbyte)
9188 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009190 do
9191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009193 if (!revins_on) /* put cursor on char to be deleted */
9194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009196
9197 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009199 /* look multi-byte character class */
9200 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009202 prev_cclass = cclass;
9203 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009206
9207 /* start of word? */
9208 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9209 {
9210 mode = BACKSPACE_WORD_NOT_SPACE;
9211 temp = vim_iswordc(cc);
9212 }
9213 /* end of word? */
9214 else if (mode == BACKSPACE_WORD_NOT_SPACE
9215 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9216#ifdef FEAT_MBYTE
9217 || prev_cclass != cclass
9218#endif
9219 ))
9220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009222 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009224 inc_cursor();
9225#ifdef FEAT_RIGHTLEFT
9226 else if (State & REPLACE_FLAG)
9227 dec_cursor();
9228#endif
9229 break;
9230 }
9231 if (State & REPLACE_FLAG)
9232 replace_do_bs(-1);
9233 else
9234 {
9235#ifdef FEAT_MBYTE
9236 if (enc_utf8 && p_deco)
9237 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9238#endif
9239 (void)del_char(FALSE);
9240#ifdef FEAT_MBYTE
9241 /*
9242 * If there are combining characters and 'delcombine' is set
9243 * move the cursor back. Don't back up before the base
9244 * character.
9245 */
9246 if (enc_utf8 && p_deco && cpc[0] != NUL)
9247 inc_cursor();
9248#endif
9249#ifdef FEAT_RIGHTLEFT
9250 if (revins_chars)
9251 {
9252 revins_chars--;
9253 revins_legal++;
9254 }
9255 if (revins_on && gchar_cursor() == NUL)
9256 break;
9257#endif
9258 }
9259 /* Just a single backspace?: */
9260 if (mode == BACKSPACE_CHAR)
9261 break;
9262 } while (
9263#ifdef FEAT_RIGHTLEFT
9264 revins_on ||
9265#endif
9266 (curwin->w_cursor.col > mincol
9267 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9268 || curwin->w_cursor.col != Insstart_orig.col)));
9269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 did_backspace = TRUE;
9271 }
9272#ifdef FEAT_SMARTINDENT
9273 did_si = FALSE;
9274 can_si = FALSE;
9275 can_si_back = FALSE;
9276#endif
9277 if (curwin->w_cursor.col <= 1)
9278 did_ai = FALSE;
9279 /*
9280 * It's a little strange to put backspaces into the redo
9281 * buffer, but it makes auto-indent a lot easier to deal
9282 * with.
9283 */
9284 AppendCharToRedobuff(c);
9285
9286 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009287 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9288 && curwin->w_cursor.col < Insstart_orig.col)
9289 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290
9291 /* vi behaviour: the cursor moves backward but the character that
9292 * was there remains visible
9293 * Vim behaviour: the cursor moves backward and the character that
9294 * was there is erased from the screen.
9295 * We can emulate the vi behaviour by pretending there is a dollar
9296 * displayed even when there isn't.
9297 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009298 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 dollar_vcol = curwin->w_virtcol;
9300
Bram Moolenaarce3be472008-01-14 19:12:28 +00009301#ifdef FEAT_FOLDING
9302 /* When deleting a char the cursor line must never be in a closed fold.
9303 * E.g., when 'foldmethod' is indent and deleting the first non-white
9304 * char before a Tab. */
9305 if (did_backspace)
9306 foldOpenCursor();
9307#endif
9308
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 return did_backspace;
9310}
9311
9312#ifdef FEAT_MOUSE
9313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009314ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315{
9316 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009317 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318
9319# ifdef FEAT_GUI
9320 /* When GUI is active, also move/paste when 'mouse' is empty */
9321 if (!gui.in_use)
9322# endif
9323 if (!mouse_has(MOUSE_INSERT))
9324 return;
9325
9326 undisplay_dollar();
9327 tpos = curwin->w_cursor;
9328 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9329 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009330#ifdef FEAT_WINDOWS
9331 win_T *new_curwin = curwin;
9332
9333 if (curwin != old_curwin && win_valid(old_curwin))
9334 {
9335 /* Mouse took us to another window. We need to go back to the
9336 * previous one to stop insert there properly. */
9337 curwin = old_curwin;
9338 curbuf = curwin->w_buffer;
9339 }
9340#endif
9341 start_arrow(curwin == old_curwin ? &tpos : NULL);
9342#ifdef FEAT_WINDOWS
9343 if (curwin != new_curwin && win_valid(new_curwin))
9344 {
9345 curwin = new_curwin;
9346 curbuf = curwin->w_buffer;
9347 }
9348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349# ifdef FEAT_CINDENT
9350 can_cindent = TRUE;
9351# endif
9352 }
9353
9354#ifdef FEAT_WINDOWS
9355 /* redraw status lines (in case another window became active) */
9356 redraw_statuslines();
9357#endif
9358}
9359
9360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009361ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362{
9363 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009364# if defined(FEAT_WINDOWS)
9365 win_T *old_curwin = curwin;
9366# endif
9367# ifdef FEAT_INS_EXPAND
9368 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369# endif
9370
9371 tpos = curwin->w_cursor;
9372
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009373# ifdef FEAT_WINDOWS
9374 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 {
9376 int row, col;
9377
9378 row = mouse_row;
9379 col = mouse_col;
9380
9381 /* find the window at the pointer coordinates */
9382 curwin = mouse_find_win(&row, &col);
9383 curbuf = curwin->w_buffer;
9384 }
9385 if (curwin == old_curwin)
9386# endif
9387 undisplay_dollar();
9388
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009389# ifdef FEAT_INS_EXPAND
9390 /* Don't scroll the window in which completion is being done. */
9391 if (!pum_visible()
9392# if defined(FEAT_WINDOWS)
9393 || curwin != old_curwin
9394# endif
9395 )
9396# endif
9397 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009398 if (dir == MSCR_DOWN || dir == MSCR_UP)
9399 {
9400 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9401 scroll_redraw(dir,
9402 (long)(curwin->w_botline - curwin->w_topline));
9403 else
9404 scroll_redraw(dir, 3L);
9405 }
9406#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009407 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009408 {
9409 int val, step = 6;
9410
9411 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9412 step = W_WIDTH(curwin);
9413 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9414 if (val < 0)
9415 val = 0;
9416 gui_do_horiz_scroll(val, TRUE);
9417 }
9418#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009419# ifdef FEAT_INS_EXPAND
9420 did_scroll = TRUE;
9421# endif
9422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009424# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 curwin->w_redr_status = TRUE;
9426
9427 curwin = old_curwin;
9428 curbuf = curwin->w_buffer;
9429# endif
9430
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009431# ifdef FEAT_INS_EXPAND
9432 /* The popup menu may overlay the window, need to redraw it.
9433 * TODO: Would be more efficient to only redraw the windows that are
9434 * overlapped by the popup menu. */
9435 if (pum_visible() && did_scroll)
9436 {
9437 redraw_all_later(NOT_VALID);
9438 ins_compl_show_pum();
9439 }
9440# endif
9441
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 if (!equalpos(curwin->w_cursor, tpos))
9443 {
9444 start_arrow(&tpos);
9445# ifdef FEAT_CINDENT
9446 can_cindent = TRUE;
9447# endif
9448 }
9449}
9450#endif
9451
Bram Moolenaarec2da362017-01-21 20:04:22 +01009452/*
9453 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9454 * P_PE literally.
9455 * When "drop" is TRUE then consume the text and drop it.
9456 */
9457 int
9458bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9459{
9460 int c;
9461 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9462 int idx = 0;
9463 char_u *end = find_termcode((char_u *)"PE");
9464 int ret_char = -1;
9465 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009466 int save_paste = p_paste;
9467 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009468
9469 /* If the end code is too long we can't detect it, read everything. */
9470 if (STRLEN(end) >= NUMBUFLEN)
9471 end = NULL;
9472 ++no_mapping;
9473 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009474 p_paste = TRUE;
9475 curbuf->b_p_ai = FALSE;
9476
Bram Moolenaarec2da362017-01-21 20:04:22 +01009477 for (;;)
9478 {
9479 /* When the end is not defined read everything. */
9480 if (end == NULL && vpeekc() == NUL)
9481 break;
9482 c = plain_vgetc();
9483#ifdef FEAT_MBYTE
9484 if (has_mbyte)
9485 idx += (*mb_char2bytes)(c, buf + idx);
9486 else
9487#endif
9488 buf[idx++] = c;
9489 buf[idx] = NUL;
9490 if (end != NUL && STRNCMP(buf, end, idx) == 0)
9491 {
9492 if (end[idx] == NUL)
9493 break; /* Found the end of paste code. */
9494 continue;
9495 }
9496 if (!drop)
9497 {
9498 switch (mode)
9499 {
9500 case PASTE_CMDLINE:
9501 put_on_cmdline(buf, idx, TRUE);
9502 break;
9503
9504 case PASTE_EX:
9505 if (gap != NULL && ga_grow(gap, idx) == OK)
9506 {
9507 mch_memmove((char *)gap->ga_data + gap->ga_len,
9508 buf, (size_t)idx);
9509 gap->ga_len += idx;
9510 }
9511 break;
9512
9513 case PASTE_INSERT:
9514 if (stop_arrow() == OK)
9515 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009516 c = buf[0];
9517 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9518 ins_eol(c);
9519 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009520 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009521 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009522 AppendToRedobuffLit(buf, idx);
9523 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009524 }
9525 break;
9526
9527 case PASTE_ONE_CHAR:
9528 if (ret_char == -1)
9529 {
9530#ifdef FEAT_MBYTE
9531 if (has_mbyte)
9532 ret_char = (*mb_ptr2char)(buf);
9533 else
9534#endif
9535 ret_char = buf[0];
9536 }
9537 break;
9538 }
9539 }
9540 idx = 0;
9541 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009542
Bram Moolenaarec2da362017-01-21 20:04:22 +01009543 --no_mapping;
9544 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009545 p_paste = save_paste;
9546 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009547
9548 return ret_char;
9549}
9550
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009551#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009553ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009554{
9555 /* We will be leaving the current window, unless closing another tab. */
9556 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9557 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9558 {
9559 undisplay_dollar();
9560 start_arrow(&curwin->w_cursor);
9561# ifdef FEAT_CINDENT
9562 can_cindent = TRUE;
9563# endif
9564 }
9565
9566 if (c == K_TABLINE)
9567 goto_tabpage(current_tab);
9568 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009569 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009570 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009571 redraw_statuslines(); /* will redraw the tabline when needed */
9572 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009573}
9574#endif
9575
9576#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009578ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
9580 pos_T tpos;
9581
9582 undisplay_dollar();
9583 tpos = curwin->w_cursor;
9584 if (gui_do_scroll())
9585 {
9586 start_arrow(&tpos);
9587# ifdef FEAT_CINDENT
9588 can_cindent = TRUE;
9589# endif
9590 }
9591}
9592
9593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009594ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595{
9596 pos_T tpos;
9597
9598 undisplay_dollar();
9599 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009600 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 {
9602 start_arrow(&tpos);
9603# ifdef FEAT_CINDENT
9604 can_cindent = TRUE;
9605# endif
9606 }
9607}
9608#endif
9609
9610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009611ins_left(
9612 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
9614 pos_T tpos;
9615
9616#ifdef FEAT_FOLDING
9617 if ((fdo_flags & FDO_HOR) && KeyTyped)
9618 foldOpenCursor();
9619#endif
9620 undisplay_dollar();
9621 tpos = curwin->w_cursor;
9622 if (oneleft() == OK)
9623 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009624#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9625 /* Only call start_arrow() when not busy with preediting, it will
9626 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9627 if (!im_is_preediting())
9628#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009629 {
9630 start_arrow_with_change(&tpos, end_change);
9631 if (!end_change)
9632 AppendCharToRedobuff(K_LEFT);
9633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634#ifdef FEAT_RIGHTLEFT
9635 /* If exit reversed string, position is fixed */
9636 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9637 revins_legal++;
9638 revins_chars++;
9639#endif
9640 }
9641
9642 /*
9643 * if 'whichwrap' set for cursor in insert mode may go to
9644 * previous line
9645 */
9646 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9647 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009648 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 start_arrow(&tpos);
9650 --(curwin->w_cursor.lnum);
9651 coladvance((colnr_T)MAXCOL);
9652 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9653 }
9654 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009655 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009656 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657}
9658
9659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009660ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662 pos_T tpos;
9663
9664#ifdef FEAT_FOLDING
9665 if ((fdo_flags & FDO_HOR) && KeyTyped)
9666 foldOpenCursor();
9667#endif
9668 undisplay_dollar();
9669 tpos = curwin->w_cursor;
9670 if (c == K_C_HOME)
9671 curwin->w_cursor.lnum = 1;
9672 curwin->w_cursor.col = 0;
9673#ifdef FEAT_VIRTUALEDIT
9674 curwin->w_cursor.coladd = 0;
9675#endif
9676 curwin->w_curswant = 0;
9677 start_arrow(&tpos);
9678}
9679
9680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009681ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682{
9683 pos_T tpos;
9684
9685#ifdef FEAT_FOLDING
9686 if ((fdo_flags & FDO_HOR) && KeyTyped)
9687 foldOpenCursor();
9688#endif
9689 undisplay_dollar();
9690 tpos = curwin->w_cursor;
9691 if (c == K_C_END)
9692 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9693 coladvance((colnr_T)MAXCOL);
9694 curwin->w_curswant = MAXCOL;
9695
9696 start_arrow(&tpos);
9697}
9698
9699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009700ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
9702#ifdef FEAT_FOLDING
9703 if ((fdo_flags & FDO_HOR) && KeyTyped)
9704 foldOpenCursor();
9705#endif
9706 undisplay_dollar();
9707 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9708 {
9709 start_arrow(&curwin->w_cursor);
9710 (void)bck_word(1L, FALSE, FALSE);
9711 curwin->w_set_curswant = TRUE;
9712 }
9713 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009714 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715}
9716
9717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009718ins_right(
9719 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
9721#ifdef FEAT_FOLDING
9722 if ((fdo_flags & FDO_HOR) && KeyTyped)
9723 foldOpenCursor();
9724#endif
9725 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009726 if (gchar_cursor() != NUL
9727#ifdef FEAT_VIRTUALEDIT
9728 || virtual_active()
9729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 )
9731 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009732 start_arrow_with_change(&curwin->w_cursor, end_change);
9733 if (!end_change)
9734 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 curwin->w_set_curswant = TRUE;
9736#ifdef FEAT_VIRTUALEDIT
9737 if (virtual_active())
9738 oneright();
9739 else
9740#endif
9741 {
9742#ifdef FEAT_MBYTE
9743 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009744 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 else
9746#endif
9747 ++curwin->w_cursor.col;
9748 }
9749
9750#ifdef FEAT_RIGHTLEFT
9751 revins_legal++;
9752 if (revins_chars)
9753 revins_chars--;
9754#endif
9755 }
9756 /* if 'whichwrap' set for cursor in insert mode, may move the
9757 * cursor to the next line */
9758 else if (vim_strchr(p_ww, ']') != NULL
9759 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9760 {
9761 start_arrow(&curwin->w_cursor);
9762 curwin->w_set_curswant = TRUE;
9763 ++curwin->w_cursor.lnum;
9764 curwin->w_cursor.col = 0;
9765 }
9766 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009767 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009768 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769}
9770
9771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009772ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773{
9774#ifdef FEAT_FOLDING
9775 if ((fdo_flags & FDO_HOR) && KeyTyped)
9776 foldOpenCursor();
9777#endif
9778 undisplay_dollar();
9779 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9780 || gchar_cursor() != NUL)
9781 {
9782 start_arrow(&curwin->w_cursor);
9783 (void)fwd_word(1L, FALSE, 0);
9784 curwin->w_set_curswant = TRUE;
9785 }
9786 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009787 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788}
9789
9790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009791ins_up(
9792 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793{
9794 pos_T tpos;
9795 linenr_T old_topline = curwin->w_topline;
9796#ifdef FEAT_DIFF
9797 int old_topfill = curwin->w_topfill;
9798#endif
9799
9800 undisplay_dollar();
9801 tpos = curwin->w_cursor;
9802 if (cursor_up(1L, TRUE) == OK)
9803 {
9804 if (startcol)
9805 coladvance(getvcol_nolist(&Insstart));
9806 if (old_topline != curwin->w_topline
9807#ifdef FEAT_DIFF
9808 || old_topfill != curwin->w_topfill
9809#endif
9810 )
9811 redraw_later(VALID);
9812 start_arrow(&tpos);
9813#ifdef FEAT_CINDENT
9814 can_cindent = TRUE;
9815#endif
9816 }
9817 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009818 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819}
9820
9821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009822ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823{
9824 pos_T tpos;
9825
9826 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009827
9828#ifdef FEAT_WINDOWS
9829 if (mod_mask & MOD_MASK_CTRL)
9830 {
9831 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009832 if (first_tabpage->tp_next != NULL)
9833 {
9834 start_arrow(&curwin->w_cursor);
9835 goto_tabpage(-1);
9836 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009837 return;
9838 }
9839#endif
9840
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 tpos = curwin->w_cursor;
9842 if (onepage(BACKWARD, 1L) == OK)
9843 {
9844 start_arrow(&tpos);
9845#ifdef FEAT_CINDENT
9846 can_cindent = TRUE;
9847#endif
9848 }
9849 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009850 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851}
9852
9853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009854ins_down(
9855 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856{
9857 pos_T tpos;
9858 linenr_T old_topline = curwin->w_topline;
9859#ifdef FEAT_DIFF
9860 int old_topfill = curwin->w_topfill;
9861#endif
9862
9863 undisplay_dollar();
9864 tpos = curwin->w_cursor;
9865 if (cursor_down(1L, TRUE) == OK)
9866 {
9867 if (startcol)
9868 coladvance(getvcol_nolist(&Insstart));
9869 if (old_topline != curwin->w_topline
9870#ifdef FEAT_DIFF
9871 || old_topfill != curwin->w_topfill
9872#endif
9873 )
9874 redraw_later(VALID);
9875 start_arrow(&tpos);
9876#ifdef FEAT_CINDENT
9877 can_cindent = TRUE;
9878#endif
9879 }
9880 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009881 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882}
9883
9884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009885ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886{
9887 pos_T tpos;
9888
9889 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009890
9891#ifdef FEAT_WINDOWS
9892 if (mod_mask & MOD_MASK_CTRL)
9893 {
9894 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009895 if (first_tabpage->tp_next != NULL)
9896 {
9897 start_arrow(&curwin->w_cursor);
9898 goto_tabpage(0);
9899 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009900 return;
9901 }
9902#endif
9903
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 tpos = curwin->w_cursor;
9905 if (onepage(FORWARD, 1L) == OK)
9906 {
9907 start_arrow(&tpos);
9908#ifdef FEAT_CINDENT
9909 can_cindent = TRUE;
9910#endif
9911 }
9912 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009913 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914}
9915
9916#ifdef FEAT_DND
9917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009918ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919{
9920 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9921}
9922#endif
9923
9924/*
9925 * Handle TAB in Insert or Replace mode.
9926 * Return TRUE when the TAB needs to be inserted like a normal character.
9927 */
9928 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009929ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930{
9931 int ind;
9932 int i;
9933 int temp;
9934
9935 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9936 Insstart_blank_vcol = get_nolist_virtcol();
9937 if (echeck_abbr(TAB + ABBR_OFF))
9938 return FALSE;
9939
9940 ind = inindent(0);
9941#ifdef FEAT_CINDENT
9942 if (ind)
9943 can_cindent = FALSE;
9944#endif
9945
9946 /*
9947 * When nothing special, insert TAB like a normal character
9948 */
9949 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009950 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009951 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 return TRUE;
9953
9954 if (stop_arrow() == FAIL)
9955 return TRUE;
9956
9957 did_ai = FALSE;
9958#ifdef FEAT_SMARTINDENT
9959 did_si = FALSE;
9960 can_si = FALSE;
9961 can_si_back = FALSE;
9962#endif
9963 AppendToRedobuff((char_u *)"\t");
9964
9965 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009966 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009967 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9968 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 else /* otherwise use 'tabstop' */
9970 temp = (int)curbuf->b_p_ts;
9971 temp -= get_nolist_virtcol() % temp;
9972
9973 /*
9974 * Insert the first space with ins_char(). It will delete one char in
9975 * replace mode. Insert the rest with ins_str(); it will not delete any
9976 * chars. For VREPLACE mode, we use ins_char() for all characters.
9977 */
9978 ins_char(' ');
9979 while (--temp > 0)
9980 {
9981#ifdef FEAT_VREPLACE
9982 if (State & VREPLACE_FLAG)
9983 ins_char(' ');
9984 else
9985#endif
9986 {
9987 ins_str((char_u *)" ");
9988 if (State & REPLACE_FLAG) /* no char replaced */
9989 replace_push(NUL);
9990 }
9991 }
9992
9993 /*
9994 * When 'expandtab' not set: Replace spaces by TABs where possible.
9995 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009996 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 {
9998 char_u *ptr;
9999#ifdef FEAT_VREPLACE
10000 char_u *saved_line = NULL; /* init for GCC */
10001 pos_T pos;
10002#endif
10003 pos_T fpos;
10004 pos_T *cursor;
10005 colnr_T want_vcol, vcol;
10006 int change_col = -1;
10007 int save_list = curwin->w_p_list;
10008
10009 /*
10010 * Get the current line. For VREPLACE mode, don't make real changes
10011 * yet, just work on a copy of the line.
10012 */
10013#ifdef FEAT_VREPLACE
10014 if (State & VREPLACE_FLAG)
10015 {
10016 pos = curwin->w_cursor;
10017 cursor = &pos;
10018 saved_line = vim_strsave(ml_get_curline());
10019 if (saved_line == NULL)
10020 return FALSE;
10021 ptr = saved_line + pos.col;
10022 }
10023 else
10024#endif
10025 {
10026 ptr = ml_get_cursor();
10027 cursor = &curwin->w_cursor;
10028 }
10029
10030 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10031 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10032 curwin->w_p_list = FALSE;
10033
10034 /* Find first white before the cursor */
10035 fpos = curwin->w_cursor;
10036 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
10037 {
10038 --fpos.col;
10039 --ptr;
10040 }
10041
10042 /* In Replace mode, don't change characters before the insert point. */
10043 if ((State & REPLACE_FLAG)
10044 && fpos.lnum == Insstart.lnum
10045 && fpos.col < Insstart.col)
10046 {
10047 ptr += Insstart.col - fpos.col;
10048 fpos.col = Insstart.col;
10049 }
10050
10051 /* compute virtual column numbers of first white and cursor */
10052 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10053 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10054
Bram Moolenaar597a4222014-06-25 14:39:50 +020010055 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10056 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 while (vim_iswhite(*ptr))
10058 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010059 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 if (vcol + i > want_vcol)
10061 break;
10062 if (*ptr != TAB)
10063 {
10064 *ptr = TAB;
10065 if (change_col < 0)
10066 {
10067 change_col = fpos.col; /* Column of first change */
10068 /* May have to adjust Insstart */
10069 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10070 Insstart.col = fpos.col;
10071 }
10072 }
10073 ++fpos.col;
10074 ++ptr;
10075 vcol += i;
10076 }
10077
10078 if (change_col >= 0)
10079 {
10080 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010081 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082
10083 /* Skip over the spaces we need. */
10084 while (vcol < want_vcol && *ptr == ' ')
10085 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010086 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 ++ptr;
10088 ++repl_off;
10089 }
10090 if (vcol > want_vcol)
10091 {
10092 /* Must have a char with 'showbreak' just before it. */
10093 --ptr;
10094 --repl_off;
10095 }
10096 fpos.col += repl_off;
10097
10098 /* Delete following spaces. */
10099 i = cursor->col - fpos.col;
10100 if (i > 0)
10101 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010102 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 /* correct replace stack. */
10104 if ((State & REPLACE_FLAG)
10105#ifdef FEAT_VREPLACE
10106 && !(State & VREPLACE_FLAG)
10107#endif
10108 )
10109 for (temp = i; --temp >= 0; )
10110 replace_join(repl_off);
10111 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010112#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010113 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010114 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010115 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010116 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10117 (char_u *)"\t", 1);
10118 }
10119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 cursor->col -= i;
10121
10122#ifdef FEAT_VREPLACE
10123 /*
10124 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10125 * backspacing over the changed spacing and then inserting the new
10126 * spacing.
10127 */
10128 if (State & VREPLACE_FLAG)
10129 {
10130 /* Backspace from real cursor to change_col */
10131 backspace_until_column(change_col);
10132
10133 /* Insert each char in saved_line from changed_col to
10134 * ptr-cursor */
10135 ins_bytes_len(saved_line + change_col,
10136 cursor->col - change_col);
10137 }
10138#endif
10139 }
10140
10141#ifdef FEAT_VREPLACE
10142 if (State & VREPLACE_FLAG)
10143 vim_free(saved_line);
10144#endif
10145 curwin->w_p_list = save_list;
10146 }
10147
10148 return FALSE;
10149}
10150
10151/*
10152 * Handle CR or NL in insert mode.
10153 * Return TRUE when out of memory or can't undo.
10154 */
10155 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010156ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157{
10158 int i;
10159
10160 if (echeck_abbr(c + ABBR_OFF))
10161 return FALSE;
10162 if (stop_arrow() == FAIL)
10163 return TRUE;
10164 undisplay_dollar();
10165
10166 /*
10167 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10168 * character under the cursor. Only push a NUL on the replace stack,
10169 * nothing to put back when the NL is deleted.
10170 */
10171 if ((State & REPLACE_FLAG)
10172#ifdef FEAT_VREPLACE
10173 && !(State & VREPLACE_FLAG)
10174#endif
10175 )
10176 replace_push(NUL);
10177
10178 /*
10179 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10180 * replacing the next line, so we push all of the characters left on the
10181 * line onto the replace stack. This is not done here though, it is done
10182 * in open_line().
10183 */
10184
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010185#ifdef FEAT_VIRTUALEDIT
10186 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10187 * CTRL-O). */
10188 if (virtual_active() && curwin->w_cursor.coladd > 0)
10189 coladvance(getviscol());
10190#endif
10191
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192#ifdef FEAT_RIGHTLEFT
10193# ifdef FEAT_FKMAP
10194 if (p_altkeymap && p_fkmap)
10195 fkmap(NL);
10196# endif
10197 /* NL in reverse insert will always start in the end of
10198 * current line. */
10199 if (revins_on)
10200 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10201#endif
10202
10203 AppendToRedobuff(NL_STR);
10204 i = open_line(FORWARD,
10205#ifdef FEAT_COMMENTS
10206 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10207#endif
10208 0, old_indent);
10209 old_indent = 0;
10210#ifdef FEAT_CINDENT
10211 can_cindent = TRUE;
10212#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010213#ifdef FEAT_FOLDING
10214 /* When inserting a line the cursor line must never be in a closed fold. */
10215 foldOpenCursor();
10216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217
10218 return (!i);
10219}
10220
10221#ifdef FEAT_DIGRAPHS
10222/*
10223 * Handle digraph in insert mode.
10224 * Returns character still to be inserted, or NUL when nothing remaining to be
10225 * done.
10226 */
10227 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010228ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229{
10230 int c;
10231 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010232 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233
10234 pc_status = PC_STATUS_UNSET;
10235 if (redrawing() && !char_avail())
10236 {
10237 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010238 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239
10240 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010241 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242#ifdef FEAT_CMDL_INFO
10243 add_to_showcmd_c(Ctrl_K);
10244#endif
10245 }
10246
10247#ifdef USE_ON_FLY_SCROLL
10248 dont_scroll = TRUE; /* disallow scrolling here */
10249#endif
10250
10251 /* don't map the digraph chars. This also prevents the
10252 * mode message to be deleted when ESC is hit */
10253 ++no_mapping;
10254 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010255 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 --no_mapping;
10257 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010258 if (did_putchar)
10259 /* when the line fits in 'columns' the '?' is at the start of the next
10260 * line and will not be removed by the redraw */
10261 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010262
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 if (IS_SPECIAL(c) || mod_mask) /* special key */
10264 {
10265#ifdef FEAT_CMDL_INFO
10266 clear_showcmd();
10267#endif
10268 insert_special(c, TRUE, FALSE);
10269 return NUL;
10270 }
10271 if (c != ESC)
10272 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010273 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 if (redrawing() && !char_avail())
10275 {
10276 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010277 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278
10279 if (char2cells(c) == 1)
10280 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010281 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010283 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 }
10285#ifdef FEAT_CMDL_INFO
10286 add_to_showcmd_c(c);
10287#endif
10288 }
10289 ++no_mapping;
10290 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010291 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 --no_mapping;
10293 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010294 if (did_putchar)
10295 /* when the line fits in 'columns' the '?' is at the start of the
10296 * next line and will not be removed by a redraw */
10297 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 if (cc != ESC)
10299 {
10300 AppendToRedobuff((char_u *)CTRL_V_STR);
10301 c = getdigraph(c, cc, TRUE);
10302#ifdef FEAT_CMDL_INFO
10303 clear_showcmd();
10304#endif
10305 return c;
10306 }
10307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308#ifdef FEAT_CMDL_INFO
10309 clear_showcmd();
10310#endif
10311 return NUL;
10312}
10313#endif
10314
10315/*
10316 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10317 * Returns the char to be inserted, or NUL if none found.
10318 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010320ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321{
10322 int c;
10323 int temp;
10324 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010325 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326
10327 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10328 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010329 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 return NUL;
10331 }
10332
10333 /* try to advance to the cursor column */
10334 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010335 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 prev_ptr = ptr;
10337 validate_virtcol();
10338 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10339 {
10340 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010341 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 }
10343 if ((colnr_T)temp > curwin->w_virtcol)
10344 ptr = prev_ptr;
10345
10346#ifdef FEAT_MBYTE
10347 c = (*mb_ptr2char)(ptr);
10348#else
10349 c = *ptr;
10350#endif
10351 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010352 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 return c;
10354}
10355
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010356/*
10357 * CTRL-Y or CTRL-E typed in Insert mode.
10358 */
10359 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010360ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010361{
10362 int c = tc;
10363
10364#ifdef FEAT_INS_EXPAND
10365 if (ctrl_x_mode == CTRL_X_SCROLL)
10366 {
10367 if (c == Ctrl_Y)
10368 scrolldown_clamp();
10369 else
10370 scrollup_clamp();
10371 redraw_later(VALID);
10372 }
10373 else
10374#endif
10375 {
10376 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10377 if (c != NUL)
10378 {
10379 long tw_save;
10380
10381 /* The character must be taken literally, insert like it
10382 * was typed after a CTRL-V, and pretend 'textwidth'
10383 * wasn't set. Digits, 'o' and 'x' are special after a
10384 * CTRL-V, don't use it for these. */
10385 if (c < 256 && !isalnum(c))
10386 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10387 tw_save = curbuf->b_p_tw;
10388 curbuf->b_p_tw = -1;
10389 insert_special(c, TRUE, FALSE);
10390 curbuf->b_p_tw = tw_save;
10391#ifdef FEAT_RIGHTLEFT
10392 revins_chars++;
10393 revins_legal++;
10394#endif
10395 c = Ctrl_V; /* pretend CTRL-V is last character */
10396 auto_format(FALSE, TRUE);
10397 }
10398 }
10399 return c;
10400}
10401
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402#ifdef FEAT_SMARTINDENT
10403/*
10404 * Try to do some very smart auto-indenting.
10405 * Used when inserting a "normal" character.
10406 */
10407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010408ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409{
10410 pos_T *pos, old_pos;
10411 char_u *ptr;
10412 int i;
10413 int temp;
10414
10415 /*
10416 * do some very smart indenting when entering '{' or '}'
10417 */
10418 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10419 {
10420 /*
10421 * for '}' set indent equal to indent of line containing matching '{'
10422 */
10423 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10424 {
10425 old_pos = curwin->w_cursor;
10426 /*
10427 * If the matching '{' has a ')' immediately before it (ignoring
10428 * white-space), then line up with the start of the line
10429 * containing the matching '(' if there is one. This handles the
10430 * case where an "if (..\n..) {" statement continues over multiple
10431 * lines -- webb
10432 */
10433 ptr = ml_get(pos->lnum);
10434 i = pos->col;
10435 if (i > 0) /* skip blanks before '{' */
10436 while (--i > 0 && vim_iswhite(ptr[i]))
10437 ;
10438 curwin->w_cursor.lnum = pos->lnum;
10439 curwin->w_cursor.col = i;
10440 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10441 curwin->w_cursor = *pos;
10442 i = get_indent();
10443 curwin->w_cursor = old_pos;
10444#ifdef FEAT_VREPLACE
10445 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010446 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 else
10448#endif
10449 (void)set_indent(i, SIN_CHANGED);
10450 }
10451 else if (curwin->w_cursor.col > 0)
10452 {
10453 /*
10454 * when inserting '{' after "O" reduce indent, but not
10455 * more than indent of previous line
10456 */
10457 temp = TRUE;
10458 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10459 {
10460 old_pos = curwin->w_cursor;
10461 i = get_indent();
10462 while (curwin->w_cursor.lnum > 1)
10463 {
10464 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10465
10466 /* ignore empty lines and lines starting with '#'. */
10467 if (*ptr != '#' && *ptr != NUL)
10468 break;
10469 }
10470 if (get_indent() >= i)
10471 temp = FALSE;
10472 curwin->w_cursor = old_pos;
10473 }
10474 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010475 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 }
10477 }
10478
10479 /*
10480 * set indent of '#' always to 0
10481 */
10482 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10483 {
10484 /* remember current indent for next line */
10485 old_indent = get_indent();
10486 (void)set_indent(0, SIN_CHANGED);
10487 }
10488
10489 /* Adjust ai_col, the char at this position can be deleted. */
10490 if (ai_col > curwin->w_cursor.col)
10491 ai_col = curwin->w_cursor.col;
10492}
10493#endif
10494
10495/*
10496 * Get the value that w_virtcol would have when 'list' is off.
10497 * Unless 'cpo' contains the 'L' flag.
10498 */
10499 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010500get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501{
10502 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10503 return getvcol_nolist(&curwin->w_cursor);
10504 validate_virtcol();
10505 return curwin->w_virtcol;
10506}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010507
10508#ifdef FEAT_AUTOCMD
10509/*
10510 * Handle the InsertCharPre autocommand.
10511 * "c" is the character that was typed.
10512 * Return a pointer to allocated memory with the replacement string.
10513 * Return NULL to continue inserting "c".
10514 */
10515 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010516do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010517{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010518 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010519 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010520
10521 /* Return quickly when there is nothing to do. */
10522 if (!has_insertcharpre())
10523 return NULL;
10524
Bram Moolenaar704984a2012-06-01 14:57:51 +020010525#ifdef FEAT_MBYTE
10526 if (has_mbyte)
10527 buf[(*mb_char2bytes)(c, buf)] = NUL;
10528 else
10529#endif
10530 {
10531 buf[0] = c;
10532 buf[1] = NUL;
10533 }
10534
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010535 /* Lock the text to avoid weird things from happening. */
10536 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010537 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010538
Bram Moolenaar704984a2012-06-01 14:57:51 +020010539 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010540 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010541 {
10542 /* Get the value of v:char. It may be empty or more than one
10543 * character. Only use it when changed, otherwise continue with the
10544 * original character to avoid breaking autoindent. */
10545 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10546 res = vim_strsave(get_vim_var_str(VV_CHAR));
10547 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010548
10549 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10550 --textlock;
10551
10552 return res;
10553}
10554#endif