blob: 25896f3d3c746716e416de15e44ed33aff0a9db1 [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);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100189static void show_pum(int prev_w_wrow, int prev_w_leftcol);
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 Moolenaar74a47162017-02-26 19:09:05 +01001041#ifdef CURSOR_SHAPE
1042 ui_cursor_shape(); /* may need to update cursor shape */
1043#endif
1044 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045
1046 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001047#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001048 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 goto docomplete;
1050#endif
1051 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1052 break;
1053 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001054
1055#ifdef FEAT_VIRTUALEDIT
1056 /* don't move the cursor left when 'virtualedit' has "onemore". */
1057 if (ve_flags & VE_ONEMORE)
1058 {
1059 ins_at_eol = FALSE;
1060 nomove = TRUE;
1061 }
1062#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001063 count = 0;
1064 goto doESCkey;
1065
Bram Moolenaar572cb562005-08-05 21:35:02 +00001066 case K_INS: /* toggle insert/replace mode */
1067 case K_KINS:
1068 ins_insert(replaceState);
1069 break;
1070
1071 case K_SELECT: /* end of Select mode mapping - ignore */
1072 break;
1073
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001074 case K_HELP: /* Help key works like <ESC> <Help> */
1075 case K_F1:
1076 case K_XF1:
1077 stuffcharReadbuff(K_HELP);
1078 if (p_im)
1079 need_start_insertmode = TRUE;
1080 goto doESCkey;
1081
1082#ifdef FEAT_NETBEANS_INTG
1083 case K_F21: /* NetBeans command */
1084 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001085 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 --no_mapping;
1087 netbeans_keycommand(i);
1088 break;
1089#endif
1090
1091 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 case NUL:
1093 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 /* For ^@ the trailing ESC will end the insert, unless there is an
1095 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1097 && c != Ctrl_A && !p_im)
1098 goto doESCkey; /* quit insert mode */
1099 inserted_space = FALSE;
1100 break;
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 ins_reg();
1104 auto_format(FALSE, TRUE);
1105 inserted_space = FALSE;
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_ctrl_g();
1110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case Ctrl_HAT: /* switch input mode and/or langmap */
1113 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 break;
1115
1116#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 if (!p_ari)
1119 goto normalchar;
1120 ins_ctrl_();
1121 break;
1122#endif
1123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1126 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1127 goto docomplete;
1128#endif
1129 /* FALLTHROUGH */
1130
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001131 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132# ifdef FEAT_INS_EXPAND
1133 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1134 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 if (has_compl_option(FALSE))
1136 goto docomplete;
1137 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139# endif
1140 ins_shift(c, lastc);
1141 auto_format(FALSE, TRUE);
1142 inserted_space = FALSE;
1143 break;
1144
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 case K_KDEL:
1147 ins_del();
1148 auto_format(FALSE, TRUE);
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case Ctrl_H:
1153 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1154 auto_format(FALSE, TRUE);
1155 break;
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1159 auto_format(FALSE, TRUE);
1160 break;
1161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001162 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001163# ifdef FEAT_COMPL_FUNC
1164 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001166 goto docomplete;
1167# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1169 auto_format(FALSE, TRUE);
1170 inserted_space = FALSE;
1171 break;
1172
1173#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 case K_LEFTMOUSE_NM:
1176 case K_LEFTDRAG:
1177 case K_LEFTRELEASE:
1178 case K_LEFTRELEASE_NM:
1179 case K_MIDDLEMOUSE:
1180 case K_MIDDLEDRAG:
1181 case K_MIDDLERELEASE:
1182 case K_RIGHTMOUSE:
1183 case K_RIGHTDRAG:
1184 case K_RIGHTRELEASE:
1185 case K_X1MOUSE:
1186 case K_X1DRAG:
1187 case K_X1RELEASE:
1188 case K_X2MOUSE:
1189 case K_X2DRAG:
1190 case K_X2RELEASE:
1191 ins_mouse(c);
1192 break;
1193
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001194 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001195 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 break;
1197
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001199 ins_mousescroll(MSCR_UP);
1200 break;
1201
1202 case K_MOUSELEFT: /* Scroll wheel left */
1203 ins_mousescroll(MSCR_LEFT);
1204 break;
1205
1206 case K_MOUSERIGHT: /* Scroll wheel right */
1207 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 break;
1209#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001210 case K_PS:
1211 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1212 if (cmdchar == K_PS)
1213 /* invoked from normal mode, bail out */
1214 goto doESCkey;
1215 break;
1216 case K_PE:
1217 /* Got K_PE without K_PS, ignore. */
1218 break;
1219
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001220#ifdef FEAT_GUI_TABLINE
1221 case K_TABLINE:
1222 case K_TABMENU:
1223 ins_tabline(c);
1224 break;
1225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001227 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 break;
1229
Bram Moolenaar754b5602006-02-09 23:53:20 +00001230#ifdef FEAT_AUTOCMD
1231 case K_CURSORHOLD: /* Didn't type something for a while. */
1232 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1233 did_cursorhold = TRUE;
1234 break;
1235#endif
1236
Bram Moolenaar4770d092006-01-12 23:22:24 +00001237#ifdef FEAT_GUI_W32
1238 /* On Win32 ignore <M-F4>, we get it when closing the window was
1239 * cancelled. */
1240 case K_F4:
1241 if (mod_mask != MOD_MASK_ALT)
1242 goto normalchar;
1243 break;
1244#endif
1245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#ifdef FEAT_GUI
1247 case K_VER_SCROLLBAR:
1248 ins_scroll();
1249 break;
1250
1251 case K_HOR_SCROLLBAR:
1252 ins_horscroll();
1253 break;
1254#endif
1255
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001256 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 case K_S_HOME:
1259 case K_C_HOME:
1260 ins_home(c);
1261 break;
1262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 case K_S_END:
1266 case K_C_END:
1267 ins_end(c);
1268 break;
1269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001270 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001271 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1272 ins_s_left();
1273 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001274 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 break;
1276
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001277 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 case K_C_LEFT:
1279 ins_s_left();
1280 break;
1281
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001282 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001283 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1284 ins_s_right();
1285 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001286 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 break;
1288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001289 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 case K_C_RIGHT:
1291 ins_s_right();
1292 break;
1293
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001294 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001295#ifdef FEAT_INS_EXPAND
1296 if (pum_visible())
1297 goto docomplete;
1298#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001299 if (mod_mask & MOD_MASK_SHIFT)
1300 ins_pageup();
1301 else
1302 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 break;
1304
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001305 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 case K_PAGEUP:
1307 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001308#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001309 if (pum_visible())
1310 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 ins_pageup();
1313 break;
1314
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001315 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001316#ifdef FEAT_INS_EXPAND
1317 if (pum_visible())
1318 goto docomplete;
1319#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001320 if (mod_mask & MOD_MASK_SHIFT)
1321 ins_pagedown();
1322 else
1323 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 break;
1325
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001326 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 case K_PAGEDOWN:
1328 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001329#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001330 if (pum_visible())
1331 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 ins_pagedown();
1334 break;
1335
1336#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001337 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 ins_drop();
1339 break;
1340#endif
1341
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001342 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 c = TAB;
1344 /* FALLTHROUGH */
1345
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001346 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1348 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1349 goto docomplete;
1350#endif
1351 inserted_space = FALSE;
1352 if (ins_tab())
1353 goto normalchar; /* insert TAB as a normal char */
1354 auto_format(FALSE, TRUE);
1355 break;
1356
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001357 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 c = CAR;
1359 /* FALLTHROUGH */
1360 case CAR:
1361 case NL:
1362#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1363 /* In a quickfix window a <CR> jumps to the error under the
1364 * cursor. */
1365 if (bt_quickfix(curbuf) && c == CAR)
1366 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001367 if (curwin->w_llist_ref == NULL) /* quickfix window */
1368 do_cmdline_cmd((char_u *)".cc");
1369 else /* location list window */
1370 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 break;
1372 }
1373#endif
1374#ifdef FEAT_CMDWIN
1375 if (cmdwin_type != 0)
1376 {
1377 /* Execute the command in the cmdline window. */
1378 cmdwin_result = CAR;
1379 goto doESCkey;
1380 }
1381#endif
1382 if (ins_eol(c) && !p_im)
1383 goto doESCkey; /* out of memory */
1384 auto_format(FALSE, FALSE);
1385 inserted_space = FALSE;
1386 break;
1387
Bram Moolenaar860cae12010-06-05 23:22:07 +02001388#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001389 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390# ifdef FEAT_INS_EXPAND
1391 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1392 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001393 if (has_compl_option(TRUE))
1394 goto docomplete;
1395 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
1397# endif
1398# ifdef FEAT_DIGRAPHS
1399 c = ins_digraph();
1400 if (c == NUL)
1401 break;
1402# endif
1403 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
1406#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001407 case Ctrl_X: /* Enter CTRL-X mode */
1408 ins_ctrl_x();
1409 break;
1410
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001411 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 if (ctrl_x_mode != CTRL_X_TAGS)
1413 goto normalchar;
1414 goto docomplete;
1415
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001416 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (ctrl_x_mode != CTRL_X_FILES)
1418 goto normalchar;
1419 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001420
1421 case 's': /* Spelling completion after ^X */
1422 case Ctrl_S:
1423 if (ctrl_x_mode != CTRL_X_SPELL)
1424 goto normalchar;
1425 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426#endif
1427
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001428 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429#ifdef FEAT_INS_EXPAND
1430 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1431#endif
1432 {
1433 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1434 if (p_im)
1435 {
1436 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1437 break;
1438 goto doESCkey;
1439 }
1440 goto normalchar;
1441 }
1442#ifdef FEAT_INS_EXPAND
1443 /* FALLTHROUGH */
1444
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001445 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 case Ctrl_N:
1447 /* if 'complete' is empty then plain ^P is no longer special,
1448 * but it is under other ^X modes */
1449 if (*curbuf->b_p_cpt == NUL
1450 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001451 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 goto normalchar;
1453
1454docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001455 compl_busy = TRUE;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001456 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001457 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001458 compl_cont_status = 0;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001459 disable_fold_update--;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001460 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 break;
1462#endif /* FEAT_INS_EXPAND */
1463
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001464 case Ctrl_Y: /* copy from previous line or scroll down */
1465 case Ctrl_E: /* copy from next line or scroll up */
1466 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 break;
1468
1469 default:
1470#ifdef UNIX
1471 if (c == intr_char) /* special interrupt char */
1472 goto do_intr;
1473#endif
1474
Bram Moolenaare659c952011-05-19 17:25:41 +02001475normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001477 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001479#ifdef FEAT_AUTOCMD
1480 if (!p_paste)
1481 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001482 /* Trigger InsertCharPre. */
1483 char_u *str = do_insert_char_pre(c);
1484 char_u *p;
1485
1486 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001487 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001488 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001489 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001490 /* Insert the new value of v:char literally. */
1491 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001492 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001493 c = PTR2CHAR(p);
1494 if (c == CAR || c == K_KENTER || c == NL)
1495 ins_eol(c);
1496 else
1497 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001498 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001499 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001500 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001501 vim_free(str);
1502 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001503 }
1504
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001505 /* If the new value is already inserted or an empty string
1506 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001507 if (c == NUL)
1508 break;
1509 }
1510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#ifdef FEAT_SMARTINDENT
1512 /* Try to perform smart-indenting. */
1513 ins_try_si(c);
1514#endif
1515
1516 if (c == ' ')
1517 {
1518 inserted_space = TRUE;
1519#ifdef FEAT_CINDENT
1520 if (inindent(0))
1521 can_cindent = FALSE;
1522#endif
1523 if (Insstart_blank_vcol == MAXCOL
1524 && curwin->w_cursor.lnum == Insstart.lnum)
1525 Insstart_blank_vcol = get_nolist_virtcol();
1526 }
1527
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001528 /* Insert a normal character and check for abbreviations on a
1529 * special character. Let CTRL-] expand abbreviations without
1530 * inserting it. */
1531 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532#ifdef FEAT_MBYTE
1533 /* Add ABBR_OFF for characters above 0x100, this is
1534 * what check_abbr() expects. */
1535 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1536#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001537 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {
1539 insert_special(c, FALSE, FALSE);
1540#ifdef FEAT_RIGHTLEFT
1541 revins_legal++;
1542 revins_chars++;
1543#endif
1544 }
1545
1546 auto_format(FALSE, TRUE);
1547
1548#ifdef FEAT_FOLDING
1549 /* When inserting a character the cursor line must never be in a
1550 * closed fold. */
1551 foldOpenCursor();
1552#endif
1553 break;
1554 } /* end of switch (c) */
1555
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001556#ifdef FEAT_AUTOCMD
1557 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001558 if (c != K_CURSORHOLD
1559# ifdef FEAT_COMPL_FUNC
1560 /* but not in CTRL-X mode, a script can't restore the state */
1561 && ctrl_x_mode == 0
1562# endif
1563 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001564 did_cursorhold = FALSE;
1565#endif
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 /* If the cursor was moved we didn't just insert a space */
1568 if (arrow_used)
1569 inserted_space = FALSE;
1570
1571#ifdef FEAT_CINDENT
1572 if (can_cindent && cindent_on()
1573# ifdef FEAT_INS_EXPAND
1574 && ctrl_x_mode == 0
1575# endif
1576 )
1577 {
1578force_cindent:
1579 /*
1580 * Indent now if a key was typed that is in 'cinkeys'.
1581 */
1582 if (in_cinkeys(c, ' ', line_is_white))
1583 {
1584 if (stop_arrow() == OK)
1585 /* re-indent the current line */
1586 do_c_expr_indent();
1587 }
1588 }
1589#endif /* FEAT_CINDENT */
1590
1591 } /* for (;;) */
1592 /* NOTREACHED */
1593}
1594
1595/*
1596 * Redraw for Insert mode.
1597 * This is postponed until getting the next character to make '$' in the 'cpo'
1598 * option work correctly.
1599 * Only redraw when there are no characters available. This speeds up
1600 * inserting sequences of characters (e.g., for CTRL-R).
1601 */
1602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001603ins_redraw(
1604 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001606#ifdef FEAT_CONCEAL
1607 linenr_T conceal_old_cursor_line = 0;
1608 linenr_T conceal_new_cursor_line = 0;
1609 int conceal_update_lines = FALSE;
1610#endif
1611
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001612 if (char_avail())
1613 return;
1614
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001615#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001616 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1617 * visible, the command might delete it. */
1618 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001619# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001620 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001621# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001622# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001623 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001624# endif
1625# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001626 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001627# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001628 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001629# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001630 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001631# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001632# ifdef FEAT_INS_EXPAND
1633 && !pum_visible()
1634# endif
1635 )
1636 {
1637# ifdef FEAT_SYN_HL
1638 /* Need to update the screen first, to make sure syntax
1639 * highlighting is correct after making a change (e.g., inserting
1640 * a "(". The autocommand may also require a redraw, so it's done
1641 * again below, unfortunately. */
1642 if (syntax_present(curwin) && must_redraw)
1643 update_screen(0);
1644# endif
1645# ifdef FEAT_AUTOCMD
1646 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001647 {
1648 /* Make sure curswant is correct, an autocommand may call
1649 * getcurpos(). */
1650 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001651 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001652 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001653# endif
1654# ifdef FEAT_CONCEAL
1655 if (curwin->w_p_cole > 0)
1656 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001657# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001658 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001659# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001660 conceal_new_cursor_line = curwin->w_cursor.lnum;
1661 conceal_update_lines = TRUE;
1662 }
1663# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001664# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001666# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001667 }
1668#endif
1669
1670#ifdef FEAT_AUTOCMD
1671 /* Trigger TextChangedI if b_changedtick differs. */
1672 if (ready && has_textchangedI()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001673 && last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001674# ifdef FEAT_INS_EXPAND
1675 && !pum_visible()
1676# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001677 )
1678 {
1679 if (last_changedtick_buf == curbuf)
1680 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1681 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001682 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001684#endif
1685
1686 if (must_redraw)
1687 update_screen(0);
1688 else if (clear_cmdline || redraw_cmdline)
1689 showmode(); /* clear cmdline and show mode */
1690# if defined(FEAT_CONCEAL)
1691 if ((conceal_update_lines
1692 && (conceal_old_cursor_line != conceal_new_cursor_line
1693 || conceal_cursor_line(curwin)))
1694 || need_cursor_line_redraw)
1695 {
1696 if (conceal_old_cursor_line != conceal_new_cursor_line)
1697 update_single_line(curwin, conceal_old_cursor_line);
1698 update_single_line(curwin, conceal_new_cursor_line == 0
1699 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1700 curwin->w_valid &= ~VALID_CROW;
1701 }
1702# endif
1703 showruler(FALSE);
1704 setcursor();
1705 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706}
1707
1708/*
1709 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1710 */
1711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001712ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001715 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
1717 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001718 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
1720 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001723 did_putchar = TRUE;
1724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1726
1727#ifdef FEAT_CMDL_INFO
1728 add_to_showcmd_c(Ctrl_V);
1729#endif
1730
1731 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001732 if (did_putchar)
1733 /* when the line fits in 'columns' the '^' is at the start of the next
1734 * line and will not removed by the redraw */
1735 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736#ifdef FEAT_CMDL_INFO
1737 clear_showcmd();
1738#endif
1739 insert_special(c, FALSE, TRUE);
1740#ifdef FEAT_RIGHTLEFT
1741 revins_chars++;
1742 revins_legal++;
1743#endif
1744}
1745
1746/*
1747 * Put a character directly onto the screen. It's not stored in a buffer.
1748 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1749 */
1750static int pc_status;
1751#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1752#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1753#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1754#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756static int pc_attr;
1757static int pc_row;
1758static int pc_col;
1759
1760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001761edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
1763 int attr;
1764
1765 if (ScreenLines != NULL)
1766 {
1767 update_topline(); /* just in case w_topline isn't valid */
1768 validate_cursor();
1769 if (highlight)
1770 attr = hl_attr(HLF_8);
1771 else
1772 attr = 0;
1773 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1774 pc_col = W_WINCOL(curwin);
1775#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1776 pc_status = PC_STATUS_UNSET;
1777#endif
1778#ifdef FEAT_RIGHTLEFT
1779 if (curwin->w_p_rl)
1780 {
1781 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1782# ifdef FEAT_MBYTE
1783 if (has_mbyte)
1784 {
1785 int fix_col = mb_fix_col(pc_col, pc_row);
1786
1787 if (fix_col != pc_col)
1788 {
1789 screen_putchar(' ', pc_row, fix_col, attr);
1790 --curwin->w_wcol;
1791 pc_status = PC_STATUS_RIGHT;
1792 }
1793 }
1794# endif
1795 }
1796 else
1797#endif
1798 {
1799 pc_col += curwin->w_wcol;
1800#ifdef FEAT_MBYTE
1801 if (mb_lefthalve(pc_row, pc_col))
1802 pc_status = PC_STATUS_LEFT;
1803#endif
1804 }
1805
1806 /* save the character to be able to put it back */
1807#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1808 if (pc_status == PC_STATUS_UNSET)
1809#endif
1810 {
1811 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1812 pc_status = PC_STATUS_SET;
1813 }
1814 screen_putchar(c, pc_row, pc_col, attr);
1815 }
1816}
1817
1818/*
1819 * Undo the previous edit_putchar().
1820 */
1821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1825 {
1826#if defined(FEAT_MBYTE)
1827 if (pc_status == PC_STATUS_RIGHT)
1828 ++curwin->w_wcol;
1829 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1830 redrawWinline(curwin->w_cursor.lnum, FALSE);
1831 else
1832#endif
1833 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1834 }
1835}
1836
1837/*
1838 * Called when p_dollar is set: display a '$' at the end of the changed text
1839 * Only works when cursor is in the line that changes.
1840 */
1841 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001842display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843{
1844 colnr_T save_col;
1845
1846 if (!redrawing())
1847 return;
1848
1849 cursor_off();
1850 save_col = curwin->w_cursor.col;
1851 curwin->w_cursor.col = col;
1852#ifdef FEAT_MBYTE
1853 if (has_mbyte)
1854 {
1855 char_u *p;
1856
1857 /* If on the last byte of a multi-byte move to the first byte. */
1858 p = ml_get_curline();
1859 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1860 }
1861#endif
1862 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1863 if (curwin->w_wcol < W_WIDTH(curwin))
1864 {
1865 edit_putchar('$', FALSE);
1866 dollar_vcol = curwin->w_virtcol;
1867 }
1868 curwin->w_cursor.col = save_col;
1869}
1870
1871/*
1872 * Call this function before moving the cursor from the normal insert position
1873 * in insert mode.
1874 */
1875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001876undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001878 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001880 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 redrawWinline(curwin->w_cursor.lnum, FALSE);
1882 }
1883}
1884
1885/*
1886 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1887 * Keep the cursor on the same character.
1888 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1889 * type == INDENT_DEC decrease indent (for CTRL-D)
1890 * type == INDENT_SET set indent to "amount"
1891 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1892 */
1893 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001894change_indent(
1895 int type,
1896 int amount,
1897 int round,
1898 int replaced, /* replaced character, put on replace stack */
1899 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900{
1901 int vcol;
1902 int last_vcol;
1903 int insstart_less; /* reduction for Insstart.col */
1904 int new_cursor_col;
1905 int i;
1906 char_u *ptr;
1907 int save_p_list;
1908 int start_col;
1909 colnr_T vc;
1910#ifdef FEAT_VREPLACE
1911 colnr_T orig_col = 0; /* init for GCC */
1912 char_u *new_line, *orig_line = NULL; /* init for GCC */
1913
1914 /* VREPLACE mode needs to know what the line was like before changing */
1915 if (State & VREPLACE_FLAG)
1916 {
1917 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1918 orig_col = curwin->w_cursor.col;
1919 }
1920#endif
1921
1922 /* for the following tricks we don't want list mode */
1923 save_p_list = curwin->w_p_list;
1924 curwin->w_p_list = FALSE;
1925 vc = getvcol_nolist(&curwin->w_cursor);
1926 vcol = vc;
1927
1928 /*
1929 * For Replace mode we need to fix the replace stack later, which is only
1930 * possible when the cursor is in the indent. Remember the number of
1931 * characters before the cursor if it's possible.
1932 */
1933 start_col = curwin->w_cursor.col;
1934
1935 /* determine offset from first non-blank */
1936 new_cursor_col = curwin->w_cursor.col;
1937 beginline(BL_WHITE);
1938 new_cursor_col -= curwin->w_cursor.col;
1939
1940 insstart_less = curwin->w_cursor.col;
1941
1942 /*
1943 * If the cursor is in the indent, compute how many screen columns the
1944 * cursor is to the left of the first non-blank.
1945 */
1946 if (new_cursor_col < 0)
1947 vcol = get_indent() - vcol;
1948
1949 if (new_cursor_col > 0) /* can't fix replace stack */
1950 start_col = -1;
1951
1952 /*
1953 * Set the new indent. The cursor will be put on the first non-blank.
1954 */
1955 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001956 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 else
1958 {
1959#ifdef FEAT_VREPLACE
1960 int save_State = State;
1961
1962 /* Avoid being called recursively. */
1963 if (State & VREPLACE_FLAG)
1964 State = INSERT;
1965#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001966 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967#ifdef FEAT_VREPLACE
1968 State = save_State;
1969#endif
1970 }
1971 insstart_less -= curwin->w_cursor.col;
1972
1973 /*
1974 * Try to put cursor on same character.
1975 * If the cursor is at or after the first non-blank in the line,
1976 * compute the cursor column relative to the column of the first
1977 * non-blank character.
1978 * If we are not in insert mode, leave the cursor on the first non-blank.
1979 * If the cursor is before the first non-blank, position it relative
1980 * to the first non-blank, counted in screen columns.
1981 */
1982 if (new_cursor_col >= 0)
1983 {
1984 /*
1985 * When changing the indent while the cursor is touching it, reset
1986 * Insstart_col to 0.
1987 */
1988 if (new_cursor_col == 0)
1989 insstart_less = MAXCOL;
1990 new_cursor_col += curwin->w_cursor.col;
1991 }
1992 else if (!(State & INSERT))
1993 new_cursor_col = curwin->w_cursor.col;
1994 else
1995 {
1996 /*
1997 * Compute the screen column where the cursor should be.
1998 */
1999 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002000 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
2002 /*
2003 * Advance the cursor until we reach the right screen column.
2004 */
2005 vcol = last_vcol = 0;
2006 new_cursor_col = -1;
2007 ptr = ml_get_curline();
2008 while (vcol <= (int)curwin->w_virtcol)
2009 {
2010 last_vcol = vcol;
2011#ifdef FEAT_MBYTE
2012 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002013 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 else
2015#endif
2016 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002017 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
2019 vcol = last_vcol;
2020
2021 /*
2022 * May need to insert spaces to be able to position the cursor on
2023 * the right screen column.
2024 */
2025 if (vcol != (int)curwin->w_virtcol)
2026 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002027 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002029 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (ptr != NULL)
2031 {
2032 new_cursor_col += i;
2033 ptr[i] = NUL;
2034 while (--i >= 0)
2035 ptr[i] = ' ';
2036 ins_str(ptr);
2037 vim_free(ptr);
2038 }
2039 }
2040
2041 /*
2042 * When changing the indent while the cursor is in it, reset
2043 * Insstart_col to 0.
2044 */
2045 insstart_less = MAXCOL;
2046 }
2047
2048 curwin->w_p_list = save_p_list;
2049
2050 if (new_cursor_col <= 0)
2051 curwin->w_cursor.col = 0;
2052 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002053 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 curwin->w_set_curswant = TRUE;
2055 changed_cline_bef_curs();
2056
2057 /*
2058 * May have to adjust the start of the insert.
2059 */
2060 if (State & INSERT)
2061 {
2062 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2063 {
2064 if ((int)Insstart.col <= insstart_less)
2065 Insstart.col = 0;
2066 else
2067 Insstart.col -= insstart_less;
2068 }
2069 if ((int)ai_col <= insstart_less)
2070 ai_col = 0;
2071 else
2072 ai_col -= insstart_less;
2073 }
2074
2075 /*
2076 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2077 * If the number of characters before the cursor decreased, need to pop a
2078 * few characters from the replace stack.
2079 * If the number of characters before the cursor increased, need to push a
2080 * few NULs onto the replace stack.
2081 */
2082 if (REPLACE_NORMAL(State) && start_col >= 0)
2083 {
2084 while (start_col > (int)curwin->w_cursor.col)
2085 {
2086 replace_join(0); /* remove a NUL from the replace stack */
2087 --start_col;
2088 }
2089 while (start_col < (int)curwin->w_cursor.col || replaced)
2090 {
2091 replace_push(NUL);
2092 if (replaced)
2093 {
2094 replace_push(replaced);
2095 replaced = NUL;
2096 }
2097 ++start_col;
2098 }
2099 }
2100
2101#ifdef FEAT_VREPLACE
2102 /*
2103 * For VREPLACE mode, we also have to fix the replace stack. In this case
2104 * it is always possible because we backspace over the whole line and then
2105 * put it back again the way we wanted it.
2106 */
2107 if (State & VREPLACE_FLAG)
2108 {
2109 /* If orig_line didn't allocate, just return. At least we did the job,
2110 * even if you can't backspace. */
2111 if (orig_line == NULL)
2112 return;
2113
2114 /* Save new line */
2115 new_line = vim_strsave(ml_get_curline());
2116 if (new_line == NULL)
2117 return;
2118
2119 /* We only put back the new line up to the cursor */
2120 new_line[curwin->w_cursor.col] = NUL;
2121
2122 /* Put back original line */
2123 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2124 curwin->w_cursor.col = orig_col;
2125
2126 /* Backspace from cursor to start of line */
2127 backspace_until_column(0);
2128
2129 /* Insert new stuff into line again */
2130 ins_bytes(new_line);
2131
2132 vim_free(new_line);
2133 }
2134#endif
2135}
2136
2137/*
2138 * Truncate the space at the end of a line. This is to be used only in an
2139 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2140 * modes.
2141 */
2142 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002143truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 int i;
2146
2147 /* find start of trailing white space */
2148 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2149 {
2150 if (State & REPLACE_FLAG)
2151 replace_join(0); /* remove a NUL from the replace stack */
2152 }
2153 line[i + 1] = NUL;
2154}
2155
2156#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2157 || defined(FEAT_COMMENTS) || defined(PROTO)
2158/*
2159 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2160 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002161 * Will attempt not to go before "col" even when there is a composing
2162 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 */
2164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002165backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 while ((int)curwin->w_cursor.col > col)
2168 {
2169 curwin->w_cursor.col--;
2170 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002171 replace_do_bs(col);
2172 else if (!del_char_after_col(col))
2173 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175}
2176#endif
2177
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002178/*
2179 * Like del_char(), but make sure not to go before column "limit_col".
2180 * Only matters when there are composing characters.
2181 * Return TRUE when something was deleted.
2182 */
2183 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002184del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002185{
2186#ifdef FEAT_MBYTE
2187 if (enc_utf8 && limit_col >= 0)
2188 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002189 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002190
2191 /* Make sure the cursor is at the start of a character, but
2192 * skip forward again when going too far back because of a
2193 * composing character. */
2194 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002195 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002196 {
2197 int l = utf_ptr2len(ml_get_cursor());
2198
2199 if (l == 0) /* end of line */
2200 break;
2201 curwin->w_cursor.col += l;
2202 }
2203 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2204 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002205 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002206 }
2207 else
2208#endif
2209 (void)del_char(FALSE);
2210 return TRUE;
2211}
2212
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2214/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002215 * CTRL-X pressed in Insert mode.
2216 */
2217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002218ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002219{
2220 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2221 * CTRL-V works like CTRL-N */
2222 if (ctrl_x_mode != CTRL_X_CMDLINE)
2223 {
2224 /* if the next ^X<> won't ADD nothing, then reset
2225 * compl_cont_status */
2226 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002227 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002228 else
2229 compl_cont_status = 0;
2230 /* We're not sure which CTRL-X mode it will be yet */
2231 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2232 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2233 edit_submode_pre = NULL;
2234 showmode();
2235 }
2236}
2237
2238/*
2239 * Return TRUE if the 'dict' or 'tsr' option can be used.
2240 */
2241 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002242has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002243{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002244 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002245# ifdef FEAT_SPELL
2246 && !curwin->w_p_spell
2247# endif
2248 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002249 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2250 {
2251 ctrl_x_mode = 0;
2252 edit_submode = NULL;
2253 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2254 : (char_u *)_("'thesaurus' option is empty"),
2255 hl_attr(HLF_E));
2256 if (emsg_silent == 0)
2257 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002258 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002259 setcursor();
2260 out_flush();
2261 ui_delay(2000L, FALSE);
2262 }
2263 return FALSE;
2264 }
2265 return TRUE;
2266}
2267
2268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2270 * This depends on the current mode.
2271 */
2272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002273vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 /* Always allow ^R - let it's results then be checked */
2276 if (c == Ctrl_R)
2277 return TRUE;
2278
Bram Moolenaare3226be2005-12-18 22:10:00 +00002279 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002280 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002281 return TRUE;
2282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 switch (ctrl_x_mode)
2284 {
2285 case 0: /* Not in any CTRL-X mode */
2286 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2287 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002288 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2290 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2291 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002292 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002293 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 case CTRL_X_SCROLL:
2295 return (c == Ctrl_Y || c == Ctrl_E);
2296 case CTRL_X_WHOLE_LINE:
2297 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2298 case CTRL_X_FILES:
2299 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2300 case CTRL_X_DICTIONARY:
2301 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2302 case CTRL_X_THESAURUS:
2303 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2304 case CTRL_X_TAGS:
2305 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2306#ifdef FEAT_FIND_ID
2307 case CTRL_X_PATH_PATTERNS:
2308 return (c == Ctrl_P || c == Ctrl_N);
2309 case CTRL_X_PATH_DEFINES:
2310 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2311#endif
2312 case CTRL_X_CMDLINE:
2313 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2314 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002315#ifdef FEAT_COMPL_FUNC
2316 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002317 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002318 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002319 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002320#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002321 case CTRL_X_SPELL:
2322 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002323 case CTRL_X_EVAL:
2324 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002326 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 return FALSE;
2328}
2329
2330/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002331 * Return TRUE when character "c" is part of the item currently being
2332 * completed. Used to decide whether to abandon complete mode when the menu
2333 * is visible.
2334 */
2335 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002336ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002337{
2338 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2339 /* When expanding an identifier only accept identifier chars. */
2340 return vim_isIDc(c);
2341
2342 switch (ctrl_x_mode)
2343 {
2344 case CTRL_X_FILES:
2345 /* When expanding file name only accept file name chars. But not
2346 * path separators, so that "proto/<Tab>" expands files in
2347 * "proto", not "proto/" as a whole */
2348 return vim_isfilec(c) && !vim_ispathsep(c);
2349
2350 case CTRL_X_CMDLINE:
2351 case CTRL_X_OMNI:
2352 /* Command line and Omni completion can work with just about any
2353 * printable character, but do stop at white space. */
2354 return vim_isprintc(c) && !vim_iswhite(c);
2355
2356 case CTRL_X_WHOLE_LINE:
2357 /* For while line completion a space can be part of the line. */
2358 return vim_isprintc(c);
2359 }
2360 return vim_iswordc(c);
2361}
2362
2363/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002364 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002366 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 * the rest of the word to be in -- webb
2368 */
2369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002370ins_compl_add_infercase(
2371 char_u *str,
2372 int len,
2373 int icase,
2374 char_u *fname,
2375 int dir,
2376 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002378 char_u *p;
2379 int i, c;
2380 int actual_len; /* Take multi-byte characters */
2381 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002382 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002383 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 int has_lower = FALSE;
2385 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002387 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002389 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
Bram Moolenaara2993e12007-08-12 14:38:46 +00002391 /* Find actual length of completion. */
2392#ifdef FEAT_MBYTE
2393 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002395 p = str;
2396 actual_len = 0;
2397 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002399 mb_ptr_adv(p);
2400 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
2402 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002403 else
2404#endif
2405 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
Bram Moolenaara2993e12007-08-12 14:38:46 +00002407 /* Find actual length of original text. */
2408#ifdef FEAT_MBYTE
2409 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002411 p = compl_orig_text;
2412 actual_compl_length = 0;
2413 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002415 mb_ptr_adv(p);
2416 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002419 else
2420#endif
2421 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002423 /* "actual_len" may be smaller than "actual_compl_length" when using
2424 * thesaurus, only use the minimum when comparing. */
2425 min_len = actual_len < actual_compl_length
2426 ? actual_len : actual_compl_length;
2427
Bram Moolenaara2993e12007-08-12 14:38:46 +00002428 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002429 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002430 if (wca != NULL)
2431 {
2432 p = str;
2433 for (i = 0; i < actual_len; ++i)
2434#ifdef FEAT_MBYTE
2435 if (has_mbyte)
2436 wca[i] = mb_ptr2char_adv(&p);
2437 else
2438#endif
2439 wca[i] = *(p++);
2440
2441 /* Rule 1: Were any chars converted to lower? */
2442 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002443 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002444 {
2445#ifdef FEAT_MBYTE
2446 if (has_mbyte)
2447 c = mb_ptr2char_adv(&p);
2448 else
2449#endif
2450 c = *(p++);
2451 if (MB_ISLOWER(c))
2452 {
2453 has_lower = TRUE;
2454 if (MB_ISUPPER(wca[i]))
2455 {
2456 /* Rule 1 is satisfied. */
2457 for (i = actual_compl_length; i < actual_len; ++i)
2458 wca[i] = MB_TOLOWER(wca[i]);
2459 break;
2460 }
2461 }
2462 }
2463
2464 /*
2465 * Rule 2: No lower case, 2nd consecutive letter converted to
2466 * upper case.
2467 */
2468 if (!has_lower)
2469 {
2470 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002471 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002472 {
2473#ifdef FEAT_MBYTE
2474 if (has_mbyte)
2475 c = mb_ptr2char_adv(&p);
2476 else
2477#endif
2478 c = *(p++);
2479 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2480 {
2481 /* Rule 2 is satisfied. */
2482 for (i = actual_compl_length; i < actual_len; ++i)
2483 wca[i] = MB_TOUPPER(wca[i]);
2484 break;
2485 }
2486 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2487 }
2488 }
2489
2490 /* Copy the original case of the part we typed. */
2491 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002492 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002493 {
2494#ifdef FEAT_MBYTE
2495 if (has_mbyte)
2496 c = mb_ptr2char_adv(&p);
2497 else
2498#endif
2499 c = *(p++);
2500 if (MB_ISLOWER(c))
2501 wca[i] = MB_TOLOWER(wca[i]);
2502 else if (MB_ISUPPER(c))
2503 wca[i] = MB_TOUPPER(wca[i]);
2504 }
2505
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002506 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002507 * Generate encoding specific output from wide character array.
2508 * Multi-byte characters can occupy up to five bytes more than
2509 * ASCII characters, and we also need one byte for NUL, so stay
2510 * six bytes away from the edge of IObuff.
2511 */
2512 p = IObuff;
2513 i = 0;
2514 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2515#ifdef FEAT_MBYTE
2516 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002517 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002518 else
2519#endif
2520 *(p++) = wca[i++];
2521 *p = NUL;
2522
2523 vim_free(wca);
2524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002526 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2527 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002529 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530}
2531
2532/*
2533 * Add a match to the list of matches.
2534 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002535 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002536 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002539ins_compl_add(
2540 char_u *str,
2541 int len,
2542 int icase,
2543 char_u *fname,
2544 char_u **cptext, /* extra text for popup menu or NULL */
2545 int cdir,
2546 int flags,
2547 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002549 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002550 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552 ui_breakcheck();
2553 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002554 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 if (len < 0)
2556 len = (int)STRLEN(str);
2557
2558 /*
2559 * If the same match is already present, don't add it.
2560 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002561 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002563 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 do
2565 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002566 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002567 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002568 && match->cp_str[len] == NUL)
2569 return NOTDONE;
2570 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002571 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002574 /* Remove any popup menu before changing the list of matches. */
2575 ins_compl_del_pum();
2576
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 /*
2578 * Allocate a new match structure.
2579 * Copy the values to the new match structure.
2580 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002581 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002583 return FAIL;
2584 match->cp_number = -1;
2585 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002586 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002587 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
2589 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002590 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002592 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002593
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002595 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2597 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002598 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002599 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002600 && compl_curr_match->cp_fname != NULL
2601 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002602 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002603 else if (fname != NULL)
2604 {
2605 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002606 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002609 match->cp_fname = NULL;
2610 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002611
2612 if (cptext != NULL)
2613 {
2614 int i;
2615
2616 for (i = 0; i < CPT_COUNT; ++i)
2617 if (cptext[i] != NULL && *cptext[i] != NUL)
2618 match->cp_text[i] = vim_strsave(cptext[i]);
2619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621 /*
2622 * Link the new match structure in the list of matches.
2623 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002624 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002625 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 else if (dir == FORWARD)
2627 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002628 match->cp_next = compl_curr_match->cp_next;
2629 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631 else /* BACKWARD */
2632 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002633 match->cp_next = compl_curr_match;
2634 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002636 if (match->cp_next)
2637 match->cp_next->cp_prev = match;
2638 if (match->cp_prev)
2639 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002641 compl_first_match = match;
2642 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002644 /*
2645 * Find the longest common string if still doing that.
2646 */
2647 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2648 ins_compl_longest_match(match);
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 return OK;
2651}
2652
2653/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002654 * Return TRUE if "str[len]" matches with match->cp_str, considering
2655 * match->cp_icase.
2656 */
2657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002658ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002659{
2660 if (match->cp_icase)
2661 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2662 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2663}
2664
2665/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002666 * Reduce the longest common string for match "match".
2667 */
2668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002669ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002670{
2671 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002672 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002673 int had_match;
2674
2675 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002676 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002677 /* First match, use it as a whole. */
2678 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002679 if (compl_leader != NULL)
2680 {
2681 had_match = (curwin->w_cursor.col > compl_col);
2682 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002683 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002684 ins_redraw(FALSE);
2685
2686 /* When the match isn't there (to avoid matching itself) remove it
2687 * again after redrawing. */
2688 if (!had_match)
2689 ins_compl_delete();
2690 compl_used_match = FALSE;
2691 }
2692 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002693 else
2694 {
2695 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002696 p = compl_leader;
2697 s = match->cp_str;
2698 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002699 {
2700#ifdef FEAT_MBYTE
2701 if (has_mbyte)
2702 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002703 c1 = mb_ptr2char(p);
2704 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002705 }
2706 else
2707#endif
2708 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002709 c1 = *p;
2710 c2 = *s;
2711 }
2712 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2713 : (c1 != c2))
2714 break;
2715#ifdef FEAT_MBYTE
2716 if (has_mbyte)
2717 {
2718 mb_ptr_adv(p);
2719 mb_ptr_adv(s);
2720 }
2721 else
2722#endif
2723 {
2724 ++p;
2725 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002726 }
2727 }
2728
2729 if (*p != NUL)
2730 {
2731 /* Leader was shortened, need to change the inserted text. */
2732 *p = NUL;
2733 had_match = (curwin->w_cursor.col > compl_col);
2734 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002735 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002736 ins_redraw(FALSE);
2737
2738 /* When the match isn't there (to avoid matching itself) remove it
2739 * again after redrawing. */
2740 if (!had_match)
2741 ins_compl_delete();
2742 }
2743
2744 compl_used_match = FALSE;
2745 }
2746}
2747
2748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 * Add an array of matches to the list of matches.
2750 * Frees matches[].
2751 */
2752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002753ins_compl_add_matches(
2754 int num_matches,
2755 char_u **matches,
2756 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
2758 int i;
2759 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002760 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
Bram Moolenaar572cb562005-08-05 21:35:02 +00002762 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002763 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002764 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002766 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 FreeWild(num_matches, matches);
2768}
2769
2770/* Make the completion list cyclic.
2771 * Return the number of matches (excluding the original).
2772 */
2773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002774ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002776 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 int count = 0;
2778
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002779 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
2781 /*
2782 * Find the end of the list.
2783 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002784 match = compl_first_match;
2785 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002786 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002788 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 ++count;
2790 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002791 match->cp_next = compl_first_match;
2792 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794 return count;
2795}
2796
Bram Moolenaara94bc432006-03-10 21:42:59 +00002797/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002798 * Set variables that store noselect and noinsert behavior from the
2799 * 'completeopt' value.
2800 */
2801 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002802completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002803{
2804 compl_no_insert = FALSE;
2805 compl_no_select = FALSE;
2806 if (strstr((char *)p_cot, "noselect") != NULL)
2807 compl_no_select = TRUE;
2808 if (strstr((char *)p_cot, "noinsert") != NULL)
2809 compl_no_insert = TRUE;
2810}
2811
2812/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002813 * Start completion for the complete() function.
2814 * "startcol" is where the matched text starts (1 is first column).
2815 * "list" is the list of matches.
2816 */
2817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002818set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002819{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002820 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002821 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002822
Bram Moolenaara94bc432006-03-10 21:42:59 +00002823 /* If already doing completions stop it. */
2824 if (ctrl_x_mode != 0)
2825 ins_compl_prep(' ');
2826 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002827 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002828
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002829 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002830 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002831 startcol = curwin->w_cursor.col;
2832 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002833 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002834 /* compl_pattern doesn't need to be set */
2835 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2836 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002837 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002838 return;
2839
Bram Moolenaare4214502015-03-05 18:08:43 +01002840 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002841
2842 ins_compl_add_list(list);
2843 compl_matches = ins_compl_make_cyclic();
2844 compl_started = TRUE;
2845 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002846 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002847
2848 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002849 if (compl_no_insert || compl_no_select)
2850 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002851 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002852 if (compl_no_select)
2853 /* Down/Up has no real effect. */
2854 ins_complete(K_UP, FALSE);
2855 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002856 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002857 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002858 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002859
2860 /* Lazily show the popup menu, unless we got interrupted. */
2861 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002862 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002863 out_flush();
2864}
2865
2866
Bram Moolenaar9372a112005-12-06 19:59:18 +00002867/* "compl_match_array" points the currently displayed list of entries in the
2868 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002869static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002870static int compl_match_arraysize;
2871
2872/*
2873 * Update the screen and when there is any scrolling remove the popup menu.
2874 */
2875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002876ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002877{
2878 int h;
2879
2880 if (compl_match_array != NULL)
2881 {
2882 h = curwin->w_cline_height;
2883 update_screen(0);
2884 if (h != curwin->w_cline_height)
2885 ins_compl_del_pum();
2886 }
2887}
2888
2889/*
2890 * Remove any popup menu.
2891 */
2892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002893ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002894{
2895 if (compl_match_array != NULL)
2896 {
2897 pum_undisplay();
2898 vim_free(compl_match_array);
2899 compl_match_array = NULL;
2900 }
2901}
2902
2903/*
2904 * Return TRUE if the popup menu should be displayed.
2905 */
2906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002907pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002908{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002909 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002910 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002911 return FALSE;
2912
2913 /* The display looks bad on a B&W display. */
2914 if (t_colors < 8
2915#ifdef FEAT_GUI
2916 && !gui.in_use
2917#endif
2918 )
2919 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002920 return TRUE;
2921}
2922
2923/*
2924 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002925 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002926 */
2927 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002928pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002929{
2930 compl_T *compl;
2931 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002932
2933 /* Don't display the popup menu if there are no matches or there is only
2934 * one (ignoring the original text). */
2935 compl = compl_first_match;
2936 i = 0;
2937 do
2938 {
2939 if (compl == NULL
2940 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2941 break;
2942 compl = compl->cp_next;
2943 } while (compl != compl_first_match);
2944
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002945 if (strstr((char *)p_cot, "menuone") != NULL)
2946 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002947 return (i >= 2);
2948}
2949
2950/*
2951 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002952 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002953 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002954 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002955ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002956{
2957 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002958 compl_T *shown_compl = NULL;
2959 int did_find_shown_match = FALSE;
2960 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002961 int i;
2962 int cur = -1;
2963 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002964 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002965
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002966 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002967 return;
2968
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002969#if defined(FEAT_EVAL)
2970 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2971 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2972#endif
2973
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002974 /* Update the screen before drawing the popup menu over it. */
2975 update_screen(0);
2976
2977 if (compl_match_array == NULL)
2978 {
2979 /* Need to build the popup menu list. */
2980 compl_match_arraysize = 0;
2981 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002982 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002983 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002984 do
2985 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002986 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2987 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002988 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002989 ++compl_match_arraysize;
2990 compl = compl->cp_next;
2991 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002992 if (compl_match_arraysize == 0)
2993 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002994 compl_match_array = (pumitem_T *)alloc_clear(
2995 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002996 * compl_match_arraysize));
2997 if (compl_match_array != NULL)
2998 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002999 /* If the current match is the original text don't find the first
3000 * match after it, don't highlight anything. */
3001 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3002 shown_match_ok = TRUE;
3003
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003004 i = 0;
3005 compl = compl_first_match;
3006 do
3007 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003008 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3009 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003010 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003011 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003012 if (!shown_match_ok)
3013 {
3014 if (compl == compl_shown_match || did_find_shown_match)
3015 {
3016 /* This item is the shown match or this is the
3017 * first displayed item after the shown match. */
3018 compl_shown_match = compl;
3019 did_find_shown_match = TRUE;
3020 shown_match_ok = TRUE;
3021 }
3022 else
3023 /* Remember this displayed match for when the
3024 * shown match is just below it. */
3025 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003026 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003027 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003028
3029 if (compl->cp_text[CPT_ABBR] != NULL)
3030 compl_match_array[i].pum_text =
3031 compl->cp_text[CPT_ABBR];
3032 else
3033 compl_match_array[i].pum_text = compl->cp_str;
3034 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3035 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3036 if (compl->cp_text[CPT_MENU] != NULL)
3037 compl_match_array[i++].pum_extra =
3038 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003039 else
3040 compl_match_array[i++].pum_extra = compl->cp_fname;
3041 }
3042
3043 if (compl == compl_shown_match)
3044 {
3045 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003046
3047 /* When the original text is the shown match don't set
3048 * compl_shown_match. */
3049 if (compl->cp_flags & ORIGINAL_TEXT)
3050 shown_match_ok = TRUE;
3051
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003052 if (!shown_match_ok && shown_compl != NULL)
3053 {
3054 /* The shown match isn't displayed, set it to the
3055 * previously displayed match. */
3056 compl_shown_match = shown_compl;
3057 shown_match_ok = TRUE;
3058 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003059 }
3060 compl = compl->cp_next;
3061 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003062
3063 if (!shown_match_ok) /* no displayed match at all */
3064 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003065 }
3066 }
3067 else
3068 {
3069 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003070 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003071 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3072 || compl_match_array[i].pum_text
3073 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003074 {
3075 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003076 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003077 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003078 }
3079
3080 if (compl_match_array != NULL)
3081 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003082 /* In Replace mode when a $ is displayed at the end of the line only
3083 * part of the screen would be updated. We do need to redraw here. */
3084 dollar_vcol = -1;
3085
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003086 /* Compute the screen column of the start of the completed text.
3087 * Use the cursor to get all wrapping and other settings right. */
3088 col = curwin->w_cursor.col;
3089 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003090 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003091 curwin->w_cursor.col = col;
3092 }
3093}
3094
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#define DICT_FIRST (1) /* use just first element in "dict" */
3096#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003097
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003099 * Add any identifiers that match the given pattern in the list of dictionary
3100 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 */
3102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003103ins_compl_dictionaries(
3104 char_u *dict_start,
3105 char_u *pat,
3106 int flags, /* DICT_FIRST and/or DICT_EXACT */
3107 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003109 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 char_u *ptr;
3111 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 char_u **files;
3114 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003116 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
Bram Moolenaar0b238792006-03-02 22:49:12 +00003118 if (*dict == NUL)
3119 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003120#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003121 /* When 'dictionary' is empty and spell checking is enabled use
3122 * "spell". */
3123 if (!thesaurus && curwin->w_p_spell)
3124 dict = (char_u *)"spell";
3125 else
3126#endif
3127 return;
3128 }
3129
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003131 if (buf == NULL)
3132 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003133 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 /* If 'infercase' is set, don't use 'smartcase' here */
3136 save_p_scs = p_scs;
3137 if (curbuf->b_p_inf)
3138 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003139
3140 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3141 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003142 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003143 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003144 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003145 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003146 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003147
3148 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003149 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003150 len = STRLEN(pat_esc) + 10;
3151 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003152 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003153 {
3154 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003155 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003156 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003157 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003158 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3159 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003160 vim_free(ptr);
3161 }
3162 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003163 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003164 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003165 if (regmatch.regprog == NULL)
3166 goto theend;
3167 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3170 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003171 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
3173 /* copy one dictionary file name into buf */
3174 if (flags == DICT_EXACT)
3175 {
3176 count = 1;
3177 files = &dict;
3178 }
3179 else
3180 {
3181 /* Expand wildcards in the dictionary name, but do not allow
3182 * backticks (for security, the 'dict' option may have been set in
3183 * a modeline). */
3184 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003185# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003186 if (!thesaurus && STRCMP(buf, "spell") == 0)
3187 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003188 else
3189# endif
3190 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 || expand_wildcards(1, &buf, &count, &files,
3192 EW_FILE|EW_SILENT) != OK)
3193 count = 0;
3194 }
3195
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003196# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003197 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003199 /* Complete from active spelling. Skip "\<" in the pattern, we
3200 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003201 if (pat[0] == '\\' && pat[1] == '<')
3202 ptr = pat + 2;
3203 else
3204 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003205 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003207 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003208# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003209 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003210 {
3211 ins_compl_files(count, files, thesaurus, flags,
3212 &regmatch, buf, &dir);
3213 if (flags != DICT_EXACT)
3214 FreeWild(count, files);
3215 }
3216 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 break;
3218 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003219
3220theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003222 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 vim_free(buf);
3224}
3225
Bram Moolenaar0b238792006-03-02 22:49:12 +00003226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003227ins_compl_files(
3228 int count,
3229 char_u **files,
3230 int thesaurus,
3231 int flags,
3232 regmatch_T *regmatch,
3233 char_u *buf,
3234 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003235{
3236 char_u *ptr;
3237 int i;
3238 FILE *fp;
3239 int add_r;
3240
3241 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3242 {
3243 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3244 if (flags != DICT_EXACT)
3245 {
3246 vim_snprintf((char *)IObuff, IOSIZE,
3247 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003248 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003249 }
3250
3251 if (fp != NULL)
3252 {
3253 /*
3254 * Read dictionary file line by line.
3255 * Check each line for a match.
3256 */
3257 while (!got_int && !compl_interrupted
3258 && !vim_fgets(buf, LSIZE, fp))
3259 {
3260 ptr = buf;
3261 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3262 {
3263 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003264 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003265 ptr = find_line_end(ptr);
3266 else
3267 ptr = find_word_end(ptr);
3268 add_r = ins_compl_add_infercase(regmatch->startp[0],
3269 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003270 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003271 if (thesaurus)
3272 {
3273 char_u *wstart;
3274
3275 /*
3276 * Add the other matches on the line
3277 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003278 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003279 while (!got_int)
3280 {
3281 /* Find start of the next word. Skip white
3282 * space and punctuation. */
3283 ptr = find_word_start(ptr);
3284 if (*ptr == NUL || *ptr == NL)
3285 break;
3286 wstart = ptr;
3287
Bram Moolenaara2993e12007-08-12 14:38:46 +00003288 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003289#ifdef FEAT_MBYTE
3290 if (has_mbyte)
3291 /* Japanese words may have characters in
3292 * different classes, only separate words
3293 * with single-byte non-word characters. */
3294 while (*ptr != NUL)
3295 {
3296 int l = (*mb_ptr2len)(ptr);
3297
3298 if (l < 2 && !vim_iswordc(*ptr))
3299 break;
3300 ptr += l;
3301 }
3302 else
3303#endif
3304 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003305
3306 /* Add the word. Skip the regexp match. */
3307 if (wstart != regmatch->startp[0])
3308 add_r = ins_compl_add_infercase(wstart,
3309 (int)(ptr - wstart),
3310 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003311 }
3312 }
3313 if (add_r == OK)
3314 /* if dir was BACKWARD then honor it just once */
3315 *dir = FORWARD;
3316 else if (add_r == FAIL)
3317 break;
3318 /* avoid expensive call to vim_regexec() when at end
3319 * of line */
3320 if (*ptr == '\n' || got_int)
3321 break;
3322 }
3323 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003324 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003325 }
3326 fclose(fp);
3327 }
3328 }
3329}
3330
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331/*
3332 * Find the start of the next word.
3333 * Returns a pointer to the first char of the word. Also stops at a NUL.
3334 */
3335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
3338#ifdef FEAT_MBYTE
3339 if (has_mbyte)
3340 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003341 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 else
3343#endif
3344 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3345 ++ptr;
3346 return ptr;
3347}
3348
3349/*
3350 * Find the end of the word. Assumes it starts inside a word.
3351 * Returns a pointer to just after the word.
3352 */
3353 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003354find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
3356#ifdef FEAT_MBYTE
3357 int start_class;
3358
3359 if (has_mbyte)
3360 {
3361 start_class = mb_get_class(ptr);
3362 if (start_class > 1)
3363 while (*ptr != NUL)
3364 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003365 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 if (mb_get_class(ptr) != start_class)
3367 break;
3368 }
3369 }
3370 else
3371#endif
3372 while (vim_iswordc(*ptr))
3373 ++ptr;
3374 return ptr;
3375}
3376
3377/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003378 * Find the end of the line, omitting CR and NL at the end.
3379 * Returns a pointer to just after the line.
3380 */
3381 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003382find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003383{
3384 char_u *s;
3385
3386 s = ptr + STRLEN(ptr);
3387 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3388 --s;
3389 return s;
3390}
3391
3392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * Free the list of completions
3394 */
3395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003396ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003398 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003399 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003401 vim_free(compl_pattern);
3402 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003403 vim_free(compl_leader);
3404 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003406 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003408
3409 ins_compl_del_pum();
3410 pum_clear();
3411
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003412 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 do
3414 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003415 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003416 compl_curr_match = compl_curr_match->cp_next;
3417 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003419 if (match->cp_flags & FREE_FNAME)
3420 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003421 for (i = 0; i < CPT_COUNT; ++i)
3422 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003424 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3425 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003426 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427}
3428
3429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003430ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003432 compl_cont_status = 0;
3433 compl_started = FALSE;
3434 compl_matches = 0;
3435 vim_free(compl_pattern);
3436 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003437 vim_free(compl_leader);
3438 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003440 vim_free(compl_orig_text);
3441 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003442 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003443 /* clear v:completed_item */
3444 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445}
3446
3447/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003448 * Return TRUE when Insert completion is active.
3449 */
3450 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003451ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003452{
3453 return compl_started;
3454}
3455
3456/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003457 * Delete one character before the cursor and show the subset of the matches
3458 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003459 * Returns the character to be used, NUL if the work is done and another char
3460 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003461 */
3462 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003463ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003464{
3465 char_u *line;
3466 char_u *p;
3467
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003468 line = ml_get_curline();
3469 p = line + curwin->w_cursor.col;
3470 mb_ptr_back(line, p);
3471
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003472 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003473 * allow the word to be deleted, we won't match everything.
3474 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003475 if ((int)(p - line) - (int)compl_col < 0
3476 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003477 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3478 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3479 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003480 return K_BS;
3481
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003482 /* Deleted more than what was used to find matches or didn't finish
3483 * finding all matches: need to look for matches all over again. */
3484 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003485 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003486 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003487
Bram Moolenaara6557602006-02-04 22:43:20 +00003488 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003489 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003490 if (compl_leader != NULL)
3491 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003492 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003493 if (compl_shown_match != NULL)
3494 /* Make sure current match is not a hidden item. */
3495 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003496 return NUL;
3497 }
3498 return K_BS;
3499}
Bram Moolenaara6557602006-02-04 22:43:20 +00003500
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003501/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003502 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3503 * be called.
3504 */
3505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003506ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003507{
3508 /* Return TRUE if we didn't complete finding matches or when the
3509 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3510 return compl_was_interrupted
3511 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3512 && compl_opt_refresh_always);
3513}
3514
3515/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003516 * Called after changing "compl_leader".
3517 * Show the popup menu with a different set of matches.
3518 * May also search for matches again if the previous search was interrupted.
3519 */
3520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003521ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003522{
3523 ins_compl_del_pum();
3524 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003525 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003526 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003527
3528 if (compl_started)
3529 ins_compl_set_original_text(compl_leader);
3530 else
3531 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003532#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003533 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003534#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003535 /*
3536 * Matches were cleared, need to search for them now. First display
3537 * the changed text before the cursor. Set "compl_restarting" to
3538 * avoid that the first match is inserted.
3539 */
3540 update_screen(0);
3541#ifdef FEAT_GUI
3542 if (gui.in_use)
3543 {
3544 /* Show the cursor after the match, not after the redrawn text. */
3545 setcursor();
3546 out_flush();
3547 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003548 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003549#endif
3550 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003551 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003552 compl_cont_status = 0;
3553 compl_restarting = FALSE;
3554 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003555
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003556 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003557
3558 /* Show the popup menu with a different set of matches. */
3559 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003560
3561 /* Don't let Enter select the original text when there is no popup menu. */
3562 if (compl_match_array == NULL)
3563 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003564}
3565
3566/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003567 * Return the length of the completion, from the completion start column to
3568 * the cursor column. Making sure it never goes below zero.
3569 */
3570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003571ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003572{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003573 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003574
3575 if (off < 0)
3576 return 0;
3577 return off;
3578}
3579
3580/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003581 * Append one character to the match leader. May reduce the number of
3582 * matches.
3583 */
3584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003585ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003586{
3587#ifdef FEAT_MBYTE
3588 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003589#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003590
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003591 if (stop_arrow() == FAIL)
3592 return;
3593#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003594 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3595 {
3596 char_u buf[MB_MAXBYTES + 1];
3597
3598 (*mb_char2bytes)(c, buf);
3599 buf[cc] = NUL;
3600 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003601 if (compl_opt_refresh_always)
3602 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003603 }
3604 else
3605#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003606 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003607 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003608 if (compl_opt_refresh_always)
3609 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003610 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003611
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003612 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003613 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003614 ins_compl_restart();
3615
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003616 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003617 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003618 * break redo. */
3619 if (!compl_opt_refresh_always)
3620 {
3621 vim_free(compl_leader);
3622 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003623 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003624 if (compl_leader != NULL)
3625 ins_compl_new_leader();
3626 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003627}
3628
3629/*
3630 * Setup for finding completions again without leaving CTRL-X mode. Used when
3631 * BS or a key was typed while still searching for matches.
3632 */
3633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003634ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003635{
3636 ins_compl_free();
3637 compl_started = FALSE;
3638 compl_matches = 0;
3639 compl_cont_status = 0;
3640 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003641}
3642
3643/*
3644 * Set the first match, the original text.
3645 */
3646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003647ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003648{
3649 char_u *p;
3650
3651 /* Replace the original text entry. */
3652 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3653 {
3654 p = vim_strsave(str);
3655 if (p != NULL)
3656 {
3657 vim_free(compl_first_match->cp_str);
3658 compl_first_match->cp_str = p;
3659 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003660 }
3661}
3662
3663/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003664 * Append one character to the match leader. May reduce the number of
3665 * matches.
3666 */
3667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003668ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003669{
3670 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003671 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003672 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003673 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003674
3675 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003676 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003677 {
3678 /* When still at the original match use the first entry that matches
3679 * the leader. */
3680 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3681 {
3682 p = NULL;
3683 for (cp = compl_shown_match->cp_next; cp != NULL
3684 && cp != compl_first_match; cp = cp->cp_next)
3685 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003686 if (compl_leader == NULL
3687 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003688 (int)STRLEN(compl_leader)))
3689 {
3690 p = cp->cp_str;
3691 break;
3692 }
3693 }
3694 if (p == NULL || (int)STRLEN(p) <= len)
3695 return;
3696 }
3697 else
3698 return;
3699 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003700 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003701 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003702 ins_compl_addleader(c);
3703}
3704
3705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003707 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003708 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003711ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712{
3713 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003715 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
3717 /* Forget any previous 'special' messages if this is actually
3718 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3719 */
3720 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3721 edit_submode_extra = NULL;
3722
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003723 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003724 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3725 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003726 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003728 /* Set "compl_get_longest" when finding the first matches. */
3729 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3730 || (ctrl_x_mode == 0 && !compl_started))
3731 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003732 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003733 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003734
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003735 }
3736
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3738 {
3739 /*
3740 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3741 * it will be yet. Now we decide.
3742 */
3743 switch (c)
3744 {
3745 case Ctrl_E:
3746 case Ctrl_Y:
3747 ctrl_x_mode = CTRL_X_SCROLL;
3748 if (!(State & REPLACE_FLAG))
3749 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3750 else
3751 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3752 edit_submode_pre = NULL;
3753 showmode();
3754 break;
3755 case Ctrl_L:
3756 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3757 break;
3758 case Ctrl_F:
3759 ctrl_x_mode = CTRL_X_FILES;
3760 break;
3761 case Ctrl_K:
3762 ctrl_x_mode = CTRL_X_DICTIONARY;
3763 break;
3764 case Ctrl_R:
3765 /* Simply allow ^R to happen without affecting ^X mode */
3766 break;
3767 case Ctrl_T:
3768 ctrl_x_mode = CTRL_X_THESAURUS;
3769 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003770#ifdef FEAT_COMPL_FUNC
3771 case Ctrl_U:
3772 ctrl_x_mode = CTRL_X_FUNCTION;
3773 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003774 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003775 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003776 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003777#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003778 case 's':
3779 case Ctrl_S:
3780 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003781#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003782 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003783 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003784 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003785#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003786 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 case Ctrl_RSB:
3788 ctrl_x_mode = CTRL_X_TAGS;
3789 break;
3790#ifdef FEAT_FIND_ID
3791 case Ctrl_I:
3792 case K_S_TAB:
3793 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3794 break;
3795 case Ctrl_D:
3796 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3797 break;
3798#endif
3799 case Ctrl_V:
3800 case Ctrl_Q:
3801 ctrl_x_mode = CTRL_X_CMDLINE;
3802 break;
3803 case Ctrl_P:
3804 case Ctrl_N:
3805 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3806 * just started ^X mode, or there were enough ^X's to cancel
3807 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3808 * do normal expansion when interrupting a different mode (say
3809 * ^X^F^X^P or ^P^X^X^P, see below)
3810 * nothing changes if interrupting mode 0, (eg, the flag
3811 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003812 if (!(compl_cont_status & CONT_INTRPT))
3813 compl_cont_status |= CONT_LOCAL;
3814 else if (compl_cont_mode != 0)
3815 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 /* FALLTHROUGH */
3817 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003818 /* If we have typed at least 2 ^X's... for modes != 0, we set
3819 * compl_cont_status = 0 (eg, as if we had just started ^X
3820 * mode).
3821 * For mode 0, we set "compl_cont_mode" to an impossible
3822 * value, in both cases ^X^X can be used to restart the same
3823 * mode (avoiding ADDING mode).
3824 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3825 * 'complete' and local ^P expansions respectively.
3826 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3827 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 if (c == Ctrl_X)
3829 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003830 if (compl_cont_mode != 0)
3831 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003833 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835 ctrl_x_mode = 0;
3836 edit_submode = NULL;
3837 showmode();
3838 break;
3839 }
3840 }
3841 else if (ctrl_x_mode != 0)
3842 {
3843 /* We're already in CTRL-X mode, do we stay in it? */
3844 if (!vim_is_ctrl_x_key(c))
3845 {
3846 if (ctrl_x_mode == CTRL_X_SCROLL)
3847 ctrl_x_mode = 0;
3848 else
3849 ctrl_x_mode = CTRL_X_FINISHED;
3850 edit_submode = NULL;
3851 }
3852 showmode();
3853 }
3854
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003855 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 {
3857 /* Show error message from attempted keyword completion (probably
3858 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003859 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003861 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3862 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 || ctrl_x_mode == CTRL_X_FINISHED)
3864 {
3865 /* Get here when we have finished typing a sequence of ^N and
3866 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003867 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003868 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
3870 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003871 * If any of the original typed text has been changed, eg when
3872 * ignorecase is set, we must add back-spaces to the redo
3873 * buffer. We add as few as necessary to delete just the part
3874 * of the original text that has changed.
3875 * When using the longest match, edited the match or used
3876 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003878 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3879 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003880 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003881 ptr = NULL;
3882 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884
3885#ifdef FEAT_CINDENT
3886 want_cindent = (can_cindent && cindent_on());
3887#endif
3888 /*
3889 * When completing whole lines: fix indent for 'cindent'.
3890 * Otherwise, break line if it's too long.
3891 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003892 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
3894#ifdef FEAT_CINDENT
3895 /* re-indent the current line */
3896 if (want_cindent)
3897 {
3898 do_c_expr_indent();
3899 want_cindent = FALSE; /* don't do it again */
3900 }
3901#endif
3902 }
3903 else
3904 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003905 int prev_col = curwin->w_cursor.col;
3906
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003908 if (prev_col > 0)
3909 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003910 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003911 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003913 if (prev_col > 0
3914 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3915 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003918 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003919 * the selection without inserting anything. When
3920 * compl_enter_selects is set the Enter key does the same. */
3921 if ((c == Ctrl_Y || (compl_enter_selects
3922 && (c == CAR || c == K_KENTER || c == NL)))
3923 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003924 retval = TRUE;
3925
Bram Moolenaar47247282016-08-02 22:36:02 +02003926 /* CTRL-E means completion is Ended, go back to the typed text.
3927 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003928 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003929 {
3930 ins_compl_delete();
3931 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003932 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003933 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003934 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003935 retval = TRUE;
3936 }
3937
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003938 auto_format(FALSE, TRUE);
3939
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003941 compl_started = FALSE;
3942 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003943 if (!shortmess(SHM_COMPLETIONMENU))
3944 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003946 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 if (edit_submode != NULL)
3948 {
3949 edit_submode = NULL;
3950 showmode();
3951 }
3952
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003953#ifdef FEAT_CMDWIN
3954 if (c == Ctrl_C && cmdwin_type != 0)
3955 /* Avoid the popup menu remains displayed when leaving the
3956 * command line window. */
3957 update_screen(0);
3958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959#ifdef FEAT_CINDENT
3960 /*
3961 * Indent now if a key was typed that is in 'cinkeys'.
3962 */
3963 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3964 do_c_expr_indent();
3965#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003966#ifdef FEAT_AUTOCMD
3967 /* Trigger the CompleteDone event to give scripts a chance to act
3968 * upon the completion. */
3969 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
3972 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003973#ifdef FEAT_AUTOCMD
3974 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3975 /* Trigger the CompleteDone event to give scripts a chance to act
3976 * upon the (possibly failed) completion. */
3977 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
3980 /* reset continue_* if we left expansion-mode, if we stay they'll be
3981 * (re)set properly in ins_complete() */
3982 if (!vim_is_ctrl_x_key(c))
3983 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003984 compl_cont_status = 0;
3985 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003987
3988 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989}
3990
3991/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003992 * Fix the redo buffer for the completion leader replacing some of the typed
3993 * text. This inserts backspaces and appends the changed text.
3994 * "ptr" is the known leader text or NUL.
3995 */
3996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003997ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003998{
3999 int len;
4000 char_u *p;
4001 char_u *ptr = ptr_arg;
4002
4003 if (ptr == NULL)
4004 {
4005 if (compl_leader != NULL)
4006 ptr = compl_leader;
4007 else
4008 return; /* nothing to do */
4009 }
4010 if (compl_orig_text != NULL)
4011 {
4012 p = compl_orig_text;
4013 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4014 ;
4015#ifdef FEAT_MBYTE
4016 if (len > 0)
4017 len -= (*mb_head_off)(p, p + len);
4018#endif
4019 for (p += len; *p != NUL; mb_ptr_adv(p))
4020 AppendCharToRedobuff(K_BS);
4021 }
4022 else
4023 len = 0;
4024 if (ptr != NULL)
4025 AppendToRedobuffLit(ptr + len, -1);
4026}
4027
4028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4030 * (depending on flag) starting from buf and looking for a non-scanned
4031 * buffer (other than curbuf). curbuf is special, if it is called with
4032 * buf=curbuf then it has to be the first call for a given flag/expansion.
4033 *
4034 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4035 */
4036 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004037ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038{
4039#ifdef FEAT_WINDOWS
4040 static win_T *wp;
4041#endif
4042
4043 if (flag == 'w') /* just windows */
4044 {
4045#ifdef FEAT_WINDOWS
4046 if (buf == curbuf) /* first call for this flag/expansion */
4047 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004048 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 && wp->w_buffer->b_scanned)
4050 ;
4051 buf = wp->w_buffer;
4052#else
4053 buf = curbuf;
4054#endif
4055 }
4056 else
4057 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4058 * (unlisted buffers)
4059 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004060 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 && ((flag == 'U'
4062 ? buf->b_p_bl
4063 : (!buf->b_p_bl
4064 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004065 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 ;
4067 return buf;
4068}
4069
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004070#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004071/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004072 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004073 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004074 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004076expand_by_function(
4077 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4078 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004079{
Bram Moolenaar82139082011-09-14 16:52:09 +02004080 list_T *matchlist = NULL;
4081 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004082 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004083 char_u *funcname;
4084 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004085 win_T *curwin_save;
4086 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004087 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004088
Bram Moolenaare344bea2005-09-01 20:46:49 +00004089 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4090 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004091 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004092
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004093 /* Call 'completefunc' to obtain the list of matches. */
4094 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004095 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004096
Bram Moolenaare344bea2005-09-01 20:46:49 +00004097 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004098 curwin_save = curwin;
4099 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004100
4101 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004102 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004103 {
4104 switch (rettv.v_type)
4105 {
4106 case VAR_LIST:
4107 matchlist = rettv.vval.v_list;
4108 break;
4109 case VAR_DICT:
4110 matchdict = rettv.vval.v_dict;
4111 break;
4112 default:
4113 /* TODO: Give error message? */
4114 clear_tv(&rettv);
4115 break;
4116 }
4117 }
4118
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004119 if (curwin_save != curwin || curbuf_save != curbuf)
4120 {
4121 EMSG(_(e_complwin));
4122 goto theend;
4123 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004124 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004125 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004126 if (!equalpos(curwin->w_cursor, pos))
4127 {
4128 EMSG(_(e_compldel));
4129 goto theend;
4130 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004131
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004132 if (matchlist != NULL)
4133 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004134 else if (matchdict != NULL)
4135 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004136
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004137theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004138 if (matchdict != NULL)
4139 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004140 if (matchlist != NULL)
4141 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004142}
4143#endif /* FEAT_COMPL_FUNC */
4144
Bram Moolenaar39f05632006-03-19 22:15:26 +00004145#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004146/*
4147 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004148 */
4149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004150ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004151{
4152 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004153 int dir = compl_direction;
4154
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004155 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004156 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004157 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004158 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4159 /* if dir was BACKWARD then honor it just once */
4160 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004161 else if (did_emsg)
4162 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004163 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004164}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004165
4166/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004167 * Add completions from a dict.
4168 */
4169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004170ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004171{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004172 dictitem_T *di_refresh;
4173 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004174
4175 /* Check for optional "refresh" item. */
4176 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004177 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4178 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004179 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004180 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004181
4182 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4183 compl_opt_refresh_always = TRUE;
4184 }
4185
4186 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004187 di_words = dict_find(dict, (char_u *)"words", 5);
4188 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4189 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004190}
4191
4192/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004193 * Add a match to the list of matches from a typeval_T.
4194 * If the given string is already in the list of completions, then return
4195 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4196 * maybe because alloc() returns NULL, then FAIL is returned.
4197 */
4198 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004199ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004200{
4201 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004202 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004203 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004204 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004205 char_u *(cptext[CPT_COUNT]);
4206
4207 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4208 {
4209 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4210 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4211 (char_u *)"abbr", FALSE);
4212 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4213 (char_u *)"menu", FALSE);
4214 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4215 (char_u *)"kind", FALSE);
4216 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4217 (char_u *)"info", FALSE);
4218 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4219 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004220 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004221 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004222 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4223 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004224 }
4225 else
4226 {
4227 word = get_tv_string_chk(tv);
4228 vim_memset(cptext, 0, sizeof(cptext));
4229 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004230 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004231 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004232 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004233}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004234#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004235
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004236/*
4237 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004238 * The search starts at position "ini" in curbuf and in the direction
4239 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004240 * When "compl_started" is FALSE start at that position, otherwise continue
4241 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242 * This may return before finding all the matches.
4243 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 */
4245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004246ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
4248 static pos_T first_match_pos;
4249 static pos_T last_match_pos;
4250 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 static int found_all = FALSE; /* Found all matches of a
4252 certain type. */
4253 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
Bram Moolenaar572cb562005-08-05 21:35:02 +00004255 pos_T *pos;
4256 char_u **matches;
4257 int save_p_scs;
4258 int save_p_ws;
4259 int save_p_ic;
4260 int i;
4261 int num_matches;
4262 int len;
4263 int found_new_match;
4264 int type = ctrl_x_mode;
4265 char_u *ptr;
4266 char_u *dict = NULL;
4267 int dict_f = 0;
4268 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004269 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004271 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004273 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 ins_buf->b_scanned = 0;
4275 found_all = FALSE;
4276 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004277 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004278 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 last_match_pos = first_match_pos = *ini;
4280 }
4281
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004282 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004283 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4285 for (;;)
4286 {
4287 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004288 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004290 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 * or if found_all says this entry is done. For ^X^L only use the
4292 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004293 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004294 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
4296 found_all = FALSE;
4297 while (*e_cpt == ',' || *e_cpt == ' ')
4298 e_cpt++;
4299 if (*e_cpt == '.' && !curbuf->b_scanned)
4300 {
4301 ins_buf = curbuf;
4302 first_match_pos = *ini;
4303 /* So that ^N can match word immediately after cursor */
4304 if (ctrl_x_mode == 0)
4305 dec(&first_match_pos);
4306 last_match_pos = first_match_pos;
4307 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004308
4309 /* Remember the first match so that the loop stops when we
4310 * wrap and come back there a second time. */
4311 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4314 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4315 {
4316 /* Scan a buffer, but not the current one. */
4317 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4318 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004319 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 first_match_pos.col = last_match_pos.col = 0;
4321 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4322 last_match_pos.lnum = 0;
4323 type = 0;
4324 }
4325 else /* unloaded buffer, scan like dictionary */
4326 {
4327 found_all = TRUE;
4328 if (ins_buf->b_fname == NULL)
4329 continue;
4330 type = CTRL_X_DICTIONARY;
4331 dict = ins_buf->b_fname;
4332 dict_f = DICT_EXACT;
4333 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004334 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 ins_buf->b_fname == NULL
4336 ? buf_spname(ins_buf)
4337 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004338 ? ins_buf->b_fname
4339 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004340 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
4342 else if (*e_cpt == NUL)
4343 break;
4344 else
4345 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004346 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 type = -1;
4348 else if (*e_cpt == 'k' || *e_cpt == 's')
4349 {
4350 if (*e_cpt == 'k')
4351 type = CTRL_X_DICTIONARY;
4352 else
4353 type = CTRL_X_THESAURUS;
4354 if (*++e_cpt != ',' && *e_cpt != NUL)
4355 {
4356 dict = e_cpt;
4357 dict_f = DICT_FIRST;
4358 }
4359 }
4360#ifdef FEAT_FIND_ID
4361 else if (*e_cpt == 'i')
4362 type = CTRL_X_PATH_PATTERNS;
4363 else if (*e_cpt == 'd')
4364 type = CTRL_X_PATH_DEFINES;
4365#endif
4366 else if (*e_cpt == ']' || *e_cpt == 't')
4367 {
4368 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004369 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004370 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 }
4372 else
4373 type = -1;
4374
4375 /* in any case e_cpt is advanced to the next entry */
4376 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4377
4378 found_all = TRUE;
4379 if (type == -1)
4380 continue;
4381 }
4382 }
4383
4384 switch (type)
4385 {
4386 case -1:
4387 break;
4388#ifdef FEAT_FIND_ID
4389 case CTRL_X_PATH_PATTERNS:
4390 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004391 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004394 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4396 (linenr_T)1, (linenr_T)MAXLNUM);
4397 break;
4398#endif
4399
4400 case CTRL_X_DICTIONARY:
4401 case CTRL_X_THESAURUS:
4402 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004403 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 : (type == CTRL_X_THESAURUS
4405 ? (*curbuf->b_p_tsr == NUL
4406 ? p_tsr
4407 : curbuf->b_p_tsr)
4408 : (*curbuf->b_p_dict == NUL
4409 ? p_dict
4410 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004411 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004412 dict != NULL ? dict_f
4413 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 dict = NULL;
4415 break;
4416
4417 case CTRL_X_TAGS:
4418 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4419 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004422 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423 * of matches is found when compl_pattern is empty */
4424 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4426 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4427 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4428 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004429 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431 p_ic = save_p_ic;
4432 break;
4433
4434 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004435 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4437 {
4438
4439 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004440 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004441 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443 break;
4444
4445 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004446 if (expand_cmdline(&compl_xp, compl_pattern,
4447 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004449 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 break;
4451
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004452#ifdef FEAT_COMPL_FUNC
4453 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004454 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004455 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004456 break;
4457#endif
4458
Bram Moolenaar488c6512005-08-11 20:09:58 +00004459 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004460#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004461 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004462 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004463 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004464 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004465#endif
4466 break;
4467
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 default: /* normal ^P/^N and ^X^L */
4469 /*
4470 * If 'infercase' is set, don't use 'smartcase' here
4471 */
4472 save_p_scs = p_scs;
4473 if (ins_buf->b_p_inf)
4474 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004475
Bram Moolenaar361aa502014-01-23 22:45:58 +01004476 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 * end but never from the middle, thus setting nowrapscan in this
4478 * buffers is a good idea, on the other hand, we always set
4479 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4480 save_p_ws = p_ws;
4481 if (ins_buf != curbuf)
4482 p_ws = FALSE;
4483 else if (*e_cpt == '.')
4484 p_ws = TRUE;
4485 for (;;)
4486 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004487 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004489 ++msg_silent; /* Don't want messages for wrapscan. */
4490
Bram Moolenaare4214502015-03-05 18:08:43 +01004491 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4492 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004493 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004494 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004495 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004497 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004499 found_new_match = searchit(NULL, ins_buf, pos,
4500 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004501 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004502 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004503 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004504 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004506 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004507 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 first_match_pos = *pos;
4509 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004510 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
4512 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004513 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 found_new_match = FAIL;
4515 if (found_new_match == FAIL)
4516 {
4517 if (ins_buf == curbuf)
4518 found_all = TRUE;
4519 break;
4520 }
4521
4522 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004523 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 && ini->lnum == pos->lnum
4525 && ini->col == pos->col)
4526 continue;
4527 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004528 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004530 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 {
4532 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4533 continue;
4534 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4535 if (!p_paste)
4536 ptr = skipwhite(ptr);
4537 }
4538 len = (int)STRLEN(ptr);
4539 }
4540 else
4541 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004542 char_u *tmp_ptr = ptr;
4543
4544 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004546 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 /* Skip if already inside a word. */
4548 if (vim_iswordp(tmp_ptr))
4549 continue;
4550 /* Find start of next word. */
4551 tmp_ptr = find_word_start(tmp_ptr);
4552 }
4553 /* Find end of this word. */
4554 tmp_ptr = find_word_end(tmp_ptr);
4555 len = (int)(tmp_ptr - ptr);
4556
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 if ((compl_cont_status & CONT_ADDING)
4558 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
4560 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4561 {
4562 /* Try next line, if any. the new word will be
4563 * "join" as if the normal command "J" was used.
4564 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004565 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 * works -- Acevedo */
4567 STRNCPY(IObuff, ptr, len);
4568 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4569 tmp_ptr = ptr = skipwhite(ptr);
4570 /* Find start of next word. */
4571 tmp_ptr = find_word_start(tmp_ptr);
4572 /* Find end of next word. */
4573 tmp_ptr = find_word_end(tmp_ptr);
4574 if (tmp_ptr > ptr)
4575 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004576 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004578 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 IObuff[len++] = ' ';
4580 /* IObuf =~ "\k.* ", thus len >= 2 */
4581 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004582 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 || (vim_strchr(p_cpo, CPO_JOINSP)
4584 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004585 && (IObuff[len - 2] == '?'
4586 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 IObuff[len++] = ' ';
4588 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004589 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 if (tmp_ptr - ptr >= IOSIZE - len)
4591 tmp_ptr = ptr + IOSIZE - len - 1;
4592 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4593 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004594 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 IObuff[len] = NUL;
4597 ptr = IObuff;
4598 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004599 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 continue;
4601 }
4602 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004603 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004604 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004605 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
4607 found_new_match = OK;
4608 break;
4609 }
4610 }
4611 p_scs = save_p_scs;
4612 p_ws = save_p_ws;
4613 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004614
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004615 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004616 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004617 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 found_new_match = OK;
4619
4620 /* break the loop for specialized modes (use 'complete' just for the
4621 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004622 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004623 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004624 {
4625 if (got_int)
4626 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004627 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004628 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004629 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004630
Bram Moolenaare4214502015-03-05 18:08:43 +01004631 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004632 || compl_interrupted)
4633 break;
4634 compl_started = TRUE;
4635 }
4636 else
4637 {
4638 /* Mark a buffer scanned when it has been scanned completely */
4639 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4640 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004642 compl_started = FALSE;
4643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004645 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646
Bram Moolenaare4214502015-03-05 18:08:43 +01004647 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 && *e_cpt == NUL) /* Got to end of 'complete' */
4649 found_new_match = FAIL;
4650
4651 i = -1; /* total of matches, unknown */
4652 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004653 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 i = ins_compl_make_cyclic();
4655
4656 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004657 * just been made cyclic then we have to move compl_curr_match to the next
4658 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004659 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4660 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004661 if (compl_curr_match == NULL)
4662 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 return i;
4664}
4665
4666/* Delete the old text being completed. */
4667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004668ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004670 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672 /*
4673 * In insert mode: Delete the typed part.
4674 * In replace mode: Put the old characters back, if any.
4675 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004676 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4677 if ((int)curwin->w_cursor.col > col)
4678 {
4679 if (stop_arrow() == FAIL)
4680 return;
4681 backspace_until_column(col);
4682 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004683
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004684 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4685 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004687 /* clear v:completed_item */
4688 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689}
4690
Bram Moolenaar472e8592016-10-15 17:06:47 +02004691/*
4692 * Insert the new text being completed.
4693 * "in_compl_func" is TRUE when called from complete_check().
4694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004696ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004698 dict_T *dict;
4699
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004700 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004701 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4702 compl_used_match = FALSE;
4703 else
4704 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004705
4706 /* Set completed item. */
4707 /* { word, abbr, menu, kind, info } */
4708 dict = dict_alloc();
4709 if (dict != NULL)
4710 {
4711 dict_add_nr_str(dict, "word", 0L,
4712 EMPTY_IF_NULL(compl_shown_match->cp_str));
4713 dict_add_nr_str(dict, "abbr", 0L,
4714 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4715 dict_add_nr_str(dict, "menu", 0L,
4716 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4717 dict_add_nr_str(dict, "kind", 0L,
4718 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4719 dict_add_nr_str(dict, "info", 0L,
4720 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4721 }
4722 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004723 if (!in_compl_func)
4724 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725}
4726
4727/*
4728 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004729 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4730 * get more completions. If it is FALSE, then we just do nothing when there
4731 * are no more completions in a given direction. The latter case is used when
4732 * we are still in the middle of finding completions, to allow browsing
4733 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 * Return the total number of matches, or -1 if still unknown -- webb.
4735 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004736 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4737 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 *
4739 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004740 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4741 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 */
4743 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004744ins_compl_next(
4745 int allow_get_expansion,
4746 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004747 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004748 int insert_match, /* Insert the newly selected match */
4749 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750{
4751 int num_matches = -1;
4752 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004753 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004754 compl_T *found_compl = NULL;
4755 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004756 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004757 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004759 /* When user complete function return -1 for findstart which is next
4760 * time of 'always', compl_shown_match become NULL. */
4761 if (compl_shown_match == NULL)
4762 return -1;
4763
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004764 if (compl_leader != NULL
4765 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004767 /* Set "compl_shown_match" to the actually shown match, it may differ
4768 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004769 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004770 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004771 && compl_shown_match->cp_next != NULL
4772 && compl_shown_match->cp_next != compl_first_match)
4773 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004774
4775 /* If we didn't find it searching forward, and compl_shows_dir is
4776 * backward, find the last match. */
4777 if (compl_shows_dir == BACKWARD
4778 && !ins_compl_equal(compl_shown_match,
4779 compl_leader, (int)STRLEN(compl_leader))
4780 && (compl_shown_match->cp_next == NULL
4781 || compl_shown_match->cp_next == compl_first_match))
4782 {
4783 while (!ins_compl_equal(compl_shown_match,
4784 compl_leader, (int)STRLEN(compl_leader))
4785 && compl_shown_match->cp_prev != NULL
4786 && compl_shown_match->cp_prev != compl_first_match)
4787 compl_shown_match = compl_shown_match->cp_prev;
4788 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004789 }
4790
4791 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004792 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 /* Delete old text to be replaced */
4794 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004795
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004796 /* When finding the longest common text we stick at the original text,
4797 * don't let CTRL-N or CTRL-P move to the first match. */
4798 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4799
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004800 /* When restarting the search don't insert the first match either. */
4801 if (compl_restarting)
4802 {
4803 advance = FALSE;
4804 compl_restarting = FALSE;
4805 }
4806
Bram Moolenaare3226be2005-12-18 22:10:00 +00004807 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4808 * around. */
4809 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004811 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004813 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004814 found_end = (compl_first_match != NULL
4815 && (compl_shown_match->cp_next == compl_first_match
4816 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004817 }
4818 else if (compl_shows_dir == BACKWARD
4819 && compl_shown_match->cp_prev != NULL)
4820 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004821 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004822 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004823 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
4825 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004826 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004827 if (!allow_get_expansion)
4828 {
4829 if (advance)
4830 {
4831 if (compl_shows_dir == BACKWARD)
4832 compl_pending -= todo + 1;
4833 else
4834 compl_pending += todo + 1;
4835 }
4836 return -1;
4837 }
4838
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004839 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004840 {
4841 if (compl_shows_dir == BACKWARD)
4842 --compl_pending;
4843 else
4844 ++compl_pending;
4845 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004846
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004847 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004848 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004849
4850 /* handle any pending completions */
4851 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004852 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004853 {
4854 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4855 {
4856 compl_shown_match = compl_shown_match->cp_next;
4857 --compl_pending;
4858 }
4859 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4860 {
4861 compl_shown_match = compl_shown_match->cp_prev;
4862 ++compl_pending;
4863 }
4864 else
4865 break;
4866 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004867 found_end = FALSE;
4868 }
4869 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4870 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004871 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004872 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004873 ++todo;
4874 else
4875 /* Remember a matching item. */
4876 found_compl = compl_shown_match;
4877
4878 /* Stop at the end of the list when we found a usable match. */
4879 if (found_end)
4880 {
4881 if (found_compl != NULL)
4882 {
4883 compl_shown_match = found_compl;
4884 break;
4885 }
4886 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004890 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004891 if (compl_no_insert && !started)
4892 {
4893 ins_bytes(compl_orig_text + ins_compl_len());
4894 compl_used_match = FALSE;
4895 }
4896 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004897 {
4898 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004899 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004900 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004901 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004902 }
4903 else
4904 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905
4906 if (!allow_get_expansion)
4907 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004908 /* may undisplay the popup menu first */
4909 ins_compl_upd_pum();
4910
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004911 /* redraw to show the user what was inserted */
4912 update_screen(0);
4913
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004914 /* display the updated popup menu */
4915 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004916#ifdef FEAT_GUI
4917 if (gui.in_use)
4918 {
4919 /* Show the cursor after the match, not after the redrawn text. */
4920 setcursor();
4921 out_flush();
4922 gui_update_cursor(FALSE, FALSE);
4923 }
4924#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004925
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 /* Delete old text to be replaced, since we're still searching and
4927 * don't want to match ourselves! */
4928 ins_compl_delete();
4929 }
4930
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004931 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004932 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004933 if (compl_no_insert && !started)
4934 compl_enter_selects = TRUE;
4935 else
4936 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004937
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 /*
4939 * Show the file name for the match (if any)
4940 * Truncate the file name to avoid a wait for return.
4941 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004942 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
4944 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004945 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 if (i <= 0)
4947 i = 0;
4948 else
4949 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004950 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 msg(IObuff);
4952 redraw_cmdline = FALSE; /* don't overwrite! */
4953 }
4954
4955 return num_matches;
4956}
4957
4958/*
4959 * Call this while finding completions, to check whether the user has hit a key
4960 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004961 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004963 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004964 * "in_compl_func" is TRUE when called from complete_check(), don't set
4965 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 */
4967 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004968ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
4970 static int count = 0;
4971
4972 int c;
4973
4974 /* Don't check when reading keys from a script. That would break the test
4975 * scripts */
4976 if (using_script())
4977 return;
4978
4979 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004980 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 return;
4982 count = 0;
4983
Bram Moolenaara260a972006-06-23 19:36:29 +00004984 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4985 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (c != NUL)
4988 {
4989 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4990 {
4991 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004992 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004993 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02004994 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004996 else
4997 {
4998 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004999 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005000 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005001 if (c != K_IGNORE)
5002 {
5003 /* Don't interrupt completion when the character wasn't typed,
5004 * e.g., when doing @q to replay keys. */
5005 if (c != Ctrl_R && KeyTyped)
5006 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005007
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005008 vungetc(c);
5009 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005012 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005013 {
5014 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5015
5016 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005017 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005018 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005019}
5020
5021/*
5022 * Decide the direction of Insert mode complete from the key typed.
5023 * Returns BACKWARD or FORWARD.
5024 */
5025 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005026ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005027{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005028 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005029 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005030 return BACKWARD;
5031 return FORWARD;
5032}
5033
5034/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005035 * Return TRUE for keys that are used for completion only when the popup menu
5036 * is visible.
5037 */
5038 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005039ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005040{
5041 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005042 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5043 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005044}
5045
5046/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005047 * Decide the number of completions to move forward.
5048 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5049 */
5050 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005051ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005052{
5053 int h;
5054
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005055 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005056 {
5057 h = pum_get_height();
5058 if (h > 3)
5059 h -= 2; /* keep some context */
5060 return h;
5061 }
5062 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063}
5064
5065/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005066 * Return TRUE if completion with "c" should insert the match, FALSE if only
5067 * to change the currently selected completion.
5068 */
5069 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005070ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005071{
5072 switch (c)
5073 {
5074 case K_UP:
5075 case K_DOWN:
5076 case K_PAGEDOWN:
5077 case K_KPAGEDOWN:
5078 case K_S_DOWN:
5079 case K_PAGEUP:
5080 case K_KPAGEUP:
5081 case K_S_UP:
5082 return FALSE;
5083 }
5084 return TRUE;
5085}
5086
5087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 * Do Insert mode completion.
5089 * Called when character "c" was typed, which has a meaning for completion.
5090 * Returns OK if completion was done, FAIL if something failed (out of mem).
5091 */
5092 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005093ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005095 char_u *line;
5096 int startcol = 0; /* column where searched text starts */
5097 colnr_T curs_col; /* cursor column */
5098 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005099 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005100 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005101 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005102 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103
Bram Moolenaare3226be2005-12-18 22:10:00 +00005104 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005105 insert_match = ins_compl_use_match(c);
5106
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005107 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
5109 /* First time we hit ^N or ^P (in a row, I mean) */
5110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 did_ai = FALSE;
5112#ifdef FEAT_SMARTINDENT
5113 did_si = FALSE;
5114 can_si = FALSE;
5115 can_si_back = FALSE;
5116#endif
5117 if (stop_arrow() == FAIL)
5118 return FAIL;
5119
5120 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005121 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005122 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005124 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 * "compl_startpos" to the cursor as a pattern to add a new word
5126 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005127 * "compl_startpos" is not in the same line as the cursor then fix it
5128 * (the line has been split because it was longer than 'tw'). if SOL
5129 * is set then skip the previous pattern, a word at the beginning of
5130 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005131 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5132 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
5134 /*
5135 * it is a continued search
5136 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005137 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5139 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5140 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005141 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005143 /* line (probably) wrapped, set compl_startpos to the
5144 * first non_blank in the line, if it is not a wordchar
5145 * include it to get a better pattern, but then we don't
5146 * want the "\\<" prefix, check it bellow */
5147 compl_col = (colnr_T)(skipwhite(line) - line);
5148 compl_startpos.col = compl_col;
5149 compl_startpos.lnum = curwin->w_cursor.lnum;
5150 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
5152 else
5153 {
5154 /* S_IPOS was set when we inserted a word that was at the
5155 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005156 * mode but first we need to redefine compl_startpos */
5157 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005159 compl_cont_status |= CONT_SOL;
5160 compl_startpos.col = (colnr_T)(skipwhite(
5161 line + compl_length
5162 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005164 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005166 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005167 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005168 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005170 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005172 compl_cont_status &= ~CONT_SOL;
5173 compl_length = (IOSIZE - MIN_SPACE);
5174 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005176 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5177 if (compl_length < 1)
5178 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005180 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005181 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005183 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
5185 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005186 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005188 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005192 compl_cont_status = 0;
5193 compl_cont_status |= CONT_N_ADDS;
5194 compl_startpos = curwin->w_cursor;
5195 startcol = (int)curs_col;
5196 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198
5199 /* Work out completion pattern and original text -- webb */
5200 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5201 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005202 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5204 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005205 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005207 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005209 compl_col += ++startcol;
5210 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
5212 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005213 compl_pattern = str_foldcase(line + compl_col,
5214 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005216 compl_pattern = vim_strnsave(line + compl_col,
5217 compl_length);
5218 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 return FAIL;
5220 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005221 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
5223 char_u *prefix = (char_u *)"\\<";
5224
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005225 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005226 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005227 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005230 if (!vim_iswordp(line + compl_col)
5231 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 && (
5233#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005234 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237#endif
5238 )))
5239 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005240 STRCPY((char *)compl_pattern, prefix);
5241 (void)quote_meta(compl_pattern + STRLEN(prefix),
5242 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005246 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005248 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#endif
5250 )
5251 {
5252 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005253 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5254 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005256 compl_col += curs_col;
5257 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
5259 else
5260 {
5261#ifdef FEAT_MBYTE
5262 /* Search the point of change class of multibyte character
5263 * or not a word single byte character backward. */
5264 if (has_mbyte)
5265 {
5266 int base_class;
5267 int head_off;
5268
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005269 startcol -= (*mb_head_off)(line, line + startcol);
5270 base_class = mb_get_class(line + startcol);
5271 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005273 head_off = (*mb_head_off)(line, line + startcol);
5274 if (base_class != mb_get_class(line + startcol
5275 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005277 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279 }
5280 else
5281#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005282 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005284 compl_col += ++startcol;
5285 compl_length = (int)curs_col - startcol;
5286 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 {
5288 /* Only match word with at least two chars -- webb
5289 * there's no need to call quote_meta,
5290 * alloc(7) is enough -- Acevedo
5291 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005292 compl_pattern = alloc(7);
5293 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005295 STRCPY((char *)compl_pattern, "\\<");
5296 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5297 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
5299 else
5300 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005302 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005303 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005305 STRCPY((char *)compl_pattern, "\\<");
5306 (void)quote_meta(compl_pattern + 2, line + compl_col,
5307 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309 }
5310 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005311 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005313 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005314 compl_length = (int)curs_col - (int)compl_col;
5315 if (compl_length < 0) /* cursor in indent: empty pattern */
5316 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005318 compl_pattern = str_foldcase(line + compl_col, compl_length,
5319 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005321 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5322 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 return FAIL;
5324 }
5325 else if (ctrl_x_mode == CTRL_X_FILES)
5326 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005327 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005328 if (startcol > 0)
5329 {
5330 char_u *p = line + startcol;
5331
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005332 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005333 while (p > line && vim_isfilec(PTR2CHAR(p)))
5334 mb_ptr_back(line, p);
5335 if (p == line && vim_isfilec(PTR2CHAR(p)))
5336 startcol = 0;
5337 else
5338 startcol = (int)(p - line) + 1;
5339 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005340
Bram Moolenaar0300e462013-09-08 16:03:45 +02005341 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 compl_length = (int)curs_col - startcol;
5343 compl_pattern = addstar(line + compl_col, compl_length,
5344 EXPAND_FILES);
5345 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 return FAIL;
5347 }
5348 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5349 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005350 compl_pattern = vim_strnsave(line, curs_col);
5351 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005353 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005354 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5356 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005357 /* No completion possible, use an empty pattern to get a
5358 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005359 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005360 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005361 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5362 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005364 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005365 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005366#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005367 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005368 * Call user defined function 'completefunc' with "a:findstart"
5369 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005370 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005371 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005372 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005373 char_u *funcname;
5374 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005375 win_T *curwin_save;
5376 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005377
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005378 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005379 * string */
5380 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5381 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5382 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005383 {
5384 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5385 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005386 /* restore did_ai, so that adding comment leader works */
5387 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005388 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005389 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005390
5391 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005392 args[1] = NULL;
5393 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005394 curwin_save = curwin;
5395 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005396 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005397 if (curwin_save != curwin || curbuf_save != curbuf)
5398 {
5399 EMSG(_(e_complwin));
5400 return FAIL;
5401 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005402 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005403 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005404 if (!equalpos(curwin->w_cursor, pos))
5405 {
5406 EMSG(_(e_compldel));
5407 return FAIL;
5408 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005409
Bram Moolenaar6110a002012-01-26 18:58:38 +01005410 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005411 * cancel the complete without an error.
5412 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005413 if (col == -2)
5414 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005415 if (col == -3)
5416 {
5417 ctrl_x_mode = 0;
5418 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005419 if (!shortmess(SHM_COMPLETIONMENU))
5420 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005421 return FAIL;
5422 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005423
Bram Moolenaar82139082011-09-14 16:52:09 +02005424 /*
5425 * Reset extended parameters of completion, when start new
5426 * completion.
5427 */
5428 compl_opt_refresh_always = FALSE;
5429
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005430 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005431 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005432 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005433 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005434 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005435
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 /* Setup variables for completion. Need to obtain "line" again,
5437 * it may have become invalid. */
5438 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005439 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005440 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5441 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005442#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005443 return FAIL;
5444 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005445 else if (ctrl_x_mode == CTRL_X_SPELL)
5446 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005447#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005448 if (spell_bad_len > 0)
5449 compl_col = curs_col - spell_bad_len;
5450 else
5451 compl_col = spell_word_start(startcol);
5452 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005453 {
5454 compl_length = 0;
5455 compl_col = curs_col;
5456 }
5457 else
5458 {
5459 spell_expand_check_cap(compl_col);
5460 compl_length = (int)curs_col - compl_col;
5461 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005462 /* Need to obtain "line" again, it may have become invalid. */
5463 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005464 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5465 if (compl_pattern == NULL)
5466#endif
5467 return FAIL;
5468 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005469 else
5470 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005471 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005472 return FAIL;
5473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005475 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {
5477 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005478 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 {
5480 /* Insert a new line, keep indentation but ignore 'comments' */
5481#ifdef FEAT_COMMENTS
5482 char_u *old = curbuf->b_p_com;
5483
5484 curbuf->b_p_com = (char_u *)"";
5485#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005486 compl_startpos.lnum = curwin->w_cursor.lnum;
5487 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 ins_eol('\r');
5489#ifdef FEAT_COMMENTS
5490 curbuf->b_p_com = old;
5491#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005492 compl_length = 0;
5493 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
5495 }
5496 else
5497 {
5498 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005499 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 }
5501
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005502 if (compl_cont_status & CONT_LOCAL)
5503 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 else
5505 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5506
Bram Moolenaar62951b12011-09-21 18:23:05 +02005507 /* If any of the original typed text has been changed we need to fix
5508 * the redo buffer. */
5509 ins_compl_fixRedoBufForLeader(NULL);
5510
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005511 /* Always add completion for the original text. */
5512 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005513 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5514 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005515 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005517 vim_free(compl_pattern);
5518 compl_pattern = NULL;
5519 vim_free(compl_orig_text);
5520 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 return FAIL;
5522 }
5523
5524 /* showmode might reset the internal line pointers, so it must
5525 * be called before line = ml_get(), or when this address is no
5526 * longer needed. -- Acevedo.
5527 */
5528 edit_submode_extra = (char_u *)_("-- Searching...");
5529 edit_submode_highl = HLF_COUNT;
5530 showmode();
5531 edit_submode_extra = NULL;
5532 out_flush();
5533 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005534 else if (insert_match && stop_arrow() == FAIL)
5535 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005537 compl_shown_match = compl_curr_match;
5538 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005541 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005543 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005544 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005545 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005547 /* may undisplay the popup menu */
5548 ins_compl_upd_pum();
5549
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005550 if (n > 1) /* all matches have been found */
5551 compl_matches = n;
5552 compl_curr_match = compl_shown_match;
5553 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554
Bram Moolenaard68071d2006-05-02 22:08:30 +00005555 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5556 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 if (got_int && !global_busy)
5558 {
5559 (void)vgetc();
5560 got_int = FALSE;
5561 }
5562
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005563 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005564 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005566 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5567 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5569 edit_submode_highl = HLF_E;
5570 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5571 * because we couldn't expand anything at first place, but if we used
5572 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5573 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005574 if ( compl_length > 1
5575 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 || (ctrl_x_mode != 0
5577 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5578 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005579 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 }
5581
Bram Moolenaar572cb562005-08-05 21:35:02 +00005582 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005583 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005585 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
5587 if (edit_submode_extra == NULL)
5588 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005589 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
5591 edit_submode_extra = (char_u *)_("Back at original");
5592 edit_submode_highl = HLF_W;
5593 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005594 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 {
5596 edit_submode_extra = (char_u *)_("Word from other line");
5597 edit_submode_highl = HLF_COUNT;
5598 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005599 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
5601 edit_submode_extra = (char_u *)_("The only match");
5602 edit_submode_highl = HLF_COUNT;
5603 }
5604 else
5605 {
5606 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005607 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005609 int number = 0;
5610 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005612 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 {
5614 /* search backwards for the first valid (!= -1) number.
5615 * This should normally succeed already at the first loop
5616 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005617 for (match = compl_curr_match->cp_prev; match != NULL
5618 && match != compl_first_match;
5619 match = match->cp_prev)
5620 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005622 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 break;
5624 }
5625 if (match != NULL)
5626 /* go up and assign all numbers which are not assigned
5627 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005628 for (match = match->cp_next;
5629 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005630 match = match->cp_next)
5631 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 }
5633 else /* BACKWARD */
5634 {
5635 /* search forwards (upwards) for the first valid (!= -1)
5636 * number. This should normally succeed already at the
5637 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005638 for (match = compl_curr_match->cp_next; match != NULL
5639 && match != compl_first_match;
5640 match = match->cp_next)
5641 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005643 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 break;
5645 }
5646 if (match != NULL)
5647 /* go down and assign all numbers which are not
5648 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005649 for (match = match->cp_prev; match
5650 && match->cp_number == -1;
5651 match = match->cp_prev)
5652 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 }
5654 }
5655
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005656 /* The match should always have a sequence number now, this is
5657 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005658 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005660 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5661 * Translations may need more than twice that. */
5662 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005664 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005665 vim_snprintf((char *)match_ref, sizeof(match_ref),
5666 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005667 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005669 vim_snprintf((char *)match_ref, sizeof(match_ref),
5670 _("match %d"),
5671 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 edit_submode_extra = match_ref;
5673 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005674 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 curs_columns(FALSE);
5676 }
5677 }
5678 }
5679
5680 /* Show a message about what (completion) mode we're in. */
5681 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005682 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005684 if (edit_submode_extra != NULL)
5685 {
5686 if (!p_smd)
5687 msg_attr(edit_submode_extra,
5688 edit_submode_highl < HLF_COUNT
5689 ? hl_attr(edit_submode_highl) : 0);
5690 }
5691 else
5692 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
Bram Moolenaard68071d2006-05-02 22:08:30 +00005695 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005696 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005697 show_pum(save_w_wrow, save_w_leftcol);
5698
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005699 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005700 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005701
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 return OK;
5703}
5704
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005705 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005706show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005707{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005708 /* RedrawingDisabled may be set when invoked through complete(). */
5709 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005710
Bram Moolenaarcb036422017-03-01 12:29:10 +01005711 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005712
Bram Moolenaarcb036422017-03-01 12:29:10 +01005713 /* If the cursor moved or the display scrolled we need to remove the pum
5714 * first. */
5715 setcursor();
5716 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5717 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005718
Bram Moolenaarcb036422017-03-01 12:29:10 +01005719 ins_compl_show_pum();
5720 setcursor();
5721 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005722}
5723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724/*
5725 * Looks in the first "len" chars. of "src" for search-metachars.
5726 * If dest is not NULL the chars. are copied there quoting (with
5727 * a backslash) the metachars, and dest would be NUL terminated.
5728 * Returns the length (needed) of dest
5729 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005730 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005731quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005733 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005735 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 {
5737 switch (*src)
5738 {
5739 case '.':
5740 case '*':
5741 case '[':
5742 if (ctrl_x_mode == CTRL_X_DICTIONARY
5743 || ctrl_x_mode == CTRL_X_THESAURUS)
5744 break;
5745 case '~':
5746 if (!p_magic) /* quote these only if magic is set */
5747 break;
5748 case '\\':
5749 if (ctrl_x_mode == CTRL_X_DICTIONARY
5750 || ctrl_x_mode == CTRL_X_THESAURUS)
5751 break;
5752 case '^': /* currently it's not needed. */
5753 case '$':
5754 m++;
5755 if (dest != NULL)
5756 *dest++ = '\\';
5757 break;
5758 }
5759 if (dest != NULL)
5760 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005761# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 /* Copy remaining bytes of a multibyte character. */
5763 if (has_mbyte)
5764 {
5765 int i, mb_len;
5766
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005767 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 if (mb_len > 0 && len >= mb_len)
5769 for (i = 0; i < mb_len; ++i)
5770 {
5771 --len;
5772 ++src;
5773 if (dest != NULL)
5774 *dest++ = *src;
5775 }
5776 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
5779 if (dest != NULL)
5780 *dest = NUL;
5781
5782 return m;
5783}
5784#endif /* FEAT_INS_EXPAND */
5785
5786/*
5787 * Next character is interpreted literally.
5788 * A one, two or three digit decimal number is interpreted as its byte value.
5789 * If one or two digits are entered, the next character is given to vungetc().
5790 * For Unicode a character > 255 may be returned.
5791 */
5792 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005793get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 int cc;
5796 int nc;
5797 int i;
5798 int hex = FALSE;
5799 int octal = FALSE;
5800#ifdef FEAT_MBYTE
5801 int unicode = 0;
5802#endif
5803
5804 if (got_int)
5805 return Ctrl_C;
5806
5807#ifdef FEAT_GUI
5808 /*
5809 * In GUI there is no point inserting the internal code for a special key.
5810 * It is more useful to insert the string "<KEY>" instead. This would
5811 * probably be useful in a text window too, but it would not be
5812 * vi-compatible (maybe there should be an option for it?) -- webb
5813 */
5814 if (gui.in_use)
5815 ++allow_keys;
5816#endif
5817#ifdef USE_ON_FLY_SCROLL
5818 dont_scroll = TRUE; /* disallow scrolling here */
5819#endif
5820 ++no_mapping; /* don't map the next key hits */
5821 cc = 0;
5822 i = 0;
5823 for (;;)
5824 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005825 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#ifdef FEAT_CMDL_INFO
5827 if (!(State & CMDLINE)
5828# ifdef FEAT_MBYTE
5829 && MB_BYTE2LEN_CHECK(nc) == 1
5830# endif
5831 )
5832 add_to_showcmd(nc);
5833#endif
5834 if (nc == 'x' || nc == 'X')
5835 hex = TRUE;
5836 else if (nc == 'o' || nc == 'O')
5837 octal = TRUE;
5838#ifdef FEAT_MBYTE
5839 else if (nc == 'u' || nc == 'U')
5840 unicode = nc;
5841#endif
5842 else
5843 {
5844 if (hex
5845#ifdef FEAT_MBYTE
5846 || unicode != 0
5847#endif
5848 )
5849 {
5850 if (!vim_isxdigit(nc))
5851 break;
5852 cc = cc * 16 + hex2nr(nc);
5853 }
5854 else if (octal)
5855 {
5856 if (nc < '0' || nc > '7')
5857 break;
5858 cc = cc * 8 + nc - '0';
5859 }
5860 else
5861 {
5862 if (!VIM_ISDIGIT(nc))
5863 break;
5864 cc = cc * 10 + nc - '0';
5865 }
5866
5867 ++i;
5868 }
5869
5870 if (cc > 255
5871#ifdef FEAT_MBYTE
5872 && unicode == 0
5873#endif
5874 )
5875 cc = 255; /* limit range to 0-255 */
5876 nc = 0;
5877
5878 if (hex) /* hex: up to two chars */
5879 {
5880 if (i >= 2)
5881 break;
5882 }
5883#ifdef FEAT_MBYTE
5884 else if (unicode) /* Unicode: up to four or eight chars */
5885 {
5886 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5887 break;
5888 }
5889#endif
5890 else if (i >= 3) /* decimal or octal: up to three chars */
5891 break;
5892 }
5893 if (i == 0) /* no number entered */
5894 {
5895 if (nc == K_ZERO) /* NUL is stored as NL */
5896 {
5897 cc = '\n';
5898 nc = 0;
5899 }
5900 else
5901 {
5902 cc = nc;
5903 nc = 0;
5904 }
5905 }
5906
5907 if (cc == 0) /* NUL is stored as NL */
5908 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005909#ifdef FEAT_MBYTE
5910 if (enc_dbcs && (cc & 0xff) == 0)
5911 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5912 second byte will cause trouble! */
5913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914
5915 --no_mapping;
5916#ifdef FEAT_GUI
5917 if (gui.in_use)
5918 --allow_keys;
5919#endif
5920 if (nc)
5921 vungetc(nc);
5922 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5923 return cc;
5924}
5925
5926/*
5927 * Insert character, taking care of special keys and mod_mask
5928 */
5929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005930insert_special(
5931 int c,
5932 int allow_modmask,
5933 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 char_u *p;
5936 int len;
5937
5938 /*
5939 * Special function key, translate into "<Key>". Up to the last '>' is
5940 * inserted with ins_str(), so as not to replace characters in replace
5941 * mode.
5942 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5943 * unless 'allow_modmask' is TRUE.
5944 */
5945#ifdef MACOS
5946 /* Command-key never produces a normal key */
5947 if (mod_mask & MOD_MASK_CMD)
5948 allow_modmask = TRUE;
5949#endif
5950 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5951 {
5952 p = get_special_key_name(c, mod_mask);
5953 len = (int)STRLEN(p);
5954 c = p[len - 1];
5955 if (len > 2)
5956 {
5957 if (stop_arrow() == FAIL)
5958 return;
5959 p[len - 1] = NUL;
5960 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005961 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 ctrlv = FALSE;
5963 }
5964 }
5965 if (stop_arrow() == OK)
5966 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5967}
5968
5969/*
5970 * Special characters in this context are those that need processing other
5971 * than the simple insertion that can be performed here. This includes ESC
5972 * which terminates the insert, and CR/NL which need special processing to
5973 * open up a new line. This routine tries to optimize insertions performed by
5974 * the "redo", "undo" or "put" commands, so it needs to know when it should
5975 * stop and defer processing to the "normal" mechanism.
5976 * '0' and '^' are special, because they can be followed by CTRL-D.
5977 */
5978#ifdef EBCDIC
5979# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5980#else
5981# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5982#endif
5983
5984#ifdef FEAT_MBYTE
5985# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5986#else
5987# define WHITECHAR(cc) vim_iswhite(cc)
5988#endif
5989
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005990/*
5991 * "flags": INSCHAR_FORMAT - force formatting
5992 * INSCHAR_CTRLV - char typed just after CTRL-V
5993 * INSCHAR_NO_FEX - don't use 'formatexpr'
5994 *
5995 * NOTE: passes the flags value straight through to internal_format() which,
5996 * beside INSCHAR_FORMAT (above), is also looking for these:
5997 * INSCHAR_DO_COM - format comments
5998 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006001insertchar(
6002 int c, /* character to insert or NUL */
6003 int flags, /* INSCHAR_FORMAT, etc. */
6004 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 int textwidth;
6007#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006011 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006013 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
6016 /*
6017 * Try to break the line in two or more pieces when:
6018 * - Always do this if we have been called to do formatting only.
6019 * - Always do this when 'formatoptions' has the 'a' flag and the line
6020 * ends in white space.
6021 * - Otherwise:
6022 * - Don't do this if inserting a blank
6023 * - Don't do this if an existing character is being replaced, unless
6024 * we're in VREPLACE mode.
6025 * - Do this if the cursor is not on the line where insert started
6026 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6027 * before the insert.
6028 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6029 * before 'textwidth'
6030 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006031 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006032 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 || (!vim_iswhite(c)
6034 && !((State & REPLACE_FLAG)
6035#ifdef FEAT_VREPLACE
6036 && !(State & VREPLACE_FLAG)
6037#endif
6038 && *ml_get_cursor() != NUL)
6039 && (curwin->w_cursor.lnum != Insstart.lnum
6040 || ((!has_format_option(FO_INS_LONG)
6041 || Insstart_textlen <= (colnr_T)textwidth)
6042 && (!fo_ins_blank
6043 || Insstart_blank_vcol <= (colnr_T)textwidth
6044 ))))))
6045 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006046 /* Format with 'formatexpr' when it's set. Use internal formatting
6047 * when 'formatexpr' isn't set or it returns non-zero. */
6048#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006049 int do_internal = TRUE;
6050 colnr_T virtcol = get_nolist_virtcol()
6051 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006052
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006053 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6054 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006055 {
6056 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6057 /* It may be required to save for undo again, e.g. when setline()
6058 * was called. */
6059 ins_need_undo = TRUE;
6060 }
6061 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006063 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006065
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 if (c == NUL) /* only formatting was wanted */
6067 return;
6068
6069#ifdef FEAT_COMMENTS
6070 /* Check whether this character should end a comment. */
6071 if (did_ai && (int)c == end_comment_pending)
6072 {
6073 char_u *line;
6074 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6075 int middle_len, end_len;
6076 int i;
6077
6078 /*
6079 * Need to remove existing (middle) comment leader and insert end
6080 * comment leader. First, check what comment leader we can find.
6081 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006082 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6084 {
6085 /* Skip middle-comment string */
6086 while (*p && p[-1] != ':') /* find end of middle flags */
6087 ++p;
6088 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6089 /* Don't count trailing white space for middle_len */
6090 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6091 --middle_len;
6092
6093 /* Find the end-comment string */
6094 while (*p && p[-1] != ':') /* find end of end flags */
6095 ++p;
6096 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6097
6098 /* Skip white space before the cursor */
6099 i = curwin->w_cursor.col;
6100 while (--i >= 0 && vim_iswhite(line[i]))
6101 ;
6102 i++;
6103
6104 /* Skip to before the middle leader */
6105 i -= middle_len;
6106
6107 /* Check some expected things before we go on */
6108 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6109 {
6110 /* Backspace over all the stuff we want to replace */
6111 backspace_until_column(i);
6112
6113 /*
6114 * Insert the end-comment string, except for the last
6115 * character, which will get inserted as normal later.
6116 */
6117 ins_bytes_len(lead_end, end_len - 1);
6118 }
6119 }
6120 }
6121 end_comment_pending = NUL;
6122#endif
6123
6124 did_ai = FALSE;
6125#ifdef FEAT_SMARTINDENT
6126 did_si = FALSE;
6127 can_si = FALSE;
6128 can_si_back = FALSE;
6129#endif
6130
6131 /*
6132 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6133 * This speeds up normal text input considerably.
6134 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6135 * need to re-indent at a ':', or any other character (but not what
6136 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006137 * Don't do this when there an InsertCharPre autocommand is defined,
6138 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 */
6140#ifdef USE_ON_FLY_SCROLL
6141 dont_scroll = FALSE; /* allow scrolling here */
6142#endif
6143
6144 if ( !ISSPECIAL(c)
6145#ifdef FEAT_MBYTE
6146 && (!has_mbyte || (*mb_char2len)(c) == 1)
6147#endif
6148 && vpeekc() != NUL
6149 && !(State & REPLACE_FLAG)
6150#ifdef FEAT_CINDENT
6151 && !cindent_on()
6152#endif
6153#ifdef FEAT_RIGHTLEFT
6154 && !p_ri
6155#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006156#ifdef FEAT_AUTOCMD
6157 && !has_insertcharpre()
6158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 )
6160 {
6161#define INPUT_BUFLEN 100
6162 char_u buf[INPUT_BUFLEN + 1];
6163 int i;
6164 colnr_T virtcol = 0;
6165
6166 buf[0] = c;
6167 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006168 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 virtcol = get_nolist_virtcol();
6170 /*
6171 * Stop the string when:
6172 * - no more chars available
6173 * - finding a special character (command key)
6174 * - buffer is full
6175 * - running into the 'textwidth' boundary
6176 * - need to check for abbreviation: A non-word char after a word-char
6177 */
6178 while ( (c = vpeekc()) != NUL
6179 && !ISSPECIAL(c)
6180#ifdef FEAT_MBYTE
6181 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6182#endif
6183 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006184# ifdef FEAT_FKMAP
6185 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 && (textwidth == 0
6188 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6189 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6190 {
6191#ifdef FEAT_RIGHTLEFT
6192 c = vgetc();
6193 if (p_hkmap && KeyTyped)
6194 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 buf[i++] = c;
6196#else
6197 buf[i++] = vgetc();
6198#endif
6199 }
6200
6201#ifdef FEAT_DIGRAPHS
6202 do_digraph(-1); /* clear digraphs */
6203 do_digraph(buf[i-1]); /* may be the start of a digraph */
6204#endif
6205 buf[i] = NUL;
6206 ins_str(buf);
6207 if (flags & INSCHAR_CTRLV)
6208 {
6209 redo_literal(*buf);
6210 i = 1;
6211 }
6212 else
6213 i = 0;
6214 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006215 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 }
6217 else
6218 {
6219#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006220 int cc;
6221
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6223 {
6224 char_u buf[MB_MAXBYTES + 1];
6225
6226 (*mb_char2bytes)(c, buf);
6227 buf[cc] = NUL;
6228 ins_char_bytes(buf, cc);
6229 AppendCharToRedobuff(c);
6230 }
6231 else
6232#endif
6233 {
6234 ins_char(c);
6235 if (flags & INSCHAR_CTRLV)
6236 redo_literal(c);
6237 else
6238 AppendCharToRedobuff(c);
6239 }
6240 }
6241}
6242
6243/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006244 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006245 *
6246 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6247 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006248 */
6249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006250internal_format(
6251 int textwidth,
6252 int second_indent,
6253 int flags,
6254 int format_only,
6255 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006256{
6257 int cc;
6258 int save_char = NUL;
6259 int haveto_redraw = FALSE;
6260 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6261#ifdef FEAT_MBYTE
6262 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6263#endif
6264 int fo_white_par = has_format_option(FO_WHITE_PAR);
6265 int first_line = TRUE;
6266#ifdef FEAT_COMMENTS
6267 colnr_T leader_len;
6268 int no_leader = FALSE;
6269 int do_comments = (flags & INSCHAR_DO_COM);
6270#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006271#ifdef FEAT_LINEBREAK
6272 int has_lbr = curwin->w_p_lbr;
6273
6274 /* make sure win_lbr_chartabsize() counts correctly */
6275 curwin->w_p_lbr = FALSE;
6276#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006277
6278 /*
6279 * When 'ai' is off we don't want a space under the cursor to be
6280 * deleted. Replace it with an 'x' temporarily.
6281 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006282 if (!curbuf->b_p_ai
6283#ifdef FEAT_VREPLACE
6284 && !(State & VREPLACE_FLAG)
6285#endif
6286 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006287 {
6288 cc = gchar_cursor();
6289 if (vim_iswhite(cc))
6290 {
6291 save_char = cc;
6292 pchar_cursor('x');
6293 }
6294 }
6295
6296 /*
6297 * Repeat breaking lines, until the current line is not too long.
6298 */
6299 while (!got_int)
6300 {
6301 int startcol; /* Cursor column at entry */
6302 int wantcol; /* column at textwidth border */
6303 int foundcol; /* column for start of spaces */
6304 int end_foundcol = 0; /* column for start of word */
6305 colnr_T len;
6306 colnr_T virtcol;
6307#ifdef FEAT_VREPLACE
6308 int orig_col = 0;
6309 char_u *saved_text = NULL;
6310#endif
6311 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006312 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006313
Bram Moolenaar97b98102009-11-17 16:41:01 +00006314 virtcol = get_nolist_virtcol()
6315 + char2cells(c != NUL ? c : gchar_cursor());
6316 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006317 break;
6318
6319#ifdef FEAT_COMMENTS
6320 if (no_leader)
6321 do_comments = FALSE;
6322 else if (!(flags & INSCHAR_FORMAT)
6323 && has_format_option(FO_WRAP_COMS))
6324 do_comments = TRUE;
6325
6326 /* Don't break until after the comment leader */
6327 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006328 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006329 else
6330 leader_len = 0;
6331
6332 /* If the line doesn't start with a comment leader, then don't
6333 * start one in a following broken line. Avoids that a %word
6334 * moved to the start of the next line causes all following lines
6335 * to start with %. */
6336 if (leader_len == 0)
6337 no_leader = TRUE;
6338#endif
6339 if (!(flags & INSCHAR_FORMAT)
6340#ifdef FEAT_COMMENTS
6341 && leader_len == 0
6342#endif
6343 && !has_format_option(FO_WRAP))
6344
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006345 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006346 if ((startcol = curwin->w_cursor.col) == 0)
6347 break;
6348
6349 /* find column of textwidth border */
6350 coladvance((colnr_T)textwidth);
6351 wantcol = curwin->w_cursor.col;
6352
Bram Moolenaar97b98102009-11-17 16:41:01 +00006353 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006354 foundcol = 0;
6355
6356 /*
6357 * Find position to break at.
6358 * Stop at first entered white when 'formatoptions' has 'v'
6359 */
6360 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006361 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006362 || curwin->w_cursor.lnum != Insstart.lnum
6363 || curwin->w_cursor.col >= Insstart.col)
6364 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006365 if (curwin->w_cursor.col == startcol && c != NUL)
6366 cc = c;
6367 else
6368 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006369 if (WHITECHAR(cc))
6370 {
6371 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006372 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006373
6374 /* find start of sequence of blanks */
6375 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6376 {
6377 dec_cursor();
6378 cc = gchar_cursor();
6379 }
6380 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6381 break; /* only spaces in front of text */
6382#ifdef FEAT_COMMENTS
6383 /* Don't break until after the comment leader */
6384 if (curwin->w_cursor.col < leader_len)
6385 break;
6386#endif
6387 if (has_format_option(FO_ONE_LETTER))
6388 {
6389 /* do not break after one-letter words */
6390 if (curwin->w_cursor.col == 0)
6391 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006392#ifdef FEAT_COMMENTS
6393 /* do not break "#a b" when 'tw' is 2 */
6394 if (curwin->w_cursor.col <= leader_len)
6395 break;
6396#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006397 col = curwin->w_cursor.col;
6398 dec_cursor();
6399 cc = gchar_cursor();
6400
6401 if (WHITECHAR(cc))
6402 continue; /* one-letter, continue */
6403 curwin->w_cursor.col = col;
6404 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006405
6406 inc_cursor();
6407
6408 end_foundcol = end_col + 1;
6409 foundcol = curwin->w_cursor.col;
6410 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006411 break;
6412 }
6413#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006414 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006415 {
6416 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006417 if (curwin->w_cursor.col != startcol)
6418 {
6419#ifdef FEAT_COMMENTS
6420 /* Don't break until after the comment leader */
6421 if (curwin->w_cursor.col < leader_len)
6422 break;
6423#endif
6424 col = curwin->w_cursor.col;
6425 inc_cursor();
6426 /* Don't change end_foundcol if already set. */
6427 if (foundcol != curwin->w_cursor.col)
6428 {
6429 foundcol = curwin->w_cursor.col;
6430 end_foundcol = foundcol;
6431 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6432 break;
6433 }
6434 curwin->w_cursor.col = col;
6435 }
6436
6437 if (curwin->w_cursor.col == 0)
6438 break;
6439
6440 col = curwin->w_cursor.col;
6441
6442 dec_cursor();
6443 cc = gchar_cursor();
6444
6445 if (WHITECHAR(cc))
6446 continue; /* break with space */
6447#ifdef FEAT_COMMENTS
6448 /* Don't break until after the comment leader */
6449 if (curwin->w_cursor.col < leader_len)
6450 break;
6451#endif
6452
6453 curwin->w_cursor.col = col;
6454
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006455 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006456 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006457 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6458 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006459 }
6460#endif
6461 if (curwin->w_cursor.col == 0)
6462 break;
6463 dec_cursor();
6464 }
6465
6466 if (foundcol == 0) /* no spaces, cannot break line */
6467 {
6468 curwin->w_cursor.col = startcol;
6469 break;
6470 }
6471
6472 /* Going to break the line, remove any "$" now. */
6473 undisplay_dollar();
6474
6475 /*
6476 * Offset between cursor position and line break is used by replace
6477 * stack functions. VREPLACE does not use this, and backspaces
6478 * over the text instead.
6479 */
6480#ifdef FEAT_VREPLACE
6481 if (State & VREPLACE_FLAG)
6482 orig_col = startcol; /* Will start backspacing from here */
6483 else
6484#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006485 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006486
6487 /*
6488 * adjust startcol for spaces that will be deleted and
6489 * characters that will remain on top line
6490 */
6491 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006492 while ((cc = gchar_cursor(), WHITECHAR(cc))
6493 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006494 inc_cursor();
6495 startcol -= curwin->w_cursor.col;
6496 if (startcol < 0)
6497 startcol = 0;
6498
6499#ifdef FEAT_VREPLACE
6500 if (State & VREPLACE_FLAG)
6501 {
6502 /*
6503 * In VREPLACE mode, we will backspace over the text to be
6504 * wrapped, so save a copy now to put on the next line.
6505 */
6506 saved_text = vim_strsave(ml_get_cursor());
6507 curwin->w_cursor.col = orig_col;
6508 if (saved_text == NULL)
6509 break; /* Can't do it, out of memory */
6510 saved_text[startcol] = NUL;
6511
6512 /* Backspace over characters that will move to the next line */
6513 if (!fo_white_par)
6514 backspace_until_column(foundcol);
6515 }
6516 else
6517#endif
6518 {
6519 /* put cursor after pos. to break line */
6520 if (!fo_white_par)
6521 curwin->w_cursor.col = foundcol;
6522 }
6523
6524 /*
6525 * Split the line just before the margin.
6526 * Only insert/delete lines, but don't really redraw the window.
6527 */
6528 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6529 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6530#ifdef FEAT_COMMENTS
6531 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006532 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006533#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006534 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6535 if (!(flags & INSCHAR_COM_LIST))
6536 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006537
6538 replace_offset = 0;
6539 if (first_line)
6540 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006541 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006542 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006543 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006544 * This section is for auto-wrap of numeric lists. When not
6545 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6546 * flag will be set and open_line() will handle it (as seen
6547 * above). The code here (and in get_number_indent()) will
6548 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006549 */
6550 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006551 second_indent =
6552 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006553 if (second_indent >= 0)
6554 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006555#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006556 if (State & VREPLACE_FLAG)
6557 change_indent(INDENT_SET, second_indent,
6558 FALSE, NUL, TRUE);
6559 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006560#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006561#ifdef FEAT_COMMENTS
6562 if (leader_len > 0 && second_indent - leader_len > 0)
6563 {
6564 int i;
6565 int padding = second_indent - leader_len;
6566
6567 /* We started at the first_line of a numbered list
6568 * that has a comment. the open_line() function has
6569 * inserted the proper comment leader and positioned
6570 * the cursor at the end of the split line. Now we
6571 * add the additional whitespace needed after the
6572 * comment leader for the numbered list. */
6573 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006574 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006575 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006576 }
6577 else
6578 {
6579#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006580 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006581#ifdef FEAT_COMMENTS
6582 }
6583#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006584 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006585 }
6586 first_line = FALSE;
6587 }
6588
6589#ifdef FEAT_VREPLACE
6590 if (State & VREPLACE_FLAG)
6591 {
6592 /*
6593 * In VREPLACE mode we have backspaced over the text to be
6594 * moved, now we re-insert it into the new line.
6595 */
6596 ins_bytes(saved_text);
6597 vim_free(saved_text);
6598 }
6599 else
6600#endif
6601 {
6602 /*
6603 * Check if cursor is not past the NUL off the line, cindent
6604 * may have added or removed indent.
6605 */
6606 curwin->w_cursor.col += startcol;
6607 len = (colnr_T)STRLEN(ml_get_curline());
6608 if (curwin->w_cursor.col > len)
6609 curwin->w_cursor.col = len;
6610 }
6611
6612 haveto_redraw = TRUE;
6613#ifdef FEAT_CINDENT
6614 can_cindent = TRUE;
6615#endif
6616 /* moved the cursor, don't autoindent or cindent now */
6617 did_ai = FALSE;
6618#ifdef FEAT_SMARTINDENT
6619 did_si = FALSE;
6620 can_si = FALSE;
6621 can_si_back = FALSE;
6622#endif
6623 line_breakcheck();
6624 }
6625
6626 if (save_char != NUL) /* put back space after cursor */
6627 pchar_cursor(save_char);
6628
Bram Moolenaar0026d472014-09-09 16:32:39 +02006629#ifdef FEAT_LINEBREAK
6630 curwin->w_p_lbr = has_lbr;
6631#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006632 if (!format_only && haveto_redraw)
6633 {
6634 update_topline();
6635 redraw_curbuf_later(VALID);
6636 }
6637}
6638
6639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 * Called after inserting or deleting text: When 'formatoptions' includes the
6641 * 'a' flag format from the current line until the end of the paragraph.
6642 * Keep the cursor at the same position relative to the text.
6643 * The caller must have saved the cursor line for undo, following ones will be
6644 * saved here.
6645 */
6646 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006647auto_format(
6648 int trailblank, /* when TRUE also format with trailing blank */
6649 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
6651 pos_T pos;
6652 colnr_T len;
6653 char_u *old;
6654 char_u *new, *pnew;
6655 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006656 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657
6658 if (!has_format_option(FO_AUTO))
6659 return;
6660
6661 pos = curwin->w_cursor;
6662 old = ml_get_curline();
6663
6664 /* may remove added space */
6665 check_auto_format(FALSE);
6666
6667 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6668 * user might insert normal text next. Also skip formatting when "1" is
6669 * in 'formatoptions' and there is a single character before the cursor.
6670 * Otherwise the line would be broken and when typing another non-white
6671 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006672 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 if (*old != NUL && !trailblank && wasatend)
6674 {
6675 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006676 cc = gchar_cursor();
6677 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6678 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006680 cc = gchar_cursor();
6681 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {
6683 curwin->w_cursor = pos;
6684 return;
6685 }
6686 curwin->w_cursor = pos;
6687 }
6688
6689#ifdef FEAT_COMMENTS
6690 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6691 * comments. */
6692 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006693 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 return;
6695#endif
6696
6697 /*
6698 * May start formatting in a previous line, so that after "x" a word is
6699 * moved to the previous line if it fits there now. Only when this is not
6700 * the start of a paragraph.
6701 */
6702 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6703 {
6704 --curwin->w_cursor.lnum;
6705 if (u_save_cursor() == FAIL)
6706 return;
6707 }
6708
6709 /*
6710 * Do the formatting and restore the cursor position. "saved_cursor" will
6711 * be adjusted for the text formatting.
6712 */
6713 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006714 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 curwin->w_cursor = saved_cursor;
6716 saved_cursor.lnum = 0;
6717
6718 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6719 {
6720 /* "cannot happen" */
6721 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6722 coladvance((colnr_T)MAXCOL);
6723 }
6724 else
6725 check_cursor_col();
6726
6727 /* Insert mode: If the cursor is now after the end of the line while it
6728 * previously wasn't, the line was broken. Because of the rule above we
6729 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6730 * formatted. */
6731 if (!wasatend && has_format_option(FO_WHITE_PAR))
6732 {
6733 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006734 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 if (curwin->w_cursor.col == len)
6736 {
6737 pnew = vim_strnsave(new, len + 2);
6738 pnew[len] = ' ';
6739 pnew[len + 1] = NUL;
6740 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6741 /* remove the space later */
6742 did_add_space = TRUE;
6743 }
6744 else
6745 /* may remove added space */
6746 check_auto_format(FALSE);
6747 }
6748
6749 check_cursor();
6750}
6751
6752/*
6753 * When an extra space was added to continue a paragraph for auto-formatting,
6754 * delete it now. The space must be under the cursor, just after the insert
6755 * position.
6756 */
6757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006758check_auto_format(
6759 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
6761 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006762 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763
6764 if (did_add_space)
6765 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006766 cc = gchar_cursor();
6767 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 /* Somehow the space was removed already. */
6769 did_add_space = FALSE;
6770 else
6771 {
6772 if (!end_insert)
6773 {
6774 inc_cursor();
6775 c = gchar_cursor();
6776 dec_cursor();
6777 }
6778 if (c != NUL)
6779 {
6780 /* The space is no longer at the end of the line, delete it. */
6781 del_char(FALSE);
6782 did_add_space = FALSE;
6783 }
6784 }
6785 }
6786}
6787
6788/*
6789 * Find out textwidth to be used for formatting:
6790 * if 'textwidth' option is set, use it
6791 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6792 * if invalid value, use 0.
6793 * Set default to window width (maximum 79) for "gq" operator.
6794 */
6795 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006796comp_textwidth(
6797 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798{
6799 int textwidth;
6800
6801 textwidth = curbuf->b_p_tw;
6802 if (textwidth == 0 && curbuf->b_p_wm)
6803 {
6804 /* The width is the window width minus 'wrapmargin' minus all the
6805 * things that add to the margin. */
6806 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6807#ifdef FEAT_CMDWIN
6808 if (cmdwin_type != 0)
6809 textwidth -= 1;
6810#endif
6811#ifdef FEAT_FOLDING
6812 textwidth -= curwin->w_p_fdc;
6813#endif
6814#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006815 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 textwidth -= 1;
6817#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006818 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 textwidth -= 8;
6820 }
6821 if (textwidth < 0)
6822 textwidth = 0;
6823 if (ff && textwidth == 0)
6824 {
6825 textwidth = W_WIDTH(curwin) - 1;
6826 if (textwidth > 79)
6827 textwidth = 79;
6828 }
6829 return textwidth;
6830}
6831
6832/*
6833 * Put a character in the redo buffer, for when just after a CTRL-V.
6834 */
6835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006836redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837{
6838 char_u buf[10];
6839
6840 /* Only digits need special treatment. Translate them into a string of
6841 * three digits. */
6842 if (VIM_ISDIGIT(c))
6843 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006844 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 AppendToRedobuff(buf);
6846 }
6847 else
6848 AppendCharToRedobuff(c);
6849}
6850
6851/*
6852 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006853 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 */
6855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006856start_arrow(
6857 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006859 start_arrow_common(end_insert_pos, TRUE);
6860}
6861
6862/*
6863 * Like start_arrow() but with end_change argument.
6864 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6865 */
6866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006867start_arrow_with_change(
6868 pos_T *end_insert_pos, /* can be NULL */
6869 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006870{
6871 start_arrow_common(end_insert_pos, end_change);
6872 if (!end_change)
6873 {
6874 AppendCharToRedobuff(Ctrl_G);
6875 AppendCharToRedobuff('U');
6876 }
6877}
6878
6879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006880start_arrow_common(
6881 pos_T *end_insert_pos, /* can be NULL */
6882 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006883{
6884 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
6886 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006887 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 arrow_used = TRUE; /* this means we stopped the current insert */
6889 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006890#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006891 check_spell_redraw();
6892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893}
6894
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006895#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006896/*
6897 * If we skipped highlighting word at cursor, do it now.
6898 * It may be skipped again, thus reset spell_redraw_lnum first.
6899 */
6900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006901check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006902{
6903 if (spell_redraw_lnum != 0)
6904 {
6905 linenr_T lnum = spell_redraw_lnum;
6906
6907 spell_redraw_lnum = 0;
6908 redrawWinline(lnum, FALSE);
6909 }
6910}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006911
6912/*
6913 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6914 * spelled word, if there is one.
6915 */
6916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006917spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006918{
6919 pos_T tpos = curwin->w_cursor;
6920
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006921 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006922 if (curwin->w_cursor.col != tpos.col)
6923 start_arrow(&tpos);
6924}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006925#endif
6926
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927/*
6928 * stop_arrow() is called before a change is made in insert mode.
6929 * If an arrow key has been used, start a new insertion.
6930 * Returns FAIL if undo is impossible, shouldn't insert then.
6931 */
6932 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006933stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934{
6935 if (arrow_used)
6936 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006937 Insstart = curwin->w_cursor; /* new insertion starts here */
6938 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6939 /* Don't update the original insert position when moved to the
6940 * right, except when nothing was inserted yet. */
6941 update_Insstart_orig = FALSE;
6942 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6943
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 if (u_save_cursor() == OK)
6945 {
6946 arrow_used = FALSE;
6947 ins_need_undo = FALSE;
6948 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006949
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 ai_col = 0;
6951#ifdef FEAT_VREPLACE
6952 if (State & VREPLACE_FLAG)
6953 {
6954 orig_line_count = curbuf->b_ml.ml_line_count;
6955 vr_lines_changed = 1;
6956 }
6957#endif
6958 ResetRedobuff();
6959 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006960 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 }
6962 else if (ins_need_undo)
6963 {
6964 if (u_save_cursor() == OK)
6965 ins_need_undo = FALSE;
6966 }
6967
6968#ifdef FEAT_FOLDING
6969 /* Always open fold at the cursor line when inserting something. */
6970 foldOpenCursor();
6971#endif
6972
6973 return (arrow_used || ins_need_undo ? FAIL : OK);
6974}
6975
6976/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006977 * Do a few things to stop inserting.
6978 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6979 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 */
6981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006982stop_insert(
6983 pos_T *end_insert_pos,
6984 int esc, /* called by ins_esc() */
6985 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006987 int cc;
6988 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989
6990 stop_redo_ins();
6991 replace_flush(); /* abandon replace stack */
6992
6993 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006994 * Save the inserted text for later redo with ^@ and CTRL-A.
6995 * Don't do it when "restart_edit" was set and nothing was inserted,
6996 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006998 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006999 if (did_restart_edit == 0 || (ptr != NULL
7000 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007001 {
7002 vim_free(last_insert);
7003 last_insert = ptr;
7004 last_insert_skip = new_insert_skip;
7005 }
7006 else
7007 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007009 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
7011 /* Auto-format now. It may seem strange to do this when stopping an
7012 * insertion (or moving the cursor), but it's required when appending
7013 * a line and having it end in a space. But only do it when something
7014 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007015 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007017 pos_T tpos = curwin->w_cursor;
7018
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 /* When the cursor is at the end of the line after a space the
7020 * formatting will move it to the following word. Avoid that by
7021 * moving the cursor onto the space. */
7022 cc = 'x';
7023 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7024 {
7025 dec_cursor();
7026 cc = gchar_cursor();
7027 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007028 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 }
7030
7031 auto_format(TRUE, FALSE);
7032
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007033 if (vim_iswhite(cc))
7034 {
7035 if (gchar_cursor() != NUL)
7036 inc_cursor();
7037#ifdef FEAT_VIRTUALEDIT
7038 /* If the cursor is still at the same character, also keep
7039 * the "coladd". */
7040 if (gchar_cursor() == NUL
7041 && curwin->w_cursor.lnum == tpos.lnum
7042 && curwin->w_cursor.col == tpos.col)
7043 curwin->w_cursor.coladd = tpos.coladd;
7044#endif
7045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047
7048 /* If a space was inserted for auto-formatting, remove it now. */
7049 check_auto_format(TRUE);
7050
7051 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007052 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007053 * Do this when ESC was used or moving the cursor up/down.
7054 * Check for the old position still being valid, just in case the text
7055 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007056 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007057 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7058 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007060 pos_T tpos = curwin->w_cursor;
7061
7062 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007063 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007064 for (;;)
7065 {
7066 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7067 --curwin->w_cursor.col;
7068 cc = gchar_cursor();
7069 if (!vim_iswhite(cc))
7070 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007071 if (del_char(TRUE) == FAIL)
7072 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007073 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007074 if (curwin->w_cursor.lnum != tpos.lnum)
7075 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007076 else
7077 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007078 /* reset tpos, could have been invalidated in the loop above */
7079 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007080 tpos.col++;
7081 if (cc != NUL && gchar_pos(&tpos) == NUL)
7082 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 /* <C-S-Right> may have started Visual mode, adjust the position for
7086 * deleted characters. */
7087 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7088 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007089 int len = (int)STRLEN(ml_get_curline());
7090
7091 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007093 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007094#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 }
7098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
7100 }
7101 did_ai = FALSE;
7102#ifdef FEAT_SMARTINDENT
7103 did_si = FALSE;
7104 can_si = FALSE;
7105 can_si_back = FALSE;
7106#endif
7107
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007108 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7109 * now in a different buffer. */
7110 if (end_insert_pos != NULL)
7111 {
7112 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007113 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007114 curbuf->b_op_end = *end_insert_pos;
7115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116}
7117
7118/*
7119 * Set the last inserted text to a single character.
7120 * Used for the replace command.
7121 */
7122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007123set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124{
7125 char_u *s;
7126
7127 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 if (last_insert != NULL)
7130 {
7131 s = last_insert;
7132 /* Use the CTRL-V only when entering a special char */
7133 if (c < ' ' || c == DEL)
7134 *s++ = Ctrl_V;
7135 s = add_char2buf(c, s);
7136 *s++ = ESC;
7137 *s++ = NUL;
7138 last_insert_skip = 0;
7139 }
7140}
7141
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007142#if defined(EXITFREE) || defined(PROTO)
7143 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007144free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007145{
7146 vim_free(last_insert);
7147 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007148# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007149 vim_free(compl_orig_text);
7150 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007151# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007152}
7153#endif
7154
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155/*
7156 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7157 * and CSI. Handle multi-byte characters.
7158 * Returns a pointer to after the added bytes.
7159 */
7160 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007161add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
7163#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007164 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 int i;
7166 int len;
7167
7168 len = (*mb_char2bytes)(c, temp);
7169 for (i = 0; i < len; ++i)
7170 {
7171 c = temp[i];
7172#endif
7173 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7174 if (c == K_SPECIAL)
7175 {
7176 *s++ = K_SPECIAL;
7177 *s++ = KS_SPECIAL;
7178 *s++ = KE_FILLER;
7179 }
7180#ifdef FEAT_GUI
7181 else if (c == CSI)
7182 {
7183 *s++ = CSI;
7184 *s++ = KS_EXTRA;
7185 *s++ = (int)KE_CSI;
7186 }
7187#endif
7188 else
7189 *s++ = c;
7190#ifdef FEAT_MBYTE
7191 }
7192#endif
7193 return s;
7194}
7195
7196/*
7197 * move cursor to start of line
7198 * if flags & BL_WHITE move to first non-white
7199 * if flags & BL_SOL move to first non-white if startofline is set,
7200 * otherwise keep "curswant" column
7201 * if flags & BL_FIX don't leave the cursor on a NUL.
7202 */
7203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007204beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205{
7206 if ((flags & BL_SOL) && !p_sol)
7207 coladvance(curwin->w_curswant);
7208 else
7209 {
7210 curwin->w_cursor.col = 0;
7211#ifdef FEAT_VIRTUALEDIT
7212 curwin->w_cursor.coladd = 0;
7213#endif
7214
7215 if (flags & (BL_WHITE | BL_SOL))
7216 {
7217 char_u *ptr;
7218
7219 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7220 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7221 ++curwin->w_cursor.col;
7222 }
7223 curwin->w_set_curswant = TRUE;
7224 }
7225}
7226
7227/*
7228 * oneright oneleft cursor_down cursor_up
7229 *
7230 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007231 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 * Return OK when successful, FAIL when we hit a line of file boundary.
7233 */
7234
7235 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007236oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
7238 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
7241#ifdef FEAT_VIRTUALEDIT
7242 if (virtual_active())
7243 {
7244 pos_T prevpos = curwin->w_cursor;
7245
7246 /* Adjust for multi-wide char (excluding TAB) */
7247 ptr = ml_get_cursor();
7248 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007249# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007251# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 ))
7255 ? ptr2cells(ptr) : 1));
7256 curwin->w_set_curswant = TRUE;
7257 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7258 return (prevpos.col != curwin->w_cursor.col
7259 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7260 }
7261#endif
7262
7263 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007264 if (*ptr == NUL)
7265 return FAIL; /* already at the very end */
7266
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007268 if (has_mbyte)
7269 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 else
7271#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007272 l = 1;
7273
7274 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7275 * contains "onemore". */
7276 if (ptr[l] == NUL
7277#ifdef FEAT_VIRTUALEDIT
7278 && (ve_flags & VE_ONEMORE) == 0
7279#endif
7280 )
7281 return FAIL;
7282 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
7284 curwin->w_set_curswant = TRUE;
7285 return OK;
7286}
7287
7288 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007289oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290{
7291#ifdef FEAT_VIRTUALEDIT
7292 if (virtual_active())
7293 {
7294 int width;
7295 int v = getviscol();
7296
7297 if (v == 0)
7298 return FAIL;
7299
7300# ifdef FEAT_LINEBREAK
7301 /* We might get stuck on 'showbreak', skip over it. */
7302 width = 1;
7303 for (;;)
7304 {
7305 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007306 /* getviscol() is slow, skip it when 'showbreak' is empty,
7307 * 'breakindent' is not set and there are no multi-byte
7308 * characters */
7309 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310# ifdef FEAT_MBYTE
7311 && !has_mbyte
7312# endif
7313 ) || getviscol() < v)
7314 break;
7315 ++width;
7316 }
7317# else
7318 coladvance(v - 1);
7319# endif
7320
7321 if (curwin->w_cursor.coladd == 1)
7322 {
7323 char_u *ptr;
7324
7325 /* Adjust for multi-wide char (not a TAB) */
7326 ptr = ml_get_cursor();
7327 if (*ptr != TAB && vim_isprintc(
7328# ifdef FEAT_MBYTE
7329 (*mb_ptr2char)(ptr)
7330# else
7331 *ptr
7332# endif
7333 ) && ptr2cells(ptr) > 1)
7334 curwin->w_cursor.coladd = 0;
7335 }
7336
7337 curwin->w_set_curswant = TRUE;
7338 return OK;
7339 }
7340#endif
7341
7342 if (curwin->w_cursor.col == 0)
7343 return FAIL;
7344
7345 curwin->w_set_curswant = TRUE;
7346 --curwin->w_cursor.col;
7347
7348#ifdef FEAT_MBYTE
7349 /* if the character on the left of the current cursor is a multi-byte
7350 * character, move to its first byte */
7351 if (has_mbyte)
7352 mb_adjust_cursor();
7353#endif
7354 return OK;
7355}
7356
7357 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007358cursor_up(
7359 long n,
7360 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
7362 linenr_T lnum;
7363
7364 if (n > 0)
7365 {
7366 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007367 /* This fails if the cursor is already in the first line or the count
7368 * is larger than the line number and '-' is in 'cpoptions' */
7369 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 return FAIL;
7371 if (n >= lnum)
7372 lnum = 1;
7373 else
7374#ifdef FEAT_FOLDING
7375 if (hasAnyFolding(curwin))
7376 {
7377 /*
7378 * Count each sequence of folded lines as one logical line.
7379 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007380 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 (void)hasFolding(lnum, &lnum, NULL);
7382
7383 while (n--)
7384 {
7385 /* move up one line */
7386 --lnum;
7387 if (lnum <= 1)
7388 break;
7389 /* If we entered a fold, move to the beginning, unless in
7390 * Insert mode or when 'foldopen' contains "all": it will open
7391 * in a moment. */
7392 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7393 (void)hasFolding(lnum, &lnum, NULL);
7394 }
7395 if (lnum < 1)
7396 lnum = 1;
7397 }
7398 else
7399#endif
7400 lnum -= n;
7401 curwin->w_cursor.lnum = lnum;
7402 }
7403
7404 /* try to advance to the column we want to be at */
7405 coladvance(curwin->w_curswant);
7406
7407 if (upd_topline)
7408 update_topline(); /* make sure curwin->w_topline is valid */
7409
7410 return OK;
7411}
7412
7413/*
7414 * Cursor down a number of logical lines.
7415 */
7416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007417cursor_down(
7418 long n,
7419 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
7421 linenr_T lnum;
7422
7423 if (n > 0)
7424 {
7425 lnum = curwin->w_cursor.lnum;
7426#ifdef FEAT_FOLDING
7427 /* Move to last line of fold, will fail if it's the end-of-file. */
7428 (void)hasFolding(lnum, NULL, &lnum);
7429#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007430 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007431 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007432 if (lnum >= curbuf->b_ml.ml_line_count
7433 || (lnum + n > curbuf->b_ml.ml_line_count
7434 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 return FAIL;
7436 if (lnum + n >= curbuf->b_ml.ml_line_count)
7437 lnum = curbuf->b_ml.ml_line_count;
7438 else
7439#ifdef FEAT_FOLDING
7440 if (hasAnyFolding(curwin))
7441 {
7442 linenr_T last;
7443
7444 /* count each sequence of folded lines as one logical line */
7445 while (n--)
7446 {
7447 if (hasFolding(lnum, NULL, &last))
7448 lnum = last + 1;
7449 else
7450 ++lnum;
7451 if (lnum >= curbuf->b_ml.ml_line_count)
7452 break;
7453 }
7454 if (lnum > curbuf->b_ml.ml_line_count)
7455 lnum = curbuf->b_ml.ml_line_count;
7456 }
7457 else
7458#endif
7459 lnum += n;
7460 curwin->w_cursor.lnum = lnum;
7461 }
7462
7463 /* try to advance to the column we want to be at */
7464 coladvance(curwin->w_curswant);
7465
7466 if (upd_topline)
7467 update_topline(); /* make sure curwin->w_topline is valid */
7468
7469 return OK;
7470}
7471
7472/*
7473 * Stuff the last inserted text in the read buffer.
7474 * Last_insert actually is a copy of the redo buffer, so we
7475 * first have to remove the command.
7476 */
7477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007478stuff_inserted(
7479 int c, /* Command character to be inserted */
7480 long count, /* Repeat this many times */
7481 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482{
7483 char_u *esc_ptr;
7484 char_u *ptr;
7485 char_u *last_ptr;
7486 char_u last = NUL;
7487
7488 ptr = get_last_insert();
7489 if (ptr == NULL)
7490 {
7491 EMSG(_(e_noinstext));
7492 return FAIL;
7493 }
7494
7495 /* may want to stuff the command character, to start Insert mode */
7496 if (c != NUL)
7497 stuffcharReadbuff(c);
7498 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7499 *esc_ptr = NUL; /* remove the ESC */
7500
7501 /* when the last char is either "0" or "^" it will be quoted if no ESC
7502 * comes after it OR if it will inserted more than once and "ptr"
7503 * starts with ^D. -- Acevedo
7504 */
7505 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7506 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7507 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7508 {
7509 last = *last_ptr;
7510 *last_ptr = NUL;
7511 }
7512
7513 do
7514 {
7515 stuffReadbuff(ptr);
7516 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7517 if (last)
7518 stuffReadbuff((char_u *)(last == '0'
7519 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7520 : IF_EB("\026^", CTRL_V_STR "^")));
7521 }
7522 while (--count > 0);
7523
7524 if (last)
7525 *last_ptr = last;
7526
7527 if (esc_ptr != NULL)
7528 *esc_ptr = ESC; /* put the ESC back */
7529
7530 /* may want to stuff a trailing ESC, to get out of Insert mode */
7531 if (!no_esc)
7532 stuffcharReadbuff(ESC);
7533
7534 return OK;
7535}
7536
7537 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007538get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539{
7540 if (last_insert == NULL)
7541 return NULL;
7542 return last_insert + last_insert_skip;
7543}
7544
7545/*
7546 * Get last inserted string, and remove trailing <Esc>.
7547 * Returns pointer to allocated memory (must be freed) or NULL.
7548 */
7549 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007550get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551{
7552 char_u *s;
7553 int len;
7554
7555 if (last_insert == NULL)
7556 return NULL;
7557 s = vim_strsave(last_insert + last_insert_skip);
7558 if (s != NULL)
7559 {
7560 len = (int)STRLEN(s);
7561 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7562 s[len - 1] = NUL;
7563 }
7564 return s;
7565}
7566
7567/*
7568 * Check the word in front of the cursor for an abbreviation.
7569 * Called when the non-id character "c" has been entered.
7570 * When an abbreviation is recognized it is removed from the text and
7571 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7572 */
7573 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007574echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
7576 /* Don't check for abbreviation in paste mode, when disabled and just
7577 * after moving around with cursor keys. */
7578 if (p_paste || no_abbr || arrow_used)
7579 return FALSE;
7580
7581 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7582 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7583}
7584
7585/*
7586 * replace-stack functions
7587 *
7588 * When replacing characters, the replaced characters are remembered for each
7589 * new character. This is used to re-insert the old text when backspacing.
7590 *
7591 * There is a NUL headed list of characters for each character that is
7592 * currently in the file after the insertion point. When BS is used, one NUL
7593 * headed list is put back for the deleted character.
7594 *
7595 * For a newline, there are two NUL headed lists. One contains the characters
7596 * that the NL replaced. The extra one stores the characters after the cursor
7597 * that were deleted (always white space).
7598 *
7599 * Replace_offset is normally 0, in which case replace_push will add a new
7600 * character at the end of the stack. If replace_offset is not 0, that many
7601 * characters will be left on the stack above the newly inserted character.
7602 */
7603
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007604static char_u *replace_stack = NULL;
7605static long replace_stack_nr = 0; /* next entry in replace stack */
7606static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607
7608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007609replace_push(
7610 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611{
7612 char_u *p;
7613
7614 if (replace_stack_nr < replace_offset) /* nothing to do */
7615 return;
7616 if (replace_stack_len <= replace_stack_nr)
7617 {
7618 replace_stack_len += 50;
7619 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7620 if (p == NULL) /* out of memory */
7621 {
7622 replace_stack_len -= 50;
7623 return;
7624 }
7625 if (replace_stack != NULL)
7626 {
7627 mch_memmove(p, replace_stack,
7628 (size_t)(replace_stack_nr * sizeof(char_u)));
7629 vim_free(replace_stack);
7630 }
7631 replace_stack = p;
7632 }
7633 p = replace_stack + replace_stack_nr - replace_offset;
7634 if (replace_offset)
7635 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7636 *p = c;
7637 ++replace_stack_nr;
7638}
7639
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007640#if defined(FEAT_MBYTE) || defined(PROTO)
7641/*
7642 * Push a character onto the replace stack. Handles a multi-byte character in
7643 * reverse byte order, so that the first byte is popped off first.
7644 * Return the number of bytes done (includes composing characters).
7645 */
7646 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007647replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007648{
7649 int l = (*mb_ptr2len)(p);
7650 int j;
7651
7652 for (j = l - 1; j >= 0; --j)
7653 replace_push(p[j]);
7654 return l;
7655}
7656#endif
7657
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658/*
7659 * Pop one item from the replace stack.
7660 * return -1 if stack empty
7661 * return replaced character or NUL otherwise
7662 */
7663 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007664replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665{
7666 if (replace_stack_nr == 0)
7667 return -1;
7668 return (int)replace_stack[--replace_stack_nr];
7669}
7670
7671/*
7672 * Join the top two items on the replace stack. This removes to "off"'th NUL
7673 * encountered.
7674 */
7675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007676replace_join(
7677 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679 int i;
7680
7681 for (i = replace_stack_nr; --i >= 0; )
7682 if (replace_stack[i] == NUL && off-- <= 0)
7683 {
7684 --replace_stack_nr;
7685 mch_memmove(replace_stack + i, replace_stack + i + 1,
7686 (size_t)(replace_stack_nr - i));
7687 return;
7688 }
7689}
7690
7691/*
7692 * Pop bytes from the replace stack until a NUL is found, and insert them
7693 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7694 */
7695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007696replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697{
7698 int cc;
7699 int oldState = State;
7700
7701 State = NORMAL; /* don't want REPLACE here */
7702 while ((cc = replace_pop()) > 0)
7703 {
7704#ifdef FEAT_MBYTE
7705 mb_replace_pop_ins(cc);
7706#else
7707 ins_char(cc);
7708#endif
7709 dec_cursor();
7710 }
7711 State = oldState;
7712}
7713
7714#ifdef FEAT_MBYTE
7715/*
7716 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7717 * indicates a multi-byte char, pop the other bytes too.
7718 */
7719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007720mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
7722 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007723 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 int i;
7725 int c;
7726
7727 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7728 {
7729 buf[0] = cc;
7730 for (i = 1; i < n; ++i)
7731 buf[i] = replace_pop();
7732 ins_bytes_len(buf, n);
7733 }
7734 else
7735 ins_char(cc);
7736
7737 if (enc_utf8)
7738 /* Handle composing chars. */
7739 for (;;)
7740 {
7741 c = replace_pop();
7742 if (c == -1) /* stack empty */
7743 break;
7744 if ((n = MB_BYTE2LEN(c)) == 1)
7745 {
7746 /* Not a multi-byte char, put it back. */
7747 replace_push(c);
7748 break;
7749 }
7750 else
7751 {
7752 buf[0] = c;
7753 for (i = 1; i < n; ++i)
7754 buf[i] = replace_pop();
7755 if (utf_iscomposing(utf_ptr2char(buf)))
7756 ins_bytes_len(buf, n);
7757 else
7758 {
7759 /* Not a composing char, put it back. */
7760 for (i = n - 1; i >= 0; --i)
7761 replace_push(buf[i]);
7762 break;
7763 }
7764 }
7765 }
7766}
7767#endif
7768
7769/*
7770 * make the replace stack empty
7771 * (called when exiting replace mode)
7772 */
7773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007774replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
7776 vim_free(replace_stack);
7777 replace_stack = NULL;
7778 replace_stack_len = 0;
7779 replace_stack_nr = 0;
7780}
7781
7782/*
7783 * Handle doing a BS for one character.
7784 * cc < 0: replace stack empty, just move cursor
7785 * cc == 0: character was inserted, delete it
7786 * cc > 0: character was replaced, put cc (first byte of original char) back
7787 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007788 * When "limit_col" is >= 0, don't delete before this column. Matters when
7789 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 */
7791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007792replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 int cc;
7795#ifdef FEAT_VREPLACE
7796 int orig_len = 0;
7797 int ins_len;
7798 int orig_vcols = 0;
7799 colnr_T start_vcol;
7800 char_u *p;
7801 int i;
7802 int vcol;
7803#endif
7804
7805 cc = replace_pop();
7806 if (cc > 0)
7807 {
7808#ifdef FEAT_VREPLACE
7809 if (State & VREPLACE_FLAG)
7810 {
7811 /* Get the number of screen cells used by the character we are
7812 * going to delete. */
7813 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7814 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7815 }
7816#endif
7817#ifdef FEAT_MBYTE
7818 if (has_mbyte)
7819 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007820 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821# ifdef FEAT_VREPLACE
7822 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007823 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824# endif
7825 replace_push(cc);
7826 }
7827 else
7828#endif
7829 {
7830 pchar_cursor(cc);
7831#ifdef FEAT_VREPLACE
7832 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007833 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834#endif
7835 }
7836 replace_pop_ins();
7837
7838#ifdef FEAT_VREPLACE
7839 if (State & VREPLACE_FLAG)
7840 {
7841 /* Get the number of screen cells used by the inserted characters */
7842 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007843 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 vcol = start_vcol;
7845 for (i = 0; i < ins_len; ++i)
7846 {
7847 vcol += chartabsize(p + i, vcol);
7848#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007849 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850#endif
7851 }
7852 vcol -= start_vcol;
7853
7854 /* Delete spaces that were inserted after the cursor to keep the
7855 * text aligned. */
7856 curwin->w_cursor.col += ins_len;
7857 while (vcol > orig_vcols && gchar_cursor() == ' ')
7858 {
7859 del_char(FALSE);
7860 ++orig_vcols;
7861 }
7862 curwin->w_cursor.col -= ins_len;
7863 }
7864#endif
7865
7866 /* mark the buffer as changed and prepare for displaying */
7867 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7868 }
7869 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007870 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871}
7872
7873#ifdef FEAT_CINDENT
7874/*
7875 * Return TRUE if C-indenting is on.
7876 */
7877 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007878cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
7880 return (!p_paste && (curbuf->b_p_cin
7881# ifdef FEAT_EVAL
7882 || *curbuf->b_p_inde != NUL
7883# endif
7884 ));
7885}
7886#endif
7887
7888#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7889/*
7890 * Re-indent the current line, based on the current contents of it and the
7891 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7892 * confused what all the part that handles Control-T is doing that I'm not.
7893 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7894 */
7895
7896 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007897fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007899 int amount = get_the_indent();
7900
7901 if (amount >= 0)
7902 {
7903 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7904 if (linewhite(curwin->w_cursor.lnum))
7905 did_ai = TRUE; /* delete the indent if the line stays empty */
7906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907}
7908
7909 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007910fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 if (p_paste)
7913 return;
7914# ifdef FEAT_LISP
7915 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7916 fixthisline(get_lisp_indent);
7917# endif
7918# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7919 else
7920# endif
7921# ifdef FEAT_CINDENT
7922 if (cindent_on())
7923 do_c_expr_indent();
7924# endif
7925}
7926
7927#endif
7928
7929#ifdef FEAT_CINDENT
7930/*
7931 * return TRUE if 'cinkeys' contains the key "keytyped",
7932 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007933 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7935 *
7936 * "keytyped" can have a few special values:
7937 * KEY_OPEN_FORW
7938 * KEY_OPEN_BACK
7939 * KEY_COMPLETE just finished completion.
7940 *
7941 * If line_is_empty is TRUE accept keys with '0' before them.
7942 */
7943 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007944in_cinkeys(
7945 int keytyped,
7946 int when,
7947 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948{
7949 char_u *look;
7950 int try_match;
7951 int try_match_word;
7952 char_u *p;
7953 char_u *line;
7954 int icase;
7955 int i;
7956
Bram Moolenaar30848942009-12-24 14:46:12 +00007957 if (keytyped == NUL)
7958 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7959 return FALSE;
7960
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961#ifdef FEAT_EVAL
7962 if (*curbuf->b_p_inde != NUL)
7963 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7964 else
7965#endif
7966 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7967 while (*look)
7968 {
7969 /*
7970 * Find out if we want to try a match with this key, depending on
7971 * 'when' and a '*' or '!' before the key.
7972 */
7973 switch (when)
7974 {
7975 case '*': try_match = (*look == '*'); break;
7976 case '!': try_match = (*look == '!'); break;
7977 default: try_match = (*look != '*'); break;
7978 }
7979 if (*look == '*' || *look == '!')
7980 ++look;
7981
7982 /*
7983 * If there is a '0', only accept a match if the line is empty.
7984 * But may still match when typing last char of a word.
7985 */
7986 if (*look == '0')
7987 {
7988 try_match_word = try_match;
7989 if (!line_is_empty)
7990 try_match = FALSE;
7991 ++look;
7992 }
7993 else
7994 try_match_word = FALSE;
7995
7996 /*
7997 * does it look like a control character?
7998 */
7999 if (*look == '^'
8000#ifdef EBCDIC
8001 && (Ctrl_chr(look[1]) != 0)
8002#else
8003 && look[1] >= '?' && look[1] <= '_'
8004#endif
8005 )
8006 {
8007 if (try_match && keytyped == Ctrl_chr(look[1]))
8008 return TRUE;
8009 look += 2;
8010 }
8011 /*
8012 * 'o' means "o" command, open forward.
8013 * 'O' means "O" command, open backward.
8014 */
8015 else if (*look == 'o')
8016 {
8017 if (try_match && keytyped == KEY_OPEN_FORW)
8018 return TRUE;
8019 ++look;
8020 }
8021 else if (*look == 'O')
8022 {
8023 if (try_match && keytyped == KEY_OPEN_BACK)
8024 return TRUE;
8025 ++look;
8026 }
8027
8028 /*
8029 * 'e' means to check for "else" at start of line and just before the
8030 * cursor.
8031 */
8032 else if (*look == 'e')
8033 {
8034 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8035 {
8036 p = ml_get_curline();
8037 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8038 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8039 return TRUE;
8040 }
8041 ++look;
8042 }
8043
8044 /*
8045 * ':' only causes an indent if it is at the end of a label or case
8046 * statement, or when it was before typing the ':' (to fix
8047 * class::method for C++).
8048 */
8049 else if (*look == ':')
8050 {
8051 if (try_match && keytyped == ':')
8052 {
8053 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008054 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008056 /* Need to get the line again after cin_islabel(). */
8057 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 if (curwin->w_cursor.col > 2
8059 && p[curwin->w_cursor.col - 1] == ':'
8060 && p[curwin->w_cursor.col - 2] == ':')
8061 {
8062 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008063 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008064 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 p = ml_get_curline();
8066 p[curwin->w_cursor.col - 1] = ':';
8067 if (i)
8068 return TRUE;
8069 }
8070 }
8071 ++look;
8072 }
8073
8074
8075 /*
8076 * Is it a key in <>, maybe?
8077 */
8078 else if (*look == '<')
8079 {
8080 if (try_match)
8081 {
8082 /*
8083 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8084 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8085 * >, *, : and ! keys if they really really want to.
8086 */
8087 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8088 && keytyped == look[1])
8089 return TRUE;
8090
8091 if (keytyped == get_special_key_code(look + 1))
8092 return TRUE;
8093 }
8094 while (*look && *look != '>')
8095 look++;
8096 while (*look == '>')
8097 look++;
8098 }
8099
8100 /*
8101 * Is it a word: "=word"?
8102 */
8103 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8104 {
8105 ++look;
8106 if (*look == '~')
8107 {
8108 icase = TRUE;
8109 ++look;
8110 }
8111 else
8112 icase = FALSE;
8113 p = vim_strchr(look, ',');
8114 if (p == NULL)
8115 p = look + STRLEN(look);
8116 if ((try_match || try_match_word)
8117 && curwin->w_cursor.col >= (colnr_T)(p - look))
8118 {
8119 int match = FALSE;
8120
8121#ifdef FEAT_INS_EXPAND
8122 if (keytyped == KEY_COMPLETE)
8123 {
8124 char_u *s;
8125
8126 /* Just completed a word, check if it starts with "look".
8127 * search back for the start of a word. */
8128 line = ml_get_curline();
8129# ifdef FEAT_MBYTE
8130 if (has_mbyte)
8131 {
8132 char_u *n;
8133
8134 for (s = line + curwin->w_cursor.col; s > line; s = n)
8135 {
8136 n = mb_prevptr(line, s);
8137 if (!vim_iswordp(n))
8138 break;
8139 }
8140 }
8141 else
8142# endif
8143 for (s = line + curwin->w_cursor.col; s > line; --s)
8144 if (!vim_iswordc(s[-1]))
8145 break;
8146 if (s + (p - look) <= line + curwin->w_cursor.col
8147 && (icase
8148 ? MB_STRNICMP(s, look, p - look)
8149 : STRNCMP(s, look, p - look)) == 0)
8150 match = TRUE;
8151 }
8152 else
8153#endif
8154 /* TODO: multi-byte */
8155 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8156 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8157 {
8158 line = ml_get_cursor();
8159 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8160 || !vim_iswordc(line[-(p - look) - 1]))
8161 && (icase
8162 ? MB_STRNICMP(line - (p - look), look, p - look)
8163 : STRNCMP(line - (p - look), look, p - look))
8164 == 0)
8165 match = TRUE;
8166 }
8167 if (match && try_match_word && !try_match)
8168 {
8169 /* "0=word": Check if there are only blanks before the
8170 * word. */
8171 line = ml_get_curline();
8172 if ((int)(skipwhite(line) - line) !=
8173 (int)(curwin->w_cursor.col - (p - look)))
8174 match = FALSE;
8175 }
8176 if (match)
8177 return TRUE;
8178 }
8179 look = p;
8180 }
8181
8182 /*
8183 * ok, it's a boring generic character.
8184 */
8185 else
8186 {
8187 if (try_match && *look == keytyped)
8188 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008189 if (*look != NUL)
8190 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 }
8192
8193 /*
8194 * Skip over ", ".
8195 */
8196 look = skip_to_option_part(look);
8197 }
8198 return FALSE;
8199}
8200#endif /* FEAT_CINDENT */
8201
8202#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8203/*
8204 * Map Hebrew keyboard when in hkmap mode.
8205 */
8206 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008207hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208{
8209 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8210 {
8211 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8212 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8213 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8214 static char_u map[26] =
8215 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8216 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8217 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8218 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8219 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8220 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8221 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8222 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8223 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8224
8225 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8226 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8227 /* '-1'='sofit' */
8228 else if (c == 'x')
8229 return 'X';
8230 else if (c == 'q')
8231 return '\''; /* {geresh}={'} */
8232 else if (c == 246)
8233 return ' '; /* \"o --> ' ' for a german keyboard */
8234 else if (c == 228)
8235 return ' '; /* \"a --> ' ' -- / -- */
8236 else if (c == 252)
8237 return ' '; /* \"u --> ' ' -- / -- */
8238#ifdef EBCDIC
8239 else if (islower(c))
8240#else
8241 /* NOTE: islower() does not do the right thing for us on Linux so we
8242 * do this the same was as 5.7 and previous, so it works correctly on
8243 * all systems. Specifically, the e.g. Delete and Arrow keys are
8244 * munged and won't work if e.g. searching for Hebrew text.
8245 */
8246 else if (c >= 'a' && c <= 'z')
8247#endif
8248 return (int)(map[CharOrdLow(c)] + p_aleph);
8249 else
8250 return c;
8251 }
8252 else
8253 {
8254 switch (c)
8255 {
8256 case '`': return ';';
8257 case '/': return '.';
8258 case '\'': return ',';
8259 case 'q': return '/';
8260 case 'w': return '\'';
8261
8262 /* Hebrew letters - set offset from 'a' */
8263 case ',': c = '{'; break;
8264 case '.': c = 'v'; break;
8265 case ';': c = 't'; break;
8266 default: {
8267 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8268
8269#ifdef EBCDIC
8270 /* see note about islower() above */
8271 if (!islower(c))
8272#else
8273 if (c < 'a' || c > 'z')
8274#endif
8275 return c;
8276 c = str[CharOrdLow(c)];
8277 break;
8278 }
8279 }
8280
8281 return (int)(CharOrdLow(c) + p_aleph);
8282 }
8283}
8284#endif
8285
8286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008287ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
8289 int need_redraw = FALSE;
8290 int regname;
8291 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008292 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293
8294 /*
8295 * If we are going to wait for a character, show a '"'.
8296 */
8297 pc_status = PC_STATUS_UNSET;
8298 if (redrawing() && !char_avail())
8299 {
8300 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008301 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302
8303 edit_putchar('"', TRUE);
8304#ifdef FEAT_CMDL_INFO
8305 add_to_showcmd_c(Ctrl_R);
8306#endif
8307 }
8308
8309#ifdef USE_ON_FLY_SCROLL
8310 dont_scroll = TRUE; /* disallow scrolling here */
8311#endif
8312
8313 /*
8314 * Don't map the register name. This also prevents the mode message to be
8315 * deleted when ESC is hit.
8316 */
8317 ++no_mapping;
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 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8321 {
8322 /* Get a third key for literal register insertion */
8323 literally = regname;
8324#ifdef FEAT_CMDL_INFO
8325 add_to_showcmd_c(literally);
8326#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008327 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 }
8330 --no_mapping;
8331
8332#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008333 /* Don't call u_sync() while typing the expression or giving an error
8334 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 ++no_u_sync;
8336 if (regname == '=')
8337 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008338# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008340# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008341 /* Sync undo when evaluating the expression calls setline() or
8342 * append(), so that it can be undone separately. */
8343 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008344
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008346# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 /* Restore the Input Method. */
8348 if (im_on)
8349 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008352 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8353 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008354 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 else
8358 {
8359#endif
8360 if (literally == Ctrl_O || literally == Ctrl_P)
8361 {
8362 /* Append the command to the redo buffer. */
8363 AppendCharToRedobuff(Ctrl_R);
8364 AppendCharToRedobuff(literally);
8365 AppendCharToRedobuff(regname);
8366
8367 do_put(regname, BACKWARD, 1L,
8368 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8369 }
8370 else if (insert_reg(regname, literally) == FAIL)
8371 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008372 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 need_redraw = TRUE; /* remove the '"' */
8374 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008375 else if (stop_insert_mode)
8376 /* When the '=' register was used and a function was invoked that
8377 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8378 * insert anything, need to remove the '"' */
8379 need_redraw = TRUE;
8380
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381#ifdef FEAT_EVAL
8382 }
8383 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008384 if (u_sync_once == 1)
8385 ins_need_undo = TRUE;
8386 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387#endif
8388#ifdef FEAT_CMDL_INFO
8389 clear_showcmd();
8390#endif
8391
8392 /* If the inserted register is empty, we need to remove the '"' */
8393 if (need_redraw || stuff_empty())
8394 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008395
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008396 /* Disallow starting Visual mode here, would get a weird mode. */
8397 if (!vis_active && VIsual_active)
8398 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399}
8400
8401/*
8402 * CTRL-G commands in Insert mode.
8403 */
8404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008405ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
8407 int c;
8408
8409#ifdef FEAT_INS_EXPAND
8410 /* Right after CTRL-X the cursor will be after the ruler. */
8411 setcursor();
8412#endif
8413
8414 /*
8415 * Don't map the second key. This also prevents the mode message to be
8416 * deleted when ESC is hit.
8417 */
8418 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008419 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 --no_mapping;
8421 switch (c)
8422 {
8423 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8424 case K_UP:
8425 case Ctrl_K:
8426 case 'k': ins_up(TRUE);
8427 break;
8428
8429 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8430 case K_DOWN:
8431 case Ctrl_J:
8432 case 'j': ins_down(TRUE);
8433 break;
8434
8435 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008436 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008438
8439 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008440 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008441 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008442 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 break;
8444
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008445 /* CTRL-G U: do not break undo with the next char */
8446 case 'U':
8447 /* Allow one left/right cursor movement with the next char,
8448 * without breaking undo. */
8449 dont_sync_undo = MAYBE;
8450 break;
8451
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008453 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 }
8455}
8456
8457/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008458 * CTRL-^ in Insert mode.
8459 */
8460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008461ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008462{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008463 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008464 {
8465 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8466 if (State & LANGMAP)
8467 {
8468 curbuf->b_p_iminsert = B_IMODE_NONE;
8469 State &= ~LANGMAP;
8470 }
8471 else
8472 {
8473 curbuf->b_p_iminsert = B_IMODE_LMAP;
8474 State |= LANGMAP;
8475#ifdef USE_IM_CONTROL
8476 im_set_active(FALSE);
8477#endif
8478 }
8479 }
8480#ifdef USE_IM_CONTROL
8481 else
8482 {
8483 /* There are no ":lmap" mappings, toggle IM */
8484 if (im_get_status())
8485 {
8486 curbuf->b_p_iminsert = B_IMODE_NONE;
8487 im_set_active(FALSE);
8488 }
8489 else
8490 {
8491 curbuf->b_p_iminsert = B_IMODE_IM;
8492 State &= ~LANGMAP;
8493 im_set_active(TRUE);
8494 }
8495 }
8496#endif
8497 set_iminsert_global();
8498 showmode();
8499#ifdef FEAT_GUI
8500 /* may show different cursor shape or color */
8501 if (gui.in_use)
8502 gui_update_cursor(TRUE, FALSE);
8503#endif
8504#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8505 /* Show/unshow value of 'keymap' in status lines. */
8506 status_redraw_curbuf();
8507#endif
8508}
8509
8510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 * Handle ESC in insert mode.
8512 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8513 * insert.
8514 */
8515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008516ins_esc(
8517 long *count,
8518 int cmdchar,
8519 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520{
8521 int temp;
8522 static int disabled_redraw = FALSE;
8523
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008524#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008525 check_spell_redraw();
8526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527#if defined(FEAT_HANGULIN)
8528# if defined(ESC_CHG_TO_ENG_MODE)
8529 hangul_input_state_set(0);
8530# endif
8531 if (composing_hangul)
8532 {
8533 push_raw_key(composing_hangul_buffer, 2);
8534 composing_hangul = 0;
8535 }
8536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537
8538 temp = curwin->w_cursor.col;
8539 if (disabled_redraw)
8540 {
8541 --RedrawingDisabled;
8542 disabled_redraw = FALSE;
8543 }
8544 if (!arrow_used)
8545 {
8546 /*
8547 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008548 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8549 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 */
8551 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008552 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553
8554 /*
8555 * Repeating insert may take a long time. Check for
8556 * interrupt now and then.
8557 */
8558 if (*count > 0)
8559 {
8560 line_breakcheck();
8561 if (got_int)
8562 *count = 0;
8563 }
8564
8565 if (--*count > 0) /* repeat what was typed */
8566 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008567 /* Vi repeats the insert without replacing characters. */
8568 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8569 State &= ~REPLACE_FLAG;
8570
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 (void)start_redo_ins();
8572 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008573 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 ++RedrawingDisabled;
8575 disabled_redraw = TRUE;
8576 return FALSE; /* repeat the insert */
8577 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008578 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 undisplay_dollar();
8580 }
8581
8582 /* When an autoindent was removed, curswant stays after the
8583 * indent */
8584 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8585 curwin->w_set_curswant = TRUE;
8586
8587 /* Remember the last Insert position in the '^ mark. */
8588 if (!cmdmod.keepjumps)
8589 curbuf->b_last_insert = curwin->w_cursor;
8590
8591 /*
8592 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008593 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008595 if (!nomove
8596 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597#ifdef FEAT_VIRTUALEDIT
8598 || curwin->w_cursor.coladd > 0
8599#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008600 )
8601 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008602 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603#ifdef FEAT_RIGHTLEFT
8604 && !revins_on
8605#endif
8606 )
8607 {
8608#ifdef FEAT_VIRTUALEDIT
8609 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8610 {
8611 oneleft();
8612 if (restart_edit != NUL)
8613 ++curwin->w_cursor.coladd;
8614 }
8615 else
8616#endif
8617 {
8618 --curwin->w_cursor.col;
8619#ifdef FEAT_MBYTE
8620 /* Correct cursor for multi-byte character. */
8621 if (has_mbyte)
8622 mb_adjust_cursor();
8623#endif
8624 }
8625 }
8626
8627#ifdef USE_IM_CONTROL
8628 /* Disable IM to allow typing English directly for Normal mode commands.
8629 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8630 * well). */
8631 if (!(State & LANGMAP))
8632 im_save_status(&curbuf->b_p_iminsert);
8633 im_set_active(FALSE);
8634#endif
8635
8636 State = NORMAL;
8637 /* need to position cursor again (e.g. when on a TAB ) */
8638 changed_cline_bef_curs();
8639
8640#ifdef FEAT_MOUSE
8641 setmouse();
8642#endif
8643#ifdef CURSOR_SHAPE
8644 ui_cursor_shape(); /* may show different cursor shape */
8645#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008646 if (!p_ek)
8647 /* Re-enable bracketed paste mode. */
8648 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649
8650 /*
8651 * When recording or for CTRL-O, need to display the new mode.
8652 * Otherwise remove the mode message.
8653 */
8654 if (Recording || restart_edit != NUL)
8655 showmode();
8656 else if (p_smd)
8657 MSG("");
8658
8659 return TRUE; /* exit Insert mode */
8660}
8661
8662#ifdef FEAT_RIGHTLEFT
8663/*
8664 * Toggle language: hkmap and revins_on.
8665 * Move to end of reverse inserted text.
8666 */
8667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008668ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669{
8670 if (revins_on && revins_chars && revins_scol >= 0)
8671 {
8672 while (gchar_cursor() != NUL && revins_chars--)
8673 ++curwin->w_cursor.col;
8674 }
8675 p_ri = !p_ri;
8676 revins_on = (State == INSERT && p_ri);
8677 if (revins_on)
8678 {
8679 revins_scol = curwin->w_cursor.col;
8680 revins_legal++;
8681 revins_chars = 0;
8682 undisplay_dollar();
8683 }
8684 else
8685 revins_scol = -1;
8686#ifdef FEAT_FKMAP
8687 if (p_altkeymap)
8688 {
8689 /*
8690 * to be consistent also for redo command, using '.'
8691 * set arrow_used to true and stop it - causing to redo
8692 * characters entered in one mode (normal/reverse insert).
8693 */
8694 arrow_used = TRUE;
8695 (void)stop_arrow();
8696 p_fkmap = curwin->w_p_rl ^ p_ri;
8697 if (p_fkmap && p_ri)
8698 State = INSERT;
8699 }
8700 else
8701#endif
8702 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8703 showmode();
8704}
8705#endif
8706
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707/*
8708 * If 'keymodel' contains "startsel", may start selection.
8709 * Returns TRUE when a CTRL-O and other keys stuffed.
8710 */
8711 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008712ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713{
8714 if (km_startsel)
8715 switch (c)
8716 {
8717 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 case K_PAGEUP:
8720 case K_KPAGEUP:
8721 case K_PAGEDOWN:
8722 case K_KPAGEDOWN:
8723# ifdef MACOS
8724 case K_LEFT:
8725 case K_RIGHT:
8726 case K_UP:
8727 case K_DOWN:
8728 case K_END:
8729 case K_HOME:
8730# endif
8731 if (!(mod_mask & MOD_MASK_SHIFT))
8732 break;
8733 /* FALLTHROUGH */
8734 case K_S_LEFT:
8735 case K_S_RIGHT:
8736 case K_S_UP:
8737 case K_S_DOWN:
8738 case K_S_END:
8739 case K_S_HOME:
8740 /* Start selection right away, the cursor can move with
8741 * CTRL-O when beyond the end of the line. */
8742 start_selection();
8743
8744 /* Execute the key in (insert) Select mode. */
8745 stuffcharReadbuff(Ctrl_O);
8746 if (mod_mask)
8747 {
8748 char_u buf[4];
8749
8750 buf[0] = K_SPECIAL;
8751 buf[1] = KS_MODIFIER;
8752 buf[2] = mod_mask;
8753 buf[3] = NUL;
8754 stuffReadbuff(buf);
8755 }
8756 stuffcharReadbuff(c);
8757 return TRUE;
8758 }
8759 return FALSE;
8760}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761
8762/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008763 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008764 */
8765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008766ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008767{
8768#ifdef FEAT_FKMAP
8769 if (p_fkmap && p_ri)
8770 {
8771 beep_flush();
8772 EMSG(farsi_text_3); /* encoded in Farsi */
8773 return;
8774 }
8775#endif
8776
8777#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008778# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008779 set_vim_var_string(VV_INSERTMODE,
8780 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008781# ifdef FEAT_VREPLACE
8782 replaceState == VREPLACE ? "v" :
8783# endif
8784 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008785# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008786 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8787#endif
8788 if (State & REPLACE_FLAG)
8789 State = INSERT | (State & LANGMAP);
8790 else
8791 State = replaceState | (State & LANGMAP);
8792 AppendCharToRedobuff(K_INS);
8793 showmode();
8794#ifdef CURSOR_SHAPE
8795 ui_cursor_shape(); /* may show different cursor shape */
8796#endif
8797}
8798
8799/*
8800 * Pressed CTRL-O in Insert mode.
8801 */
8802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008803ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008804{
8805#ifdef FEAT_VREPLACE
8806 if (State & VREPLACE_FLAG)
8807 restart_edit = 'V';
8808 else
8809#endif
8810 if (State & REPLACE_FLAG)
8811 restart_edit = 'R';
8812 else
8813 restart_edit = 'I';
8814#ifdef FEAT_VIRTUALEDIT
8815 if (virtual_active())
8816 ins_at_eol = FALSE; /* cursor always keeps its column */
8817 else
8818#endif
8819 ins_at_eol = (gchar_cursor() == NUL);
8820}
8821
8822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 * If the cursor is on an indent, ^T/^D insert/delete one
8824 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008825 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 * with vi. But vi only supports ^T and ^D after an
8827 * autoindent, we support it everywhere.
8828 */
8829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008830ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831{
8832 if (stop_arrow() == FAIL)
8833 return;
8834 AppendCharToRedobuff(c);
8835
8836 /*
8837 * 0^D and ^^D: remove all indent.
8838 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008839 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8840 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 {
8842 --curwin->w_cursor.col;
8843 (void)del_char(FALSE); /* delete the '^' or '0' */
8844 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8845 if (State & REPLACE_FLAG)
8846 replace_pop_ins();
8847 if (lastc == '^')
8848 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008849 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 }
8851 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008852 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853
8854 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8855 did_ai = FALSE;
8856#ifdef FEAT_SMARTINDENT
8857 did_si = FALSE;
8858 can_si = FALSE;
8859 can_si_back = FALSE;
8860#endif
8861#ifdef FEAT_CINDENT
8862 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8863#endif
8864}
8865
8866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008867ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
8869 int temp;
8870
8871 if (stop_arrow() == FAIL)
8872 return;
8873 if (gchar_cursor() == NUL) /* delete newline */
8874 {
8875 temp = curwin->w_cursor.col;
8876 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008877 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008878 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 else
8880 curwin->w_cursor.col = temp;
8881 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008882 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8883 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 did_ai = FALSE;
8885#ifdef FEAT_SMARTINDENT
8886 did_si = FALSE;
8887 can_si = FALSE;
8888 can_si_back = FALSE;
8889#endif
8890 AppendCharToRedobuff(K_DEL);
8891}
8892
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008893static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008894
8895/*
8896 * Delete one character for ins_bs().
8897 */
8898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008899ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008900{
8901 dec_cursor();
8902 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8903 if (State & REPLACE_FLAG)
8904 {
8905 /* Don't delete characters before the insert point when in
8906 * Replace mode */
8907 if (curwin->w_cursor.lnum != Insstart.lnum
8908 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008909 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008910 }
8911 else
8912 (void)del_char(FALSE);
8913}
8914
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915/*
8916 * Handle Backspace, delete-word and delete-line in Insert mode.
8917 * Return TRUE when backspace was actually used.
8918 */
8919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008920ins_bs(
8921 int c,
8922 int mode,
8923 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924{
8925 linenr_T lnum;
8926 int cc;
8927 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008928 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 colnr_T mincol;
8930 int did_backspace = FALSE;
8931 int in_indent;
8932 int oldState;
8933#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008934 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935#endif
8936
8937 /*
8938 * can't delete anything in an empty file
8939 * can't backup past first character in buffer
8940 * can't backup past starting point unless 'backspace' > 1
8941 * can backup to a previous line if 'backspace' == 0
8942 */
8943 if ( bufempty()
8944 || (
8945#ifdef FEAT_RIGHTLEFT
8946 !revins_on &&
8947#endif
8948 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8949 || (!can_bs(BS_START)
8950 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008951 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8952 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8954 && curwin->w_cursor.col <= ai_col)
8955 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8956 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008957 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 return FALSE;
8959 }
8960
8961 if (stop_arrow() == FAIL)
8962 return FALSE;
8963 in_indent = inindent(0);
8964#ifdef FEAT_CINDENT
8965 if (in_indent)
8966 can_cindent = FALSE;
8967#endif
8968#ifdef FEAT_COMMENTS
8969 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8970#endif
8971#ifdef FEAT_RIGHTLEFT
8972 if (revins_on) /* put cursor after last inserted char */
8973 inc_cursor();
8974#endif
8975
8976#ifdef FEAT_VIRTUALEDIT
8977 /* Virtualedit:
8978 * BACKSPACE_CHAR eats a virtual space
8979 * BACKSPACE_WORD eats all coladd
8980 * BACKSPACE_LINE eats all coladd and keeps going
8981 */
8982 if (curwin->w_cursor.coladd > 0)
8983 {
8984 if (mode == BACKSPACE_CHAR)
8985 {
8986 --curwin->w_cursor.coladd;
8987 return TRUE;
8988 }
8989 if (mode == BACKSPACE_WORD)
8990 {
8991 curwin->w_cursor.coladd = 0;
8992 return TRUE;
8993 }
8994 curwin->w_cursor.coladd = 0;
8995 }
8996#endif
8997
8998 /*
8999 * delete newline!
9000 */
9001 if (curwin->w_cursor.col == 0)
9002 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009003 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009004 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005#ifdef FEAT_RIGHTLEFT
9006 || revins_on
9007#endif
9008 )
9009 {
9010 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9011 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9012 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009013 --Insstart.lnum;
9014 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 }
9016 /*
9017 * In replace mode:
9018 * cc < 0: NL was inserted, delete it
9019 * cc >= 0: NL was replaced, put original characters back
9020 */
9021 cc = -1;
9022 if (State & REPLACE_FLAG)
9023 cc = replace_pop(); /* returns -1 if NL was inserted */
9024 /*
9025 * In replace mode, in the line we started replacing, we only move the
9026 * cursor.
9027 */
9028 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9029 {
9030 dec_cursor();
9031 }
9032 else
9033 {
9034#ifdef FEAT_VREPLACE
9035 if (!(State & VREPLACE_FLAG)
9036 || curwin->w_cursor.lnum > orig_line_count)
9037#endif
9038 {
9039 temp = gchar_cursor(); /* remember current char */
9040 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009041
9042 /* When "aw" is in 'formatoptions' we must delete the space at
9043 * the end of the line, otherwise the line will be broken
9044 * again when auto-formatting. */
9045 if (has_format_option(FO_AUTO)
9046 && has_format_option(FO_WHITE_PAR))
9047 {
9048 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9049 TRUE);
9050 int len;
9051
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009052 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009053 if (len > 0 && ptr[len - 1] == ' ')
9054 ptr[len - 1] = NUL;
9055 }
9056
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009057 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 if (temp == NUL && gchar_cursor() != NUL)
9059 inc_cursor();
9060 }
9061#ifdef FEAT_VREPLACE
9062 else
9063 dec_cursor();
9064#endif
9065
9066 /*
9067 * In REPLACE mode we have to put back the text that was replaced
9068 * by the NL. On the replace stack is first a NUL-terminated
9069 * sequence of characters that were deleted and then the
9070 * characters that NL replaced.
9071 */
9072 if (State & REPLACE_FLAG)
9073 {
9074 /*
9075 * Do the next ins_char() in NORMAL state, to
9076 * prevent ins_char() from replacing characters and
9077 * avoiding showmatch().
9078 */
9079 oldState = State;
9080 State = NORMAL;
9081 /*
9082 * restore characters (blanks) deleted after cursor
9083 */
9084 while (cc > 0)
9085 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009086 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087#ifdef FEAT_MBYTE
9088 mb_replace_pop_ins(cc);
9089#else
9090 ins_char(cc);
9091#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009092 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 cc = replace_pop();
9094 }
9095 /* restore the characters that NL replaced */
9096 replace_pop_ins();
9097 State = oldState;
9098 }
9099 }
9100 did_ai = FALSE;
9101 }
9102 else
9103 {
9104 /*
9105 * Delete character(s) before the cursor.
9106 */
9107#ifdef FEAT_RIGHTLEFT
9108 if (revins_on) /* put cursor on last inserted char */
9109 dec_cursor();
9110#endif
9111 mincol = 0;
9112 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009113 if (mode == BACKSPACE_LINE
9114 && (curbuf->b_p_ai
9115#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009116 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009117#endif
9118 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119#ifdef FEAT_RIGHTLEFT
9120 && !revins_on
9121#endif
9122 )
9123 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009124 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009126 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009128 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 }
9130
9131 /*
9132 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9133 */
9134 if ( mode == BACKSPACE_CHAR
9135 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009136 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009137 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 && (*(ml_get_cursor() - 1) == TAB
9139 || (*(ml_get_cursor() - 1) == ' '
9140 && (!*inserted_space_p
9141 || arrow_used))))))
9142 {
9143 int ts;
9144 colnr_T vcol;
9145 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009146 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147
9148 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009149 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009150 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009152 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 /* Compute the virtual column where we want to be. Since
9154 * 'showbreak' may get in the way, need to get the last column of
9155 * the previous character. */
9156 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009157 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 dec_cursor();
9159 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9160 inc_cursor();
9161 want_vcol = (want_vcol / ts) * ts;
9162
9163 /* delete characters until we are at or before want_vcol */
9164 while (vcol > want_vcol
9165 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009166 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167
9168 /* insert extra spaces until we are at want_vcol */
9169 while (vcol < want_vcol)
9170 {
9171 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009172 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9173 && curwin->w_cursor.col < Insstart_orig.col)
9174 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175
9176#ifdef FEAT_VREPLACE
9177 if (State & VREPLACE_FLAG)
9178 ins_char(' ');
9179 else
9180#endif
9181 {
9182 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009183 if ((State & REPLACE_FLAG))
9184 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 }
9186 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9187 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009188
9189 /* If we are now back where we started delete one character. Can
9190 * happen when using 'sts' and 'linebreak'. */
9191 if (vcol >= start_vcol)
9192 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 }
9194
9195 /*
9196 * Delete upto starting point, start of line or previous word.
9197 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009200#ifdef FEAT_MBYTE
9201 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009203 if (has_mbyte)
9204 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009206 do
9207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009209 if (!revins_on) /* put cursor on char to be deleted */
9210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009212
9213 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009215 /* look multi-byte character class */
9216 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009218 prev_cclass = cclass;
9219 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009222
9223 /* start of word? */
9224 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9225 {
9226 mode = BACKSPACE_WORD_NOT_SPACE;
9227 temp = vim_iswordc(cc);
9228 }
9229 /* end of word? */
9230 else if (mode == BACKSPACE_WORD_NOT_SPACE
9231 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9232#ifdef FEAT_MBYTE
9233 || prev_cclass != cclass
9234#endif
9235 ))
9236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009238 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009240 inc_cursor();
9241#ifdef FEAT_RIGHTLEFT
9242 else if (State & REPLACE_FLAG)
9243 dec_cursor();
9244#endif
9245 break;
9246 }
9247 if (State & REPLACE_FLAG)
9248 replace_do_bs(-1);
9249 else
9250 {
9251#ifdef FEAT_MBYTE
9252 if (enc_utf8 && p_deco)
9253 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9254#endif
9255 (void)del_char(FALSE);
9256#ifdef FEAT_MBYTE
9257 /*
9258 * If there are combining characters and 'delcombine' is set
9259 * move the cursor back. Don't back up before the base
9260 * character.
9261 */
9262 if (enc_utf8 && p_deco && cpc[0] != NUL)
9263 inc_cursor();
9264#endif
9265#ifdef FEAT_RIGHTLEFT
9266 if (revins_chars)
9267 {
9268 revins_chars--;
9269 revins_legal++;
9270 }
9271 if (revins_on && gchar_cursor() == NUL)
9272 break;
9273#endif
9274 }
9275 /* Just a single backspace?: */
9276 if (mode == BACKSPACE_CHAR)
9277 break;
9278 } while (
9279#ifdef FEAT_RIGHTLEFT
9280 revins_on ||
9281#endif
9282 (curwin->w_cursor.col > mincol
9283 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9284 || curwin->w_cursor.col != Insstart_orig.col)));
9285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 did_backspace = TRUE;
9287 }
9288#ifdef FEAT_SMARTINDENT
9289 did_si = FALSE;
9290 can_si = FALSE;
9291 can_si_back = FALSE;
9292#endif
9293 if (curwin->w_cursor.col <= 1)
9294 did_ai = FALSE;
9295 /*
9296 * It's a little strange to put backspaces into the redo
9297 * buffer, but it makes auto-indent a lot easier to deal
9298 * with.
9299 */
9300 AppendCharToRedobuff(c);
9301
9302 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009303 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9304 && curwin->w_cursor.col < Insstart_orig.col)
9305 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306
9307 /* vi behaviour: the cursor moves backward but the character that
9308 * was there remains visible
9309 * Vim behaviour: the cursor moves backward and the character that
9310 * was there is erased from the screen.
9311 * We can emulate the vi behaviour by pretending there is a dollar
9312 * displayed even when there isn't.
9313 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009314 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 dollar_vcol = curwin->w_virtcol;
9316
Bram Moolenaarce3be472008-01-14 19:12:28 +00009317#ifdef FEAT_FOLDING
9318 /* When deleting a char the cursor line must never be in a closed fold.
9319 * E.g., when 'foldmethod' is indent and deleting the first non-white
9320 * char before a Tab. */
9321 if (did_backspace)
9322 foldOpenCursor();
9323#endif
9324
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 return did_backspace;
9326}
9327
9328#ifdef FEAT_MOUSE
9329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009330ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331{
9332 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009333 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334
9335# ifdef FEAT_GUI
9336 /* When GUI is active, also move/paste when 'mouse' is empty */
9337 if (!gui.in_use)
9338# endif
9339 if (!mouse_has(MOUSE_INSERT))
9340 return;
9341
9342 undisplay_dollar();
9343 tpos = curwin->w_cursor;
9344 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9345 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009346#ifdef FEAT_WINDOWS
9347 win_T *new_curwin = curwin;
9348
9349 if (curwin != old_curwin && win_valid(old_curwin))
9350 {
9351 /* Mouse took us to another window. We need to go back to the
9352 * previous one to stop insert there properly. */
9353 curwin = old_curwin;
9354 curbuf = curwin->w_buffer;
9355 }
9356#endif
9357 start_arrow(curwin == old_curwin ? &tpos : NULL);
9358#ifdef FEAT_WINDOWS
9359 if (curwin != new_curwin && win_valid(new_curwin))
9360 {
9361 curwin = new_curwin;
9362 curbuf = curwin->w_buffer;
9363 }
9364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365# ifdef FEAT_CINDENT
9366 can_cindent = TRUE;
9367# endif
9368 }
9369
9370#ifdef FEAT_WINDOWS
9371 /* redraw status lines (in case another window became active) */
9372 redraw_statuslines();
9373#endif
9374}
9375
9376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009377ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
9379 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009380# if defined(FEAT_WINDOWS)
9381 win_T *old_curwin = curwin;
9382# endif
9383# ifdef FEAT_INS_EXPAND
9384 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385# endif
9386
9387 tpos = curwin->w_cursor;
9388
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009389# ifdef FEAT_WINDOWS
9390 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 {
9392 int row, col;
9393
9394 row = mouse_row;
9395 col = mouse_col;
9396
9397 /* find the window at the pointer coordinates */
9398 curwin = mouse_find_win(&row, &col);
9399 curbuf = curwin->w_buffer;
9400 }
9401 if (curwin == old_curwin)
9402# endif
9403 undisplay_dollar();
9404
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009405# ifdef FEAT_INS_EXPAND
9406 /* Don't scroll the window in which completion is being done. */
9407 if (!pum_visible()
9408# if defined(FEAT_WINDOWS)
9409 || curwin != old_curwin
9410# endif
9411 )
9412# endif
9413 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009414 if (dir == MSCR_DOWN || dir == MSCR_UP)
9415 {
9416 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9417 scroll_redraw(dir,
9418 (long)(curwin->w_botline - curwin->w_topline));
9419 else
9420 scroll_redraw(dir, 3L);
9421 }
9422#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009423 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009424 {
9425 int val, step = 6;
9426
9427 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9428 step = W_WIDTH(curwin);
9429 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9430 if (val < 0)
9431 val = 0;
9432 gui_do_horiz_scroll(val, TRUE);
9433 }
9434#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009435# ifdef FEAT_INS_EXPAND
9436 did_scroll = TRUE;
9437# endif
9438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009440# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 curwin->w_redr_status = TRUE;
9442
9443 curwin = old_curwin;
9444 curbuf = curwin->w_buffer;
9445# endif
9446
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009447# ifdef FEAT_INS_EXPAND
9448 /* The popup menu may overlay the window, need to redraw it.
9449 * TODO: Would be more efficient to only redraw the windows that are
9450 * overlapped by the popup menu. */
9451 if (pum_visible() && did_scroll)
9452 {
9453 redraw_all_later(NOT_VALID);
9454 ins_compl_show_pum();
9455 }
9456# endif
9457
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 if (!equalpos(curwin->w_cursor, tpos))
9459 {
9460 start_arrow(&tpos);
9461# ifdef FEAT_CINDENT
9462 can_cindent = TRUE;
9463# endif
9464 }
9465}
9466#endif
9467
Bram Moolenaarec2da362017-01-21 20:04:22 +01009468/*
9469 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9470 * P_PE literally.
9471 * When "drop" is TRUE then consume the text and drop it.
9472 */
9473 int
9474bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9475{
9476 int c;
9477 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9478 int idx = 0;
9479 char_u *end = find_termcode((char_u *)"PE");
9480 int ret_char = -1;
9481 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009482 int save_paste = p_paste;
9483 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009484
9485 /* If the end code is too long we can't detect it, read everything. */
9486 if (STRLEN(end) >= NUMBUFLEN)
9487 end = NULL;
9488 ++no_mapping;
9489 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009490 p_paste = TRUE;
9491 curbuf->b_p_ai = FALSE;
9492
Bram Moolenaarec2da362017-01-21 20:04:22 +01009493 for (;;)
9494 {
9495 /* When the end is not defined read everything. */
9496 if (end == NULL && vpeekc() == NUL)
9497 break;
9498 c = plain_vgetc();
9499#ifdef FEAT_MBYTE
9500 if (has_mbyte)
9501 idx += (*mb_char2bytes)(c, buf + idx);
9502 else
9503#endif
9504 buf[idx++] = c;
9505 buf[idx] = NUL;
9506 if (end != NUL && STRNCMP(buf, end, idx) == 0)
9507 {
9508 if (end[idx] == NUL)
9509 break; /* Found the end of paste code. */
9510 continue;
9511 }
9512 if (!drop)
9513 {
9514 switch (mode)
9515 {
9516 case PASTE_CMDLINE:
9517 put_on_cmdline(buf, idx, TRUE);
9518 break;
9519
9520 case PASTE_EX:
9521 if (gap != NULL && ga_grow(gap, idx) == OK)
9522 {
9523 mch_memmove((char *)gap->ga_data + gap->ga_len,
9524 buf, (size_t)idx);
9525 gap->ga_len += idx;
9526 }
9527 break;
9528
9529 case PASTE_INSERT:
9530 if (stop_arrow() == OK)
9531 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009532 c = buf[0];
9533 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9534 ins_eol(c);
9535 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009536 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009537 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009538 AppendToRedobuffLit(buf, idx);
9539 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009540 }
9541 break;
9542
9543 case PASTE_ONE_CHAR:
9544 if (ret_char == -1)
9545 {
9546#ifdef FEAT_MBYTE
9547 if (has_mbyte)
9548 ret_char = (*mb_ptr2char)(buf);
9549 else
9550#endif
9551 ret_char = buf[0];
9552 }
9553 break;
9554 }
9555 }
9556 idx = 0;
9557 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009558
Bram Moolenaarec2da362017-01-21 20:04:22 +01009559 --no_mapping;
9560 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009561 p_paste = save_paste;
9562 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009563
9564 return ret_char;
9565}
9566
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009567#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009569ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009570{
9571 /* We will be leaving the current window, unless closing another tab. */
9572 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9573 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9574 {
9575 undisplay_dollar();
9576 start_arrow(&curwin->w_cursor);
9577# ifdef FEAT_CINDENT
9578 can_cindent = TRUE;
9579# endif
9580 }
9581
9582 if (c == K_TABLINE)
9583 goto_tabpage(current_tab);
9584 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009585 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009586 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009587 redraw_statuslines(); /* will redraw the tabline when needed */
9588 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009589}
9590#endif
9591
9592#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009594ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595{
9596 pos_T tpos;
9597
9598 undisplay_dollar();
9599 tpos = curwin->w_cursor;
9600 if (gui_do_scroll())
9601 {
9602 start_arrow(&tpos);
9603# ifdef FEAT_CINDENT
9604 can_cindent = TRUE;
9605# endif
9606 }
9607}
9608
9609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009610ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611{
9612 pos_T tpos;
9613
9614 undisplay_dollar();
9615 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009616 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 {
9618 start_arrow(&tpos);
9619# ifdef FEAT_CINDENT
9620 can_cindent = TRUE;
9621# endif
9622 }
9623}
9624#endif
9625
9626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009627ins_left(
9628 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629{
9630 pos_T tpos;
9631
9632#ifdef FEAT_FOLDING
9633 if ((fdo_flags & FDO_HOR) && KeyTyped)
9634 foldOpenCursor();
9635#endif
9636 undisplay_dollar();
9637 tpos = curwin->w_cursor;
9638 if (oneleft() == OK)
9639 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009640#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9641 /* Only call start_arrow() when not busy with preediting, it will
9642 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9643 if (!im_is_preediting())
9644#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009645 {
9646 start_arrow_with_change(&tpos, end_change);
9647 if (!end_change)
9648 AppendCharToRedobuff(K_LEFT);
9649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650#ifdef FEAT_RIGHTLEFT
9651 /* If exit reversed string, position is fixed */
9652 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9653 revins_legal++;
9654 revins_chars++;
9655#endif
9656 }
9657
9658 /*
9659 * if 'whichwrap' set for cursor in insert mode may go to
9660 * previous line
9661 */
9662 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9663 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009664 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 start_arrow(&tpos);
9666 --(curwin->w_cursor.lnum);
9667 coladvance((colnr_T)MAXCOL);
9668 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9669 }
9670 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009671 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009672 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673}
9674
9675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009676ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677{
9678 pos_T tpos;
9679
9680#ifdef FEAT_FOLDING
9681 if ((fdo_flags & FDO_HOR) && KeyTyped)
9682 foldOpenCursor();
9683#endif
9684 undisplay_dollar();
9685 tpos = curwin->w_cursor;
9686 if (c == K_C_HOME)
9687 curwin->w_cursor.lnum = 1;
9688 curwin->w_cursor.col = 0;
9689#ifdef FEAT_VIRTUALEDIT
9690 curwin->w_cursor.coladd = 0;
9691#endif
9692 curwin->w_curswant = 0;
9693 start_arrow(&tpos);
9694}
9695
9696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009697ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
9699 pos_T tpos;
9700
9701#ifdef FEAT_FOLDING
9702 if ((fdo_flags & FDO_HOR) && KeyTyped)
9703 foldOpenCursor();
9704#endif
9705 undisplay_dollar();
9706 tpos = curwin->w_cursor;
9707 if (c == K_C_END)
9708 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9709 coladvance((colnr_T)MAXCOL);
9710 curwin->w_curswant = MAXCOL;
9711
9712 start_arrow(&tpos);
9713}
9714
9715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009716ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717{
9718#ifdef FEAT_FOLDING
9719 if ((fdo_flags & FDO_HOR) && KeyTyped)
9720 foldOpenCursor();
9721#endif
9722 undisplay_dollar();
9723 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9724 {
9725 start_arrow(&curwin->w_cursor);
9726 (void)bck_word(1L, FALSE, FALSE);
9727 curwin->w_set_curswant = TRUE;
9728 }
9729 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009730 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732
9733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009734ins_right(
9735 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736{
9737#ifdef FEAT_FOLDING
9738 if ((fdo_flags & FDO_HOR) && KeyTyped)
9739 foldOpenCursor();
9740#endif
9741 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009742 if (gchar_cursor() != NUL
9743#ifdef FEAT_VIRTUALEDIT
9744 || virtual_active()
9745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 )
9747 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009748 start_arrow_with_change(&curwin->w_cursor, end_change);
9749 if (!end_change)
9750 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 curwin->w_set_curswant = TRUE;
9752#ifdef FEAT_VIRTUALEDIT
9753 if (virtual_active())
9754 oneright();
9755 else
9756#endif
9757 {
9758#ifdef FEAT_MBYTE
9759 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009760 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 else
9762#endif
9763 ++curwin->w_cursor.col;
9764 }
9765
9766#ifdef FEAT_RIGHTLEFT
9767 revins_legal++;
9768 if (revins_chars)
9769 revins_chars--;
9770#endif
9771 }
9772 /* if 'whichwrap' set for cursor in insert mode, may move the
9773 * cursor to the next line */
9774 else if (vim_strchr(p_ww, ']') != NULL
9775 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9776 {
9777 start_arrow(&curwin->w_cursor);
9778 curwin->w_set_curswant = TRUE;
9779 ++curwin->w_cursor.lnum;
9780 curwin->w_cursor.col = 0;
9781 }
9782 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009783 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009784 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785}
9786
9787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009788ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789{
9790#ifdef FEAT_FOLDING
9791 if ((fdo_flags & FDO_HOR) && KeyTyped)
9792 foldOpenCursor();
9793#endif
9794 undisplay_dollar();
9795 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9796 || gchar_cursor() != NUL)
9797 {
9798 start_arrow(&curwin->w_cursor);
9799 (void)fwd_word(1L, FALSE, 0);
9800 curwin->w_set_curswant = TRUE;
9801 }
9802 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009803 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804}
9805
9806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009807ins_up(
9808 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809{
9810 pos_T tpos;
9811 linenr_T old_topline = curwin->w_topline;
9812#ifdef FEAT_DIFF
9813 int old_topfill = curwin->w_topfill;
9814#endif
9815
9816 undisplay_dollar();
9817 tpos = curwin->w_cursor;
9818 if (cursor_up(1L, TRUE) == OK)
9819 {
9820 if (startcol)
9821 coladvance(getvcol_nolist(&Insstart));
9822 if (old_topline != curwin->w_topline
9823#ifdef FEAT_DIFF
9824 || old_topfill != curwin->w_topfill
9825#endif
9826 )
9827 redraw_later(VALID);
9828 start_arrow(&tpos);
9829#ifdef FEAT_CINDENT
9830 can_cindent = TRUE;
9831#endif
9832 }
9833 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009834 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835}
9836
9837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009838ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839{
9840 pos_T tpos;
9841
9842 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009843
9844#ifdef FEAT_WINDOWS
9845 if (mod_mask & MOD_MASK_CTRL)
9846 {
9847 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009848 if (first_tabpage->tp_next != NULL)
9849 {
9850 start_arrow(&curwin->w_cursor);
9851 goto_tabpage(-1);
9852 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009853 return;
9854 }
9855#endif
9856
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 tpos = curwin->w_cursor;
9858 if (onepage(BACKWARD, 1L) == OK)
9859 {
9860 start_arrow(&tpos);
9861#ifdef FEAT_CINDENT
9862 can_cindent = TRUE;
9863#endif
9864 }
9865 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009866 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867}
9868
9869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009870ins_down(
9871 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872{
9873 pos_T tpos;
9874 linenr_T old_topline = curwin->w_topline;
9875#ifdef FEAT_DIFF
9876 int old_topfill = curwin->w_topfill;
9877#endif
9878
9879 undisplay_dollar();
9880 tpos = curwin->w_cursor;
9881 if (cursor_down(1L, TRUE) == OK)
9882 {
9883 if (startcol)
9884 coladvance(getvcol_nolist(&Insstart));
9885 if (old_topline != curwin->w_topline
9886#ifdef FEAT_DIFF
9887 || old_topfill != curwin->w_topfill
9888#endif
9889 )
9890 redraw_later(VALID);
9891 start_arrow(&tpos);
9892#ifdef FEAT_CINDENT
9893 can_cindent = TRUE;
9894#endif
9895 }
9896 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009897 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898}
9899
9900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009901ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902{
9903 pos_T tpos;
9904
9905 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009906
9907#ifdef FEAT_WINDOWS
9908 if (mod_mask & MOD_MASK_CTRL)
9909 {
9910 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009911 if (first_tabpage->tp_next != NULL)
9912 {
9913 start_arrow(&curwin->w_cursor);
9914 goto_tabpage(0);
9915 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009916 return;
9917 }
9918#endif
9919
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 tpos = curwin->w_cursor;
9921 if (onepage(FORWARD, 1L) == OK)
9922 {
9923 start_arrow(&tpos);
9924#ifdef FEAT_CINDENT
9925 can_cindent = TRUE;
9926#endif
9927 }
9928 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009929 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930}
9931
9932#ifdef FEAT_DND
9933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009934ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935{
9936 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9937}
9938#endif
9939
9940/*
9941 * Handle TAB in Insert or Replace mode.
9942 * Return TRUE when the TAB needs to be inserted like a normal character.
9943 */
9944 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009945ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946{
9947 int ind;
9948 int i;
9949 int temp;
9950
9951 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9952 Insstart_blank_vcol = get_nolist_virtcol();
9953 if (echeck_abbr(TAB + ABBR_OFF))
9954 return FALSE;
9955
9956 ind = inindent(0);
9957#ifdef FEAT_CINDENT
9958 if (ind)
9959 can_cindent = FALSE;
9960#endif
9961
9962 /*
9963 * When nothing special, insert TAB like a normal character
9964 */
9965 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009966 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009967 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 return TRUE;
9969
9970 if (stop_arrow() == FAIL)
9971 return TRUE;
9972
9973 did_ai = FALSE;
9974#ifdef FEAT_SMARTINDENT
9975 did_si = FALSE;
9976 can_si = FALSE;
9977 can_si_back = FALSE;
9978#endif
9979 AppendToRedobuff((char_u *)"\t");
9980
9981 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009982 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009983 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9984 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 else /* otherwise use 'tabstop' */
9986 temp = (int)curbuf->b_p_ts;
9987 temp -= get_nolist_virtcol() % temp;
9988
9989 /*
9990 * Insert the first space with ins_char(). It will delete one char in
9991 * replace mode. Insert the rest with ins_str(); it will not delete any
9992 * chars. For VREPLACE mode, we use ins_char() for all characters.
9993 */
9994 ins_char(' ');
9995 while (--temp > 0)
9996 {
9997#ifdef FEAT_VREPLACE
9998 if (State & VREPLACE_FLAG)
9999 ins_char(' ');
10000 else
10001#endif
10002 {
10003 ins_str((char_u *)" ");
10004 if (State & REPLACE_FLAG) /* no char replaced */
10005 replace_push(NUL);
10006 }
10007 }
10008
10009 /*
10010 * When 'expandtab' not set: Replace spaces by TABs where possible.
10011 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010012 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 {
10014 char_u *ptr;
10015#ifdef FEAT_VREPLACE
10016 char_u *saved_line = NULL; /* init for GCC */
10017 pos_T pos;
10018#endif
10019 pos_T fpos;
10020 pos_T *cursor;
10021 colnr_T want_vcol, vcol;
10022 int change_col = -1;
10023 int save_list = curwin->w_p_list;
10024
10025 /*
10026 * Get the current line. For VREPLACE mode, don't make real changes
10027 * yet, just work on a copy of the line.
10028 */
10029#ifdef FEAT_VREPLACE
10030 if (State & VREPLACE_FLAG)
10031 {
10032 pos = curwin->w_cursor;
10033 cursor = &pos;
10034 saved_line = vim_strsave(ml_get_curline());
10035 if (saved_line == NULL)
10036 return FALSE;
10037 ptr = saved_line + pos.col;
10038 }
10039 else
10040#endif
10041 {
10042 ptr = ml_get_cursor();
10043 cursor = &curwin->w_cursor;
10044 }
10045
10046 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10047 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10048 curwin->w_p_list = FALSE;
10049
10050 /* Find first white before the cursor */
10051 fpos = curwin->w_cursor;
10052 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
10053 {
10054 --fpos.col;
10055 --ptr;
10056 }
10057
10058 /* In Replace mode, don't change characters before the insert point. */
10059 if ((State & REPLACE_FLAG)
10060 && fpos.lnum == Insstart.lnum
10061 && fpos.col < Insstart.col)
10062 {
10063 ptr += Insstart.col - fpos.col;
10064 fpos.col = Insstart.col;
10065 }
10066
10067 /* compute virtual column numbers of first white and cursor */
10068 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10069 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10070
Bram Moolenaar597a4222014-06-25 14:39:50 +020010071 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10072 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 while (vim_iswhite(*ptr))
10074 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010075 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 if (vcol + i > want_vcol)
10077 break;
10078 if (*ptr != TAB)
10079 {
10080 *ptr = TAB;
10081 if (change_col < 0)
10082 {
10083 change_col = fpos.col; /* Column of first change */
10084 /* May have to adjust Insstart */
10085 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10086 Insstart.col = fpos.col;
10087 }
10088 }
10089 ++fpos.col;
10090 ++ptr;
10091 vcol += i;
10092 }
10093
10094 if (change_col >= 0)
10095 {
10096 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010097 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098
10099 /* Skip over the spaces we need. */
10100 while (vcol < want_vcol && *ptr == ' ')
10101 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010102 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 ++ptr;
10104 ++repl_off;
10105 }
10106 if (vcol > want_vcol)
10107 {
10108 /* Must have a char with 'showbreak' just before it. */
10109 --ptr;
10110 --repl_off;
10111 }
10112 fpos.col += repl_off;
10113
10114 /* Delete following spaces. */
10115 i = cursor->col - fpos.col;
10116 if (i > 0)
10117 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010118 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 /* correct replace stack. */
10120 if ((State & REPLACE_FLAG)
10121#ifdef FEAT_VREPLACE
10122 && !(State & VREPLACE_FLAG)
10123#endif
10124 )
10125 for (temp = i; --temp >= 0; )
10126 replace_join(repl_off);
10127 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010128#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010129 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010130 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010131 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010132 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10133 (char_u *)"\t", 1);
10134 }
10135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 cursor->col -= i;
10137
10138#ifdef FEAT_VREPLACE
10139 /*
10140 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10141 * backspacing over the changed spacing and then inserting the new
10142 * spacing.
10143 */
10144 if (State & VREPLACE_FLAG)
10145 {
10146 /* Backspace from real cursor to change_col */
10147 backspace_until_column(change_col);
10148
10149 /* Insert each char in saved_line from changed_col to
10150 * ptr-cursor */
10151 ins_bytes_len(saved_line + change_col,
10152 cursor->col - change_col);
10153 }
10154#endif
10155 }
10156
10157#ifdef FEAT_VREPLACE
10158 if (State & VREPLACE_FLAG)
10159 vim_free(saved_line);
10160#endif
10161 curwin->w_p_list = save_list;
10162 }
10163
10164 return FALSE;
10165}
10166
10167/*
10168 * Handle CR or NL in insert mode.
10169 * Return TRUE when out of memory or can't undo.
10170 */
10171 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010172ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173{
10174 int i;
10175
10176 if (echeck_abbr(c + ABBR_OFF))
10177 return FALSE;
10178 if (stop_arrow() == FAIL)
10179 return TRUE;
10180 undisplay_dollar();
10181
10182 /*
10183 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10184 * character under the cursor. Only push a NUL on the replace stack,
10185 * nothing to put back when the NL is deleted.
10186 */
10187 if ((State & REPLACE_FLAG)
10188#ifdef FEAT_VREPLACE
10189 && !(State & VREPLACE_FLAG)
10190#endif
10191 )
10192 replace_push(NUL);
10193
10194 /*
10195 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10196 * replacing the next line, so we push all of the characters left on the
10197 * line onto the replace stack. This is not done here though, it is done
10198 * in open_line().
10199 */
10200
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010201#ifdef FEAT_VIRTUALEDIT
10202 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10203 * CTRL-O). */
10204 if (virtual_active() && curwin->w_cursor.coladd > 0)
10205 coladvance(getviscol());
10206#endif
10207
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208#ifdef FEAT_RIGHTLEFT
10209# ifdef FEAT_FKMAP
10210 if (p_altkeymap && p_fkmap)
10211 fkmap(NL);
10212# endif
10213 /* NL in reverse insert will always start in the end of
10214 * current line. */
10215 if (revins_on)
10216 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10217#endif
10218
10219 AppendToRedobuff(NL_STR);
10220 i = open_line(FORWARD,
10221#ifdef FEAT_COMMENTS
10222 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10223#endif
10224 0, old_indent);
10225 old_indent = 0;
10226#ifdef FEAT_CINDENT
10227 can_cindent = TRUE;
10228#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010229#ifdef FEAT_FOLDING
10230 /* When inserting a line the cursor line must never be in a closed fold. */
10231 foldOpenCursor();
10232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233
10234 return (!i);
10235}
10236
10237#ifdef FEAT_DIGRAPHS
10238/*
10239 * Handle digraph in insert mode.
10240 * Returns character still to be inserted, or NUL when nothing remaining to be
10241 * done.
10242 */
10243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010244ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245{
10246 int c;
10247 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010248 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249
10250 pc_status = PC_STATUS_UNSET;
10251 if (redrawing() && !char_avail())
10252 {
10253 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010254 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255
10256 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010257 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258#ifdef FEAT_CMDL_INFO
10259 add_to_showcmd_c(Ctrl_K);
10260#endif
10261 }
10262
10263#ifdef USE_ON_FLY_SCROLL
10264 dont_scroll = TRUE; /* disallow scrolling here */
10265#endif
10266
10267 /* don't map the digraph chars. This also prevents the
10268 * mode message to be deleted when ESC is hit */
10269 ++no_mapping;
10270 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010271 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 --no_mapping;
10273 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010274 if (did_putchar)
10275 /* when the line fits in 'columns' the '?' is at the start of the next
10276 * line and will not be removed by the redraw */
10277 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010278
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 if (IS_SPECIAL(c) || mod_mask) /* special key */
10280 {
10281#ifdef FEAT_CMDL_INFO
10282 clear_showcmd();
10283#endif
10284 insert_special(c, TRUE, FALSE);
10285 return NUL;
10286 }
10287 if (c != ESC)
10288 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010289 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 if (redrawing() && !char_avail())
10291 {
10292 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010293 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294
10295 if (char2cells(c) == 1)
10296 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010297 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010299 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 }
10301#ifdef FEAT_CMDL_INFO
10302 add_to_showcmd_c(c);
10303#endif
10304 }
10305 ++no_mapping;
10306 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010307 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 --no_mapping;
10309 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010310 if (did_putchar)
10311 /* when the line fits in 'columns' the '?' is at the start of the
10312 * next line and will not be removed by a redraw */
10313 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 if (cc != ESC)
10315 {
10316 AppendToRedobuff((char_u *)CTRL_V_STR);
10317 c = getdigraph(c, cc, TRUE);
10318#ifdef FEAT_CMDL_INFO
10319 clear_showcmd();
10320#endif
10321 return c;
10322 }
10323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324#ifdef FEAT_CMDL_INFO
10325 clear_showcmd();
10326#endif
10327 return NUL;
10328}
10329#endif
10330
10331/*
10332 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10333 * Returns the char to be inserted, or NUL if none found.
10334 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010336ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337{
10338 int c;
10339 int temp;
10340 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010341 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342
10343 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10344 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010345 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 return NUL;
10347 }
10348
10349 /* try to advance to the cursor column */
10350 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010351 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 prev_ptr = ptr;
10353 validate_virtcol();
10354 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10355 {
10356 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010357 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 }
10359 if ((colnr_T)temp > curwin->w_virtcol)
10360 ptr = prev_ptr;
10361
10362#ifdef FEAT_MBYTE
10363 c = (*mb_ptr2char)(ptr);
10364#else
10365 c = *ptr;
10366#endif
10367 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010368 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 return c;
10370}
10371
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010372/*
10373 * CTRL-Y or CTRL-E typed in Insert mode.
10374 */
10375 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010376ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010377{
10378 int c = tc;
10379
10380#ifdef FEAT_INS_EXPAND
10381 if (ctrl_x_mode == CTRL_X_SCROLL)
10382 {
10383 if (c == Ctrl_Y)
10384 scrolldown_clamp();
10385 else
10386 scrollup_clamp();
10387 redraw_later(VALID);
10388 }
10389 else
10390#endif
10391 {
10392 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10393 if (c != NUL)
10394 {
10395 long tw_save;
10396
10397 /* The character must be taken literally, insert like it
10398 * was typed after a CTRL-V, and pretend 'textwidth'
10399 * wasn't set. Digits, 'o' and 'x' are special after a
10400 * CTRL-V, don't use it for these. */
10401 if (c < 256 && !isalnum(c))
10402 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10403 tw_save = curbuf->b_p_tw;
10404 curbuf->b_p_tw = -1;
10405 insert_special(c, TRUE, FALSE);
10406 curbuf->b_p_tw = tw_save;
10407#ifdef FEAT_RIGHTLEFT
10408 revins_chars++;
10409 revins_legal++;
10410#endif
10411 c = Ctrl_V; /* pretend CTRL-V is last character */
10412 auto_format(FALSE, TRUE);
10413 }
10414 }
10415 return c;
10416}
10417
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418#ifdef FEAT_SMARTINDENT
10419/*
10420 * Try to do some very smart auto-indenting.
10421 * Used when inserting a "normal" character.
10422 */
10423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010424ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425{
10426 pos_T *pos, old_pos;
10427 char_u *ptr;
10428 int i;
10429 int temp;
10430
10431 /*
10432 * do some very smart indenting when entering '{' or '}'
10433 */
10434 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10435 {
10436 /*
10437 * for '}' set indent equal to indent of line containing matching '{'
10438 */
10439 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10440 {
10441 old_pos = curwin->w_cursor;
10442 /*
10443 * If the matching '{' has a ')' immediately before it (ignoring
10444 * white-space), then line up with the start of the line
10445 * containing the matching '(' if there is one. This handles the
10446 * case where an "if (..\n..) {" statement continues over multiple
10447 * lines -- webb
10448 */
10449 ptr = ml_get(pos->lnum);
10450 i = pos->col;
10451 if (i > 0) /* skip blanks before '{' */
10452 while (--i > 0 && vim_iswhite(ptr[i]))
10453 ;
10454 curwin->w_cursor.lnum = pos->lnum;
10455 curwin->w_cursor.col = i;
10456 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10457 curwin->w_cursor = *pos;
10458 i = get_indent();
10459 curwin->w_cursor = old_pos;
10460#ifdef FEAT_VREPLACE
10461 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010462 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 else
10464#endif
10465 (void)set_indent(i, SIN_CHANGED);
10466 }
10467 else if (curwin->w_cursor.col > 0)
10468 {
10469 /*
10470 * when inserting '{' after "O" reduce indent, but not
10471 * more than indent of previous line
10472 */
10473 temp = TRUE;
10474 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10475 {
10476 old_pos = curwin->w_cursor;
10477 i = get_indent();
10478 while (curwin->w_cursor.lnum > 1)
10479 {
10480 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10481
10482 /* ignore empty lines and lines starting with '#'. */
10483 if (*ptr != '#' && *ptr != NUL)
10484 break;
10485 }
10486 if (get_indent() >= i)
10487 temp = FALSE;
10488 curwin->w_cursor = old_pos;
10489 }
10490 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010491 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 }
10493 }
10494
10495 /*
10496 * set indent of '#' always to 0
10497 */
10498 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10499 {
10500 /* remember current indent for next line */
10501 old_indent = get_indent();
10502 (void)set_indent(0, SIN_CHANGED);
10503 }
10504
10505 /* Adjust ai_col, the char at this position can be deleted. */
10506 if (ai_col > curwin->w_cursor.col)
10507 ai_col = curwin->w_cursor.col;
10508}
10509#endif
10510
10511/*
10512 * Get the value that w_virtcol would have when 'list' is off.
10513 * Unless 'cpo' contains the 'L' flag.
10514 */
10515 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010516get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517{
10518 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10519 return getvcol_nolist(&curwin->w_cursor);
10520 validate_virtcol();
10521 return curwin->w_virtcol;
10522}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010523
10524#ifdef FEAT_AUTOCMD
10525/*
10526 * Handle the InsertCharPre autocommand.
10527 * "c" is the character that was typed.
10528 * Return a pointer to allocated memory with the replacement string.
10529 * Return NULL to continue inserting "c".
10530 */
10531 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010532do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010533{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010534 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010535 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010536
10537 /* Return quickly when there is nothing to do. */
10538 if (!has_insertcharpre())
10539 return NULL;
10540
Bram Moolenaar704984a2012-06-01 14:57:51 +020010541#ifdef FEAT_MBYTE
10542 if (has_mbyte)
10543 buf[(*mb_char2bytes)(c, buf)] = NUL;
10544 else
10545#endif
10546 {
10547 buf[0] = c;
10548 buf[1] = NUL;
10549 }
10550
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010551 /* Lock the text to avoid weird things from happening. */
10552 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010553 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010554
Bram Moolenaar704984a2012-06-01 14:57:51 +020010555 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010556 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010557 {
10558 /* Get the value of v:char. It may be empty or more than one
10559 * character. Only use it when changed, otherwise continue with the
10560 * original character to avoid breaking autoindent. */
10561 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10562 res = vim_strsave(get_vim_var_str(VV_CHAR));
10563 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010564
10565 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10566 --textlock;
10567
10568 return res;
10569}
10570#endif