blob: d7f89bcbed8b36245345a8804206f663b698a08d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaare4214502015-03-05 18:08:43 +010037#define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
Bram Moolenaare4214502015-03-05 18:08:43 +010040#define CTRL_X_MODE_LINE_OR_EVAL(m) (m == CTRL_X_WHOLE_LINE || m == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42static char *ctrl_x_msgs[] =
43{
44 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000046 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 N_(" Whole line completion (^L^N^P)"),
48 N_(" File name completion (^F^N^P)"),
49 N_(" Tag completion (^]^N^P)"),
50 N_(" Path pattern completion (^N^P)"),
51 N_(" Definition completion (^D^N^P)"),
52 NULL,
53 N_(" Dictionary completion (^K^N^P)"),
54 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000055 N_(" Command-line completion (^V^N^P)"),
56 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000057 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000058 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000059 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010060 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061};
62
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000063static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010064#ifdef FEAT_COMPL_FUNC
65static char e_complwin[] = N_("E839: Completion function changed window");
66static char e_compldel[] = N_("E840: Completion function deleted text");
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
69/*
70 * Structure used to store one match for insert completion.
71 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000072typedef struct compl_S compl_T;
73struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000074{
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 compl_T *cp_next;
76 compl_T *cp_prev;
77 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000078 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000079 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000080 char_u *cp_fname; /* file containing the match, allocated when
81 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000082 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
83 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084};
85
Bram Moolenaar572cb562005-08-05 21:35:02 +000086#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#define FREE_FNAME (2)
88
89/*
90 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000091 * "compl_first_match" points to the start of the list.
92 * "compl_curr_match" points to the currently selected entry.
93 * "compl_shown_match" is different from compl_curr_match during
94 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000096static compl_T *compl_first_match = NULL;
97static compl_T *compl_curr_match = NULL;
98static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000100/* After using a cursor key <Enter> selects a match in the popup menu,
101 * otherwise it inserts a line break. */
102static int compl_enter_selects = FALSE;
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104/* When "compl_leader" is not NULL only matches that start with this string
105 * are used. */
106static char_u *compl_leader = NULL;
107
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000108static int compl_get_longest = FALSE; /* put longest common string
109 in compl_leader */
110
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200111static int compl_no_insert = FALSE; /* FALSE: select & insert
112 TRUE: noinsert */
113static int compl_no_select = FALSE; /* FALSE: select & insert
114 TRUE: noselect */
115
Bram Moolenaara6557602006-02-04 22:43:20 +0000116static int compl_used_match; /* Selected one of the matches. When
117 FALSE the match was edited or using
118 the longest common string. */
119
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000120static int compl_was_interrupted = FALSE; /* didn't finish finding
121 completions. */
122
123static int compl_restarting = FALSE; /* don't insert match */
124
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000125/* When the first completion is done "compl_started" is set. When it's
126 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000127static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000129/* Set when doing something for completion that may call edit() recursively,
130 * which is not allowed. */
131static int compl_busy = FALSE;
132
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static int compl_matches = 0;
134static char_u *compl_pattern = NULL;
135static int compl_direction = FORWARD;
136static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000137static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000138static pos_T compl_startpos;
139static colnr_T compl_col = 0; /* column where the text starts
140 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000141static char_u *compl_orig_text = NULL; /* text as it was before
142 * completion started */
143static int compl_cont_mode = 0;
144static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar82139082011-09-14 16:52:09 +0200146static int compl_opt_refresh_always = FALSE;
147
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100148static void ins_ctrl_x(void);
149static int has_compl_option(int dict_opt);
150static int ins_compl_accept_char(int c);
151static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
152static int ins_compl_equal(compl_T *match, char_u *str, int len);
153static void ins_compl_longest_match(compl_T *match);
154static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
155static int ins_compl_make_cyclic(void);
156static void ins_compl_upd_pum(void);
157static void ins_compl_del_pum(void);
158static int pum_wanted(void);
159static int pum_enough_matches(void);
160static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
161static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
162static char_u *find_line_end(char_u *ptr);
163static void ins_compl_free(void);
164static void ins_compl_clear(void);
165static int ins_compl_bs(void);
166static int ins_compl_need_restart(void);
167static void ins_compl_new_leader(void);
168static void ins_compl_addleader(int c);
169static int ins_compl_len(void);
170static void ins_compl_restart(void);
171static void ins_compl_set_original_text(char_u *str);
172static void ins_compl_addfrommatch(void);
173static int ins_compl_prep(int c);
174static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
175static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000176#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100177static void ins_compl_add_list(list_T *list);
178static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000179#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static int ins_compl_get_exp(pos_T *ini);
181static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200182static void ins_compl_insert(int in_compl_func);
183static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100184static int ins_compl_key2dir(int c);
185static int ins_compl_pum_key(int c);
186static int ins_compl_key2count(int c);
187static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100188static int ins_complete(int c, int enable_pum);
189static void show_pum(int save_w_wrow);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#endif /* FEAT_INS_EXPAND */
192
193#define BACKSPACE_CHAR 1
194#define BACKSPACE_WORD 2
195#define BACKSPACE_WORD_NOT_SPACE 3
196#define BACKSPACE_LINE 4
197
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100198static void ins_redraw(int ready);
199static void ins_ctrl_v(void);
200static void undisplay_dollar(void);
201static void insert_special(int, int, int);
202static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
203static void check_auto_format(int);
204static void redo_literal(int c);
205static void start_arrow(pos_T *end_insert_pos);
206static void start_arrow_with_change(pos_T *end_insert_pos, int change);
207static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000208#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void check_spell_redraw(void);
210static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000211static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
214static int echeck_abbr(int);
215static int replace_pop(void);
216static void replace_join(int off);
217static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void replace_flush(void);
222static void replace_do_bs(int limit_col);
223static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ins_reg(void);
228static void ins_ctrl_g(void);
229static void ins_ctrl_hat(void);
230static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100234static int ins_start_select(int c);
235static void ins_insert(int replaceState);
236static void ins_ctrl_o(void);
237static void ins_shift(int c, int lastc);
238static void ins_del(void);
239static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_mouse(int c);
242static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000244#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000246#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ins_left(int end_change);
248static void ins_home(int c);
249static void ins_end(int c);
250static void ins_s_left(void);
251static void ins_right(int end_change);
252static void ins_s_right(void);
253static void ins_up(int startcol);
254static void ins_pageup(void);
255static void ins_down(int startcol);
256static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static int ins_tab(void);
261static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100270#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274static colnr_T Insstart_textlen; /* length of line when insert started */
275static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100276static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
278static char_u *last_insert = NULL; /* the text of the previous insert,
279 K_SPECIAL and CSI are escaped */
280static int last_insert_skip; /* nr of chars in front of previous insert */
281static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000282static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284#ifdef FEAT_CINDENT
285static int can_cindent; /* may do cindenting on this line */
286#endif
287
288static int old_indent = 0; /* for ^^D command in insert mode */
289
290#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000291static int revins_on; /* reverse insert mode on */
292static int revins_chars; /* how much to skip after edit */
293static int revins_legal; /* was the last char 'legal'? */
294static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static int ins_need_undo; /* call u_save() before inserting a
298 char. Set when edit() is called.
299 after that arrow_used is used. */
300
301static int did_add_space = FALSE; /* auto_format() added an extra space
302 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200303static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
304 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
306/*
307 * edit(): Start inserting text.
308 *
309 * "cmdchar" can be:
310 * 'i' normal insert command
311 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100312 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 * 'R' replace command
314 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
315 * but still only one <CR> is inserted. The <Esc> is not used for redo.
316 * 'g' "gI" command.
317 * 'V' "gR" command for Virtual Replace mode.
318 * 'v' "gr" command for single character Virtual Replace mode.
319 *
320 * This function is not called recursively. For CTRL-O commands, it returns
321 * and lets the caller handle the Normal-mode command.
322 *
323 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
324 */
325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100326edit(
327 int cmdchar,
328 int startln, /* if set, insert at start of line */
329 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330{
331 int c = 0;
332 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100333 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000334 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 int i;
337 int did_backspace = TRUE; /* previous char was backspace */
338#ifdef FEAT_CINDENT
339 int line_is_white = FALSE; /* line is empty before insert */
340#endif
341 linenr_T old_topline = 0; /* topline before insertion */
342#ifdef FEAT_DIFF
343 int old_topfill = -1;
344#endif
345 int inserted_space = FALSE; /* just inserted a space */
346 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000347 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000349 /* Remember whether editing was restarted after CTRL-O. */
350 did_restart_edit = restart_edit;
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 /* sleep before redrawing, needed for "CTRL-O :" that results in an
353 * error message */
354 check_for_delay(TRUE);
355
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100356 /* set Insstart_orig to Insstart */
357 update_Insstart_orig = TRUE;
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef HAVE_SANDBOX
360 /* Don't allow inserting in the sandbox. */
361 if (sandbox != 0)
362 {
363 EMSG(_(e_sandbox));
364 return FALSE;
365 }
366#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000367 /* Don't allow changes in the buffer while editing the cmdline. The
368 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000369 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000370 {
371 EMSG(_(e_secure));
372 return FALSE;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000376 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000377 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000378 {
379 EMSG(_(e_secure));
380 return FALSE;
381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 ins_compl_clear(); /* clear stuff for CTRL-X mode */
383#endif
384
Bram Moolenaar843ee412004-06-30 16:16:41 +0000385#ifdef FEAT_AUTOCMD
386 /*
387 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
388 */
389 if (cmdchar != 'r' && cmdchar != 'v')
390 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100391 pos_T save_cursor = curwin->w_cursor;
392
Bram Moolenaar1e015462005-09-25 22:16:38 +0000393# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000394 if (cmdchar == 'R')
395 ptr = (char_u *)"r";
396 else if (cmdchar == 'V')
397 ptr = (char_u *)"v";
398 else
399 ptr = (char_u *)"i";
400 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200401 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000402# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000403 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100404
Bram Moolenaar097c9922013-05-19 21:15:15 +0200405 /* Make sure the cursor didn't move. Do call check_cursor_col() in
406 * case the text was modified. Since Insert mode was not started yet
407 * a call to check_cursor_col() may move the cursor, especially with
408 * the "A" command, thus set State to avoid that. Also check that the
409 * line number is still valid (lines may have been deleted).
410 * Do not restore if v:char was set to a non-empty string. */
411 if (!equalpos(curwin->w_cursor, save_cursor)
412# ifdef FEAT_EVAL
413 && *get_vim_var_str(VV_CHAR) == NUL
414# endif
415 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100416 {
417 int save_state = State;
418
419 curwin->w_cursor = save_cursor;
420 State = INSERT;
421 check_cursor_col();
422 State = save_state;
423 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000424 }
425#endif
426
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200427#ifdef FEAT_CONCEAL
428 /* Check if the cursor line needs redrawing before changing State. If
429 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200430 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200431#endif
432
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433#ifdef FEAT_MOUSE
434 /*
435 * When doing a paste with the middle mouse button, Insstart is set to
436 * where the paste started.
437 */
438 if (where_paste_started.lnum != 0)
439 Insstart = where_paste_started;
440 else
441#endif
442 {
443 Insstart = curwin->w_cursor;
444 if (startln)
445 Insstart.col = 0;
446 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000447 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 Insstart_blank_vcol = MAXCOL;
449 if (!did_ai)
450 ai_col = 0;
451
452 if (cmdchar != NUL && restart_edit == 0)
453 {
454 ResetRedobuff();
455 AppendNumberToRedobuff(count);
456#ifdef FEAT_VREPLACE
457 if (cmdchar == 'V' || cmdchar == 'v')
458 {
459 /* "gR" or "gr" command */
460 AppendCharToRedobuff('g');
461 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
462 }
463 else
464#endif
465 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100466 if (cmdchar == K_PS)
467 AppendCharToRedobuff('a');
468 else
469 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 if (cmdchar == 'g') /* "gI" command */
471 AppendCharToRedobuff('I');
472 else if (cmdchar == 'r') /* "r<CR>" command */
473 count = 1; /* insert only one <CR> */
474 }
475 }
476
477 if (cmdchar == 'R')
478 {
479#ifdef FEAT_FKMAP
480 if (p_fkmap && p_ri)
481 {
482 beep_flush();
483 EMSG(farsi_text_3); /* encoded in Farsi */
484 State = INSERT;
485 }
486 else
487#endif
488 State = REPLACE;
489 }
490#ifdef FEAT_VREPLACE
491 else if (cmdchar == 'V' || cmdchar == 'v')
492 {
493 State = VREPLACE;
494 replaceState = VREPLACE;
495 orig_line_count = curbuf->b_ml.ml_line_count;
496 vr_lines_changed = 1;
497 }
498#endif
499 else
500 State = INSERT;
501
502 stop_insert_mode = FALSE;
503
504 /*
505 * Need to recompute the cursor position, it might move when the cursor is
506 * on a TAB or special character.
507 */
508 curs_columns(TRUE);
509
510 /*
511 * Enable langmap or IME, indicated by 'iminsert'.
512 * Note that IME may enabled/disabled without us noticing here, thus the
513 * 'iminsert' value may not reflect what is actually used. It is updated
514 * when hitting <Esc>.
515 */
516 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
517 State |= LANGMAP;
518#ifdef USE_IM_CONTROL
519 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
520#endif
521
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522#ifdef FEAT_MOUSE
523 setmouse();
524#endif
525#ifdef FEAT_CMDL_INFO
526 clear_showcmd();
527#endif
528#ifdef FEAT_RIGHTLEFT
529 /* there is no reverse replace mode */
530 revins_on = (State == INSERT && p_ri);
531 if (revins_on)
532 undisplay_dollar();
533 revins_chars = 0;
534 revins_legal = 0;
535 revins_scol = -1;
536#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100537 if (!p_ek)
538 /* Disable bracketed paste mode, we won't recognize the escape
539 * sequences. */
540 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542 /*
543 * Handle restarting Insert mode.
544 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
545 * restart_edit non-zero, and something in the stuff buffer.
546 */
547 if (restart_edit != 0 && stuff_empty())
548 {
549#ifdef FEAT_MOUSE
550 /*
551 * After a paste we consider text typed to be part of the insert for
552 * the pasted text. You can backspace over the pasted text too.
553 */
554 if (where_paste_started.lnum)
555 arrow_used = FALSE;
556 else
557#endif
558 arrow_used = TRUE;
559 restart_edit = 0;
560
561 /*
562 * If the cursor was after the end-of-line before the CTRL-O and it is
563 * now at the end-of-line, put it after the end-of-line (this is not
564 * correct in very rare cases).
565 * Also do this if curswant is greater than the current virtual
566 * column. Eg after "^O$" or "^O80|".
567 */
568 validate_virtcol();
569 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000570 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 || curwin->w_curswant > curwin->w_virtcol)
572 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
573 {
574 if (ptr[1] == NUL)
575 ++curwin->w_cursor.col;
576#ifdef FEAT_MBYTE
577 else if (has_mbyte)
578 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000579 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (ptr[i] == NUL)
581 curwin->w_cursor.col += i;
582 }
583#endif
584 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000585 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 else
588 arrow_used = FALSE;
589
590 /* we are in insert mode now, don't need to start it anymore */
591 need_start_insertmode = FALSE;
592
593 /* Need to save the line for undo before inserting the first char. */
594 ins_need_undo = TRUE;
595
596#ifdef FEAT_MOUSE
597 where_paste_started.lnum = 0;
598#endif
599#ifdef FEAT_CINDENT
600 can_cindent = TRUE;
601#endif
602#ifdef FEAT_FOLDING
603 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
604 * restarting. */
605 if (!p_im && did_restart_edit == 0)
606 foldOpenCursor();
607#endif
608
609 /*
610 * If 'showmode' is set, show the current (insert/replace/..) mode.
611 * A warning message for changing a readonly file is given here, before
612 * actually changing anything. It's put after the mode, if any.
613 */
614 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000615 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 i = showmode();
617
618 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000619 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621#ifdef CURSOR_SHAPE
622 ui_cursor_shape(); /* may show different cursor shape */
623#endif
624#ifdef FEAT_DIGRAPHS
625 do_digraph(-1); /* clear digraphs */
626#endif
627
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000628 /*
629 * Get the current length of the redo buffer, those characters have to be
630 * skipped if we want to get to the inserted characters.
631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 ptr = get_inserted();
633 if (ptr == NULL)
634 new_insert_skip = 0;
635 else
636 {
637 new_insert_skip = (int)STRLEN(ptr);
638 vim_free(ptr);
639 }
640
641 old_indent = 0;
642
643 /*
644 * Main loop in Insert mode: repeat until Insert mode is left.
645 */
646 for (;;)
647 {
648#ifdef FEAT_RIGHTLEFT
649 if (!revins_legal)
650 revins_scol = -1; /* reset on illegal motions */
651 else
652 revins_legal = 0;
653#endif
654 if (arrow_used) /* don't repeat insert when arrow key used */
655 count = 0;
656
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100657 if (update_Insstart_orig)
658 Insstart_orig = Insstart;
659
Bram Moolenaar00672e12016-06-26 18:38:13 +0200660 if (stop_insert_mode
661#ifdef FEAT_INS_EXPAND
662 && !pum_visible()
663#endif
664 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 {
666 /* ":stopinsert" used or 'insertmode' reset */
667 count = 0;
668 goto doESCkey;
669 }
670
671 /* set curwin->w_curswant for next K_DOWN or K_UP */
672 if (!arrow_used)
673 curwin->w_set_curswant = TRUE;
674
675 /* If there is no typeahead may check for timestamps (e.g., for when a
676 * menu invoked a shell command). */
677 if (stuff_empty())
678 {
679 did_check_timestamps = FALSE;
680 if (need_check_timestamps)
681 check_timestamps(FALSE);
682 }
683
684 /*
685 * When emsg() was called msg_scroll will have been set.
686 */
687 msg_scroll = FALSE;
688
689#ifdef FEAT_GUI
690 /* When 'mousefocus' is set a mouse movement may have taken us to
691 * another window. "need_mouse_correct" may then be set because of an
692 * autocommand. */
693 if (need_mouse_correct)
694 gui_mouse_correct();
695#endif
696
697#ifdef FEAT_FOLDING
698 /* Open fold at the cursor line, according to 'foldopen'. */
699 if (fdo_flags & FDO_INSERT)
700 foldOpenCursor();
701 /* Close folds where the cursor isn't, according to 'foldclose' */
702 if (!char_avail())
703 foldCheckClose();
704#endif
705
706 /*
707 * If we inserted a character at the last position of the last line in
708 * the window, scroll the window one line up. This avoids an extra
709 * redraw.
710 * This is detected when the cursor column is smaller after inserting
711 * something.
712 * Don't do this when the topline changed already, it has
713 * already been adjusted (by insertchar() calling open_line())).
714 */
715 if (curbuf->b_mod_set
716 && curwin->w_p_wrap
717 && !did_backspace
718 && curwin->w_topline == old_topline
719#ifdef FEAT_DIFF
720 && curwin->w_topfill == old_topfill
721#endif
722 )
723 {
724 mincol = curwin->w_wcol;
725 validate_cursor_col();
726
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000727 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 && curwin->w_wrow == W_WINROW(curwin)
729 + curwin->w_height - 1 - p_so
730 && (curwin->w_cursor.lnum != curwin->w_topline
731#ifdef FEAT_DIFF
732 || curwin->w_topfill > 0
733#endif
734 ))
735 {
736#ifdef FEAT_DIFF
737 if (curwin->w_topfill > 0)
738 --curwin->w_topfill;
739 else
740#endif
741#ifdef FEAT_FOLDING
742 if (hasFolding(curwin->w_topline, NULL, &old_topline))
743 set_topline(curwin, old_topline + 1);
744 else
745#endif
746 set_topline(curwin, curwin->w_topline + 1);
747 }
748 }
749
750 /* May need to adjust w_topline to show the cursor. */
751 update_topline();
752
753 did_backspace = FALSE;
754
755 validate_cursor(); /* may set must_redraw */
756
757 /*
758 * Redraw the display when no characters are waiting.
759 * Also shows mode, ruler and positions cursor.
760 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000761 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
763#ifdef FEAT_SCROLLBIND
764 if (curwin->w_p_scb)
765 do_check_scrollbind(TRUE);
766#endif
767
Bram Moolenaar860cae12010-06-05 23:22:07 +0200768#ifdef FEAT_CURSORBIND
769 if (curwin->w_p_crb)
770 do_check_cursorbind();
771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 update_curswant();
773 old_topline = curwin->w_topline;
774#ifdef FEAT_DIFF
775 old_topfill = curwin->w_topfill;
776#endif
777
778#ifdef USE_ON_FLY_SCROLL
779 dont_scroll = FALSE; /* allow scrolling here */
780#endif
781
782 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000783 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100785 if (c != K_CURSORHOLD)
786 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200787
788 /* After using CTRL-G U the next cursor key will not break undo. */
789 if (dont_sync_undo == MAYBE)
790 dont_sync_undo = TRUE;
791 else
792 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100793 if (cmdchar == K_PS)
794 /* Got here from normal mode when bracketed paste started. */
795 c = K_PS;
796 else
797 do
798 {
799 c = safe_vgetc();
800 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000802#ifdef FEAT_AUTOCMD
803 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
804 did_cursorhold = TRUE;
805#endif
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#ifdef FEAT_RIGHTLEFT
808 if (p_hkmap && KeyTyped)
809 c = hkmap(c); /* Hebrew mode mapping */
810#endif
811#ifdef FEAT_FKMAP
812 if (p_fkmap && KeyTyped)
813 c = fkmap(c); /* Farsi mode mapping */
814#endif
815
816#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000817 /*
818 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000819 * and the cursor is still in the completed word. Only when there is
820 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000821 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000822 if (compl_started
823 && pum_wanted()
824 && curwin->w_cursor.col >= compl_col
825 && (compl_shown_match == NULL
826 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000827 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000828 /* BS: Delete one character from "compl_leader". */
829 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000830 && curwin->w_cursor.col > compl_col
831 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000832 continue;
833
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000834 /* When no match was selected or it was edited. */
835 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000836 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000837 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000838 * "compl_leader". Except when at the original match and
839 * there is nothing to add, CTRL-L works like CTRL-P then. */
840 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100841 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000842 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000843 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 {
845 ins_compl_addfrommatch();
846 continue;
847 }
848
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000849 /* A non-white character that fits in with the current
850 * completion: Add to "compl_leader". */
851 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000852 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100853#ifdef FEAT_AUTOCMD
854 /* Trigger InsertCharPre. */
855 char_u *str = do_insert_char_pre(c);
856 char_u *p;
857
858 if (str != NULL)
859 {
860 for (p = str; *p != NUL; mb_ptr_adv(p))
861 ins_compl_addleader(PTR2CHAR(p));
862 vim_free(str);
863 }
864 else
865#endif
866 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000867 continue;
868 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000869
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000870 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000871 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200872 if ((c == Ctrl_Y || (compl_enter_selects
873 && (c == CAR || c == K_KENTER || c == NL)))
874 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000875 {
876 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200877 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000878 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000879 }
880 }
881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
883 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000884 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000885 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000886 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#endif
888
Bram Moolenaar488c6512005-08-11 20:09:58 +0000889 /* CTRL-\ CTRL-N goes to Normal mode,
890 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
891 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 if (c == Ctrl_BSL)
893 {
894 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000895 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 ++no_mapping;
897 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000898 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 --no_mapping;
900 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000901 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000903 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 vungetc(c);
905 c = Ctrl_BSL;
906 }
907 else if (c == Ctrl_G && p_im)
908 continue;
909 else
910 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000911 if (c == Ctrl_O)
912 {
913 ins_ctrl_o();
914 ins_at_eol = FALSE; /* cursor keeps its column */
915 nomove = TRUE;
916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 count = 0;
918 goto doESCkey;
919 }
920 }
921
922#ifdef FEAT_DIGRAPHS
923 c = do_digraph(c);
924#endif
925
926#ifdef FEAT_INS_EXPAND
927 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
928 goto docomplete;
929#endif
930 if (c == Ctrl_V || c == Ctrl_Q)
931 {
932 ins_ctrl_v();
933 c = Ctrl_V; /* pretend CTRL-V is last typed character */
934 continue;
935 }
936
937#ifdef FEAT_CINDENT
938 if (cindent_on()
939# ifdef FEAT_INS_EXPAND
940 && ctrl_x_mode == 0
941# endif
942 )
943 {
944 /* A key name preceded by a bang means this key is not to be
945 * inserted. Skip ahead to the re-indenting below.
946 * A key name preceded by a star means that indenting has to be
947 * done before inserting the key. */
948 line_is_white = inindent(0);
949 if (in_cinkeys(c, '!', line_is_white))
950 goto force_cindent;
951 if (can_cindent && in_cinkeys(c, '*', line_is_white)
952 && stop_arrow() == OK)
953 do_c_expr_indent();
954 }
955#endif
956
957#ifdef FEAT_RIGHTLEFT
958 if (curwin->w_p_rl)
959 switch (c)
960 {
961 case K_LEFT: c = K_RIGHT; break;
962 case K_S_LEFT: c = K_S_RIGHT; break;
963 case K_C_LEFT: c = K_C_RIGHT; break;
964 case K_RIGHT: c = K_LEFT; break;
965 case K_S_RIGHT: c = K_S_LEFT; break;
966 case K_C_RIGHT: c = K_C_LEFT; break;
967 }
968#endif
969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 /*
971 * If 'keymodel' contains "startsel", may start selection. If it
972 * does, a CTRL-O and c will be stuffed, we need to get these
973 * characters.
974 */
975 if (ins_start_select(c))
976 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 /*
979 * The big switch to handle a character in insert mode.
980 */
981 switch (c)
982 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000983 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (echeck_abbr(ESC + ABBR_OFF))
985 break;
986 /*FALLTHROUGH*/
987
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#ifdef FEAT_CMDWIN
990 if (c == Ctrl_C && cmdwin_type != 0)
991 {
992 /* Close the cmdline window. */
993 cmdwin_result = K_IGNORE;
994 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000995 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 goto doESCkey;
997 }
998#endif
999
1000#ifdef UNIX
1001do_intr:
1002#endif
1003 /* when 'insertmode' set, and not halfway a mapping, don't leave
1004 * Insert mode */
1005 if (goto_im())
1006 {
1007 if (got_int)
1008 {
1009 (void)vgetc(); /* flush all buffers */
1010 got_int = FALSE;
1011 }
1012 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001013 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 break;
1015 }
1016doESCkey:
1017 /*
1018 * This is the ONLY return from edit()!
1019 */
1020 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1021 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001022 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 o_lnum = curwin->w_cursor.lnum;
1024
Bram Moolenaar488c6512005-08-11 20:09:58 +00001025 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001026 {
1027#ifdef FEAT_AUTOCMD
1028 if (cmdchar != 'r' && cmdchar != 'v')
1029 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1030 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001031 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 continue;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case Ctrl_Z: /* suspend when 'insertmode' set */
1038 if (!p_im)
1039 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001040 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001041 c = Ctrl_O;
1042 /*FALLTHROUGH*/
1043
1044 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001045#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001046 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001047 goto docomplete;
1048#endif
1049 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1050 break;
1051 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001052
1053#ifdef FEAT_VIRTUALEDIT
1054 /* don't move the cursor left when 'virtualedit' has "onemore". */
1055 if (ve_flags & VE_ONEMORE)
1056 {
1057 ins_at_eol = FALSE;
1058 nomove = TRUE;
1059 }
1060#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 count = 0;
1062 goto doESCkey;
1063
Bram Moolenaar572cb562005-08-05 21:35:02 +00001064 case K_INS: /* toggle insert/replace mode */
1065 case K_KINS:
1066 ins_insert(replaceState);
1067 break;
1068
1069 case K_SELECT: /* end of Select mode mapping - ignore */
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case K_HELP: /* Help key works like <ESC> <Help> */
1073 case K_F1:
1074 case K_XF1:
1075 stuffcharReadbuff(K_HELP);
1076 if (p_im)
1077 need_start_insertmode = TRUE;
1078 goto doESCkey;
1079
1080#ifdef FEAT_NETBEANS_INTG
1081 case K_F21: /* NetBeans command */
1082 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001083 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001084 --no_mapping;
1085 netbeans_keycommand(i);
1086 break;
1087#endif
1088
1089 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 case NUL:
1091 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 /* For ^@ the trailing ESC will end the insert, unless there is an
1093 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1095 && c != Ctrl_A && !p_im)
1096 goto doESCkey; /* quit insert mode */
1097 inserted_space = FALSE;
1098 break;
1099
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 ins_reg();
1102 auto_format(FALSE, TRUE);
1103 inserted_space = FALSE;
1104 break;
1105
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001106 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 ins_ctrl_g();
1108 break;
1109
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001110 case Ctrl_HAT: /* switch input mode and/or langmap */
1111 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 break;
1113
1114#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (!p_ari)
1117 goto normalchar;
1118 ins_ctrl_();
1119 break;
1120#endif
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1124 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1125 goto docomplete;
1126#endif
1127 /* FALLTHROUGH */
1128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001129 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130# ifdef FEAT_INS_EXPAND
1131 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1132 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001133 if (has_compl_option(FALSE))
1134 goto docomplete;
1135 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
1137# endif
1138 ins_shift(c, lastc);
1139 auto_format(FALSE, TRUE);
1140 inserted_space = FALSE;
1141 break;
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 case K_KDEL:
1145 ins_del();
1146 auto_format(FALSE, TRUE);
1147 break;
1148
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001149 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 case Ctrl_H:
1151 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1152 auto_format(FALSE, TRUE);
1153 break;
1154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1157 auto_format(FALSE, TRUE);
1158 break;
1159
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001160 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001161# ifdef FEAT_COMPL_FUNC
1162 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001164 goto docomplete;
1165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1167 auto_format(FALSE, TRUE);
1168 inserted_space = FALSE;
1169 break;
1170
1171#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001172 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 case K_LEFTMOUSE_NM:
1174 case K_LEFTDRAG:
1175 case K_LEFTRELEASE:
1176 case K_LEFTRELEASE_NM:
1177 case K_MIDDLEMOUSE:
1178 case K_MIDDLEDRAG:
1179 case K_MIDDLERELEASE:
1180 case K_RIGHTMOUSE:
1181 case K_RIGHTDRAG:
1182 case K_RIGHTRELEASE:
1183 case K_X1MOUSE:
1184 case K_X1DRAG:
1185 case K_X1RELEASE:
1186 case K_X2MOUSE:
1187 case K_X2DRAG:
1188 case K_X2RELEASE:
1189 ins_mouse(c);
1190 break;
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001193 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 break;
1195
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001196 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001197 ins_mousescroll(MSCR_UP);
1198 break;
1199
1200 case K_MOUSELEFT: /* Scroll wheel left */
1201 ins_mousescroll(MSCR_LEFT);
1202 break;
1203
1204 case K_MOUSERIGHT: /* Scroll wheel right */
1205 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 break;
1207#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001208 case K_PS:
1209 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1210 if (cmdchar == K_PS)
1211 /* invoked from normal mode, bail out */
1212 goto doESCkey;
1213 break;
1214 case K_PE:
1215 /* Got K_PE without K_PS, ignore. */
1216 break;
1217
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001218#ifdef FEAT_GUI_TABLINE
1219 case K_TABLINE:
1220 case K_TABMENU:
1221 ins_tabline(c);
1222 break;
1223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 break;
1227
Bram Moolenaar754b5602006-02-09 23:53:20 +00001228#ifdef FEAT_AUTOCMD
1229 case K_CURSORHOLD: /* Didn't type something for a while. */
1230 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1231 did_cursorhold = TRUE;
1232 break;
1233#endif
1234
Bram Moolenaar4770d092006-01-12 23:22:24 +00001235#ifdef FEAT_GUI_W32
1236 /* On Win32 ignore <M-F4>, we get it when closing the window was
1237 * cancelled. */
1238 case K_F4:
1239 if (mod_mask != MOD_MASK_ALT)
1240 goto normalchar;
1241 break;
1242#endif
1243
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244#ifdef FEAT_GUI
1245 case K_VER_SCROLLBAR:
1246 ins_scroll();
1247 break;
1248
1249 case K_HOR_SCROLLBAR:
1250 ins_horscroll();
1251 break;
1252#endif
1253
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001254 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 case K_S_HOME:
1257 case K_C_HOME:
1258 ins_home(c);
1259 break;
1260
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001261 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 case K_S_END:
1264 case K_C_END:
1265 ins_end(c);
1266 break;
1267
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001268 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001269 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1270 ins_s_left();
1271 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001272 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 break;
1274
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001275 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 case K_C_LEFT:
1277 ins_s_left();
1278 break;
1279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001280 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001281 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1282 ins_s_right();
1283 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001284 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 break;
1286
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001287 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 case K_C_RIGHT:
1289 ins_s_right();
1290 break;
1291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001292 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001293#ifdef FEAT_INS_EXPAND
1294 if (pum_visible())
1295 goto docomplete;
1296#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001297 if (mod_mask & MOD_MASK_SHIFT)
1298 ins_pageup();
1299 else
1300 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 break;
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 case K_PAGEUP:
1305 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001306#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001307 if (pum_visible())
1308 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 ins_pageup();
1311 break;
1312
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001313 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001314#ifdef FEAT_INS_EXPAND
1315 if (pum_visible())
1316 goto docomplete;
1317#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001318 if (mod_mask & MOD_MASK_SHIFT)
1319 ins_pagedown();
1320 else
1321 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 break;
1323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001324 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 case K_PAGEDOWN:
1326 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001327#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001328 if (pum_visible())
1329 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 ins_pagedown();
1332 break;
1333
1334#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001335 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 ins_drop();
1337 break;
1338#endif
1339
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001340 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 c = TAB;
1342 /* FALLTHROUGH */
1343
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1346 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1347 goto docomplete;
1348#endif
1349 inserted_space = FALSE;
1350 if (ins_tab())
1351 goto normalchar; /* insert TAB as a normal char */
1352 auto_format(FALSE, TRUE);
1353 break;
1354
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001355 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 c = CAR;
1357 /* FALLTHROUGH */
1358 case CAR:
1359 case NL:
1360#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1361 /* In a quickfix window a <CR> jumps to the error under the
1362 * cursor. */
1363 if (bt_quickfix(curbuf) && c == CAR)
1364 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001365 if (curwin->w_llist_ref == NULL) /* quickfix window */
1366 do_cmdline_cmd((char_u *)".cc");
1367 else /* location list window */
1368 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 break;
1370 }
1371#endif
1372#ifdef FEAT_CMDWIN
1373 if (cmdwin_type != 0)
1374 {
1375 /* Execute the command in the cmdline window. */
1376 cmdwin_result = CAR;
1377 goto doESCkey;
1378 }
1379#endif
1380 if (ins_eol(c) && !p_im)
1381 goto doESCkey; /* out of memory */
1382 auto_format(FALSE, FALSE);
1383 inserted_space = FALSE;
1384 break;
1385
Bram Moolenaar860cae12010-06-05 23:22:07 +02001386#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001387 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388# ifdef FEAT_INS_EXPAND
1389 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1390 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001391 if (has_compl_option(TRUE))
1392 goto docomplete;
1393 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395# endif
1396# ifdef FEAT_DIGRAPHS
1397 c = ins_digraph();
1398 if (c == NUL)
1399 break;
1400# endif
1401 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
1404#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001405 case Ctrl_X: /* Enter CTRL-X mode */
1406 ins_ctrl_x();
1407 break;
1408
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001409 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (ctrl_x_mode != CTRL_X_TAGS)
1411 goto normalchar;
1412 goto docomplete;
1413
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001414 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (ctrl_x_mode != CTRL_X_FILES)
1416 goto normalchar;
1417 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001418
1419 case 's': /* Spelling completion after ^X */
1420 case Ctrl_S:
1421 if (ctrl_x_mode != CTRL_X_SPELL)
1422 goto normalchar;
1423 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424#endif
1425
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001426 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427#ifdef FEAT_INS_EXPAND
1428 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1429#endif
1430 {
1431 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1432 if (p_im)
1433 {
1434 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1435 break;
1436 goto doESCkey;
1437 }
1438 goto normalchar;
1439 }
1440#ifdef FEAT_INS_EXPAND
1441 /* FALLTHROUGH */
1442
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001443 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 case Ctrl_N:
1445 /* if 'complete' is empty then plain ^P is no longer special,
1446 * but it is under other ^X modes */
1447 if (*curbuf->b_p_cpt == NUL
1448 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001449 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 goto normalchar;
1451
1452docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001453 compl_busy = TRUE;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001454 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001455 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001456 compl_cont_status = 0;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001457 disable_fold_update--;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001458 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 break;
1460#endif /* FEAT_INS_EXPAND */
1461
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001462 case Ctrl_Y: /* copy from previous line or scroll down */
1463 case Ctrl_E: /* copy from next line or scroll up */
1464 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 break;
1466
1467 default:
1468#ifdef UNIX
1469 if (c == intr_char) /* special interrupt char */
1470 goto do_intr;
1471#endif
1472
Bram Moolenaare659c952011-05-19 17:25:41 +02001473normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001475 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001477#ifdef FEAT_AUTOCMD
1478 if (!p_paste)
1479 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001480 /* Trigger InsertCharPre. */
1481 char_u *str = do_insert_char_pre(c);
1482 char_u *p;
1483
1484 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001485 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001486 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001487 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001488 /* Insert the new value of v:char literally. */
1489 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001490 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001491 c = PTR2CHAR(p);
1492 if (c == CAR || c == K_KENTER || c == NL)
1493 ins_eol(c);
1494 else
1495 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001496 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001497 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001498 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001499 vim_free(str);
1500 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001501 }
1502
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001503 /* If the new value is already inserted or an empty string
1504 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001505 if (c == NUL)
1506 break;
1507 }
1508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509#ifdef FEAT_SMARTINDENT
1510 /* Try to perform smart-indenting. */
1511 ins_try_si(c);
1512#endif
1513
1514 if (c == ' ')
1515 {
1516 inserted_space = TRUE;
1517#ifdef FEAT_CINDENT
1518 if (inindent(0))
1519 can_cindent = FALSE;
1520#endif
1521 if (Insstart_blank_vcol == MAXCOL
1522 && curwin->w_cursor.lnum == Insstart.lnum)
1523 Insstart_blank_vcol = get_nolist_virtcol();
1524 }
1525
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001526 /* Insert a normal character and check for abbreviations on a
1527 * special character. Let CTRL-] expand abbreviations without
1528 * inserting it. */
1529 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530#ifdef FEAT_MBYTE
1531 /* Add ABBR_OFF for characters above 0x100, this is
1532 * what check_abbr() expects. */
1533 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1534#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001535 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 insert_special(c, FALSE, FALSE);
1538#ifdef FEAT_RIGHTLEFT
1539 revins_legal++;
1540 revins_chars++;
1541#endif
1542 }
1543
1544 auto_format(FALSE, TRUE);
1545
1546#ifdef FEAT_FOLDING
1547 /* When inserting a character the cursor line must never be in a
1548 * closed fold. */
1549 foldOpenCursor();
1550#endif
1551 break;
1552 } /* end of switch (c) */
1553
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001554#ifdef FEAT_AUTOCMD
1555 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001556 if (c != K_CURSORHOLD
1557# ifdef FEAT_COMPL_FUNC
1558 /* but not in CTRL-X mode, a script can't restore the state */
1559 && ctrl_x_mode == 0
1560# endif
1561 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001562 did_cursorhold = FALSE;
1563#endif
1564
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 /* If the cursor was moved we didn't just insert a space */
1566 if (arrow_used)
1567 inserted_space = FALSE;
1568
1569#ifdef FEAT_CINDENT
1570 if (can_cindent && cindent_on()
1571# ifdef FEAT_INS_EXPAND
1572 && ctrl_x_mode == 0
1573# endif
1574 )
1575 {
1576force_cindent:
1577 /*
1578 * Indent now if a key was typed that is in 'cinkeys'.
1579 */
1580 if (in_cinkeys(c, ' ', line_is_white))
1581 {
1582 if (stop_arrow() == OK)
1583 /* re-indent the current line */
1584 do_c_expr_indent();
1585 }
1586 }
1587#endif /* FEAT_CINDENT */
1588
1589 } /* for (;;) */
1590 /* NOTREACHED */
1591}
1592
1593/*
1594 * Redraw for Insert mode.
1595 * This is postponed until getting the next character to make '$' in the 'cpo'
1596 * option work correctly.
1597 * Only redraw when there are no characters available. This speeds up
1598 * inserting sequences of characters (e.g., for CTRL-R).
1599 */
1600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601ins_redraw(
1602 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001604#ifdef FEAT_CONCEAL
1605 linenr_T conceal_old_cursor_line = 0;
1606 linenr_T conceal_new_cursor_line = 0;
1607 int conceal_update_lines = FALSE;
1608#endif
1609
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001610 if (char_avail())
1611 return;
1612
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001613#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001614 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1615 * visible, the command might delete it. */
1616 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001617# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001618 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001619# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001620# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001621 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001622# endif
1623# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001625# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001626 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001627# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001628 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001629# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001630# ifdef FEAT_INS_EXPAND
1631 && !pum_visible()
1632# endif
1633 )
1634 {
1635# ifdef FEAT_SYN_HL
1636 /* Need to update the screen first, to make sure syntax
1637 * highlighting is correct after making a change (e.g., inserting
1638 * a "(". The autocommand may also require a redraw, so it's done
1639 * again below, unfortunately. */
1640 if (syntax_present(curwin) && must_redraw)
1641 update_screen(0);
1642# endif
1643# ifdef FEAT_AUTOCMD
1644 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001645 {
1646 /* Make sure curswant is correct, an autocommand may call
1647 * getcurpos(). */
1648 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001649 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001650 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001651# endif
1652# ifdef FEAT_CONCEAL
1653 if (curwin->w_p_cole > 0)
1654 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001655# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001656 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001657# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001658 conceal_new_cursor_line = curwin->w_cursor.lnum;
1659 conceal_update_lines = TRUE;
1660 }
1661# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001662# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001663 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001664# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665 }
1666#endif
1667
1668#ifdef FEAT_AUTOCMD
1669 /* Trigger TextChangedI if b_changedtick differs. */
1670 if (ready && has_textchangedI()
Bram Moolenaar79518e22017-02-17 16:31:35 +01001671 && last_changedtick != *curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001672# ifdef FEAT_INS_EXPAND
1673 && !pum_visible()
1674# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001675 )
1676 {
1677 if (last_changedtick_buf == curbuf)
1678 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1679 last_changedtick_buf = curbuf;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001680 last_changedtick = *curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001682#endif
1683
1684 if (must_redraw)
1685 update_screen(0);
1686 else if (clear_cmdline || redraw_cmdline)
1687 showmode(); /* clear cmdline and show mode */
1688# if defined(FEAT_CONCEAL)
1689 if ((conceal_update_lines
1690 && (conceal_old_cursor_line != conceal_new_cursor_line
1691 || conceal_cursor_line(curwin)))
1692 || need_cursor_line_redraw)
1693 {
1694 if (conceal_old_cursor_line != conceal_new_cursor_line)
1695 update_single_line(curwin, conceal_old_cursor_line);
1696 update_single_line(curwin, conceal_new_cursor_line == 0
1697 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1698 curwin->w_valid &= ~VALID_CROW;
1699 }
1700# endif
1701 showruler(FALSE);
1702 setcursor();
1703 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704}
1705
1706/*
1707 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1708 */
1709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001710ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
1712 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001713 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001716 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
1718 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001721 did_putchar = TRUE;
1722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1724
1725#ifdef FEAT_CMDL_INFO
1726 add_to_showcmd_c(Ctrl_V);
1727#endif
1728
1729 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001730 if (did_putchar)
1731 /* when the line fits in 'columns' the '^' is at the start of the next
1732 * line and will not removed by the redraw */
1733 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734#ifdef FEAT_CMDL_INFO
1735 clear_showcmd();
1736#endif
1737 insert_special(c, FALSE, TRUE);
1738#ifdef FEAT_RIGHTLEFT
1739 revins_chars++;
1740 revins_legal++;
1741#endif
1742}
1743
1744/*
1745 * Put a character directly onto the screen. It's not stored in a buffer.
1746 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1747 */
1748static int pc_status;
1749#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1750#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1751#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1752#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754static int pc_attr;
1755static int pc_row;
1756static int pc_col;
1757
1758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001759edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 int attr;
1762
1763 if (ScreenLines != NULL)
1764 {
1765 update_topline(); /* just in case w_topline isn't valid */
1766 validate_cursor();
1767 if (highlight)
1768 attr = hl_attr(HLF_8);
1769 else
1770 attr = 0;
1771 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1772 pc_col = W_WINCOL(curwin);
1773#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1774 pc_status = PC_STATUS_UNSET;
1775#endif
1776#ifdef FEAT_RIGHTLEFT
1777 if (curwin->w_p_rl)
1778 {
1779 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1780# ifdef FEAT_MBYTE
1781 if (has_mbyte)
1782 {
1783 int fix_col = mb_fix_col(pc_col, pc_row);
1784
1785 if (fix_col != pc_col)
1786 {
1787 screen_putchar(' ', pc_row, fix_col, attr);
1788 --curwin->w_wcol;
1789 pc_status = PC_STATUS_RIGHT;
1790 }
1791 }
1792# endif
1793 }
1794 else
1795#endif
1796 {
1797 pc_col += curwin->w_wcol;
1798#ifdef FEAT_MBYTE
1799 if (mb_lefthalve(pc_row, pc_col))
1800 pc_status = PC_STATUS_LEFT;
1801#endif
1802 }
1803
1804 /* save the character to be able to put it back */
1805#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1806 if (pc_status == PC_STATUS_UNSET)
1807#endif
1808 {
1809 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1810 pc_status = PC_STATUS_SET;
1811 }
1812 screen_putchar(c, pc_row, pc_col, attr);
1813 }
1814}
1815
1816/*
1817 * Undo the previous edit_putchar().
1818 */
1819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001820edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821{
1822 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1823 {
1824#if defined(FEAT_MBYTE)
1825 if (pc_status == PC_STATUS_RIGHT)
1826 ++curwin->w_wcol;
1827 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1828 redrawWinline(curwin->w_cursor.lnum, FALSE);
1829 else
1830#endif
1831 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1832 }
1833}
1834
1835/*
1836 * Called when p_dollar is set: display a '$' at the end of the changed text
1837 * Only works when cursor is in the line that changes.
1838 */
1839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
1842 colnr_T save_col;
1843
1844 if (!redrawing())
1845 return;
1846
1847 cursor_off();
1848 save_col = curwin->w_cursor.col;
1849 curwin->w_cursor.col = col;
1850#ifdef FEAT_MBYTE
1851 if (has_mbyte)
1852 {
1853 char_u *p;
1854
1855 /* If on the last byte of a multi-byte move to the first byte. */
1856 p = ml_get_curline();
1857 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1858 }
1859#endif
1860 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1861 if (curwin->w_wcol < W_WIDTH(curwin))
1862 {
1863 edit_putchar('$', FALSE);
1864 dollar_vcol = curwin->w_virtcol;
1865 }
1866 curwin->w_cursor.col = save_col;
1867}
1868
1869/*
1870 * Call this function before moving the cursor from the normal insert position
1871 * in insert mode.
1872 */
1873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001874undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001876 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001878 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 redrawWinline(curwin->w_cursor.lnum, FALSE);
1880 }
1881}
1882
1883/*
1884 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1885 * Keep the cursor on the same character.
1886 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1887 * type == INDENT_DEC decrease indent (for CTRL-D)
1888 * type == INDENT_SET set indent to "amount"
1889 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1890 */
1891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001892change_indent(
1893 int type,
1894 int amount,
1895 int round,
1896 int replaced, /* replaced character, put on replace stack */
1897 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898{
1899 int vcol;
1900 int last_vcol;
1901 int insstart_less; /* reduction for Insstart.col */
1902 int new_cursor_col;
1903 int i;
1904 char_u *ptr;
1905 int save_p_list;
1906 int start_col;
1907 colnr_T vc;
1908#ifdef FEAT_VREPLACE
1909 colnr_T orig_col = 0; /* init for GCC */
1910 char_u *new_line, *orig_line = NULL; /* init for GCC */
1911
1912 /* VREPLACE mode needs to know what the line was like before changing */
1913 if (State & VREPLACE_FLAG)
1914 {
1915 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1916 orig_col = curwin->w_cursor.col;
1917 }
1918#endif
1919
1920 /* for the following tricks we don't want list mode */
1921 save_p_list = curwin->w_p_list;
1922 curwin->w_p_list = FALSE;
1923 vc = getvcol_nolist(&curwin->w_cursor);
1924 vcol = vc;
1925
1926 /*
1927 * For Replace mode we need to fix the replace stack later, which is only
1928 * possible when the cursor is in the indent. Remember the number of
1929 * characters before the cursor if it's possible.
1930 */
1931 start_col = curwin->w_cursor.col;
1932
1933 /* determine offset from first non-blank */
1934 new_cursor_col = curwin->w_cursor.col;
1935 beginline(BL_WHITE);
1936 new_cursor_col -= curwin->w_cursor.col;
1937
1938 insstart_less = curwin->w_cursor.col;
1939
1940 /*
1941 * If the cursor is in the indent, compute how many screen columns the
1942 * cursor is to the left of the first non-blank.
1943 */
1944 if (new_cursor_col < 0)
1945 vcol = get_indent() - vcol;
1946
1947 if (new_cursor_col > 0) /* can't fix replace stack */
1948 start_col = -1;
1949
1950 /*
1951 * Set the new indent. The cursor will be put on the first non-blank.
1952 */
1953 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001954 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 else
1956 {
1957#ifdef FEAT_VREPLACE
1958 int save_State = State;
1959
1960 /* Avoid being called recursively. */
1961 if (State & VREPLACE_FLAG)
1962 State = INSERT;
1963#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001964 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965#ifdef FEAT_VREPLACE
1966 State = save_State;
1967#endif
1968 }
1969 insstart_less -= curwin->w_cursor.col;
1970
1971 /*
1972 * Try to put cursor on same character.
1973 * If the cursor is at or after the first non-blank in the line,
1974 * compute the cursor column relative to the column of the first
1975 * non-blank character.
1976 * If we are not in insert mode, leave the cursor on the first non-blank.
1977 * If the cursor is before the first non-blank, position it relative
1978 * to the first non-blank, counted in screen columns.
1979 */
1980 if (new_cursor_col >= 0)
1981 {
1982 /*
1983 * When changing the indent while the cursor is touching it, reset
1984 * Insstart_col to 0.
1985 */
1986 if (new_cursor_col == 0)
1987 insstart_less = MAXCOL;
1988 new_cursor_col += curwin->w_cursor.col;
1989 }
1990 else if (!(State & INSERT))
1991 new_cursor_col = curwin->w_cursor.col;
1992 else
1993 {
1994 /*
1995 * Compute the screen column where the cursor should be.
1996 */
1997 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001998 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000 /*
2001 * Advance the cursor until we reach the right screen column.
2002 */
2003 vcol = last_vcol = 0;
2004 new_cursor_col = -1;
2005 ptr = ml_get_curline();
2006 while (vcol <= (int)curwin->w_virtcol)
2007 {
2008 last_vcol = vcol;
2009#ifdef FEAT_MBYTE
2010 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002011 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 else
2013#endif
2014 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002015 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
2017 vcol = last_vcol;
2018
2019 /*
2020 * May need to insert spaces to be able to position the cursor on
2021 * the right screen column.
2022 */
2023 if (vcol != (int)curwin->w_virtcol)
2024 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002025 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002027 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 if (ptr != NULL)
2029 {
2030 new_cursor_col += i;
2031 ptr[i] = NUL;
2032 while (--i >= 0)
2033 ptr[i] = ' ';
2034 ins_str(ptr);
2035 vim_free(ptr);
2036 }
2037 }
2038
2039 /*
2040 * When changing the indent while the cursor is in it, reset
2041 * Insstart_col to 0.
2042 */
2043 insstart_less = MAXCOL;
2044 }
2045
2046 curwin->w_p_list = save_p_list;
2047
2048 if (new_cursor_col <= 0)
2049 curwin->w_cursor.col = 0;
2050 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002051 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 curwin->w_set_curswant = TRUE;
2053 changed_cline_bef_curs();
2054
2055 /*
2056 * May have to adjust the start of the insert.
2057 */
2058 if (State & INSERT)
2059 {
2060 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2061 {
2062 if ((int)Insstart.col <= insstart_less)
2063 Insstart.col = 0;
2064 else
2065 Insstart.col -= insstart_less;
2066 }
2067 if ((int)ai_col <= insstart_less)
2068 ai_col = 0;
2069 else
2070 ai_col -= insstart_less;
2071 }
2072
2073 /*
2074 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2075 * If the number of characters before the cursor decreased, need to pop a
2076 * few characters from the replace stack.
2077 * If the number of characters before the cursor increased, need to push a
2078 * few NULs onto the replace stack.
2079 */
2080 if (REPLACE_NORMAL(State) && start_col >= 0)
2081 {
2082 while (start_col > (int)curwin->w_cursor.col)
2083 {
2084 replace_join(0); /* remove a NUL from the replace stack */
2085 --start_col;
2086 }
2087 while (start_col < (int)curwin->w_cursor.col || replaced)
2088 {
2089 replace_push(NUL);
2090 if (replaced)
2091 {
2092 replace_push(replaced);
2093 replaced = NUL;
2094 }
2095 ++start_col;
2096 }
2097 }
2098
2099#ifdef FEAT_VREPLACE
2100 /*
2101 * For VREPLACE mode, we also have to fix the replace stack. In this case
2102 * it is always possible because we backspace over the whole line and then
2103 * put it back again the way we wanted it.
2104 */
2105 if (State & VREPLACE_FLAG)
2106 {
2107 /* If orig_line didn't allocate, just return. At least we did the job,
2108 * even if you can't backspace. */
2109 if (orig_line == NULL)
2110 return;
2111
2112 /* Save new line */
2113 new_line = vim_strsave(ml_get_curline());
2114 if (new_line == NULL)
2115 return;
2116
2117 /* We only put back the new line up to the cursor */
2118 new_line[curwin->w_cursor.col] = NUL;
2119
2120 /* Put back original line */
2121 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2122 curwin->w_cursor.col = orig_col;
2123
2124 /* Backspace from cursor to start of line */
2125 backspace_until_column(0);
2126
2127 /* Insert new stuff into line again */
2128 ins_bytes(new_line);
2129
2130 vim_free(new_line);
2131 }
2132#endif
2133}
2134
2135/*
2136 * Truncate the space at the end of a line. This is to be used only in an
2137 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2138 * modes.
2139 */
2140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002141truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142{
2143 int i;
2144
2145 /* find start of trailing white space */
2146 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2147 {
2148 if (State & REPLACE_FLAG)
2149 replace_join(0); /* remove a NUL from the replace stack */
2150 }
2151 line[i + 1] = NUL;
2152}
2153
2154#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2155 || defined(FEAT_COMMENTS) || defined(PROTO)
2156/*
2157 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2158 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002159 * Will attempt not to go before "col" even when there is a composing
2160 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 */
2162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002163backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164{
2165 while ((int)curwin->w_cursor.col > col)
2166 {
2167 curwin->w_cursor.col--;
2168 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002169 replace_do_bs(col);
2170 else if (!del_char_after_col(col))
2171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
2173}
2174#endif
2175
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002176/*
2177 * Like del_char(), but make sure not to go before column "limit_col".
2178 * Only matters when there are composing characters.
2179 * Return TRUE when something was deleted.
2180 */
2181 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002182del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002183{
2184#ifdef FEAT_MBYTE
2185 if (enc_utf8 && limit_col >= 0)
2186 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002187 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002188
2189 /* Make sure the cursor is at the start of a character, but
2190 * skip forward again when going too far back because of a
2191 * composing character. */
2192 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002193 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002194 {
2195 int l = utf_ptr2len(ml_get_cursor());
2196
2197 if (l == 0) /* end of line */
2198 break;
2199 curwin->w_cursor.col += l;
2200 }
2201 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2202 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002203 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002204 }
2205 else
2206#endif
2207 (void)del_char(FALSE);
2208 return TRUE;
2209}
2210
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2212/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002213 * CTRL-X pressed in Insert mode.
2214 */
2215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002216ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002217{
2218 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2219 * CTRL-V works like CTRL-N */
2220 if (ctrl_x_mode != CTRL_X_CMDLINE)
2221 {
2222 /* if the next ^X<> won't ADD nothing, then reset
2223 * compl_cont_status */
2224 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002225 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002226 else
2227 compl_cont_status = 0;
2228 /* We're not sure which CTRL-X mode it will be yet */
2229 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2230 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2231 edit_submode_pre = NULL;
2232 showmode();
2233 }
2234}
2235
2236/*
2237 * Return TRUE if the 'dict' or 'tsr' option can be used.
2238 */
2239 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002240has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002241{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002242 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002243# ifdef FEAT_SPELL
2244 && !curwin->w_p_spell
2245# endif
2246 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002247 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2248 {
2249 ctrl_x_mode = 0;
2250 edit_submode = NULL;
2251 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2252 : (char_u *)_("'thesaurus' option is empty"),
2253 hl_attr(HLF_E));
2254 if (emsg_silent == 0)
2255 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002256 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002257 setcursor();
2258 out_flush();
2259 ui_delay(2000L, FALSE);
2260 }
2261 return FALSE;
2262 }
2263 return TRUE;
2264}
2265
2266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2268 * This depends on the current mode.
2269 */
2270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002271vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
2273 /* Always allow ^R - let it's results then be checked */
2274 if (c == Ctrl_R)
2275 return TRUE;
2276
Bram Moolenaare3226be2005-12-18 22:10:00 +00002277 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002278 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002279 return TRUE;
2280
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 switch (ctrl_x_mode)
2282 {
2283 case 0: /* Not in any CTRL-X mode */
2284 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2285 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002286 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2288 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2289 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002290 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002291 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 case CTRL_X_SCROLL:
2293 return (c == Ctrl_Y || c == Ctrl_E);
2294 case CTRL_X_WHOLE_LINE:
2295 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2296 case CTRL_X_FILES:
2297 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2298 case CTRL_X_DICTIONARY:
2299 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2300 case CTRL_X_THESAURUS:
2301 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2302 case CTRL_X_TAGS:
2303 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2304#ifdef FEAT_FIND_ID
2305 case CTRL_X_PATH_PATTERNS:
2306 return (c == Ctrl_P || c == Ctrl_N);
2307 case CTRL_X_PATH_DEFINES:
2308 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2309#endif
2310 case CTRL_X_CMDLINE:
2311 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2312 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002313#ifdef FEAT_COMPL_FUNC
2314 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002315 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002316 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002317 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002318#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002319 case CTRL_X_SPELL:
2320 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002321 case CTRL_X_EVAL:
2322 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002324 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 return FALSE;
2326}
2327
2328/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002329 * Return TRUE when character "c" is part of the item currently being
2330 * completed. Used to decide whether to abandon complete mode when the menu
2331 * is visible.
2332 */
2333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002334ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002335{
2336 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2337 /* When expanding an identifier only accept identifier chars. */
2338 return vim_isIDc(c);
2339
2340 switch (ctrl_x_mode)
2341 {
2342 case CTRL_X_FILES:
2343 /* When expanding file name only accept file name chars. But not
2344 * path separators, so that "proto/<Tab>" expands files in
2345 * "proto", not "proto/" as a whole */
2346 return vim_isfilec(c) && !vim_ispathsep(c);
2347
2348 case CTRL_X_CMDLINE:
2349 case CTRL_X_OMNI:
2350 /* Command line and Omni completion can work with just about any
2351 * printable character, but do stop at white space. */
2352 return vim_isprintc(c) && !vim_iswhite(c);
2353
2354 case CTRL_X_WHOLE_LINE:
2355 /* For while line completion a space can be part of the line. */
2356 return vim_isprintc(c);
2357 }
2358 return vim_iswordc(c);
2359}
2360
2361/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002362 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002364 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 * the rest of the word to be in -- webb
2366 */
2367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002368ins_compl_add_infercase(
2369 char_u *str,
2370 int len,
2371 int icase,
2372 char_u *fname,
2373 int dir,
2374 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002376 char_u *p;
2377 int i, c;
2378 int actual_len; /* Take multi-byte characters */
2379 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002380 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002381 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 int has_lower = FALSE;
2383 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002385 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002387 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaara2993e12007-08-12 14:38:46 +00002389 /* Find actual length of completion. */
2390#ifdef FEAT_MBYTE
2391 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002393 p = str;
2394 actual_len = 0;
2395 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002397 mb_ptr_adv(p);
2398 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002401 else
2402#endif
2403 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
Bram Moolenaara2993e12007-08-12 14:38:46 +00002405 /* Find actual length of original text. */
2406#ifdef FEAT_MBYTE
2407 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002409 p = compl_orig_text;
2410 actual_compl_length = 0;
2411 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002413 mb_ptr_adv(p);
2414 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002417 else
2418#endif
2419 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002421 /* "actual_len" may be smaller than "actual_compl_length" when using
2422 * thesaurus, only use the minimum when comparing. */
2423 min_len = actual_len < actual_compl_length
2424 ? actual_len : actual_compl_length;
2425
Bram Moolenaara2993e12007-08-12 14:38:46 +00002426 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002427 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002428 if (wca != NULL)
2429 {
2430 p = str;
2431 for (i = 0; i < actual_len; ++i)
2432#ifdef FEAT_MBYTE
2433 if (has_mbyte)
2434 wca[i] = mb_ptr2char_adv(&p);
2435 else
2436#endif
2437 wca[i] = *(p++);
2438
2439 /* Rule 1: Were any chars converted to lower? */
2440 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002441 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002442 {
2443#ifdef FEAT_MBYTE
2444 if (has_mbyte)
2445 c = mb_ptr2char_adv(&p);
2446 else
2447#endif
2448 c = *(p++);
2449 if (MB_ISLOWER(c))
2450 {
2451 has_lower = TRUE;
2452 if (MB_ISUPPER(wca[i]))
2453 {
2454 /* Rule 1 is satisfied. */
2455 for (i = actual_compl_length; i < actual_len; ++i)
2456 wca[i] = MB_TOLOWER(wca[i]);
2457 break;
2458 }
2459 }
2460 }
2461
2462 /*
2463 * Rule 2: No lower case, 2nd consecutive letter converted to
2464 * upper case.
2465 */
2466 if (!has_lower)
2467 {
2468 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002469 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002470 {
2471#ifdef FEAT_MBYTE
2472 if (has_mbyte)
2473 c = mb_ptr2char_adv(&p);
2474 else
2475#endif
2476 c = *(p++);
2477 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2478 {
2479 /* Rule 2 is satisfied. */
2480 for (i = actual_compl_length; i < actual_len; ++i)
2481 wca[i] = MB_TOUPPER(wca[i]);
2482 break;
2483 }
2484 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2485 }
2486 }
2487
2488 /* Copy the original case of the part we typed. */
2489 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002490 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002491 {
2492#ifdef FEAT_MBYTE
2493 if (has_mbyte)
2494 c = mb_ptr2char_adv(&p);
2495 else
2496#endif
2497 c = *(p++);
2498 if (MB_ISLOWER(c))
2499 wca[i] = MB_TOLOWER(wca[i]);
2500 else if (MB_ISUPPER(c))
2501 wca[i] = MB_TOUPPER(wca[i]);
2502 }
2503
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002504 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002505 * Generate encoding specific output from wide character array.
2506 * Multi-byte characters can occupy up to five bytes more than
2507 * ASCII characters, and we also need one byte for NUL, so stay
2508 * six bytes away from the edge of IObuff.
2509 */
2510 p = IObuff;
2511 i = 0;
2512 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2513#ifdef FEAT_MBYTE
2514 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002515 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002516 else
2517#endif
2518 *(p++) = wca[i++];
2519 *p = NUL;
2520
2521 vim_free(wca);
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002524 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2525 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002527 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528}
2529
2530/*
2531 * Add a match to the list of matches.
2532 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002533 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002537ins_compl_add(
2538 char_u *str,
2539 int len,
2540 int icase,
2541 char_u *fname,
2542 char_u **cptext, /* extra text for popup menu or NULL */
2543 int cdir,
2544 int flags,
2545 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002547 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002548 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
2550 ui_breakcheck();
2551 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002552 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (len < 0)
2554 len = (int)STRLEN(str);
2555
2556 /*
2557 * If the same match is already present, don't add it.
2558 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002559 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002561 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 do
2563 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002564 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002565 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002566 && match->cp_str[len] == NUL)
2567 return NOTDONE;
2568 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002569 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002572 /* Remove any popup menu before changing the list of matches. */
2573 ins_compl_del_pum();
2574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 /*
2576 * Allocate a new match structure.
2577 * Copy the values to the new match structure.
2578 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002579 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002581 return FAIL;
2582 match->cp_number = -1;
2583 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002584 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002585 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
2587 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002590 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002593 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2595 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002596 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002597 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002598 && compl_curr_match->cp_fname != NULL
2599 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002600 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002601 else if (fname != NULL)
2602 {
2603 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002604 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002607 match->cp_fname = NULL;
2608 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002609
2610 if (cptext != NULL)
2611 {
2612 int i;
2613
2614 for (i = 0; i < CPT_COUNT; ++i)
2615 if (cptext[i] != NULL && *cptext[i] != NUL)
2616 match->cp_text[i] = vim_strsave(cptext[i]);
2617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 /*
2620 * Link the new match structure in the list of matches.
2621 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002622 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002623 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 else if (dir == FORWARD)
2625 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002626 match->cp_next = compl_curr_match->cp_next;
2627 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
2629 else /* BACKWARD */
2630 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002631 match->cp_next = compl_curr_match;
2632 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002634 if (match->cp_next)
2635 match->cp_next->cp_prev = match;
2636 if (match->cp_prev)
2637 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002639 compl_first_match = match;
2640 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002642 /*
2643 * Find the longest common string if still doing that.
2644 */
2645 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2646 ins_compl_longest_match(match);
2647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 return OK;
2649}
2650
2651/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002652 * Return TRUE if "str[len]" matches with match->cp_str, considering
2653 * match->cp_icase.
2654 */
2655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002656ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002657{
2658 if (match->cp_icase)
2659 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2660 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2661}
2662
2663/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002664 * Reduce the longest common string for match "match".
2665 */
2666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002667ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002668{
2669 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002670 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002671 int had_match;
2672
2673 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002674 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002675 /* First match, use it as a whole. */
2676 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002677 if (compl_leader != NULL)
2678 {
2679 had_match = (curwin->w_cursor.col > compl_col);
2680 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002681 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002682 ins_redraw(FALSE);
2683
2684 /* When the match isn't there (to avoid matching itself) remove it
2685 * again after redrawing. */
2686 if (!had_match)
2687 ins_compl_delete();
2688 compl_used_match = FALSE;
2689 }
2690 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002691 else
2692 {
2693 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002694 p = compl_leader;
2695 s = match->cp_str;
2696 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002697 {
2698#ifdef FEAT_MBYTE
2699 if (has_mbyte)
2700 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002701 c1 = mb_ptr2char(p);
2702 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002703 }
2704 else
2705#endif
2706 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002707 c1 = *p;
2708 c2 = *s;
2709 }
2710 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2711 : (c1 != c2))
2712 break;
2713#ifdef FEAT_MBYTE
2714 if (has_mbyte)
2715 {
2716 mb_ptr_adv(p);
2717 mb_ptr_adv(s);
2718 }
2719 else
2720#endif
2721 {
2722 ++p;
2723 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002724 }
2725 }
2726
2727 if (*p != NUL)
2728 {
2729 /* Leader was shortened, need to change the inserted text. */
2730 *p = NUL;
2731 had_match = (curwin->w_cursor.col > compl_col);
2732 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002733 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002734 ins_redraw(FALSE);
2735
2736 /* When the match isn't there (to avoid matching itself) remove it
2737 * again after redrawing. */
2738 if (!had_match)
2739 ins_compl_delete();
2740 }
2741
2742 compl_used_match = FALSE;
2743 }
2744}
2745
2746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 * Add an array of matches to the list of matches.
2748 * Frees matches[].
2749 */
2750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002751ins_compl_add_matches(
2752 int num_matches,
2753 char_u **matches,
2754 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 int i;
2757 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002758 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
Bram Moolenaar572cb562005-08-05 21:35:02 +00002760 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002761 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002762 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002764 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 FreeWild(num_matches, matches);
2766}
2767
2768/* Make the completion list cyclic.
2769 * Return the number of matches (excluding the original).
2770 */
2771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002772ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002774 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 int count = 0;
2776
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002777 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
2779 /*
2780 * Find the end of the list.
2781 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002782 match = compl_first_match;
2783 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002784 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002786 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 ++count;
2788 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002789 match->cp_next = compl_first_match;
2790 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
2792 return count;
2793}
2794
Bram Moolenaara94bc432006-03-10 21:42:59 +00002795/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002796 * Set variables that store noselect and noinsert behavior from the
2797 * 'completeopt' value.
2798 */
2799 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002800completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002801{
2802 compl_no_insert = FALSE;
2803 compl_no_select = FALSE;
2804 if (strstr((char *)p_cot, "noselect") != NULL)
2805 compl_no_select = TRUE;
2806 if (strstr((char *)p_cot, "noinsert") != NULL)
2807 compl_no_insert = TRUE;
2808}
2809
2810/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002811 * Start completion for the complete() function.
2812 * "startcol" is where the matched text starts (1 is first column).
2813 * "list" is the list of matches.
2814 */
2815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002816set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002817{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002818 int save_w_wrow = curwin->w_wrow;
2819
Bram Moolenaara94bc432006-03-10 21:42:59 +00002820 /* If already doing completions stop it. */
2821 if (ctrl_x_mode != 0)
2822 ins_compl_prep(' ');
2823 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002824 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002825
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002826 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002827 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002828 startcol = curwin->w_cursor.col;
2829 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002830 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002831 /* compl_pattern doesn't need to be set */
2832 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2833 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002834 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002835 return;
2836
Bram Moolenaare4214502015-03-05 18:08:43 +01002837 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002838
2839 ins_compl_add_list(list);
2840 compl_matches = ins_compl_make_cyclic();
2841 compl_started = TRUE;
2842 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002843 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002844
2845 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002846 if (compl_no_insert || compl_no_select)
2847 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002848 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002849 if (compl_no_select)
2850 /* Down/Up has no real effect. */
2851 ins_complete(K_UP, FALSE);
2852 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002853 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002854 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002855 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002856
2857 /* Lazily show the popup menu, unless we got interrupted. */
2858 if (!compl_interrupted)
2859 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002860 out_flush();
2861}
2862
2863
Bram Moolenaar9372a112005-12-06 19:59:18 +00002864/* "compl_match_array" points the currently displayed list of entries in the
2865 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002866static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002867static int compl_match_arraysize;
2868
2869/*
2870 * Update the screen and when there is any scrolling remove the popup menu.
2871 */
2872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002873ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002874{
2875 int h;
2876
2877 if (compl_match_array != NULL)
2878 {
2879 h = curwin->w_cline_height;
2880 update_screen(0);
2881 if (h != curwin->w_cline_height)
2882 ins_compl_del_pum();
2883 }
2884}
2885
2886/*
2887 * Remove any popup menu.
2888 */
2889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002890ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002891{
2892 if (compl_match_array != NULL)
2893 {
2894 pum_undisplay();
2895 vim_free(compl_match_array);
2896 compl_match_array = NULL;
2897 }
2898}
2899
2900/*
2901 * Return TRUE if the popup menu should be displayed.
2902 */
2903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002904pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002905{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002906 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002907 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002908 return FALSE;
2909
2910 /* The display looks bad on a B&W display. */
2911 if (t_colors < 8
2912#ifdef FEAT_GUI
2913 && !gui.in_use
2914#endif
2915 )
2916 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002917 return TRUE;
2918}
2919
2920/*
2921 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002922 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002923 */
2924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002925pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002926{
2927 compl_T *compl;
2928 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002929
2930 /* Don't display the popup menu if there are no matches or there is only
2931 * one (ignoring the original text). */
2932 compl = compl_first_match;
2933 i = 0;
2934 do
2935 {
2936 if (compl == NULL
2937 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2938 break;
2939 compl = compl->cp_next;
2940 } while (compl != compl_first_match);
2941
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002942 if (strstr((char *)p_cot, "menuone") != NULL)
2943 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002944 return (i >= 2);
2945}
2946
2947/*
2948 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002949 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002950 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002951 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002952ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002953{
2954 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002955 compl_T *shown_compl = NULL;
2956 int did_find_shown_match = FALSE;
2957 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002958 int i;
2959 int cur = -1;
2960 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002961 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002962
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002963 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002964 return;
2965
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002966#if defined(FEAT_EVAL)
2967 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2968 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2969#endif
2970
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002971 /* Update the screen before drawing the popup menu over it. */
2972 update_screen(0);
2973
2974 if (compl_match_array == NULL)
2975 {
2976 /* Need to build the popup menu list. */
2977 compl_match_arraysize = 0;
2978 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002979 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002980 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002981 do
2982 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002983 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2984 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002985 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002986 ++compl_match_arraysize;
2987 compl = compl->cp_next;
2988 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002989 if (compl_match_arraysize == 0)
2990 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002991 compl_match_array = (pumitem_T *)alloc_clear(
2992 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002993 * compl_match_arraysize));
2994 if (compl_match_array != NULL)
2995 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002996 /* If the current match is the original text don't find the first
2997 * match after it, don't highlight anything. */
2998 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2999 shown_match_ok = TRUE;
3000
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003001 i = 0;
3002 compl = compl_first_match;
3003 do
3004 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003005 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3006 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003007 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003008 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003009 if (!shown_match_ok)
3010 {
3011 if (compl == compl_shown_match || did_find_shown_match)
3012 {
3013 /* This item is the shown match or this is the
3014 * first displayed item after the shown match. */
3015 compl_shown_match = compl;
3016 did_find_shown_match = TRUE;
3017 shown_match_ok = TRUE;
3018 }
3019 else
3020 /* Remember this displayed match for when the
3021 * shown match is just below it. */
3022 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003023 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003024 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003025
3026 if (compl->cp_text[CPT_ABBR] != NULL)
3027 compl_match_array[i].pum_text =
3028 compl->cp_text[CPT_ABBR];
3029 else
3030 compl_match_array[i].pum_text = compl->cp_str;
3031 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3032 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3033 if (compl->cp_text[CPT_MENU] != NULL)
3034 compl_match_array[i++].pum_extra =
3035 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003036 else
3037 compl_match_array[i++].pum_extra = compl->cp_fname;
3038 }
3039
3040 if (compl == compl_shown_match)
3041 {
3042 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003043
3044 /* When the original text is the shown match don't set
3045 * compl_shown_match. */
3046 if (compl->cp_flags & ORIGINAL_TEXT)
3047 shown_match_ok = TRUE;
3048
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003049 if (!shown_match_ok && shown_compl != NULL)
3050 {
3051 /* The shown match isn't displayed, set it to the
3052 * previously displayed match. */
3053 compl_shown_match = shown_compl;
3054 shown_match_ok = TRUE;
3055 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003056 }
3057 compl = compl->cp_next;
3058 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003059
3060 if (!shown_match_ok) /* no displayed match at all */
3061 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003062 }
3063 }
3064 else
3065 {
3066 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003067 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003068 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3069 || compl_match_array[i].pum_text
3070 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003071 {
3072 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003073 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003074 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003075 }
3076
3077 if (compl_match_array != NULL)
3078 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003079 /* In Replace mode when a $ is displayed at the end of the line only
3080 * part of the screen would be updated. We do need to redraw here. */
3081 dollar_vcol = -1;
3082
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003083 /* Compute the screen column of the start of the completed text.
3084 * Use the cursor to get all wrapping and other settings right. */
3085 col = curwin->w_cursor.col;
3086 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003087 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003088 curwin->w_cursor.col = col;
3089 }
3090}
3091
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092#define DICT_FIRST (1) /* use just first element in "dict" */
3093#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003094
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003096 * Add any identifiers that match the given pattern in the list of dictionary
3097 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 */
3099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003100ins_compl_dictionaries(
3101 char_u *dict_start,
3102 char_u *pat,
3103 int flags, /* DICT_FIRST and/or DICT_EXACT */
3104 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003106 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 char_u *ptr;
3108 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 char_u **files;
3111 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003113 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
Bram Moolenaar0b238792006-03-02 22:49:12 +00003115 if (*dict == NUL)
3116 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003117#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003118 /* When 'dictionary' is empty and spell checking is enabled use
3119 * "spell". */
3120 if (!thesaurus && curwin->w_p_spell)
3121 dict = (char_u *)"spell";
3122 else
3123#endif
3124 return;
3125 }
3126
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003128 if (buf == NULL)
3129 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003130 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 /* If 'infercase' is set, don't use 'smartcase' here */
3133 save_p_scs = p_scs;
3134 if (curbuf->b_p_inf)
3135 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003136
3137 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3138 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003139 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003140 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003141 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003142 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003143 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003144
3145 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003146 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003147 len = STRLEN(pat_esc) + 10;
3148 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003149 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003150 {
3151 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003152 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003153 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003154 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003155 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3156 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003157 vim_free(ptr);
3158 }
3159 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003160 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003161 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003162 if (regmatch.regprog == NULL)
3163 goto theend;
3164 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3167 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003168 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
3170 /* copy one dictionary file name into buf */
3171 if (flags == DICT_EXACT)
3172 {
3173 count = 1;
3174 files = &dict;
3175 }
3176 else
3177 {
3178 /* Expand wildcards in the dictionary name, but do not allow
3179 * backticks (for security, the 'dict' option may have been set in
3180 * a modeline). */
3181 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003182# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003183 if (!thesaurus && STRCMP(buf, "spell") == 0)
3184 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003185 else
3186# endif
3187 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 || expand_wildcards(1, &buf, &count, &files,
3189 EW_FILE|EW_SILENT) != OK)
3190 count = 0;
3191 }
3192
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003193# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003194 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003196 /* Complete from active spelling. Skip "\<" in the pattern, we
3197 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003198 if (pat[0] == '\\' && pat[1] == '<')
3199 ptr = pat + 2;
3200 else
3201 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003202 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003204 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003205# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003206 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003207 {
3208 ins_compl_files(count, files, thesaurus, flags,
3209 &regmatch, buf, &dir);
3210 if (flags != DICT_EXACT)
3211 FreeWild(count, files);
3212 }
3213 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 break;
3215 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003216
3217theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003219 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 vim_free(buf);
3221}
3222
Bram Moolenaar0b238792006-03-02 22:49:12 +00003223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003224ins_compl_files(
3225 int count,
3226 char_u **files,
3227 int thesaurus,
3228 int flags,
3229 regmatch_T *regmatch,
3230 char_u *buf,
3231 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003232{
3233 char_u *ptr;
3234 int i;
3235 FILE *fp;
3236 int add_r;
3237
3238 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3239 {
3240 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3241 if (flags != DICT_EXACT)
3242 {
3243 vim_snprintf((char *)IObuff, IOSIZE,
3244 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003245 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003246 }
3247
3248 if (fp != NULL)
3249 {
3250 /*
3251 * Read dictionary file line by line.
3252 * Check each line for a match.
3253 */
3254 while (!got_int && !compl_interrupted
3255 && !vim_fgets(buf, LSIZE, fp))
3256 {
3257 ptr = buf;
3258 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3259 {
3260 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003261 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003262 ptr = find_line_end(ptr);
3263 else
3264 ptr = find_word_end(ptr);
3265 add_r = ins_compl_add_infercase(regmatch->startp[0],
3266 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003267 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003268 if (thesaurus)
3269 {
3270 char_u *wstart;
3271
3272 /*
3273 * Add the other matches on the line
3274 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003275 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003276 while (!got_int)
3277 {
3278 /* Find start of the next word. Skip white
3279 * space and punctuation. */
3280 ptr = find_word_start(ptr);
3281 if (*ptr == NUL || *ptr == NL)
3282 break;
3283 wstart = ptr;
3284
Bram Moolenaara2993e12007-08-12 14:38:46 +00003285 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003286#ifdef FEAT_MBYTE
3287 if (has_mbyte)
3288 /* Japanese words may have characters in
3289 * different classes, only separate words
3290 * with single-byte non-word characters. */
3291 while (*ptr != NUL)
3292 {
3293 int l = (*mb_ptr2len)(ptr);
3294
3295 if (l < 2 && !vim_iswordc(*ptr))
3296 break;
3297 ptr += l;
3298 }
3299 else
3300#endif
3301 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003302
3303 /* Add the word. Skip the regexp match. */
3304 if (wstart != regmatch->startp[0])
3305 add_r = ins_compl_add_infercase(wstart,
3306 (int)(ptr - wstart),
3307 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003308 }
3309 }
3310 if (add_r == OK)
3311 /* if dir was BACKWARD then honor it just once */
3312 *dir = FORWARD;
3313 else if (add_r == FAIL)
3314 break;
3315 /* avoid expensive call to vim_regexec() when at end
3316 * of line */
3317 if (*ptr == '\n' || got_int)
3318 break;
3319 }
3320 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003321 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003322 }
3323 fclose(fp);
3324 }
3325 }
3326}
3327
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328/*
3329 * Find the start of the next word.
3330 * Returns a pointer to the first char of the word. Also stops at a NUL.
3331 */
3332 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003333find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335#ifdef FEAT_MBYTE
3336 if (has_mbyte)
3337 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003338 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 else
3340#endif
3341 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3342 ++ptr;
3343 return ptr;
3344}
3345
3346/*
3347 * Find the end of the word. Assumes it starts inside a word.
3348 * Returns a pointer to just after the word.
3349 */
3350 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003351find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353#ifdef FEAT_MBYTE
3354 int start_class;
3355
3356 if (has_mbyte)
3357 {
3358 start_class = mb_get_class(ptr);
3359 if (start_class > 1)
3360 while (*ptr != NUL)
3361 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003362 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (mb_get_class(ptr) != start_class)
3364 break;
3365 }
3366 }
3367 else
3368#endif
3369 while (vim_iswordc(*ptr))
3370 ++ptr;
3371 return ptr;
3372}
3373
3374/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003375 * Find the end of the line, omitting CR and NL at the end.
3376 * Returns a pointer to just after the line.
3377 */
3378 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003379find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003380{
3381 char_u *s;
3382
3383 s = ptr + STRLEN(ptr);
3384 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3385 --s;
3386 return s;
3387}
3388
3389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 * Free the list of completions
3391 */
3392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003393ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003395 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003396 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003398 vim_free(compl_pattern);
3399 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003400 vim_free(compl_leader);
3401 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003403 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003405
3406 ins_compl_del_pum();
3407 pum_clear();
3408
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003409 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 do
3411 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003412 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003413 compl_curr_match = compl_curr_match->cp_next;
3414 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003416 if (match->cp_flags & FREE_FNAME)
3417 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003418 for (i = 0; i < CPT_COUNT; ++i)
3419 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003421 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3422 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003423 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424}
3425
3426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003427ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003429 compl_cont_status = 0;
3430 compl_started = FALSE;
3431 compl_matches = 0;
3432 vim_free(compl_pattern);
3433 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003434 vim_free(compl_leader);
3435 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003437 vim_free(compl_orig_text);
3438 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003439 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003440 /* clear v:completed_item */
3441 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442}
3443
3444/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003445 * Return TRUE when Insert completion is active.
3446 */
3447 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003448ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003449{
3450 return compl_started;
3451}
3452
3453/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003454 * Delete one character before the cursor and show the subset of the matches
3455 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003456 * Returns the character to be used, NUL if the work is done and another char
3457 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003458 */
3459 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003460ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003461{
3462 char_u *line;
3463 char_u *p;
3464
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003465 line = ml_get_curline();
3466 p = line + curwin->w_cursor.col;
3467 mb_ptr_back(line, p);
3468
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003469 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003470 * allow the word to be deleted, we won't match everything.
3471 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003472 if ((int)(p - line) - (int)compl_col < 0
3473 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003474 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3475 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3476 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003477 return K_BS;
3478
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003479 /* Deleted more than what was used to find matches or didn't finish
3480 * finding all matches: need to look for matches all over again. */
3481 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003482 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003483 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003484
Bram Moolenaara6557602006-02-04 22:43:20 +00003485 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003486 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003487 if (compl_leader != NULL)
3488 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003489 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003490 if (compl_shown_match != NULL)
3491 /* Make sure current match is not a hidden item. */
3492 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003493 return NUL;
3494 }
3495 return K_BS;
3496}
Bram Moolenaara6557602006-02-04 22:43:20 +00003497
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003498/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003499 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3500 * be called.
3501 */
3502 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003503ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003504{
3505 /* Return TRUE if we didn't complete finding matches or when the
3506 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3507 return compl_was_interrupted
3508 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3509 && compl_opt_refresh_always);
3510}
3511
3512/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003513 * Called after changing "compl_leader".
3514 * Show the popup menu with a different set of matches.
3515 * May also search for matches again if the previous search was interrupted.
3516 */
3517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003518ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003519{
3520 ins_compl_del_pum();
3521 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003522 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003523 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003524
3525 if (compl_started)
3526 ins_compl_set_original_text(compl_leader);
3527 else
3528 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003529#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003530 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003531#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003532 /*
3533 * Matches were cleared, need to search for them now. First display
3534 * the changed text before the cursor. Set "compl_restarting" to
3535 * avoid that the first match is inserted.
3536 */
3537 update_screen(0);
3538#ifdef FEAT_GUI
3539 if (gui.in_use)
3540 {
3541 /* Show the cursor after the match, not after the redrawn text. */
3542 setcursor();
3543 out_flush();
3544 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003545 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003546#endif
3547 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003548 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003549 compl_cont_status = 0;
3550 compl_restarting = FALSE;
3551 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003552
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003553 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003554
3555 /* Show the popup menu with a different set of matches. */
3556 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003557
3558 /* Don't let Enter select the original text when there is no popup menu. */
3559 if (compl_match_array == NULL)
3560 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003561}
3562
3563/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003564 * Return the length of the completion, from the completion start column to
3565 * the cursor column. Making sure it never goes below zero.
3566 */
3567 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003568ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003569{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003570 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003571
3572 if (off < 0)
3573 return 0;
3574 return off;
3575}
3576
3577/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003578 * Append one character to the match leader. May reduce the number of
3579 * matches.
3580 */
3581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003582ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003583{
3584#ifdef FEAT_MBYTE
3585 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003586#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003587
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003588 if (stop_arrow() == FAIL)
3589 return;
3590#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003591 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3592 {
3593 char_u buf[MB_MAXBYTES + 1];
3594
3595 (*mb_char2bytes)(c, buf);
3596 buf[cc] = NUL;
3597 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003598 if (compl_opt_refresh_always)
3599 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003600 }
3601 else
3602#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003603 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003604 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003605 if (compl_opt_refresh_always)
3606 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003607 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003608
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003609 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003610 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003611 ins_compl_restart();
3612
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003613 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003614 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003615 * break redo. */
3616 if (!compl_opt_refresh_always)
3617 {
3618 vim_free(compl_leader);
3619 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003620 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003621 if (compl_leader != NULL)
3622 ins_compl_new_leader();
3623 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003624}
3625
3626/*
3627 * Setup for finding completions again without leaving CTRL-X mode. Used when
3628 * BS or a key was typed while still searching for matches.
3629 */
3630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003631ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003632{
3633 ins_compl_free();
3634 compl_started = FALSE;
3635 compl_matches = 0;
3636 compl_cont_status = 0;
3637 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003638}
3639
3640/*
3641 * Set the first match, the original text.
3642 */
3643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003644ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003645{
3646 char_u *p;
3647
3648 /* Replace the original text entry. */
3649 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3650 {
3651 p = vim_strsave(str);
3652 if (p != NULL)
3653 {
3654 vim_free(compl_first_match->cp_str);
3655 compl_first_match->cp_str = p;
3656 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003657 }
3658}
3659
3660/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003661 * Append one character to the match leader. May reduce the number of
3662 * matches.
3663 */
3664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003665ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003666{
3667 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003668 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003669 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003670 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003671
3672 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003673 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003674 {
3675 /* When still at the original match use the first entry that matches
3676 * the leader. */
3677 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3678 {
3679 p = NULL;
3680 for (cp = compl_shown_match->cp_next; cp != NULL
3681 && cp != compl_first_match; cp = cp->cp_next)
3682 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003683 if (compl_leader == NULL
3684 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003685 (int)STRLEN(compl_leader)))
3686 {
3687 p = cp->cp_str;
3688 break;
3689 }
3690 }
3691 if (p == NULL || (int)STRLEN(p) <= len)
3692 return;
3693 }
3694 else
3695 return;
3696 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003697 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003698 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003699 ins_compl_addleader(c);
3700}
3701
3702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003704 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003705 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003707 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003708ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
3710 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003712 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
3714 /* Forget any previous 'special' messages if this is actually
3715 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3716 */
3717 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3718 edit_submode_extra = NULL;
3719
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003720 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003721 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3722 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003723 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003725 /* Set "compl_get_longest" when finding the first matches. */
3726 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3727 || (ctrl_x_mode == 0 && !compl_started))
3728 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003729 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003730 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003731
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003732 }
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3735 {
3736 /*
3737 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3738 * it will be yet. Now we decide.
3739 */
3740 switch (c)
3741 {
3742 case Ctrl_E:
3743 case Ctrl_Y:
3744 ctrl_x_mode = CTRL_X_SCROLL;
3745 if (!(State & REPLACE_FLAG))
3746 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3747 else
3748 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3749 edit_submode_pre = NULL;
3750 showmode();
3751 break;
3752 case Ctrl_L:
3753 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3754 break;
3755 case Ctrl_F:
3756 ctrl_x_mode = CTRL_X_FILES;
3757 break;
3758 case Ctrl_K:
3759 ctrl_x_mode = CTRL_X_DICTIONARY;
3760 break;
3761 case Ctrl_R:
3762 /* Simply allow ^R to happen without affecting ^X mode */
3763 break;
3764 case Ctrl_T:
3765 ctrl_x_mode = CTRL_X_THESAURUS;
3766 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003767#ifdef FEAT_COMPL_FUNC
3768 case Ctrl_U:
3769 ctrl_x_mode = CTRL_X_FUNCTION;
3770 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003771 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003772 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003773 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003774#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003775 case 's':
3776 case Ctrl_S:
3777 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003778#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003779 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003780 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003781 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003782#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003783 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 case Ctrl_RSB:
3785 ctrl_x_mode = CTRL_X_TAGS;
3786 break;
3787#ifdef FEAT_FIND_ID
3788 case Ctrl_I:
3789 case K_S_TAB:
3790 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3791 break;
3792 case Ctrl_D:
3793 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3794 break;
3795#endif
3796 case Ctrl_V:
3797 case Ctrl_Q:
3798 ctrl_x_mode = CTRL_X_CMDLINE;
3799 break;
3800 case Ctrl_P:
3801 case Ctrl_N:
3802 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3803 * just started ^X mode, or there were enough ^X's to cancel
3804 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3805 * do normal expansion when interrupting a different mode (say
3806 * ^X^F^X^P or ^P^X^X^P, see below)
3807 * nothing changes if interrupting mode 0, (eg, the flag
3808 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003809 if (!(compl_cont_status & CONT_INTRPT))
3810 compl_cont_status |= CONT_LOCAL;
3811 else if (compl_cont_mode != 0)
3812 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 /* FALLTHROUGH */
3814 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003815 /* If we have typed at least 2 ^X's... for modes != 0, we set
3816 * compl_cont_status = 0 (eg, as if we had just started ^X
3817 * mode).
3818 * For mode 0, we set "compl_cont_mode" to an impossible
3819 * value, in both cases ^X^X can be used to restart the same
3820 * mode (avoiding ADDING mode).
3821 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3822 * 'complete' and local ^P expansions respectively.
3823 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3824 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (c == Ctrl_X)
3826 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003827 if (compl_cont_mode != 0)
3828 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003830 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832 ctrl_x_mode = 0;
3833 edit_submode = NULL;
3834 showmode();
3835 break;
3836 }
3837 }
3838 else if (ctrl_x_mode != 0)
3839 {
3840 /* We're already in CTRL-X mode, do we stay in it? */
3841 if (!vim_is_ctrl_x_key(c))
3842 {
3843 if (ctrl_x_mode == CTRL_X_SCROLL)
3844 ctrl_x_mode = 0;
3845 else
3846 ctrl_x_mode = CTRL_X_FINISHED;
3847 edit_submode = NULL;
3848 }
3849 showmode();
3850 }
3851
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003852 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
3854 /* Show error message from attempted keyword completion (probably
3855 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003856 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003858 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3859 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 || ctrl_x_mode == CTRL_X_FINISHED)
3861 {
3862 /* Get here when we have finished typing a sequence of ^N and
3863 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003864 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003865 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
3867 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003868 * If any of the original typed text has been changed, eg when
3869 * ignorecase is set, we must add back-spaces to the redo
3870 * buffer. We add as few as necessary to delete just the part
3871 * of the original text that has changed.
3872 * When using the longest match, edited the match or used
3873 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003875 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3876 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003877 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003878 ptr = NULL;
3879 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881
3882#ifdef FEAT_CINDENT
3883 want_cindent = (can_cindent && cindent_on());
3884#endif
3885 /*
3886 * When completing whole lines: fix indent for 'cindent'.
3887 * Otherwise, break line if it's too long.
3888 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003889 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
3891#ifdef FEAT_CINDENT
3892 /* re-indent the current line */
3893 if (want_cindent)
3894 {
3895 do_c_expr_indent();
3896 want_cindent = FALSE; /* don't do it again */
3897 }
3898#endif
3899 }
3900 else
3901 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003902 int prev_col = curwin->w_cursor.col;
3903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003905 if (prev_col > 0)
3906 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003907 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003908 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003910 if (prev_col > 0
3911 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3912 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003915 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003916 * the selection without inserting anything. When
3917 * compl_enter_selects is set the Enter key does the same. */
3918 if ((c == Ctrl_Y || (compl_enter_selects
3919 && (c == CAR || c == K_KENTER || c == NL)))
3920 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003921 retval = TRUE;
3922
Bram Moolenaar47247282016-08-02 22:36:02 +02003923 /* CTRL-E means completion is Ended, go back to the typed text.
3924 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003925 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003926 {
3927 ins_compl_delete();
3928 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003929 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003930 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003931 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003932 retval = TRUE;
3933 }
3934
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003935 auto_format(FALSE, TRUE);
3936
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003938 compl_started = FALSE;
3939 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003940 if (!shortmess(SHM_COMPLETIONMENU))
3941 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003943 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 if (edit_submode != NULL)
3945 {
3946 edit_submode = NULL;
3947 showmode();
3948 }
3949
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003950#ifdef FEAT_CMDWIN
3951 if (c == Ctrl_C && cmdwin_type != 0)
3952 /* Avoid the popup menu remains displayed when leaving the
3953 * command line window. */
3954 update_screen(0);
3955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956#ifdef FEAT_CINDENT
3957 /*
3958 * Indent now if a key was typed that is in 'cinkeys'.
3959 */
3960 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3961 do_c_expr_indent();
3962#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003963#ifdef FEAT_AUTOCMD
3964 /* Trigger the CompleteDone event to give scripts a chance to act
3965 * upon the completion. */
3966 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003970#ifdef FEAT_AUTOCMD
3971 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3972 /* Trigger the CompleteDone event to give scripts a chance to act
3973 * upon the (possibly failed) completion. */
3974 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
3977 /* reset continue_* if we left expansion-mode, if we stay they'll be
3978 * (re)set properly in ins_complete() */
3979 if (!vim_is_ctrl_x_key(c))
3980 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003981 compl_cont_status = 0;
3982 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003984
3985 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986}
3987
3988/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003989 * Fix the redo buffer for the completion leader replacing some of the typed
3990 * text. This inserts backspaces and appends the changed text.
3991 * "ptr" is the known leader text or NUL.
3992 */
3993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003994ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003995{
3996 int len;
3997 char_u *p;
3998 char_u *ptr = ptr_arg;
3999
4000 if (ptr == NULL)
4001 {
4002 if (compl_leader != NULL)
4003 ptr = compl_leader;
4004 else
4005 return; /* nothing to do */
4006 }
4007 if (compl_orig_text != NULL)
4008 {
4009 p = compl_orig_text;
4010 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4011 ;
4012#ifdef FEAT_MBYTE
4013 if (len > 0)
4014 len -= (*mb_head_off)(p, p + len);
4015#endif
4016 for (p += len; *p != NUL; mb_ptr_adv(p))
4017 AppendCharToRedobuff(K_BS);
4018 }
4019 else
4020 len = 0;
4021 if (ptr != NULL)
4022 AppendToRedobuffLit(ptr + len, -1);
4023}
4024
4025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4027 * (depending on flag) starting from buf and looking for a non-scanned
4028 * buffer (other than curbuf). curbuf is special, if it is called with
4029 * buf=curbuf then it has to be the first call for a given flag/expansion.
4030 *
4031 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4032 */
4033 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004034ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
4036#ifdef FEAT_WINDOWS
4037 static win_T *wp;
4038#endif
4039
4040 if (flag == 'w') /* just windows */
4041 {
4042#ifdef FEAT_WINDOWS
4043 if (buf == curbuf) /* first call for this flag/expansion */
4044 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004045 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 && wp->w_buffer->b_scanned)
4047 ;
4048 buf = wp->w_buffer;
4049#else
4050 buf = curbuf;
4051#endif
4052 }
4053 else
4054 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4055 * (unlisted buffers)
4056 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004057 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 && ((flag == 'U'
4059 ? buf->b_p_bl
4060 : (!buf->b_p_bl
4061 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004062 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 ;
4064 return buf;
4065}
4066
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004067#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004068/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004069 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004070 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004071 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004073expand_by_function(
4074 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4075 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004076{
Bram Moolenaar82139082011-09-14 16:52:09 +02004077 list_T *matchlist = NULL;
4078 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004079 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004080 char_u *funcname;
4081 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004082 win_T *curwin_save;
4083 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004084 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004085
Bram Moolenaare344bea2005-09-01 20:46:49 +00004086 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4087 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004088 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004089
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004090 /* Call 'completefunc' to obtain the list of matches. */
4091 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004092 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004093
Bram Moolenaare344bea2005-09-01 20:46:49 +00004094 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004095 curwin_save = curwin;
4096 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004097
4098 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004099 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004100 {
4101 switch (rettv.v_type)
4102 {
4103 case VAR_LIST:
4104 matchlist = rettv.vval.v_list;
4105 break;
4106 case VAR_DICT:
4107 matchdict = rettv.vval.v_dict;
4108 break;
4109 default:
4110 /* TODO: Give error message? */
4111 clear_tv(&rettv);
4112 break;
4113 }
4114 }
4115
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004116 if (curwin_save != curwin || curbuf_save != curbuf)
4117 {
4118 EMSG(_(e_complwin));
4119 goto theend;
4120 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004121 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004122 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004123 if (!equalpos(curwin->w_cursor, pos))
4124 {
4125 EMSG(_(e_compldel));
4126 goto theend;
4127 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004128
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004129 if (matchlist != NULL)
4130 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004131 else if (matchdict != NULL)
4132 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004133
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004134theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004135 if (matchdict != NULL)
4136 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004137 if (matchlist != NULL)
4138 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004139}
4140#endif /* FEAT_COMPL_FUNC */
4141
Bram Moolenaar39f05632006-03-19 22:15:26 +00004142#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004143/*
4144 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004145 */
4146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004147ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004148{
4149 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004150 int dir = compl_direction;
4151
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004152 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004153 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004154 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004155 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4156 /* if dir was BACKWARD then honor it just once */
4157 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004158 else if (did_emsg)
4159 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004160 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004161}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004162
4163/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004164 * Add completions from a dict.
4165 */
4166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004167ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004168{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004169 dictitem_T *di_refresh;
4170 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004171
4172 /* Check for optional "refresh" item. */
4173 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004174 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4175 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004176 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004177 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004178
4179 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4180 compl_opt_refresh_always = TRUE;
4181 }
4182
4183 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004184 di_words = dict_find(dict, (char_u *)"words", 5);
4185 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4186 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004187}
4188
4189/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004190 * Add a match to the list of matches from a typeval_T.
4191 * If the given string is already in the list of completions, then return
4192 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4193 * maybe because alloc() returns NULL, then FAIL is returned.
4194 */
4195 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004196ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004197{
4198 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004199 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004200 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004201 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004202 char_u *(cptext[CPT_COUNT]);
4203
4204 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4205 {
4206 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4207 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4208 (char_u *)"abbr", FALSE);
4209 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4210 (char_u *)"menu", FALSE);
4211 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4212 (char_u *)"kind", FALSE);
4213 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4214 (char_u *)"info", FALSE);
4215 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4216 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004217 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004218 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004219 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4220 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004221 }
4222 else
4223 {
4224 word = get_tv_string_chk(tv);
4225 vim_memset(cptext, 0, sizeof(cptext));
4226 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004227 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004228 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004229 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004230}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004231#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004232
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004233/*
4234 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004235 * The search starts at position "ini" in curbuf and in the direction
4236 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004237 * When "compl_started" is FALSE start at that position, otherwise continue
4238 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004239 * This may return before finding all the matches.
4240 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 */
4242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004243ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
4245 static pos_T first_match_pos;
4246 static pos_T last_match_pos;
4247 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004248 static int found_all = FALSE; /* Found all matches of a
4249 certain type. */
4250 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Bram Moolenaar572cb562005-08-05 21:35:02 +00004252 pos_T *pos;
4253 char_u **matches;
4254 int save_p_scs;
4255 int save_p_ws;
4256 int save_p_ic;
4257 int i;
4258 int num_matches;
4259 int len;
4260 int found_new_match;
4261 int type = ctrl_x_mode;
4262 char_u *ptr;
4263 char_u *dict = NULL;
4264 int dict_f = 0;
4265 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004266 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004268 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004270 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 ins_buf->b_scanned = 0;
4272 found_all = FALSE;
4273 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004274 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004275 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 last_match_pos = first_match_pos = *ini;
4277 }
4278
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004279 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004280 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4282 for (;;)
4283 {
4284 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004285 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004287 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 * or if found_all says this entry is done. For ^X^L only use the
4289 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004290 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004291 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
4293 found_all = FALSE;
4294 while (*e_cpt == ',' || *e_cpt == ' ')
4295 e_cpt++;
4296 if (*e_cpt == '.' && !curbuf->b_scanned)
4297 {
4298 ins_buf = curbuf;
4299 first_match_pos = *ini;
4300 /* So that ^N can match word immediately after cursor */
4301 if (ctrl_x_mode == 0)
4302 dec(&first_match_pos);
4303 last_match_pos = first_match_pos;
4304 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004305
4306 /* Remember the first match so that the loop stops when we
4307 * wrap and come back there a second time. */
4308 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
4310 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4311 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4312 {
4313 /* Scan a buffer, but not the current one. */
4314 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4315 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004316 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 first_match_pos.col = last_match_pos.col = 0;
4318 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4319 last_match_pos.lnum = 0;
4320 type = 0;
4321 }
4322 else /* unloaded buffer, scan like dictionary */
4323 {
4324 found_all = TRUE;
4325 if (ins_buf->b_fname == NULL)
4326 continue;
4327 type = CTRL_X_DICTIONARY;
4328 dict = ins_buf->b_fname;
4329 dict_f = DICT_EXACT;
4330 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004331 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 ins_buf->b_fname == NULL
4333 ? buf_spname(ins_buf)
4334 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004335 ? ins_buf->b_fname
4336 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004337 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 else if (*e_cpt == NUL)
4340 break;
4341 else
4342 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004343 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 type = -1;
4345 else if (*e_cpt == 'k' || *e_cpt == 's')
4346 {
4347 if (*e_cpt == 'k')
4348 type = CTRL_X_DICTIONARY;
4349 else
4350 type = CTRL_X_THESAURUS;
4351 if (*++e_cpt != ',' && *e_cpt != NUL)
4352 {
4353 dict = e_cpt;
4354 dict_f = DICT_FIRST;
4355 }
4356 }
4357#ifdef FEAT_FIND_ID
4358 else if (*e_cpt == 'i')
4359 type = CTRL_X_PATH_PATTERNS;
4360 else if (*e_cpt == 'd')
4361 type = CTRL_X_PATH_DEFINES;
4362#endif
4363 else if (*e_cpt == ']' || *e_cpt == 't')
4364 {
4365 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004366 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004367 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 else
4370 type = -1;
4371
4372 /* in any case e_cpt is advanced to the next entry */
4373 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4374
4375 found_all = TRUE;
4376 if (type == -1)
4377 continue;
4378 }
4379 }
4380
4381 switch (type)
4382 {
4383 case -1:
4384 break;
4385#ifdef FEAT_FIND_ID
4386 case CTRL_X_PATH_PATTERNS:
4387 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004388 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004389 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4393 (linenr_T)1, (linenr_T)MAXLNUM);
4394 break;
4395#endif
4396
4397 case CTRL_X_DICTIONARY:
4398 case CTRL_X_THESAURUS:
4399 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004400 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 : (type == CTRL_X_THESAURUS
4402 ? (*curbuf->b_p_tsr == NUL
4403 ? p_tsr
4404 : curbuf->b_p_tsr)
4405 : (*curbuf->b_p_dict == NUL
4406 ? p_dict
4407 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004408 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004409 dict != NULL ? dict_f
4410 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 dict = NULL;
4412 break;
4413
4414 case CTRL_X_TAGS:
4415 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4416 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004419 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 * of matches is found when compl_pattern is empty */
4421 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4423 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4424 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4425 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004426 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 }
4428 p_ic = save_p_ic;
4429 break;
4430
4431 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004432 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4434 {
4435
4436 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004438 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440 break;
4441
4442 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004443 if (expand_cmdline(&compl_xp, compl_pattern,
4444 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004446 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 break;
4448
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004449#ifdef FEAT_COMPL_FUNC
4450 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004451 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004452 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004453 break;
4454#endif
4455
Bram Moolenaar488c6512005-08-11 20:09:58 +00004456 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004457#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004458 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004459 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004460 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004461 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004462#endif
4463 break;
4464
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 default: /* normal ^P/^N and ^X^L */
4466 /*
4467 * If 'infercase' is set, don't use 'smartcase' here
4468 */
4469 save_p_scs = p_scs;
4470 if (ins_buf->b_p_inf)
4471 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004472
Bram Moolenaar361aa502014-01-23 22:45:58 +01004473 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 * end but never from the middle, thus setting nowrapscan in this
4475 * buffers is a good idea, on the other hand, we always set
4476 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4477 save_p_ws = p_ws;
4478 if (ins_buf != curbuf)
4479 p_ws = FALSE;
4480 else if (*e_cpt == '.')
4481 p_ws = TRUE;
4482 for (;;)
4483 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004484 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004486 ++msg_silent; /* Don't want messages for wrapscan. */
4487
Bram Moolenaare4214502015-03-05 18:08:43 +01004488 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4489 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004490 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004491 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004492 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004494 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004496 found_new_match = searchit(NULL, ins_buf, pos,
4497 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004498 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004499 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004500 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004501 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004503 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004504 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 first_match_pos = *pos;
4506 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004507 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004510 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 found_new_match = FAIL;
4512 if (found_new_match == FAIL)
4513 {
4514 if (ins_buf == curbuf)
4515 found_all = TRUE;
4516 break;
4517 }
4518
4519 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004520 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 && ini->lnum == pos->lnum
4522 && ini->col == pos->col)
4523 continue;
4524 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004525 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004527 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
4529 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4530 continue;
4531 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4532 if (!p_paste)
4533 ptr = skipwhite(ptr);
4534 }
4535 len = (int)STRLEN(ptr);
4536 }
4537 else
4538 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539 char_u *tmp_ptr = ptr;
4540
4541 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004543 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 /* Skip if already inside a word. */
4545 if (vim_iswordp(tmp_ptr))
4546 continue;
4547 /* Find start of next word. */
4548 tmp_ptr = find_word_start(tmp_ptr);
4549 }
4550 /* Find end of this word. */
4551 tmp_ptr = find_word_end(tmp_ptr);
4552 len = (int)(tmp_ptr - ptr);
4553
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 if ((compl_cont_status & CONT_ADDING)
4555 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
4557 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4558 {
4559 /* Try next line, if any. the new word will be
4560 * "join" as if the normal command "J" was used.
4561 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 * works -- Acevedo */
4564 STRNCPY(IObuff, ptr, len);
4565 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4566 tmp_ptr = ptr = skipwhite(ptr);
4567 /* Find start of next word. */
4568 tmp_ptr = find_word_start(tmp_ptr);
4569 /* Find end of next word. */
4570 tmp_ptr = find_word_end(tmp_ptr);
4571 if (tmp_ptr > ptr)
4572 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004573 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004575 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 IObuff[len++] = ' ';
4577 /* IObuf =~ "\k.* ", thus len >= 2 */
4578 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004579 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 || (vim_strchr(p_cpo, CPO_JOINSP)
4581 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004582 && (IObuff[len - 2] == '?'
4583 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 IObuff[len++] = ' ';
4585 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004586 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 if (tmp_ptr - ptr >= IOSIZE - len)
4588 tmp_ptr = ptr + IOSIZE - len - 1;
4589 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4590 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004591 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 IObuff[len] = NUL;
4594 ptr = IObuff;
4595 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 continue;
4598 }
4599 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004600 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004601 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004602 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
4604 found_new_match = OK;
4605 break;
4606 }
4607 }
4608 p_scs = save_p_scs;
4609 p_ws = save_p_ws;
4610 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004611
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004613 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004614 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 found_new_match = OK;
4616
4617 /* break the loop for specialized modes (use 'complete' just for the
4618 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004619 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004620 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004621 {
4622 if (got_int)
4623 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004624 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004625 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004626 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004627
Bram Moolenaare4214502015-03-05 18:08:43 +01004628 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004629 || compl_interrupted)
4630 break;
4631 compl_started = TRUE;
4632 }
4633 else
4634 {
4635 /* Mark a buffer scanned when it has been scanned completely */
4636 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4637 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004639 compl_started = FALSE;
4640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004642 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
Bram Moolenaare4214502015-03-05 18:08:43 +01004644 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 && *e_cpt == NUL) /* Got to end of 'complete' */
4646 found_new_match = FAIL;
4647
4648 i = -1; /* total of matches, unknown */
4649 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004650 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 i = ins_compl_make_cyclic();
4652
4653 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004654 * just been made cyclic then we have to move compl_curr_match to the next
4655 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004656 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4657 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004658 if (compl_curr_match == NULL)
4659 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 return i;
4661}
4662
4663/* Delete the old text being completed. */
4664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004665ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004667 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668
4669 /*
4670 * In insert mode: Delete the typed part.
4671 * In replace mode: Put the old characters back, if any.
4672 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004673 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4674 if ((int)curwin->w_cursor.col > col)
4675 {
4676 if (stop_arrow() == FAIL)
4677 return;
4678 backspace_until_column(col);
4679 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004680
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004681 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4682 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004684 /* clear v:completed_item */
4685 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686}
4687
Bram Moolenaar472e8592016-10-15 17:06:47 +02004688/*
4689 * Insert the new text being completed.
4690 * "in_compl_func" is TRUE when called from complete_check().
4691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004693ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004695 dict_T *dict;
4696
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004697 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004698 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4699 compl_used_match = FALSE;
4700 else
4701 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004702
4703 /* Set completed item. */
4704 /* { word, abbr, menu, kind, info } */
4705 dict = dict_alloc();
4706 if (dict != NULL)
4707 {
4708 dict_add_nr_str(dict, "word", 0L,
4709 EMPTY_IF_NULL(compl_shown_match->cp_str));
4710 dict_add_nr_str(dict, "abbr", 0L,
4711 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4712 dict_add_nr_str(dict, "menu", 0L,
4713 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4714 dict_add_nr_str(dict, "kind", 0L,
4715 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4716 dict_add_nr_str(dict, "info", 0L,
4717 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4718 }
4719 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004720 if (!in_compl_func)
4721 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722}
4723
4724/*
4725 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004726 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4727 * get more completions. If it is FALSE, then we just do nothing when there
4728 * are no more completions in a given direction. The latter case is used when
4729 * we are still in the middle of finding completions, to allow browsing
4730 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 * Return the total number of matches, or -1 if still unknown -- webb.
4732 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004733 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4734 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 *
4736 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004737 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4738 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 */
4740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004741ins_compl_next(
4742 int allow_get_expansion,
4743 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004744 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004745 int insert_match, /* Insert the newly selected match */
4746 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 int num_matches = -1;
4749 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004750 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004751 compl_T *found_compl = NULL;
4752 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004753 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004754 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004756 /* When user complete function return -1 for findstart which is next
4757 * time of 'always', compl_shown_match become NULL. */
4758 if (compl_shown_match == NULL)
4759 return -1;
4760
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004761 if (compl_leader != NULL
4762 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004764 /* Set "compl_shown_match" to the actually shown match, it may differ
4765 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004766 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004767 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004768 && compl_shown_match->cp_next != NULL
4769 && compl_shown_match->cp_next != compl_first_match)
4770 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004771
4772 /* If we didn't find it searching forward, and compl_shows_dir is
4773 * backward, find the last match. */
4774 if (compl_shows_dir == BACKWARD
4775 && !ins_compl_equal(compl_shown_match,
4776 compl_leader, (int)STRLEN(compl_leader))
4777 && (compl_shown_match->cp_next == NULL
4778 || compl_shown_match->cp_next == compl_first_match))
4779 {
4780 while (!ins_compl_equal(compl_shown_match,
4781 compl_leader, (int)STRLEN(compl_leader))
4782 && compl_shown_match->cp_prev != NULL
4783 && compl_shown_match->cp_prev != compl_first_match)
4784 compl_shown_match = compl_shown_match->cp_prev;
4785 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004786 }
4787
4788 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004789 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 /* Delete old text to be replaced */
4791 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004792
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004793 /* When finding the longest common text we stick at the original text,
4794 * don't let CTRL-N or CTRL-P move to the first match. */
4795 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4796
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004797 /* When restarting the search don't insert the first match either. */
4798 if (compl_restarting)
4799 {
4800 advance = FALSE;
4801 compl_restarting = FALSE;
4802 }
4803
Bram Moolenaare3226be2005-12-18 22:10:00 +00004804 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4805 * around. */
4806 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004808 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004810 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004811 found_end = (compl_first_match != NULL
4812 && (compl_shown_match->cp_next == compl_first_match
4813 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004814 }
4815 else if (compl_shows_dir == BACKWARD
4816 && compl_shown_match->cp_prev != NULL)
4817 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004818 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004819 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004820 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
4822 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004823 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004824 if (!allow_get_expansion)
4825 {
4826 if (advance)
4827 {
4828 if (compl_shows_dir == BACKWARD)
4829 compl_pending -= todo + 1;
4830 else
4831 compl_pending += todo + 1;
4832 }
4833 return -1;
4834 }
4835
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004836 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004837 {
4838 if (compl_shows_dir == BACKWARD)
4839 --compl_pending;
4840 else
4841 ++compl_pending;
4842 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004843
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004844 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004845 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004846
4847 /* handle any pending completions */
4848 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004849 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004850 {
4851 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4852 {
4853 compl_shown_match = compl_shown_match->cp_next;
4854 --compl_pending;
4855 }
4856 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4857 {
4858 compl_shown_match = compl_shown_match->cp_prev;
4859 ++compl_pending;
4860 }
4861 else
4862 break;
4863 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004864 found_end = FALSE;
4865 }
4866 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4867 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004868 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004869 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004870 ++todo;
4871 else
4872 /* Remember a matching item. */
4873 found_compl = compl_shown_match;
4874
4875 /* Stop at the end of the list when we found a usable match. */
4876 if (found_end)
4877 {
4878 if (found_compl != NULL)
4879 {
4880 compl_shown_match = found_compl;
4881 break;
4882 }
4883 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
4886
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004887 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004888 if (compl_no_insert && !started)
4889 {
4890 ins_bytes(compl_orig_text + ins_compl_len());
4891 compl_used_match = FALSE;
4892 }
4893 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004894 {
4895 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004896 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004897 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004898 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004899 }
4900 else
4901 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
4903 if (!allow_get_expansion)
4904 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004905 /* may undisplay the popup menu first */
4906 ins_compl_upd_pum();
4907
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004908 /* redraw to show the user what was inserted */
4909 update_screen(0);
4910
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004911 /* display the updated popup menu */
4912 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004913#ifdef FEAT_GUI
4914 if (gui.in_use)
4915 {
4916 /* Show the cursor after the match, not after the redrawn text. */
4917 setcursor();
4918 out_flush();
4919 gui_update_cursor(FALSE, FALSE);
4920 }
4921#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004922
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 /* Delete old text to be replaced, since we're still searching and
4924 * don't want to match ourselves! */
4925 ins_compl_delete();
4926 }
4927
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004928 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004929 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004930 if (compl_no_insert && !started)
4931 compl_enter_selects = TRUE;
4932 else
4933 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 /*
4936 * Show the file name for the match (if any)
4937 * Truncate the file name to avoid a wait for return.
4938 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004939 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
4941 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004942 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 if (i <= 0)
4944 i = 0;
4945 else
4946 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004947 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 msg(IObuff);
4949 redraw_cmdline = FALSE; /* don't overwrite! */
4950 }
4951
4952 return num_matches;
4953}
4954
4955/*
4956 * Call this while finding completions, to check whether the user has hit a key
4957 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004958 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004960 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004961 * "in_compl_func" is TRUE when called from complete_check(), don't set
4962 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 */
4964 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004965ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966{
4967 static int count = 0;
4968
4969 int c;
4970
4971 /* Don't check when reading keys from a script. That would break the test
4972 * scripts */
4973 if (using_script())
4974 return;
4975
4976 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004977 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 return;
4979 count = 0;
4980
Bram Moolenaara260a972006-06-23 19:36:29 +00004981 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4982 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 if (c != NUL)
4985 {
4986 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4987 {
4988 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004989 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004990 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02004991 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004993 else
4994 {
4995 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004996 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004997 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004998 if (c != K_IGNORE)
4999 {
5000 /* Don't interrupt completion when the character wasn't typed,
5001 * e.g., when doing @q to replay keys. */
5002 if (c != Ctrl_R && KeyTyped)
5003 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005004
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005005 vungetc(c);
5006 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005009 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005010 {
5011 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5012
5013 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005014 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005015 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005016}
5017
5018/*
5019 * Decide the direction of Insert mode complete from the key typed.
5020 * Returns BACKWARD or FORWARD.
5021 */
5022 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005023ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005024{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005025 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005026 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005027 return BACKWARD;
5028 return FORWARD;
5029}
5030
5031/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005032 * Return TRUE for keys that are used for completion only when the popup menu
5033 * is visible.
5034 */
5035 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005036ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005037{
5038 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005039 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5040 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005041}
5042
5043/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005044 * Decide the number of completions to move forward.
5045 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5046 */
5047 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005048ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005049{
5050 int h;
5051
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005052 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005053 {
5054 h = pum_get_height();
5055 if (h > 3)
5056 h -= 2; /* keep some context */
5057 return h;
5058 }
5059 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060}
5061
5062/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005063 * Return TRUE if completion with "c" should insert the match, FALSE if only
5064 * to change the currently selected completion.
5065 */
5066 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005067ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005068{
5069 switch (c)
5070 {
5071 case K_UP:
5072 case K_DOWN:
5073 case K_PAGEDOWN:
5074 case K_KPAGEDOWN:
5075 case K_S_DOWN:
5076 case K_PAGEUP:
5077 case K_KPAGEUP:
5078 case K_S_UP:
5079 return FALSE;
5080 }
5081 return TRUE;
5082}
5083
5084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 * Do Insert mode completion.
5086 * Called when character "c" was typed, which has a meaning for completion.
5087 * Returns OK if completion was done, FAIL if something failed (out of mem).
5088 */
5089 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005090ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005092 char_u *line;
5093 int startcol = 0; /* column where searched text starts */
5094 colnr_T curs_col; /* cursor column */
5095 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005096 int save_w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005097 int insert_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098
Bram Moolenaare3226be2005-12-18 22:10:00 +00005099 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005100 insert_match = ins_compl_use_match(c);
5101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005102 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 {
5104 /* First time we hit ^N or ^P (in a row, I mean) */
5105
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 did_ai = FALSE;
5107#ifdef FEAT_SMARTINDENT
5108 did_si = FALSE;
5109 can_si = FALSE;
5110 can_si_back = FALSE;
5111#endif
5112 if (stop_arrow() == FAIL)
5113 return FAIL;
5114
5115 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005116 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005117 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005119 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005120 * "compl_startpos" to the cursor as a pattern to add a new word
5121 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005122 * "compl_startpos" is not in the same line as the cursor then fix it
5123 * (the line has been split because it was longer than 'tw'). if SOL
5124 * is set then skip the previous pattern, a word at the beginning of
5125 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005126 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5127 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 /*
5130 * it is a continued search
5131 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005132 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5134 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5135 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005136 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005138 /* line (probably) wrapped, set compl_startpos to the
5139 * first non_blank in the line, if it is not a wordchar
5140 * include it to get a better pattern, but then we don't
5141 * want the "\\<" prefix, check it bellow */
5142 compl_col = (colnr_T)(skipwhite(line) - line);
5143 compl_startpos.col = compl_col;
5144 compl_startpos.lnum = curwin->w_cursor.lnum;
5145 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147 else
5148 {
5149 /* S_IPOS was set when we inserted a word that was at the
5150 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005151 * mode but first we need to redefine compl_startpos */
5152 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005154 compl_cont_status |= CONT_SOL;
5155 compl_startpos.col = (colnr_T)(skipwhite(
5156 line + compl_length
5157 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005159 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005161 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005162 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005163 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005165 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 compl_cont_status &= ~CONT_SOL;
5168 compl_length = (IOSIZE - MIN_SPACE);
5169 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005171 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5172 if (compl_length < 1)
5173 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005175 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005176 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005178 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005181 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005183 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005185 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005187 compl_cont_status = 0;
5188 compl_cont_status |= CONT_N_ADDS;
5189 compl_startpos = curwin->w_cursor;
5190 startcol = (int)curs_col;
5191 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193
5194 /* Work out completion pattern and original text -- webb */
5195 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5196 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005197 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5199 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005200 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005202 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005204 compl_col += ++startcol;
5205 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
5207 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005208 compl_pattern = str_foldcase(line + compl_col,
5209 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005211 compl_pattern = vim_strnsave(line + compl_col,
5212 compl_length);
5213 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 return FAIL;
5215 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005216 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 char_u *prefix = (char_u *)"\\<";
5219
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005220 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005221 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005222 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005225 if (!vim_iswordp(line + compl_col)
5226 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 && (
5228#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005229 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005231 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232#endif
5233 )))
5234 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005235 STRCPY((char *)compl_pattern, prefix);
5236 (void)quote_meta(compl_pattern + STRLEN(prefix),
5237 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005239 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005241 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005243 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#endif
5245 )
5246 {
5247 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005248 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5249 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005251 compl_col += curs_col;
5252 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 }
5254 else
5255 {
5256#ifdef FEAT_MBYTE
5257 /* Search the point of change class of multibyte character
5258 * or not a word single byte character backward. */
5259 if (has_mbyte)
5260 {
5261 int base_class;
5262 int head_off;
5263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005264 startcol -= (*mb_head_off)(line, line + startcol);
5265 base_class = mb_get_class(line + startcol);
5266 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005268 head_off = (*mb_head_off)(line, line + startcol);
5269 if (base_class != mb_get_class(line + startcol
5270 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005272 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 }
5274 }
5275 else
5276#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005277 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005279 compl_col += ++startcol;
5280 compl_length = (int)curs_col - startcol;
5281 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
5283 /* Only match word with at least two chars -- webb
5284 * there's no need to call quote_meta,
5285 * alloc(7) is enough -- Acevedo
5286 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005287 compl_pattern = alloc(7);
5288 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005290 STRCPY((char *)compl_pattern, "\\<");
5291 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5292 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
5294 else
5295 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005296 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005297 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005298 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005300 STRCPY((char *)compl_pattern, "\\<");
5301 (void)quote_meta(compl_pattern + 2, line + compl_col,
5302 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 }
5304 }
5305 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005306 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005308 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005309 compl_length = (int)curs_col - (int)compl_col;
5310 if (compl_length < 0) /* cursor in indent: empty pattern */
5311 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 compl_pattern = str_foldcase(line + compl_col, compl_length,
5314 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005316 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5317 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 return FAIL;
5319 }
5320 else if (ctrl_x_mode == CTRL_X_FILES)
5321 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005322 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005323 if (startcol > 0)
5324 {
5325 char_u *p = line + startcol;
5326
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005327 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005328 while (p > line && vim_isfilec(PTR2CHAR(p)))
5329 mb_ptr_back(line, p);
5330 if (p == line && vim_isfilec(PTR2CHAR(p)))
5331 startcol = 0;
5332 else
5333 startcol = (int)(p - line) + 1;
5334 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005335
Bram Moolenaar0300e462013-09-08 16:03:45 +02005336 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 compl_length = (int)curs_col - startcol;
5338 compl_pattern = addstar(line + compl_col, compl_length,
5339 EXPAND_FILES);
5340 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 return FAIL;
5342 }
5343 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5344 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005345 compl_pattern = vim_strnsave(line, curs_col);
5346 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005349 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005350 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5351 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005352 /* No completion possible, use an empty pattern to get a
5353 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005354 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005355 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005356 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5357 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005359 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005360 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005361#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005362 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005363 * Call user defined function 'completefunc' with "a:findstart"
5364 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005365 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005366 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005367 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005368 char_u *funcname;
5369 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005370 win_T *curwin_save;
5371 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005372
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005373 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005374 * string */
5375 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5376 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5377 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005378 {
5379 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5380 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005381 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005382 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005383
5384 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005385 args[1] = NULL;
5386 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005387 curwin_save = curwin;
5388 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005389 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005390 if (curwin_save != curwin || curbuf_save != curbuf)
5391 {
5392 EMSG(_(e_complwin));
5393 return FAIL;
5394 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005395 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005396 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005397 if (!equalpos(curwin->w_cursor, pos))
5398 {
5399 EMSG(_(e_compldel));
5400 return FAIL;
5401 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005402
Bram Moolenaar6110a002012-01-26 18:58:38 +01005403 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005404 * cancel the complete without an error.
5405 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005406 if (col == -2)
5407 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005408 if (col == -3)
5409 {
5410 ctrl_x_mode = 0;
5411 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005412 if (!shortmess(SHM_COMPLETIONMENU))
5413 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005414 return FAIL;
5415 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005416
Bram Moolenaar82139082011-09-14 16:52:09 +02005417 /*
5418 * Reset extended parameters of completion, when start new
5419 * completion.
5420 */
5421 compl_opt_refresh_always = FALSE;
5422
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005423 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005424 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005425 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005426 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005427 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005428
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005429 /* Setup variables for completion. Need to obtain "line" again,
5430 * it may have become invalid. */
5431 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005432 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005433 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5434 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005435#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 return FAIL;
5437 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005438 else if (ctrl_x_mode == CTRL_X_SPELL)
5439 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005440#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005441 if (spell_bad_len > 0)
5442 compl_col = curs_col - spell_bad_len;
5443 else
5444 compl_col = spell_word_start(startcol);
5445 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005446 {
5447 compl_length = 0;
5448 compl_col = curs_col;
5449 }
5450 else
5451 {
5452 spell_expand_check_cap(compl_col);
5453 compl_length = (int)curs_col - compl_col;
5454 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005455 /* Need to obtain "line" again, it may have become invalid. */
5456 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005457 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5458 if (compl_pattern == NULL)
5459#endif
5460 return FAIL;
5461 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005462 else
5463 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005464 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005465 return FAIL;
5466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005468 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {
5470 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005471 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 {
5473 /* Insert a new line, keep indentation but ignore 'comments' */
5474#ifdef FEAT_COMMENTS
5475 char_u *old = curbuf->b_p_com;
5476
5477 curbuf->b_p_com = (char_u *)"";
5478#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005479 compl_startpos.lnum = curwin->w_cursor.lnum;
5480 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 ins_eol('\r');
5482#ifdef FEAT_COMMENTS
5483 curbuf->b_p_com = old;
5484#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005485 compl_length = 0;
5486 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488 }
5489 else
5490 {
5491 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005492 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 }
5494
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005495 if (compl_cont_status & CONT_LOCAL)
5496 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 else
5498 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5499
Bram Moolenaar62951b12011-09-21 18:23:05 +02005500 /* If any of the original typed text has been changed we need to fix
5501 * the redo buffer. */
5502 ins_compl_fixRedoBufForLeader(NULL);
5503
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005504 /* Always add completion for the original text. */
5505 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005506 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5507 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005508 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005510 vim_free(compl_pattern);
5511 compl_pattern = NULL;
5512 vim_free(compl_orig_text);
5513 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 return FAIL;
5515 }
5516
5517 /* showmode might reset the internal line pointers, so it must
5518 * be called before line = ml_get(), or when this address is no
5519 * longer needed. -- Acevedo.
5520 */
5521 edit_submode_extra = (char_u *)_("-- Searching...");
5522 edit_submode_highl = HLF_COUNT;
5523 showmode();
5524 edit_submode_extra = NULL;
5525 out_flush();
5526 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005527 else if (insert_match && stop_arrow() == FAIL)
5528 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005530 compl_shown_match = compl_curr_match;
5531 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
5533 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005534 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005536 save_w_wrow = curwin->w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005537 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005539 /* may undisplay the popup menu */
5540 ins_compl_upd_pum();
5541
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005542 if (n > 1) /* all matches have been found */
5543 compl_matches = n;
5544 compl_curr_match = compl_shown_match;
5545 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
Bram Moolenaard68071d2006-05-02 22:08:30 +00005547 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5548 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 if (got_int && !global_busy)
5550 {
5551 (void)vgetc();
5552 got_int = FALSE;
5553 }
5554
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005555 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005556 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005558 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5559 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5561 edit_submode_highl = HLF_E;
5562 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5563 * because we couldn't expand anything at first place, but if we used
5564 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5565 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005566 if ( compl_length > 1
5567 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 || (ctrl_x_mode != 0
5569 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5570 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005571 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
5573
Bram Moolenaar572cb562005-08-05 21:35:02 +00005574 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005575 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005577 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
5579 if (edit_submode_extra == NULL)
5580 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005581 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 {
5583 edit_submode_extra = (char_u *)_("Back at original");
5584 edit_submode_highl = HLF_W;
5585 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005586 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
5588 edit_submode_extra = (char_u *)_("Word from other line");
5589 edit_submode_highl = HLF_COUNT;
5590 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005591 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 {
5593 edit_submode_extra = (char_u *)_("The only match");
5594 edit_submode_highl = HLF_COUNT;
5595 }
5596 else
5597 {
5598 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005599 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005601 int number = 0;
5602 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005604 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 {
5606 /* search backwards for the first valid (!= -1) number.
5607 * This should normally succeed already at the first loop
5608 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005609 for (match = compl_curr_match->cp_prev; match != NULL
5610 && match != compl_first_match;
5611 match = match->cp_prev)
5612 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005614 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 break;
5616 }
5617 if (match != NULL)
5618 /* go up and assign all numbers which are not assigned
5619 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005620 for (match = match->cp_next;
5621 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005622 match = match->cp_next)
5623 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 }
5625 else /* BACKWARD */
5626 {
5627 /* search forwards (upwards) for the first valid (!= -1)
5628 * number. This should normally succeed already at the
5629 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005630 for (match = compl_curr_match->cp_next; match != NULL
5631 && match != compl_first_match;
5632 match = match->cp_next)
5633 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005635 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 break;
5637 }
5638 if (match != NULL)
5639 /* go down and assign all numbers which are not
5640 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005641 for (match = match->cp_prev; match
5642 && match->cp_number == -1;
5643 match = match->cp_prev)
5644 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 }
5646 }
5647
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005648 /* The match should always have a sequence number now, this is
5649 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005650 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005652 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5653 * Translations may need more than twice that. */
5654 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005656 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005657 vim_snprintf((char *)match_ref, sizeof(match_ref),
5658 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005659 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005661 vim_snprintf((char *)match_ref, sizeof(match_ref),
5662 _("match %d"),
5663 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 edit_submode_extra = match_ref;
5665 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005666 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 curs_columns(FALSE);
5668 }
5669 }
5670 }
5671
5672 /* Show a message about what (completion) mode we're in. */
5673 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005674 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005676 if (edit_submode_extra != NULL)
5677 {
5678 if (!p_smd)
5679 msg_attr(edit_submode_extra,
5680 edit_submode_highl < HLF_COUNT
5681 ? hl_attr(edit_submode_highl) : 0);
5682 }
5683 else
5684 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
Bram Moolenaard68071d2006-05-02 22:08:30 +00005687 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005688 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005689 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005690 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005691 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005692 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005693 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005694
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 return OK;
5696}
5697
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005698 static void
5699show_pum(int save_w_wrow)
5700{
5701 /* RedrawingDisabled may be set when invoked through complete(). */
5702 int n = RedrawingDisabled;
5703
5704 RedrawingDisabled = 0;
5705
5706 /* If the cursor moved we need to remove the pum first. */
5707 setcursor();
5708 if (save_w_wrow != curwin->w_wrow)
5709 ins_compl_del_pum();
5710
5711 ins_compl_show_pum();
5712 setcursor();
5713 RedrawingDisabled = n;
5714}
5715
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716/*
5717 * Looks in the first "len" chars. of "src" for search-metachars.
5718 * If dest is not NULL the chars. are copied there quoting (with
5719 * a backslash) the metachars, and dest would be NUL terminated.
5720 * Returns the length (needed) of dest
5721 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005722 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005723quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005725 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005727 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
5729 switch (*src)
5730 {
5731 case '.':
5732 case '*':
5733 case '[':
5734 if (ctrl_x_mode == CTRL_X_DICTIONARY
5735 || ctrl_x_mode == CTRL_X_THESAURUS)
5736 break;
5737 case '~':
5738 if (!p_magic) /* quote these only if magic is set */
5739 break;
5740 case '\\':
5741 if (ctrl_x_mode == CTRL_X_DICTIONARY
5742 || ctrl_x_mode == CTRL_X_THESAURUS)
5743 break;
5744 case '^': /* currently it's not needed. */
5745 case '$':
5746 m++;
5747 if (dest != NULL)
5748 *dest++ = '\\';
5749 break;
5750 }
5751 if (dest != NULL)
5752 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005753# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 /* Copy remaining bytes of a multibyte character. */
5755 if (has_mbyte)
5756 {
5757 int i, mb_len;
5758
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005759 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 if (mb_len > 0 && len >= mb_len)
5761 for (i = 0; i < mb_len; ++i)
5762 {
5763 --len;
5764 ++src;
5765 if (dest != NULL)
5766 *dest++ = *src;
5767 }
5768 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
5771 if (dest != NULL)
5772 *dest = NUL;
5773
5774 return m;
5775}
5776#endif /* FEAT_INS_EXPAND */
5777
5778/*
5779 * Next character is interpreted literally.
5780 * A one, two or three digit decimal number is interpreted as its byte value.
5781 * If one or two digits are entered, the next character is given to vungetc().
5782 * For Unicode a character > 255 may be returned.
5783 */
5784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005785get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787 int cc;
5788 int nc;
5789 int i;
5790 int hex = FALSE;
5791 int octal = FALSE;
5792#ifdef FEAT_MBYTE
5793 int unicode = 0;
5794#endif
5795
5796 if (got_int)
5797 return Ctrl_C;
5798
5799#ifdef FEAT_GUI
5800 /*
5801 * In GUI there is no point inserting the internal code for a special key.
5802 * It is more useful to insert the string "<KEY>" instead. This would
5803 * probably be useful in a text window too, but it would not be
5804 * vi-compatible (maybe there should be an option for it?) -- webb
5805 */
5806 if (gui.in_use)
5807 ++allow_keys;
5808#endif
5809#ifdef USE_ON_FLY_SCROLL
5810 dont_scroll = TRUE; /* disallow scrolling here */
5811#endif
5812 ++no_mapping; /* don't map the next key hits */
5813 cc = 0;
5814 i = 0;
5815 for (;;)
5816 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005817 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818#ifdef FEAT_CMDL_INFO
5819 if (!(State & CMDLINE)
5820# ifdef FEAT_MBYTE
5821 && MB_BYTE2LEN_CHECK(nc) == 1
5822# endif
5823 )
5824 add_to_showcmd(nc);
5825#endif
5826 if (nc == 'x' || nc == 'X')
5827 hex = TRUE;
5828 else if (nc == 'o' || nc == 'O')
5829 octal = TRUE;
5830#ifdef FEAT_MBYTE
5831 else if (nc == 'u' || nc == 'U')
5832 unicode = nc;
5833#endif
5834 else
5835 {
5836 if (hex
5837#ifdef FEAT_MBYTE
5838 || unicode != 0
5839#endif
5840 )
5841 {
5842 if (!vim_isxdigit(nc))
5843 break;
5844 cc = cc * 16 + hex2nr(nc);
5845 }
5846 else if (octal)
5847 {
5848 if (nc < '0' || nc > '7')
5849 break;
5850 cc = cc * 8 + nc - '0';
5851 }
5852 else
5853 {
5854 if (!VIM_ISDIGIT(nc))
5855 break;
5856 cc = cc * 10 + nc - '0';
5857 }
5858
5859 ++i;
5860 }
5861
5862 if (cc > 255
5863#ifdef FEAT_MBYTE
5864 && unicode == 0
5865#endif
5866 )
5867 cc = 255; /* limit range to 0-255 */
5868 nc = 0;
5869
5870 if (hex) /* hex: up to two chars */
5871 {
5872 if (i >= 2)
5873 break;
5874 }
5875#ifdef FEAT_MBYTE
5876 else if (unicode) /* Unicode: up to four or eight chars */
5877 {
5878 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5879 break;
5880 }
5881#endif
5882 else if (i >= 3) /* decimal or octal: up to three chars */
5883 break;
5884 }
5885 if (i == 0) /* no number entered */
5886 {
5887 if (nc == K_ZERO) /* NUL is stored as NL */
5888 {
5889 cc = '\n';
5890 nc = 0;
5891 }
5892 else
5893 {
5894 cc = nc;
5895 nc = 0;
5896 }
5897 }
5898
5899 if (cc == 0) /* NUL is stored as NL */
5900 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005901#ifdef FEAT_MBYTE
5902 if (enc_dbcs && (cc & 0xff) == 0)
5903 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5904 second byte will cause trouble! */
5905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906
5907 --no_mapping;
5908#ifdef FEAT_GUI
5909 if (gui.in_use)
5910 --allow_keys;
5911#endif
5912 if (nc)
5913 vungetc(nc);
5914 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5915 return cc;
5916}
5917
5918/*
5919 * Insert character, taking care of special keys and mod_mask
5920 */
5921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005922insert_special(
5923 int c,
5924 int allow_modmask,
5925 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
5927 char_u *p;
5928 int len;
5929
5930 /*
5931 * Special function key, translate into "<Key>". Up to the last '>' is
5932 * inserted with ins_str(), so as not to replace characters in replace
5933 * mode.
5934 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5935 * unless 'allow_modmask' is TRUE.
5936 */
5937#ifdef MACOS
5938 /* Command-key never produces a normal key */
5939 if (mod_mask & MOD_MASK_CMD)
5940 allow_modmask = TRUE;
5941#endif
5942 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5943 {
5944 p = get_special_key_name(c, mod_mask);
5945 len = (int)STRLEN(p);
5946 c = p[len - 1];
5947 if (len > 2)
5948 {
5949 if (stop_arrow() == FAIL)
5950 return;
5951 p[len - 1] = NUL;
5952 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005953 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 ctrlv = FALSE;
5955 }
5956 }
5957 if (stop_arrow() == OK)
5958 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5959}
5960
5961/*
5962 * Special characters in this context are those that need processing other
5963 * than the simple insertion that can be performed here. This includes ESC
5964 * which terminates the insert, and CR/NL which need special processing to
5965 * open up a new line. This routine tries to optimize insertions performed by
5966 * the "redo", "undo" or "put" commands, so it needs to know when it should
5967 * stop and defer processing to the "normal" mechanism.
5968 * '0' and '^' are special, because they can be followed by CTRL-D.
5969 */
5970#ifdef EBCDIC
5971# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5972#else
5973# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5974#endif
5975
5976#ifdef FEAT_MBYTE
5977# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5978#else
5979# define WHITECHAR(cc) vim_iswhite(cc)
5980#endif
5981
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005982/*
5983 * "flags": INSCHAR_FORMAT - force formatting
5984 * INSCHAR_CTRLV - char typed just after CTRL-V
5985 * INSCHAR_NO_FEX - don't use 'formatexpr'
5986 *
5987 * NOTE: passes the flags value straight through to internal_format() which,
5988 * beside INSCHAR_FORMAT (above), is also looking for these:
5989 * INSCHAR_DO_COM - format comments
5990 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005993insertchar(
5994 int c, /* character to insert or NUL */
5995 int flags, /* INSCHAR_FORMAT, etc. */
5996 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 int textwidth;
5999#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006003 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006005 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007
6008 /*
6009 * Try to break the line in two or more pieces when:
6010 * - Always do this if we have been called to do formatting only.
6011 * - Always do this when 'formatoptions' has the 'a' flag and the line
6012 * ends in white space.
6013 * - Otherwise:
6014 * - Don't do this if inserting a blank
6015 * - Don't do this if an existing character is being replaced, unless
6016 * we're in VREPLACE mode.
6017 * - Do this if the cursor is not on the line where insert started
6018 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6019 * before the insert.
6020 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6021 * before 'textwidth'
6022 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006023 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006024 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 || (!vim_iswhite(c)
6026 && !((State & REPLACE_FLAG)
6027#ifdef FEAT_VREPLACE
6028 && !(State & VREPLACE_FLAG)
6029#endif
6030 && *ml_get_cursor() != NUL)
6031 && (curwin->w_cursor.lnum != Insstart.lnum
6032 || ((!has_format_option(FO_INS_LONG)
6033 || Insstart_textlen <= (colnr_T)textwidth)
6034 && (!fo_ins_blank
6035 || Insstart_blank_vcol <= (colnr_T)textwidth
6036 ))))))
6037 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006038 /* Format with 'formatexpr' when it's set. Use internal formatting
6039 * when 'formatexpr' isn't set or it returns non-zero. */
6040#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006041 int do_internal = TRUE;
6042 colnr_T virtcol = get_nolist_virtcol()
6043 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006044
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006045 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6046 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006047 {
6048 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6049 /* It may be required to save for undo again, e.g. when setline()
6050 * was called. */
6051 ins_need_undo = TRUE;
6052 }
6053 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006055 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006057
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 if (c == NUL) /* only formatting was wanted */
6059 return;
6060
6061#ifdef FEAT_COMMENTS
6062 /* Check whether this character should end a comment. */
6063 if (did_ai && (int)c == end_comment_pending)
6064 {
6065 char_u *line;
6066 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6067 int middle_len, end_len;
6068 int i;
6069
6070 /*
6071 * Need to remove existing (middle) comment leader and insert end
6072 * comment leader. First, check what comment leader we can find.
6073 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006074 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6076 {
6077 /* Skip middle-comment string */
6078 while (*p && p[-1] != ':') /* find end of middle flags */
6079 ++p;
6080 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6081 /* Don't count trailing white space for middle_len */
6082 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6083 --middle_len;
6084
6085 /* Find the end-comment string */
6086 while (*p && p[-1] != ':') /* find end of end flags */
6087 ++p;
6088 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6089
6090 /* Skip white space before the cursor */
6091 i = curwin->w_cursor.col;
6092 while (--i >= 0 && vim_iswhite(line[i]))
6093 ;
6094 i++;
6095
6096 /* Skip to before the middle leader */
6097 i -= middle_len;
6098
6099 /* Check some expected things before we go on */
6100 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6101 {
6102 /* Backspace over all the stuff we want to replace */
6103 backspace_until_column(i);
6104
6105 /*
6106 * Insert the end-comment string, except for the last
6107 * character, which will get inserted as normal later.
6108 */
6109 ins_bytes_len(lead_end, end_len - 1);
6110 }
6111 }
6112 }
6113 end_comment_pending = NUL;
6114#endif
6115
6116 did_ai = FALSE;
6117#ifdef FEAT_SMARTINDENT
6118 did_si = FALSE;
6119 can_si = FALSE;
6120 can_si_back = FALSE;
6121#endif
6122
6123 /*
6124 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6125 * This speeds up normal text input considerably.
6126 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6127 * need to re-indent at a ':', or any other character (but not what
6128 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006129 * Don't do this when there an InsertCharPre autocommand is defined,
6130 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 */
6132#ifdef USE_ON_FLY_SCROLL
6133 dont_scroll = FALSE; /* allow scrolling here */
6134#endif
6135
6136 if ( !ISSPECIAL(c)
6137#ifdef FEAT_MBYTE
6138 && (!has_mbyte || (*mb_char2len)(c) == 1)
6139#endif
6140 && vpeekc() != NUL
6141 && !(State & REPLACE_FLAG)
6142#ifdef FEAT_CINDENT
6143 && !cindent_on()
6144#endif
6145#ifdef FEAT_RIGHTLEFT
6146 && !p_ri
6147#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006148#ifdef FEAT_AUTOCMD
6149 && !has_insertcharpre()
6150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 )
6152 {
6153#define INPUT_BUFLEN 100
6154 char_u buf[INPUT_BUFLEN + 1];
6155 int i;
6156 colnr_T virtcol = 0;
6157
6158 buf[0] = c;
6159 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006160 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 virtcol = get_nolist_virtcol();
6162 /*
6163 * Stop the string when:
6164 * - no more chars available
6165 * - finding a special character (command key)
6166 * - buffer is full
6167 * - running into the 'textwidth' boundary
6168 * - need to check for abbreviation: A non-word char after a word-char
6169 */
6170 while ( (c = vpeekc()) != NUL
6171 && !ISSPECIAL(c)
6172#ifdef FEAT_MBYTE
6173 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6174#endif
6175 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006176# ifdef FEAT_FKMAP
6177 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 && (textwidth == 0
6180 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6181 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6182 {
6183#ifdef FEAT_RIGHTLEFT
6184 c = vgetc();
6185 if (p_hkmap && KeyTyped)
6186 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 buf[i++] = c;
6188#else
6189 buf[i++] = vgetc();
6190#endif
6191 }
6192
6193#ifdef FEAT_DIGRAPHS
6194 do_digraph(-1); /* clear digraphs */
6195 do_digraph(buf[i-1]); /* may be the start of a digraph */
6196#endif
6197 buf[i] = NUL;
6198 ins_str(buf);
6199 if (flags & INSCHAR_CTRLV)
6200 {
6201 redo_literal(*buf);
6202 i = 1;
6203 }
6204 else
6205 i = 0;
6206 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006207 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 }
6209 else
6210 {
6211#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006212 int cc;
6213
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6215 {
6216 char_u buf[MB_MAXBYTES + 1];
6217
6218 (*mb_char2bytes)(c, buf);
6219 buf[cc] = NUL;
6220 ins_char_bytes(buf, cc);
6221 AppendCharToRedobuff(c);
6222 }
6223 else
6224#endif
6225 {
6226 ins_char(c);
6227 if (flags & INSCHAR_CTRLV)
6228 redo_literal(c);
6229 else
6230 AppendCharToRedobuff(c);
6231 }
6232 }
6233}
6234
6235/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006236 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006237 *
6238 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6239 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006240 */
6241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006242internal_format(
6243 int textwidth,
6244 int second_indent,
6245 int flags,
6246 int format_only,
6247 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006248{
6249 int cc;
6250 int save_char = NUL;
6251 int haveto_redraw = FALSE;
6252 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6253#ifdef FEAT_MBYTE
6254 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6255#endif
6256 int fo_white_par = has_format_option(FO_WHITE_PAR);
6257 int first_line = TRUE;
6258#ifdef FEAT_COMMENTS
6259 colnr_T leader_len;
6260 int no_leader = FALSE;
6261 int do_comments = (flags & INSCHAR_DO_COM);
6262#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006263#ifdef FEAT_LINEBREAK
6264 int has_lbr = curwin->w_p_lbr;
6265
6266 /* make sure win_lbr_chartabsize() counts correctly */
6267 curwin->w_p_lbr = FALSE;
6268#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006269
6270 /*
6271 * When 'ai' is off we don't want a space under the cursor to be
6272 * deleted. Replace it with an 'x' temporarily.
6273 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006274 if (!curbuf->b_p_ai
6275#ifdef FEAT_VREPLACE
6276 && !(State & VREPLACE_FLAG)
6277#endif
6278 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006279 {
6280 cc = gchar_cursor();
6281 if (vim_iswhite(cc))
6282 {
6283 save_char = cc;
6284 pchar_cursor('x');
6285 }
6286 }
6287
6288 /*
6289 * Repeat breaking lines, until the current line is not too long.
6290 */
6291 while (!got_int)
6292 {
6293 int startcol; /* Cursor column at entry */
6294 int wantcol; /* column at textwidth border */
6295 int foundcol; /* column for start of spaces */
6296 int end_foundcol = 0; /* column for start of word */
6297 colnr_T len;
6298 colnr_T virtcol;
6299#ifdef FEAT_VREPLACE
6300 int orig_col = 0;
6301 char_u *saved_text = NULL;
6302#endif
6303 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006304 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006305
Bram Moolenaar97b98102009-11-17 16:41:01 +00006306 virtcol = get_nolist_virtcol()
6307 + char2cells(c != NUL ? c : gchar_cursor());
6308 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006309 break;
6310
6311#ifdef FEAT_COMMENTS
6312 if (no_leader)
6313 do_comments = FALSE;
6314 else if (!(flags & INSCHAR_FORMAT)
6315 && has_format_option(FO_WRAP_COMS))
6316 do_comments = TRUE;
6317
6318 /* Don't break until after the comment leader */
6319 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006320 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006321 else
6322 leader_len = 0;
6323
6324 /* If the line doesn't start with a comment leader, then don't
6325 * start one in a following broken line. Avoids that a %word
6326 * moved to the start of the next line causes all following lines
6327 * to start with %. */
6328 if (leader_len == 0)
6329 no_leader = TRUE;
6330#endif
6331 if (!(flags & INSCHAR_FORMAT)
6332#ifdef FEAT_COMMENTS
6333 && leader_len == 0
6334#endif
6335 && !has_format_option(FO_WRAP))
6336
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006337 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006338 if ((startcol = curwin->w_cursor.col) == 0)
6339 break;
6340
6341 /* find column of textwidth border */
6342 coladvance((colnr_T)textwidth);
6343 wantcol = curwin->w_cursor.col;
6344
Bram Moolenaar97b98102009-11-17 16:41:01 +00006345 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006346 foundcol = 0;
6347
6348 /*
6349 * Find position to break at.
6350 * Stop at first entered white when 'formatoptions' has 'v'
6351 */
6352 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006353 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006354 || curwin->w_cursor.lnum != Insstart.lnum
6355 || curwin->w_cursor.col >= Insstart.col)
6356 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006357 if (curwin->w_cursor.col == startcol && c != NUL)
6358 cc = c;
6359 else
6360 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006361 if (WHITECHAR(cc))
6362 {
6363 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006364 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006365
6366 /* find start of sequence of blanks */
6367 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6368 {
6369 dec_cursor();
6370 cc = gchar_cursor();
6371 }
6372 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6373 break; /* only spaces in front of text */
6374#ifdef FEAT_COMMENTS
6375 /* Don't break until after the comment leader */
6376 if (curwin->w_cursor.col < leader_len)
6377 break;
6378#endif
6379 if (has_format_option(FO_ONE_LETTER))
6380 {
6381 /* do not break after one-letter words */
6382 if (curwin->w_cursor.col == 0)
6383 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006384#ifdef FEAT_COMMENTS
6385 /* do not break "#a b" when 'tw' is 2 */
6386 if (curwin->w_cursor.col <= leader_len)
6387 break;
6388#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006389 col = curwin->w_cursor.col;
6390 dec_cursor();
6391 cc = gchar_cursor();
6392
6393 if (WHITECHAR(cc))
6394 continue; /* one-letter, continue */
6395 curwin->w_cursor.col = col;
6396 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006397
6398 inc_cursor();
6399
6400 end_foundcol = end_col + 1;
6401 foundcol = curwin->w_cursor.col;
6402 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006403 break;
6404 }
6405#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006406 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006407 {
6408 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006409 if (curwin->w_cursor.col != startcol)
6410 {
6411#ifdef FEAT_COMMENTS
6412 /* Don't break until after the comment leader */
6413 if (curwin->w_cursor.col < leader_len)
6414 break;
6415#endif
6416 col = curwin->w_cursor.col;
6417 inc_cursor();
6418 /* Don't change end_foundcol if already set. */
6419 if (foundcol != curwin->w_cursor.col)
6420 {
6421 foundcol = curwin->w_cursor.col;
6422 end_foundcol = foundcol;
6423 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6424 break;
6425 }
6426 curwin->w_cursor.col = col;
6427 }
6428
6429 if (curwin->w_cursor.col == 0)
6430 break;
6431
6432 col = curwin->w_cursor.col;
6433
6434 dec_cursor();
6435 cc = gchar_cursor();
6436
6437 if (WHITECHAR(cc))
6438 continue; /* break with space */
6439#ifdef FEAT_COMMENTS
6440 /* Don't break until after the comment leader */
6441 if (curwin->w_cursor.col < leader_len)
6442 break;
6443#endif
6444
6445 curwin->w_cursor.col = col;
6446
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006447 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006448 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006449 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6450 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006451 }
6452#endif
6453 if (curwin->w_cursor.col == 0)
6454 break;
6455 dec_cursor();
6456 }
6457
6458 if (foundcol == 0) /* no spaces, cannot break line */
6459 {
6460 curwin->w_cursor.col = startcol;
6461 break;
6462 }
6463
6464 /* Going to break the line, remove any "$" now. */
6465 undisplay_dollar();
6466
6467 /*
6468 * Offset between cursor position and line break is used by replace
6469 * stack functions. VREPLACE does not use this, and backspaces
6470 * over the text instead.
6471 */
6472#ifdef FEAT_VREPLACE
6473 if (State & VREPLACE_FLAG)
6474 orig_col = startcol; /* Will start backspacing from here */
6475 else
6476#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006477 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006478
6479 /*
6480 * adjust startcol for spaces that will be deleted and
6481 * characters that will remain on top line
6482 */
6483 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006484 while ((cc = gchar_cursor(), WHITECHAR(cc))
6485 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006486 inc_cursor();
6487 startcol -= curwin->w_cursor.col;
6488 if (startcol < 0)
6489 startcol = 0;
6490
6491#ifdef FEAT_VREPLACE
6492 if (State & VREPLACE_FLAG)
6493 {
6494 /*
6495 * In VREPLACE mode, we will backspace over the text to be
6496 * wrapped, so save a copy now to put on the next line.
6497 */
6498 saved_text = vim_strsave(ml_get_cursor());
6499 curwin->w_cursor.col = orig_col;
6500 if (saved_text == NULL)
6501 break; /* Can't do it, out of memory */
6502 saved_text[startcol] = NUL;
6503
6504 /* Backspace over characters that will move to the next line */
6505 if (!fo_white_par)
6506 backspace_until_column(foundcol);
6507 }
6508 else
6509#endif
6510 {
6511 /* put cursor after pos. to break line */
6512 if (!fo_white_par)
6513 curwin->w_cursor.col = foundcol;
6514 }
6515
6516 /*
6517 * Split the line just before the margin.
6518 * Only insert/delete lines, but don't really redraw the window.
6519 */
6520 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6521 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6522#ifdef FEAT_COMMENTS
6523 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006524 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006525#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006526 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6527 if (!(flags & INSCHAR_COM_LIST))
6528 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006529
6530 replace_offset = 0;
6531 if (first_line)
6532 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006533 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006534 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006535 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006536 * This section is for auto-wrap of numeric lists. When not
6537 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6538 * flag will be set and open_line() will handle it (as seen
6539 * above). The code here (and in get_number_indent()) will
6540 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006541 */
6542 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006543 second_indent =
6544 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006545 if (second_indent >= 0)
6546 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006547#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006548 if (State & VREPLACE_FLAG)
6549 change_indent(INDENT_SET, second_indent,
6550 FALSE, NUL, TRUE);
6551 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006552#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006553#ifdef FEAT_COMMENTS
6554 if (leader_len > 0 && second_indent - leader_len > 0)
6555 {
6556 int i;
6557 int padding = second_indent - leader_len;
6558
6559 /* We started at the first_line of a numbered list
6560 * that has a comment. the open_line() function has
6561 * inserted the proper comment leader and positioned
6562 * the cursor at the end of the split line. Now we
6563 * add the additional whitespace needed after the
6564 * comment leader for the numbered list. */
6565 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006566 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006567 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006568 }
6569 else
6570 {
6571#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006572 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006573#ifdef FEAT_COMMENTS
6574 }
6575#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006576 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006577 }
6578 first_line = FALSE;
6579 }
6580
6581#ifdef FEAT_VREPLACE
6582 if (State & VREPLACE_FLAG)
6583 {
6584 /*
6585 * In VREPLACE mode we have backspaced over the text to be
6586 * moved, now we re-insert it into the new line.
6587 */
6588 ins_bytes(saved_text);
6589 vim_free(saved_text);
6590 }
6591 else
6592#endif
6593 {
6594 /*
6595 * Check if cursor is not past the NUL off the line, cindent
6596 * may have added or removed indent.
6597 */
6598 curwin->w_cursor.col += startcol;
6599 len = (colnr_T)STRLEN(ml_get_curline());
6600 if (curwin->w_cursor.col > len)
6601 curwin->w_cursor.col = len;
6602 }
6603
6604 haveto_redraw = TRUE;
6605#ifdef FEAT_CINDENT
6606 can_cindent = TRUE;
6607#endif
6608 /* moved the cursor, don't autoindent or cindent now */
6609 did_ai = FALSE;
6610#ifdef FEAT_SMARTINDENT
6611 did_si = FALSE;
6612 can_si = FALSE;
6613 can_si_back = FALSE;
6614#endif
6615 line_breakcheck();
6616 }
6617
6618 if (save_char != NUL) /* put back space after cursor */
6619 pchar_cursor(save_char);
6620
Bram Moolenaar0026d472014-09-09 16:32:39 +02006621#ifdef FEAT_LINEBREAK
6622 curwin->w_p_lbr = has_lbr;
6623#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006624 if (!format_only && haveto_redraw)
6625 {
6626 update_topline();
6627 redraw_curbuf_later(VALID);
6628 }
6629}
6630
6631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 * Called after inserting or deleting text: When 'formatoptions' includes the
6633 * 'a' flag format from the current line until the end of the paragraph.
6634 * Keep the cursor at the same position relative to the text.
6635 * The caller must have saved the cursor line for undo, following ones will be
6636 * saved here.
6637 */
6638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006639auto_format(
6640 int trailblank, /* when TRUE also format with trailing blank */
6641 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
6643 pos_T pos;
6644 colnr_T len;
6645 char_u *old;
6646 char_u *new, *pnew;
6647 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006648 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649
6650 if (!has_format_option(FO_AUTO))
6651 return;
6652
6653 pos = curwin->w_cursor;
6654 old = ml_get_curline();
6655
6656 /* may remove added space */
6657 check_auto_format(FALSE);
6658
6659 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6660 * user might insert normal text next. Also skip formatting when "1" is
6661 * in 'formatoptions' and there is a single character before the cursor.
6662 * Otherwise the line would be broken and when typing another non-white
6663 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006664 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 if (*old != NUL && !trailblank && wasatend)
6666 {
6667 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006668 cc = gchar_cursor();
6669 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6670 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006672 cc = gchar_cursor();
6673 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {
6675 curwin->w_cursor = pos;
6676 return;
6677 }
6678 curwin->w_cursor = pos;
6679 }
6680
6681#ifdef FEAT_COMMENTS
6682 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6683 * comments. */
6684 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006685 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 return;
6687#endif
6688
6689 /*
6690 * May start formatting in a previous line, so that after "x" a word is
6691 * moved to the previous line if it fits there now. Only when this is not
6692 * the start of a paragraph.
6693 */
6694 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6695 {
6696 --curwin->w_cursor.lnum;
6697 if (u_save_cursor() == FAIL)
6698 return;
6699 }
6700
6701 /*
6702 * Do the formatting and restore the cursor position. "saved_cursor" will
6703 * be adjusted for the text formatting.
6704 */
6705 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006706 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 curwin->w_cursor = saved_cursor;
6708 saved_cursor.lnum = 0;
6709
6710 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6711 {
6712 /* "cannot happen" */
6713 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6714 coladvance((colnr_T)MAXCOL);
6715 }
6716 else
6717 check_cursor_col();
6718
6719 /* Insert mode: If the cursor is now after the end of the line while it
6720 * previously wasn't, the line was broken. Because of the rule above we
6721 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6722 * formatted. */
6723 if (!wasatend && has_format_option(FO_WHITE_PAR))
6724 {
6725 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006726 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 if (curwin->w_cursor.col == len)
6728 {
6729 pnew = vim_strnsave(new, len + 2);
6730 pnew[len] = ' ';
6731 pnew[len + 1] = NUL;
6732 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6733 /* remove the space later */
6734 did_add_space = TRUE;
6735 }
6736 else
6737 /* may remove added space */
6738 check_auto_format(FALSE);
6739 }
6740
6741 check_cursor();
6742}
6743
6744/*
6745 * When an extra space was added to continue a paragraph for auto-formatting,
6746 * delete it now. The space must be under the cursor, just after the insert
6747 * position.
6748 */
6749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006750check_auto_format(
6751 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006754 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
6756 if (did_add_space)
6757 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006758 cc = gchar_cursor();
6759 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 /* Somehow the space was removed already. */
6761 did_add_space = FALSE;
6762 else
6763 {
6764 if (!end_insert)
6765 {
6766 inc_cursor();
6767 c = gchar_cursor();
6768 dec_cursor();
6769 }
6770 if (c != NUL)
6771 {
6772 /* The space is no longer at the end of the line, delete it. */
6773 del_char(FALSE);
6774 did_add_space = FALSE;
6775 }
6776 }
6777 }
6778}
6779
6780/*
6781 * Find out textwidth to be used for formatting:
6782 * if 'textwidth' option is set, use it
6783 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6784 * if invalid value, use 0.
6785 * Set default to window width (maximum 79) for "gq" operator.
6786 */
6787 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006788comp_textwidth(
6789 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790{
6791 int textwidth;
6792
6793 textwidth = curbuf->b_p_tw;
6794 if (textwidth == 0 && curbuf->b_p_wm)
6795 {
6796 /* The width is the window width minus 'wrapmargin' minus all the
6797 * things that add to the margin. */
6798 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6799#ifdef FEAT_CMDWIN
6800 if (cmdwin_type != 0)
6801 textwidth -= 1;
6802#endif
6803#ifdef FEAT_FOLDING
6804 textwidth -= curwin->w_p_fdc;
6805#endif
6806#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006807 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 textwidth -= 1;
6809#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006810 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 textwidth -= 8;
6812 }
6813 if (textwidth < 0)
6814 textwidth = 0;
6815 if (ff && textwidth == 0)
6816 {
6817 textwidth = W_WIDTH(curwin) - 1;
6818 if (textwidth > 79)
6819 textwidth = 79;
6820 }
6821 return textwidth;
6822}
6823
6824/*
6825 * Put a character in the redo buffer, for when just after a CTRL-V.
6826 */
6827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006828redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829{
6830 char_u buf[10];
6831
6832 /* Only digits need special treatment. Translate them into a string of
6833 * three digits. */
6834 if (VIM_ISDIGIT(c))
6835 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006836 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 AppendToRedobuff(buf);
6838 }
6839 else
6840 AppendCharToRedobuff(c);
6841}
6842
6843/*
6844 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006845 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 */
6847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006848start_arrow(
6849 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006851 start_arrow_common(end_insert_pos, TRUE);
6852}
6853
6854/*
6855 * Like start_arrow() but with end_change argument.
6856 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6857 */
6858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006859start_arrow_with_change(
6860 pos_T *end_insert_pos, /* can be NULL */
6861 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006862{
6863 start_arrow_common(end_insert_pos, end_change);
6864 if (!end_change)
6865 {
6866 AppendCharToRedobuff(Ctrl_G);
6867 AppendCharToRedobuff('U');
6868 }
6869}
6870
6871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006872start_arrow_common(
6873 pos_T *end_insert_pos, /* can be NULL */
6874 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006875{
6876 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {
6878 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006879 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 arrow_used = TRUE; /* this means we stopped the current insert */
6881 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006882#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006883 check_spell_redraw();
6884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885}
6886
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006887#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006888/*
6889 * If we skipped highlighting word at cursor, do it now.
6890 * It may be skipped again, thus reset spell_redraw_lnum first.
6891 */
6892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006893check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006894{
6895 if (spell_redraw_lnum != 0)
6896 {
6897 linenr_T lnum = spell_redraw_lnum;
6898
6899 spell_redraw_lnum = 0;
6900 redrawWinline(lnum, FALSE);
6901 }
6902}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006903
6904/*
6905 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6906 * spelled word, if there is one.
6907 */
6908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006909spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006910{
6911 pos_T tpos = curwin->w_cursor;
6912
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006913 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006914 if (curwin->w_cursor.col != tpos.col)
6915 start_arrow(&tpos);
6916}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006917#endif
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919/*
6920 * stop_arrow() is called before a change is made in insert mode.
6921 * If an arrow key has been used, start a new insertion.
6922 * Returns FAIL if undo is impossible, shouldn't insert then.
6923 */
6924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006925stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926{
6927 if (arrow_used)
6928 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006929 Insstart = curwin->w_cursor; /* new insertion starts here */
6930 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6931 /* Don't update the original insert position when moved to the
6932 * right, except when nothing was inserted yet. */
6933 update_Insstart_orig = FALSE;
6934 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 if (u_save_cursor() == OK)
6937 {
6938 arrow_used = FALSE;
6939 ins_need_undo = FALSE;
6940 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006941
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 ai_col = 0;
6943#ifdef FEAT_VREPLACE
6944 if (State & VREPLACE_FLAG)
6945 {
6946 orig_line_count = curbuf->b_ml.ml_line_count;
6947 vr_lines_changed = 1;
6948 }
6949#endif
6950 ResetRedobuff();
6951 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006952 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 }
6954 else if (ins_need_undo)
6955 {
6956 if (u_save_cursor() == OK)
6957 ins_need_undo = FALSE;
6958 }
6959
6960#ifdef FEAT_FOLDING
6961 /* Always open fold at the cursor line when inserting something. */
6962 foldOpenCursor();
6963#endif
6964
6965 return (arrow_used || ins_need_undo ? FAIL : OK);
6966}
6967
6968/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006969 * Do a few things to stop inserting.
6970 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6971 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 */
6973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006974stop_insert(
6975 pos_T *end_insert_pos,
6976 int esc, /* called by ins_esc() */
6977 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006979 int cc;
6980 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
6982 stop_redo_ins();
6983 replace_flush(); /* abandon replace stack */
6984
6985 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006986 * Save the inserted text for later redo with ^@ and CTRL-A.
6987 * Don't do it when "restart_edit" was set and nothing was inserted,
6988 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006990 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006991 if (did_restart_edit == 0 || (ptr != NULL
6992 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006993 {
6994 vim_free(last_insert);
6995 last_insert = ptr;
6996 last_insert_skip = new_insert_skip;
6997 }
6998 else
6999 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007001 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {
7003 /* Auto-format now. It may seem strange to do this when stopping an
7004 * insertion (or moving the cursor), but it's required when appending
7005 * a line and having it end in a space. But only do it when something
7006 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007007 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007009 pos_T tpos = curwin->w_cursor;
7010
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 /* When the cursor is at the end of the line after a space the
7012 * formatting will move it to the following word. Avoid that by
7013 * moving the cursor onto the space. */
7014 cc = 'x';
7015 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7016 {
7017 dec_cursor();
7018 cc = gchar_cursor();
7019 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007020 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 }
7022
7023 auto_format(TRUE, FALSE);
7024
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007025 if (vim_iswhite(cc))
7026 {
7027 if (gchar_cursor() != NUL)
7028 inc_cursor();
7029#ifdef FEAT_VIRTUALEDIT
7030 /* If the cursor is still at the same character, also keep
7031 * the "coladd". */
7032 if (gchar_cursor() == NUL
7033 && curwin->w_cursor.lnum == tpos.lnum
7034 && curwin->w_cursor.col == tpos.col)
7035 curwin->w_cursor.coladd = tpos.coladd;
7036#endif
7037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
7039
7040 /* If a space was inserted for auto-formatting, remove it now. */
7041 check_auto_format(TRUE);
7042
7043 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007044 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007045 * Do this when ESC was used or moving the cursor up/down.
7046 * Check for the old position still being valid, just in case the text
7047 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007048 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007049 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7050 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007052 pos_T tpos = curwin->w_cursor;
7053
7054 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007055 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007056 for (;;)
7057 {
7058 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7059 --curwin->w_cursor.col;
7060 cc = gchar_cursor();
7061 if (!vim_iswhite(cc))
7062 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007063 if (del_char(TRUE) == FAIL)
7064 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007065 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007066 if (curwin->w_cursor.lnum != tpos.lnum)
7067 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007068 else
7069 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007070 /* reset tpos, could have been invalidated in the loop above */
7071 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007072 tpos.col++;
7073 if (cc != NUL && gchar_pos(&tpos) == NUL)
7074 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 /* <C-S-Right> may have started Visual mode, adjust the position for
7078 * deleted characters. */
7079 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7080 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007081 int len = (int)STRLEN(ml_get_curline());
7082
7083 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007085 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007086#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 }
7090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 }
7092 }
7093 did_ai = FALSE;
7094#ifdef FEAT_SMARTINDENT
7095 did_si = FALSE;
7096 can_si = FALSE;
7097 can_si_back = FALSE;
7098#endif
7099
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007100 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7101 * now in a different buffer. */
7102 if (end_insert_pos != NULL)
7103 {
7104 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007105 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007106 curbuf->b_op_end = *end_insert_pos;
7107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108}
7109
7110/*
7111 * Set the last inserted text to a single character.
7112 * Used for the replace command.
7113 */
7114 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007115set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116{
7117 char_u *s;
7118
7119 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 if (last_insert != NULL)
7122 {
7123 s = last_insert;
7124 /* Use the CTRL-V only when entering a special char */
7125 if (c < ' ' || c == DEL)
7126 *s++ = Ctrl_V;
7127 s = add_char2buf(c, s);
7128 *s++ = ESC;
7129 *s++ = NUL;
7130 last_insert_skip = 0;
7131 }
7132}
7133
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007134#if defined(EXITFREE) || defined(PROTO)
7135 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007136free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007137{
7138 vim_free(last_insert);
7139 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007140# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007141 vim_free(compl_orig_text);
7142 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007143# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007144}
7145#endif
7146
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147/*
7148 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7149 * and CSI. Handle multi-byte characters.
7150 * Returns a pointer to after the added bytes.
7151 */
7152 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007153add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154{
7155#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007156 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 int i;
7158 int len;
7159
7160 len = (*mb_char2bytes)(c, temp);
7161 for (i = 0; i < len; ++i)
7162 {
7163 c = temp[i];
7164#endif
7165 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7166 if (c == K_SPECIAL)
7167 {
7168 *s++ = K_SPECIAL;
7169 *s++ = KS_SPECIAL;
7170 *s++ = KE_FILLER;
7171 }
7172#ifdef FEAT_GUI
7173 else if (c == CSI)
7174 {
7175 *s++ = CSI;
7176 *s++ = KS_EXTRA;
7177 *s++ = (int)KE_CSI;
7178 }
7179#endif
7180 else
7181 *s++ = c;
7182#ifdef FEAT_MBYTE
7183 }
7184#endif
7185 return s;
7186}
7187
7188/*
7189 * move cursor to start of line
7190 * if flags & BL_WHITE move to first non-white
7191 * if flags & BL_SOL move to first non-white if startofline is set,
7192 * otherwise keep "curswant" column
7193 * if flags & BL_FIX don't leave the cursor on a NUL.
7194 */
7195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007196beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 if ((flags & BL_SOL) && !p_sol)
7199 coladvance(curwin->w_curswant);
7200 else
7201 {
7202 curwin->w_cursor.col = 0;
7203#ifdef FEAT_VIRTUALEDIT
7204 curwin->w_cursor.coladd = 0;
7205#endif
7206
7207 if (flags & (BL_WHITE | BL_SOL))
7208 {
7209 char_u *ptr;
7210
7211 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7212 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7213 ++curwin->w_cursor.col;
7214 }
7215 curwin->w_set_curswant = TRUE;
7216 }
7217}
7218
7219/*
7220 * oneright oneleft cursor_down cursor_up
7221 *
7222 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007223 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 * Return OK when successful, FAIL when we hit a line of file boundary.
7225 */
7226
7227 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007228oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
7233#ifdef FEAT_VIRTUALEDIT
7234 if (virtual_active())
7235 {
7236 pos_T prevpos = curwin->w_cursor;
7237
7238 /* Adjust for multi-wide char (excluding TAB) */
7239 ptr = ml_get_cursor();
7240 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007241# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007243# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 ))
7247 ? ptr2cells(ptr) : 1));
7248 curwin->w_set_curswant = TRUE;
7249 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7250 return (prevpos.col != curwin->w_cursor.col
7251 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7252 }
7253#endif
7254
7255 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007256 if (*ptr == NUL)
7257 return FAIL; /* already at the very end */
7258
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007260 if (has_mbyte)
7261 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 else
7263#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007264 l = 1;
7265
7266 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7267 * contains "onemore". */
7268 if (ptr[l] == NUL
7269#ifdef FEAT_VIRTUALEDIT
7270 && (ve_flags & VE_ONEMORE) == 0
7271#endif
7272 )
7273 return FAIL;
7274 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275
7276 curwin->w_set_curswant = TRUE;
7277 return OK;
7278}
7279
7280 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007281oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282{
7283#ifdef FEAT_VIRTUALEDIT
7284 if (virtual_active())
7285 {
7286 int width;
7287 int v = getviscol();
7288
7289 if (v == 0)
7290 return FAIL;
7291
7292# ifdef FEAT_LINEBREAK
7293 /* We might get stuck on 'showbreak', skip over it. */
7294 width = 1;
7295 for (;;)
7296 {
7297 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007298 /* getviscol() is slow, skip it when 'showbreak' is empty,
7299 * 'breakindent' is not set and there are no multi-byte
7300 * characters */
7301 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302# ifdef FEAT_MBYTE
7303 && !has_mbyte
7304# endif
7305 ) || getviscol() < v)
7306 break;
7307 ++width;
7308 }
7309# else
7310 coladvance(v - 1);
7311# endif
7312
7313 if (curwin->w_cursor.coladd == 1)
7314 {
7315 char_u *ptr;
7316
7317 /* Adjust for multi-wide char (not a TAB) */
7318 ptr = ml_get_cursor();
7319 if (*ptr != TAB && vim_isprintc(
7320# ifdef FEAT_MBYTE
7321 (*mb_ptr2char)(ptr)
7322# else
7323 *ptr
7324# endif
7325 ) && ptr2cells(ptr) > 1)
7326 curwin->w_cursor.coladd = 0;
7327 }
7328
7329 curwin->w_set_curswant = TRUE;
7330 return OK;
7331 }
7332#endif
7333
7334 if (curwin->w_cursor.col == 0)
7335 return FAIL;
7336
7337 curwin->w_set_curswant = TRUE;
7338 --curwin->w_cursor.col;
7339
7340#ifdef FEAT_MBYTE
7341 /* if the character on the left of the current cursor is a multi-byte
7342 * character, move to its first byte */
7343 if (has_mbyte)
7344 mb_adjust_cursor();
7345#endif
7346 return OK;
7347}
7348
7349 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007350cursor_up(
7351 long n,
7352 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353{
7354 linenr_T lnum;
7355
7356 if (n > 0)
7357 {
7358 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007359 /* This fails if the cursor is already in the first line or the count
7360 * is larger than the line number and '-' is in 'cpoptions' */
7361 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 return FAIL;
7363 if (n >= lnum)
7364 lnum = 1;
7365 else
7366#ifdef FEAT_FOLDING
7367 if (hasAnyFolding(curwin))
7368 {
7369 /*
7370 * Count each sequence of folded lines as one logical line.
7371 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007372 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 (void)hasFolding(lnum, &lnum, NULL);
7374
7375 while (n--)
7376 {
7377 /* move up one line */
7378 --lnum;
7379 if (lnum <= 1)
7380 break;
7381 /* If we entered a fold, move to the beginning, unless in
7382 * Insert mode or when 'foldopen' contains "all": it will open
7383 * in a moment. */
7384 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7385 (void)hasFolding(lnum, &lnum, NULL);
7386 }
7387 if (lnum < 1)
7388 lnum = 1;
7389 }
7390 else
7391#endif
7392 lnum -= n;
7393 curwin->w_cursor.lnum = lnum;
7394 }
7395
7396 /* try to advance to the column we want to be at */
7397 coladvance(curwin->w_curswant);
7398
7399 if (upd_topline)
7400 update_topline(); /* make sure curwin->w_topline is valid */
7401
7402 return OK;
7403}
7404
7405/*
7406 * Cursor down a number of logical lines.
7407 */
7408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007409cursor_down(
7410 long n,
7411 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
7413 linenr_T lnum;
7414
7415 if (n > 0)
7416 {
7417 lnum = curwin->w_cursor.lnum;
7418#ifdef FEAT_FOLDING
7419 /* Move to last line of fold, will fail if it's the end-of-file. */
7420 (void)hasFolding(lnum, NULL, &lnum);
7421#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007422 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007423 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007424 if (lnum >= curbuf->b_ml.ml_line_count
7425 || (lnum + n > curbuf->b_ml.ml_line_count
7426 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 return FAIL;
7428 if (lnum + n >= curbuf->b_ml.ml_line_count)
7429 lnum = curbuf->b_ml.ml_line_count;
7430 else
7431#ifdef FEAT_FOLDING
7432 if (hasAnyFolding(curwin))
7433 {
7434 linenr_T last;
7435
7436 /* count each sequence of folded lines as one logical line */
7437 while (n--)
7438 {
7439 if (hasFolding(lnum, NULL, &last))
7440 lnum = last + 1;
7441 else
7442 ++lnum;
7443 if (lnum >= curbuf->b_ml.ml_line_count)
7444 break;
7445 }
7446 if (lnum > curbuf->b_ml.ml_line_count)
7447 lnum = curbuf->b_ml.ml_line_count;
7448 }
7449 else
7450#endif
7451 lnum += n;
7452 curwin->w_cursor.lnum = lnum;
7453 }
7454
7455 /* try to advance to the column we want to be at */
7456 coladvance(curwin->w_curswant);
7457
7458 if (upd_topline)
7459 update_topline(); /* make sure curwin->w_topline is valid */
7460
7461 return OK;
7462}
7463
7464/*
7465 * Stuff the last inserted text in the read buffer.
7466 * Last_insert actually is a copy of the redo buffer, so we
7467 * first have to remove the command.
7468 */
7469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007470stuff_inserted(
7471 int c, /* Command character to be inserted */
7472 long count, /* Repeat this many times */
7473 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474{
7475 char_u *esc_ptr;
7476 char_u *ptr;
7477 char_u *last_ptr;
7478 char_u last = NUL;
7479
7480 ptr = get_last_insert();
7481 if (ptr == NULL)
7482 {
7483 EMSG(_(e_noinstext));
7484 return FAIL;
7485 }
7486
7487 /* may want to stuff the command character, to start Insert mode */
7488 if (c != NUL)
7489 stuffcharReadbuff(c);
7490 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7491 *esc_ptr = NUL; /* remove the ESC */
7492
7493 /* when the last char is either "0" or "^" it will be quoted if no ESC
7494 * comes after it OR if it will inserted more than once and "ptr"
7495 * starts with ^D. -- Acevedo
7496 */
7497 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7498 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7499 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7500 {
7501 last = *last_ptr;
7502 *last_ptr = NUL;
7503 }
7504
7505 do
7506 {
7507 stuffReadbuff(ptr);
7508 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7509 if (last)
7510 stuffReadbuff((char_u *)(last == '0'
7511 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7512 : IF_EB("\026^", CTRL_V_STR "^")));
7513 }
7514 while (--count > 0);
7515
7516 if (last)
7517 *last_ptr = last;
7518
7519 if (esc_ptr != NULL)
7520 *esc_ptr = ESC; /* put the ESC back */
7521
7522 /* may want to stuff a trailing ESC, to get out of Insert mode */
7523 if (!no_esc)
7524 stuffcharReadbuff(ESC);
7525
7526 return OK;
7527}
7528
7529 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007530get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531{
7532 if (last_insert == NULL)
7533 return NULL;
7534 return last_insert + last_insert_skip;
7535}
7536
7537/*
7538 * Get last inserted string, and remove trailing <Esc>.
7539 * Returns pointer to allocated memory (must be freed) or NULL.
7540 */
7541 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007542get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543{
7544 char_u *s;
7545 int len;
7546
7547 if (last_insert == NULL)
7548 return NULL;
7549 s = vim_strsave(last_insert + last_insert_skip);
7550 if (s != NULL)
7551 {
7552 len = (int)STRLEN(s);
7553 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7554 s[len - 1] = NUL;
7555 }
7556 return s;
7557}
7558
7559/*
7560 * Check the word in front of the cursor for an abbreviation.
7561 * Called when the non-id character "c" has been entered.
7562 * When an abbreviation is recognized it is removed from the text and
7563 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7564 */
7565 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007566echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568 /* Don't check for abbreviation in paste mode, when disabled and just
7569 * after moving around with cursor keys. */
7570 if (p_paste || no_abbr || arrow_used)
7571 return FALSE;
7572
7573 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7574 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7575}
7576
7577/*
7578 * replace-stack functions
7579 *
7580 * When replacing characters, the replaced characters are remembered for each
7581 * new character. This is used to re-insert the old text when backspacing.
7582 *
7583 * There is a NUL headed list of characters for each character that is
7584 * currently in the file after the insertion point. When BS is used, one NUL
7585 * headed list is put back for the deleted character.
7586 *
7587 * For a newline, there are two NUL headed lists. One contains the characters
7588 * that the NL replaced. The extra one stores the characters after the cursor
7589 * that were deleted (always white space).
7590 *
7591 * Replace_offset is normally 0, in which case replace_push will add a new
7592 * character at the end of the stack. If replace_offset is not 0, that many
7593 * characters will be left on the stack above the newly inserted character.
7594 */
7595
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007596static char_u *replace_stack = NULL;
7597static long replace_stack_nr = 0; /* next entry in replace stack */
7598static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599
7600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007601replace_push(
7602 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603{
7604 char_u *p;
7605
7606 if (replace_stack_nr < replace_offset) /* nothing to do */
7607 return;
7608 if (replace_stack_len <= replace_stack_nr)
7609 {
7610 replace_stack_len += 50;
7611 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7612 if (p == NULL) /* out of memory */
7613 {
7614 replace_stack_len -= 50;
7615 return;
7616 }
7617 if (replace_stack != NULL)
7618 {
7619 mch_memmove(p, replace_stack,
7620 (size_t)(replace_stack_nr * sizeof(char_u)));
7621 vim_free(replace_stack);
7622 }
7623 replace_stack = p;
7624 }
7625 p = replace_stack + replace_stack_nr - replace_offset;
7626 if (replace_offset)
7627 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7628 *p = c;
7629 ++replace_stack_nr;
7630}
7631
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007632#if defined(FEAT_MBYTE) || defined(PROTO)
7633/*
7634 * Push a character onto the replace stack. Handles a multi-byte character in
7635 * reverse byte order, so that the first byte is popped off first.
7636 * Return the number of bytes done (includes composing characters).
7637 */
7638 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007639replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007640{
7641 int l = (*mb_ptr2len)(p);
7642 int j;
7643
7644 for (j = l - 1; j >= 0; --j)
7645 replace_push(p[j]);
7646 return l;
7647}
7648#endif
7649
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650/*
7651 * Pop one item from the replace stack.
7652 * return -1 if stack empty
7653 * return replaced character or NUL otherwise
7654 */
7655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007656replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657{
7658 if (replace_stack_nr == 0)
7659 return -1;
7660 return (int)replace_stack[--replace_stack_nr];
7661}
7662
7663/*
7664 * Join the top two items on the replace stack. This removes to "off"'th NUL
7665 * encountered.
7666 */
7667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007668replace_join(
7669 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
7671 int i;
7672
7673 for (i = replace_stack_nr; --i >= 0; )
7674 if (replace_stack[i] == NUL && off-- <= 0)
7675 {
7676 --replace_stack_nr;
7677 mch_memmove(replace_stack + i, replace_stack + i + 1,
7678 (size_t)(replace_stack_nr - i));
7679 return;
7680 }
7681}
7682
7683/*
7684 * Pop bytes from the replace stack until a NUL is found, and insert them
7685 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7686 */
7687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007688replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689{
7690 int cc;
7691 int oldState = State;
7692
7693 State = NORMAL; /* don't want REPLACE here */
7694 while ((cc = replace_pop()) > 0)
7695 {
7696#ifdef FEAT_MBYTE
7697 mb_replace_pop_ins(cc);
7698#else
7699 ins_char(cc);
7700#endif
7701 dec_cursor();
7702 }
7703 State = oldState;
7704}
7705
7706#ifdef FEAT_MBYTE
7707/*
7708 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7709 * indicates a multi-byte char, pop the other bytes too.
7710 */
7711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007712mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
7714 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007715 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 int i;
7717 int c;
7718
7719 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7720 {
7721 buf[0] = cc;
7722 for (i = 1; i < n; ++i)
7723 buf[i] = replace_pop();
7724 ins_bytes_len(buf, n);
7725 }
7726 else
7727 ins_char(cc);
7728
7729 if (enc_utf8)
7730 /* Handle composing chars. */
7731 for (;;)
7732 {
7733 c = replace_pop();
7734 if (c == -1) /* stack empty */
7735 break;
7736 if ((n = MB_BYTE2LEN(c)) == 1)
7737 {
7738 /* Not a multi-byte char, put it back. */
7739 replace_push(c);
7740 break;
7741 }
7742 else
7743 {
7744 buf[0] = c;
7745 for (i = 1; i < n; ++i)
7746 buf[i] = replace_pop();
7747 if (utf_iscomposing(utf_ptr2char(buf)))
7748 ins_bytes_len(buf, n);
7749 else
7750 {
7751 /* Not a composing char, put it back. */
7752 for (i = n - 1; i >= 0; --i)
7753 replace_push(buf[i]);
7754 break;
7755 }
7756 }
7757 }
7758}
7759#endif
7760
7761/*
7762 * make the replace stack empty
7763 * (called when exiting replace mode)
7764 */
7765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007766replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
7768 vim_free(replace_stack);
7769 replace_stack = NULL;
7770 replace_stack_len = 0;
7771 replace_stack_nr = 0;
7772}
7773
7774/*
7775 * Handle doing a BS for one character.
7776 * cc < 0: replace stack empty, just move cursor
7777 * cc == 0: character was inserted, delete it
7778 * cc > 0: character was replaced, put cc (first byte of original char) back
7779 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007780 * When "limit_col" is >= 0, don't delete before this column. Matters when
7781 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 */
7783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007784replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786 int cc;
7787#ifdef FEAT_VREPLACE
7788 int orig_len = 0;
7789 int ins_len;
7790 int orig_vcols = 0;
7791 colnr_T start_vcol;
7792 char_u *p;
7793 int i;
7794 int vcol;
7795#endif
7796
7797 cc = replace_pop();
7798 if (cc > 0)
7799 {
7800#ifdef FEAT_VREPLACE
7801 if (State & VREPLACE_FLAG)
7802 {
7803 /* Get the number of screen cells used by the character we are
7804 * going to delete. */
7805 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7806 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7807 }
7808#endif
7809#ifdef FEAT_MBYTE
7810 if (has_mbyte)
7811 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007812 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813# ifdef FEAT_VREPLACE
7814 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007815 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816# endif
7817 replace_push(cc);
7818 }
7819 else
7820#endif
7821 {
7822 pchar_cursor(cc);
7823#ifdef FEAT_VREPLACE
7824 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007825 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826#endif
7827 }
7828 replace_pop_ins();
7829
7830#ifdef FEAT_VREPLACE
7831 if (State & VREPLACE_FLAG)
7832 {
7833 /* Get the number of screen cells used by the inserted characters */
7834 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007835 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 vcol = start_vcol;
7837 for (i = 0; i < ins_len; ++i)
7838 {
7839 vcol += chartabsize(p + i, vcol);
7840#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007841 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842#endif
7843 }
7844 vcol -= start_vcol;
7845
7846 /* Delete spaces that were inserted after the cursor to keep the
7847 * text aligned. */
7848 curwin->w_cursor.col += ins_len;
7849 while (vcol > orig_vcols && gchar_cursor() == ' ')
7850 {
7851 del_char(FALSE);
7852 ++orig_vcols;
7853 }
7854 curwin->w_cursor.col -= ins_len;
7855 }
7856#endif
7857
7858 /* mark the buffer as changed and prepare for displaying */
7859 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7860 }
7861 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007862 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863}
7864
7865#ifdef FEAT_CINDENT
7866/*
7867 * Return TRUE if C-indenting is on.
7868 */
7869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007870cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871{
7872 return (!p_paste && (curbuf->b_p_cin
7873# ifdef FEAT_EVAL
7874 || *curbuf->b_p_inde != NUL
7875# endif
7876 ));
7877}
7878#endif
7879
7880#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7881/*
7882 * Re-indent the current line, based on the current contents of it and the
7883 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7884 * confused what all the part that handles Control-T is doing that I'm not.
7885 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7886 */
7887
7888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007889fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007891 int amount = get_the_indent();
7892
7893 if (amount >= 0)
7894 {
7895 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7896 if (linewhite(curwin->w_cursor.lnum))
7897 did_ai = TRUE; /* delete the indent if the line stays empty */
7898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899}
7900
7901 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007902fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903{
7904 if (p_paste)
7905 return;
7906# ifdef FEAT_LISP
7907 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7908 fixthisline(get_lisp_indent);
7909# endif
7910# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7911 else
7912# endif
7913# ifdef FEAT_CINDENT
7914 if (cindent_on())
7915 do_c_expr_indent();
7916# endif
7917}
7918
7919#endif
7920
7921#ifdef FEAT_CINDENT
7922/*
7923 * return TRUE if 'cinkeys' contains the key "keytyped",
7924 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007925 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7927 *
7928 * "keytyped" can have a few special values:
7929 * KEY_OPEN_FORW
7930 * KEY_OPEN_BACK
7931 * KEY_COMPLETE just finished completion.
7932 *
7933 * If line_is_empty is TRUE accept keys with '0' before them.
7934 */
7935 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007936in_cinkeys(
7937 int keytyped,
7938 int when,
7939 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
7941 char_u *look;
7942 int try_match;
7943 int try_match_word;
7944 char_u *p;
7945 char_u *line;
7946 int icase;
7947 int i;
7948
Bram Moolenaar30848942009-12-24 14:46:12 +00007949 if (keytyped == NUL)
7950 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7951 return FALSE;
7952
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953#ifdef FEAT_EVAL
7954 if (*curbuf->b_p_inde != NUL)
7955 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7956 else
7957#endif
7958 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7959 while (*look)
7960 {
7961 /*
7962 * Find out if we want to try a match with this key, depending on
7963 * 'when' and a '*' or '!' before the key.
7964 */
7965 switch (when)
7966 {
7967 case '*': try_match = (*look == '*'); break;
7968 case '!': try_match = (*look == '!'); break;
7969 default: try_match = (*look != '*'); break;
7970 }
7971 if (*look == '*' || *look == '!')
7972 ++look;
7973
7974 /*
7975 * If there is a '0', only accept a match if the line is empty.
7976 * But may still match when typing last char of a word.
7977 */
7978 if (*look == '0')
7979 {
7980 try_match_word = try_match;
7981 if (!line_is_empty)
7982 try_match = FALSE;
7983 ++look;
7984 }
7985 else
7986 try_match_word = FALSE;
7987
7988 /*
7989 * does it look like a control character?
7990 */
7991 if (*look == '^'
7992#ifdef EBCDIC
7993 && (Ctrl_chr(look[1]) != 0)
7994#else
7995 && look[1] >= '?' && look[1] <= '_'
7996#endif
7997 )
7998 {
7999 if (try_match && keytyped == Ctrl_chr(look[1]))
8000 return TRUE;
8001 look += 2;
8002 }
8003 /*
8004 * 'o' means "o" command, open forward.
8005 * 'O' means "O" command, open backward.
8006 */
8007 else if (*look == 'o')
8008 {
8009 if (try_match && keytyped == KEY_OPEN_FORW)
8010 return TRUE;
8011 ++look;
8012 }
8013 else if (*look == 'O')
8014 {
8015 if (try_match && keytyped == KEY_OPEN_BACK)
8016 return TRUE;
8017 ++look;
8018 }
8019
8020 /*
8021 * 'e' means to check for "else" at start of line and just before the
8022 * cursor.
8023 */
8024 else if (*look == 'e')
8025 {
8026 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8027 {
8028 p = ml_get_curline();
8029 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8030 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8031 return TRUE;
8032 }
8033 ++look;
8034 }
8035
8036 /*
8037 * ':' only causes an indent if it is at the end of a label or case
8038 * statement, or when it was before typing the ':' (to fix
8039 * class::method for C++).
8040 */
8041 else if (*look == ':')
8042 {
8043 if (try_match && keytyped == ':')
8044 {
8045 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008046 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008048 /* Need to get the line again after cin_islabel(). */
8049 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 if (curwin->w_cursor.col > 2
8051 && p[curwin->w_cursor.col - 1] == ':'
8052 && p[curwin->w_cursor.col - 2] == ':')
8053 {
8054 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008055 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008056 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 p = ml_get_curline();
8058 p[curwin->w_cursor.col - 1] = ':';
8059 if (i)
8060 return TRUE;
8061 }
8062 }
8063 ++look;
8064 }
8065
8066
8067 /*
8068 * Is it a key in <>, maybe?
8069 */
8070 else if (*look == '<')
8071 {
8072 if (try_match)
8073 {
8074 /*
8075 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8076 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8077 * >, *, : and ! keys if they really really want to.
8078 */
8079 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8080 && keytyped == look[1])
8081 return TRUE;
8082
8083 if (keytyped == get_special_key_code(look + 1))
8084 return TRUE;
8085 }
8086 while (*look && *look != '>')
8087 look++;
8088 while (*look == '>')
8089 look++;
8090 }
8091
8092 /*
8093 * Is it a word: "=word"?
8094 */
8095 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8096 {
8097 ++look;
8098 if (*look == '~')
8099 {
8100 icase = TRUE;
8101 ++look;
8102 }
8103 else
8104 icase = FALSE;
8105 p = vim_strchr(look, ',');
8106 if (p == NULL)
8107 p = look + STRLEN(look);
8108 if ((try_match || try_match_word)
8109 && curwin->w_cursor.col >= (colnr_T)(p - look))
8110 {
8111 int match = FALSE;
8112
8113#ifdef FEAT_INS_EXPAND
8114 if (keytyped == KEY_COMPLETE)
8115 {
8116 char_u *s;
8117
8118 /* Just completed a word, check if it starts with "look".
8119 * search back for the start of a word. */
8120 line = ml_get_curline();
8121# ifdef FEAT_MBYTE
8122 if (has_mbyte)
8123 {
8124 char_u *n;
8125
8126 for (s = line + curwin->w_cursor.col; s > line; s = n)
8127 {
8128 n = mb_prevptr(line, s);
8129 if (!vim_iswordp(n))
8130 break;
8131 }
8132 }
8133 else
8134# endif
8135 for (s = line + curwin->w_cursor.col; s > line; --s)
8136 if (!vim_iswordc(s[-1]))
8137 break;
8138 if (s + (p - look) <= line + curwin->w_cursor.col
8139 && (icase
8140 ? MB_STRNICMP(s, look, p - look)
8141 : STRNCMP(s, look, p - look)) == 0)
8142 match = TRUE;
8143 }
8144 else
8145#endif
8146 /* TODO: multi-byte */
8147 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8148 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8149 {
8150 line = ml_get_cursor();
8151 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8152 || !vim_iswordc(line[-(p - look) - 1]))
8153 && (icase
8154 ? MB_STRNICMP(line - (p - look), look, p - look)
8155 : STRNCMP(line - (p - look), look, p - look))
8156 == 0)
8157 match = TRUE;
8158 }
8159 if (match && try_match_word && !try_match)
8160 {
8161 /* "0=word": Check if there are only blanks before the
8162 * word. */
8163 line = ml_get_curline();
8164 if ((int)(skipwhite(line) - line) !=
8165 (int)(curwin->w_cursor.col - (p - look)))
8166 match = FALSE;
8167 }
8168 if (match)
8169 return TRUE;
8170 }
8171 look = p;
8172 }
8173
8174 /*
8175 * ok, it's a boring generic character.
8176 */
8177 else
8178 {
8179 if (try_match && *look == keytyped)
8180 return TRUE;
8181 ++look;
8182 }
8183
8184 /*
8185 * Skip over ", ".
8186 */
8187 look = skip_to_option_part(look);
8188 }
8189 return FALSE;
8190}
8191#endif /* FEAT_CINDENT */
8192
8193#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8194/*
8195 * Map Hebrew keyboard when in hkmap mode.
8196 */
8197 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008198hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
8200 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8201 {
8202 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8203 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8204 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8205 static char_u map[26] =
8206 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8207 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8208 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8209 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8210 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8211 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8212 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8213 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8214 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8215
8216 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8217 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8218 /* '-1'='sofit' */
8219 else if (c == 'x')
8220 return 'X';
8221 else if (c == 'q')
8222 return '\''; /* {geresh}={'} */
8223 else if (c == 246)
8224 return ' '; /* \"o --> ' ' for a german keyboard */
8225 else if (c == 228)
8226 return ' '; /* \"a --> ' ' -- / -- */
8227 else if (c == 252)
8228 return ' '; /* \"u --> ' ' -- / -- */
8229#ifdef EBCDIC
8230 else if (islower(c))
8231#else
8232 /* NOTE: islower() does not do the right thing for us on Linux so we
8233 * do this the same was as 5.7 and previous, so it works correctly on
8234 * all systems. Specifically, the e.g. Delete and Arrow keys are
8235 * munged and won't work if e.g. searching for Hebrew text.
8236 */
8237 else if (c >= 'a' && c <= 'z')
8238#endif
8239 return (int)(map[CharOrdLow(c)] + p_aleph);
8240 else
8241 return c;
8242 }
8243 else
8244 {
8245 switch (c)
8246 {
8247 case '`': return ';';
8248 case '/': return '.';
8249 case '\'': return ',';
8250 case 'q': return '/';
8251 case 'w': return '\'';
8252
8253 /* Hebrew letters - set offset from 'a' */
8254 case ',': c = '{'; break;
8255 case '.': c = 'v'; break;
8256 case ';': c = 't'; break;
8257 default: {
8258 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8259
8260#ifdef EBCDIC
8261 /* see note about islower() above */
8262 if (!islower(c))
8263#else
8264 if (c < 'a' || c > 'z')
8265#endif
8266 return c;
8267 c = str[CharOrdLow(c)];
8268 break;
8269 }
8270 }
8271
8272 return (int)(CharOrdLow(c) + p_aleph);
8273 }
8274}
8275#endif
8276
8277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008278ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279{
8280 int need_redraw = FALSE;
8281 int regname;
8282 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008283 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284
8285 /*
8286 * If we are going to wait for a character, show a '"'.
8287 */
8288 pc_status = PC_STATUS_UNSET;
8289 if (redrawing() && !char_avail())
8290 {
8291 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008292 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293
8294 edit_putchar('"', TRUE);
8295#ifdef FEAT_CMDL_INFO
8296 add_to_showcmd_c(Ctrl_R);
8297#endif
8298 }
8299
8300#ifdef USE_ON_FLY_SCROLL
8301 dont_scroll = TRUE; /* disallow scrolling here */
8302#endif
8303
8304 /*
8305 * Don't map the register name. This also prevents the mode message to be
8306 * deleted when ESC is hit.
8307 */
8308 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008309 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8312 {
8313 /* Get a third key for literal register insertion */
8314 literally = regname;
8315#ifdef FEAT_CMDL_INFO
8316 add_to_showcmd_c(literally);
8317#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008318 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 }
8321 --no_mapping;
8322
8323#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008324 /* Don't call u_sync() while typing the expression or giving an error
8325 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 ++no_u_sync;
8327 if (regname == '=')
8328 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008329# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008331# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008332 /* Sync undo when evaluating the expression calls setline() or
8333 * append(), so that it can be undone separately. */
8334 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008335
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008337# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 /* Restore the Input Method. */
8339 if (im_on)
8340 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008343 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8344 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008345 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 else
8349 {
8350#endif
8351 if (literally == Ctrl_O || literally == Ctrl_P)
8352 {
8353 /* Append the command to the redo buffer. */
8354 AppendCharToRedobuff(Ctrl_R);
8355 AppendCharToRedobuff(literally);
8356 AppendCharToRedobuff(regname);
8357
8358 do_put(regname, BACKWARD, 1L,
8359 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8360 }
8361 else if (insert_reg(regname, literally) == FAIL)
8362 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008363 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 need_redraw = TRUE; /* remove the '"' */
8365 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008366 else if (stop_insert_mode)
8367 /* When the '=' register was used and a function was invoked that
8368 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8369 * insert anything, need to remove the '"' */
8370 need_redraw = TRUE;
8371
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372#ifdef FEAT_EVAL
8373 }
8374 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008375 if (u_sync_once == 1)
8376 ins_need_undo = TRUE;
8377 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378#endif
8379#ifdef FEAT_CMDL_INFO
8380 clear_showcmd();
8381#endif
8382
8383 /* If the inserted register is empty, we need to remove the '"' */
8384 if (need_redraw || stuff_empty())
8385 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008386
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008387 /* Disallow starting Visual mode here, would get a weird mode. */
8388 if (!vis_active && VIsual_active)
8389 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390}
8391
8392/*
8393 * CTRL-G commands in Insert mode.
8394 */
8395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008396ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397{
8398 int c;
8399
8400#ifdef FEAT_INS_EXPAND
8401 /* Right after CTRL-X the cursor will be after the ruler. */
8402 setcursor();
8403#endif
8404
8405 /*
8406 * Don't map the second key. This also prevents the mode message to be
8407 * deleted when ESC is hit.
8408 */
8409 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008410 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 --no_mapping;
8412 switch (c)
8413 {
8414 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8415 case K_UP:
8416 case Ctrl_K:
8417 case 'k': ins_up(TRUE);
8418 break;
8419
8420 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8421 case K_DOWN:
8422 case Ctrl_J:
8423 case 'j': ins_down(TRUE);
8424 break;
8425
8426 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008427 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008429
8430 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008431 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008432 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008433 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 break;
8435
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008436 /* CTRL-G U: do not break undo with the next char */
8437 case 'U':
8438 /* Allow one left/right cursor movement with the next char,
8439 * without breaking undo. */
8440 dont_sync_undo = MAYBE;
8441 break;
8442
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008444 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 }
8446}
8447
8448/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008449 * CTRL-^ in Insert mode.
8450 */
8451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008452ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008453{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008454 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008455 {
8456 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8457 if (State & LANGMAP)
8458 {
8459 curbuf->b_p_iminsert = B_IMODE_NONE;
8460 State &= ~LANGMAP;
8461 }
8462 else
8463 {
8464 curbuf->b_p_iminsert = B_IMODE_LMAP;
8465 State |= LANGMAP;
8466#ifdef USE_IM_CONTROL
8467 im_set_active(FALSE);
8468#endif
8469 }
8470 }
8471#ifdef USE_IM_CONTROL
8472 else
8473 {
8474 /* There are no ":lmap" mappings, toggle IM */
8475 if (im_get_status())
8476 {
8477 curbuf->b_p_iminsert = B_IMODE_NONE;
8478 im_set_active(FALSE);
8479 }
8480 else
8481 {
8482 curbuf->b_p_iminsert = B_IMODE_IM;
8483 State &= ~LANGMAP;
8484 im_set_active(TRUE);
8485 }
8486 }
8487#endif
8488 set_iminsert_global();
8489 showmode();
8490#ifdef FEAT_GUI
8491 /* may show different cursor shape or color */
8492 if (gui.in_use)
8493 gui_update_cursor(TRUE, FALSE);
8494#endif
8495#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8496 /* Show/unshow value of 'keymap' in status lines. */
8497 status_redraw_curbuf();
8498#endif
8499}
8500
8501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 * Handle ESC in insert mode.
8503 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8504 * insert.
8505 */
8506 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008507ins_esc(
8508 long *count,
8509 int cmdchar,
8510 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511{
8512 int temp;
8513 static int disabled_redraw = FALSE;
8514
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008515#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008516 check_spell_redraw();
8517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518#if defined(FEAT_HANGULIN)
8519# if defined(ESC_CHG_TO_ENG_MODE)
8520 hangul_input_state_set(0);
8521# endif
8522 if (composing_hangul)
8523 {
8524 push_raw_key(composing_hangul_buffer, 2);
8525 composing_hangul = 0;
8526 }
8527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528
8529 temp = curwin->w_cursor.col;
8530 if (disabled_redraw)
8531 {
8532 --RedrawingDisabled;
8533 disabled_redraw = FALSE;
8534 }
8535 if (!arrow_used)
8536 {
8537 /*
8538 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008539 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8540 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 */
8542 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008543 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544
8545 /*
8546 * Repeating insert may take a long time. Check for
8547 * interrupt now and then.
8548 */
8549 if (*count > 0)
8550 {
8551 line_breakcheck();
8552 if (got_int)
8553 *count = 0;
8554 }
8555
8556 if (--*count > 0) /* repeat what was typed */
8557 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008558 /* Vi repeats the insert without replacing characters. */
8559 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8560 State &= ~REPLACE_FLAG;
8561
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 (void)start_redo_ins();
8563 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008564 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 ++RedrawingDisabled;
8566 disabled_redraw = TRUE;
8567 return FALSE; /* repeat the insert */
8568 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008569 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 undisplay_dollar();
8571 }
8572
8573 /* When an autoindent was removed, curswant stays after the
8574 * indent */
8575 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8576 curwin->w_set_curswant = TRUE;
8577
8578 /* Remember the last Insert position in the '^ mark. */
8579 if (!cmdmod.keepjumps)
8580 curbuf->b_last_insert = curwin->w_cursor;
8581
8582 /*
8583 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008584 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008586 if (!nomove
8587 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588#ifdef FEAT_VIRTUALEDIT
8589 || curwin->w_cursor.coladd > 0
8590#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008591 )
8592 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008593 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594#ifdef FEAT_RIGHTLEFT
8595 && !revins_on
8596#endif
8597 )
8598 {
8599#ifdef FEAT_VIRTUALEDIT
8600 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8601 {
8602 oneleft();
8603 if (restart_edit != NUL)
8604 ++curwin->w_cursor.coladd;
8605 }
8606 else
8607#endif
8608 {
8609 --curwin->w_cursor.col;
8610#ifdef FEAT_MBYTE
8611 /* Correct cursor for multi-byte character. */
8612 if (has_mbyte)
8613 mb_adjust_cursor();
8614#endif
8615 }
8616 }
8617
8618#ifdef USE_IM_CONTROL
8619 /* Disable IM to allow typing English directly for Normal mode commands.
8620 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8621 * well). */
8622 if (!(State & LANGMAP))
8623 im_save_status(&curbuf->b_p_iminsert);
8624 im_set_active(FALSE);
8625#endif
8626
8627 State = NORMAL;
8628 /* need to position cursor again (e.g. when on a TAB ) */
8629 changed_cline_bef_curs();
8630
8631#ifdef FEAT_MOUSE
8632 setmouse();
8633#endif
8634#ifdef CURSOR_SHAPE
8635 ui_cursor_shape(); /* may show different cursor shape */
8636#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008637 if (!p_ek)
8638 /* Re-enable bracketed paste mode. */
8639 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640
8641 /*
8642 * When recording or for CTRL-O, need to display the new mode.
8643 * Otherwise remove the mode message.
8644 */
8645 if (Recording || restart_edit != NUL)
8646 showmode();
8647 else if (p_smd)
8648 MSG("");
8649
8650 return TRUE; /* exit Insert mode */
8651}
8652
8653#ifdef FEAT_RIGHTLEFT
8654/*
8655 * Toggle language: hkmap and revins_on.
8656 * Move to end of reverse inserted text.
8657 */
8658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008659ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
8661 if (revins_on && revins_chars && revins_scol >= 0)
8662 {
8663 while (gchar_cursor() != NUL && revins_chars--)
8664 ++curwin->w_cursor.col;
8665 }
8666 p_ri = !p_ri;
8667 revins_on = (State == INSERT && p_ri);
8668 if (revins_on)
8669 {
8670 revins_scol = curwin->w_cursor.col;
8671 revins_legal++;
8672 revins_chars = 0;
8673 undisplay_dollar();
8674 }
8675 else
8676 revins_scol = -1;
8677#ifdef FEAT_FKMAP
8678 if (p_altkeymap)
8679 {
8680 /*
8681 * to be consistent also for redo command, using '.'
8682 * set arrow_used to true and stop it - causing to redo
8683 * characters entered in one mode (normal/reverse insert).
8684 */
8685 arrow_used = TRUE;
8686 (void)stop_arrow();
8687 p_fkmap = curwin->w_p_rl ^ p_ri;
8688 if (p_fkmap && p_ri)
8689 State = INSERT;
8690 }
8691 else
8692#endif
8693 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8694 showmode();
8695}
8696#endif
8697
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698/*
8699 * If 'keymodel' contains "startsel", may start selection.
8700 * Returns TRUE when a CTRL-O and other keys stuffed.
8701 */
8702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008703ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
8705 if (km_startsel)
8706 switch (c)
8707 {
8708 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 case K_PAGEUP:
8711 case K_KPAGEUP:
8712 case K_PAGEDOWN:
8713 case K_KPAGEDOWN:
8714# ifdef MACOS
8715 case K_LEFT:
8716 case K_RIGHT:
8717 case K_UP:
8718 case K_DOWN:
8719 case K_END:
8720 case K_HOME:
8721# endif
8722 if (!(mod_mask & MOD_MASK_SHIFT))
8723 break;
8724 /* FALLTHROUGH */
8725 case K_S_LEFT:
8726 case K_S_RIGHT:
8727 case K_S_UP:
8728 case K_S_DOWN:
8729 case K_S_END:
8730 case K_S_HOME:
8731 /* Start selection right away, the cursor can move with
8732 * CTRL-O when beyond the end of the line. */
8733 start_selection();
8734
8735 /* Execute the key in (insert) Select mode. */
8736 stuffcharReadbuff(Ctrl_O);
8737 if (mod_mask)
8738 {
8739 char_u buf[4];
8740
8741 buf[0] = K_SPECIAL;
8742 buf[1] = KS_MODIFIER;
8743 buf[2] = mod_mask;
8744 buf[3] = NUL;
8745 stuffReadbuff(buf);
8746 }
8747 stuffcharReadbuff(c);
8748 return TRUE;
8749 }
8750 return FALSE;
8751}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752
8753/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008754 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008755 */
8756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008757ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008758{
8759#ifdef FEAT_FKMAP
8760 if (p_fkmap && p_ri)
8761 {
8762 beep_flush();
8763 EMSG(farsi_text_3); /* encoded in Farsi */
8764 return;
8765 }
8766#endif
8767
8768#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008769# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008770 set_vim_var_string(VV_INSERTMODE,
8771 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008772# ifdef FEAT_VREPLACE
8773 replaceState == VREPLACE ? "v" :
8774# endif
8775 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008776# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008777 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8778#endif
8779 if (State & REPLACE_FLAG)
8780 State = INSERT | (State & LANGMAP);
8781 else
8782 State = replaceState | (State & LANGMAP);
8783 AppendCharToRedobuff(K_INS);
8784 showmode();
8785#ifdef CURSOR_SHAPE
8786 ui_cursor_shape(); /* may show different cursor shape */
8787#endif
8788}
8789
8790/*
8791 * Pressed CTRL-O in Insert mode.
8792 */
8793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008794ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008795{
8796#ifdef FEAT_VREPLACE
8797 if (State & VREPLACE_FLAG)
8798 restart_edit = 'V';
8799 else
8800#endif
8801 if (State & REPLACE_FLAG)
8802 restart_edit = 'R';
8803 else
8804 restart_edit = 'I';
8805#ifdef FEAT_VIRTUALEDIT
8806 if (virtual_active())
8807 ins_at_eol = FALSE; /* cursor always keeps its column */
8808 else
8809#endif
8810 ins_at_eol = (gchar_cursor() == NUL);
8811}
8812
8813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 * If the cursor is on an indent, ^T/^D insert/delete one
8815 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008816 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 * with vi. But vi only supports ^T and ^D after an
8818 * autoindent, we support it everywhere.
8819 */
8820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008821ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822{
8823 if (stop_arrow() == FAIL)
8824 return;
8825 AppendCharToRedobuff(c);
8826
8827 /*
8828 * 0^D and ^^D: remove all indent.
8829 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008830 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8831 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 {
8833 --curwin->w_cursor.col;
8834 (void)del_char(FALSE); /* delete the '^' or '0' */
8835 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8836 if (State & REPLACE_FLAG)
8837 replace_pop_ins();
8838 if (lastc == '^')
8839 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008840 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 }
8842 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008843 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844
8845 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8846 did_ai = FALSE;
8847#ifdef FEAT_SMARTINDENT
8848 did_si = FALSE;
8849 can_si = FALSE;
8850 can_si_back = FALSE;
8851#endif
8852#ifdef FEAT_CINDENT
8853 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8854#endif
8855}
8856
8857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008858ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860 int temp;
8861
8862 if (stop_arrow() == FAIL)
8863 return;
8864 if (gchar_cursor() == NUL) /* delete newline */
8865 {
8866 temp = curwin->w_cursor.col;
8867 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008868 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008869 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 else
8871 curwin->w_cursor.col = temp;
8872 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008873 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8874 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 did_ai = FALSE;
8876#ifdef FEAT_SMARTINDENT
8877 did_si = FALSE;
8878 can_si = FALSE;
8879 can_si_back = FALSE;
8880#endif
8881 AppendCharToRedobuff(K_DEL);
8882}
8883
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008884static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008885
8886/*
8887 * Delete one character for ins_bs().
8888 */
8889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008890ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008891{
8892 dec_cursor();
8893 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8894 if (State & REPLACE_FLAG)
8895 {
8896 /* Don't delete characters before the insert point when in
8897 * Replace mode */
8898 if (curwin->w_cursor.lnum != Insstart.lnum
8899 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008900 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008901 }
8902 else
8903 (void)del_char(FALSE);
8904}
8905
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906/*
8907 * Handle Backspace, delete-word and delete-line in Insert mode.
8908 * Return TRUE when backspace was actually used.
8909 */
8910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008911ins_bs(
8912 int c,
8913 int mode,
8914 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915{
8916 linenr_T lnum;
8917 int cc;
8918 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008919 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 colnr_T mincol;
8921 int did_backspace = FALSE;
8922 int in_indent;
8923 int oldState;
8924#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008925 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926#endif
8927
8928 /*
8929 * can't delete anything in an empty file
8930 * can't backup past first character in buffer
8931 * can't backup past starting point unless 'backspace' > 1
8932 * can backup to a previous line if 'backspace' == 0
8933 */
8934 if ( bufempty()
8935 || (
8936#ifdef FEAT_RIGHTLEFT
8937 !revins_on &&
8938#endif
8939 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8940 || (!can_bs(BS_START)
8941 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008942 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8943 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8945 && curwin->w_cursor.col <= ai_col)
8946 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8947 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008948 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 return FALSE;
8950 }
8951
8952 if (stop_arrow() == FAIL)
8953 return FALSE;
8954 in_indent = inindent(0);
8955#ifdef FEAT_CINDENT
8956 if (in_indent)
8957 can_cindent = FALSE;
8958#endif
8959#ifdef FEAT_COMMENTS
8960 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8961#endif
8962#ifdef FEAT_RIGHTLEFT
8963 if (revins_on) /* put cursor after last inserted char */
8964 inc_cursor();
8965#endif
8966
8967#ifdef FEAT_VIRTUALEDIT
8968 /* Virtualedit:
8969 * BACKSPACE_CHAR eats a virtual space
8970 * BACKSPACE_WORD eats all coladd
8971 * BACKSPACE_LINE eats all coladd and keeps going
8972 */
8973 if (curwin->w_cursor.coladd > 0)
8974 {
8975 if (mode == BACKSPACE_CHAR)
8976 {
8977 --curwin->w_cursor.coladd;
8978 return TRUE;
8979 }
8980 if (mode == BACKSPACE_WORD)
8981 {
8982 curwin->w_cursor.coladd = 0;
8983 return TRUE;
8984 }
8985 curwin->w_cursor.coladd = 0;
8986 }
8987#endif
8988
8989 /*
8990 * delete newline!
8991 */
8992 if (curwin->w_cursor.col == 0)
8993 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008994 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008995 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996#ifdef FEAT_RIGHTLEFT
8997 || revins_on
8998#endif
8999 )
9000 {
9001 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9002 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9003 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009004 --Insstart.lnum;
9005 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 }
9007 /*
9008 * In replace mode:
9009 * cc < 0: NL was inserted, delete it
9010 * cc >= 0: NL was replaced, put original characters back
9011 */
9012 cc = -1;
9013 if (State & REPLACE_FLAG)
9014 cc = replace_pop(); /* returns -1 if NL was inserted */
9015 /*
9016 * In replace mode, in the line we started replacing, we only move the
9017 * cursor.
9018 */
9019 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9020 {
9021 dec_cursor();
9022 }
9023 else
9024 {
9025#ifdef FEAT_VREPLACE
9026 if (!(State & VREPLACE_FLAG)
9027 || curwin->w_cursor.lnum > orig_line_count)
9028#endif
9029 {
9030 temp = gchar_cursor(); /* remember current char */
9031 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009032
9033 /* When "aw" is in 'formatoptions' we must delete the space at
9034 * the end of the line, otherwise the line will be broken
9035 * again when auto-formatting. */
9036 if (has_format_option(FO_AUTO)
9037 && has_format_option(FO_WHITE_PAR))
9038 {
9039 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9040 TRUE);
9041 int len;
9042
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009043 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009044 if (len > 0 && ptr[len - 1] == ' ')
9045 ptr[len - 1] = NUL;
9046 }
9047
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009048 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 if (temp == NUL && gchar_cursor() != NUL)
9050 inc_cursor();
9051 }
9052#ifdef FEAT_VREPLACE
9053 else
9054 dec_cursor();
9055#endif
9056
9057 /*
9058 * In REPLACE mode we have to put back the text that was replaced
9059 * by the NL. On the replace stack is first a NUL-terminated
9060 * sequence of characters that were deleted and then the
9061 * characters that NL replaced.
9062 */
9063 if (State & REPLACE_FLAG)
9064 {
9065 /*
9066 * Do the next ins_char() in NORMAL state, to
9067 * prevent ins_char() from replacing characters and
9068 * avoiding showmatch().
9069 */
9070 oldState = State;
9071 State = NORMAL;
9072 /*
9073 * restore characters (blanks) deleted after cursor
9074 */
9075 while (cc > 0)
9076 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009077 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078#ifdef FEAT_MBYTE
9079 mb_replace_pop_ins(cc);
9080#else
9081 ins_char(cc);
9082#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009083 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 cc = replace_pop();
9085 }
9086 /* restore the characters that NL replaced */
9087 replace_pop_ins();
9088 State = oldState;
9089 }
9090 }
9091 did_ai = FALSE;
9092 }
9093 else
9094 {
9095 /*
9096 * Delete character(s) before the cursor.
9097 */
9098#ifdef FEAT_RIGHTLEFT
9099 if (revins_on) /* put cursor on last inserted char */
9100 dec_cursor();
9101#endif
9102 mincol = 0;
9103 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009104 if (mode == BACKSPACE_LINE
9105 && (curbuf->b_p_ai
9106#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009107 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009108#endif
9109 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110#ifdef FEAT_RIGHTLEFT
9111 && !revins_on
9112#endif
9113 )
9114 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009115 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009117 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009119 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 }
9121
9122 /*
9123 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9124 */
9125 if ( mode == BACKSPACE_CHAR
9126 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009127 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009128 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 && (*(ml_get_cursor() - 1) == TAB
9130 || (*(ml_get_cursor() - 1) == ' '
9131 && (!*inserted_space_p
9132 || arrow_used))))))
9133 {
9134 int ts;
9135 colnr_T vcol;
9136 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009137 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138
9139 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009140 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009141 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009143 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 /* Compute the virtual column where we want to be. Since
9145 * 'showbreak' may get in the way, need to get the last column of
9146 * the previous character. */
9147 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009148 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 dec_cursor();
9150 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9151 inc_cursor();
9152 want_vcol = (want_vcol / ts) * ts;
9153
9154 /* delete characters until we are at or before want_vcol */
9155 while (vcol > want_vcol
9156 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009157 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158
9159 /* insert extra spaces until we are at want_vcol */
9160 while (vcol < want_vcol)
9161 {
9162 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009163 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9164 && curwin->w_cursor.col < Insstart_orig.col)
9165 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166
9167#ifdef FEAT_VREPLACE
9168 if (State & VREPLACE_FLAG)
9169 ins_char(' ');
9170 else
9171#endif
9172 {
9173 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009174 if ((State & REPLACE_FLAG))
9175 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 }
9177 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9178 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009179
9180 /* If we are now back where we started delete one character. Can
9181 * happen when using 'sts' and 'linebreak'. */
9182 if (vcol >= start_vcol)
9183 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 }
9185
9186 /*
9187 * Delete upto starting point, start of line or previous word.
9188 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009189 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009191#ifdef FEAT_MBYTE
9192 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009194 if (has_mbyte)
9195 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009197 do
9198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009200 if (!revins_on) /* put cursor on char to be deleted */
9201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009203
9204 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009206 /* look multi-byte character class */
9207 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009209 prev_cclass = cclass;
9210 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009213
9214 /* start of word? */
9215 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9216 {
9217 mode = BACKSPACE_WORD_NOT_SPACE;
9218 temp = vim_iswordc(cc);
9219 }
9220 /* end of word? */
9221 else if (mode == BACKSPACE_WORD_NOT_SPACE
9222 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9223#ifdef FEAT_MBYTE
9224 || prev_cclass != cclass
9225#endif
9226 ))
9227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009229 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009231 inc_cursor();
9232#ifdef FEAT_RIGHTLEFT
9233 else if (State & REPLACE_FLAG)
9234 dec_cursor();
9235#endif
9236 break;
9237 }
9238 if (State & REPLACE_FLAG)
9239 replace_do_bs(-1);
9240 else
9241 {
9242#ifdef FEAT_MBYTE
9243 if (enc_utf8 && p_deco)
9244 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9245#endif
9246 (void)del_char(FALSE);
9247#ifdef FEAT_MBYTE
9248 /*
9249 * If there are combining characters and 'delcombine' is set
9250 * move the cursor back. Don't back up before the base
9251 * character.
9252 */
9253 if (enc_utf8 && p_deco && cpc[0] != NUL)
9254 inc_cursor();
9255#endif
9256#ifdef FEAT_RIGHTLEFT
9257 if (revins_chars)
9258 {
9259 revins_chars--;
9260 revins_legal++;
9261 }
9262 if (revins_on && gchar_cursor() == NUL)
9263 break;
9264#endif
9265 }
9266 /* Just a single backspace?: */
9267 if (mode == BACKSPACE_CHAR)
9268 break;
9269 } while (
9270#ifdef FEAT_RIGHTLEFT
9271 revins_on ||
9272#endif
9273 (curwin->w_cursor.col > mincol
9274 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9275 || curwin->w_cursor.col != Insstart_orig.col)));
9276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 did_backspace = TRUE;
9278 }
9279#ifdef FEAT_SMARTINDENT
9280 did_si = FALSE;
9281 can_si = FALSE;
9282 can_si_back = FALSE;
9283#endif
9284 if (curwin->w_cursor.col <= 1)
9285 did_ai = FALSE;
9286 /*
9287 * It's a little strange to put backspaces into the redo
9288 * buffer, but it makes auto-indent a lot easier to deal
9289 * with.
9290 */
9291 AppendCharToRedobuff(c);
9292
9293 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009294 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9295 && curwin->w_cursor.col < Insstart_orig.col)
9296 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297
9298 /* vi behaviour: the cursor moves backward but the character that
9299 * was there remains visible
9300 * Vim behaviour: the cursor moves backward and the character that
9301 * was there is erased from the screen.
9302 * We can emulate the vi behaviour by pretending there is a dollar
9303 * displayed even when there isn't.
9304 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009305 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 dollar_vcol = curwin->w_virtcol;
9307
Bram Moolenaarce3be472008-01-14 19:12:28 +00009308#ifdef FEAT_FOLDING
9309 /* When deleting a char the cursor line must never be in a closed fold.
9310 * E.g., when 'foldmethod' is indent and deleting the first non-white
9311 * char before a Tab. */
9312 if (did_backspace)
9313 foldOpenCursor();
9314#endif
9315
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 return did_backspace;
9317}
9318
9319#ifdef FEAT_MOUSE
9320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009321ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322{
9323 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009324 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325
9326# ifdef FEAT_GUI
9327 /* When GUI is active, also move/paste when 'mouse' is empty */
9328 if (!gui.in_use)
9329# endif
9330 if (!mouse_has(MOUSE_INSERT))
9331 return;
9332
9333 undisplay_dollar();
9334 tpos = curwin->w_cursor;
9335 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9336 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009337#ifdef FEAT_WINDOWS
9338 win_T *new_curwin = curwin;
9339
9340 if (curwin != old_curwin && win_valid(old_curwin))
9341 {
9342 /* Mouse took us to another window. We need to go back to the
9343 * previous one to stop insert there properly. */
9344 curwin = old_curwin;
9345 curbuf = curwin->w_buffer;
9346 }
9347#endif
9348 start_arrow(curwin == old_curwin ? &tpos : NULL);
9349#ifdef FEAT_WINDOWS
9350 if (curwin != new_curwin && win_valid(new_curwin))
9351 {
9352 curwin = new_curwin;
9353 curbuf = curwin->w_buffer;
9354 }
9355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356# ifdef FEAT_CINDENT
9357 can_cindent = TRUE;
9358# endif
9359 }
9360
9361#ifdef FEAT_WINDOWS
9362 /* redraw status lines (in case another window became active) */
9363 redraw_statuslines();
9364#endif
9365}
9366
9367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009368ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
9370 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009371# if defined(FEAT_WINDOWS)
9372 win_T *old_curwin = curwin;
9373# endif
9374# ifdef FEAT_INS_EXPAND
9375 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376# endif
9377
9378 tpos = curwin->w_cursor;
9379
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009380# ifdef FEAT_WINDOWS
9381 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 {
9383 int row, col;
9384
9385 row = mouse_row;
9386 col = mouse_col;
9387
9388 /* find the window at the pointer coordinates */
9389 curwin = mouse_find_win(&row, &col);
9390 curbuf = curwin->w_buffer;
9391 }
9392 if (curwin == old_curwin)
9393# endif
9394 undisplay_dollar();
9395
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009396# ifdef FEAT_INS_EXPAND
9397 /* Don't scroll the window in which completion is being done. */
9398 if (!pum_visible()
9399# if defined(FEAT_WINDOWS)
9400 || curwin != old_curwin
9401# endif
9402 )
9403# endif
9404 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009405 if (dir == MSCR_DOWN || dir == MSCR_UP)
9406 {
9407 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9408 scroll_redraw(dir,
9409 (long)(curwin->w_botline - curwin->w_topline));
9410 else
9411 scroll_redraw(dir, 3L);
9412 }
9413#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009414 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009415 {
9416 int val, step = 6;
9417
9418 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9419 step = W_WIDTH(curwin);
9420 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9421 if (val < 0)
9422 val = 0;
9423 gui_do_horiz_scroll(val, TRUE);
9424 }
9425#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009426# ifdef FEAT_INS_EXPAND
9427 did_scroll = TRUE;
9428# endif
9429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009431# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 curwin->w_redr_status = TRUE;
9433
9434 curwin = old_curwin;
9435 curbuf = curwin->w_buffer;
9436# endif
9437
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009438# ifdef FEAT_INS_EXPAND
9439 /* The popup menu may overlay the window, need to redraw it.
9440 * TODO: Would be more efficient to only redraw the windows that are
9441 * overlapped by the popup menu. */
9442 if (pum_visible() && did_scroll)
9443 {
9444 redraw_all_later(NOT_VALID);
9445 ins_compl_show_pum();
9446 }
9447# endif
9448
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 if (!equalpos(curwin->w_cursor, tpos))
9450 {
9451 start_arrow(&tpos);
9452# ifdef FEAT_CINDENT
9453 can_cindent = TRUE;
9454# endif
9455 }
9456}
9457#endif
9458
Bram Moolenaarec2da362017-01-21 20:04:22 +01009459/*
9460 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9461 * P_PE literally.
9462 * When "drop" is TRUE then consume the text and drop it.
9463 */
9464 int
9465bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9466{
9467 int c;
9468 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9469 int idx = 0;
9470 char_u *end = find_termcode((char_u *)"PE");
9471 int ret_char = -1;
9472 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009473 int save_paste = p_paste;
9474 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009475
9476 /* If the end code is too long we can't detect it, read everything. */
9477 if (STRLEN(end) >= NUMBUFLEN)
9478 end = NULL;
9479 ++no_mapping;
9480 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009481 p_paste = TRUE;
9482 curbuf->b_p_ai = FALSE;
9483
Bram Moolenaarec2da362017-01-21 20:04:22 +01009484 for (;;)
9485 {
9486 /* When the end is not defined read everything. */
9487 if (end == NULL && vpeekc() == NUL)
9488 break;
9489 c = plain_vgetc();
9490#ifdef FEAT_MBYTE
9491 if (has_mbyte)
9492 idx += (*mb_char2bytes)(c, buf + idx);
9493 else
9494#endif
9495 buf[idx++] = c;
9496 buf[idx] = NUL;
9497 if (end != NUL && STRNCMP(buf, end, idx) == 0)
9498 {
9499 if (end[idx] == NUL)
9500 break; /* Found the end of paste code. */
9501 continue;
9502 }
9503 if (!drop)
9504 {
9505 switch (mode)
9506 {
9507 case PASTE_CMDLINE:
9508 put_on_cmdline(buf, idx, TRUE);
9509 break;
9510
9511 case PASTE_EX:
9512 if (gap != NULL && ga_grow(gap, idx) == OK)
9513 {
9514 mch_memmove((char *)gap->ga_data + gap->ga_len,
9515 buf, (size_t)idx);
9516 gap->ga_len += idx;
9517 }
9518 break;
9519
9520 case PASTE_INSERT:
9521 if (stop_arrow() == OK)
9522 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009523 c = buf[0];
9524 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9525 ins_eol(c);
9526 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009527 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009528 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009529 AppendToRedobuffLit(buf, idx);
9530 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009531 }
9532 break;
9533
9534 case PASTE_ONE_CHAR:
9535 if (ret_char == -1)
9536 {
9537#ifdef FEAT_MBYTE
9538 if (has_mbyte)
9539 ret_char = (*mb_ptr2char)(buf);
9540 else
9541#endif
9542 ret_char = buf[0];
9543 }
9544 break;
9545 }
9546 }
9547 idx = 0;
9548 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009549
Bram Moolenaarec2da362017-01-21 20:04:22 +01009550 --no_mapping;
9551 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009552 p_paste = save_paste;
9553 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009554
9555 return ret_char;
9556}
9557
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009558#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009560ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009561{
9562 /* We will be leaving the current window, unless closing another tab. */
9563 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9564 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9565 {
9566 undisplay_dollar();
9567 start_arrow(&curwin->w_cursor);
9568# ifdef FEAT_CINDENT
9569 can_cindent = TRUE;
9570# endif
9571 }
9572
9573 if (c == K_TABLINE)
9574 goto_tabpage(current_tab);
9575 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009576 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009577 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009578 redraw_statuslines(); /* will redraw the tabline when needed */
9579 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009580}
9581#endif
9582
9583#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009585ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586{
9587 pos_T tpos;
9588
9589 undisplay_dollar();
9590 tpos = curwin->w_cursor;
9591 if (gui_do_scroll())
9592 {
9593 start_arrow(&tpos);
9594# ifdef FEAT_CINDENT
9595 can_cindent = TRUE;
9596# endif
9597 }
9598}
9599
9600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009601ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602{
9603 pos_T tpos;
9604
9605 undisplay_dollar();
9606 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009607 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 {
9609 start_arrow(&tpos);
9610# ifdef FEAT_CINDENT
9611 can_cindent = TRUE;
9612# endif
9613 }
9614}
9615#endif
9616
9617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009618ins_left(
9619 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620{
9621 pos_T tpos;
9622
9623#ifdef FEAT_FOLDING
9624 if ((fdo_flags & FDO_HOR) && KeyTyped)
9625 foldOpenCursor();
9626#endif
9627 undisplay_dollar();
9628 tpos = curwin->w_cursor;
9629 if (oneleft() == OK)
9630 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009631#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9632 /* Only call start_arrow() when not busy with preediting, it will
9633 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9634 if (!im_is_preediting())
9635#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009636 {
9637 start_arrow_with_change(&tpos, end_change);
9638 if (!end_change)
9639 AppendCharToRedobuff(K_LEFT);
9640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641#ifdef FEAT_RIGHTLEFT
9642 /* If exit reversed string, position is fixed */
9643 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9644 revins_legal++;
9645 revins_chars++;
9646#endif
9647 }
9648
9649 /*
9650 * if 'whichwrap' set for cursor in insert mode may go to
9651 * previous line
9652 */
9653 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9654 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009655 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 start_arrow(&tpos);
9657 --(curwin->w_cursor.lnum);
9658 coladvance((colnr_T)MAXCOL);
9659 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9660 }
9661 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009662 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009663 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664}
9665
9666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009667ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668{
9669 pos_T tpos;
9670
9671#ifdef FEAT_FOLDING
9672 if ((fdo_flags & FDO_HOR) && KeyTyped)
9673 foldOpenCursor();
9674#endif
9675 undisplay_dollar();
9676 tpos = curwin->w_cursor;
9677 if (c == K_C_HOME)
9678 curwin->w_cursor.lnum = 1;
9679 curwin->w_cursor.col = 0;
9680#ifdef FEAT_VIRTUALEDIT
9681 curwin->w_cursor.coladd = 0;
9682#endif
9683 curwin->w_curswant = 0;
9684 start_arrow(&tpos);
9685}
9686
9687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009688ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689{
9690 pos_T tpos;
9691
9692#ifdef FEAT_FOLDING
9693 if ((fdo_flags & FDO_HOR) && KeyTyped)
9694 foldOpenCursor();
9695#endif
9696 undisplay_dollar();
9697 tpos = curwin->w_cursor;
9698 if (c == K_C_END)
9699 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9700 coladvance((colnr_T)MAXCOL);
9701 curwin->w_curswant = MAXCOL;
9702
9703 start_arrow(&tpos);
9704}
9705
9706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009707ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708{
9709#ifdef FEAT_FOLDING
9710 if ((fdo_flags & FDO_HOR) && KeyTyped)
9711 foldOpenCursor();
9712#endif
9713 undisplay_dollar();
9714 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9715 {
9716 start_arrow(&curwin->w_cursor);
9717 (void)bck_word(1L, FALSE, FALSE);
9718 curwin->w_set_curswant = TRUE;
9719 }
9720 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009721 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722}
9723
9724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009725ins_right(
9726 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727{
9728#ifdef FEAT_FOLDING
9729 if ((fdo_flags & FDO_HOR) && KeyTyped)
9730 foldOpenCursor();
9731#endif
9732 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009733 if (gchar_cursor() != NUL
9734#ifdef FEAT_VIRTUALEDIT
9735 || virtual_active()
9736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 )
9738 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009739 start_arrow_with_change(&curwin->w_cursor, end_change);
9740 if (!end_change)
9741 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 curwin->w_set_curswant = TRUE;
9743#ifdef FEAT_VIRTUALEDIT
9744 if (virtual_active())
9745 oneright();
9746 else
9747#endif
9748 {
9749#ifdef FEAT_MBYTE
9750 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009751 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 else
9753#endif
9754 ++curwin->w_cursor.col;
9755 }
9756
9757#ifdef FEAT_RIGHTLEFT
9758 revins_legal++;
9759 if (revins_chars)
9760 revins_chars--;
9761#endif
9762 }
9763 /* if 'whichwrap' set for cursor in insert mode, may move the
9764 * cursor to the next line */
9765 else if (vim_strchr(p_ww, ']') != NULL
9766 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9767 {
9768 start_arrow(&curwin->w_cursor);
9769 curwin->w_set_curswant = TRUE;
9770 ++curwin->w_cursor.lnum;
9771 curwin->w_cursor.col = 0;
9772 }
9773 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009774 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009775 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776}
9777
9778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009779ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780{
9781#ifdef FEAT_FOLDING
9782 if ((fdo_flags & FDO_HOR) && KeyTyped)
9783 foldOpenCursor();
9784#endif
9785 undisplay_dollar();
9786 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9787 || gchar_cursor() != NUL)
9788 {
9789 start_arrow(&curwin->w_cursor);
9790 (void)fwd_word(1L, FALSE, 0);
9791 curwin->w_set_curswant = TRUE;
9792 }
9793 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009794 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795}
9796
9797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009798ins_up(
9799 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
9801 pos_T tpos;
9802 linenr_T old_topline = curwin->w_topline;
9803#ifdef FEAT_DIFF
9804 int old_topfill = curwin->w_topfill;
9805#endif
9806
9807 undisplay_dollar();
9808 tpos = curwin->w_cursor;
9809 if (cursor_up(1L, TRUE) == OK)
9810 {
9811 if (startcol)
9812 coladvance(getvcol_nolist(&Insstart));
9813 if (old_topline != curwin->w_topline
9814#ifdef FEAT_DIFF
9815 || old_topfill != curwin->w_topfill
9816#endif
9817 )
9818 redraw_later(VALID);
9819 start_arrow(&tpos);
9820#ifdef FEAT_CINDENT
9821 can_cindent = TRUE;
9822#endif
9823 }
9824 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009825 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826}
9827
9828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009829ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830{
9831 pos_T tpos;
9832
9833 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009834
9835#ifdef FEAT_WINDOWS
9836 if (mod_mask & MOD_MASK_CTRL)
9837 {
9838 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009839 if (first_tabpage->tp_next != NULL)
9840 {
9841 start_arrow(&curwin->w_cursor);
9842 goto_tabpage(-1);
9843 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009844 return;
9845 }
9846#endif
9847
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 tpos = curwin->w_cursor;
9849 if (onepage(BACKWARD, 1L) == OK)
9850 {
9851 start_arrow(&tpos);
9852#ifdef FEAT_CINDENT
9853 can_cindent = TRUE;
9854#endif
9855 }
9856 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009857 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858}
9859
9860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009861ins_down(
9862 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863{
9864 pos_T tpos;
9865 linenr_T old_topline = curwin->w_topline;
9866#ifdef FEAT_DIFF
9867 int old_topfill = curwin->w_topfill;
9868#endif
9869
9870 undisplay_dollar();
9871 tpos = curwin->w_cursor;
9872 if (cursor_down(1L, TRUE) == OK)
9873 {
9874 if (startcol)
9875 coladvance(getvcol_nolist(&Insstart));
9876 if (old_topline != curwin->w_topline
9877#ifdef FEAT_DIFF
9878 || old_topfill != curwin->w_topfill
9879#endif
9880 )
9881 redraw_later(VALID);
9882 start_arrow(&tpos);
9883#ifdef FEAT_CINDENT
9884 can_cindent = TRUE;
9885#endif
9886 }
9887 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009888 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889}
9890
9891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009892ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893{
9894 pos_T tpos;
9895
9896 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009897
9898#ifdef FEAT_WINDOWS
9899 if (mod_mask & MOD_MASK_CTRL)
9900 {
9901 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009902 if (first_tabpage->tp_next != NULL)
9903 {
9904 start_arrow(&curwin->w_cursor);
9905 goto_tabpage(0);
9906 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009907 return;
9908 }
9909#endif
9910
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 tpos = curwin->w_cursor;
9912 if (onepage(FORWARD, 1L) == OK)
9913 {
9914 start_arrow(&tpos);
9915#ifdef FEAT_CINDENT
9916 can_cindent = TRUE;
9917#endif
9918 }
9919 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009920 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921}
9922
9923#ifdef FEAT_DND
9924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009925ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926{
9927 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9928}
9929#endif
9930
9931/*
9932 * Handle TAB in Insert or Replace mode.
9933 * Return TRUE when the TAB needs to be inserted like a normal character.
9934 */
9935 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009936ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937{
9938 int ind;
9939 int i;
9940 int temp;
9941
9942 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9943 Insstart_blank_vcol = get_nolist_virtcol();
9944 if (echeck_abbr(TAB + ABBR_OFF))
9945 return FALSE;
9946
9947 ind = inindent(0);
9948#ifdef FEAT_CINDENT
9949 if (ind)
9950 can_cindent = FALSE;
9951#endif
9952
9953 /*
9954 * When nothing special, insert TAB like a normal character
9955 */
9956 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009957 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009958 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 return TRUE;
9960
9961 if (stop_arrow() == FAIL)
9962 return TRUE;
9963
9964 did_ai = FALSE;
9965#ifdef FEAT_SMARTINDENT
9966 did_si = FALSE;
9967 can_si = FALSE;
9968 can_si_back = FALSE;
9969#endif
9970 AppendToRedobuff((char_u *)"\t");
9971
9972 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009973 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009974 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9975 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 else /* otherwise use 'tabstop' */
9977 temp = (int)curbuf->b_p_ts;
9978 temp -= get_nolist_virtcol() % temp;
9979
9980 /*
9981 * Insert the first space with ins_char(). It will delete one char in
9982 * replace mode. Insert the rest with ins_str(); it will not delete any
9983 * chars. For VREPLACE mode, we use ins_char() for all characters.
9984 */
9985 ins_char(' ');
9986 while (--temp > 0)
9987 {
9988#ifdef FEAT_VREPLACE
9989 if (State & VREPLACE_FLAG)
9990 ins_char(' ');
9991 else
9992#endif
9993 {
9994 ins_str((char_u *)" ");
9995 if (State & REPLACE_FLAG) /* no char replaced */
9996 replace_push(NUL);
9997 }
9998 }
9999
10000 /*
10001 * When 'expandtab' not set: Replace spaces by TABs where possible.
10002 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010003 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 {
10005 char_u *ptr;
10006#ifdef FEAT_VREPLACE
10007 char_u *saved_line = NULL; /* init for GCC */
10008 pos_T pos;
10009#endif
10010 pos_T fpos;
10011 pos_T *cursor;
10012 colnr_T want_vcol, vcol;
10013 int change_col = -1;
10014 int save_list = curwin->w_p_list;
10015
10016 /*
10017 * Get the current line. For VREPLACE mode, don't make real changes
10018 * yet, just work on a copy of the line.
10019 */
10020#ifdef FEAT_VREPLACE
10021 if (State & VREPLACE_FLAG)
10022 {
10023 pos = curwin->w_cursor;
10024 cursor = &pos;
10025 saved_line = vim_strsave(ml_get_curline());
10026 if (saved_line == NULL)
10027 return FALSE;
10028 ptr = saved_line + pos.col;
10029 }
10030 else
10031#endif
10032 {
10033 ptr = ml_get_cursor();
10034 cursor = &curwin->w_cursor;
10035 }
10036
10037 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10038 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10039 curwin->w_p_list = FALSE;
10040
10041 /* Find first white before the cursor */
10042 fpos = curwin->w_cursor;
10043 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
10044 {
10045 --fpos.col;
10046 --ptr;
10047 }
10048
10049 /* In Replace mode, don't change characters before the insert point. */
10050 if ((State & REPLACE_FLAG)
10051 && fpos.lnum == Insstart.lnum
10052 && fpos.col < Insstart.col)
10053 {
10054 ptr += Insstart.col - fpos.col;
10055 fpos.col = Insstart.col;
10056 }
10057
10058 /* compute virtual column numbers of first white and cursor */
10059 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10060 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10061
Bram Moolenaar597a4222014-06-25 14:39:50 +020010062 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10063 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 while (vim_iswhite(*ptr))
10065 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010066 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 if (vcol + i > want_vcol)
10068 break;
10069 if (*ptr != TAB)
10070 {
10071 *ptr = TAB;
10072 if (change_col < 0)
10073 {
10074 change_col = fpos.col; /* Column of first change */
10075 /* May have to adjust Insstart */
10076 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10077 Insstart.col = fpos.col;
10078 }
10079 }
10080 ++fpos.col;
10081 ++ptr;
10082 vcol += i;
10083 }
10084
10085 if (change_col >= 0)
10086 {
10087 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010088 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089
10090 /* Skip over the spaces we need. */
10091 while (vcol < want_vcol && *ptr == ' ')
10092 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010093 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 ++ptr;
10095 ++repl_off;
10096 }
10097 if (vcol > want_vcol)
10098 {
10099 /* Must have a char with 'showbreak' just before it. */
10100 --ptr;
10101 --repl_off;
10102 }
10103 fpos.col += repl_off;
10104
10105 /* Delete following spaces. */
10106 i = cursor->col - fpos.col;
10107 if (i > 0)
10108 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010109 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 /* correct replace stack. */
10111 if ((State & REPLACE_FLAG)
10112#ifdef FEAT_VREPLACE
10113 && !(State & VREPLACE_FLAG)
10114#endif
10115 )
10116 for (temp = i; --temp >= 0; )
10117 replace_join(repl_off);
10118 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010119#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010120 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010121 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010122 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010123 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10124 (char_u *)"\t", 1);
10125 }
10126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 cursor->col -= i;
10128
10129#ifdef FEAT_VREPLACE
10130 /*
10131 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10132 * backspacing over the changed spacing and then inserting the new
10133 * spacing.
10134 */
10135 if (State & VREPLACE_FLAG)
10136 {
10137 /* Backspace from real cursor to change_col */
10138 backspace_until_column(change_col);
10139
10140 /* Insert each char in saved_line from changed_col to
10141 * ptr-cursor */
10142 ins_bytes_len(saved_line + change_col,
10143 cursor->col - change_col);
10144 }
10145#endif
10146 }
10147
10148#ifdef FEAT_VREPLACE
10149 if (State & VREPLACE_FLAG)
10150 vim_free(saved_line);
10151#endif
10152 curwin->w_p_list = save_list;
10153 }
10154
10155 return FALSE;
10156}
10157
10158/*
10159 * Handle CR or NL in insert mode.
10160 * Return TRUE when out of memory or can't undo.
10161 */
10162 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010163ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164{
10165 int i;
10166
10167 if (echeck_abbr(c + ABBR_OFF))
10168 return FALSE;
10169 if (stop_arrow() == FAIL)
10170 return TRUE;
10171 undisplay_dollar();
10172
10173 /*
10174 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10175 * character under the cursor. Only push a NUL on the replace stack,
10176 * nothing to put back when the NL is deleted.
10177 */
10178 if ((State & REPLACE_FLAG)
10179#ifdef FEAT_VREPLACE
10180 && !(State & VREPLACE_FLAG)
10181#endif
10182 )
10183 replace_push(NUL);
10184
10185 /*
10186 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10187 * replacing the next line, so we push all of the characters left on the
10188 * line onto the replace stack. This is not done here though, it is done
10189 * in open_line().
10190 */
10191
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010192#ifdef FEAT_VIRTUALEDIT
10193 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10194 * CTRL-O). */
10195 if (virtual_active() && curwin->w_cursor.coladd > 0)
10196 coladvance(getviscol());
10197#endif
10198
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199#ifdef FEAT_RIGHTLEFT
10200# ifdef FEAT_FKMAP
10201 if (p_altkeymap && p_fkmap)
10202 fkmap(NL);
10203# endif
10204 /* NL in reverse insert will always start in the end of
10205 * current line. */
10206 if (revins_on)
10207 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10208#endif
10209
10210 AppendToRedobuff(NL_STR);
10211 i = open_line(FORWARD,
10212#ifdef FEAT_COMMENTS
10213 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10214#endif
10215 0, old_indent);
10216 old_indent = 0;
10217#ifdef FEAT_CINDENT
10218 can_cindent = TRUE;
10219#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010220#ifdef FEAT_FOLDING
10221 /* When inserting a line the cursor line must never be in a closed fold. */
10222 foldOpenCursor();
10223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224
10225 return (!i);
10226}
10227
10228#ifdef FEAT_DIGRAPHS
10229/*
10230 * Handle digraph in insert mode.
10231 * Returns character still to be inserted, or NUL when nothing remaining to be
10232 * done.
10233 */
10234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010235ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236{
10237 int c;
10238 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010239 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240
10241 pc_status = PC_STATUS_UNSET;
10242 if (redrawing() && !char_avail())
10243 {
10244 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010245 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246
10247 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010248 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249#ifdef FEAT_CMDL_INFO
10250 add_to_showcmd_c(Ctrl_K);
10251#endif
10252 }
10253
10254#ifdef USE_ON_FLY_SCROLL
10255 dont_scroll = TRUE; /* disallow scrolling here */
10256#endif
10257
10258 /* don't map the digraph chars. This also prevents the
10259 * mode message to be deleted when ESC is hit */
10260 ++no_mapping;
10261 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010262 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 --no_mapping;
10264 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010265 if (did_putchar)
10266 /* when the line fits in 'columns' the '?' is at the start of the next
10267 * line and will not be removed by the redraw */
10268 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010269
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 if (IS_SPECIAL(c) || mod_mask) /* special key */
10271 {
10272#ifdef FEAT_CMDL_INFO
10273 clear_showcmd();
10274#endif
10275 insert_special(c, TRUE, FALSE);
10276 return NUL;
10277 }
10278 if (c != ESC)
10279 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010280 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 if (redrawing() && !char_avail())
10282 {
10283 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010284 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285
10286 if (char2cells(c) == 1)
10287 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010288 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010290 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 }
10292#ifdef FEAT_CMDL_INFO
10293 add_to_showcmd_c(c);
10294#endif
10295 }
10296 ++no_mapping;
10297 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010298 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 --no_mapping;
10300 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010301 if (did_putchar)
10302 /* when the line fits in 'columns' the '?' is at the start of the
10303 * next line and will not be removed by a redraw */
10304 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 if (cc != ESC)
10306 {
10307 AppendToRedobuff((char_u *)CTRL_V_STR);
10308 c = getdigraph(c, cc, TRUE);
10309#ifdef FEAT_CMDL_INFO
10310 clear_showcmd();
10311#endif
10312 return c;
10313 }
10314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315#ifdef FEAT_CMDL_INFO
10316 clear_showcmd();
10317#endif
10318 return NUL;
10319}
10320#endif
10321
10322/*
10323 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10324 * Returns the char to be inserted, or NUL if none found.
10325 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010326 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010327ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328{
10329 int c;
10330 int temp;
10331 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010332 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333
10334 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10335 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010336 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 return NUL;
10338 }
10339
10340 /* try to advance to the cursor column */
10341 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010342 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 prev_ptr = ptr;
10344 validate_virtcol();
10345 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10346 {
10347 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010348 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 }
10350 if ((colnr_T)temp > curwin->w_virtcol)
10351 ptr = prev_ptr;
10352
10353#ifdef FEAT_MBYTE
10354 c = (*mb_ptr2char)(ptr);
10355#else
10356 c = *ptr;
10357#endif
10358 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010359 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 return c;
10361}
10362
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010363/*
10364 * CTRL-Y or CTRL-E typed in Insert mode.
10365 */
10366 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010367ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010368{
10369 int c = tc;
10370
10371#ifdef FEAT_INS_EXPAND
10372 if (ctrl_x_mode == CTRL_X_SCROLL)
10373 {
10374 if (c == Ctrl_Y)
10375 scrolldown_clamp();
10376 else
10377 scrollup_clamp();
10378 redraw_later(VALID);
10379 }
10380 else
10381#endif
10382 {
10383 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10384 if (c != NUL)
10385 {
10386 long tw_save;
10387
10388 /* The character must be taken literally, insert like it
10389 * was typed after a CTRL-V, and pretend 'textwidth'
10390 * wasn't set. Digits, 'o' and 'x' are special after a
10391 * CTRL-V, don't use it for these. */
10392 if (c < 256 && !isalnum(c))
10393 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10394 tw_save = curbuf->b_p_tw;
10395 curbuf->b_p_tw = -1;
10396 insert_special(c, TRUE, FALSE);
10397 curbuf->b_p_tw = tw_save;
10398#ifdef FEAT_RIGHTLEFT
10399 revins_chars++;
10400 revins_legal++;
10401#endif
10402 c = Ctrl_V; /* pretend CTRL-V is last character */
10403 auto_format(FALSE, TRUE);
10404 }
10405 }
10406 return c;
10407}
10408
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409#ifdef FEAT_SMARTINDENT
10410/*
10411 * Try to do some very smart auto-indenting.
10412 * Used when inserting a "normal" character.
10413 */
10414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010415ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416{
10417 pos_T *pos, old_pos;
10418 char_u *ptr;
10419 int i;
10420 int temp;
10421
10422 /*
10423 * do some very smart indenting when entering '{' or '}'
10424 */
10425 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10426 {
10427 /*
10428 * for '}' set indent equal to indent of line containing matching '{'
10429 */
10430 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10431 {
10432 old_pos = curwin->w_cursor;
10433 /*
10434 * If the matching '{' has a ')' immediately before it (ignoring
10435 * white-space), then line up with the start of the line
10436 * containing the matching '(' if there is one. This handles the
10437 * case where an "if (..\n..) {" statement continues over multiple
10438 * lines -- webb
10439 */
10440 ptr = ml_get(pos->lnum);
10441 i = pos->col;
10442 if (i > 0) /* skip blanks before '{' */
10443 while (--i > 0 && vim_iswhite(ptr[i]))
10444 ;
10445 curwin->w_cursor.lnum = pos->lnum;
10446 curwin->w_cursor.col = i;
10447 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10448 curwin->w_cursor = *pos;
10449 i = get_indent();
10450 curwin->w_cursor = old_pos;
10451#ifdef FEAT_VREPLACE
10452 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010453 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 else
10455#endif
10456 (void)set_indent(i, SIN_CHANGED);
10457 }
10458 else if (curwin->w_cursor.col > 0)
10459 {
10460 /*
10461 * when inserting '{' after "O" reduce indent, but not
10462 * more than indent of previous line
10463 */
10464 temp = TRUE;
10465 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10466 {
10467 old_pos = curwin->w_cursor;
10468 i = get_indent();
10469 while (curwin->w_cursor.lnum > 1)
10470 {
10471 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10472
10473 /* ignore empty lines and lines starting with '#'. */
10474 if (*ptr != '#' && *ptr != NUL)
10475 break;
10476 }
10477 if (get_indent() >= i)
10478 temp = FALSE;
10479 curwin->w_cursor = old_pos;
10480 }
10481 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010482 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 }
10484 }
10485
10486 /*
10487 * set indent of '#' always to 0
10488 */
10489 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10490 {
10491 /* remember current indent for next line */
10492 old_indent = get_indent();
10493 (void)set_indent(0, SIN_CHANGED);
10494 }
10495
10496 /* Adjust ai_col, the char at this position can be deleted. */
10497 if (ai_col > curwin->w_cursor.col)
10498 ai_col = curwin->w_cursor.col;
10499}
10500#endif
10501
10502/*
10503 * Get the value that w_virtcol would have when 'list' is off.
10504 * Unless 'cpo' contains the 'L' flag.
10505 */
10506 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010507get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10510 return getvcol_nolist(&curwin->w_cursor);
10511 validate_virtcol();
10512 return curwin->w_virtcol;
10513}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010514
10515#ifdef FEAT_AUTOCMD
10516/*
10517 * Handle the InsertCharPre autocommand.
10518 * "c" is the character that was typed.
10519 * Return a pointer to allocated memory with the replacement string.
10520 * Return NULL to continue inserting "c".
10521 */
10522 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010523do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010524{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010525 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010526 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010527
10528 /* Return quickly when there is nothing to do. */
10529 if (!has_insertcharpre())
10530 return NULL;
10531
Bram Moolenaar704984a2012-06-01 14:57:51 +020010532#ifdef FEAT_MBYTE
10533 if (has_mbyte)
10534 buf[(*mb_char2bytes)(c, buf)] = NUL;
10535 else
10536#endif
10537 {
10538 buf[0] = c;
10539 buf[1] = NUL;
10540 }
10541
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010542 /* Lock the text to avoid weird things from happening. */
10543 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010544 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010545
Bram Moolenaar704984a2012-06-01 14:57:51 +020010546 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010547 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010548 {
10549 /* Get the value of v:char. It may be empty or more than one
10550 * character. Only use it when changed, otherwise continue with the
10551 * original character to avoid breaking autoindent. */
10552 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10553 res = vim_strsave(get_vim_var_str(VV_CHAR));
10554 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010555
10556 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10557 --textlock;
10558
10559 return res;
10560}
10561#endif