blob: 6621515b575e788d4c5dac4ed697bfb6e9e33740 [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.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100544 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
545 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 */
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 Moolenaara6c07602017-03-05 21:18:27 +01001456#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001457 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001458#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001459 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001460 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001461#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001462 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001463#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001464 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 break;
1466#endif /* FEAT_INS_EXPAND */
1467
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001468 case Ctrl_Y: /* copy from previous line or scroll down */
1469 case Ctrl_E: /* copy from next line or scroll up */
1470 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 break;
1472
1473 default:
1474#ifdef UNIX
1475 if (c == intr_char) /* special interrupt char */
1476 goto do_intr;
1477#endif
1478
Bram Moolenaare659c952011-05-19 17:25:41 +02001479normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001481 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001483#ifdef FEAT_AUTOCMD
1484 if (!p_paste)
1485 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001486 /* Trigger InsertCharPre. */
1487 char_u *str = do_insert_char_pre(c);
1488 char_u *p;
1489
1490 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001491 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001492 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001493 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001494 /* Insert the new value of v:char literally. */
1495 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001496 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001497 c = PTR2CHAR(p);
1498 if (c == CAR || c == K_KENTER || c == NL)
1499 ins_eol(c);
1500 else
1501 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001502 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001503 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001504 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001505 vim_free(str);
1506 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001507 }
1508
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001509 /* If the new value is already inserted or an empty string
1510 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001511 if (c == NUL)
1512 break;
1513 }
1514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515#ifdef FEAT_SMARTINDENT
1516 /* Try to perform smart-indenting. */
1517 ins_try_si(c);
1518#endif
1519
1520 if (c == ' ')
1521 {
1522 inserted_space = TRUE;
1523#ifdef FEAT_CINDENT
1524 if (inindent(0))
1525 can_cindent = FALSE;
1526#endif
1527 if (Insstart_blank_vcol == MAXCOL
1528 && curwin->w_cursor.lnum == Insstart.lnum)
1529 Insstart_blank_vcol = get_nolist_virtcol();
1530 }
1531
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001532 /* Insert a normal character and check for abbreviations on a
1533 * special character. Let CTRL-] expand abbreviations without
1534 * inserting it. */
1535 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#ifdef FEAT_MBYTE
1537 /* Add ABBR_OFF for characters above 0x100, this is
1538 * what check_abbr() expects. */
1539 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1540#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001541 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
1543 insert_special(c, FALSE, FALSE);
1544#ifdef FEAT_RIGHTLEFT
1545 revins_legal++;
1546 revins_chars++;
1547#endif
1548 }
1549
1550 auto_format(FALSE, TRUE);
1551
1552#ifdef FEAT_FOLDING
1553 /* When inserting a character the cursor line must never be in a
1554 * closed fold. */
1555 foldOpenCursor();
1556#endif
1557 break;
1558 } /* end of switch (c) */
1559
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001560#ifdef FEAT_AUTOCMD
1561 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001562 if (c != K_CURSORHOLD
1563# ifdef FEAT_COMPL_FUNC
1564 /* but not in CTRL-X mode, a script can't restore the state */
1565 && ctrl_x_mode == 0
1566# endif
1567 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001568 did_cursorhold = FALSE;
1569#endif
1570
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 /* If the cursor was moved we didn't just insert a space */
1572 if (arrow_used)
1573 inserted_space = FALSE;
1574
1575#ifdef FEAT_CINDENT
1576 if (can_cindent && cindent_on()
1577# ifdef FEAT_INS_EXPAND
1578 && ctrl_x_mode == 0
1579# endif
1580 )
1581 {
1582force_cindent:
1583 /*
1584 * Indent now if a key was typed that is in 'cinkeys'.
1585 */
1586 if (in_cinkeys(c, ' ', line_is_white))
1587 {
1588 if (stop_arrow() == OK)
1589 /* re-indent the current line */
1590 do_c_expr_indent();
1591 }
1592 }
1593#endif /* FEAT_CINDENT */
1594
1595 } /* for (;;) */
1596 /* NOTREACHED */
1597}
1598
1599/*
1600 * Redraw for Insert mode.
1601 * This is postponed until getting the next character to make '$' in the 'cpo'
1602 * option work correctly.
1603 * Only redraw when there are no characters available. This speeds up
1604 * inserting sequences of characters (e.g., for CTRL-R).
1605 */
1606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001607ins_redraw(
1608 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001610#ifdef FEAT_CONCEAL
1611 linenr_T conceal_old_cursor_line = 0;
1612 linenr_T conceal_new_cursor_line = 0;
1613 int conceal_update_lines = FALSE;
1614#endif
1615
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001616 if (char_avail())
1617 return;
1618
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001619#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001620 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1621 * visible, the command might delete it. */
1622 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001623# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001625# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001626# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001627 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001628# endif
1629# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001630 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001631# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001632 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001633# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001634 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001635# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001636# ifdef FEAT_INS_EXPAND
1637 && !pum_visible()
1638# endif
1639 )
1640 {
1641# ifdef FEAT_SYN_HL
1642 /* Need to update the screen first, to make sure syntax
1643 * highlighting is correct after making a change (e.g., inserting
1644 * a "(". The autocommand may also require a redraw, so it's done
1645 * again below, unfortunately. */
1646 if (syntax_present(curwin) && must_redraw)
1647 update_screen(0);
1648# endif
1649# ifdef FEAT_AUTOCMD
1650 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001651 {
1652 /* Make sure curswant is correct, an autocommand may call
1653 * getcurpos(). */
1654 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001655 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001656 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001657# endif
1658# ifdef FEAT_CONCEAL
1659 if (curwin->w_p_cole > 0)
1660 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001661# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001662 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001663# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001664 conceal_new_cursor_line = curwin->w_cursor.lnum;
1665 conceal_update_lines = TRUE;
1666 }
1667# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001668# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001669 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001670# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001671 }
1672#endif
1673
1674#ifdef FEAT_AUTOCMD
1675 /* Trigger TextChangedI if b_changedtick differs. */
1676 if (ready && has_textchangedI()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001677 && last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001678# ifdef FEAT_INS_EXPAND
1679 && !pum_visible()
1680# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001681 )
1682 {
1683 if (last_changedtick_buf == curbuf)
1684 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1685 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001686 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001688#endif
1689
1690 if (must_redraw)
1691 update_screen(0);
1692 else if (clear_cmdline || redraw_cmdline)
1693 showmode(); /* clear cmdline and show mode */
1694# if defined(FEAT_CONCEAL)
1695 if ((conceal_update_lines
1696 && (conceal_old_cursor_line != conceal_new_cursor_line
1697 || conceal_cursor_line(curwin)))
1698 || need_cursor_line_redraw)
1699 {
1700 if (conceal_old_cursor_line != conceal_new_cursor_line)
1701 update_single_line(curwin, conceal_old_cursor_line);
1702 update_single_line(curwin, conceal_new_cursor_line == 0
1703 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1704 curwin->w_valid &= ~VALID_CROW;
1705 }
1706# endif
1707 showruler(FALSE);
1708 setcursor();
1709 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710}
1711
1712/*
1713 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1714 */
1715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001716ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
1718 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001719 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
1721 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001722 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
1724 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001727 did_putchar = TRUE;
1728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1730
1731#ifdef FEAT_CMDL_INFO
1732 add_to_showcmd_c(Ctrl_V);
1733#endif
1734
1735 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001736 if (did_putchar)
1737 /* when the line fits in 'columns' the '^' is at the start of the next
1738 * line and will not removed by the redraw */
1739 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740#ifdef FEAT_CMDL_INFO
1741 clear_showcmd();
1742#endif
1743 insert_special(c, FALSE, TRUE);
1744#ifdef FEAT_RIGHTLEFT
1745 revins_chars++;
1746 revins_legal++;
1747#endif
1748}
1749
1750/*
1751 * Put a character directly onto the screen. It's not stored in a buffer.
1752 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1753 */
1754static int pc_status;
1755#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1756#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1757#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1758#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760static int pc_attr;
1761static int pc_row;
1762static int pc_col;
1763
1764 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001765edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 int attr;
1768
1769 if (ScreenLines != NULL)
1770 {
1771 update_topline(); /* just in case w_topline isn't valid */
1772 validate_cursor();
1773 if (highlight)
1774 attr = hl_attr(HLF_8);
1775 else
1776 attr = 0;
1777 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1778 pc_col = W_WINCOL(curwin);
1779#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1780 pc_status = PC_STATUS_UNSET;
1781#endif
1782#ifdef FEAT_RIGHTLEFT
1783 if (curwin->w_p_rl)
1784 {
1785 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1786# ifdef FEAT_MBYTE
1787 if (has_mbyte)
1788 {
1789 int fix_col = mb_fix_col(pc_col, pc_row);
1790
1791 if (fix_col != pc_col)
1792 {
1793 screen_putchar(' ', pc_row, fix_col, attr);
1794 --curwin->w_wcol;
1795 pc_status = PC_STATUS_RIGHT;
1796 }
1797 }
1798# endif
1799 }
1800 else
1801#endif
1802 {
1803 pc_col += curwin->w_wcol;
1804#ifdef FEAT_MBYTE
1805 if (mb_lefthalve(pc_row, pc_col))
1806 pc_status = PC_STATUS_LEFT;
1807#endif
1808 }
1809
1810 /* save the character to be able to put it back */
1811#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1812 if (pc_status == PC_STATUS_UNSET)
1813#endif
1814 {
1815 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1816 pc_status = PC_STATUS_SET;
1817 }
1818 screen_putchar(c, pc_row, pc_col, attr);
1819 }
1820}
1821
1822/*
1823 * Undo the previous edit_putchar().
1824 */
1825 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001826edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
1828 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1829 {
1830#if defined(FEAT_MBYTE)
1831 if (pc_status == PC_STATUS_RIGHT)
1832 ++curwin->w_wcol;
1833 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1834 redrawWinline(curwin->w_cursor.lnum, FALSE);
1835 else
1836#endif
1837 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1838 }
1839}
1840
1841/*
1842 * Called when p_dollar is set: display a '$' at the end of the changed text
1843 * Only works when cursor is in the line that changes.
1844 */
1845 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001846display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847{
1848 colnr_T save_col;
1849
1850 if (!redrawing())
1851 return;
1852
1853 cursor_off();
1854 save_col = curwin->w_cursor.col;
1855 curwin->w_cursor.col = col;
1856#ifdef FEAT_MBYTE
1857 if (has_mbyte)
1858 {
1859 char_u *p;
1860
1861 /* If on the last byte of a multi-byte move to the first byte. */
1862 p = ml_get_curline();
1863 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1864 }
1865#endif
1866 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1867 if (curwin->w_wcol < W_WIDTH(curwin))
1868 {
1869 edit_putchar('$', FALSE);
1870 dollar_vcol = curwin->w_virtcol;
1871 }
1872 curwin->w_cursor.col = save_col;
1873}
1874
1875/*
1876 * Call this function before moving the cursor from the normal insert position
1877 * in insert mode.
1878 */
1879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001880undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001882 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001884 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 redrawWinline(curwin->w_cursor.lnum, FALSE);
1886 }
1887}
1888
1889/*
1890 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1891 * Keep the cursor on the same character.
1892 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1893 * type == INDENT_DEC decrease indent (for CTRL-D)
1894 * type == INDENT_SET set indent to "amount"
1895 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1896 */
1897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001898change_indent(
1899 int type,
1900 int amount,
1901 int round,
1902 int replaced, /* replaced character, put on replace stack */
1903 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904{
1905 int vcol;
1906 int last_vcol;
1907 int insstart_less; /* reduction for Insstart.col */
1908 int new_cursor_col;
1909 int i;
1910 char_u *ptr;
1911 int save_p_list;
1912 int start_col;
1913 colnr_T vc;
1914#ifdef FEAT_VREPLACE
1915 colnr_T orig_col = 0; /* init for GCC */
1916 char_u *new_line, *orig_line = NULL; /* init for GCC */
1917
1918 /* VREPLACE mode needs to know what the line was like before changing */
1919 if (State & VREPLACE_FLAG)
1920 {
1921 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1922 orig_col = curwin->w_cursor.col;
1923 }
1924#endif
1925
1926 /* for the following tricks we don't want list mode */
1927 save_p_list = curwin->w_p_list;
1928 curwin->w_p_list = FALSE;
1929 vc = getvcol_nolist(&curwin->w_cursor);
1930 vcol = vc;
1931
1932 /*
1933 * For Replace mode we need to fix the replace stack later, which is only
1934 * possible when the cursor is in the indent. Remember the number of
1935 * characters before the cursor if it's possible.
1936 */
1937 start_col = curwin->w_cursor.col;
1938
1939 /* determine offset from first non-blank */
1940 new_cursor_col = curwin->w_cursor.col;
1941 beginline(BL_WHITE);
1942 new_cursor_col -= curwin->w_cursor.col;
1943
1944 insstart_less = curwin->w_cursor.col;
1945
1946 /*
1947 * If the cursor is in the indent, compute how many screen columns the
1948 * cursor is to the left of the first non-blank.
1949 */
1950 if (new_cursor_col < 0)
1951 vcol = get_indent() - vcol;
1952
1953 if (new_cursor_col > 0) /* can't fix replace stack */
1954 start_col = -1;
1955
1956 /*
1957 * Set the new indent. The cursor will be put on the first non-blank.
1958 */
1959 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001960 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 else
1962 {
1963#ifdef FEAT_VREPLACE
1964 int save_State = State;
1965
1966 /* Avoid being called recursively. */
1967 if (State & VREPLACE_FLAG)
1968 State = INSERT;
1969#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001970 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971#ifdef FEAT_VREPLACE
1972 State = save_State;
1973#endif
1974 }
1975 insstart_less -= curwin->w_cursor.col;
1976
1977 /*
1978 * Try to put cursor on same character.
1979 * If the cursor is at or after the first non-blank in the line,
1980 * compute the cursor column relative to the column of the first
1981 * non-blank character.
1982 * If we are not in insert mode, leave the cursor on the first non-blank.
1983 * If the cursor is before the first non-blank, position it relative
1984 * to the first non-blank, counted in screen columns.
1985 */
1986 if (new_cursor_col >= 0)
1987 {
1988 /*
1989 * When changing the indent while the cursor is touching it, reset
1990 * Insstart_col to 0.
1991 */
1992 if (new_cursor_col == 0)
1993 insstart_less = MAXCOL;
1994 new_cursor_col += curwin->w_cursor.col;
1995 }
1996 else if (!(State & INSERT))
1997 new_cursor_col = curwin->w_cursor.col;
1998 else
1999 {
2000 /*
2001 * Compute the screen column where the cursor should be.
2002 */
2003 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002004 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
2006 /*
2007 * Advance the cursor until we reach the right screen column.
2008 */
2009 vcol = last_vcol = 0;
2010 new_cursor_col = -1;
2011 ptr = ml_get_curline();
2012 while (vcol <= (int)curwin->w_virtcol)
2013 {
2014 last_vcol = vcol;
2015#ifdef FEAT_MBYTE
2016 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002017 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 else
2019#endif
2020 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002021 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 }
2023 vcol = last_vcol;
2024
2025 /*
2026 * May need to insert spaces to be able to position the cursor on
2027 * the right screen column.
2028 */
2029 if (vcol != (int)curwin->w_virtcol)
2030 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002031 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002033 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (ptr != NULL)
2035 {
2036 new_cursor_col += i;
2037 ptr[i] = NUL;
2038 while (--i >= 0)
2039 ptr[i] = ' ';
2040 ins_str(ptr);
2041 vim_free(ptr);
2042 }
2043 }
2044
2045 /*
2046 * When changing the indent while the cursor is in it, reset
2047 * Insstart_col to 0.
2048 */
2049 insstart_less = MAXCOL;
2050 }
2051
2052 curwin->w_p_list = save_p_list;
2053
2054 if (new_cursor_col <= 0)
2055 curwin->w_cursor.col = 0;
2056 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002057 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 curwin->w_set_curswant = TRUE;
2059 changed_cline_bef_curs();
2060
2061 /*
2062 * May have to adjust the start of the insert.
2063 */
2064 if (State & INSERT)
2065 {
2066 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2067 {
2068 if ((int)Insstart.col <= insstart_less)
2069 Insstart.col = 0;
2070 else
2071 Insstart.col -= insstart_less;
2072 }
2073 if ((int)ai_col <= insstart_less)
2074 ai_col = 0;
2075 else
2076 ai_col -= insstart_less;
2077 }
2078
2079 /*
2080 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2081 * If the number of characters before the cursor decreased, need to pop a
2082 * few characters from the replace stack.
2083 * If the number of characters before the cursor increased, need to push a
2084 * few NULs onto the replace stack.
2085 */
2086 if (REPLACE_NORMAL(State) && start_col >= 0)
2087 {
2088 while (start_col > (int)curwin->w_cursor.col)
2089 {
2090 replace_join(0); /* remove a NUL from the replace stack */
2091 --start_col;
2092 }
2093 while (start_col < (int)curwin->w_cursor.col || replaced)
2094 {
2095 replace_push(NUL);
2096 if (replaced)
2097 {
2098 replace_push(replaced);
2099 replaced = NUL;
2100 }
2101 ++start_col;
2102 }
2103 }
2104
2105#ifdef FEAT_VREPLACE
2106 /*
2107 * For VREPLACE mode, we also have to fix the replace stack. In this case
2108 * it is always possible because we backspace over the whole line and then
2109 * put it back again the way we wanted it.
2110 */
2111 if (State & VREPLACE_FLAG)
2112 {
2113 /* If orig_line didn't allocate, just return. At least we did the job,
2114 * even if you can't backspace. */
2115 if (orig_line == NULL)
2116 return;
2117
2118 /* Save new line */
2119 new_line = vim_strsave(ml_get_curline());
2120 if (new_line == NULL)
2121 return;
2122
2123 /* We only put back the new line up to the cursor */
2124 new_line[curwin->w_cursor.col] = NUL;
2125
2126 /* Put back original line */
2127 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2128 curwin->w_cursor.col = orig_col;
2129
2130 /* Backspace from cursor to start of line */
2131 backspace_until_column(0);
2132
2133 /* Insert new stuff into line again */
2134 ins_bytes(new_line);
2135
2136 vim_free(new_line);
2137 }
2138#endif
2139}
2140
2141/*
2142 * Truncate the space at the end of a line. This is to be used only in an
2143 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2144 * modes.
2145 */
2146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002147truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
2149 int i;
2150
2151 /* find start of trailing white space */
2152 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2153 {
2154 if (State & REPLACE_FLAG)
2155 replace_join(0); /* remove a NUL from the replace stack */
2156 }
2157 line[i + 1] = NUL;
2158}
2159
2160#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2161 || defined(FEAT_COMMENTS) || defined(PROTO)
2162/*
2163 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2164 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002165 * Will attempt not to go before "col" even when there is a composing
2166 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 */
2168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002169backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170{
2171 while ((int)curwin->w_cursor.col > col)
2172 {
2173 curwin->w_cursor.col--;
2174 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002175 replace_do_bs(col);
2176 else if (!del_char_after_col(col))
2177 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179}
2180#endif
2181
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002182/*
2183 * Like del_char(), but make sure not to go before column "limit_col".
2184 * Only matters when there are composing characters.
2185 * Return TRUE when something was deleted.
2186 */
2187 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002188del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002189{
2190#ifdef FEAT_MBYTE
2191 if (enc_utf8 && limit_col >= 0)
2192 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002193 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002194
2195 /* Make sure the cursor is at the start of a character, but
2196 * skip forward again when going too far back because of a
2197 * composing character. */
2198 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002199 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002200 {
2201 int l = utf_ptr2len(ml_get_cursor());
2202
2203 if (l == 0) /* end of line */
2204 break;
2205 curwin->w_cursor.col += l;
2206 }
2207 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2208 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002209 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002210 }
2211 else
2212#endif
2213 (void)del_char(FALSE);
2214 return TRUE;
2215}
2216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2218/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002219 * CTRL-X pressed in Insert mode.
2220 */
2221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002222ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002223{
2224 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2225 * CTRL-V works like CTRL-N */
2226 if (ctrl_x_mode != CTRL_X_CMDLINE)
2227 {
2228 /* if the next ^X<> won't ADD nothing, then reset
2229 * compl_cont_status */
2230 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002231 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002232 else
2233 compl_cont_status = 0;
2234 /* We're not sure which CTRL-X mode it will be yet */
2235 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2236 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2237 edit_submode_pre = NULL;
2238 showmode();
2239 }
2240}
2241
2242/*
2243 * Return TRUE if the 'dict' or 'tsr' option can be used.
2244 */
2245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002246has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002247{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002248 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002249# ifdef FEAT_SPELL
2250 && !curwin->w_p_spell
2251# endif
2252 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002253 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2254 {
2255 ctrl_x_mode = 0;
2256 edit_submode = NULL;
2257 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2258 : (char_u *)_("'thesaurus' option is empty"),
2259 hl_attr(HLF_E));
2260 if (emsg_silent == 0)
2261 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002262 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002263 setcursor();
2264 out_flush();
2265 ui_delay(2000L, FALSE);
2266 }
2267 return FALSE;
2268 }
2269 return TRUE;
2270}
2271
2272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2274 * This depends on the current mode.
2275 */
2276 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002277vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
2279 /* Always allow ^R - let it's results then be checked */
2280 if (c == Ctrl_R)
2281 return TRUE;
2282
Bram Moolenaare3226be2005-12-18 22:10:00 +00002283 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002284 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002285 return TRUE;
2286
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 switch (ctrl_x_mode)
2288 {
2289 case 0: /* Not in any CTRL-X mode */
2290 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2291 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002292 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2294 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2295 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002296 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002297 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 case CTRL_X_SCROLL:
2299 return (c == Ctrl_Y || c == Ctrl_E);
2300 case CTRL_X_WHOLE_LINE:
2301 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2302 case CTRL_X_FILES:
2303 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2304 case CTRL_X_DICTIONARY:
2305 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2306 case CTRL_X_THESAURUS:
2307 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2308 case CTRL_X_TAGS:
2309 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2310#ifdef FEAT_FIND_ID
2311 case CTRL_X_PATH_PATTERNS:
2312 return (c == Ctrl_P || c == Ctrl_N);
2313 case CTRL_X_PATH_DEFINES:
2314 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2315#endif
2316 case CTRL_X_CMDLINE:
2317 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2318 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002319#ifdef FEAT_COMPL_FUNC
2320 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002321 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002322 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002323 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002324#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002325 case CTRL_X_SPELL:
2326 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002327 case CTRL_X_EVAL:
2328 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002330 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 return FALSE;
2332}
2333
2334/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002335 * Return TRUE when character "c" is part of the item currently being
2336 * completed. Used to decide whether to abandon complete mode when the menu
2337 * is visible.
2338 */
2339 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002340ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002341{
2342 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2343 /* When expanding an identifier only accept identifier chars. */
2344 return vim_isIDc(c);
2345
2346 switch (ctrl_x_mode)
2347 {
2348 case CTRL_X_FILES:
2349 /* When expanding file name only accept file name chars. But not
2350 * path separators, so that "proto/<Tab>" expands files in
2351 * "proto", not "proto/" as a whole */
2352 return vim_isfilec(c) && !vim_ispathsep(c);
2353
2354 case CTRL_X_CMDLINE:
2355 case CTRL_X_OMNI:
2356 /* Command line and Omni completion can work with just about any
2357 * printable character, but do stop at white space. */
2358 return vim_isprintc(c) && !vim_iswhite(c);
2359
2360 case CTRL_X_WHOLE_LINE:
2361 /* For while line completion a space can be part of the line. */
2362 return vim_isprintc(c);
2363 }
2364 return vim_iswordc(c);
2365}
2366
2367/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002368 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002370 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 * the rest of the word to be in -- webb
2372 */
2373 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002374ins_compl_add_infercase(
2375 char_u *str,
2376 int len,
2377 int icase,
2378 char_u *fname,
2379 int dir,
2380 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002382 char_u *p;
2383 int i, c;
2384 int actual_len; /* Take multi-byte characters */
2385 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002386 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002387 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 int has_lower = FALSE;
2389 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002391 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002393 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
Bram Moolenaara2993e12007-08-12 14:38:46 +00002395 /* Find actual length of completion. */
2396#ifdef FEAT_MBYTE
2397 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002399 p = str;
2400 actual_len = 0;
2401 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002403 mb_ptr_adv(p);
2404 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
2406 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002407 else
2408#endif
2409 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
Bram Moolenaara2993e12007-08-12 14:38:46 +00002411 /* Find actual length of original text. */
2412#ifdef FEAT_MBYTE
2413 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002415 p = compl_orig_text;
2416 actual_compl_length = 0;
2417 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002419 mb_ptr_adv(p);
2420 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002423 else
2424#endif
2425 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002427 /* "actual_len" may be smaller than "actual_compl_length" when using
2428 * thesaurus, only use the minimum when comparing. */
2429 min_len = actual_len < actual_compl_length
2430 ? actual_len : actual_compl_length;
2431
Bram Moolenaara2993e12007-08-12 14:38:46 +00002432 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002433 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002434 if (wca != NULL)
2435 {
2436 p = str;
2437 for (i = 0; i < actual_len; ++i)
2438#ifdef FEAT_MBYTE
2439 if (has_mbyte)
2440 wca[i] = mb_ptr2char_adv(&p);
2441 else
2442#endif
2443 wca[i] = *(p++);
2444
2445 /* Rule 1: Were any chars converted to lower? */
2446 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002447 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002448 {
2449#ifdef FEAT_MBYTE
2450 if (has_mbyte)
2451 c = mb_ptr2char_adv(&p);
2452 else
2453#endif
2454 c = *(p++);
2455 if (MB_ISLOWER(c))
2456 {
2457 has_lower = TRUE;
2458 if (MB_ISUPPER(wca[i]))
2459 {
2460 /* Rule 1 is satisfied. */
2461 for (i = actual_compl_length; i < actual_len; ++i)
2462 wca[i] = MB_TOLOWER(wca[i]);
2463 break;
2464 }
2465 }
2466 }
2467
2468 /*
2469 * Rule 2: No lower case, 2nd consecutive letter converted to
2470 * upper case.
2471 */
2472 if (!has_lower)
2473 {
2474 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002475 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002476 {
2477#ifdef FEAT_MBYTE
2478 if (has_mbyte)
2479 c = mb_ptr2char_adv(&p);
2480 else
2481#endif
2482 c = *(p++);
2483 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2484 {
2485 /* Rule 2 is satisfied. */
2486 for (i = actual_compl_length; i < actual_len; ++i)
2487 wca[i] = MB_TOUPPER(wca[i]);
2488 break;
2489 }
2490 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2491 }
2492 }
2493
2494 /* Copy the original case of the part we typed. */
2495 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002496 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002497 {
2498#ifdef FEAT_MBYTE
2499 if (has_mbyte)
2500 c = mb_ptr2char_adv(&p);
2501 else
2502#endif
2503 c = *(p++);
2504 if (MB_ISLOWER(c))
2505 wca[i] = MB_TOLOWER(wca[i]);
2506 else if (MB_ISUPPER(c))
2507 wca[i] = MB_TOUPPER(wca[i]);
2508 }
2509
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002510 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002511 * Generate encoding specific output from wide character array.
2512 * Multi-byte characters can occupy up to five bytes more than
2513 * ASCII characters, and we also need one byte for NUL, so stay
2514 * six bytes away from the edge of IObuff.
2515 */
2516 p = IObuff;
2517 i = 0;
2518 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2519#ifdef FEAT_MBYTE
2520 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002521 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002522 else
2523#endif
2524 *(p++) = wca[i++];
2525 *p = NUL;
2526
2527 vim_free(wca);
2528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002530 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2531 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002533 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534}
2535
2536/*
2537 * Add a match to the list of matches.
2538 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002539 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002540 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002542 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002543ins_compl_add(
2544 char_u *str,
2545 int len,
2546 int icase,
2547 char_u *fname,
2548 char_u **cptext, /* extra text for popup menu or NULL */
2549 int cdir,
2550 int flags,
2551 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002553 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002554 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
2556 ui_breakcheck();
2557 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002558 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (len < 0)
2560 len = (int)STRLEN(str);
2561
2562 /*
2563 * If the same match is already present, don't add it.
2564 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002565 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002567 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 do
2569 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002570 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002571 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002572 && match->cp_str[len] == NUL)
2573 return NOTDONE;
2574 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002575 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002578 /* Remove any popup menu before changing the list of matches. */
2579 ins_compl_del_pum();
2580
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 /*
2582 * Allocate a new match structure.
2583 * Copy the values to the new match structure.
2584 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002585 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002587 return FAIL;
2588 match->cp_number = -1;
2589 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002590 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002591 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
2593 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002594 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002596 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002597
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002599 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2601 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002602 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002603 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002604 && compl_curr_match->cp_fname != NULL
2605 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002606 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002607 else if (fname != NULL)
2608 {
2609 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002610 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002613 match->cp_fname = NULL;
2614 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002615
2616 if (cptext != NULL)
2617 {
2618 int i;
2619
2620 for (i = 0; i < CPT_COUNT; ++i)
2621 if (cptext[i] != NULL && *cptext[i] != NUL)
2622 match->cp_text[i] = vim_strsave(cptext[i]);
2623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
2625 /*
2626 * Link the new match structure in the list of matches.
2627 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002628 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002629 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 else if (dir == FORWARD)
2631 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002632 match->cp_next = compl_curr_match->cp_next;
2633 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
2635 else /* BACKWARD */
2636 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002637 match->cp_next = compl_curr_match;
2638 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002640 if (match->cp_next)
2641 match->cp_next->cp_prev = match;
2642 if (match->cp_prev)
2643 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002645 compl_first_match = match;
2646 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002648 /*
2649 * Find the longest common string if still doing that.
2650 */
2651 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2652 ins_compl_longest_match(match);
2653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 return OK;
2655}
2656
2657/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002658 * Return TRUE if "str[len]" matches with match->cp_str, considering
2659 * match->cp_icase.
2660 */
2661 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002662ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002663{
2664 if (match->cp_icase)
2665 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2666 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2667}
2668
2669/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002670 * Reduce the longest common string for match "match".
2671 */
2672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002673ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002674{
2675 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002676 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002677 int had_match;
2678
2679 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002680 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002681 /* First match, use it as a whole. */
2682 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002683 if (compl_leader != NULL)
2684 {
2685 had_match = (curwin->w_cursor.col > compl_col);
2686 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002687 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002688 ins_redraw(FALSE);
2689
2690 /* When the match isn't there (to avoid matching itself) remove it
2691 * again after redrawing. */
2692 if (!had_match)
2693 ins_compl_delete();
2694 compl_used_match = FALSE;
2695 }
2696 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002697 else
2698 {
2699 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002700 p = compl_leader;
2701 s = match->cp_str;
2702 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002703 {
2704#ifdef FEAT_MBYTE
2705 if (has_mbyte)
2706 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002707 c1 = mb_ptr2char(p);
2708 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002709 }
2710 else
2711#endif
2712 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002713 c1 = *p;
2714 c2 = *s;
2715 }
2716 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2717 : (c1 != c2))
2718 break;
2719#ifdef FEAT_MBYTE
2720 if (has_mbyte)
2721 {
2722 mb_ptr_adv(p);
2723 mb_ptr_adv(s);
2724 }
2725 else
2726#endif
2727 {
2728 ++p;
2729 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002730 }
2731 }
2732
2733 if (*p != NUL)
2734 {
2735 /* Leader was shortened, need to change the inserted text. */
2736 *p = NUL;
2737 had_match = (curwin->w_cursor.col > compl_col);
2738 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002739 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002740 ins_redraw(FALSE);
2741
2742 /* When the match isn't there (to avoid matching itself) remove it
2743 * again after redrawing. */
2744 if (!had_match)
2745 ins_compl_delete();
2746 }
2747
2748 compl_used_match = FALSE;
2749 }
2750}
2751
2752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 * Add an array of matches to the list of matches.
2754 * Frees matches[].
2755 */
2756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002757ins_compl_add_matches(
2758 int num_matches,
2759 char_u **matches,
2760 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761{
2762 int i;
2763 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002764 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
Bram Moolenaar572cb562005-08-05 21:35:02 +00002766 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002767 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002768 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002770 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 FreeWild(num_matches, matches);
2772}
2773
2774/* Make the completion list cyclic.
2775 * Return the number of matches (excluding the original).
2776 */
2777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002778ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002780 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 int count = 0;
2782
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002783 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
2785 /*
2786 * Find the end of the list.
2787 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002788 match = compl_first_match;
2789 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002790 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002792 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 ++count;
2794 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002795 match->cp_next = compl_first_match;
2796 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798 return count;
2799}
2800
Bram Moolenaara94bc432006-03-10 21:42:59 +00002801/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002802 * Set variables that store noselect and noinsert behavior from the
2803 * 'completeopt' value.
2804 */
2805 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002806completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002807{
2808 compl_no_insert = FALSE;
2809 compl_no_select = FALSE;
2810 if (strstr((char *)p_cot, "noselect") != NULL)
2811 compl_no_select = TRUE;
2812 if (strstr((char *)p_cot, "noinsert") != NULL)
2813 compl_no_insert = TRUE;
2814}
2815
2816/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002817 * Start completion for the complete() function.
2818 * "startcol" is where the matched text starts (1 is first column).
2819 * "list" is the list of matches.
2820 */
2821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002822set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002823{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002824 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002825 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002826
Bram Moolenaara94bc432006-03-10 21:42:59 +00002827 /* If already doing completions stop it. */
2828 if (ctrl_x_mode != 0)
2829 ins_compl_prep(' ');
2830 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002831 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002832
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002833 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002834 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002835 startcol = curwin->w_cursor.col;
2836 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002837 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002838 /* compl_pattern doesn't need to be set */
2839 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2840 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002841 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002842 return;
2843
Bram Moolenaare4214502015-03-05 18:08:43 +01002844 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002845
2846 ins_compl_add_list(list);
2847 compl_matches = ins_compl_make_cyclic();
2848 compl_started = TRUE;
2849 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002850 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002851
2852 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002853 if (compl_no_insert || compl_no_select)
2854 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002855 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002856 if (compl_no_select)
2857 /* Down/Up has no real effect. */
2858 ins_complete(K_UP, FALSE);
2859 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002860 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002861 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002862 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002863
2864 /* Lazily show the popup menu, unless we got interrupted. */
2865 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002866 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002867 out_flush();
2868}
2869
2870
Bram Moolenaar9372a112005-12-06 19:59:18 +00002871/* "compl_match_array" points the currently displayed list of entries in the
2872 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002873static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002874static int compl_match_arraysize;
2875
2876/*
2877 * Update the screen and when there is any scrolling remove the popup menu.
2878 */
2879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002880ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002881{
2882 int h;
2883
2884 if (compl_match_array != NULL)
2885 {
2886 h = curwin->w_cline_height;
2887 update_screen(0);
2888 if (h != curwin->w_cline_height)
2889 ins_compl_del_pum();
2890 }
2891}
2892
2893/*
2894 * Remove any popup menu.
2895 */
2896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002897ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002898{
2899 if (compl_match_array != NULL)
2900 {
2901 pum_undisplay();
2902 vim_free(compl_match_array);
2903 compl_match_array = NULL;
2904 }
2905}
2906
2907/*
2908 * Return TRUE if the popup menu should be displayed.
2909 */
2910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002911pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002912{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002913 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002914 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002915 return FALSE;
2916
2917 /* The display looks bad on a B&W display. */
2918 if (t_colors < 8
2919#ifdef FEAT_GUI
2920 && !gui.in_use
2921#endif
2922 )
2923 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002924 return TRUE;
2925}
2926
2927/*
2928 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002929 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002930 */
2931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002932pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002933{
2934 compl_T *compl;
2935 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002936
2937 /* Don't display the popup menu if there are no matches or there is only
2938 * one (ignoring the original text). */
2939 compl = compl_first_match;
2940 i = 0;
2941 do
2942 {
2943 if (compl == NULL
2944 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2945 break;
2946 compl = compl->cp_next;
2947 } while (compl != compl_first_match);
2948
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002949 if (strstr((char *)p_cot, "menuone") != NULL)
2950 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002951 return (i >= 2);
2952}
2953
2954/*
2955 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002956 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002957 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002958 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002959ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002960{
2961 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002962 compl_T *shown_compl = NULL;
2963 int did_find_shown_match = FALSE;
2964 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002965 int i;
2966 int cur = -1;
2967 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002968 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002969
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002970 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002971 return;
2972
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002973#if defined(FEAT_EVAL)
2974 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2975 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2976#endif
2977
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002978 /* Update the screen before drawing the popup menu over it. */
2979 update_screen(0);
2980
2981 if (compl_match_array == NULL)
2982 {
2983 /* Need to build the popup menu list. */
2984 compl_match_arraysize = 0;
2985 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002986 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002987 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002988 do
2989 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002990 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2991 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002992 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002993 ++compl_match_arraysize;
2994 compl = compl->cp_next;
2995 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002996 if (compl_match_arraysize == 0)
2997 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002998 compl_match_array = (pumitem_T *)alloc_clear(
2999 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003000 * compl_match_arraysize));
3001 if (compl_match_array != NULL)
3002 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003003 /* If the current match is the original text don't find the first
3004 * match after it, don't highlight anything. */
3005 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3006 shown_match_ok = TRUE;
3007
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003008 i = 0;
3009 compl = compl_first_match;
3010 do
3011 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003012 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3013 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003014 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003015 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003016 if (!shown_match_ok)
3017 {
3018 if (compl == compl_shown_match || did_find_shown_match)
3019 {
3020 /* This item is the shown match or this is the
3021 * first displayed item after the shown match. */
3022 compl_shown_match = compl;
3023 did_find_shown_match = TRUE;
3024 shown_match_ok = TRUE;
3025 }
3026 else
3027 /* Remember this displayed match for when the
3028 * shown match is just below it. */
3029 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003030 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003031 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003032
3033 if (compl->cp_text[CPT_ABBR] != NULL)
3034 compl_match_array[i].pum_text =
3035 compl->cp_text[CPT_ABBR];
3036 else
3037 compl_match_array[i].pum_text = compl->cp_str;
3038 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3039 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3040 if (compl->cp_text[CPT_MENU] != NULL)
3041 compl_match_array[i++].pum_extra =
3042 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003043 else
3044 compl_match_array[i++].pum_extra = compl->cp_fname;
3045 }
3046
3047 if (compl == compl_shown_match)
3048 {
3049 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003050
3051 /* When the original text is the shown match don't set
3052 * compl_shown_match. */
3053 if (compl->cp_flags & ORIGINAL_TEXT)
3054 shown_match_ok = TRUE;
3055
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003056 if (!shown_match_ok && shown_compl != NULL)
3057 {
3058 /* The shown match isn't displayed, set it to the
3059 * previously displayed match. */
3060 compl_shown_match = shown_compl;
3061 shown_match_ok = TRUE;
3062 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003063 }
3064 compl = compl->cp_next;
3065 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003066
3067 if (!shown_match_ok) /* no displayed match at all */
3068 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003069 }
3070 }
3071 else
3072 {
3073 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003074 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003075 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3076 || compl_match_array[i].pum_text
3077 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003078 {
3079 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003080 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003081 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003082 }
3083
3084 if (compl_match_array != NULL)
3085 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003086 /* In Replace mode when a $ is displayed at the end of the line only
3087 * part of the screen would be updated. We do need to redraw here. */
3088 dollar_vcol = -1;
3089
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003090 /* Compute the screen column of the start of the completed text.
3091 * Use the cursor to get all wrapping and other settings right. */
3092 col = curwin->w_cursor.col;
3093 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003094 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003095 curwin->w_cursor.col = col;
3096 }
3097}
3098
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099#define DICT_FIRST (1) /* use just first element in "dict" */
3100#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003101
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003103 * Add any identifiers that match the given pattern in the list of dictionary
3104 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 */
3106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003107ins_compl_dictionaries(
3108 char_u *dict_start,
3109 char_u *pat,
3110 int flags, /* DICT_FIRST and/or DICT_EXACT */
3111 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003113 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 char_u *ptr;
3115 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 char_u **files;
3118 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003120 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121
Bram Moolenaar0b238792006-03-02 22:49:12 +00003122 if (*dict == NUL)
3123 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003124#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003125 /* When 'dictionary' is empty and spell checking is enabled use
3126 * "spell". */
3127 if (!thesaurus && curwin->w_p_spell)
3128 dict = (char_u *)"spell";
3129 else
3130#endif
3131 return;
3132 }
3133
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003135 if (buf == NULL)
3136 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003137 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003138
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 /* If 'infercase' is set, don't use 'smartcase' here */
3140 save_p_scs = p_scs;
3141 if (curbuf->b_p_inf)
3142 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003143
3144 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3145 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003146 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003147 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003148 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003149 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003150 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003151
3152 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003153 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003154 len = STRLEN(pat_esc) + 10;
3155 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003156 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003157 {
3158 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003159 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003160 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003161 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003162 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3163 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003164 vim_free(ptr);
3165 }
3166 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003167 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003168 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003169 if (regmatch.regprog == NULL)
3170 goto theend;
3171 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003172
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3174 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003175 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 {
3177 /* copy one dictionary file name into buf */
3178 if (flags == DICT_EXACT)
3179 {
3180 count = 1;
3181 files = &dict;
3182 }
3183 else
3184 {
3185 /* Expand wildcards in the dictionary name, but do not allow
3186 * backticks (for security, the 'dict' option may have been set in
3187 * a modeline). */
3188 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003189# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003190 if (!thesaurus && STRCMP(buf, "spell") == 0)
3191 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003192 else
3193# endif
3194 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 || expand_wildcards(1, &buf, &count, &files,
3196 EW_FILE|EW_SILENT) != OK)
3197 count = 0;
3198 }
3199
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003200# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003201 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003203 /* Complete from active spelling. Skip "\<" in the pattern, we
3204 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003205 if (pat[0] == '\\' && pat[1] == '<')
3206 ptr = pat + 2;
3207 else
3208 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003209 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003211 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003212# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003213 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003214 {
3215 ins_compl_files(count, files, thesaurus, flags,
3216 &regmatch, buf, &dir);
3217 if (flags != DICT_EXACT)
3218 FreeWild(count, files);
3219 }
3220 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 break;
3222 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003223
3224theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003226 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 vim_free(buf);
3228}
3229
Bram Moolenaar0b238792006-03-02 22:49:12 +00003230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003231ins_compl_files(
3232 int count,
3233 char_u **files,
3234 int thesaurus,
3235 int flags,
3236 regmatch_T *regmatch,
3237 char_u *buf,
3238 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003239{
3240 char_u *ptr;
3241 int i;
3242 FILE *fp;
3243 int add_r;
3244
3245 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3246 {
3247 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3248 if (flags != DICT_EXACT)
3249 {
3250 vim_snprintf((char *)IObuff, IOSIZE,
3251 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003252 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003253 }
3254
3255 if (fp != NULL)
3256 {
3257 /*
3258 * Read dictionary file line by line.
3259 * Check each line for a match.
3260 */
3261 while (!got_int && !compl_interrupted
3262 && !vim_fgets(buf, LSIZE, fp))
3263 {
3264 ptr = buf;
3265 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3266 {
3267 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003268 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003269 ptr = find_line_end(ptr);
3270 else
3271 ptr = find_word_end(ptr);
3272 add_r = ins_compl_add_infercase(regmatch->startp[0],
3273 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003274 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003275 if (thesaurus)
3276 {
3277 char_u *wstart;
3278
3279 /*
3280 * Add the other matches on the line
3281 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003282 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003283 while (!got_int)
3284 {
3285 /* Find start of the next word. Skip white
3286 * space and punctuation. */
3287 ptr = find_word_start(ptr);
3288 if (*ptr == NUL || *ptr == NL)
3289 break;
3290 wstart = ptr;
3291
Bram Moolenaara2993e12007-08-12 14:38:46 +00003292 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003293#ifdef FEAT_MBYTE
3294 if (has_mbyte)
3295 /* Japanese words may have characters in
3296 * different classes, only separate words
3297 * with single-byte non-word characters. */
3298 while (*ptr != NUL)
3299 {
3300 int l = (*mb_ptr2len)(ptr);
3301
3302 if (l < 2 && !vim_iswordc(*ptr))
3303 break;
3304 ptr += l;
3305 }
3306 else
3307#endif
3308 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003309
3310 /* Add the word. Skip the regexp match. */
3311 if (wstart != regmatch->startp[0])
3312 add_r = ins_compl_add_infercase(wstart,
3313 (int)(ptr - wstart),
3314 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003315 }
3316 }
3317 if (add_r == OK)
3318 /* if dir was BACKWARD then honor it just once */
3319 *dir = FORWARD;
3320 else if (add_r == FAIL)
3321 break;
3322 /* avoid expensive call to vim_regexec() when at end
3323 * of line */
3324 if (*ptr == '\n' || got_int)
3325 break;
3326 }
3327 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003328 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003329 }
3330 fclose(fp);
3331 }
3332 }
3333}
3334
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335/*
3336 * Find the start of the next word.
3337 * Returns a pointer to the first char of the word. Also stops at a NUL.
3338 */
3339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003340find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342#ifdef FEAT_MBYTE
3343 if (has_mbyte)
3344 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003345 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 else
3347#endif
3348 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3349 ++ptr;
3350 return ptr;
3351}
3352
3353/*
3354 * Find the end of the word. Assumes it starts inside a word.
3355 * Returns a pointer to just after the word.
3356 */
3357 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003358find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360#ifdef FEAT_MBYTE
3361 int start_class;
3362
3363 if (has_mbyte)
3364 {
3365 start_class = mb_get_class(ptr);
3366 if (start_class > 1)
3367 while (*ptr != NUL)
3368 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003369 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if (mb_get_class(ptr) != start_class)
3371 break;
3372 }
3373 }
3374 else
3375#endif
3376 while (vim_iswordc(*ptr))
3377 ++ptr;
3378 return ptr;
3379}
3380
3381/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003382 * Find the end of the line, omitting CR and NL at the end.
3383 * Returns a pointer to just after the line.
3384 */
3385 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003386find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003387{
3388 char_u *s;
3389
3390 s = ptr + STRLEN(ptr);
3391 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3392 --s;
3393 return s;
3394}
3395
3396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 * Free the list of completions
3398 */
3399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003400ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003402 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003403 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003405 vim_free(compl_pattern);
3406 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003407 vim_free(compl_leader);
3408 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003410 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003412
3413 ins_compl_del_pum();
3414 pum_clear();
3415
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003416 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 do
3418 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003419 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003420 compl_curr_match = compl_curr_match->cp_next;
3421 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003423 if (match->cp_flags & FREE_FNAME)
3424 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003425 for (i = 0; i < CPT_COUNT; ++i)
3426 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003428 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3429 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003430 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431}
3432
3433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003434ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003436 compl_cont_status = 0;
3437 compl_started = FALSE;
3438 compl_matches = 0;
3439 vim_free(compl_pattern);
3440 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003441 vim_free(compl_leader);
3442 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003444 vim_free(compl_orig_text);
3445 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003446 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003447 /* clear v:completed_item */
3448 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449}
3450
3451/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003452 * Return TRUE when Insert completion is active.
3453 */
3454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003455ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003456{
3457 return compl_started;
3458}
3459
3460/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003461 * Delete one character before the cursor and show the subset of the matches
3462 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003463 * Returns the character to be used, NUL if the work is done and another char
3464 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003465 */
3466 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003467ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003468{
3469 char_u *line;
3470 char_u *p;
3471
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003472 line = ml_get_curline();
3473 p = line + curwin->w_cursor.col;
3474 mb_ptr_back(line, p);
3475
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003476 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003477 * allow the word to be deleted, we won't match everything.
3478 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003479 if ((int)(p - line) - (int)compl_col < 0
3480 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003481 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3482 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3483 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003484 return K_BS;
3485
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003486 /* Deleted more than what was used to find matches or didn't finish
3487 * finding all matches: need to look for matches all over again. */
3488 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003489 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003490 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003491
Bram Moolenaara6557602006-02-04 22:43:20 +00003492 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003493 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003494 if (compl_leader != NULL)
3495 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003496 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003497 if (compl_shown_match != NULL)
3498 /* Make sure current match is not a hidden item. */
3499 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003500 return NUL;
3501 }
3502 return K_BS;
3503}
Bram Moolenaara6557602006-02-04 22:43:20 +00003504
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003505/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003506 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3507 * be called.
3508 */
3509 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003510ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003511{
3512 /* Return TRUE if we didn't complete finding matches or when the
3513 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3514 return compl_was_interrupted
3515 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3516 && compl_opt_refresh_always);
3517}
3518
3519/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003520 * Called after changing "compl_leader".
3521 * Show the popup menu with a different set of matches.
3522 * May also search for matches again if the previous search was interrupted.
3523 */
3524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003525ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003526{
3527 ins_compl_del_pum();
3528 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003529 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003530 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003531
3532 if (compl_started)
3533 ins_compl_set_original_text(compl_leader);
3534 else
3535 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003536#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003537 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003538#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003539 /*
3540 * Matches were cleared, need to search for them now. First display
3541 * the changed text before the cursor. Set "compl_restarting" to
3542 * avoid that the first match is inserted.
3543 */
3544 update_screen(0);
3545#ifdef FEAT_GUI
3546 if (gui.in_use)
3547 {
3548 /* Show the cursor after the match, not after the redrawn text. */
3549 setcursor();
3550 out_flush();
3551 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003552 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003553#endif
3554 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003555 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003556 compl_cont_status = 0;
3557 compl_restarting = FALSE;
3558 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003559
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003560 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003561
3562 /* Show the popup menu with a different set of matches. */
3563 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003564
3565 /* Don't let Enter select the original text when there is no popup menu. */
3566 if (compl_match_array == NULL)
3567 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003568}
3569
3570/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003571 * Return the length of the completion, from the completion start column to
3572 * the cursor column. Making sure it never goes below zero.
3573 */
3574 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003575ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003576{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003577 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003578
3579 if (off < 0)
3580 return 0;
3581 return off;
3582}
3583
3584/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003585 * Append one character to the match leader. May reduce the number of
3586 * matches.
3587 */
3588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003589ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003590{
3591#ifdef FEAT_MBYTE
3592 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003593#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003594
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003595 if (stop_arrow() == FAIL)
3596 return;
3597#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003598 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3599 {
3600 char_u buf[MB_MAXBYTES + 1];
3601
3602 (*mb_char2bytes)(c, buf);
3603 buf[cc] = NUL;
3604 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003605 if (compl_opt_refresh_always)
3606 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003607 }
3608 else
3609#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003610 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003611 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003612 if (compl_opt_refresh_always)
3613 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003614 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003615
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003616 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003617 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003618 ins_compl_restart();
3619
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003620 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003621 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003622 * break redo. */
3623 if (!compl_opt_refresh_always)
3624 {
3625 vim_free(compl_leader);
3626 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003627 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003628 if (compl_leader != NULL)
3629 ins_compl_new_leader();
3630 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003631}
3632
3633/*
3634 * Setup for finding completions again without leaving CTRL-X mode. Used when
3635 * BS or a key was typed while still searching for matches.
3636 */
3637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003638ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003639{
3640 ins_compl_free();
3641 compl_started = FALSE;
3642 compl_matches = 0;
3643 compl_cont_status = 0;
3644 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003645}
3646
3647/*
3648 * Set the first match, the original text.
3649 */
3650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003651ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003652{
3653 char_u *p;
3654
3655 /* Replace the original text entry. */
3656 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3657 {
3658 p = vim_strsave(str);
3659 if (p != NULL)
3660 {
3661 vim_free(compl_first_match->cp_str);
3662 compl_first_match->cp_str = p;
3663 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003664 }
3665}
3666
3667/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003668 * Append one character to the match leader. May reduce the number of
3669 * matches.
3670 */
3671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003672ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003673{
3674 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003675 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003676 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003677 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003678
3679 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003680 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003681 {
3682 /* When still at the original match use the first entry that matches
3683 * the leader. */
3684 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3685 {
3686 p = NULL;
3687 for (cp = compl_shown_match->cp_next; cp != NULL
3688 && cp != compl_first_match; cp = cp->cp_next)
3689 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003690 if (compl_leader == NULL
3691 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003692 (int)STRLEN(compl_leader)))
3693 {
3694 p = cp->cp_str;
3695 break;
3696 }
3697 }
3698 if (p == NULL || (int)STRLEN(p) <= len)
3699 return;
3700 }
3701 else
3702 return;
3703 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003704 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003705 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003706 ins_compl_addleader(c);
3707}
3708
3709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003711 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003712 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003715ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
3717 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003719 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
3721 /* Forget any previous 'special' messages if this is actually
3722 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3723 */
3724 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3725 edit_submode_extra = NULL;
3726
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003727 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003728 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3729 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003730 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003732 /* Set "compl_get_longest" when finding the first matches. */
3733 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3734 || (ctrl_x_mode == 0 && !compl_started))
3735 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003736 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003737 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003738
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003739 }
3740
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3742 {
3743 /*
3744 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3745 * it will be yet. Now we decide.
3746 */
3747 switch (c)
3748 {
3749 case Ctrl_E:
3750 case Ctrl_Y:
3751 ctrl_x_mode = CTRL_X_SCROLL;
3752 if (!(State & REPLACE_FLAG))
3753 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3754 else
3755 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3756 edit_submode_pre = NULL;
3757 showmode();
3758 break;
3759 case Ctrl_L:
3760 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3761 break;
3762 case Ctrl_F:
3763 ctrl_x_mode = CTRL_X_FILES;
3764 break;
3765 case Ctrl_K:
3766 ctrl_x_mode = CTRL_X_DICTIONARY;
3767 break;
3768 case Ctrl_R:
3769 /* Simply allow ^R to happen without affecting ^X mode */
3770 break;
3771 case Ctrl_T:
3772 ctrl_x_mode = CTRL_X_THESAURUS;
3773 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003774#ifdef FEAT_COMPL_FUNC
3775 case Ctrl_U:
3776 ctrl_x_mode = CTRL_X_FUNCTION;
3777 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003778 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003779 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003780 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003781#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003782 case 's':
3783 case Ctrl_S:
3784 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003785#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003786 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003787 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003788 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003789#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003790 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 case Ctrl_RSB:
3792 ctrl_x_mode = CTRL_X_TAGS;
3793 break;
3794#ifdef FEAT_FIND_ID
3795 case Ctrl_I:
3796 case K_S_TAB:
3797 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3798 break;
3799 case Ctrl_D:
3800 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3801 break;
3802#endif
3803 case Ctrl_V:
3804 case Ctrl_Q:
3805 ctrl_x_mode = CTRL_X_CMDLINE;
3806 break;
3807 case Ctrl_P:
3808 case Ctrl_N:
3809 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3810 * just started ^X mode, or there were enough ^X's to cancel
3811 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3812 * do normal expansion when interrupting a different mode (say
3813 * ^X^F^X^P or ^P^X^X^P, see below)
3814 * nothing changes if interrupting mode 0, (eg, the flag
3815 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003816 if (!(compl_cont_status & CONT_INTRPT))
3817 compl_cont_status |= CONT_LOCAL;
3818 else if (compl_cont_mode != 0)
3819 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 /* FALLTHROUGH */
3821 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003822 /* If we have typed at least 2 ^X's... for modes != 0, we set
3823 * compl_cont_status = 0 (eg, as if we had just started ^X
3824 * mode).
3825 * For mode 0, we set "compl_cont_mode" to an impossible
3826 * value, in both cases ^X^X can be used to restart the same
3827 * mode (avoiding ADDING mode).
3828 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3829 * 'complete' and local ^P expansions respectively.
3830 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3831 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (c == Ctrl_X)
3833 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003834 if (compl_cont_mode != 0)
3835 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003837 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839 ctrl_x_mode = 0;
3840 edit_submode = NULL;
3841 showmode();
3842 break;
3843 }
3844 }
3845 else if (ctrl_x_mode != 0)
3846 {
3847 /* We're already in CTRL-X mode, do we stay in it? */
3848 if (!vim_is_ctrl_x_key(c))
3849 {
3850 if (ctrl_x_mode == CTRL_X_SCROLL)
3851 ctrl_x_mode = 0;
3852 else
3853 ctrl_x_mode = CTRL_X_FINISHED;
3854 edit_submode = NULL;
3855 }
3856 showmode();
3857 }
3858
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003859 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
3861 /* Show error message from attempted keyword completion (probably
3862 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003863 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003865 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3866 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 || ctrl_x_mode == CTRL_X_FINISHED)
3868 {
3869 /* Get here when we have finished typing a sequence of ^N and
3870 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003872 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
3874 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003875 * If any of the original typed text has been changed, eg when
3876 * ignorecase is set, we must add back-spaces to the redo
3877 * buffer. We add as few as necessary to delete just the part
3878 * of the original text that has changed.
3879 * When using the longest match, edited the match or used
3880 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003882 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3883 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003884 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003885 ptr = NULL;
3886 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 }
3888
3889#ifdef FEAT_CINDENT
3890 want_cindent = (can_cindent && cindent_on());
3891#endif
3892 /*
3893 * When completing whole lines: fix indent for 'cindent'.
3894 * Otherwise, break line if it's too long.
3895 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003896 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
3898#ifdef FEAT_CINDENT
3899 /* re-indent the current line */
3900 if (want_cindent)
3901 {
3902 do_c_expr_indent();
3903 want_cindent = FALSE; /* don't do it again */
3904 }
3905#endif
3906 }
3907 else
3908 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003909 int prev_col = curwin->w_cursor.col;
3910
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003912 if (prev_col > 0)
3913 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003914 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003915 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003917 if (prev_col > 0
3918 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3919 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003922 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003923 * the selection without inserting anything. When
3924 * compl_enter_selects is set the Enter key does the same. */
3925 if ((c == Ctrl_Y || (compl_enter_selects
3926 && (c == CAR || c == K_KENTER || c == NL)))
3927 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003928 retval = TRUE;
3929
Bram Moolenaar47247282016-08-02 22:36:02 +02003930 /* CTRL-E means completion is Ended, go back to the typed text.
3931 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003932 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003933 {
3934 ins_compl_delete();
3935 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003936 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003937 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003938 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003939 retval = TRUE;
3940 }
3941
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003942 auto_format(FALSE, TRUE);
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003945 compl_started = FALSE;
3946 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003947 if (!shortmess(SHM_COMPLETIONMENU))
3948 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003950 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 if (edit_submode != NULL)
3952 {
3953 edit_submode = NULL;
3954 showmode();
3955 }
3956
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003957#ifdef FEAT_CMDWIN
3958 if (c == Ctrl_C && cmdwin_type != 0)
3959 /* Avoid the popup menu remains displayed when leaving the
3960 * command line window. */
3961 update_screen(0);
3962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963#ifdef FEAT_CINDENT
3964 /*
3965 * Indent now if a key was typed that is in 'cinkeys'.
3966 */
3967 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3968 do_c_expr_indent();
3969#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003970#ifdef FEAT_AUTOCMD
3971 /* Trigger the CompleteDone event to give scripts a chance to act
3972 * upon the completion. */
3973 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
3976 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003977#ifdef FEAT_AUTOCMD
3978 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3979 /* Trigger the CompleteDone event to give scripts a chance to act
3980 * upon the (possibly failed) completion. */
3981 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
3984 /* reset continue_* if we left expansion-mode, if we stay they'll be
3985 * (re)set properly in ins_complete() */
3986 if (!vim_is_ctrl_x_key(c))
3987 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003988 compl_cont_status = 0;
3989 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003991
3992 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993}
3994
3995/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003996 * Fix the redo buffer for the completion leader replacing some of the typed
3997 * text. This inserts backspaces and appends the changed text.
3998 * "ptr" is the known leader text or NUL.
3999 */
4000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004002{
4003 int len;
4004 char_u *p;
4005 char_u *ptr = ptr_arg;
4006
4007 if (ptr == NULL)
4008 {
4009 if (compl_leader != NULL)
4010 ptr = compl_leader;
4011 else
4012 return; /* nothing to do */
4013 }
4014 if (compl_orig_text != NULL)
4015 {
4016 p = compl_orig_text;
4017 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4018 ;
4019#ifdef FEAT_MBYTE
4020 if (len > 0)
4021 len -= (*mb_head_off)(p, p + len);
4022#endif
4023 for (p += len; *p != NUL; mb_ptr_adv(p))
4024 AppendCharToRedobuff(K_BS);
4025 }
4026 else
4027 len = 0;
4028 if (ptr != NULL)
4029 AppendToRedobuffLit(ptr + len, -1);
4030}
4031
4032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4034 * (depending on flag) starting from buf and looking for a non-scanned
4035 * buffer (other than curbuf). curbuf is special, if it is called with
4036 * buf=curbuf then it has to be the first call for a given flag/expansion.
4037 *
4038 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4039 */
4040 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004041ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042{
4043#ifdef FEAT_WINDOWS
4044 static win_T *wp;
4045#endif
4046
4047 if (flag == 'w') /* just windows */
4048 {
4049#ifdef FEAT_WINDOWS
4050 if (buf == curbuf) /* first call for this flag/expansion */
4051 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004052 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 && wp->w_buffer->b_scanned)
4054 ;
4055 buf = wp->w_buffer;
4056#else
4057 buf = curbuf;
4058#endif
4059 }
4060 else
4061 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4062 * (unlisted buffers)
4063 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004064 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 && ((flag == 'U'
4066 ? buf->b_p_bl
4067 : (!buf->b_p_bl
4068 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004069 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 ;
4071 return buf;
4072}
4073
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004074#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004075/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004076 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004077 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004078 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004080expand_by_function(
4081 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4082 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004083{
Bram Moolenaar82139082011-09-14 16:52:09 +02004084 list_T *matchlist = NULL;
4085 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004086 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004087 char_u *funcname;
4088 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004089 win_T *curwin_save;
4090 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004091 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004092
Bram Moolenaare344bea2005-09-01 20:46:49 +00004093 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4094 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004095 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004096
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004097 /* Call 'completefunc' to obtain the list of matches. */
4098 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004099 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004100
Bram Moolenaare344bea2005-09-01 20:46:49 +00004101 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004102 curwin_save = curwin;
4103 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004104
4105 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004106 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004107 {
4108 switch (rettv.v_type)
4109 {
4110 case VAR_LIST:
4111 matchlist = rettv.vval.v_list;
4112 break;
4113 case VAR_DICT:
4114 matchdict = rettv.vval.v_dict;
4115 break;
4116 default:
4117 /* TODO: Give error message? */
4118 clear_tv(&rettv);
4119 break;
4120 }
4121 }
4122
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004123 if (curwin_save != curwin || curbuf_save != curbuf)
4124 {
4125 EMSG(_(e_complwin));
4126 goto theend;
4127 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004128 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004129 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004130 if (!equalpos(curwin->w_cursor, pos))
4131 {
4132 EMSG(_(e_compldel));
4133 goto theend;
4134 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004135
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004136 if (matchlist != NULL)
4137 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004138 else if (matchdict != NULL)
4139 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004140
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004141theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004142 if (matchdict != NULL)
4143 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004144 if (matchlist != NULL)
4145 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004146}
4147#endif /* FEAT_COMPL_FUNC */
4148
Bram Moolenaar39f05632006-03-19 22:15:26 +00004149#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004150/*
4151 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004152 */
4153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004154ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004155{
4156 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004157 int dir = compl_direction;
4158
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004159 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004160 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004161 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004162 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4163 /* if dir was BACKWARD then honor it just once */
4164 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004165 else if (did_emsg)
4166 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004167 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004168}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004169
4170/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004171 * Add completions from a dict.
4172 */
4173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004174ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004175{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004176 dictitem_T *di_refresh;
4177 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004178
4179 /* Check for optional "refresh" item. */
4180 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004181 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4182 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004183 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004184 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004185
4186 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4187 compl_opt_refresh_always = TRUE;
4188 }
4189
4190 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004191 di_words = dict_find(dict, (char_u *)"words", 5);
4192 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4193 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004194}
4195
4196/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004197 * Add a match to the list of matches from a typeval_T.
4198 * If the given string is already in the list of completions, then return
4199 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4200 * maybe because alloc() returns NULL, then FAIL is returned.
4201 */
4202 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004203ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004204{
4205 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004206 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004207 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004208 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004209 char_u *(cptext[CPT_COUNT]);
4210
4211 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4212 {
4213 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4214 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4215 (char_u *)"abbr", FALSE);
4216 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4217 (char_u *)"menu", FALSE);
4218 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4219 (char_u *)"kind", FALSE);
4220 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4221 (char_u *)"info", FALSE);
4222 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4223 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004224 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004225 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004226 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4227 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004228 }
4229 else
4230 {
4231 word = get_tv_string_chk(tv);
4232 vim_memset(cptext, 0, sizeof(cptext));
4233 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004234 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004235 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004236 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004237}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004238#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004239
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004240/*
4241 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004242 * The search starts at position "ini" in curbuf and in the direction
4243 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004244 * When "compl_started" is FALSE start at that position, otherwise continue
4245 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246 * This may return before finding all the matches.
4247 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 */
4249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004250ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
4252 static pos_T first_match_pos;
4253 static pos_T last_match_pos;
4254 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004255 static int found_all = FALSE; /* Found all matches of a
4256 certain type. */
4257 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
Bram Moolenaar572cb562005-08-05 21:35:02 +00004259 pos_T *pos;
4260 char_u **matches;
4261 int save_p_scs;
4262 int save_p_ws;
4263 int save_p_ic;
4264 int i;
4265 int num_matches;
4266 int len;
4267 int found_new_match;
4268 int type = ctrl_x_mode;
4269 char_u *ptr;
4270 char_u *dict = NULL;
4271 int dict_f = 0;
4272 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004273 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004275 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004277 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 ins_buf->b_scanned = 0;
4279 found_all = FALSE;
4280 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004281 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004282 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 last_match_pos = first_match_pos = *ini;
4284 }
4285
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004286 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004287 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4289 for (;;)
4290 {
4291 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004292 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004294 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 * or if found_all says this entry is done. For ^X^L only use the
4296 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004297 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004298 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 {
4300 found_all = FALSE;
4301 while (*e_cpt == ',' || *e_cpt == ' ')
4302 e_cpt++;
4303 if (*e_cpt == '.' && !curbuf->b_scanned)
4304 {
4305 ins_buf = curbuf;
4306 first_match_pos = *ini;
4307 /* So that ^N can match word immediately after cursor */
4308 if (ctrl_x_mode == 0)
4309 dec(&first_match_pos);
4310 last_match_pos = first_match_pos;
4311 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004312
4313 /* Remember the first match so that the loop stops when we
4314 * wrap and come back there a second time. */
4315 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4318 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4319 {
4320 /* Scan a buffer, but not the current one. */
4321 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4322 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 first_match_pos.col = last_match_pos.col = 0;
4325 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4326 last_match_pos.lnum = 0;
4327 type = 0;
4328 }
4329 else /* unloaded buffer, scan like dictionary */
4330 {
4331 found_all = TRUE;
4332 if (ins_buf->b_fname == NULL)
4333 continue;
4334 type = CTRL_X_DICTIONARY;
4335 dict = ins_buf->b_fname;
4336 dict_f = DICT_EXACT;
4337 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004338 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 ins_buf->b_fname == NULL
4340 ? buf_spname(ins_buf)
4341 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004342 ? ins_buf->b_fname
4343 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004344 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
4346 else if (*e_cpt == NUL)
4347 break;
4348 else
4349 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004350 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 type = -1;
4352 else if (*e_cpt == 'k' || *e_cpt == 's')
4353 {
4354 if (*e_cpt == 'k')
4355 type = CTRL_X_DICTIONARY;
4356 else
4357 type = CTRL_X_THESAURUS;
4358 if (*++e_cpt != ',' && *e_cpt != NUL)
4359 {
4360 dict = e_cpt;
4361 dict_f = DICT_FIRST;
4362 }
4363 }
4364#ifdef FEAT_FIND_ID
4365 else if (*e_cpt == 'i')
4366 type = CTRL_X_PATH_PATTERNS;
4367 else if (*e_cpt == 'd')
4368 type = CTRL_X_PATH_DEFINES;
4369#endif
4370 else if (*e_cpt == ']' || *e_cpt == 't')
4371 {
4372 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004373 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004374 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 else
4377 type = -1;
4378
4379 /* in any case e_cpt is advanced to the next entry */
4380 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4381
4382 found_all = TRUE;
4383 if (type == -1)
4384 continue;
4385 }
4386 }
4387
4388 switch (type)
4389 {
4390 case -1:
4391 break;
4392#ifdef FEAT_FIND_ID
4393 case CTRL_X_PATH_PATTERNS:
4394 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004395 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004396 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4400 (linenr_T)1, (linenr_T)MAXLNUM);
4401 break;
4402#endif
4403
4404 case CTRL_X_DICTIONARY:
4405 case CTRL_X_THESAURUS:
4406 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004407 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 : (type == CTRL_X_THESAURUS
4409 ? (*curbuf->b_p_tsr == NUL
4410 ? p_tsr
4411 : curbuf->b_p_tsr)
4412 : (*curbuf->b_p_dict == NUL
4413 ? p_dict
4414 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004415 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004416 dict != NULL ? dict_f
4417 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 dict = NULL;
4419 break;
4420
4421 case CTRL_X_TAGS:
4422 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4423 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004426 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427 * of matches is found when compl_pattern is empty */
4428 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4430 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4431 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4432 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004433 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
4435 p_ic = save_p_ic;
4436 break;
4437
4438 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004439 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4441 {
4442
4443 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004445 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447 break;
4448
4449 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004450 if (expand_cmdline(&compl_xp, compl_pattern,
4451 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004453 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 break;
4455
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004456#ifdef FEAT_COMPL_FUNC
4457 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004458 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004459 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004460 break;
4461#endif
4462
Bram Moolenaar488c6512005-08-11 20:09:58 +00004463 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004464#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004465 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004466 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004467 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004468 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004469#endif
4470 break;
4471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 default: /* normal ^P/^N and ^X^L */
4473 /*
4474 * If 'infercase' is set, don't use 'smartcase' here
4475 */
4476 save_p_scs = p_scs;
4477 if (ins_buf->b_p_inf)
4478 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004479
Bram Moolenaar361aa502014-01-23 22:45:58 +01004480 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 * end but never from the middle, thus setting nowrapscan in this
4482 * buffers is a good idea, on the other hand, we always set
4483 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4484 save_p_ws = p_ws;
4485 if (ins_buf != curbuf)
4486 p_ws = FALSE;
4487 else if (*e_cpt == '.')
4488 p_ws = TRUE;
4489 for (;;)
4490 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004491 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004493 ++msg_silent; /* Don't want messages for wrapscan. */
4494
Bram Moolenaare4214502015-03-05 18:08:43 +01004495 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4496 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004497 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004498 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004499 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004501 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004503 found_new_match = searchit(NULL, ins_buf, pos,
4504 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004506 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004507 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004508 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004510 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004511 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 first_match_pos = *pos;
4513 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004514 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004517 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 found_new_match = FAIL;
4519 if (found_new_match == FAIL)
4520 {
4521 if (ins_buf == curbuf)
4522 found_all = TRUE;
4523 break;
4524 }
4525
4526 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004527 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 && ini->lnum == pos->lnum
4529 && ini->col == pos->col)
4530 continue;
4531 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004532 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004534 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
4536 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4537 continue;
4538 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4539 if (!p_paste)
4540 ptr = skipwhite(ptr);
4541 }
4542 len = (int)STRLEN(ptr);
4543 }
4544 else
4545 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004546 char_u *tmp_ptr = ptr;
4547
4548 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004550 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 /* Skip if already inside a word. */
4552 if (vim_iswordp(tmp_ptr))
4553 continue;
4554 /* Find start of next word. */
4555 tmp_ptr = find_word_start(tmp_ptr);
4556 }
4557 /* Find end of this word. */
4558 tmp_ptr = find_word_end(tmp_ptr);
4559 len = (int)(tmp_ptr - ptr);
4560
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004561 if ((compl_cont_status & CONT_ADDING)
4562 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
4564 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4565 {
4566 /* Try next line, if any. the new word will be
4567 * "join" as if the normal command "J" was used.
4568 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004569 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 * works -- Acevedo */
4571 STRNCPY(IObuff, ptr, len);
4572 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4573 tmp_ptr = ptr = skipwhite(ptr);
4574 /* Find start of next word. */
4575 tmp_ptr = find_word_start(tmp_ptr);
4576 /* Find end of next word. */
4577 tmp_ptr = find_word_end(tmp_ptr);
4578 if (tmp_ptr > ptr)
4579 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004580 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004582 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 IObuff[len++] = ' ';
4584 /* IObuf =~ "\k.* ", thus len >= 2 */
4585 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004586 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 || (vim_strchr(p_cpo, CPO_JOINSP)
4588 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004589 && (IObuff[len - 2] == '?'
4590 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 IObuff[len++] = ' ';
4592 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004593 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (tmp_ptr - ptr >= IOSIZE - len)
4595 tmp_ptr = ptr + IOSIZE - len - 1;
4596 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4597 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004598 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
4600 IObuff[len] = NUL;
4601 ptr = IObuff;
4602 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004603 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 continue;
4605 }
4606 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004607 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004608 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004609 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
4611 found_new_match = OK;
4612 break;
4613 }
4614 }
4615 p_scs = save_p_scs;
4616 p_ws = save_p_ws;
4617 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004618
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004619 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004620 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004621 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 found_new_match = OK;
4623
4624 /* break the loop for specialized modes (use 'complete' just for the
4625 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004626 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004627 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004628 {
4629 if (got_int)
4630 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004631 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004632 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004633 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004634
Bram Moolenaare4214502015-03-05 18:08:43 +01004635 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004636 || compl_interrupted)
4637 break;
4638 compl_started = TRUE;
4639 }
4640 else
4641 {
4642 /* Mark a buffer scanned when it has been scanned completely */
4643 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4644 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004646 compl_started = FALSE;
4647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004649 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650
Bram Moolenaare4214502015-03-05 18:08:43 +01004651 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 && *e_cpt == NUL) /* Got to end of 'complete' */
4653 found_new_match = FAIL;
4654
4655 i = -1; /* total of matches, unknown */
4656 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004657 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 i = ins_compl_make_cyclic();
4659
4660 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004661 * just been made cyclic then we have to move compl_curr_match to the next
4662 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004663 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4664 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004665 if (compl_curr_match == NULL)
4666 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 return i;
4668}
4669
4670/* Delete the old text being completed. */
4671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004672ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004674 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 /*
4677 * In insert mode: Delete the typed part.
4678 * In replace mode: Put the old characters back, if any.
4679 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004680 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4681 if ((int)curwin->w_cursor.col > col)
4682 {
4683 if (stop_arrow() == FAIL)
4684 return;
4685 backspace_until_column(col);
4686 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004687
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004688 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4689 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004691 /* clear v:completed_item */
4692 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693}
4694
Bram Moolenaar472e8592016-10-15 17:06:47 +02004695/*
4696 * Insert the new text being completed.
4697 * "in_compl_func" is TRUE when called from complete_check().
4698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004700ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004702 dict_T *dict;
4703
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004704 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004705 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4706 compl_used_match = FALSE;
4707 else
4708 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004709
4710 /* Set completed item. */
4711 /* { word, abbr, menu, kind, info } */
4712 dict = dict_alloc();
4713 if (dict != NULL)
4714 {
4715 dict_add_nr_str(dict, "word", 0L,
4716 EMPTY_IF_NULL(compl_shown_match->cp_str));
4717 dict_add_nr_str(dict, "abbr", 0L,
4718 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4719 dict_add_nr_str(dict, "menu", 0L,
4720 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4721 dict_add_nr_str(dict, "kind", 0L,
4722 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4723 dict_add_nr_str(dict, "info", 0L,
4724 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4725 }
4726 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004727 if (!in_compl_func)
4728 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729}
4730
4731/*
4732 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004733 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4734 * get more completions. If it is FALSE, then we just do nothing when there
4735 * are no more completions in a given direction. The latter case is used when
4736 * we are still in the middle of finding completions, to allow browsing
4737 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 * Return the total number of matches, or -1 if still unknown -- webb.
4739 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004740 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4741 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 *
4743 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004744 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4745 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 */
4747 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004748ins_compl_next(
4749 int allow_get_expansion,
4750 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004751 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004752 int insert_match, /* Insert the newly selected match */
4753 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
4755 int num_matches = -1;
4756 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004757 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004758 compl_T *found_compl = NULL;
4759 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004760 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004761 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004763 /* When user complete function return -1 for findstart which is next
4764 * time of 'always', compl_shown_match become NULL. */
4765 if (compl_shown_match == NULL)
4766 return -1;
4767
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004768 if (compl_leader != NULL
4769 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004771 /* Set "compl_shown_match" to the actually shown match, it may differ
4772 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004773 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004774 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004775 && compl_shown_match->cp_next != NULL
4776 && compl_shown_match->cp_next != compl_first_match)
4777 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004778
4779 /* If we didn't find it searching forward, and compl_shows_dir is
4780 * backward, find the last match. */
4781 if (compl_shows_dir == BACKWARD
4782 && !ins_compl_equal(compl_shown_match,
4783 compl_leader, (int)STRLEN(compl_leader))
4784 && (compl_shown_match->cp_next == NULL
4785 || compl_shown_match->cp_next == compl_first_match))
4786 {
4787 while (!ins_compl_equal(compl_shown_match,
4788 compl_leader, (int)STRLEN(compl_leader))
4789 && compl_shown_match->cp_prev != NULL
4790 && compl_shown_match->cp_prev != compl_first_match)
4791 compl_shown_match = compl_shown_match->cp_prev;
4792 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004793 }
4794
4795 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004796 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 /* Delete old text to be replaced */
4798 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004799
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004800 /* When finding the longest common text we stick at the original text,
4801 * don't let CTRL-N or CTRL-P move to the first match. */
4802 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4803
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004804 /* When restarting the search don't insert the first match either. */
4805 if (compl_restarting)
4806 {
4807 advance = FALSE;
4808 compl_restarting = FALSE;
4809 }
4810
Bram Moolenaare3226be2005-12-18 22:10:00 +00004811 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4812 * around. */
4813 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004815 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004817 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004818 found_end = (compl_first_match != NULL
4819 && (compl_shown_match->cp_next == compl_first_match
4820 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004821 }
4822 else if (compl_shows_dir == BACKWARD
4823 && compl_shown_match->cp_prev != NULL)
4824 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004825 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004826 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004827 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004830 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004831 if (!allow_get_expansion)
4832 {
4833 if (advance)
4834 {
4835 if (compl_shows_dir == BACKWARD)
4836 compl_pending -= todo + 1;
4837 else
4838 compl_pending += todo + 1;
4839 }
4840 return -1;
4841 }
4842
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004843 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004844 {
4845 if (compl_shows_dir == BACKWARD)
4846 --compl_pending;
4847 else
4848 ++compl_pending;
4849 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004850
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004851 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004852 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004853
4854 /* handle any pending completions */
4855 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004856 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004857 {
4858 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4859 {
4860 compl_shown_match = compl_shown_match->cp_next;
4861 --compl_pending;
4862 }
4863 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4864 {
4865 compl_shown_match = compl_shown_match->cp_prev;
4866 ++compl_pending;
4867 }
4868 else
4869 break;
4870 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004871 found_end = FALSE;
4872 }
4873 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4874 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004875 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004876 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004877 ++todo;
4878 else
4879 /* Remember a matching item. */
4880 found_compl = compl_shown_match;
4881
4882 /* Stop at the end of the list when we found a usable match. */
4883 if (found_end)
4884 {
4885 if (found_compl != NULL)
4886 {
4887 compl_shown_match = found_compl;
4888 break;
4889 }
4890 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
4893
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004894 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004895 if (compl_no_insert && !started)
4896 {
4897 ins_bytes(compl_orig_text + ins_compl_len());
4898 compl_used_match = FALSE;
4899 }
4900 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004901 {
4902 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004903 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004904 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004905 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004906 }
4907 else
4908 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
4910 if (!allow_get_expansion)
4911 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004912 /* may undisplay the popup menu first */
4913 ins_compl_upd_pum();
4914
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004915 /* redraw to show the user what was inserted */
4916 update_screen(0);
4917
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004918 /* display the updated popup menu */
4919 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004920#ifdef FEAT_GUI
4921 if (gui.in_use)
4922 {
4923 /* Show the cursor after the match, not after the redrawn text. */
4924 setcursor();
4925 out_flush();
4926 gui_update_cursor(FALSE, FALSE);
4927 }
4928#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 /* Delete old text to be replaced, since we're still searching and
4931 * don't want to match ourselves! */
4932 ins_compl_delete();
4933 }
4934
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004935 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004936 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004937 if (compl_no_insert && !started)
4938 compl_enter_selects = TRUE;
4939 else
4940 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004941
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 /*
4943 * Show the file name for the match (if any)
4944 * Truncate the file name to avoid a wait for return.
4945 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004946 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
4948 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004949 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 if (i <= 0)
4951 i = 0;
4952 else
4953 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004954 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 msg(IObuff);
4956 redraw_cmdline = FALSE; /* don't overwrite! */
4957 }
4958
4959 return num_matches;
4960}
4961
4962/*
4963 * Call this while finding completions, to check whether the user has hit a key
4964 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004965 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004967 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004968 * "in_compl_func" is TRUE when called from complete_check(), don't set
4969 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 */
4971 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004972ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
4974 static int count = 0;
4975
4976 int c;
4977
4978 /* Don't check when reading keys from a script. That would break the test
4979 * scripts */
4980 if (using_script())
4981 return;
4982
4983 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004984 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 return;
4986 count = 0;
4987
Bram Moolenaara260a972006-06-23 19:36:29 +00004988 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4989 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (c != NUL)
4992 {
4993 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4994 {
4995 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004996 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004997 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02004998 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005000 else
5001 {
5002 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005003 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005004 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005005 if (c != K_IGNORE)
5006 {
5007 /* Don't interrupt completion when the character wasn't typed,
5008 * e.g., when doing @q to replay keys. */
5009 if (c != Ctrl_R && KeyTyped)
5010 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005011
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005012 vungetc(c);
5013 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005016 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005017 {
5018 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5019
5020 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005021 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005022 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005023}
5024
5025/*
5026 * Decide the direction of Insert mode complete from the key typed.
5027 * Returns BACKWARD or FORWARD.
5028 */
5029 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005030ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005031{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005032 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005033 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005034 return BACKWARD;
5035 return FORWARD;
5036}
5037
5038/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005039 * Return TRUE for keys that are used for completion only when the popup menu
5040 * is visible.
5041 */
5042 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005043ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005044{
5045 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005046 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5047 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005048}
5049
5050/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005051 * Decide the number of completions to move forward.
5052 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5053 */
5054 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005055ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005056{
5057 int h;
5058
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005059 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005060 {
5061 h = pum_get_height();
5062 if (h > 3)
5063 h -= 2; /* keep some context */
5064 return h;
5065 }
5066 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067}
5068
5069/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005070 * Return TRUE if completion with "c" should insert the match, FALSE if only
5071 * to change the currently selected completion.
5072 */
5073 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005074ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005075{
5076 switch (c)
5077 {
5078 case K_UP:
5079 case K_DOWN:
5080 case K_PAGEDOWN:
5081 case K_KPAGEDOWN:
5082 case K_S_DOWN:
5083 case K_PAGEUP:
5084 case K_KPAGEUP:
5085 case K_S_UP:
5086 return FALSE;
5087 }
5088 return TRUE;
5089}
5090
5091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 * Do Insert mode completion.
5093 * Called when character "c" was typed, which has a meaning for completion.
5094 * Returns OK if completion was done, FAIL if something failed (out of mem).
5095 */
5096 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005097ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005099 char_u *line;
5100 int startcol = 0; /* column where searched text starts */
5101 colnr_T curs_col; /* cursor column */
5102 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005103 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005104 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005105 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005106 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
Bram Moolenaare3226be2005-12-18 22:10:00 +00005108 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005109 insert_match = ins_compl_use_match(c);
5110
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005111 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 /* First time we hit ^N or ^P (in a row, I mean) */
5114
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 did_ai = FALSE;
5116#ifdef FEAT_SMARTINDENT
5117 did_si = FALSE;
5118 can_si = FALSE;
5119 can_si_back = FALSE;
5120#endif
5121 if (stop_arrow() == FAIL)
5122 return FAIL;
5123
5124 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005126 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005128 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 * "compl_startpos" to the cursor as a pattern to add a new word
5130 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005131 * "compl_startpos" is not in the same line as the cursor then fix it
5132 * (the line has been split because it was longer than 'tw'). if SOL
5133 * is set then skip the previous pattern, a word at the beginning of
5134 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005135 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5136 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 /*
5139 * it is a continued search
5140 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005141 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5143 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5144 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005145 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005147 /* line (probably) wrapped, set compl_startpos to the
5148 * first non_blank in the line, if it is not a wordchar
5149 * include it to get a better pattern, but then we don't
5150 * want the "\\<" prefix, check it bellow */
5151 compl_col = (colnr_T)(skipwhite(line) - line);
5152 compl_startpos.col = compl_col;
5153 compl_startpos.lnum = curwin->w_cursor.lnum;
5154 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156 else
5157 {
5158 /* S_IPOS was set when we inserted a word that was at the
5159 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005160 * mode but first we need to redefine compl_startpos */
5161 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005163 compl_cont_status |= CONT_SOL;
5164 compl_startpos.col = (colnr_T)(skipwhite(
5165 line + compl_length
5166 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005168 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005170 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005171 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005172 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005174 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005176 compl_cont_status &= ~CONT_SOL;
5177 compl_length = (IOSIZE - MIN_SPACE);
5178 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005180 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5181 if (compl_length < 1)
5182 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005184 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005185 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005187 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005192 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005194 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005196 compl_cont_status = 0;
5197 compl_cont_status |= CONT_N_ADDS;
5198 compl_startpos = curwin->w_cursor;
5199 startcol = (int)curs_col;
5200 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202
5203 /* Work out completion pattern and original text -- webb */
5204 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5205 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005206 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5208 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005209 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005211 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005213 compl_col += ++startcol;
5214 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
5216 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005217 compl_pattern = str_foldcase(line + compl_col,
5218 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005220 compl_pattern = vim_strnsave(line + compl_col,
5221 compl_length);
5222 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 return FAIL;
5224 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005225 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
5227 char_u *prefix = (char_u *)"\\<";
5228
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005229 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005230 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005231 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005232 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005234 if (!vim_iswordp(line + compl_col)
5235 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 && (
5237#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005238 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005240 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#endif
5242 )))
5243 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 STRCPY((char *)compl_pattern, prefix);
5245 (void)quote_meta(compl_pattern + STRLEN(prefix),
5246 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005248 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005250 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005252 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253#endif
5254 )
5255 {
5256 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5258 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005260 compl_col += curs_col;
5261 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 }
5263 else
5264 {
5265#ifdef FEAT_MBYTE
5266 /* Search the point of change class of multibyte character
5267 * or not a word single byte character backward. */
5268 if (has_mbyte)
5269 {
5270 int base_class;
5271 int head_off;
5272
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005273 startcol -= (*mb_head_off)(line, line + startcol);
5274 base_class = mb_get_class(line + startcol);
5275 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005277 head_off = (*mb_head_off)(line, line + startcol);
5278 if (base_class != mb_get_class(line + startcol
5279 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
5283 }
5284 else
5285#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005286 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 compl_col += ++startcol;
5289 compl_length = (int)curs_col - startcol;
5290 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
5292 /* Only match word with at least two chars -- webb
5293 * there's no need to call quote_meta,
5294 * alloc(7) is enough -- Acevedo
5295 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005296 compl_pattern = alloc(7);
5297 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005299 STRCPY((char *)compl_pattern, "\\<");
5300 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5301 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
5303 else
5304 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005305 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005306 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005307 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005309 STRCPY((char *)compl_pattern, "\\<");
5310 (void)quote_meta(compl_pattern + 2, line + compl_col,
5311 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
5313 }
5314 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005315 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005317 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005318 compl_length = (int)curs_col - (int)compl_col;
5319 if (compl_length < 0) /* cursor in indent: empty pattern */
5320 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005322 compl_pattern = str_foldcase(line + compl_col, compl_length,
5323 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005325 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5326 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 return FAIL;
5328 }
5329 else if (ctrl_x_mode == CTRL_X_FILES)
5330 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005331 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005332 if (startcol > 0)
5333 {
5334 char_u *p = line + startcol;
5335
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005336 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005337 while (p > line && vim_isfilec(PTR2CHAR(p)))
5338 mb_ptr_back(line, p);
5339 if (p == line && vim_isfilec(PTR2CHAR(p)))
5340 startcol = 0;
5341 else
5342 startcol = (int)(p - line) + 1;
5343 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005344
Bram Moolenaar0300e462013-09-08 16:03:45 +02005345 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005346 compl_length = (int)curs_col - startcol;
5347 compl_pattern = addstar(line + compl_col, compl_length,
5348 EXPAND_FILES);
5349 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 return FAIL;
5351 }
5352 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5353 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005354 compl_pattern = vim_strnsave(line, curs_col);
5355 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005357 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005358 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005359 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5360 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005361 /* No completion possible, use an empty pattern to get a
5362 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005363 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005364 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005365 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5366 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005368 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005369 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005370#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005371 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005372 * Call user defined function 'completefunc' with "a:findstart"
5373 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005374 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005375 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005376 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005377 char_u *funcname;
5378 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005379 win_T *curwin_save;
5380 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005381
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005382 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005383 * string */
5384 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5385 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5386 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005387 {
5388 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5389 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005390 /* restore did_ai, so that adding comment leader works */
5391 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005392 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005393 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005394
5395 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005396 args[1] = NULL;
5397 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005398 curwin_save = curwin;
5399 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005400 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005401 if (curwin_save != curwin || curbuf_save != curbuf)
5402 {
5403 EMSG(_(e_complwin));
5404 return FAIL;
5405 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005406 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005407 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005408 if (!equalpos(curwin->w_cursor, pos))
5409 {
5410 EMSG(_(e_compldel));
5411 return FAIL;
5412 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005413
Bram Moolenaar6110a002012-01-26 18:58:38 +01005414 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005415 * cancel the complete without an error.
5416 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005417 if (col == -2)
5418 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005419 if (col == -3)
5420 {
5421 ctrl_x_mode = 0;
5422 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005423 if (!shortmess(SHM_COMPLETIONMENU))
5424 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005425 return FAIL;
5426 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005427
Bram Moolenaar82139082011-09-14 16:52:09 +02005428 /*
5429 * Reset extended parameters of completion, when start new
5430 * completion.
5431 */
5432 compl_opt_refresh_always = FALSE;
5433
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005434 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005435 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005436 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005437 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005438 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005439
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005440 /* Setup variables for completion. Need to obtain "line" again,
5441 * it may have become invalid. */
5442 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005443 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005444 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5445 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005446#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005447 return FAIL;
5448 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005449 else if (ctrl_x_mode == CTRL_X_SPELL)
5450 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005451#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005452 if (spell_bad_len > 0)
5453 compl_col = curs_col - spell_bad_len;
5454 else
5455 compl_col = spell_word_start(startcol);
5456 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005457 {
5458 compl_length = 0;
5459 compl_col = curs_col;
5460 }
5461 else
5462 {
5463 spell_expand_check_cap(compl_col);
5464 compl_length = (int)curs_col - compl_col;
5465 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005466 /* Need to obtain "line" again, it may have become invalid. */
5467 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005468 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5469 if (compl_pattern == NULL)
5470#endif
5471 return FAIL;
5472 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005473 else
5474 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005475 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005476 return FAIL;
5477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005479 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 {
5481 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005482 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
5484 /* Insert a new line, keep indentation but ignore 'comments' */
5485#ifdef FEAT_COMMENTS
5486 char_u *old = curbuf->b_p_com;
5487
5488 curbuf->b_p_com = (char_u *)"";
5489#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005490 compl_startpos.lnum = curwin->w_cursor.lnum;
5491 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 ins_eol('\r');
5493#ifdef FEAT_COMMENTS
5494 curbuf->b_p_com = old;
5495#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005496 compl_length = 0;
5497 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
5499 }
5500 else
5501 {
5502 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005503 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 }
5505
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005506 if (compl_cont_status & CONT_LOCAL)
5507 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 else
5509 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5510
Bram Moolenaar62951b12011-09-21 18:23:05 +02005511 /* If any of the original typed text has been changed we need to fix
5512 * the redo buffer. */
5513 ins_compl_fixRedoBufForLeader(NULL);
5514
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005515 /* Always add completion for the original text. */
5516 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005517 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5518 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005519 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005521 vim_free(compl_pattern);
5522 compl_pattern = NULL;
5523 vim_free(compl_orig_text);
5524 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 return FAIL;
5526 }
5527
5528 /* showmode might reset the internal line pointers, so it must
5529 * be called before line = ml_get(), or when this address is no
5530 * longer needed. -- Acevedo.
5531 */
5532 edit_submode_extra = (char_u *)_("-- Searching...");
5533 edit_submode_highl = HLF_COUNT;
5534 showmode();
5535 edit_submode_extra = NULL;
5536 out_flush();
5537 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005538 else if (insert_match && stop_arrow() == FAIL)
5539 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005541 compl_shown_match = compl_curr_match;
5542 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
5544 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005545 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005547 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005548 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005549 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005551 /* may undisplay the popup menu */
5552 ins_compl_upd_pum();
5553
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005554 if (n > 1) /* all matches have been found */
5555 compl_matches = n;
5556 compl_curr_match = compl_shown_match;
5557 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
Bram Moolenaard68071d2006-05-02 22:08:30 +00005559 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5560 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 if (got_int && !global_busy)
5562 {
5563 (void)vgetc();
5564 got_int = FALSE;
5565 }
5566
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005567 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005568 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005570 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5571 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5573 edit_submode_highl = HLF_E;
5574 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5575 * because we couldn't expand anything at first place, but if we used
5576 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5577 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005578 if ( compl_length > 1
5579 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 || (ctrl_x_mode != 0
5581 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5582 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005583 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585
Bram Moolenaar572cb562005-08-05 21:35:02 +00005586 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005587 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005589 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
5591 if (edit_submode_extra == NULL)
5592 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005593 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
5595 edit_submode_extra = (char_u *)_("Back at original");
5596 edit_submode_highl = HLF_W;
5597 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005598 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 {
5600 edit_submode_extra = (char_u *)_("Word from other line");
5601 edit_submode_highl = HLF_COUNT;
5602 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005603 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
5605 edit_submode_extra = (char_u *)_("The only match");
5606 edit_submode_highl = HLF_COUNT;
5607 }
5608 else
5609 {
5610 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005611 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005613 int number = 0;
5614 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005616 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 {
5618 /* search backwards for the first valid (!= -1) number.
5619 * This should normally succeed already at the first loop
5620 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005621 for (match = compl_curr_match->cp_prev; match != NULL
5622 && match != compl_first_match;
5623 match = match->cp_prev)
5624 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005626 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 break;
5628 }
5629 if (match != NULL)
5630 /* go up and assign all numbers which are not assigned
5631 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005632 for (match = match->cp_next;
5633 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005634 match = match->cp_next)
5635 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
5637 else /* BACKWARD */
5638 {
5639 /* search forwards (upwards) for the first valid (!= -1)
5640 * number. This should normally succeed already at the
5641 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005642 for (match = compl_curr_match->cp_next; match != NULL
5643 && match != compl_first_match;
5644 match = match->cp_next)
5645 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005647 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 break;
5649 }
5650 if (match != NULL)
5651 /* go down and assign all numbers which are not
5652 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005653 for (match = match->cp_prev; match
5654 && match->cp_number == -1;
5655 match = match->cp_prev)
5656 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
5658 }
5659
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005660 /* The match should always have a sequence number now, this is
5661 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005662 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005664 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5665 * Translations may need more than twice that. */
5666 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005668 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005669 vim_snprintf((char *)match_ref, sizeof(match_ref),
5670 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005671 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005673 vim_snprintf((char *)match_ref, sizeof(match_ref),
5674 _("match %d"),
5675 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 edit_submode_extra = match_ref;
5677 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005678 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 curs_columns(FALSE);
5680 }
5681 }
5682 }
5683
5684 /* Show a message about what (completion) mode we're in. */
5685 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005686 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005688 if (edit_submode_extra != NULL)
5689 {
5690 if (!p_smd)
5691 msg_attr(edit_submode_extra,
5692 edit_submode_highl < HLF_COUNT
5693 ? hl_attr(edit_submode_highl) : 0);
5694 }
5695 else
5696 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
Bram Moolenaard68071d2006-05-02 22:08:30 +00005699 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005700 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005701 show_pum(save_w_wrow, save_w_leftcol);
5702
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005703 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005704 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005705
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 return OK;
5707}
5708
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005709 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005710show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005711{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005712 /* RedrawingDisabled may be set when invoked through complete(). */
5713 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005714
Bram Moolenaarcb036422017-03-01 12:29:10 +01005715 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005716
Bram Moolenaarcb036422017-03-01 12:29:10 +01005717 /* If the cursor moved or the display scrolled we need to remove the pum
5718 * first. */
5719 setcursor();
5720 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5721 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005722
Bram Moolenaarcb036422017-03-01 12:29:10 +01005723 ins_compl_show_pum();
5724 setcursor();
5725 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005726}
5727
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728/*
5729 * Looks in the first "len" chars. of "src" for search-metachars.
5730 * If dest is not NULL the chars. are copied there quoting (with
5731 * a backslash) the metachars, and dest would be NUL terminated.
5732 * Returns the length (needed) of dest
5733 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005734 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005735quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005737 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005739 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 {
5741 switch (*src)
5742 {
5743 case '.':
5744 case '*':
5745 case '[':
5746 if (ctrl_x_mode == CTRL_X_DICTIONARY
5747 || ctrl_x_mode == CTRL_X_THESAURUS)
5748 break;
5749 case '~':
5750 if (!p_magic) /* quote these only if magic is set */
5751 break;
5752 case '\\':
5753 if (ctrl_x_mode == CTRL_X_DICTIONARY
5754 || ctrl_x_mode == CTRL_X_THESAURUS)
5755 break;
5756 case '^': /* currently it's not needed. */
5757 case '$':
5758 m++;
5759 if (dest != NULL)
5760 *dest++ = '\\';
5761 break;
5762 }
5763 if (dest != NULL)
5764 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005765# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 /* Copy remaining bytes of a multibyte character. */
5767 if (has_mbyte)
5768 {
5769 int i, mb_len;
5770
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005771 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 if (mb_len > 0 && len >= mb_len)
5773 for (i = 0; i < mb_len; ++i)
5774 {
5775 --len;
5776 ++src;
5777 if (dest != NULL)
5778 *dest++ = *src;
5779 }
5780 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783 if (dest != NULL)
5784 *dest = NUL;
5785
5786 return m;
5787}
5788#endif /* FEAT_INS_EXPAND */
5789
5790/*
5791 * Next character is interpreted literally.
5792 * A one, two or three digit decimal number is interpreted as its byte value.
5793 * If one or two digits are entered, the next character is given to vungetc().
5794 * For Unicode a character > 255 may be returned.
5795 */
5796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005797get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
5799 int cc;
5800 int nc;
5801 int i;
5802 int hex = FALSE;
5803 int octal = FALSE;
5804#ifdef FEAT_MBYTE
5805 int unicode = 0;
5806#endif
5807
5808 if (got_int)
5809 return Ctrl_C;
5810
5811#ifdef FEAT_GUI
5812 /*
5813 * In GUI there is no point inserting the internal code for a special key.
5814 * It is more useful to insert the string "<KEY>" instead. This would
5815 * probably be useful in a text window too, but it would not be
5816 * vi-compatible (maybe there should be an option for it?) -- webb
5817 */
5818 if (gui.in_use)
5819 ++allow_keys;
5820#endif
5821#ifdef USE_ON_FLY_SCROLL
5822 dont_scroll = TRUE; /* disallow scrolling here */
5823#endif
5824 ++no_mapping; /* don't map the next key hits */
5825 cc = 0;
5826 i = 0;
5827 for (;;)
5828 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005829 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830#ifdef FEAT_CMDL_INFO
5831 if (!(State & CMDLINE)
5832# ifdef FEAT_MBYTE
5833 && MB_BYTE2LEN_CHECK(nc) == 1
5834# endif
5835 )
5836 add_to_showcmd(nc);
5837#endif
5838 if (nc == 'x' || nc == 'X')
5839 hex = TRUE;
5840 else if (nc == 'o' || nc == 'O')
5841 octal = TRUE;
5842#ifdef FEAT_MBYTE
5843 else if (nc == 'u' || nc == 'U')
5844 unicode = nc;
5845#endif
5846 else
5847 {
5848 if (hex
5849#ifdef FEAT_MBYTE
5850 || unicode != 0
5851#endif
5852 )
5853 {
5854 if (!vim_isxdigit(nc))
5855 break;
5856 cc = cc * 16 + hex2nr(nc);
5857 }
5858 else if (octal)
5859 {
5860 if (nc < '0' || nc > '7')
5861 break;
5862 cc = cc * 8 + nc - '0';
5863 }
5864 else
5865 {
5866 if (!VIM_ISDIGIT(nc))
5867 break;
5868 cc = cc * 10 + nc - '0';
5869 }
5870
5871 ++i;
5872 }
5873
5874 if (cc > 255
5875#ifdef FEAT_MBYTE
5876 && unicode == 0
5877#endif
5878 )
5879 cc = 255; /* limit range to 0-255 */
5880 nc = 0;
5881
5882 if (hex) /* hex: up to two chars */
5883 {
5884 if (i >= 2)
5885 break;
5886 }
5887#ifdef FEAT_MBYTE
5888 else if (unicode) /* Unicode: up to four or eight chars */
5889 {
5890 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5891 break;
5892 }
5893#endif
5894 else if (i >= 3) /* decimal or octal: up to three chars */
5895 break;
5896 }
5897 if (i == 0) /* no number entered */
5898 {
5899 if (nc == K_ZERO) /* NUL is stored as NL */
5900 {
5901 cc = '\n';
5902 nc = 0;
5903 }
5904 else
5905 {
5906 cc = nc;
5907 nc = 0;
5908 }
5909 }
5910
5911 if (cc == 0) /* NUL is stored as NL */
5912 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005913#ifdef FEAT_MBYTE
5914 if (enc_dbcs && (cc & 0xff) == 0)
5915 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5916 second byte will cause trouble! */
5917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918
5919 --no_mapping;
5920#ifdef FEAT_GUI
5921 if (gui.in_use)
5922 --allow_keys;
5923#endif
5924 if (nc)
5925 vungetc(nc);
5926 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5927 return cc;
5928}
5929
5930/*
5931 * Insert character, taking care of special keys and mod_mask
5932 */
5933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005934insert_special(
5935 int c,
5936 int allow_modmask,
5937 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
5939 char_u *p;
5940 int len;
5941
5942 /*
5943 * Special function key, translate into "<Key>". Up to the last '>' is
5944 * inserted with ins_str(), so as not to replace characters in replace
5945 * mode.
5946 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5947 * unless 'allow_modmask' is TRUE.
5948 */
5949#ifdef MACOS
5950 /* Command-key never produces a normal key */
5951 if (mod_mask & MOD_MASK_CMD)
5952 allow_modmask = TRUE;
5953#endif
5954 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5955 {
5956 p = get_special_key_name(c, mod_mask);
5957 len = (int)STRLEN(p);
5958 c = p[len - 1];
5959 if (len > 2)
5960 {
5961 if (stop_arrow() == FAIL)
5962 return;
5963 p[len - 1] = NUL;
5964 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005965 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 ctrlv = FALSE;
5967 }
5968 }
5969 if (stop_arrow() == OK)
5970 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5971}
5972
5973/*
5974 * Special characters in this context are those that need processing other
5975 * than the simple insertion that can be performed here. This includes ESC
5976 * which terminates the insert, and CR/NL which need special processing to
5977 * open up a new line. This routine tries to optimize insertions performed by
5978 * the "redo", "undo" or "put" commands, so it needs to know when it should
5979 * stop and defer processing to the "normal" mechanism.
5980 * '0' and '^' are special, because they can be followed by CTRL-D.
5981 */
5982#ifdef EBCDIC
5983# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5984#else
5985# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5986#endif
5987
5988#ifdef FEAT_MBYTE
5989# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5990#else
5991# define WHITECHAR(cc) vim_iswhite(cc)
5992#endif
5993
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005994/*
5995 * "flags": INSCHAR_FORMAT - force formatting
5996 * INSCHAR_CTRLV - char typed just after CTRL-V
5997 * INSCHAR_NO_FEX - don't use 'formatexpr'
5998 *
5999 * NOTE: passes the flags value straight through to internal_format() which,
6000 * beside INSCHAR_FORMAT (above), is also looking for these:
6001 * INSCHAR_DO_COM - format comments
6002 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006005insertchar(
6006 int c, /* character to insert or NUL */
6007 int flags, /* INSCHAR_FORMAT, etc. */
6008 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 int textwidth;
6011#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006015 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006017 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019
6020 /*
6021 * Try to break the line in two or more pieces when:
6022 * - Always do this if we have been called to do formatting only.
6023 * - Always do this when 'formatoptions' has the 'a' flag and the line
6024 * ends in white space.
6025 * - Otherwise:
6026 * - Don't do this if inserting a blank
6027 * - Don't do this if an existing character is being replaced, unless
6028 * we're in VREPLACE mode.
6029 * - Do this if the cursor is not on the line where insert started
6030 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6031 * before the insert.
6032 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6033 * before 'textwidth'
6034 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006035 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006036 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 || (!vim_iswhite(c)
6038 && !((State & REPLACE_FLAG)
6039#ifdef FEAT_VREPLACE
6040 && !(State & VREPLACE_FLAG)
6041#endif
6042 && *ml_get_cursor() != NUL)
6043 && (curwin->w_cursor.lnum != Insstart.lnum
6044 || ((!has_format_option(FO_INS_LONG)
6045 || Insstart_textlen <= (colnr_T)textwidth)
6046 && (!fo_ins_blank
6047 || Insstart_blank_vcol <= (colnr_T)textwidth
6048 ))))))
6049 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006050 /* Format with 'formatexpr' when it's set. Use internal formatting
6051 * when 'formatexpr' isn't set or it returns non-zero. */
6052#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006053 int do_internal = TRUE;
6054 colnr_T virtcol = get_nolist_virtcol()
6055 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006056
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006057 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6058 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006059 {
6060 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6061 /* It may be required to save for undo again, e.g. when setline()
6062 * was called. */
6063 ins_need_undo = TRUE;
6064 }
6065 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006067 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006069
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 if (c == NUL) /* only formatting was wanted */
6071 return;
6072
6073#ifdef FEAT_COMMENTS
6074 /* Check whether this character should end a comment. */
6075 if (did_ai && (int)c == end_comment_pending)
6076 {
6077 char_u *line;
6078 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6079 int middle_len, end_len;
6080 int i;
6081
6082 /*
6083 * Need to remove existing (middle) comment leader and insert end
6084 * comment leader. First, check what comment leader we can find.
6085 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006086 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6088 {
6089 /* Skip middle-comment string */
6090 while (*p && p[-1] != ':') /* find end of middle flags */
6091 ++p;
6092 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6093 /* Don't count trailing white space for middle_len */
6094 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6095 --middle_len;
6096
6097 /* Find the end-comment string */
6098 while (*p && p[-1] != ':') /* find end of end flags */
6099 ++p;
6100 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6101
6102 /* Skip white space before the cursor */
6103 i = curwin->w_cursor.col;
6104 while (--i >= 0 && vim_iswhite(line[i]))
6105 ;
6106 i++;
6107
6108 /* Skip to before the middle leader */
6109 i -= middle_len;
6110
6111 /* Check some expected things before we go on */
6112 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6113 {
6114 /* Backspace over all the stuff we want to replace */
6115 backspace_until_column(i);
6116
6117 /*
6118 * Insert the end-comment string, except for the last
6119 * character, which will get inserted as normal later.
6120 */
6121 ins_bytes_len(lead_end, end_len - 1);
6122 }
6123 }
6124 }
6125 end_comment_pending = NUL;
6126#endif
6127
6128 did_ai = FALSE;
6129#ifdef FEAT_SMARTINDENT
6130 did_si = FALSE;
6131 can_si = FALSE;
6132 can_si_back = FALSE;
6133#endif
6134
6135 /*
6136 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6137 * This speeds up normal text input considerably.
6138 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6139 * need to re-indent at a ':', or any other character (but not what
6140 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006141 * Don't do this when there an InsertCharPre autocommand is defined,
6142 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 */
6144#ifdef USE_ON_FLY_SCROLL
6145 dont_scroll = FALSE; /* allow scrolling here */
6146#endif
6147
6148 if ( !ISSPECIAL(c)
6149#ifdef FEAT_MBYTE
6150 && (!has_mbyte || (*mb_char2len)(c) == 1)
6151#endif
6152 && vpeekc() != NUL
6153 && !(State & REPLACE_FLAG)
6154#ifdef FEAT_CINDENT
6155 && !cindent_on()
6156#endif
6157#ifdef FEAT_RIGHTLEFT
6158 && !p_ri
6159#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006160#ifdef FEAT_AUTOCMD
6161 && !has_insertcharpre()
6162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 )
6164 {
6165#define INPUT_BUFLEN 100
6166 char_u buf[INPUT_BUFLEN + 1];
6167 int i;
6168 colnr_T virtcol = 0;
6169
6170 buf[0] = c;
6171 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006172 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 virtcol = get_nolist_virtcol();
6174 /*
6175 * Stop the string when:
6176 * - no more chars available
6177 * - finding a special character (command key)
6178 * - buffer is full
6179 * - running into the 'textwidth' boundary
6180 * - need to check for abbreviation: A non-word char after a word-char
6181 */
6182 while ( (c = vpeekc()) != NUL
6183 && !ISSPECIAL(c)
6184#ifdef FEAT_MBYTE
6185 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6186#endif
6187 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006188# ifdef FEAT_FKMAP
6189 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 && (textwidth == 0
6192 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6193 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6194 {
6195#ifdef FEAT_RIGHTLEFT
6196 c = vgetc();
6197 if (p_hkmap && KeyTyped)
6198 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 buf[i++] = c;
6200#else
6201 buf[i++] = vgetc();
6202#endif
6203 }
6204
6205#ifdef FEAT_DIGRAPHS
6206 do_digraph(-1); /* clear digraphs */
6207 do_digraph(buf[i-1]); /* may be the start of a digraph */
6208#endif
6209 buf[i] = NUL;
6210 ins_str(buf);
6211 if (flags & INSCHAR_CTRLV)
6212 {
6213 redo_literal(*buf);
6214 i = 1;
6215 }
6216 else
6217 i = 0;
6218 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006219 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 }
6221 else
6222 {
6223#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006224 int cc;
6225
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6227 {
6228 char_u buf[MB_MAXBYTES + 1];
6229
6230 (*mb_char2bytes)(c, buf);
6231 buf[cc] = NUL;
6232 ins_char_bytes(buf, cc);
6233 AppendCharToRedobuff(c);
6234 }
6235 else
6236#endif
6237 {
6238 ins_char(c);
6239 if (flags & INSCHAR_CTRLV)
6240 redo_literal(c);
6241 else
6242 AppendCharToRedobuff(c);
6243 }
6244 }
6245}
6246
6247/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006248 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006249 *
6250 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6251 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006252 */
6253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006254internal_format(
6255 int textwidth,
6256 int second_indent,
6257 int flags,
6258 int format_only,
6259 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006260{
6261 int cc;
6262 int save_char = NUL;
6263 int haveto_redraw = FALSE;
6264 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6265#ifdef FEAT_MBYTE
6266 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6267#endif
6268 int fo_white_par = has_format_option(FO_WHITE_PAR);
6269 int first_line = TRUE;
6270#ifdef FEAT_COMMENTS
6271 colnr_T leader_len;
6272 int no_leader = FALSE;
6273 int do_comments = (flags & INSCHAR_DO_COM);
6274#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006275#ifdef FEAT_LINEBREAK
6276 int has_lbr = curwin->w_p_lbr;
6277
6278 /* make sure win_lbr_chartabsize() counts correctly */
6279 curwin->w_p_lbr = FALSE;
6280#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006281
6282 /*
6283 * When 'ai' is off we don't want a space under the cursor to be
6284 * deleted. Replace it with an 'x' temporarily.
6285 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006286 if (!curbuf->b_p_ai
6287#ifdef FEAT_VREPLACE
6288 && !(State & VREPLACE_FLAG)
6289#endif
6290 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006291 {
6292 cc = gchar_cursor();
6293 if (vim_iswhite(cc))
6294 {
6295 save_char = cc;
6296 pchar_cursor('x');
6297 }
6298 }
6299
6300 /*
6301 * Repeat breaking lines, until the current line is not too long.
6302 */
6303 while (!got_int)
6304 {
6305 int startcol; /* Cursor column at entry */
6306 int wantcol; /* column at textwidth border */
6307 int foundcol; /* column for start of spaces */
6308 int end_foundcol = 0; /* column for start of word */
6309 colnr_T len;
6310 colnr_T virtcol;
6311#ifdef FEAT_VREPLACE
6312 int orig_col = 0;
6313 char_u *saved_text = NULL;
6314#endif
6315 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006316 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006317
Bram Moolenaar97b98102009-11-17 16:41:01 +00006318 virtcol = get_nolist_virtcol()
6319 + char2cells(c != NUL ? c : gchar_cursor());
6320 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006321 break;
6322
6323#ifdef FEAT_COMMENTS
6324 if (no_leader)
6325 do_comments = FALSE;
6326 else if (!(flags & INSCHAR_FORMAT)
6327 && has_format_option(FO_WRAP_COMS))
6328 do_comments = TRUE;
6329
6330 /* Don't break until after the comment leader */
6331 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006332 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006333 else
6334 leader_len = 0;
6335
6336 /* If the line doesn't start with a comment leader, then don't
6337 * start one in a following broken line. Avoids that a %word
6338 * moved to the start of the next line causes all following lines
6339 * to start with %. */
6340 if (leader_len == 0)
6341 no_leader = TRUE;
6342#endif
6343 if (!(flags & INSCHAR_FORMAT)
6344#ifdef FEAT_COMMENTS
6345 && leader_len == 0
6346#endif
6347 && !has_format_option(FO_WRAP))
6348
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006349 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006350 if ((startcol = curwin->w_cursor.col) == 0)
6351 break;
6352
6353 /* find column of textwidth border */
6354 coladvance((colnr_T)textwidth);
6355 wantcol = curwin->w_cursor.col;
6356
Bram Moolenaar97b98102009-11-17 16:41:01 +00006357 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006358 foundcol = 0;
6359
6360 /*
6361 * Find position to break at.
6362 * Stop at first entered white when 'formatoptions' has 'v'
6363 */
6364 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006365 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006366 || curwin->w_cursor.lnum != Insstart.lnum
6367 || curwin->w_cursor.col >= Insstart.col)
6368 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006369 if (curwin->w_cursor.col == startcol && c != NUL)
6370 cc = c;
6371 else
6372 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006373 if (WHITECHAR(cc))
6374 {
6375 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006376 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006377
6378 /* find start of sequence of blanks */
6379 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6380 {
6381 dec_cursor();
6382 cc = gchar_cursor();
6383 }
6384 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6385 break; /* only spaces in front of text */
6386#ifdef FEAT_COMMENTS
6387 /* Don't break until after the comment leader */
6388 if (curwin->w_cursor.col < leader_len)
6389 break;
6390#endif
6391 if (has_format_option(FO_ONE_LETTER))
6392 {
6393 /* do not break after one-letter words */
6394 if (curwin->w_cursor.col == 0)
6395 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006396#ifdef FEAT_COMMENTS
6397 /* do not break "#a b" when 'tw' is 2 */
6398 if (curwin->w_cursor.col <= leader_len)
6399 break;
6400#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006401 col = curwin->w_cursor.col;
6402 dec_cursor();
6403 cc = gchar_cursor();
6404
6405 if (WHITECHAR(cc))
6406 continue; /* one-letter, continue */
6407 curwin->w_cursor.col = col;
6408 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006409
6410 inc_cursor();
6411
6412 end_foundcol = end_col + 1;
6413 foundcol = curwin->w_cursor.col;
6414 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006415 break;
6416 }
6417#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006418 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006419 {
6420 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006421 if (curwin->w_cursor.col != startcol)
6422 {
6423#ifdef FEAT_COMMENTS
6424 /* Don't break until after the comment leader */
6425 if (curwin->w_cursor.col < leader_len)
6426 break;
6427#endif
6428 col = curwin->w_cursor.col;
6429 inc_cursor();
6430 /* Don't change end_foundcol if already set. */
6431 if (foundcol != curwin->w_cursor.col)
6432 {
6433 foundcol = curwin->w_cursor.col;
6434 end_foundcol = foundcol;
6435 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6436 break;
6437 }
6438 curwin->w_cursor.col = col;
6439 }
6440
6441 if (curwin->w_cursor.col == 0)
6442 break;
6443
6444 col = curwin->w_cursor.col;
6445
6446 dec_cursor();
6447 cc = gchar_cursor();
6448
6449 if (WHITECHAR(cc))
6450 continue; /* break with space */
6451#ifdef FEAT_COMMENTS
6452 /* Don't break until after the comment leader */
6453 if (curwin->w_cursor.col < leader_len)
6454 break;
6455#endif
6456
6457 curwin->w_cursor.col = col;
6458
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006459 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006460 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006461 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6462 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006463 }
6464#endif
6465 if (curwin->w_cursor.col == 0)
6466 break;
6467 dec_cursor();
6468 }
6469
6470 if (foundcol == 0) /* no spaces, cannot break line */
6471 {
6472 curwin->w_cursor.col = startcol;
6473 break;
6474 }
6475
6476 /* Going to break the line, remove any "$" now. */
6477 undisplay_dollar();
6478
6479 /*
6480 * Offset between cursor position and line break is used by replace
6481 * stack functions. VREPLACE does not use this, and backspaces
6482 * over the text instead.
6483 */
6484#ifdef FEAT_VREPLACE
6485 if (State & VREPLACE_FLAG)
6486 orig_col = startcol; /* Will start backspacing from here */
6487 else
6488#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006489 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006490
6491 /*
6492 * adjust startcol for spaces that will be deleted and
6493 * characters that will remain on top line
6494 */
6495 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006496 while ((cc = gchar_cursor(), WHITECHAR(cc))
6497 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006498 inc_cursor();
6499 startcol -= curwin->w_cursor.col;
6500 if (startcol < 0)
6501 startcol = 0;
6502
6503#ifdef FEAT_VREPLACE
6504 if (State & VREPLACE_FLAG)
6505 {
6506 /*
6507 * In VREPLACE mode, we will backspace over the text to be
6508 * wrapped, so save a copy now to put on the next line.
6509 */
6510 saved_text = vim_strsave(ml_get_cursor());
6511 curwin->w_cursor.col = orig_col;
6512 if (saved_text == NULL)
6513 break; /* Can't do it, out of memory */
6514 saved_text[startcol] = NUL;
6515
6516 /* Backspace over characters that will move to the next line */
6517 if (!fo_white_par)
6518 backspace_until_column(foundcol);
6519 }
6520 else
6521#endif
6522 {
6523 /* put cursor after pos. to break line */
6524 if (!fo_white_par)
6525 curwin->w_cursor.col = foundcol;
6526 }
6527
6528 /*
6529 * Split the line just before the margin.
6530 * Only insert/delete lines, but don't really redraw the window.
6531 */
6532 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6533 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6534#ifdef FEAT_COMMENTS
6535 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006536 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006537#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006538 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6539 if (!(flags & INSCHAR_COM_LIST))
6540 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006541
6542 replace_offset = 0;
6543 if (first_line)
6544 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006545 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006546 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006547 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006548 * This section is for auto-wrap of numeric lists. When not
6549 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6550 * flag will be set and open_line() will handle it (as seen
6551 * above). The code here (and in get_number_indent()) will
6552 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006553 */
6554 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006555 second_indent =
6556 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006557 if (second_indent >= 0)
6558 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006559#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006560 if (State & VREPLACE_FLAG)
6561 change_indent(INDENT_SET, second_indent,
6562 FALSE, NUL, TRUE);
6563 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006564#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006565#ifdef FEAT_COMMENTS
6566 if (leader_len > 0 && second_indent - leader_len > 0)
6567 {
6568 int i;
6569 int padding = second_indent - leader_len;
6570
6571 /* We started at the first_line of a numbered list
6572 * that has a comment. the open_line() function has
6573 * inserted the proper comment leader and positioned
6574 * the cursor at the end of the split line. Now we
6575 * add the additional whitespace needed after the
6576 * comment leader for the numbered list. */
6577 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006578 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006579 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006580 }
6581 else
6582 {
6583#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006584 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006585#ifdef FEAT_COMMENTS
6586 }
6587#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006588 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006589 }
6590 first_line = FALSE;
6591 }
6592
6593#ifdef FEAT_VREPLACE
6594 if (State & VREPLACE_FLAG)
6595 {
6596 /*
6597 * In VREPLACE mode we have backspaced over the text to be
6598 * moved, now we re-insert it into the new line.
6599 */
6600 ins_bytes(saved_text);
6601 vim_free(saved_text);
6602 }
6603 else
6604#endif
6605 {
6606 /*
6607 * Check if cursor is not past the NUL off the line, cindent
6608 * may have added or removed indent.
6609 */
6610 curwin->w_cursor.col += startcol;
6611 len = (colnr_T)STRLEN(ml_get_curline());
6612 if (curwin->w_cursor.col > len)
6613 curwin->w_cursor.col = len;
6614 }
6615
6616 haveto_redraw = TRUE;
6617#ifdef FEAT_CINDENT
6618 can_cindent = TRUE;
6619#endif
6620 /* moved the cursor, don't autoindent or cindent now */
6621 did_ai = FALSE;
6622#ifdef FEAT_SMARTINDENT
6623 did_si = FALSE;
6624 can_si = FALSE;
6625 can_si_back = FALSE;
6626#endif
6627 line_breakcheck();
6628 }
6629
6630 if (save_char != NUL) /* put back space after cursor */
6631 pchar_cursor(save_char);
6632
Bram Moolenaar0026d472014-09-09 16:32:39 +02006633#ifdef FEAT_LINEBREAK
6634 curwin->w_p_lbr = has_lbr;
6635#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006636 if (!format_only && haveto_redraw)
6637 {
6638 update_topline();
6639 redraw_curbuf_later(VALID);
6640 }
6641}
6642
6643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 * Called after inserting or deleting text: When 'formatoptions' includes the
6645 * 'a' flag format from the current line until the end of the paragraph.
6646 * Keep the cursor at the same position relative to the text.
6647 * The caller must have saved the cursor line for undo, following ones will be
6648 * saved here.
6649 */
6650 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006651auto_format(
6652 int trailblank, /* when TRUE also format with trailing blank */
6653 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
6655 pos_T pos;
6656 colnr_T len;
6657 char_u *old;
6658 char_u *new, *pnew;
6659 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006660 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
6662 if (!has_format_option(FO_AUTO))
6663 return;
6664
6665 pos = curwin->w_cursor;
6666 old = ml_get_curline();
6667
6668 /* may remove added space */
6669 check_auto_format(FALSE);
6670
6671 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6672 * user might insert normal text next. Also skip formatting when "1" is
6673 * in 'formatoptions' and there is a single character before the cursor.
6674 * Otherwise the line would be broken and when typing another non-white
6675 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006676 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 if (*old != NUL && !trailblank && wasatend)
6678 {
6679 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006680 cc = gchar_cursor();
6681 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6682 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006684 cc = gchar_cursor();
6685 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
6687 curwin->w_cursor = pos;
6688 return;
6689 }
6690 curwin->w_cursor = pos;
6691 }
6692
6693#ifdef FEAT_COMMENTS
6694 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6695 * comments. */
6696 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006697 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 return;
6699#endif
6700
6701 /*
6702 * May start formatting in a previous line, so that after "x" a word is
6703 * moved to the previous line if it fits there now. Only when this is not
6704 * the start of a paragraph.
6705 */
6706 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6707 {
6708 --curwin->w_cursor.lnum;
6709 if (u_save_cursor() == FAIL)
6710 return;
6711 }
6712
6713 /*
6714 * Do the formatting and restore the cursor position. "saved_cursor" will
6715 * be adjusted for the text formatting.
6716 */
6717 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006718 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 curwin->w_cursor = saved_cursor;
6720 saved_cursor.lnum = 0;
6721
6722 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6723 {
6724 /* "cannot happen" */
6725 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6726 coladvance((colnr_T)MAXCOL);
6727 }
6728 else
6729 check_cursor_col();
6730
6731 /* Insert mode: If the cursor is now after the end of the line while it
6732 * previously wasn't, the line was broken. Because of the rule above we
6733 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6734 * formatted. */
6735 if (!wasatend && has_format_option(FO_WHITE_PAR))
6736 {
6737 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006738 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 if (curwin->w_cursor.col == len)
6740 {
6741 pnew = vim_strnsave(new, len + 2);
6742 pnew[len] = ' ';
6743 pnew[len + 1] = NUL;
6744 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6745 /* remove the space later */
6746 did_add_space = TRUE;
6747 }
6748 else
6749 /* may remove added space */
6750 check_auto_format(FALSE);
6751 }
6752
6753 check_cursor();
6754}
6755
6756/*
6757 * When an extra space was added to continue a paragraph for auto-formatting,
6758 * delete it now. The space must be under the cursor, just after the insert
6759 * position.
6760 */
6761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006762check_auto_format(
6763 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764{
6765 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006766 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767
6768 if (did_add_space)
6769 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006770 cc = gchar_cursor();
6771 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 /* Somehow the space was removed already. */
6773 did_add_space = FALSE;
6774 else
6775 {
6776 if (!end_insert)
6777 {
6778 inc_cursor();
6779 c = gchar_cursor();
6780 dec_cursor();
6781 }
6782 if (c != NUL)
6783 {
6784 /* The space is no longer at the end of the line, delete it. */
6785 del_char(FALSE);
6786 did_add_space = FALSE;
6787 }
6788 }
6789 }
6790}
6791
6792/*
6793 * Find out textwidth to be used for formatting:
6794 * if 'textwidth' option is set, use it
6795 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6796 * if invalid value, use 0.
6797 * Set default to window width (maximum 79) for "gq" operator.
6798 */
6799 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006800comp_textwidth(
6801 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802{
6803 int textwidth;
6804
6805 textwidth = curbuf->b_p_tw;
6806 if (textwidth == 0 && curbuf->b_p_wm)
6807 {
6808 /* The width is the window width minus 'wrapmargin' minus all the
6809 * things that add to the margin. */
6810 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6811#ifdef FEAT_CMDWIN
6812 if (cmdwin_type != 0)
6813 textwidth -= 1;
6814#endif
6815#ifdef FEAT_FOLDING
6816 textwidth -= curwin->w_p_fdc;
6817#endif
6818#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006819 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 textwidth -= 1;
6821#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006822 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 textwidth -= 8;
6824 }
6825 if (textwidth < 0)
6826 textwidth = 0;
6827 if (ff && textwidth == 0)
6828 {
6829 textwidth = W_WIDTH(curwin) - 1;
6830 if (textwidth > 79)
6831 textwidth = 79;
6832 }
6833 return textwidth;
6834}
6835
6836/*
6837 * Put a character in the redo buffer, for when just after a CTRL-V.
6838 */
6839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006840redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841{
6842 char_u buf[10];
6843
6844 /* Only digits need special treatment. Translate them into a string of
6845 * three digits. */
6846 if (VIM_ISDIGIT(c))
6847 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006848 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 AppendToRedobuff(buf);
6850 }
6851 else
6852 AppendCharToRedobuff(c);
6853}
6854
6855/*
6856 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006857 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 */
6859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006860start_arrow(
6861 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006863 start_arrow_common(end_insert_pos, TRUE);
6864}
6865
6866/*
6867 * Like start_arrow() but with end_change argument.
6868 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6869 */
6870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006871start_arrow_with_change(
6872 pos_T *end_insert_pos, /* can be NULL */
6873 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006874{
6875 start_arrow_common(end_insert_pos, end_change);
6876 if (!end_change)
6877 {
6878 AppendCharToRedobuff(Ctrl_G);
6879 AppendCharToRedobuff('U');
6880 }
6881}
6882
6883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006884start_arrow_common(
6885 pos_T *end_insert_pos, /* can be NULL */
6886 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006887{
6888 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
6890 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006891 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 arrow_used = TRUE; /* this means we stopped the current insert */
6893 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006894#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006895 check_spell_redraw();
6896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897}
6898
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006899#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006900/*
6901 * If we skipped highlighting word at cursor, do it now.
6902 * It may be skipped again, thus reset spell_redraw_lnum first.
6903 */
6904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006905check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006906{
6907 if (spell_redraw_lnum != 0)
6908 {
6909 linenr_T lnum = spell_redraw_lnum;
6910
6911 spell_redraw_lnum = 0;
6912 redrawWinline(lnum, FALSE);
6913 }
6914}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006915
6916/*
6917 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6918 * spelled word, if there is one.
6919 */
6920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006921spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006922{
6923 pos_T tpos = curwin->w_cursor;
6924
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006925 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006926 if (curwin->w_cursor.col != tpos.col)
6927 start_arrow(&tpos);
6928}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006929#endif
6930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931/*
6932 * stop_arrow() is called before a change is made in insert mode.
6933 * If an arrow key has been used, start a new insertion.
6934 * Returns FAIL if undo is impossible, shouldn't insert then.
6935 */
6936 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006937stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938{
6939 if (arrow_used)
6940 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006941 Insstart = curwin->w_cursor; /* new insertion starts here */
6942 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6943 /* Don't update the original insert position when moved to the
6944 * right, except when nothing was inserted yet. */
6945 update_Insstart_orig = FALSE;
6946 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6947
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 if (u_save_cursor() == OK)
6949 {
6950 arrow_used = FALSE;
6951 ins_need_undo = FALSE;
6952 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006953
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 ai_col = 0;
6955#ifdef FEAT_VREPLACE
6956 if (State & VREPLACE_FLAG)
6957 {
6958 orig_line_count = curbuf->b_ml.ml_line_count;
6959 vr_lines_changed = 1;
6960 }
6961#endif
6962 ResetRedobuff();
6963 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006964 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 }
6966 else if (ins_need_undo)
6967 {
6968 if (u_save_cursor() == OK)
6969 ins_need_undo = FALSE;
6970 }
6971
6972#ifdef FEAT_FOLDING
6973 /* Always open fold at the cursor line when inserting something. */
6974 foldOpenCursor();
6975#endif
6976
6977 return (arrow_used || ins_need_undo ? FAIL : OK);
6978}
6979
6980/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006981 * Do a few things to stop inserting.
6982 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6983 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 */
6985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986stop_insert(
6987 pos_T *end_insert_pos,
6988 int esc, /* called by ins_esc() */
6989 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006991 int cc;
6992 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
6994 stop_redo_ins();
6995 replace_flush(); /* abandon replace stack */
6996
6997 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006998 * Save the inserted text for later redo with ^@ and CTRL-A.
6999 * Don't do it when "restart_edit" was set and nothing was inserted,
7000 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007002 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007003 if (did_restart_edit == 0 || (ptr != NULL
7004 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007005 {
7006 vim_free(last_insert);
7007 last_insert = ptr;
7008 last_insert_skip = new_insert_skip;
7009 }
7010 else
7011 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007013 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 {
7015 /* Auto-format now. It may seem strange to do this when stopping an
7016 * insertion (or moving the cursor), but it's required when appending
7017 * a line and having it end in a space. But only do it when something
7018 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007019 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007021 pos_T tpos = curwin->w_cursor;
7022
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 /* When the cursor is at the end of the line after a space the
7024 * formatting will move it to the following word. Avoid that by
7025 * moving the cursor onto the space. */
7026 cc = 'x';
7027 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7028 {
7029 dec_cursor();
7030 cc = gchar_cursor();
7031 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007032 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 }
7034
7035 auto_format(TRUE, FALSE);
7036
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007037 if (vim_iswhite(cc))
7038 {
7039 if (gchar_cursor() != NUL)
7040 inc_cursor();
7041#ifdef FEAT_VIRTUALEDIT
7042 /* If the cursor is still at the same character, also keep
7043 * the "coladd". */
7044 if (gchar_cursor() == NUL
7045 && curwin->w_cursor.lnum == tpos.lnum
7046 && curwin->w_cursor.col == tpos.col)
7047 curwin->w_cursor.coladd = tpos.coladd;
7048#endif
7049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
7051
7052 /* If a space was inserted for auto-formatting, remove it now. */
7053 check_auto_format(TRUE);
7054
7055 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007056 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007057 * Do this when ESC was used or moving the cursor up/down.
7058 * Check for the old position still being valid, just in case the text
7059 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007060 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007061 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7062 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007064 pos_T tpos = curwin->w_cursor;
7065
7066 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007067 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007068 for (;;)
7069 {
7070 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7071 --curwin->w_cursor.col;
7072 cc = gchar_cursor();
7073 if (!vim_iswhite(cc))
7074 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007075 if (del_char(TRUE) == FAIL)
7076 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007077 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007078 if (curwin->w_cursor.lnum != tpos.lnum)
7079 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007080 else
7081 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007082 /* reset tpos, could have been invalidated in the loop above */
7083 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007084 tpos.col++;
7085 if (cc != NUL && gchar_pos(&tpos) == NUL)
7086 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 /* <C-S-Right> may have started Visual mode, adjust the position for
7090 * deleted characters. */
7091 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7092 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007093 int len = (int)STRLEN(ml_get_curline());
7094
7095 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007097 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007098#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 }
7102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 }
7104 }
7105 did_ai = FALSE;
7106#ifdef FEAT_SMARTINDENT
7107 did_si = FALSE;
7108 can_si = FALSE;
7109 can_si_back = FALSE;
7110#endif
7111
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007112 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7113 * now in a different buffer. */
7114 if (end_insert_pos != NULL)
7115 {
7116 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007117 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007118 curbuf->b_op_end = *end_insert_pos;
7119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120}
7121
7122/*
7123 * Set the last inserted text to a single character.
7124 * Used for the replace command.
7125 */
7126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007127set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 char_u *s;
7130
7131 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 if (last_insert != NULL)
7134 {
7135 s = last_insert;
7136 /* Use the CTRL-V only when entering a special char */
7137 if (c < ' ' || c == DEL)
7138 *s++ = Ctrl_V;
7139 s = add_char2buf(c, s);
7140 *s++ = ESC;
7141 *s++ = NUL;
7142 last_insert_skip = 0;
7143 }
7144}
7145
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007146#if defined(EXITFREE) || defined(PROTO)
7147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007148free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007149{
7150 vim_free(last_insert);
7151 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007152# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007153 vim_free(compl_orig_text);
7154 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007155# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007156}
7157#endif
7158
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159/*
7160 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7161 * and CSI. Handle multi-byte characters.
7162 * Returns a pointer to after the added bytes.
7163 */
7164 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007165add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166{
7167#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007168 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 int i;
7170 int len;
7171
7172 len = (*mb_char2bytes)(c, temp);
7173 for (i = 0; i < len; ++i)
7174 {
7175 c = temp[i];
7176#endif
7177 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7178 if (c == K_SPECIAL)
7179 {
7180 *s++ = K_SPECIAL;
7181 *s++ = KS_SPECIAL;
7182 *s++ = KE_FILLER;
7183 }
7184#ifdef FEAT_GUI
7185 else if (c == CSI)
7186 {
7187 *s++ = CSI;
7188 *s++ = KS_EXTRA;
7189 *s++ = (int)KE_CSI;
7190 }
7191#endif
7192 else
7193 *s++ = c;
7194#ifdef FEAT_MBYTE
7195 }
7196#endif
7197 return s;
7198}
7199
7200/*
7201 * move cursor to start of line
7202 * if flags & BL_WHITE move to first non-white
7203 * if flags & BL_SOL move to first non-white if startofline is set,
7204 * otherwise keep "curswant" column
7205 * if flags & BL_FIX don't leave the cursor on a NUL.
7206 */
7207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007208beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209{
7210 if ((flags & BL_SOL) && !p_sol)
7211 coladvance(curwin->w_curswant);
7212 else
7213 {
7214 curwin->w_cursor.col = 0;
7215#ifdef FEAT_VIRTUALEDIT
7216 curwin->w_cursor.coladd = 0;
7217#endif
7218
7219 if (flags & (BL_WHITE | BL_SOL))
7220 {
7221 char_u *ptr;
7222
7223 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7224 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7225 ++curwin->w_cursor.col;
7226 }
7227 curwin->w_set_curswant = TRUE;
7228 }
7229}
7230
7231/*
7232 * oneright oneleft cursor_down cursor_up
7233 *
7234 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007235 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 * Return OK when successful, FAIL when we hit a line of file boundary.
7237 */
7238
7239 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007240oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241{
7242 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244
7245#ifdef FEAT_VIRTUALEDIT
7246 if (virtual_active())
7247 {
7248 pos_T prevpos = curwin->w_cursor;
7249
7250 /* Adjust for multi-wide char (excluding TAB) */
7251 ptr = ml_get_cursor();
7252 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007253# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007255# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 ))
7259 ? ptr2cells(ptr) : 1));
7260 curwin->w_set_curswant = TRUE;
7261 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7262 return (prevpos.col != curwin->w_cursor.col
7263 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7264 }
7265#endif
7266
7267 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007268 if (*ptr == NUL)
7269 return FAIL; /* already at the very end */
7270
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007272 if (has_mbyte)
7273 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 else
7275#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007276 l = 1;
7277
7278 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7279 * contains "onemore". */
7280 if (ptr[l] == NUL
7281#ifdef FEAT_VIRTUALEDIT
7282 && (ve_flags & VE_ONEMORE) == 0
7283#endif
7284 )
7285 return FAIL;
7286 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287
7288 curwin->w_set_curswant = TRUE;
7289 return OK;
7290}
7291
7292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007293oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
7295#ifdef FEAT_VIRTUALEDIT
7296 if (virtual_active())
7297 {
7298 int width;
7299 int v = getviscol();
7300
7301 if (v == 0)
7302 return FAIL;
7303
7304# ifdef FEAT_LINEBREAK
7305 /* We might get stuck on 'showbreak', skip over it. */
7306 width = 1;
7307 for (;;)
7308 {
7309 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007310 /* getviscol() is slow, skip it when 'showbreak' is empty,
7311 * 'breakindent' is not set and there are no multi-byte
7312 * characters */
7313 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314# ifdef FEAT_MBYTE
7315 && !has_mbyte
7316# endif
7317 ) || getviscol() < v)
7318 break;
7319 ++width;
7320 }
7321# else
7322 coladvance(v - 1);
7323# endif
7324
7325 if (curwin->w_cursor.coladd == 1)
7326 {
7327 char_u *ptr;
7328
7329 /* Adjust for multi-wide char (not a TAB) */
7330 ptr = ml_get_cursor();
7331 if (*ptr != TAB && vim_isprintc(
7332# ifdef FEAT_MBYTE
7333 (*mb_ptr2char)(ptr)
7334# else
7335 *ptr
7336# endif
7337 ) && ptr2cells(ptr) > 1)
7338 curwin->w_cursor.coladd = 0;
7339 }
7340
7341 curwin->w_set_curswant = TRUE;
7342 return OK;
7343 }
7344#endif
7345
7346 if (curwin->w_cursor.col == 0)
7347 return FAIL;
7348
7349 curwin->w_set_curswant = TRUE;
7350 --curwin->w_cursor.col;
7351
7352#ifdef FEAT_MBYTE
7353 /* if the character on the left of the current cursor is a multi-byte
7354 * character, move to its first byte */
7355 if (has_mbyte)
7356 mb_adjust_cursor();
7357#endif
7358 return OK;
7359}
7360
7361 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007362cursor_up(
7363 long n,
7364 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365{
7366 linenr_T lnum;
7367
7368 if (n > 0)
7369 {
7370 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007371 /* This fails if the cursor is already in the first line or the count
7372 * is larger than the line number and '-' is in 'cpoptions' */
7373 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 return FAIL;
7375 if (n >= lnum)
7376 lnum = 1;
7377 else
7378#ifdef FEAT_FOLDING
7379 if (hasAnyFolding(curwin))
7380 {
7381 /*
7382 * Count each sequence of folded lines as one logical line.
7383 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007384 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 (void)hasFolding(lnum, &lnum, NULL);
7386
7387 while (n--)
7388 {
7389 /* move up one line */
7390 --lnum;
7391 if (lnum <= 1)
7392 break;
7393 /* If we entered a fold, move to the beginning, unless in
7394 * Insert mode or when 'foldopen' contains "all": it will open
7395 * in a moment. */
7396 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7397 (void)hasFolding(lnum, &lnum, NULL);
7398 }
7399 if (lnum < 1)
7400 lnum = 1;
7401 }
7402 else
7403#endif
7404 lnum -= n;
7405 curwin->w_cursor.lnum = lnum;
7406 }
7407
7408 /* try to advance to the column we want to be at */
7409 coladvance(curwin->w_curswant);
7410
7411 if (upd_topline)
7412 update_topline(); /* make sure curwin->w_topline is valid */
7413
7414 return OK;
7415}
7416
7417/*
7418 * Cursor down a number of logical lines.
7419 */
7420 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007421cursor_down(
7422 long n,
7423 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424{
7425 linenr_T lnum;
7426
7427 if (n > 0)
7428 {
7429 lnum = curwin->w_cursor.lnum;
7430#ifdef FEAT_FOLDING
7431 /* Move to last line of fold, will fail if it's the end-of-file. */
7432 (void)hasFolding(lnum, NULL, &lnum);
7433#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007434 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007435 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007436 if (lnum >= curbuf->b_ml.ml_line_count
7437 || (lnum + n > curbuf->b_ml.ml_line_count
7438 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 return FAIL;
7440 if (lnum + n >= curbuf->b_ml.ml_line_count)
7441 lnum = curbuf->b_ml.ml_line_count;
7442 else
7443#ifdef FEAT_FOLDING
7444 if (hasAnyFolding(curwin))
7445 {
7446 linenr_T last;
7447
7448 /* count each sequence of folded lines as one logical line */
7449 while (n--)
7450 {
7451 if (hasFolding(lnum, NULL, &last))
7452 lnum = last + 1;
7453 else
7454 ++lnum;
7455 if (lnum >= curbuf->b_ml.ml_line_count)
7456 break;
7457 }
7458 if (lnum > curbuf->b_ml.ml_line_count)
7459 lnum = curbuf->b_ml.ml_line_count;
7460 }
7461 else
7462#endif
7463 lnum += n;
7464 curwin->w_cursor.lnum = lnum;
7465 }
7466
7467 /* try to advance to the column we want to be at */
7468 coladvance(curwin->w_curswant);
7469
7470 if (upd_topline)
7471 update_topline(); /* make sure curwin->w_topline is valid */
7472
7473 return OK;
7474}
7475
7476/*
7477 * Stuff the last inserted text in the read buffer.
7478 * Last_insert actually is a copy of the redo buffer, so we
7479 * first have to remove the command.
7480 */
7481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007482stuff_inserted(
7483 int c, /* Command character to be inserted */
7484 long count, /* Repeat this many times */
7485 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
7487 char_u *esc_ptr;
7488 char_u *ptr;
7489 char_u *last_ptr;
7490 char_u last = NUL;
7491
7492 ptr = get_last_insert();
7493 if (ptr == NULL)
7494 {
7495 EMSG(_(e_noinstext));
7496 return FAIL;
7497 }
7498
7499 /* may want to stuff the command character, to start Insert mode */
7500 if (c != NUL)
7501 stuffcharReadbuff(c);
7502 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7503 *esc_ptr = NUL; /* remove the ESC */
7504
7505 /* when the last char is either "0" or "^" it will be quoted if no ESC
7506 * comes after it OR if it will inserted more than once and "ptr"
7507 * starts with ^D. -- Acevedo
7508 */
7509 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7510 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7511 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7512 {
7513 last = *last_ptr;
7514 *last_ptr = NUL;
7515 }
7516
7517 do
7518 {
7519 stuffReadbuff(ptr);
7520 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7521 if (last)
7522 stuffReadbuff((char_u *)(last == '0'
7523 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7524 : IF_EB("\026^", CTRL_V_STR "^")));
7525 }
7526 while (--count > 0);
7527
7528 if (last)
7529 *last_ptr = last;
7530
7531 if (esc_ptr != NULL)
7532 *esc_ptr = ESC; /* put the ESC back */
7533
7534 /* may want to stuff a trailing ESC, to get out of Insert mode */
7535 if (!no_esc)
7536 stuffcharReadbuff(ESC);
7537
7538 return OK;
7539}
7540
7541 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007542get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543{
7544 if (last_insert == NULL)
7545 return NULL;
7546 return last_insert + last_insert_skip;
7547}
7548
7549/*
7550 * Get last inserted string, and remove trailing <Esc>.
7551 * Returns pointer to allocated memory (must be freed) or NULL.
7552 */
7553 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007554get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555{
7556 char_u *s;
7557 int len;
7558
7559 if (last_insert == NULL)
7560 return NULL;
7561 s = vim_strsave(last_insert + last_insert_skip);
7562 if (s != NULL)
7563 {
7564 len = (int)STRLEN(s);
7565 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7566 s[len - 1] = NUL;
7567 }
7568 return s;
7569}
7570
7571/*
7572 * Check the word in front of the cursor for an abbreviation.
7573 * Called when the non-id character "c" has been entered.
7574 * When an abbreviation is recognized it is removed from the text and
7575 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7576 */
7577 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007578echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579{
7580 /* Don't check for abbreviation in paste mode, when disabled and just
7581 * after moving around with cursor keys. */
7582 if (p_paste || no_abbr || arrow_used)
7583 return FALSE;
7584
7585 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7586 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7587}
7588
7589/*
7590 * replace-stack functions
7591 *
7592 * When replacing characters, the replaced characters are remembered for each
7593 * new character. This is used to re-insert the old text when backspacing.
7594 *
7595 * There is a NUL headed list of characters for each character that is
7596 * currently in the file after the insertion point. When BS is used, one NUL
7597 * headed list is put back for the deleted character.
7598 *
7599 * For a newline, there are two NUL headed lists. One contains the characters
7600 * that the NL replaced. The extra one stores the characters after the cursor
7601 * that were deleted (always white space).
7602 *
7603 * Replace_offset is normally 0, in which case replace_push will add a new
7604 * character at the end of the stack. If replace_offset is not 0, that many
7605 * characters will be left on the stack above the newly inserted character.
7606 */
7607
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007608static char_u *replace_stack = NULL;
7609static long replace_stack_nr = 0; /* next entry in replace stack */
7610static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
7612 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007613replace_push(
7614 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615{
7616 char_u *p;
7617
7618 if (replace_stack_nr < replace_offset) /* nothing to do */
7619 return;
7620 if (replace_stack_len <= replace_stack_nr)
7621 {
7622 replace_stack_len += 50;
7623 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7624 if (p == NULL) /* out of memory */
7625 {
7626 replace_stack_len -= 50;
7627 return;
7628 }
7629 if (replace_stack != NULL)
7630 {
7631 mch_memmove(p, replace_stack,
7632 (size_t)(replace_stack_nr * sizeof(char_u)));
7633 vim_free(replace_stack);
7634 }
7635 replace_stack = p;
7636 }
7637 p = replace_stack + replace_stack_nr - replace_offset;
7638 if (replace_offset)
7639 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7640 *p = c;
7641 ++replace_stack_nr;
7642}
7643
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007644#if defined(FEAT_MBYTE) || defined(PROTO)
7645/*
7646 * Push a character onto the replace stack. Handles a multi-byte character in
7647 * reverse byte order, so that the first byte is popped off first.
7648 * Return the number of bytes done (includes composing characters).
7649 */
7650 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007651replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007652{
7653 int l = (*mb_ptr2len)(p);
7654 int j;
7655
7656 for (j = l - 1; j >= 0; --j)
7657 replace_push(p[j]);
7658 return l;
7659}
7660#endif
7661
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662/*
7663 * Pop one item from the replace stack.
7664 * return -1 if stack empty
7665 * return replaced character or NUL otherwise
7666 */
7667 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007668replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
7670 if (replace_stack_nr == 0)
7671 return -1;
7672 return (int)replace_stack[--replace_stack_nr];
7673}
7674
7675/*
7676 * Join the top two items on the replace stack. This removes to "off"'th NUL
7677 * encountered.
7678 */
7679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007680replace_join(
7681 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
7683 int i;
7684
7685 for (i = replace_stack_nr; --i >= 0; )
7686 if (replace_stack[i] == NUL && off-- <= 0)
7687 {
7688 --replace_stack_nr;
7689 mch_memmove(replace_stack + i, replace_stack + i + 1,
7690 (size_t)(replace_stack_nr - i));
7691 return;
7692 }
7693}
7694
7695/*
7696 * Pop bytes from the replace stack until a NUL is found, and insert them
7697 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7698 */
7699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007700replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
7702 int cc;
7703 int oldState = State;
7704
7705 State = NORMAL; /* don't want REPLACE here */
7706 while ((cc = replace_pop()) > 0)
7707 {
7708#ifdef FEAT_MBYTE
7709 mb_replace_pop_ins(cc);
7710#else
7711 ins_char(cc);
7712#endif
7713 dec_cursor();
7714 }
7715 State = oldState;
7716}
7717
7718#ifdef FEAT_MBYTE
7719/*
7720 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7721 * indicates a multi-byte char, pop the other bytes too.
7722 */
7723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007724mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
7726 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007727 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 int i;
7729 int c;
7730
7731 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7732 {
7733 buf[0] = cc;
7734 for (i = 1; i < n; ++i)
7735 buf[i] = replace_pop();
7736 ins_bytes_len(buf, n);
7737 }
7738 else
7739 ins_char(cc);
7740
7741 if (enc_utf8)
7742 /* Handle composing chars. */
7743 for (;;)
7744 {
7745 c = replace_pop();
7746 if (c == -1) /* stack empty */
7747 break;
7748 if ((n = MB_BYTE2LEN(c)) == 1)
7749 {
7750 /* Not a multi-byte char, put it back. */
7751 replace_push(c);
7752 break;
7753 }
7754 else
7755 {
7756 buf[0] = c;
7757 for (i = 1; i < n; ++i)
7758 buf[i] = replace_pop();
7759 if (utf_iscomposing(utf_ptr2char(buf)))
7760 ins_bytes_len(buf, n);
7761 else
7762 {
7763 /* Not a composing char, put it back. */
7764 for (i = n - 1; i >= 0; --i)
7765 replace_push(buf[i]);
7766 break;
7767 }
7768 }
7769 }
7770}
7771#endif
7772
7773/*
7774 * make the replace stack empty
7775 * (called when exiting replace mode)
7776 */
7777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007778replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780 vim_free(replace_stack);
7781 replace_stack = NULL;
7782 replace_stack_len = 0;
7783 replace_stack_nr = 0;
7784}
7785
7786/*
7787 * Handle doing a BS for one character.
7788 * cc < 0: replace stack empty, just move cursor
7789 * cc == 0: character was inserted, delete it
7790 * cc > 0: character was replaced, put cc (first byte of original char) back
7791 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007792 * When "limit_col" is >= 0, don't delete before this column. Matters when
7793 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 */
7795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007796replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797{
7798 int cc;
7799#ifdef FEAT_VREPLACE
7800 int orig_len = 0;
7801 int ins_len;
7802 int orig_vcols = 0;
7803 colnr_T start_vcol;
7804 char_u *p;
7805 int i;
7806 int vcol;
7807#endif
7808
7809 cc = replace_pop();
7810 if (cc > 0)
7811 {
7812#ifdef FEAT_VREPLACE
7813 if (State & VREPLACE_FLAG)
7814 {
7815 /* Get the number of screen cells used by the character we are
7816 * going to delete. */
7817 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7818 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7819 }
7820#endif
7821#ifdef FEAT_MBYTE
7822 if (has_mbyte)
7823 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007824 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825# ifdef FEAT_VREPLACE
7826 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007827 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828# endif
7829 replace_push(cc);
7830 }
7831 else
7832#endif
7833 {
7834 pchar_cursor(cc);
7835#ifdef FEAT_VREPLACE
7836 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007837 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838#endif
7839 }
7840 replace_pop_ins();
7841
7842#ifdef FEAT_VREPLACE
7843 if (State & VREPLACE_FLAG)
7844 {
7845 /* Get the number of screen cells used by the inserted characters */
7846 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007847 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 vcol = start_vcol;
7849 for (i = 0; i < ins_len; ++i)
7850 {
7851 vcol += chartabsize(p + i, vcol);
7852#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007853 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854#endif
7855 }
7856 vcol -= start_vcol;
7857
7858 /* Delete spaces that were inserted after the cursor to keep the
7859 * text aligned. */
7860 curwin->w_cursor.col += ins_len;
7861 while (vcol > orig_vcols && gchar_cursor() == ' ')
7862 {
7863 del_char(FALSE);
7864 ++orig_vcols;
7865 }
7866 curwin->w_cursor.col -= ins_len;
7867 }
7868#endif
7869
7870 /* mark the buffer as changed and prepare for displaying */
7871 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7872 }
7873 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007874 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875}
7876
7877#ifdef FEAT_CINDENT
7878/*
7879 * Return TRUE if C-indenting is on.
7880 */
7881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007882cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
7884 return (!p_paste && (curbuf->b_p_cin
7885# ifdef FEAT_EVAL
7886 || *curbuf->b_p_inde != NUL
7887# endif
7888 ));
7889}
7890#endif
7891
7892#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7893/*
7894 * Re-indent the current line, based on the current contents of it and the
7895 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7896 * confused what all the part that handles Control-T is doing that I'm not.
7897 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7898 */
7899
7900 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007901fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007903 int amount = get_the_indent();
7904
7905 if (amount >= 0)
7906 {
7907 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7908 if (linewhite(curwin->w_cursor.lnum))
7909 did_ai = TRUE; /* delete the indent if the line stays empty */
7910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911}
7912
7913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007914fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916 if (p_paste)
7917 return;
7918# ifdef FEAT_LISP
7919 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7920 fixthisline(get_lisp_indent);
7921# endif
7922# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7923 else
7924# endif
7925# ifdef FEAT_CINDENT
7926 if (cindent_on())
7927 do_c_expr_indent();
7928# endif
7929}
7930
7931#endif
7932
7933#ifdef FEAT_CINDENT
7934/*
7935 * return TRUE if 'cinkeys' contains the key "keytyped",
7936 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007937 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7939 *
7940 * "keytyped" can have a few special values:
7941 * KEY_OPEN_FORW
7942 * KEY_OPEN_BACK
7943 * KEY_COMPLETE just finished completion.
7944 *
7945 * If line_is_empty is TRUE accept keys with '0' before them.
7946 */
7947 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007948in_cinkeys(
7949 int keytyped,
7950 int when,
7951 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
7953 char_u *look;
7954 int try_match;
7955 int try_match_word;
7956 char_u *p;
7957 char_u *line;
7958 int icase;
7959 int i;
7960
Bram Moolenaar30848942009-12-24 14:46:12 +00007961 if (keytyped == NUL)
7962 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7963 return FALSE;
7964
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965#ifdef FEAT_EVAL
7966 if (*curbuf->b_p_inde != NUL)
7967 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7968 else
7969#endif
7970 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7971 while (*look)
7972 {
7973 /*
7974 * Find out if we want to try a match with this key, depending on
7975 * 'when' and a '*' or '!' before the key.
7976 */
7977 switch (when)
7978 {
7979 case '*': try_match = (*look == '*'); break;
7980 case '!': try_match = (*look == '!'); break;
7981 default: try_match = (*look != '*'); break;
7982 }
7983 if (*look == '*' || *look == '!')
7984 ++look;
7985
7986 /*
7987 * If there is a '0', only accept a match if the line is empty.
7988 * But may still match when typing last char of a word.
7989 */
7990 if (*look == '0')
7991 {
7992 try_match_word = try_match;
7993 if (!line_is_empty)
7994 try_match = FALSE;
7995 ++look;
7996 }
7997 else
7998 try_match_word = FALSE;
7999
8000 /*
8001 * does it look like a control character?
8002 */
8003 if (*look == '^'
8004#ifdef EBCDIC
8005 && (Ctrl_chr(look[1]) != 0)
8006#else
8007 && look[1] >= '?' && look[1] <= '_'
8008#endif
8009 )
8010 {
8011 if (try_match && keytyped == Ctrl_chr(look[1]))
8012 return TRUE;
8013 look += 2;
8014 }
8015 /*
8016 * 'o' means "o" command, open forward.
8017 * 'O' means "O" command, open backward.
8018 */
8019 else if (*look == 'o')
8020 {
8021 if (try_match && keytyped == KEY_OPEN_FORW)
8022 return TRUE;
8023 ++look;
8024 }
8025 else if (*look == 'O')
8026 {
8027 if (try_match && keytyped == KEY_OPEN_BACK)
8028 return TRUE;
8029 ++look;
8030 }
8031
8032 /*
8033 * 'e' means to check for "else" at start of line and just before the
8034 * cursor.
8035 */
8036 else if (*look == 'e')
8037 {
8038 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8039 {
8040 p = ml_get_curline();
8041 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8042 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8043 return TRUE;
8044 }
8045 ++look;
8046 }
8047
8048 /*
8049 * ':' only causes an indent if it is at the end of a label or case
8050 * statement, or when it was before typing the ':' (to fix
8051 * class::method for C++).
8052 */
8053 else if (*look == ':')
8054 {
8055 if (try_match && keytyped == ':')
8056 {
8057 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008058 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008060 /* Need to get the line again after cin_islabel(). */
8061 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 if (curwin->w_cursor.col > 2
8063 && p[curwin->w_cursor.col - 1] == ':'
8064 && p[curwin->w_cursor.col - 2] == ':')
8065 {
8066 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008067 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008068 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 p = ml_get_curline();
8070 p[curwin->w_cursor.col - 1] = ':';
8071 if (i)
8072 return TRUE;
8073 }
8074 }
8075 ++look;
8076 }
8077
8078
8079 /*
8080 * Is it a key in <>, maybe?
8081 */
8082 else if (*look == '<')
8083 {
8084 if (try_match)
8085 {
8086 /*
8087 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8088 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8089 * >, *, : and ! keys if they really really want to.
8090 */
8091 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8092 && keytyped == look[1])
8093 return TRUE;
8094
8095 if (keytyped == get_special_key_code(look + 1))
8096 return TRUE;
8097 }
8098 while (*look && *look != '>')
8099 look++;
8100 while (*look == '>')
8101 look++;
8102 }
8103
8104 /*
8105 * Is it a word: "=word"?
8106 */
8107 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8108 {
8109 ++look;
8110 if (*look == '~')
8111 {
8112 icase = TRUE;
8113 ++look;
8114 }
8115 else
8116 icase = FALSE;
8117 p = vim_strchr(look, ',');
8118 if (p == NULL)
8119 p = look + STRLEN(look);
8120 if ((try_match || try_match_word)
8121 && curwin->w_cursor.col >= (colnr_T)(p - look))
8122 {
8123 int match = FALSE;
8124
8125#ifdef FEAT_INS_EXPAND
8126 if (keytyped == KEY_COMPLETE)
8127 {
8128 char_u *s;
8129
8130 /* Just completed a word, check if it starts with "look".
8131 * search back for the start of a word. */
8132 line = ml_get_curline();
8133# ifdef FEAT_MBYTE
8134 if (has_mbyte)
8135 {
8136 char_u *n;
8137
8138 for (s = line + curwin->w_cursor.col; s > line; s = n)
8139 {
8140 n = mb_prevptr(line, s);
8141 if (!vim_iswordp(n))
8142 break;
8143 }
8144 }
8145 else
8146# endif
8147 for (s = line + curwin->w_cursor.col; s > line; --s)
8148 if (!vim_iswordc(s[-1]))
8149 break;
8150 if (s + (p - look) <= line + curwin->w_cursor.col
8151 && (icase
8152 ? MB_STRNICMP(s, look, p - look)
8153 : STRNCMP(s, look, p - look)) == 0)
8154 match = TRUE;
8155 }
8156 else
8157#endif
8158 /* TODO: multi-byte */
8159 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8160 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8161 {
8162 line = ml_get_cursor();
8163 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8164 || !vim_iswordc(line[-(p - look) - 1]))
8165 && (icase
8166 ? MB_STRNICMP(line - (p - look), look, p - look)
8167 : STRNCMP(line - (p - look), look, p - look))
8168 == 0)
8169 match = TRUE;
8170 }
8171 if (match && try_match_word && !try_match)
8172 {
8173 /* "0=word": Check if there are only blanks before the
8174 * word. */
8175 line = ml_get_curline();
8176 if ((int)(skipwhite(line) - line) !=
8177 (int)(curwin->w_cursor.col - (p - look)))
8178 match = FALSE;
8179 }
8180 if (match)
8181 return TRUE;
8182 }
8183 look = p;
8184 }
8185
8186 /*
8187 * ok, it's a boring generic character.
8188 */
8189 else
8190 {
8191 if (try_match && *look == keytyped)
8192 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008193 if (*look != NUL)
8194 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 }
8196
8197 /*
8198 * Skip over ", ".
8199 */
8200 look = skip_to_option_part(look);
8201 }
8202 return FALSE;
8203}
8204#endif /* FEAT_CINDENT */
8205
8206#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8207/*
8208 * Map Hebrew keyboard when in hkmap mode.
8209 */
8210 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008211hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212{
8213 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8214 {
8215 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8216 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8217 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8218 static char_u map[26] =
8219 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8220 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8221 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8222 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8223 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8224 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8225 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8226 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8227 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8228
8229 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8230 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8231 /* '-1'='sofit' */
8232 else if (c == 'x')
8233 return 'X';
8234 else if (c == 'q')
8235 return '\''; /* {geresh}={'} */
8236 else if (c == 246)
8237 return ' '; /* \"o --> ' ' for a german keyboard */
8238 else if (c == 228)
8239 return ' '; /* \"a --> ' ' -- / -- */
8240 else if (c == 252)
8241 return ' '; /* \"u --> ' ' -- / -- */
8242#ifdef EBCDIC
8243 else if (islower(c))
8244#else
8245 /* NOTE: islower() does not do the right thing for us on Linux so we
8246 * do this the same was as 5.7 and previous, so it works correctly on
8247 * all systems. Specifically, the e.g. Delete and Arrow keys are
8248 * munged and won't work if e.g. searching for Hebrew text.
8249 */
8250 else if (c >= 'a' && c <= 'z')
8251#endif
8252 return (int)(map[CharOrdLow(c)] + p_aleph);
8253 else
8254 return c;
8255 }
8256 else
8257 {
8258 switch (c)
8259 {
8260 case '`': return ';';
8261 case '/': return '.';
8262 case '\'': return ',';
8263 case 'q': return '/';
8264 case 'w': return '\'';
8265
8266 /* Hebrew letters - set offset from 'a' */
8267 case ',': c = '{'; break;
8268 case '.': c = 'v'; break;
8269 case ';': c = 't'; break;
8270 default: {
8271 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8272
8273#ifdef EBCDIC
8274 /* see note about islower() above */
8275 if (!islower(c))
8276#else
8277 if (c < 'a' || c > 'z')
8278#endif
8279 return c;
8280 c = str[CharOrdLow(c)];
8281 break;
8282 }
8283 }
8284
8285 return (int)(CharOrdLow(c) + p_aleph);
8286 }
8287}
8288#endif
8289
8290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008291ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292{
8293 int need_redraw = FALSE;
8294 int regname;
8295 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008296 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297
8298 /*
8299 * If we are going to wait for a character, show a '"'.
8300 */
8301 pc_status = PC_STATUS_UNSET;
8302 if (redrawing() && !char_avail())
8303 {
8304 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008305 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306
8307 edit_putchar('"', TRUE);
8308#ifdef FEAT_CMDL_INFO
8309 add_to_showcmd_c(Ctrl_R);
8310#endif
8311 }
8312
8313#ifdef USE_ON_FLY_SCROLL
8314 dont_scroll = TRUE; /* disallow scrolling here */
8315#endif
8316
8317 /*
8318 * Don't map the register name. This also prevents the mode message to be
8319 * deleted when ESC is hit.
8320 */
8321 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008322 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8325 {
8326 /* Get a third key for literal register insertion */
8327 literally = regname;
8328#ifdef FEAT_CMDL_INFO
8329 add_to_showcmd_c(literally);
8330#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008331 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 }
8334 --no_mapping;
8335
8336#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008337 /* Don't call u_sync() while typing the expression or giving an error
8338 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 ++no_u_sync;
8340 if (regname == '=')
8341 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008342# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008344# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008345 /* Sync undo when evaluating the expression calls setline() or
8346 * append(), so that it can be undone separately. */
8347 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008348
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008350# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 /* Restore the Input Method. */
8352 if (im_on)
8353 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008356 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8357 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008358 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 else
8362 {
8363#endif
8364 if (literally == Ctrl_O || literally == Ctrl_P)
8365 {
8366 /* Append the command to the redo buffer. */
8367 AppendCharToRedobuff(Ctrl_R);
8368 AppendCharToRedobuff(literally);
8369 AppendCharToRedobuff(regname);
8370
8371 do_put(regname, BACKWARD, 1L,
8372 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8373 }
8374 else if (insert_reg(regname, literally) == FAIL)
8375 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008376 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 need_redraw = TRUE; /* remove the '"' */
8378 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008379 else if (stop_insert_mode)
8380 /* When the '=' register was used and a function was invoked that
8381 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8382 * insert anything, need to remove the '"' */
8383 need_redraw = TRUE;
8384
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385#ifdef FEAT_EVAL
8386 }
8387 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008388 if (u_sync_once == 1)
8389 ins_need_undo = TRUE;
8390 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391#endif
8392#ifdef FEAT_CMDL_INFO
8393 clear_showcmd();
8394#endif
8395
8396 /* If the inserted register is empty, we need to remove the '"' */
8397 if (need_redraw || stuff_empty())
8398 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008399
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008400 /* Disallow starting Visual mode here, would get a weird mode. */
8401 if (!vis_active && VIsual_active)
8402 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403}
8404
8405/*
8406 * CTRL-G commands in Insert mode.
8407 */
8408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008409ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410{
8411 int c;
8412
8413#ifdef FEAT_INS_EXPAND
8414 /* Right after CTRL-X the cursor will be after the ruler. */
8415 setcursor();
8416#endif
8417
8418 /*
8419 * Don't map the second key. This also prevents the mode message to be
8420 * deleted when ESC is hit.
8421 */
8422 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008423 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 --no_mapping;
8425 switch (c)
8426 {
8427 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8428 case K_UP:
8429 case Ctrl_K:
8430 case 'k': ins_up(TRUE);
8431 break;
8432
8433 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8434 case K_DOWN:
8435 case Ctrl_J:
8436 case 'j': ins_down(TRUE);
8437 break;
8438
8439 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008440 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008442
8443 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008444 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008445 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008446 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 break;
8448
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008449 /* CTRL-G U: do not break undo with the next char */
8450 case 'U':
8451 /* Allow one left/right cursor movement with the next char,
8452 * without breaking undo. */
8453 dont_sync_undo = MAYBE;
8454 break;
8455
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008457 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 }
8459}
8460
8461/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008462 * CTRL-^ in Insert mode.
8463 */
8464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008465ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008466{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008467 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008468 {
8469 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8470 if (State & LANGMAP)
8471 {
8472 curbuf->b_p_iminsert = B_IMODE_NONE;
8473 State &= ~LANGMAP;
8474 }
8475 else
8476 {
8477 curbuf->b_p_iminsert = B_IMODE_LMAP;
8478 State |= LANGMAP;
8479#ifdef USE_IM_CONTROL
8480 im_set_active(FALSE);
8481#endif
8482 }
8483 }
8484#ifdef USE_IM_CONTROL
8485 else
8486 {
8487 /* There are no ":lmap" mappings, toggle IM */
8488 if (im_get_status())
8489 {
8490 curbuf->b_p_iminsert = B_IMODE_NONE;
8491 im_set_active(FALSE);
8492 }
8493 else
8494 {
8495 curbuf->b_p_iminsert = B_IMODE_IM;
8496 State &= ~LANGMAP;
8497 im_set_active(TRUE);
8498 }
8499 }
8500#endif
8501 set_iminsert_global();
8502 showmode();
8503#ifdef FEAT_GUI
8504 /* may show different cursor shape or color */
8505 if (gui.in_use)
8506 gui_update_cursor(TRUE, FALSE);
8507#endif
8508#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8509 /* Show/unshow value of 'keymap' in status lines. */
8510 status_redraw_curbuf();
8511#endif
8512}
8513
8514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 * Handle ESC in insert mode.
8516 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8517 * insert.
8518 */
8519 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008520ins_esc(
8521 long *count,
8522 int cmdchar,
8523 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524{
8525 int temp;
8526 static int disabled_redraw = FALSE;
8527
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008528#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008529 check_spell_redraw();
8530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531#if defined(FEAT_HANGULIN)
8532# if defined(ESC_CHG_TO_ENG_MODE)
8533 hangul_input_state_set(0);
8534# endif
8535 if (composing_hangul)
8536 {
8537 push_raw_key(composing_hangul_buffer, 2);
8538 composing_hangul = 0;
8539 }
8540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541
8542 temp = curwin->w_cursor.col;
8543 if (disabled_redraw)
8544 {
8545 --RedrawingDisabled;
8546 disabled_redraw = FALSE;
8547 }
8548 if (!arrow_used)
8549 {
8550 /*
8551 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008552 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8553 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 */
8555 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008556 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
8558 /*
8559 * Repeating insert may take a long time. Check for
8560 * interrupt now and then.
8561 */
8562 if (*count > 0)
8563 {
8564 line_breakcheck();
8565 if (got_int)
8566 *count = 0;
8567 }
8568
8569 if (--*count > 0) /* repeat what was typed */
8570 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008571 /* Vi repeats the insert without replacing characters. */
8572 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8573 State &= ~REPLACE_FLAG;
8574
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 (void)start_redo_ins();
8576 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008577 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 ++RedrawingDisabled;
8579 disabled_redraw = TRUE;
8580 return FALSE; /* repeat the insert */
8581 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008582 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 undisplay_dollar();
8584 }
8585
8586 /* When an autoindent was removed, curswant stays after the
8587 * indent */
8588 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8589 curwin->w_set_curswant = TRUE;
8590
8591 /* Remember the last Insert position in the '^ mark. */
8592 if (!cmdmod.keepjumps)
8593 curbuf->b_last_insert = curwin->w_cursor;
8594
8595 /*
8596 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008597 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008599 if (!nomove
8600 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601#ifdef FEAT_VIRTUALEDIT
8602 || curwin->w_cursor.coladd > 0
8603#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008604 )
8605 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008606 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607#ifdef FEAT_RIGHTLEFT
8608 && !revins_on
8609#endif
8610 )
8611 {
8612#ifdef FEAT_VIRTUALEDIT
8613 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8614 {
8615 oneleft();
8616 if (restart_edit != NUL)
8617 ++curwin->w_cursor.coladd;
8618 }
8619 else
8620#endif
8621 {
8622 --curwin->w_cursor.col;
8623#ifdef FEAT_MBYTE
8624 /* Correct cursor for multi-byte character. */
8625 if (has_mbyte)
8626 mb_adjust_cursor();
8627#endif
8628 }
8629 }
8630
8631#ifdef USE_IM_CONTROL
8632 /* Disable IM to allow typing English directly for Normal mode commands.
8633 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8634 * well). */
8635 if (!(State & LANGMAP))
8636 im_save_status(&curbuf->b_p_iminsert);
8637 im_set_active(FALSE);
8638#endif
8639
8640 State = NORMAL;
8641 /* need to position cursor again (e.g. when on a TAB ) */
8642 changed_cline_bef_curs();
8643
8644#ifdef FEAT_MOUSE
8645 setmouse();
8646#endif
8647#ifdef CURSOR_SHAPE
8648 ui_cursor_shape(); /* may show different cursor shape */
8649#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008650 if (!p_ek)
8651 /* Re-enable bracketed paste mode. */
8652 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
8654 /*
8655 * When recording or for CTRL-O, need to display the new mode.
8656 * Otherwise remove the mode message.
8657 */
8658 if (Recording || restart_edit != NUL)
8659 showmode();
8660 else if (p_smd)
8661 MSG("");
8662
8663 return TRUE; /* exit Insert mode */
8664}
8665
8666#ifdef FEAT_RIGHTLEFT
8667/*
8668 * Toggle language: hkmap and revins_on.
8669 * Move to end of reverse inserted text.
8670 */
8671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008672ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673{
8674 if (revins_on && revins_chars && revins_scol >= 0)
8675 {
8676 while (gchar_cursor() != NUL && revins_chars--)
8677 ++curwin->w_cursor.col;
8678 }
8679 p_ri = !p_ri;
8680 revins_on = (State == INSERT && p_ri);
8681 if (revins_on)
8682 {
8683 revins_scol = curwin->w_cursor.col;
8684 revins_legal++;
8685 revins_chars = 0;
8686 undisplay_dollar();
8687 }
8688 else
8689 revins_scol = -1;
8690#ifdef FEAT_FKMAP
8691 if (p_altkeymap)
8692 {
8693 /*
8694 * to be consistent also for redo command, using '.'
8695 * set arrow_used to true and stop it - causing to redo
8696 * characters entered in one mode (normal/reverse insert).
8697 */
8698 arrow_used = TRUE;
8699 (void)stop_arrow();
8700 p_fkmap = curwin->w_p_rl ^ p_ri;
8701 if (p_fkmap && p_ri)
8702 State = INSERT;
8703 }
8704 else
8705#endif
8706 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8707 showmode();
8708}
8709#endif
8710
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711/*
8712 * If 'keymodel' contains "startsel", may start selection.
8713 * Returns TRUE when a CTRL-O and other keys stuffed.
8714 */
8715 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008716ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717{
8718 if (km_startsel)
8719 switch (c)
8720 {
8721 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 case K_PAGEUP:
8724 case K_KPAGEUP:
8725 case K_PAGEDOWN:
8726 case K_KPAGEDOWN:
8727# ifdef MACOS
8728 case K_LEFT:
8729 case K_RIGHT:
8730 case K_UP:
8731 case K_DOWN:
8732 case K_END:
8733 case K_HOME:
8734# endif
8735 if (!(mod_mask & MOD_MASK_SHIFT))
8736 break;
8737 /* FALLTHROUGH */
8738 case K_S_LEFT:
8739 case K_S_RIGHT:
8740 case K_S_UP:
8741 case K_S_DOWN:
8742 case K_S_END:
8743 case K_S_HOME:
8744 /* Start selection right away, the cursor can move with
8745 * CTRL-O when beyond the end of the line. */
8746 start_selection();
8747
8748 /* Execute the key in (insert) Select mode. */
8749 stuffcharReadbuff(Ctrl_O);
8750 if (mod_mask)
8751 {
8752 char_u buf[4];
8753
8754 buf[0] = K_SPECIAL;
8755 buf[1] = KS_MODIFIER;
8756 buf[2] = mod_mask;
8757 buf[3] = NUL;
8758 stuffReadbuff(buf);
8759 }
8760 stuffcharReadbuff(c);
8761 return TRUE;
8762 }
8763 return FALSE;
8764}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765
8766/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008767 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008768 */
8769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008770ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008771{
8772#ifdef FEAT_FKMAP
8773 if (p_fkmap && p_ri)
8774 {
8775 beep_flush();
8776 EMSG(farsi_text_3); /* encoded in Farsi */
8777 return;
8778 }
8779#endif
8780
8781#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008782# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008783 set_vim_var_string(VV_INSERTMODE,
8784 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008785# ifdef FEAT_VREPLACE
8786 replaceState == VREPLACE ? "v" :
8787# endif
8788 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008789# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008790 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8791#endif
8792 if (State & REPLACE_FLAG)
8793 State = INSERT | (State & LANGMAP);
8794 else
8795 State = replaceState | (State & LANGMAP);
8796 AppendCharToRedobuff(K_INS);
8797 showmode();
8798#ifdef CURSOR_SHAPE
8799 ui_cursor_shape(); /* may show different cursor shape */
8800#endif
8801}
8802
8803/*
8804 * Pressed CTRL-O in Insert mode.
8805 */
8806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008807ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008808{
8809#ifdef FEAT_VREPLACE
8810 if (State & VREPLACE_FLAG)
8811 restart_edit = 'V';
8812 else
8813#endif
8814 if (State & REPLACE_FLAG)
8815 restart_edit = 'R';
8816 else
8817 restart_edit = 'I';
8818#ifdef FEAT_VIRTUALEDIT
8819 if (virtual_active())
8820 ins_at_eol = FALSE; /* cursor always keeps its column */
8821 else
8822#endif
8823 ins_at_eol = (gchar_cursor() == NUL);
8824}
8825
8826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 * If the cursor is on an indent, ^T/^D insert/delete one
8828 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008829 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 * with vi. But vi only supports ^T and ^D after an
8831 * autoindent, we support it everywhere.
8832 */
8833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008834ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
8836 if (stop_arrow() == FAIL)
8837 return;
8838 AppendCharToRedobuff(c);
8839
8840 /*
8841 * 0^D and ^^D: remove all indent.
8842 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008843 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8844 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 {
8846 --curwin->w_cursor.col;
8847 (void)del_char(FALSE); /* delete the '^' or '0' */
8848 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8849 if (State & REPLACE_FLAG)
8850 replace_pop_ins();
8851 if (lastc == '^')
8852 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008853 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 }
8855 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008856 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857
8858 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8859 did_ai = FALSE;
8860#ifdef FEAT_SMARTINDENT
8861 did_si = FALSE;
8862 can_si = FALSE;
8863 can_si_back = FALSE;
8864#endif
8865#ifdef FEAT_CINDENT
8866 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8867#endif
8868}
8869
8870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008871ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
8873 int temp;
8874
8875 if (stop_arrow() == FAIL)
8876 return;
8877 if (gchar_cursor() == NUL) /* delete newline */
8878 {
8879 temp = curwin->w_cursor.col;
8880 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008881 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008882 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 else
8884 curwin->w_cursor.col = temp;
8885 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008886 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8887 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 did_ai = FALSE;
8889#ifdef FEAT_SMARTINDENT
8890 did_si = FALSE;
8891 can_si = FALSE;
8892 can_si_back = FALSE;
8893#endif
8894 AppendCharToRedobuff(K_DEL);
8895}
8896
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008897static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008898
8899/*
8900 * Delete one character for ins_bs().
8901 */
8902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008903ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008904{
8905 dec_cursor();
8906 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8907 if (State & REPLACE_FLAG)
8908 {
8909 /* Don't delete characters before the insert point when in
8910 * Replace mode */
8911 if (curwin->w_cursor.lnum != Insstart.lnum
8912 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008913 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008914 }
8915 else
8916 (void)del_char(FALSE);
8917}
8918
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919/*
8920 * Handle Backspace, delete-word and delete-line in Insert mode.
8921 * Return TRUE when backspace was actually used.
8922 */
8923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008924ins_bs(
8925 int c,
8926 int mode,
8927 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928{
8929 linenr_T lnum;
8930 int cc;
8931 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008932 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 colnr_T mincol;
8934 int did_backspace = FALSE;
8935 int in_indent;
8936 int oldState;
8937#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008938 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939#endif
8940
8941 /*
8942 * can't delete anything in an empty file
8943 * can't backup past first character in buffer
8944 * can't backup past starting point unless 'backspace' > 1
8945 * can backup to a previous line if 'backspace' == 0
8946 */
8947 if ( bufempty()
8948 || (
8949#ifdef FEAT_RIGHTLEFT
8950 !revins_on &&
8951#endif
8952 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8953 || (!can_bs(BS_START)
8954 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008955 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8956 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8958 && curwin->w_cursor.col <= ai_col)
8959 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8960 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008961 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 return FALSE;
8963 }
8964
8965 if (stop_arrow() == FAIL)
8966 return FALSE;
8967 in_indent = inindent(0);
8968#ifdef FEAT_CINDENT
8969 if (in_indent)
8970 can_cindent = FALSE;
8971#endif
8972#ifdef FEAT_COMMENTS
8973 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8974#endif
8975#ifdef FEAT_RIGHTLEFT
8976 if (revins_on) /* put cursor after last inserted char */
8977 inc_cursor();
8978#endif
8979
8980#ifdef FEAT_VIRTUALEDIT
8981 /* Virtualedit:
8982 * BACKSPACE_CHAR eats a virtual space
8983 * BACKSPACE_WORD eats all coladd
8984 * BACKSPACE_LINE eats all coladd and keeps going
8985 */
8986 if (curwin->w_cursor.coladd > 0)
8987 {
8988 if (mode == BACKSPACE_CHAR)
8989 {
8990 --curwin->w_cursor.coladd;
8991 return TRUE;
8992 }
8993 if (mode == BACKSPACE_WORD)
8994 {
8995 curwin->w_cursor.coladd = 0;
8996 return TRUE;
8997 }
8998 curwin->w_cursor.coladd = 0;
8999 }
9000#endif
9001
9002 /*
9003 * delete newline!
9004 */
9005 if (curwin->w_cursor.col == 0)
9006 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009007 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009008 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009#ifdef FEAT_RIGHTLEFT
9010 || revins_on
9011#endif
9012 )
9013 {
9014 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9015 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9016 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009017 --Insstart.lnum;
9018 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 }
9020 /*
9021 * In replace mode:
9022 * cc < 0: NL was inserted, delete it
9023 * cc >= 0: NL was replaced, put original characters back
9024 */
9025 cc = -1;
9026 if (State & REPLACE_FLAG)
9027 cc = replace_pop(); /* returns -1 if NL was inserted */
9028 /*
9029 * In replace mode, in the line we started replacing, we only move the
9030 * cursor.
9031 */
9032 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9033 {
9034 dec_cursor();
9035 }
9036 else
9037 {
9038#ifdef FEAT_VREPLACE
9039 if (!(State & VREPLACE_FLAG)
9040 || curwin->w_cursor.lnum > orig_line_count)
9041#endif
9042 {
9043 temp = gchar_cursor(); /* remember current char */
9044 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009045
9046 /* When "aw" is in 'formatoptions' we must delete the space at
9047 * the end of the line, otherwise the line will be broken
9048 * again when auto-formatting. */
9049 if (has_format_option(FO_AUTO)
9050 && has_format_option(FO_WHITE_PAR))
9051 {
9052 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9053 TRUE);
9054 int len;
9055
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009056 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009057 if (len > 0 && ptr[len - 1] == ' ')
9058 ptr[len - 1] = NUL;
9059 }
9060
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009061 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 if (temp == NUL && gchar_cursor() != NUL)
9063 inc_cursor();
9064 }
9065#ifdef FEAT_VREPLACE
9066 else
9067 dec_cursor();
9068#endif
9069
9070 /*
9071 * In REPLACE mode we have to put back the text that was replaced
9072 * by the NL. On the replace stack is first a NUL-terminated
9073 * sequence of characters that were deleted and then the
9074 * characters that NL replaced.
9075 */
9076 if (State & REPLACE_FLAG)
9077 {
9078 /*
9079 * Do the next ins_char() in NORMAL state, to
9080 * prevent ins_char() from replacing characters and
9081 * avoiding showmatch().
9082 */
9083 oldState = State;
9084 State = NORMAL;
9085 /*
9086 * restore characters (blanks) deleted after cursor
9087 */
9088 while (cc > 0)
9089 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009090 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091#ifdef FEAT_MBYTE
9092 mb_replace_pop_ins(cc);
9093#else
9094 ins_char(cc);
9095#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009096 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 cc = replace_pop();
9098 }
9099 /* restore the characters that NL replaced */
9100 replace_pop_ins();
9101 State = oldState;
9102 }
9103 }
9104 did_ai = FALSE;
9105 }
9106 else
9107 {
9108 /*
9109 * Delete character(s) before the cursor.
9110 */
9111#ifdef FEAT_RIGHTLEFT
9112 if (revins_on) /* put cursor on last inserted char */
9113 dec_cursor();
9114#endif
9115 mincol = 0;
9116 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009117 if (mode == BACKSPACE_LINE
9118 && (curbuf->b_p_ai
9119#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009120 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009121#endif
9122 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123#ifdef FEAT_RIGHTLEFT
9124 && !revins_on
9125#endif
9126 )
9127 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009128 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009130 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009132 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 }
9134
9135 /*
9136 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9137 */
9138 if ( mode == BACKSPACE_CHAR
9139 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009140 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009141 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 && (*(ml_get_cursor() - 1) == TAB
9143 || (*(ml_get_cursor() - 1) == ' '
9144 && (!*inserted_space_p
9145 || arrow_used))))))
9146 {
9147 int ts;
9148 colnr_T vcol;
9149 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009150 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151
9152 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009153 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009154 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009156 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 /* Compute the virtual column where we want to be. Since
9158 * 'showbreak' may get in the way, need to get the last column of
9159 * the previous character. */
9160 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009161 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 dec_cursor();
9163 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9164 inc_cursor();
9165 want_vcol = (want_vcol / ts) * ts;
9166
9167 /* delete characters until we are at or before want_vcol */
9168 while (vcol > want_vcol
9169 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009170 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171
9172 /* insert extra spaces until we are at want_vcol */
9173 while (vcol < want_vcol)
9174 {
9175 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009176 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9177 && curwin->w_cursor.col < Insstart_orig.col)
9178 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179
9180#ifdef FEAT_VREPLACE
9181 if (State & VREPLACE_FLAG)
9182 ins_char(' ');
9183 else
9184#endif
9185 {
9186 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009187 if ((State & REPLACE_FLAG))
9188 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 }
9190 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9191 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009192
9193 /* If we are now back where we started delete one character. Can
9194 * happen when using 'sts' and 'linebreak'. */
9195 if (vcol >= start_vcol)
9196 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 }
9198
9199 /*
9200 * Delete upto starting point, start of line or previous word.
9201 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009202 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009204#ifdef FEAT_MBYTE
9205 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009207 if (has_mbyte)
9208 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009210 do
9211 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009213 if (!revins_on) /* put cursor on char to be deleted */
9214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009216
9217 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009219 /* look multi-byte character class */
9220 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009222 prev_cclass = cclass;
9223 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009226
9227 /* start of word? */
9228 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9229 {
9230 mode = BACKSPACE_WORD_NOT_SPACE;
9231 temp = vim_iswordc(cc);
9232 }
9233 /* end of word? */
9234 else if (mode == BACKSPACE_WORD_NOT_SPACE
9235 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9236#ifdef FEAT_MBYTE
9237 || prev_cclass != cclass
9238#endif
9239 ))
9240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009242 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009244 inc_cursor();
9245#ifdef FEAT_RIGHTLEFT
9246 else if (State & REPLACE_FLAG)
9247 dec_cursor();
9248#endif
9249 break;
9250 }
9251 if (State & REPLACE_FLAG)
9252 replace_do_bs(-1);
9253 else
9254 {
9255#ifdef FEAT_MBYTE
9256 if (enc_utf8 && p_deco)
9257 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9258#endif
9259 (void)del_char(FALSE);
9260#ifdef FEAT_MBYTE
9261 /*
9262 * If there are combining characters and 'delcombine' is set
9263 * move the cursor back. Don't back up before the base
9264 * character.
9265 */
9266 if (enc_utf8 && p_deco && cpc[0] != NUL)
9267 inc_cursor();
9268#endif
9269#ifdef FEAT_RIGHTLEFT
9270 if (revins_chars)
9271 {
9272 revins_chars--;
9273 revins_legal++;
9274 }
9275 if (revins_on && gchar_cursor() == NUL)
9276 break;
9277#endif
9278 }
9279 /* Just a single backspace?: */
9280 if (mode == BACKSPACE_CHAR)
9281 break;
9282 } while (
9283#ifdef FEAT_RIGHTLEFT
9284 revins_on ||
9285#endif
9286 (curwin->w_cursor.col > mincol
9287 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9288 || curwin->w_cursor.col != Insstart_orig.col)));
9289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 did_backspace = TRUE;
9291 }
9292#ifdef FEAT_SMARTINDENT
9293 did_si = FALSE;
9294 can_si = FALSE;
9295 can_si_back = FALSE;
9296#endif
9297 if (curwin->w_cursor.col <= 1)
9298 did_ai = FALSE;
9299 /*
9300 * It's a little strange to put backspaces into the redo
9301 * buffer, but it makes auto-indent a lot easier to deal
9302 * with.
9303 */
9304 AppendCharToRedobuff(c);
9305
9306 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009307 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9308 && curwin->w_cursor.col < Insstart_orig.col)
9309 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310
9311 /* vi behaviour: the cursor moves backward but the character that
9312 * was there remains visible
9313 * Vim behaviour: the cursor moves backward and the character that
9314 * was there is erased from the screen.
9315 * We can emulate the vi behaviour by pretending there is a dollar
9316 * displayed even when there isn't.
9317 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009318 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 dollar_vcol = curwin->w_virtcol;
9320
Bram Moolenaarce3be472008-01-14 19:12:28 +00009321#ifdef FEAT_FOLDING
9322 /* When deleting a char the cursor line must never be in a closed fold.
9323 * E.g., when 'foldmethod' is indent and deleting the first non-white
9324 * char before a Tab. */
9325 if (did_backspace)
9326 foldOpenCursor();
9327#endif
9328
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 return did_backspace;
9330}
9331
9332#ifdef FEAT_MOUSE
9333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009334ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335{
9336 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009337 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338
9339# ifdef FEAT_GUI
9340 /* When GUI is active, also move/paste when 'mouse' is empty */
9341 if (!gui.in_use)
9342# endif
9343 if (!mouse_has(MOUSE_INSERT))
9344 return;
9345
9346 undisplay_dollar();
9347 tpos = curwin->w_cursor;
9348 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9349 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009350#ifdef FEAT_WINDOWS
9351 win_T *new_curwin = curwin;
9352
9353 if (curwin != old_curwin && win_valid(old_curwin))
9354 {
9355 /* Mouse took us to another window. We need to go back to the
9356 * previous one to stop insert there properly. */
9357 curwin = old_curwin;
9358 curbuf = curwin->w_buffer;
9359 }
9360#endif
9361 start_arrow(curwin == old_curwin ? &tpos : NULL);
9362#ifdef FEAT_WINDOWS
9363 if (curwin != new_curwin && win_valid(new_curwin))
9364 {
9365 curwin = new_curwin;
9366 curbuf = curwin->w_buffer;
9367 }
9368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369# ifdef FEAT_CINDENT
9370 can_cindent = TRUE;
9371# endif
9372 }
9373
9374#ifdef FEAT_WINDOWS
9375 /* redraw status lines (in case another window became active) */
9376 redraw_statuslines();
9377#endif
9378}
9379
9380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009381ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
9383 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009384# if defined(FEAT_WINDOWS)
9385 win_T *old_curwin = curwin;
9386# endif
9387# ifdef FEAT_INS_EXPAND
9388 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389# endif
9390
9391 tpos = curwin->w_cursor;
9392
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009393# ifdef FEAT_WINDOWS
9394 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 {
9396 int row, col;
9397
9398 row = mouse_row;
9399 col = mouse_col;
9400
9401 /* find the window at the pointer coordinates */
9402 curwin = mouse_find_win(&row, &col);
9403 curbuf = curwin->w_buffer;
9404 }
9405 if (curwin == old_curwin)
9406# endif
9407 undisplay_dollar();
9408
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009409# ifdef FEAT_INS_EXPAND
9410 /* Don't scroll the window in which completion is being done. */
9411 if (!pum_visible()
9412# if defined(FEAT_WINDOWS)
9413 || curwin != old_curwin
9414# endif
9415 )
9416# endif
9417 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009418 if (dir == MSCR_DOWN || dir == MSCR_UP)
9419 {
9420 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9421 scroll_redraw(dir,
9422 (long)(curwin->w_botline - curwin->w_topline));
9423 else
9424 scroll_redraw(dir, 3L);
9425 }
9426#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009427 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009428 {
9429 int val, step = 6;
9430
9431 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9432 step = W_WIDTH(curwin);
9433 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9434 if (val < 0)
9435 val = 0;
9436 gui_do_horiz_scroll(val, TRUE);
9437 }
9438#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009439# ifdef FEAT_INS_EXPAND
9440 did_scroll = TRUE;
9441# endif
9442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009444# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 curwin->w_redr_status = TRUE;
9446
9447 curwin = old_curwin;
9448 curbuf = curwin->w_buffer;
9449# endif
9450
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009451# ifdef FEAT_INS_EXPAND
9452 /* The popup menu may overlay the window, need to redraw it.
9453 * TODO: Would be more efficient to only redraw the windows that are
9454 * overlapped by the popup menu. */
9455 if (pum_visible() && did_scroll)
9456 {
9457 redraw_all_later(NOT_VALID);
9458 ins_compl_show_pum();
9459 }
9460# endif
9461
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 if (!equalpos(curwin->w_cursor, tpos))
9463 {
9464 start_arrow(&tpos);
9465# ifdef FEAT_CINDENT
9466 can_cindent = TRUE;
9467# endif
9468 }
9469}
9470#endif
9471
Bram Moolenaarec2da362017-01-21 20:04:22 +01009472/*
9473 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9474 * P_PE literally.
9475 * When "drop" is TRUE then consume the text and drop it.
9476 */
9477 int
9478bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9479{
9480 int c;
9481 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9482 int idx = 0;
9483 char_u *end = find_termcode((char_u *)"PE");
9484 int ret_char = -1;
9485 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009486 int save_paste = p_paste;
9487 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009488
9489 /* If the end code is too long we can't detect it, read everything. */
9490 if (STRLEN(end) >= NUMBUFLEN)
9491 end = NULL;
9492 ++no_mapping;
9493 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009494 p_paste = TRUE;
9495 curbuf->b_p_ai = FALSE;
9496
Bram Moolenaarec2da362017-01-21 20:04:22 +01009497 for (;;)
9498 {
9499 /* When the end is not defined read everything. */
9500 if (end == NULL && vpeekc() == NUL)
9501 break;
9502 c = plain_vgetc();
9503#ifdef FEAT_MBYTE
9504 if (has_mbyte)
9505 idx += (*mb_char2bytes)(c, buf + idx);
9506 else
9507#endif
9508 buf[idx++] = c;
9509 buf[idx] = NUL;
9510 if (end != NUL && STRNCMP(buf, end, idx) == 0)
9511 {
9512 if (end[idx] == NUL)
9513 break; /* Found the end of paste code. */
9514 continue;
9515 }
9516 if (!drop)
9517 {
9518 switch (mode)
9519 {
9520 case PASTE_CMDLINE:
9521 put_on_cmdline(buf, idx, TRUE);
9522 break;
9523
9524 case PASTE_EX:
9525 if (gap != NULL && ga_grow(gap, idx) == OK)
9526 {
9527 mch_memmove((char *)gap->ga_data + gap->ga_len,
9528 buf, (size_t)idx);
9529 gap->ga_len += idx;
9530 }
9531 break;
9532
9533 case PASTE_INSERT:
9534 if (stop_arrow() == OK)
9535 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009536 c = buf[0];
9537 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9538 ins_eol(c);
9539 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009540 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009541 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009542 AppendToRedobuffLit(buf, idx);
9543 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009544 }
9545 break;
9546
9547 case PASTE_ONE_CHAR:
9548 if (ret_char == -1)
9549 {
9550#ifdef FEAT_MBYTE
9551 if (has_mbyte)
9552 ret_char = (*mb_ptr2char)(buf);
9553 else
9554#endif
9555 ret_char = buf[0];
9556 }
9557 break;
9558 }
9559 }
9560 idx = 0;
9561 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009562
Bram Moolenaarec2da362017-01-21 20:04:22 +01009563 --no_mapping;
9564 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009565 p_paste = save_paste;
9566 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009567
9568 return ret_char;
9569}
9570
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009571#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009573ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009574{
9575 /* We will be leaving the current window, unless closing another tab. */
9576 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9577 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9578 {
9579 undisplay_dollar();
9580 start_arrow(&curwin->w_cursor);
9581# ifdef FEAT_CINDENT
9582 can_cindent = TRUE;
9583# endif
9584 }
9585
9586 if (c == K_TABLINE)
9587 goto_tabpage(current_tab);
9588 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009589 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009590 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009591 redraw_statuslines(); /* will redraw the tabline when needed */
9592 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009593}
9594#endif
9595
9596#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009598ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599{
9600 pos_T tpos;
9601
9602 undisplay_dollar();
9603 tpos = curwin->w_cursor;
9604 if (gui_do_scroll())
9605 {
9606 start_arrow(&tpos);
9607# ifdef FEAT_CINDENT
9608 can_cindent = TRUE;
9609# endif
9610 }
9611}
9612
9613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009614ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615{
9616 pos_T tpos;
9617
9618 undisplay_dollar();
9619 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009620 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 {
9622 start_arrow(&tpos);
9623# ifdef FEAT_CINDENT
9624 can_cindent = TRUE;
9625# endif
9626 }
9627}
9628#endif
9629
9630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009631ins_left(
9632 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
9634 pos_T tpos;
9635
9636#ifdef FEAT_FOLDING
9637 if ((fdo_flags & FDO_HOR) && KeyTyped)
9638 foldOpenCursor();
9639#endif
9640 undisplay_dollar();
9641 tpos = curwin->w_cursor;
9642 if (oneleft() == OK)
9643 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009644#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9645 /* Only call start_arrow() when not busy with preediting, it will
9646 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9647 if (!im_is_preediting())
9648#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009649 {
9650 start_arrow_with_change(&tpos, end_change);
9651 if (!end_change)
9652 AppendCharToRedobuff(K_LEFT);
9653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654#ifdef FEAT_RIGHTLEFT
9655 /* If exit reversed string, position is fixed */
9656 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9657 revins_legal++;
9658 revins_chars++;
9659#endif
9660 }
9661
9662 /*
9663 * if 'whichwrap' set for cursor in insert mode may go to
9664 * previous line
9665 */
9666 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9667 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009668 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 start_arrow(&tpos);
9670 --(curwin->w_cursor.lnum);
9671 coladvance((colnr_T)MAXCOL);
9672 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9673 }
9674 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009675 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009676 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677}
9678
9679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009680ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681{
9682 pos_T tpos;
9683
9684#ifdef FEAT_FOLDING
9685 if ((fdo_flags & FDO_HOR) && KeyTyped)
9686 foldOpenCursor();
9687#endif
9688 undisplay_dollar();
9689 tpos = curwin->w_cursor;
9690 if (c == K_C_HOME)
9691 curwin->w_cursor.lnum = 1;
9692 curwin->w_cursor.col = 0;
9693#ifdef FEAT_VIRTUALEDIT
9694 curwin->w_cursor.coladd = 0;
9695#endif
9696 curwin->w_curswant = 0;
9697 start_arrow(&tpos);
9698}
9699
9700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009701ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
9703 pos_T tpos;
9704
9705#ifdef FEAT_FOLDING
9706 if ((fdo_flags & FDO_HOR) && KeyTyped)
9707 foldOpenCursor();
9708#endif
9709 undisplay_dollar();
9710 tpos = curwin->w_cursor;
9711 if (c == K_C_END)
9712 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9713 coladvance((colnr_T)MAXCOL);
9714 curwin->w_curswant = MAXCOL;
9715
9716 start_arrow(&tpos);
9717}
9718
9719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009720ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
9722#ifdef FEAT_FOLDING
9723 if ((fdo_flags & FDO_HOR) && KeyTyped)
9724 foldOpenCursor();
9725#endif
9726 undisplay_dollar();
9727 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9728 {
9729 start_arrow(&curwin->w_cursor);
9730 (void)bck_word(1L, FALSE, FALSE);
9731 curwin->w_set_curswant = TRUE;
9732 }
9733 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009734 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735}
9736
9737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009738ins_right(
9739 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
9741#ifdef FEAT_FOLDING
9742 if ((fdo_flags & FDO_HOR) && KeyTyped)
9743 foldOpenCursor();
9744#endif
9745 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009746 if (gchar_cursor() != NUL
9747#ifdef FEAT_VIRTUALEDIT
9748 || virtual_active()
9749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 )
9751 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009752 start_arrow_with_change(&curwin->w_cursor, end_change);
9753 if (!end_change)
9754 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 curwin->w_set_curswant = TRUE;
9756#ifdef FEAT_VIRTUALEDIT
9757 if (virtual_active())
9758 oneright();
9759 else
9760#endif
9761 {
9762#ifdef FEAT_MBYTE
9763 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009764 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 else
9766#endif
9767 ++curwin->w_cursor.col;
9768 }
9769
9770#ifdef FEAT_RIGHTLEFT
9771 revins_legal++;
9772 if (revins_chars)
9773 revins_chars--;
9774#endif
9775 }
9776 /* if 'whichwrap' set for cursor in insert mode, may move the
9777 * cursor to the next line */
9778 else if (vim_strchr(p_ww, ']') != NULL
9779 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9780 {
9781 start_arrow(&curwin->w_cursor);
9782 curwin->w_set_curswant = TRUE;
9783 ++curwin->w_cursor.lnum;
9784 curwin->w_cursor.col = 0;
9785 }
9786 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009787 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009788 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789}
9790
9791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009792ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793{
9794#ifdef FEAT_FOLDING
9795 if ((fdo_flags & FDO_HOR) && KeyTyped)
9796 foldOpenCursor();
9797#endif
9798 undisplay_dollar();
9799 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9800 || gchar_cursor() != NUL)
9801 {
9802 start_arrow(&curwin->w_cursor);
9803 (void)fwd_word(1L, FALSE, 0);
9804 curwin->w_set_curswant = TRUE;
9805 }
9806 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009807 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808}
9809
9810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009811ins_up(
9812 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813{
9814 pos_T tpos;
9815 linenr_T old_topline = curwin->w_topline;
9816#ifdef FEAT_DIFF
9817 int old_topfill = curwin->w_topfill;
9818#endif
9819
9820 undisplay_dollar();
9821 tpos = curwin->w_cursor;
9822 if (cursor_up(1L, TRUE) == OK)
9823 {
9824 if (startcol)
9825 coladvance(getvcol_nolist(&Insstart));
9826 if (old_topline != curwin->w_topline
9827#ifdef FEAT_DIFF
9828 || old_topfill != curwin->w_topfill
9829#endif
9830 )
9831 redraw_later(VALID);
9832 start_arrow(&tpos);
9833#ifdef FEAT_CINDENT
9834 can_cindent = TRUE;
9835#endif
9836 }
9837 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009838 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839}
9840
9841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009842ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843{
9844 pos_T tpos;
9845
9846 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009847
9848#ifdef FEAT_WINDOWS
9849 if (mod_mask & MOD_MASK_CTRL)
9850 {
9851 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009852 if (first_tabpage->tp_next != NULL)
9853 {
9854 start_arrow(&curwin->w_cursor);
9855 goto_tabpage(-1);
9856 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009857 return;
9858 }
9859#endif
9860
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 tpos = curwin->w_cursor;
9862 if (onepage(BACKWARD, 1L) == OK)
9863 {
9864 start_arrow(&tpos);
9865#ifdef FEAT_CINDENT
9866 can_cindent = TRUE;
9867#endif
9868 }
9869 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009870 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871}
9872
9873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009874ins_down(
9875 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876{
9877 pos_T tpos;
9878 linenr_T old_topline = curwin->w_topline;
9879#ifdef FEAT_DIFF
9880 int old_topfill = curwin->w_topfill;
9881#endif
9882
9883 undisplay_dollar();
9884 tpos = curwin->w_cursor;
9885 if (cursor_down(1L, TRUE) == OK)
9886 {
9887 if (startcol)
9888 coladvance(getvcol_nolist(&Insstart));
9889 if (old_topline != curwin->w_topline
9890#ifdef FEAT_DIFF
9891 || old_topfill != curwin->w_topfill
9892#endif
9893 )
9894 redraw_later(VALID);
9895 start_arrow(&tpos);
9896#ifdef FEAT_CINDENT
9897 can_cindent = TRUE;
9898#endif
9899 }
9900 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009901 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902}
9903
9904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009905ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906{
9907 pos_T tpos;
9908
9909 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009910
9911#ifdef FEAT_WINDOWS
9912 if (mod_mask & MOD_MASK_CTRL)
9913 {
9914 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009915 if (first_tabpage->tp_next != NULL)
9916 {
9917 start_arrow(&curwin->w_cursor);
9918 goto_tabpage(0);
9919 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009920 return;
9921 }
9922#endif
9923
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 tpos = curwin->w_cursor;
9925 if (onepage(FORWARD, 1L) == OK)
9926 {
9927 start_arrow(&tpos);
9928#ifdef FEAT_CINDENT
9929 can_cindent = TRUE;
9930#endif
9931 }
9932 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009933 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934}
9935
9936#ifdef FEAT_DND
9937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009938ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939{
9940 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9941}
9942#endif
9943
9944/*
9945 * Handle TAB in Insert or Replace mode.
9946 * Return TRUE when the TAB needs to be inserted like a normal character.
9947 */
9948 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009949ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950{
9951 int ind;
9952 int i;
9953 int temp;
9954
9955 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9956 Insstart_blank_vcol = get_nolist_virtcol();
9957 if (echeck_abbr(TAB + ABBR_OFF))
9958 return FALSE;
9959
9960 ind = inindent(0);
9961#ifdef FEAT_CINDENT
9962 if (ind)
9963 can_cindent = FALSE;
9964#endif
9965
9966 /*
9967 * When nothing special, insert TAB like a normal character
9968 */
9969 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009970 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009971 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 return TRUE;
9973
9974 if (stop_arrow() == FAIL)
9975 return TRUE;
9976
9977 did_ai = FALSE;
9978#ifdef FEAT_SMARTINDENT
9979 did_si = FALSE;
9980 can_si = FALSE;
9981 can_si_back = FALSE;
9982#endif
9983 AppendToRedobuff((char_u *)"\t");
9984
9985 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009986 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009987 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9988 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 else /* otherwise use 'tabstop' */
9990 temp = (int)curbuf->b_p_ts;
9991 temp -= get_nolist_virtcol() % temp;
9992
9993 /*
9994 * Insert the first space with ins_char(). It will delete one char in
9995 * replace mode. Insert the rest with ins_str(); it will not delete any
9996 * chars. For VREPLACE mode, we use ins_char() for all characters.
9997 */
9998 ins_char(' ');
9999 while (--temp > 0)
10000 {
10001#ifdef FEAT_VREPLACE
10002 if (State & VREPLACE_FLAG)
10003 ins_char(' ');
10004 else
10005#endif
10006 {
10007 ins_str((char_u *)" ");
10008 if (State & REPLACE_FLAG) /* no char replaced */
10009 replace_push(NUL);
10010 }
10011 }
10012
10013 /*
10014 * When 'expandtab' not set: Replace spaces by TABs where possible.
10015 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010016 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 {
10018 char_u *ptr;
10019#ifdef FEAT_VREPLACE
10020 char_u *saved_line = NULL; /* init for GCC */
10021 pos_T pos;
10022#endif
10023 pos_T fpos;
10024 pos_T *cursor;
10025 colnr_T want_vcol, vcol;
10026 int change_col = -1;
10027 int save_list = curwin->w_p_list;
10028
10029 /*
10030 * Get the current line. For VREPLACE mode, don't make real changes
10031 * yet, just work on a copy of the line.
10032 */
10033#ifdef FEAT_VREPLACE
10034 if (State & VREPLACE_FLAG)
10035 {
10036 pos = curwin->w_cursor;
10037 cursor = &pos;
10038 saved_line = vim_strsave(ml_get_curline());
10039 if (saved_line == NULL)
10040 return FALSE;
10041 ptr = saved_line + pos.col;
10042 }
10043 else
10044#endif
10045 {
10046 ptr = ml_get_cursor();
10047 cursor = &curwin->w_cursor;
10048 }
10049
10050 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10051 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10052 curwin->w_p_list = FALSE;
10053
10054 /* Find first white before the cursor */
10055 fpos = curwin->w_cursor;
10056 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
10057 {
10058 --fpos.col;
10059 --ptr;
10060 }
10061
10062 /* In Replace mode, don't change characters before the insert point. */
10063 if ((State & REPLACE_FLAG)
10064 && fpos.lnum == Insstart.lnum
10065 && fpos.col < Insstart.col)
10066 {
10067 ptr += Insstart.col - fpos.col;
10068 fpos.col = Insstart.col;
10069 }
10070
10071 /* compute virtual column numbers of first white and cursor */
10072 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10073 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10074
Bram Moolenaar597a4222014-06-25 14:39:50 +020010075 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10076 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 while (vim_iswhite(*ptr))
10078 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010079 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 if (vcol + i > want_vcol)
10081 break;
10082 if (*ptr != TAB)
10083 {
10084 *ptr = TAB;
10085 if (change_col < 0)
10086 {
10087 change_col = fpos.col; /* Column of first change */
10088 /* May have to adjust Insstart */
10089 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10090 Insstart.col = fpos.col;
10091 }
10092 }
10093 ++fpos.col;
10094 ++ptr;
10095 vcol += i;
10096 }
10097
10098 if (change_col >= 0)
10099 {
10100 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010101 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102
10103 /* Skip over the spaces we need. */
10104 while (vcol < want_vcol && *ptr == ' ')
10105 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010106 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 ++ptr;
10108 ++repl_off;
10109 }
10110 if (vcol > want_vcol)
10111 {
10112 /* Must have a char with 'showbreak' just before it. */
10113 --ptr;
10114 --repl_off;
10115 }
10116 fpos.col += repl_off;
10117
10118 /* Delete following spaces. */
10119 i = cursor->col - fpos.col;
10120 if (i > 0)
10121 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010122 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 /* correct replace stack. */
10124 if ((State & REPLACE_FLAG)
10125#ifdef FEAT_VREPLACE
10126 && !(State & VREPLACE_FLAG)
10127#endif
10128 )
10129 for (temp = i; --temp >= 0; )
10130 replace_join(repl_off);
10131 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010132#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010133 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010134 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010135 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010136 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10137 (char_u *)"\t", 1);
10138 }
10139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 cursor->col -= i;
10141
10142#ifdef FEAT_VREPLACE
10143 /*
10144 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10145 * backspacing over the changed spacing and then inserting the new
10146 * spacing.
10147 */
10148 if (State & VREPLACE_FLAG)
10149 {
10150 /* Backspace from real cursor to change_col */
10151 backspace_until_column(change_col);
10152
10153 /* Insert each char in saved_line from changed_col to
10154 * ptr-cursor */
10155 ins_bytes_len(saved_line + change_col,
10156 cursor->col - change_col);
10157 }
10158#endif
10159 }
10160
10161#ifdef FEAT_VREPLACE
10162 if (State & VREPLACE_FLAG)
10163 vim_free(saved_line);
10164#endif
10165 curwin->w_p_list = save_list;
10166 }
10167
10168 return FALSE;
10169}
10170
10171/*
10172 * Handle CR or NL in insert mode.
10173 * Return TRUE when out of memory or can't undo.
10174 */
10175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010176ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177{
10178 int i;
10179
10180 if (echeck_abbr(c + ABBR_OFF))
10181 return FALSE;
10182 if (stop_arrow() == FAIL)
10183 return TRUE;
10184 undisplay_dollar();
10185
10186 /*
10187 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10188 * character under the cursor. Only push a NUL on the replace stack,
10189 * nothing to put back when the NL is deleted.
10190 */
10191 if ((State & REPLACE_FLAG)
10192#ifdef FEAT_VREPLACE
10193 && !(State & VREPLACE_FLAG)
10194#endif
10195 )
10196 replace_push(NUL);
10197
10198 /*
10199 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10200 * replacing the next line, so we push all of the characters left on the
10201 * line onto the replace stack. This is not done here though, it is done
10202 * in open_line().
10203 */
10204
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010205#ifdef FEAT_VIRTUALEDIT
10206 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10207 * CTRL-O). */
10208 if (virtual_active() && curwin->w_cursor.coladd > 0)
10209 coladvance(getviscol());
10210#endif
10211
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212#ifdef FEAT_RIGHTLEFT
10213# ifdef FEAT_FKMAP
10214 if (p_altkeymap && p_fkmap)
10215 fkmap(NL);
10216# endif
10217 /* NL in reverse insert will always start in the end of
10218 * current line. */
10219 if (revins_on)
10220 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10221#endif
10222
10223 AppendToRedobuff(NL_STR);
10224 i = open_line(FORWARD,
10225#ifdef FEAT_COMMENTS
10226 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10227#endif
10228 0, old_indent);
10229 old_indent = 0;
10230#ifdef FEAT_CINDENT
10231 can_cindent = TRUE;
10232#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010233#ifdef FEAT_FOLDING
10234 /* When inserting a line the cursor line must never be in a closed fold. */
10235 foldOpenCursor();
10236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237
10238 return (!i);
10239}
10240
10241#ifdef FEAT_DIGRAPHS
10242/*
10243 * Handle digraph in insert mode.
10244 * Returns character still to be inserted, or NUL when nothing remaining to be
10245 * done.
10246 */
10247 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010248ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249{
10250 int c;
10251 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010252 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253
10254 pc_status = PC_STATUS_UNSET;
10255 if (redrawing() && !char_avail())
10256 {
10257 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010258 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259
10260 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010261 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262#ifdef FEAT_CMDL_INFO
10263 add_to_showcmd_c(Ctrl_K);
10264#endif
10265 }
10266
10267#ifdef USE_ON_FLY_SCROLL
10268 dont_scroll = TRUE; /* disallow scrolling here */
10269#endif
10270
10271 /* don't map the digraph chars. This also prevents the
10272 * mode message to be deleted when ESC is hit */
10273 ++no_mapping;
10274 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010275 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 --no_mapping;
10277 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010278 if (did_putchar)
10279 /* when the line fits in 'columns' the '?' is at the start of the next
10280 * line and will not be removed by the redraw */
10281 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010282
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 if (IS_SPECIAL(c) || mod_mask) /* special key */
10284 {
10285#ifdef FEAT_CMDL_INFO
10286 clear_showcmd();
10287#endif
10288 insert_special(c, TRUE, FALSE);
10289 return NUL;
10290 }
10291 if (c != ESC)
10292 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010293 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 if (redrawing() && !char_avail())
10295 {
10296 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010297 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298
10299 if (char2cells(c) == 1)
10300 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010301 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010303 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 }
10305#ifdef FEAT_CMDL_INFO
10306 add_to_showcmd_c(c);
10307#endif
10308 }
10309 ++no_mapping;
10310 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010311 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 --no_mapping;
10313 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010314 if (did_putchar)
10315 /* when the line fits in 'columns' the '?' is at the start of the
10316 * next line and will not be removed by a redraw */
10317 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 if (cc != ESC)
10319 {
10320 AppendToRedobuff((char_u *)CTRL_V_STR);
10321 c = getdigraph(c, cc, TRUE);
10322#ifdef FEAT_CMDL_INFO
10323 clear_showcmd();
10324#endif
10325 return c;
10326 }
10327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328#ifdef FEAT_CMDL_INFO
10329 clear_showcmd();
10330#endif
10331 return NUL;
10332}
10333#endif
10334
10335/*
10336 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10337 * Returns the char to be inserted, or NUL if none found.
10338 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010340ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341{
10342 int c;
10343 int temp;
10344 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010345 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346
10347 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10348 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010349 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 return NUL;
10351 }
10352
10353 /* try to advance to the cursor column */
10354 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010355 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 prev_ptr = ptr;
10357 validate_virtcol();
10358 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10359 {
10360 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010361 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 }
10363 if ((colnr_T)temp > curwin->w_virtcol)
10364 ptr = prev_ptr;
10365
10366#ifdef FEAT_MBYTE
10367 c = (*mb_ptr2char)(ptr);
10368#else
10369 c = *ptr;
10370#endif
10371 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010372 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 return c;
10374}
10375
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010376/*
10377 * CTRL-Y or CTRL-E typed in Insert mode.
10378 */
10379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010380ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010381{
10382 int c = tc;
10383
10384#ifdef FEAT_INS_EXPAND
10385 if (ctrl_x_mode == CTRL_X_SCROLL)
10386 {
10387 if (c == Ctrl_Y)
10388 scrolldown_clamp();
10389 else
10390 scrollup_clamp();
10391 redraw_later(VALID);
10392 }
10393 else
10394#endif
10395 {
10396 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10397 if (c != NUL)
10398 {
10399 long tw_save;
10400
10401 /* The character must be taken literally, insert like it
10402 * was typed after a CTRL-V, and pretend 'textwidth'
10403 * wasn't set. Digits, 'o' and 'x' are special after a
10404 * CTRL-V, don't use it for these. */
10405 if (c < 256 && !isalnum(c))
10406 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10407 tw_save = curbuf->b_p_tw;
10408 curbuf->b_p_tw = -1;
10409 insert_special(c, TRUE, FALSE);
10410 curbuf->b_p_tw = tw_save;
10411#ifdef FEAT_RIGHTLEFT
10412 revins_chars++;
10413 revins_legal++;
10414#endif
10415 c = Ctrl_V; /* pretend CTRL-V is last character */
10416 auto_format(FALSE, TRUE);
10417 }
10418 }
10419 return c;
10420}
10421
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422#ifdef FEAT_SMARTINDENT
10423/*
10424 * Try to do some very smart auto-indenting.
10425 * Used when inserting a "normal" character.
10426 */
10427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010428ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429{
10430 pos_T *pos, old_pos;
10431 char_u *ptr;
10432 int i;
10433 int temp;
10434
10435 /*
10436 * do some very smart indenting when entering '{' or '}'
10437 */
10438 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10439 {
10440 /*
10441 * for '}' set indent equal to indent of line containing matching '{'
10442 */
10443 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10444 {
10445 old_pos = curwin->w_cursor;
10446 /*
10447 * If the matching '{' has a ')' immediately before it (ignoring
10448 * white-space), then line up with the start of the line
10449 * containing the matching '(' if there is one. This handles the
10450 * case where an "if (..\n..) {" statement continues over multiple
10451 * lines -- webb
10452 */
10453 ptr = ml_get(pos->lnum);
10454 i = pos->col;
10455 if (i > 0) /* skip blanks before '{' */
10456 while (--i > 0 && vim_iswhite(ptr[i]))
10457 ;
10458 curwin->w_cursor.lnum = pos->lnum;
10459 curwin->w_cursor.col = i;
10460 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10461 curwin->w_cursor = *pos;
10462 i = get_indent();
10463 curwin->w_cursor = old_pos;
10464#ifdef FEAT_VREPLACE
10465 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010466 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 else
10468#endif
10469 (void)set_indent(i, SIN_CHANGED);
10470 }
10471 else if (curwin->w_cursor.col > 0)
10472 {
10473 /*
10474 * when inserting '{' after "O" reduce indent, but not
10475 * more than indent of previous line
10476 */
10477 temp = TRUE;
10478 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10479 {
10480 old_pos = curwin->w_cursor;
10481 i = get_indent();
10482 while (curwin->w_cursor.lnum > 1)
10483 {
10484 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10485
10486 /* ignore empty lines and lines starting with '#'. */
10487 if (*ptr != '#' && *ptr != NUL)
10488 break;
10489 }
10490 if (get_indent() >= i)
10491 temp = FALSE;
10492 curwin->w_cursor = old_pos;
10493 }
10494 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010495 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 }
10497 }
10498
10499 /*
10500 * set indent of '#' always to 0
10501 */
10502 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10503 {
10504 /* remember current indent for next line */
10505 old_indent = get_indent();
10506 (void)set_indent(0, SIN_CHANGED);
10507 }
10508
10509 /* Adjust ai_col, the char at this position can be deleted. */
10510 if (ai_col > curwin->w_cursor.col)
10511 ai_col = curwin->w_cursor.col;
10512}
10513#endif
10514
10515/*
10516 * Get the value that w_virtcol would have when 'list' is off.
10517 * Unless 'cpo' contains the 'L' flag.
10518 */
10519 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010520get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521{
10522 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10523 return getvcol_nolist(&curwin->w_cursor);
10524 validate_virtcol();
10525 return curwin->w_virtcol;
10526}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010527
10528#ifdef FEAT_AUTOCMD
10529/*
10530 * Handle the InsertCharPre autocommand.
10531 * "c" is the character that was typed.
10532 * Return a pointer to allocated memory with the replacement string.
10533 * Return NULL to continue inserting "c".
10534 */
10535 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010536do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010537{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010538 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010539 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010540
10541 /* Return quickly when there is nothing to do. */
10542 if (!has_insertcharpre())
10543 return NULL;
10544
Bram Moolenaar704984a2012-06-01 14:57:51 +020010545#ifdef FEAT_MBYTE
10546 if (has_mbyte)
10547 buf[(*mb_char2bytes)(c, buf)] = NUL;
10548 else
10549#endif
10550 {
10551 buf[0] = c;
10552 buf[1] = NUL;
10553 }
10554
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010555 /* Lock the text to avoid weird things from happening. */
10556 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010557 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010558
Bram Moolenaar704984a2012-06-01 14:57:51 +020010559 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010560 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010561 {
10562 /* Get the value of v:char. It may be empty or more than one
10563 * character. Only use it when changed, otherwise continue with the
10564 * original character to avoid breaking autoindent. */
10565 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10566 res = vim_strsave(get_vim_var_str(VV_CHAR));
10567 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010568
10569 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10570 --textlock;
10571
10572 return res;
10573}
10574#endif