blob: 6fc1e35ac8f27ab3868b3d227a75004da661107f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaare4214502015-03-05 18:08:43 +010037#define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
Bram Moolenaare4214502015-03-05 18:08:43 +010040#define CTRL_X_MODE_LINE_OR_EVAL(m) (m == CTRL_X_WHOLE_LINE || m == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42static char *ctrl_x_msgs[] =
43{
44 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000046 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 N_(" Whole line completion (^L^N^P)"),
48 N_(" File name completion (^F^N^P)"),
49 N_(" Tag completion (^]^N^P)"),
50 N_(" Path pattern completion (^N^P)"),
51 N_(" Definition completion (^D^N^P)"),
52 NULL,
53 N_(" Dictionary completion (^K^N^P)"),
54 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000055 N_(" Command-line completion (^V^N^P)"),
56 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000057 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000058 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000059 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010060 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061};
62
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000063static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010064#ifdef FEAT_COMPL_FUNC
65static char e_complwin[] = N_("E839: Completion function changed window");
66static char e_compldel[] = N_("E840: Completion function deleted text");
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
69/*
70 * Structure used to store one match for insert completion.
71 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000072typedef struct compl_S compl_T;
73struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000074{
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 compl_T *cp_next;
76 compl_T *cp_prev;
77 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000078 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000079 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000080 char_u *cp_fname; /* file containing the match, allocated when
81 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000082 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
83 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084};
85
Bram Moolenaar572cb562005-08-05 21:35:02 +000086#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#define FREE_FNAME (2)
88
89/*
90 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000091 * "compl_first_match" points to the start of the list.
92 * "compl_curr_match" points to the currently selected entry.
93 * "compl_shown_match" is different from compl_curr_match during
94 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000096static compl_T *compl_first_match = NULL;
97static compl_T *compl_curr_match = NULL;
98static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000100/* After using a cursor key <Enter> selects a match in the popup menu,
101 * otherwise it inserts a line break. */
102static int compl_enter_selects = FALSE;
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104/* When "compl_leader" is not NULL only matches that start with this string
105 * are used. */
106static char_u *compl_leader = NULL;
107
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000108static int compl_get_longest = FALSE; /* put longest common string
109 in compl_leader */
110
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200111static int compl_no_insert = FALSE; /* FALSE: select & insert
112 TRUE: noinsert */
113static int compl_no_select = FALSE; /* FALSE: select & insert
114 TRUE: noselect */
115
Bram Moolenaara6557602006-02-04 22:43:20 +0000116static int compl_used_match; /* Selected one of the matches. When
117 FALSE the match was edited or using
118 the longest common string. */
119
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000120static int compl_was_interrupted = FALSE; /* didn't finish finding
121 completions. */
122
123static int compl_restarting = FALSE; /* don't insert match */
124
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000125/* When the first completion is done "compl_started" is set. When it's
126 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000127static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000129/* Set when doing something for completion that may call edit() recursively,
130 * which is not allowed. */
131static int compl_busy = FALSE;
132
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static int compl_matches = 0;
134static char_u *compl_pattern = NULL;
135static int compl_direction = FORWARD;
136static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000137static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000138static pos_T compl_startpos;
139static colnr_T compl_col = 0; /* column where the text starts
140 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000141static char_u *compl_orig_text = NULL; /* text as it was before
142 * completion started */
143static int compl_cont_mode = 0;
144static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar82139082011-09-14 16:52:09 +0200146static int compl_opt_refresh_always = FALSE;
147
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100148static void ins_ctrl_x(void);
149static int has_compl_option(int dict_opt);
150static int ins_compl_accept_char(int c);
151static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
152static int ins_compl_equal(compl_T *match, char_u *str, int len);
153static void ins_compl_longest_match(compl_T *match);
154static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
155static int ins_compl_make_cyclic(void);
156static void ins_compl_upd_pum(void);
157static void ins_compl_del_pum(void);
158static int pum_wanted(void);
159static int pum_enough_matches(void);
160static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
161static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
162static char_u *find_line_end(char_u *ptr);
163static void ins_compl_free(void);
164static void ins_compl_clear(void);
165static int ins_compl_bs(void);
166static int ins_compl_need_restart(void);
167static void ins_compl_new_leader(void);
168static void ins_compl_addleader(int c);
169static int ins_compl_len(void);
170static void ins_compl_restart(void);
171static void ins_compl_set_original_text(char_u *str);
172static void ins_compl_addfrommatch(void);
173static int ins_compl_prep(int c);
174static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
175static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000176#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100177static void ins_compl_add_list(list_T *list);
178static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000179#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static int ins_compl_get_exp(pos_T *ini);
181static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200182static void ins_compl_insert(int in_compl_func);
183static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100184static int ins_compl_key2dir(int c);
185static int ins_compl_pum_key(int c);
186static int ins_compl_key2count(int c);
187static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100188static int ins_complete(int c, int enable_pum);
189static void show_pum(int save_w_wrow);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#endif /* FEAT_INS_EXPAND */
192
193#define BACKSPACE_CHAR 1
194#define BACKSPACE_WORD 2
195#define BACKSPACE_WORD_NOT_SPACE 3
196#define BACKSPACE_LINE 4
197
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100198static void ins_redraw(int ready);
199static void ins_ctrl_v(void);
200static void undisplay_dollar(void);
201static void insert_special(int, int, int);
202static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
203static void check_auto_format(int);
204static void redo_literal(int c);
205static void start_arrow(pos_T *end_insert_pos);
206static void start_arrow_with_change(pos_T *end_insert_pos, int change);
207static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000208#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void check_spell_redraw(void);
210static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000211static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
214static int echeck_abbr(int);
215static int replace_pop(void);
216static void replace_join(int off);
217static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void replace_flush(void);
222static void replace_do_bs(int limit_col);
223static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ins_reg(void);
228static void ins_ctrl_g(void);
229static void ins_ctrl_hat(void);
230static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100234static int ins_start_select(int c);
235static void ins_insert(int replaceState);
236static void ins_ctrl_o(void);
237static void ins_shift(int c, int lastc);
238static void ins_del(void);
239static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_mouse(int c);
242static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000244#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000246#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ins_left(int end_change);
248static void ins_home(int c);
249static void ins_end(int c);
250static void ins_s_left(void);
251static void ins_right(int end_change);
252static void ins_s_right(void);
253static void ins_up(int startcol);
254static void ins_pageup(void);
255static void ins_down(int startcol);
256static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static int ins_tab(void);
261static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100270#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274static colnr_T Insstart_textlen; /* length of line when insert started */
275static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100276static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
278static char_u *last_insert = NULL; /* the text of the previous insert,
279 K_SPECIAL and CSI are escaped */
280static int last_insert_skip; /* nr of chars in front of previous insert */
281static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000282static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284#ifdef FEAT_CINDENT
285static int can_cindent; /* may do cindenting on this line */
286#endif
287
288static int old_indent = 0; /* for ^^D command in insert mode */
289
290#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000291static int revins_on; /* reverse insert mode on */
292static int revins_chars; /* how much to skip after edit */
293static int revins_legal; /* was the last char 'legal'? */
294static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static int ins_need_undo; /* call u_save() before inserting a
298 char. Set when edit() is called.
299 after that arrow_used is used. */
300
301static int did_add_space = FALSE; /* auto_format() added an extra space
302 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200303static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
304 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
306/*
307 * edit(): Start inserting text.
308 *
309 * "cmdchar" can be:
310 * 'i' normal insert command
311 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100312 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 * 'R' replace command
314 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
315 * but still only one <CR> is inserted. The <Esc> is not used for redo.
316 * 'g' "gI" command.
317 * 'V' "gR" command for Virtual Replace mode.
318 * 'v' "gr" command for single character Virtual Replace mode.
319 *
320 * This function is not called recursively. For CTRL-O commands, it returns
321 * and lets the caller handle the Normal-mode command.
322 *
323 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
324 */
325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100326edit(
327 int cmdchar,
328 int startln, /* if set, insert at start of line */
329 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330{
331 int c = 0;
332 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100333 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000334 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 int i;
337 int did_backspace = TRUE; /* previous char was backspace */
338#ifdef FEAT_CINDENT
339 int line_is_white = FALSE; /* line is empty before insert */
340#endif
341 linenr_T old_topline = 0; /* topline before insertion */
342#ifdef FEAT_DIFF
343 int old_topfill = -1;
344#endif
345 int inserted_space = FALSE; /* just inserted a space */
346 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000347 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000349 /* Remember whether editing was restarted after CTRL-O. */
350 did_restart_edit = restart_edit;
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 /* sleep before redrawing, needed for "CTRL-O :" that results in an
353 * error message */
354 check_for_delay(TRUE);
355
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100356 /* set Insstart_orig to Insstart */
357 update_Insstart_orig = TRUE;
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef HAVE_SANDBOX
360 /* Don't allow inserting in the sandbox. */
361 if (sandbox != 0)
362 {
363 EMSG(_(e_sandbox));
364 return FALSE;
365 }
366#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000367 /* Don't allow changes in the buffer while editing the cmdline. The
368 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000369 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000370 {
371 EMSG(_(e_secure));
372 return FALSE;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000376 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000377 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000378 {
379 EMSG(_(e_secure));
380 return FALSE;
381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 ins_compl_clear(); /* clear stuff for CTRL-X mode */
383#endif
384
Bram Moolenaar843ee412004-06-30 16:16:41 +0000385#ifdef FEAT_AUTOCMD
386 /*
387 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
388 */
389 if (cmdchar != 'r' && cmdchar != 'v')
390 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100391 pos_T save_cursor = curwin->w_cursor;
392
Bram Moolenaar1e015462005-09-25 22:16:38 +0000393# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000394 if (cmdchar == 'R')
395 ptr = (char_u *)"r";
396 else if (cmdchar == 'V')
397 ptr = (char_u *)"v";
398 else
399 ptr = (char_u *)"i";
400 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200401 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000402# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000403 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100404
Bram Moolenaar097c9922013-05-19 21:15:15 +0200405 /* Make sure the cursor didn't move. Do call check_cursor_col() in
406 * case the text was modified. Since Insert mode was not started yet
407 * a call to check_cursor_col() may move the cursor, especially with
408 * the "A" command, thus set State to avoid that. Also check that the
409 * line number is still valid (lines may have been deleted).
410 * Do not restore if v:char was set to a non-empty string. */
411 if (!equalpos(curwin->w_cursor, save_cursor)
412# ifdef FEAT_EVAL
413 && *get_vim_var_str(VV_CHAR) == NUL
414# endif
415 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100416 {
417 int save_state = State;
418
419 curwin->w_cursor = save_cursor;
420 State = INSERT;
421 check_cursor_col();
422 State = save_state;
423 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000424 }
425#endif
426
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200427#ifdef FEAT_CONCEAL
428 /* Check if the cursor line needs redrawing before changing State. If
429 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200430 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200431#endif
432
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433#ifdef FEAT_MOUSE
434 /*
435 * When doing a paste with the middle mouse button, Insstart is set to
436 * where the paste started.
437 */
438 if (where_paste_started.lnum != 0)
439 Insstart = where_paste_started;
440 else
441#endif
442 {
443 Insstart = curwin->w_cursor;
444 if (startln)
445 Insstart.col = 0;
446 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000447 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 Insstart_blank_vcol = MAXCOL;
449 if (!did_ai)
450 ai_col = 0;
451
452 if (cmdchar != NUL && restart_edit == 0)
453 {
454 ResetRedobuff();
455 AppendNumberToRedobuff(count);
456#ifdef FEAT_VREPLACE
457 if (cmdchar == 'V' || cmdchar == 'v')
458 {
459 /* "gR" or "gr" command */
460 AppendCharToRedobuff('g');
461 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
462 }
463 else
464#endif
465 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100466 if (cmdchar == K_PS)
467 AppendCharToRedobuff('a');
468 else
469 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 if (cmdchar == 'g') /* "gI" command */
471 AppendCharToRedobuff('I');
472 else if (cmdchar == 'r') /* "r<CR>" command */
473 count = 1; /* insert only one <CR> */
474 }
475 }
476
477 if (cmdchar == 'R')
478 {
479#ifdef FEAT_FKMAP
480 if (p_fkmap && p_ri)
481 {
482 beep_flush();
483 EMSG(farsi_text_3); /* encoded in Farsi */
484 State = INSERT;
485 }
486 else
487#endif
488 State = REPLACE;
489 }
490#ifdef FEAT_VREPLACE
491 else if (cmdchar == 'V' || cmdchar == 'v')
492 {
493 State = VREPLACE;
494 replaceState = VREPLACE;
495 orig_line_count = curbuf->b_ml.ml_line_count;
496 vr_lines_changed = 1;
497 }
498#endif
499 else
500 State = INSERT;
501
502 stop_insert_mode = FALSE;
503
504 /*
505 * Need to recompute the cursor position, it might move when the cursor is
506 * on a TAB or special character.
507 */
508 curs_columns(TRUE);
509
510 /*
511 * Enable langmap or IME, indicated by 'iminsert'.
512 * Note that IME may enabled/disabled without us noticing here, thus the
513 * 'iminsert' value may not reflect what is actually used. It is updated
514 * when hitting <Esc>.
515 */
516 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
517 State |= LANGMAP;
518#ifdef USE_IM_CONTROL
519 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
520#endif
521
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522#ifdef FEAT_MOUSE
523 setmouse();
524#endif
525#ifdef FEAT_CMDL_INFO
526 clear_showcmd();
527#endif
528#ifdef FEAT_RIGHTLEFT
529 /* there is no reverse replace mode */
530 revins_on = (State == INSERT && p_ri);
531 if (revins_on)
532 undisplay_dollar();
533 revins_chars = 0;
534 revins_legal = 0;
535 revins_scol = -1;
536#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100537 if (!p_ek)
538 /* Disable bracketed paste mode, we won't recognize the escape
539 * sequences. */
540 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542 /*
543 * Handle restarting Insert mode.
544 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
545 * restart_edit non-zero, and something in the stuff buffer.
546 */
547 if (restart_edit != 0 && stuff_empty())
548 {
549#ifdef FEAT_MOUSE
550 /*
551 * After a paste we consider text typed to be part of the insert for
552 * the pasted text. You can backspace over the pasted text too.
553 */
554 if (where_paste_started.lnum)
555 arrow_used = FALSE;
556 else
557#endif
558 arrow_used = TRUE;
559 restart_edit = 0;
560
561 /*
562 * If the cursor was after the end-of-line before the CTRL-O and it is
563 * now at the end-of-line, put it after the end-of-line (this is not
564 * correct in very rare cases).
565 * Also do this if curswant is greater than the current virtual
566 * column. Eg after "^O$" or "^O80|".
567 */
568 validate_virtcol();
569 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000570 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 || curwin->w_curswant > curwin->w_virtcol)
572 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
573 {
574 if (ptr[1] == NUL)
575 ++curwin->w_cursor.col;
576#ifdef FEAT_MBYTE
577 else if (has_mbyte)
578 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000579 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (ptr[i] == NUL)
581 curwin->w_cursor.col += i;
582 }
583#endif
584 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000585 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 else
588 arrow_used = FALSE;
589
590 /* we are in insert mode now, don't need to start it anymore */
591 need_start_insertmode = FALSE;
592
593 /* Need to save the line for undo before inserting the first char. */
594 ins_need_undo = TRUE;
595
596#ifdef FEAT_MOUSE
597 where_paste_started.lnum = 0;
598#endif
599#ifdef FEAT_CINDENT
600 can_cindent = TRUE;
601#endif
602#ifdef FEAT_FOLDING
603 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
604 * restarting. */
605 if (!p_im && did_restart_edit == 0)
606 foldOpenCursor();
607#endif
608
609 /*
610 * If 'showmode' is set, show the current (insert/replace/..) mode.
611 * A warning message for changing a readonly file is given here, before
612 * actually changing anything. It's put after the mode, if any.
613 */
614 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000615 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 i = showmode();
617
618 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000619 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621#ifdef CURSOR_SHAPE
622 ui_cursor_shape(); /* may show different cursor shape */
623#endif
624#ifdef FEAT_DIGRAPHS
625 do_digraph(-1); /* clear digraphs */
626#endif
627
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000628 /*
629 * Get the current length of the redo buffer, those characters have to be
630 * skipped if we want to get to the inserted characters.
631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 ptr = get_inserted();
633 if (ptr == NULL)
634 new_insert_skip = 0;
635 else
636 {
637 new_insert_skip = (int)STRLEN(ptr);
638 vim_free(ptr);
639 }
640
641 old_indent = 0;
642
643 /*
644 * Main loop in Insert mode: repeat until Insert mode is left.
645 */
646 for (;;)
647 {
648#ifdef FEAT_RIGHTLEFT
649 if (!revins_legal)
650 revins_scol = -1; /* reset on illegal motions */
651 else
652 revins_legal = 0;
653#endif
654 if (arrow_used) /* don't repeat insert when arrow key used */
655 count = 0;
656
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100657 if (update_Insstart_orig)
658 Insstart_orig = Insstart;
659
Bram Moolenaar00672e12016-06-26 18:38:13 +0200660 if (stop_insert_mode
661#ifdef FEAT_INS_EXPAND
662 && !pum_visible()
663#endif
664 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 {
666 /* ":stopinsert" used or 'insertmode' reset */
667 count = 0;
668 goto doESCkey;
669 }
670
671 /* set curwin->w_curswant for next K_DOWN or K_UP */
672 if (!arrow_used)
673 curwin->w_set_curswant = TRUE;
674
675 /* If there is no typeahead may check for timestamps (e.g., for when a
676 * menu invoked a shell command). */
677 if (stuff_empty())
678 {
679 did_check_timestamps = FALSE;
680 if (need_check_timestamps)
681 check_timestamps(FALSE);
682 }
683
684 /*
685 * When emsg() was called msg_scroll will have been set.
686 */
687 msg_scroll = FALSE;
688
689#ifdef FEAT_GUI
690 /* When 'mousefocus' is set a mouse movement may have taken us to
691 * another window. "need_mouse_correct" may then be set because of an
692 * autocommand. */
693 if (need_mouse_correct)
694 gui_mouse_correct();
695#endif
696
697#ifdef FEAT_FOLDING
698 /* Open fold at the cursor line, according to 'foldopen'. */
699 if (fdo_flags & FDO_INSERT)
700 foldOpenCursor();
701 /* Close folds where the cursor isn't, according to 'foldclose' */
702 if (!char_avail())
703 foldCheckClose();
704#endif
705
706 /*
707 * If we inserted a character at the last position of the last line in
708 * the window, scroll the window one line up. This avoids an extra
709 * redraw.
710 * This is detected when the cursor column is smaller after inserting
711 * something.
712 * Don't do this when the topline changed already, it has
713 * already been adjusted (by insertchar() calling open_line())).
714 */
715 if (curbuf->b_mod_set
716 && curwin->w_p_wrap
717 && !did_backspace
718 && curwin->w_topline == old_topline
719#ifdef FEAT_DIFF
720 && curwin->w_topfill == old_topfill
721#endif
722 )
723 {
724 mincol = curwin->w_wcol;
725 validate_cursor_col();
726
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000727 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 && curwin->w_wrow == W_WINROW(curwin)
729 + curwin->w_height - 1 - p_so
730 && (curwin->w_cursor.lnum != curwin->w_topline
731#ifdef FEAT_DIFF
732 || curwin->w_topfill > 0
733#endif
734 ))
735 {
736#ifdef FEAT_DIFF
737 if (curwin->w_topfill > 0)
738 --curwin->w_topfill;
739 else
740#endif
741#ifdef FEAT_FOLDING
742 if (hasFolding(curwin->w_topline, NULL, &old_topline))
743 set_topline(curwin, old_topline + 1);
744 else
745#endif
746 set_topline(curwin, curwin->w_topline + 1);
747 }
748 }
749
750 /* May need to adjust w_topline to show the cursor. */
751 update_topline();
752
753 did_backspace = FALSE;
754
755 validate_cursor(); /* may set must_redraw */
756
757 /*
758 * Redraw the display when no characters are waiting.
759 * Also shows mode, ruler and positions cursor.
760 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000761 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
763#ifdef FEAT_SCROLLBIND
764 if (curwin->w_p_scb)
765 do_check_scrollbind(TRUE);
766#endif
767
Bram Moolenaar860cae12010-06-05 23:22:07 +0200768#ifdef FEAT_CURSORBIND
769 if (curwin->w_p_crb)
770 do_check_cursorbind();
771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 update_curswant();
773 old_topline = curwin->w_topline;
774#ifdef FEAT_DIFF
775 old_topfill = curwin->w_topfill;
776#endif
777
778#ifdef USE_ON_FLY_SCROLL
779 dont_scroll = FALSE; /* allow scrolling here */
780#endif
781
782 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000783 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100785 if (c != K_CURSORHOLD)
786 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200787
788 /* After using CTRL-G U the next cursor key will not break undo. */
789 if (dont_sync_undo == MAYBE)
790 dont_sync_undo = TRUE;
791 else
792 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100793 if (cmdchar == K_PS)
794 /* Got here from normal mode when bracketed paste started. */
795 c = K_PS;
796 else
797 do
798 {
799 c = safe_vgetc();
800 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000802#ifdef FEAT_AUTOCMD
803 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
804 did_cursorhold = TRUE;
805#endif
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#ifdef FEAT_RIGHTLEFT
808 if (p_hkmap && KeyTyped)
809 c = hkmap(c); /* Hebrew mode mapping */
810#endif
811#ifdef FEAT_FKMAP
812 if (p_fkmap && KeyTyped)
813 c = fkmap(c); /* Farsi mode mapping */
814#endif
815
816#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000817 /*
818 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000819 * and the cursor is still in the completed word. Only when there is
820 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000821 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000822 if (compl_started
823 && pum_wanted()
824 && curwin->w_cursor.col >= compl_col
825 && (compl_shown_match == NULL
826 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000827 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000828 /* BS: Delete one character from "compl_leader". */
829 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000830 && curwin->w_cursor.col > compl_col
831 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000832 continue;
833
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000834 /* When no match was selected or it was edited. */
835 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000836 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000837 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000838 * "compl_leader". Except when at the original match and
839 * there is nothing to add, CTRL-L works like CTRL-P then. */
840 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100841 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000842 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000843 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 {
845 ins_compl_addfrommatch();
846 continue;
847 }
848
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000849 /* A non-white character that fits in with the current
850 * completion: Add to "compl_leader". */
851 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000852 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100853#ifdef FEAT_AUTOCMD
854 /* Trigger InsertCharPre. */
855 char_u *str = do_insert_char_pre(c);
856 char_u *p;
857
858 if (str != NULL)
859 {
860 for (p = str; *p != NUL; mb_ptr_adv(p))
861 ins_compl_addleader(PTR2CHAR(p));
862 vim_free(str);
863 }
864 else
865#endif
866 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000867 continue;
868 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000869
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000870 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000871 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200872 if ((c == Ctrl_Y || (compl_enter_selects
873 && (c == CAR || c == K_KENTER || c == NL)))
874 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000875 {
876 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200877 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000878 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000879 }
880 }
881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
883 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000884 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000885 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000886 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#endif
888
Bram Moolenaar488c6512005-08-11 20:09:58 +0000889 /* CTRL-\ CTRL-N goes to Normal mode,
890 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
891 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 if (c == Ctrl_BSL)
893 {
894 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000895 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 ++no_mapping;
897 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000898 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 --no_mapping;
900 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000901 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000903 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 vungetc(c);
905 c = Ctrl_BSL;
906 }
907 else if (c == Ctrl_G && p_im)
908 continue;
909 else
910 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000911 if (c == Ctrl_O)
912 {
913 ins_ctrl_o();
914 ins_at_eol = FALSE; /* cursor keeps its column */
915 nomove = TRUE;
916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 count = 0;
918 goto doESCkey;
919 }
920 }
921
922#ifdef FEAT_DIGRAPHS
923 c = do_digraph(c);
924#endif
925
926#ifdef FEAT_INS_EXPAND
927 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
928 goto docomplete;
929#endif
930 if (c == Ctrl_V || c == Ctrl_Q)
931 {
932 ins_ctrl_v();
933 c = Ctrl_V; /* pretend CTRL-V is last typed character */
934 continue;
935 }
936
937#ifdef FEAT_CINDENT
938 if (cindent_on()
939# ifdef FEAT_INS_EXPAND
940 && ctrl_x_mode == 0
941# endif
942 )
943 {
944 /* A key name preceded by a bang means this key is not to be
945 * inserted. Skip ahead to the re-indenting below.
946 * A key name preceded by a star means that indenting has to be
947 * done before inserting the key. */
948 line_is_white = inindent(0);
949 if (in_cinkeys(c, '!', line_is_white))
950 goto force_cindent;
951 if (can_cindent && in_cinkeys(c, '*', line_is_white)
952 && stop_arrow() == OK)
953 do_c_expr_indent();
954 }
955#endif
956
957#ifdef FEAT_RIGHTLEFT
958 if (curwin->w_p_rl)
959 switch (c)
960 {
961 case K_LEFT: c = K_RIGHT; break;
962 case K_S_LEFT: c = K_S_RIGHT; break;
963 case K_C_LEFT: c = K_C_RIGHT; break;
964 case K_RIGHT: c = K_LEFT; break;
965 case K_S_RIGHT: c = K_S_LEFT; break;
966 case K_C_RIGHT: c = K_C_LEFT; break;
967 }
968#endif
969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 /*
971 * If 'keymodel' contains "startsel", may start selection. If it
972 * does, a CTRL-O and c will be stuffed, we need to get these
973 * characters.
974 */
975 if (ins_start_select(c))
976 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 /*
979 * The big switch to handle a character in insert mode.
980 */
981 switch (c)
982 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000983 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (echeck_abbr(ESC + ABBR_OFF))
985 break;
986 /*FALLTHROUGH*/
987
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#ifdef FEAT_CMDWIN
990 if (c == Ctrl_C && cmdwin_type != 0)
991 {
992 /* Close the cmdline window. */
993 cmdwin_result = K_IGNORE;
994 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000995 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 goto doESCkey;
997 }
998#endif
999
1000#ifdef UNIX
1001do_intr:
1002#endif
1003 /* when 'insertmode' set, and not halfway a mapping, don't leave
1004 * Insert mode */
1005 if (goto_im())
1006 {
1007 if (got_int)
1008 {
1009 (void)vgetc(); /* flush all buffers */
1010 got_int = FALSE;
1011 }
1012 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001013 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 break;
1015 }
1016doESCkey:
1017 /*
1018 * This is the ONLY return from edit()!
1019 */
1020 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1021 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001022 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 o_lnum = curwin->w_cursor.lnum;
1024
Bram Moolenaar488c6512005-08-11 20:09:58 +00001025 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001026 {
1027#ifdef FEAT_AUTOCMD
1028 if (cmdchar != 'r' && cmdchar != 'v')
1029 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1030 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001031 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 continue;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case Ctrl_Z: /* suspend when 'insertmode' set */
1038 if (!p_im)
1039 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001040 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001041#ifdef CURSOR_SHAPE
1042 ui_cursor_shape(); /* may need to update cursor shape */
1043#endif
1044 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045
1046 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001047#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001048 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 goto docomplete;
1050#endif
1051 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1052 break;
1053 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001054
1055#ifdef FEAT_VIRTUALEDIT
1056 /* don't move the cursor left when 'virtualedit' has "onemore". */
1057 if (ve_flags & VE_ONEMORE)
1058 {
1059 ins_at_eol = FALSE;
1060 nomove = TRUE;
1061 }
1062#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001063 count = 0;
1064 goto doESCkey;
1065
Bram Moolenaar572cb562005-08-05 21:35:02 +00001066 case K_INS: /* toggle insert/replace mode */
1067 case K_KINS:
1068 ins_insert(replaceState);
1069 break;
1070
1071 case K_SELECT: /* end of Select mode mapping - ignore */
1072 break;
1073
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001074 case K_HELP: /* Help key works like <ESC> <Help> */
1075 case K_F1:
1076 case K_XF1:
1077 stuffcharReadbuff(K_HELP);
1078 if (p_im)
1079 need_start_insertmode = TRUE;
1080 goto doESCkey;
1081
1082#ifdef FEAT_NETBEANS_INTG
1083 case K_F21: /* NetBeans command */
1084 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001085 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 --no_mapping;
1087 netbeans_keycommand(i);
1088 break;
1089#endif
1090
1091 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 case NUL:
1093 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 /* For ^@ the trailing ESC will end the insert, unless there is an
1095 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1097 && c != Ctrl_A && !p_im)
1098 goto doESCkey; /* quit insert mode */
1099 inserted_space = FALSE;
1100 break;
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 ins_reg();
1104 auto_format(FALSE, TRUE);
1105 inserted_space = FALSE;
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_ctrl_g();
1110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case Ctrl_HAT: /* switch input mode and/or langmap */
1113 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 break;
1115
1116#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 if (!p_ari)
1119 goto normalchar;
1120 ins_ctrl_();
1121 break;
1122#endif
1123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1126 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1127 goto docomplete;
1128#endif
1129 /* FALLTHROUGH */
1130
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001131 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132# ifdef FEAT_INS_EXPAND
1133 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1134 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 if (has_compl_option(FALSE))
1136 goto docomplete;
1137 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139# endif
1140 ins_shift(c, lastc);
1141 auto_format(FALSE, TRUE);
1142 inserted_space = FALSE;
1143 break;
1144
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 case K_KDEL:
1147 ins_del();
1148 auto_format(FALSE, TRUE);
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case Ctrl_H:
1153 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1154 auto_format(FALSE, TRUE);
1155 break;
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1159 auto_format(FALSE, TRUE);
1160 break;
1161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001162 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001163# ifdef FEAT_COMPL_FUNC
1164 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001166 goto docomplete;
1167# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1169 auto_format(FALSE, TRUE);
1170 inserted_space = FALSE;
1171 break;
1172
1173#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 case K_LEFTMOUSE_NM:
1176 case K_LEFTDRAG:
1177 case K_LEFTRELEASE:
1178 case K_LEFTRELEASE_NM:
1179 case K_MIDDLEMOUSE:
1180 case K_MIDDLEDRAG:
1181 case K_MIDDLERELEASE:
1182 case K_RIGHTMOUSE:
1183 case K_RIGHTDRAG:
1184 case K_RIGHTRELEASE:
1185 case K_X1MOUSE:
1186 case K_X1DRAG:
1187 case K_X1RELEASE:
1188 case K_X2MOUSE:
1189 case K_X2DRAG:
1190 case K_X2RELEASE:
1191 ins_mouse(c);
1192 break;
1193
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001194 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001195 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 break;
1197
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001199 ins_mousescroll(MSCR_UP);
1200 break;
1201
1202 case K_MOUSELEFT: /* Scroll wheel left */
1203 ins_mousescroll(MSCR_LEFT);
1204 break;
1205
1206 case K_MOUSERIGHT: /* Scroll wheel right */
1207 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 break;
1209#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001210 case K_PS:
1211 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1212 if (cmdchar == K_PS)
1213 /* invoked from normal mode, bail out */
1214 goto doESCkey;
1215 break;
1216 case K_PE:
1217 /* Got K_PE without K_PS, ignore. */
1218 break;
1219
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001220#ifdef FEAT_GUI_TABLINE
1221 case K_TABLINE:
1222 case K_TABMENU:
1223 ins_tabline(c);
1224 break;
1225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001227 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 break;
1229
Bram Moolenaar754b5602006-02-09 23:53:20 +00001230#ifdef FEAT_AUTOCMD
1231 case K_CURSORHOLD: /* Didn't type something for a while. */
1232 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1233 did_cursorhold = TRUE;
1234 break;
1235#endif
1236
Bram Moolenaar4770d092006-01-12 23:22:24 +00001237#ifdef FEAT_GUI_W32
1238 /* On Win32 ignore <M-F4>, we get it when closing the window was
1239 * cancelled. */
1240 case K_F4:
1241 if (mod_mask != MOD_MASK_ALT)
1242 goto normalchar;
1243 break;
1244#endif
1245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#ifdef FEAT_GUI
1247 case K_VER_SCROLLBAR:
1248 ins_scroll();
1249 break;
1250
1251 case K_HOR_SCROLLBAR:
1252 ins_horscroll();
1253 break;
1254#endif
1255
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001256 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 case K_S_HOME:
1259 case K_C_HOME:
1260 ins_home(c);
1261 break;
1262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 case K_S_END:
1266 case K_C_END:
1267 ins_end(c);
1268 break;
1269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001270 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001271 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1272 ins_s_left();
1273 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001274 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 break;
1276
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001277 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 case K_C_LEFT:
1279 ins_s_left();
1280 break;
1281
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001282 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001283 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1284 ins_s_right();
1285 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001286 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 break;
1288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001289 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 case K_C_RIGHT:
1291 ins_s_right();
1292 break;
1293
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001294 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001295#ifdef FEAT_INS_EXPAND
1296 if (pum_visible())
1297 goto docomplete;
1298#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001299 if (mod_mask & MOD_MASK_SHIFT)
1300 ins_pageup();
1301 else
1302 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 break;
1304
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001305 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 case K_PAGEUP:
1307 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001308#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001309 if (pum_visible())
1310 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 ins_pageup();
1313 break;
1314
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001315 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001316#ifdef FEAT_INS_EXPAND
1317 if (pum_visible())
1318 goto docomplete;
1319#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001320 if (mod_mask & MOD_MASK_SHIFT)
1321 ins_pagedown();
1322 else
1323 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 break;
1325
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001326 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 case K_PAGEDOWN:
1328 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001329#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001330 if (pum_visible())
1331 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 ins_pagedown();
1334 break;
1335
1336#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001337 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 ins_drop();
1339 break;
1340#endif
1341
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001342 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 c = TAB;
1344 /* FALLTHROUGH */
1345
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001346 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1348 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1349 goto docomplete;
1350#endif
1351 inserted_space = FALSE;
1352 if (ins_tab())
1353 goto normalchar; /* insert TAB as a normal char */
1354 auto_format(FALSE, TRUE);
1355 break;
1356
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001357 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 c = CAR;
1359 /* FALLTHROUGH */
1360 case CAR:
1361 case NL:
1362#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1363 /* In a quickfix window a <CR> jumps to the error under the
1364 * cursor. */
1365 if (bt_quickfix(curbuf) && c == CAR)
1366 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001367 if (curwin->w_llist_ref == NULL) /* quickfix window */
1368 do_cmdline_cmd((char_u *)".cc");
1369 else /* location list window */
1370 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 break;
1372 }
1373#endif
1374#ifdef FEAT_CMDWIN
1375 if (cmdwin_type != 0)
1376 {
1377 /* Execute the command in the cmdline window. */
1378 cmdwin_result = CAR;
1379 goto doESCkey;
1380 }
1381#endif
1382 if (ins_eol(c) && !p_im)
1383 goto doESCkey; /* out of memory */
1384 auto_format(FALSE, FALSE);
1385 inserted_space = FALSE;
1386 break;
1387
Bram Moolenaar860cae12010-06-05 23:22:07 +02001388#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001389 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390# ifdef FEAT_INS_EXPAND
1391 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1392 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001393 if (has_compl_option(TRUE))
1394 goto docomplete;
1395 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
1397# endif
1398# ifdef FEAT_DIGRAPHS
1399 c = ins_digraph();
1400 if (c == NUL)
1401 break;
1402# endif
1403 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
1406#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001407 case Ctrl_X: /* Enter CTRL-X mode */
1408 ins_ctrl_x();
1409 break;
1410
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001411 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 if (ctrl_x_mode != CTRL_X_TAGS)
1413 goto normalchar;
1414 goto docomplete;
1415
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001416 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (ctrl_x_mode != CTRL_X_FILES)
1418 goto normalchar;
1419 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001420
1421 case 's': /* Spelling completion after ^X */
1422 case Ctrl_S:
1423 if (ctrl_x_mode != CTRL_X_SPELL)
1424 goto normalchar;
1425 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426#endif
1427
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001428 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429#ifdef FEAT_INS_EXPAND
1430 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1431#endif
1432 {
1433 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1434 if (p_im)
1435 {
1436 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1437 break;
1438 goto doESCkey;
1439 }
1440 goto normalchar;
1441 }
1442#ifdef FEAT_INS_EXPAND
1443 /* FALLTHROUGH */
1444
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001445 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 case Ctrl_N:
1447 /* if 'complete' is empty then plain ^P is no longer special,
1448 * but it is under other ^X modes */
1449 if (*curbuf->b_p_cpt == NUL
1450 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001451 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 goto normalchar;
1453
1454docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001455 compl_busy = TRUE;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001456 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001457 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001458 compl_cont_status = 0;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001459 disable_fold_update--;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001460 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 break;
1462#endif /* FEAT_INS_EXPAND */
1463
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001464 case Ctrl_Y: /* copy from previous line or scroll down */
1465 case Ctrl_E: /* copy from next line or scroll up */
1466 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 break;
1468
1469 default:
1470#ifdef UNIX
1471 if (c == intr_char) /* special interrupt char */
1472 goto do_intr;
1473#endif
1474
Bram Moolenaare659c952011-05-19 17:25:41 +02001475normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001477 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001479#ifdef FEAT_AUTOCMD
1480 if (!p_paste)
1481 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001482 /* Trigger InsertCharPre. */
1483 char_u *str = do_insert_char_pre(c);
1484 char_u *p;
1485
1486 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001487 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001488 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001489 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001490 /* Insert the new value of v:char literally. */
1491 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001492 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001493 c = PTR2CHAR(p);
1494 if (c == CAR || c == K_KENTER || c == NL)
1495 ins_eol(c);
1496 else
1497 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001498 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001499 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001500 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001501 vim_free(str);
1502 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001503 }
1504
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001505 /* If the new value is already inserted or an empty string
1506 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001507 if (c == NUL)
1508 break;
1509 }
1510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#ifdef FEAT_SMARTINDENT
1512 /* Try to perform smart-indenting. */
1513 ins_try_si(c);
1514#endif
1515
1516 if (c == ' ')
1517 {
1518 inserted_space = TRUE;
1519#ifdef FEAT_CINDENT
1520 if (inindent(0))
1521 can_cindent = FALSE;
1522#endif
1523 if (Insstart_blank_vcol == MAXCOL
1524 && curwin->w_cursor.lnum == Insstart.lnum)
1525 Insstart_blank_vcol = get_nolist_virtcol();
1526 }
1527
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001528 /* Insert a normal character and check for abbreviations on a
1529 * special character. Let CTRL-] expand abbreviations without
1530 * inserting it. */
1531 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532#ifdef FEAT_MBYTE
1533 /* Add ABBR_OFF for characters above 0x100, this is
1534 * what check_abbr() expects. */
1535 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1536#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001537 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {
1539 insert_special(c, FALSE, FALSE);
1540#ifdef FEAT_RIGHTLEFT
1541 revins_legal++;
1542 revins_chars++;
1543#endif
1544 }
1545
1546 auto_format(FALSE, TRUE);
1547
1548#ifdef FEAT_FOLDING
1549 /* When inserting a character the cursor line must never be in a
1550 * closed fold. */
1551 foldOpenCursor();
1552#endif
1553 break;
1554 } /* end of switch (c) */
1555
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001556#ifdef FEAT_AUTOCMD
1557 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001558 if (c != K_CURSORHOLD
1559# ifdef FEAT_COMPL_FUNC
1560 /* but not in CTRL-X mode, a script can't restore the state */
1561 && ctrl_x_mode == 0
1562# endif
1563 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001564 did_cursorhold = FALSE;
1565#endif
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 /* If the cursor was moved we didn't just insert a space */
1568 if (arrow_used)
1569 inserted_space = FALSE;
1570
1571#ifdef FEAT_CINDENT
1572 if (can_cindent && cindent_on()
1573# ifdef FEAT_INS_EXPAND
1574 && ctrl_x_mode == 0
1575# endif
1576 )
1577 {
1578force_cindent:
1579 /*
1580 * Indent now if a key was typed that is in 'cinkeys'.
1581 */
1582 if (in_cinkeys(c, ' ', line_is_white))
1583 {
1584 if (stop_arrow() == OK)
1585 /* re-indent the current line */
1586 do_c_expr_indent();
1587 }
1588 }
1589#endif /* FEAT_CINDENT */
1590
1591 } /* for (;;) */
1592 /* NOTREACHED */
1593}
1594
1595/*
1596 * Redraw for Insert mode.
1597 * This is postponed until getting the next character to make '$' in the 'cpo'
1598 * option work correctly.
1599 * Only redraw when there are no characters available. This speeds up
1600 * inserting sequences of characters (e.g., for CTRL-R).
1601 */
1602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001603ins_redraw(
1604 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001606#ifdef FEAT_CONCEAL
1607 linenr_T conceal_old_cursor_line = 0;
1608 linenr_T conceal_new_cursor_line = 0;
1609 int conceal_update_lines = FALSE;
1610#endif
1611
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001612 if (char_avail())
1613 return;
1614
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001615#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001616 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1617 * visible, the command might delete it. */
1618 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001619# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001620 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001621# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001622# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001623 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001624# endif
1625# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001626 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001627# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001628 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001629# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001630 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001631# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001632# ifdef FEAT_INS_EXPAND
1633 && !pum_visible()
1634# endif
1635 )
1636 {
1637# ifdef FEAT_SYN_HL
1638 /* Need to update the screen first, to make sure syntax
1639 * highlighting is correct after making a change (e.g., inserting
1640 * a "(". The autocommand may also require a redraw, so it's done
1641 * again below, unfortunately. */
1642 if (syntax_present(curwin) && must_redraw)
1643 update_screen(0);
1644# endif
1645# ifdef FEAT_AUTOCMD
1646 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001647 {
1648 /* Make sure curswant is correct, an autocommand may call
1649 * getcurpos(). */
1650 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001651 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001652 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001653# endif
1654# ifdef FEAT_CONCEAL
1655 if (curwin->w_p_cole > 0)
1656 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001657# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001658 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001659# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001660 conceal_new_cursor_line = curwin->w_cursor.lnum;
1661 conceal_update_lines = TRUE;
1662 }
1663# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001664# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001666# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001667 }
1668#endif
1669
1670#ifdef FEAT_AUTOCMD
1671 /* Trigger TextChangedI if b_changedtick differs. */
1672 if (ready && has_textchangedI()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001673 && last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001674# ifdef FEAT_INS_EXPAND
1675 && !pum_visible()
1676# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001677 )
1678 {
1679 if (last_changedtick_buf == curbuf)
1680 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1681 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001682 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001684#endif
1685
1686 if (must_redraw)
1687 update_screen(0);
1688 else if (clear_cmdline || redraw_cmdline)
1689 showmode(); /* clear cmdline and show mode */
1690# if defined(FEAT_CONCEAL)
1691 if ((conceal_update_lines
1692 && (conceal_old_cursor_line != conceal_new_cursor_line
1693 || conceal_cursor_line(curwin)))
1694 || need_cursor_line_redraw)
1695 {
1696 if (conceal_old_cursor_line != conceal_new_cursor_line)
1697 update_single_line(curwin, conceal_old_cursor_line);
1698 update_single_line(curwin, conceal_new_cursor_line == 0
1699 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1700 curwin->w_valid &= ~VALID_CROW;
1701 }
1702# endif
1703 showruler(FALSE);
1704 setcursor();
1705 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706}
1707
1708/*
1709 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1710 */
1711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001712ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001715 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
1717 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001718 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
1720 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001723 did_putchar = TRUE;
1724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1726
1727#ifdef FEAT_CMDL_INFO
1728 add_to_showcmd_c(Ctrl_V);
1729#endif
1730
1731 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001732 if (did_putchar)
1733 /* when the line fits in 'columns' the '^' is at the start of the next
1734 * line and will not removed by the redraw */
1735 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736#ifdef FEAT_CMDL_INFO
1737 clear_showcmd();
1738#endif
1739 insert_special(c, FALSE, TRUE);
1740#ifdef FEAT_RIGHTLEFT
1741 revins_chars++;
1742 revins_legal++;
1743#endif
1744}
1745
1746/*
1747 * Put a character directly onto the screen. It's not stored in a buffer.
1748 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1749 */
1750static int pc_status;
1751#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1752#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1753#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1754#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756static int pc_attr;
1757static int pc_row;
1758static int pc_col;
1759
1760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001761edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
1763 int attr;
1764
1765 if (ScreenLines != NULL)
1766 {
1767 update_topline(); /* just in case w_topline isn't valid */
1768 validate_cursor();
1769 if (highlight)
1770 attr = hl_attr(HLF_8);
1771 else
1772 attr = 0;
1773 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1774 pc_col = W_WINCOL(curwin);
1775#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1776 pc_status = PC_STATUS_UNSET;
1777#endif
1778#ifdef FEAT_RIGHTLEFT
1779 if (curwin->w_p_rl)
1780 {
1781 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1782# ifdef FEAT_MBYTE
1783 if (has_mbyte)
1784 {
1785 int fix_col = mb_fix_col(pc_col, pc_row);
1786
1787 if (fix_col != pc_col)
1788 {
1789 screen_putchar(' ', pc_row, fix_col, attr);
1790 --curwin->w_wcol;
1791 pc_status = PC_STATUS_RIGHT;
1792 }
1793 }
1794# endif
1795 }
1796 else
1797#endif
1798 {
1799 pc_col += curwin->w_wcol;
1800#ifdef FEAT_MBYTE
1801 if (mb_lefthalve(pc_row, pc_col))
1802 pc_status = PC_STATUS_LEFT;
1803#endif
1804 }
1805
1806 /* save the character to be able to put it back */
1807#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1808 if (pc_status == PC_STATUS_UNSET)
1809#endif
1810 {
1811 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1812 pc_status = PC_STATUS_SET;
1813 }
1814 screen_putchar(c, pc_row, pc_col, attr);
1815 }
1816}
1817
1818/*
1819 * Undo the previous edit_putchar().
1820 */
1821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1825 {
1826#if defined(FEAT_MBYTE)
1827 if (pc_status == PC_STATUS_RIGHT)
1828 ++curwin->w_wcol;
1829 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1830 redrawWinline(curwin->w_cursor.lnum, FALSE);
1831 else
1832#endif
1833 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1834 }
1835}
1836
1837/*
1838 * Called when p_dollar is set: display a '$' at the end of the changed text
1839 * Only works when cursor is in the line that changes.
1840 */
1841 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001842display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843{
1844 colnr_T save_col;
1845
1846 if (!redrawing())
1847 return;
1848
1849 cursor_off();
1850 save_col = curwin->w_cursor.col;
1851 curwin->w_cursor.col = col;
1852#ifdef FEAT_MBYTE
1853 if (has_mbyte)
1854 {
1855 char_u *p;
1856
1857 /* If on the last byte of a multi-byte move to the first byte. */
1858 p = ml_get_curline();
1859 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1860 }
1861#endif
1862 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1863 if (curwin->w_wcol < W_WIDTH(curwin))
1864 {
1865 edit_putchar('$', FALSE);
1866 dollar_vcol = curwin->w_virtcol;
1867 }
1868 curwin->w_cursor.col = save_col;
1869}
1870
1871/*
1872 * Call this function before moving the cursor from the normal insert position
1873 * in insert mode.
1874 */
1875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001876undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001878 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001880 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 redrawWinline(curwin->w_cursor.lnum, FALSE);
1882 }
1883}
1884
1885/*
1886 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1887 * Keep the cursor on the same character.
1888 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1889 * type == INDENT_DEC decrease indent (for CTRL-D)
1890 * type == INDENT_SET set indent to "amount"
1891 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1892 */
1893 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001894change_indent(
1895 int type,
1896 int amount,
1897 int round,
1898 int replaced, /* replaced character, put on replace stack */
1899 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900{
1901 int vcol;
1902 int last_vcol;
1903 int insstart_less; /* reduction for Insstart.col */
1904 int new_cursor_col;
1905 int i;
1906 char_u *ptr;
1907 int save_p_list;
1908 int start_col;
1909 colnr_T vc;
1910#ifdef FEAT_VREPLACE
1911 colnr_T orig_col = 0; /* init for GCC */
1912 char_u *new_line, *orig_line = NULL; /* init for GCC */
1913
1914 /* VREPLACE mode needs to know what the line was like before changing */
1915 if (State & VREPLACE_FLAG)
1916 {
1917 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1918 orig_col = curwin->w_cursor.col;
1919 }
1920#endif
1921
1922 /* for the following tricks we don't want list mode */
1923 save_p_list = curwin->w_p_list;
1924 curwin->w_p_list = FALSE;
1925 vc = getvcol_nolist(&curwin->w_cursor);
1926 vcol = vc;
1927
1928 /*
1929 * For Replace mode we need to fix the replace stack later, which is only
1930 * possible when the cursor is in the indent. Remember the number of
1931 * characters before the cursor if it's possible.
1932 */
1933 start_col = curwin->w_cursor.col;
1934
1935 /* determine offset from first non-blank */
1936 new_cursor_col = curwin->w_cursor.col;
1937 beginline(BL_WHITE);
1938 new_cursor_col -= curwin->w_cursor.col;
1939
1940 insstart_less = curwin->w_cursor.col;
1941
1942 /*
1943 * If the cursor is in the indent, compute how many screen columns the
1944 * cursor is to the left of the first non-blank.
1945 */
1946 if (new_cursor_col < 0)
1947 vcol = get_indent() - vcol;
1948
1949 if (new_cursor_col > 0) /* can't fix replace stack */
1950 start_col = -1;
1951
1952 /*
1953 * Set the new indent. The cursor will be put on the first non-blank.
1954 */
1955 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001956 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 else
1958 {
1959#ifdef FEAT_VREPLACE
1960 int save_State = State;
1961
1962 /* Avoid being called recursively. */
1963 if (State & VREPLACE_FLAG)
1964 State = INSERT;
1965#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001966 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967#ifdef FEAT_VREPLACE
1968 State = save_State;
1969#endif
1970 }
1971 insstart_less -= curwin->w_cursor.col;
1972
1973 /*
1974 * Try to put cursor on same character.
1975 * If the cursor is at or after the first non-blank in the line,
1976 * compute the cursor column relative to the column of the first
1977 * non-blank character.
1978 * If we are not in insert mode, leave the cursor on the first non-blank.
1979 * If the cursor is before the first non-blank, position it relative
1980 * to the first non-blank, counted in screen columns.
1981 */
1982 if (new_cursor_col >= 0)
1983 {
1984 /*
1985 * When changing the indent while the cursor is touching it, reset
1986 * Insstart_col to 0.
1987 */
1988 if (new_cursor_col == 0)
1989 insstart_less = MAXCOL;
1990 new_cursor_col += curwin->w_cursor.col;
1991 }
1992 else if (!(State & INSERT))
1993 new_cursor_col = curwin->w_cursor.col;
1994 else
1995 {
1996 /*
1997 * Compute the screen column where the cursor should be.
1998 */
1999 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002000 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
2002 /*
2003 * Advance the cursor until we reach the right screen column.
2004 */
2005 vcol = last_vcol = 0;
2006 new_cursor_col = -1;
2007 ptr = ml_get_curline();
2008 while (vcol <= (int)curwin->w_virtcol)
2009 {
2010 last_vcol = vcol;
2011#ifdef FEAT_MBYTE
2012 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002013 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 else
2015#endif
2016 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002017 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
2019 vcol = last_vcol;
2020
2021 /*
2022 * May need to insert spaces to be able to position the cursor on
2023 * the right screen column.
2024 */
2025 if (vcol != (int)curwin->w_virtcol)
2026 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002027 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002029 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (ptr != NULL)
2031 {
2032 new_cursor_col += i;
2033 ptr[i] = NUL;
2034 while (--i >= 0)
2035 ptr[i] = ' ';
2036 ins_str(ptr);
2037 vim_free(ptr);
2038 }
2039 }
2040
2041 /*
2042 * When changing the indent while the cursor is in it, reset
2043 * Insstart_col to 0.
2044 */
2045 insstart_less = MAXCOL;
2046 }
2047
2048 curwin->w_p_list = save_p_list;
2049
2050 if (new_cursor_col <= 0)
2051 curwin->w_cursor.col = 0;
2052 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002053 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 curwin->w_set_curswant = TRUE;
2055 changed_cline_bef_curs();
2056
2057 /*
2058 * May have to adjust the start of the insert.
2059 */
2060 if (State & INSERT)
2061 {
2062 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2063 {
2064 if ((int)Insstart.col <= insstart_less)
2065 Insstart.col = 0;
2066 else
2067 Insstart.col -= insstart_less;
2068 }
2069 if ((int)ai_col <= insstart_less)
2070 ai_col = 0;
2071 else
2072 ai_col -= insstart_less;
2073 }
2074
2075 /*
2076 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2077 * If the number of characters before the cursor decreased, need to pop a
2078 * few characters from the replace stack.
2079 * If the number of characters before the cursor increased, need to push a
2080 * few NULs onto the replace stack.
2081 */
2082 if (REPLACE_NORMAL(State) && start_col >= 0)
2083 {
2084 while (start_col > (int)curwin->w_cursor.col)
2085 {
2086 replace_join(0); /* remove a NUL from the replace stack */
2087 --start_col;
2088 }
2089 while (start_col < (int)curwin->w_cursor.col || replaced)
2090 {
2091 replace_push(NUL);
2092 if (replaced)
2093 {
2094 replace_push(replaced);
2095 replaced = NUL;
2096 }
2097 ++start_col;
2098 }
2099 }
2100
2101#ifdef FEAT_VREPLACE
2102 /*
2103 * For VREPLACE mode, we also have to fix the replace stack. In this case
2104 * it is always possible because we backspace over the whole line and then
2105 * put it back again the way we wanted it.
2106 */
2107 if (State & VREPLACE_FLAG)
2108 {
2109 /* If orig_line didn't allocate, just return. At least we did the job,
2110 * even if you can't backspace. */
2111 if (orig_line == NULL)
2112 return;
2113
2114 /* Save new line */
2115 new_line = vim_strsave(ml_get_curline());
2116 if (new_line == NULL)
2117 return;
2118
2119 /* We only put back the new line up to the cursor */
2120 new_line[curwin->w_cursor.col] = NUL;
2121
2122 /* Put back original line */
2123 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2124 curwin->w_cursor.col = orig_col;
2125
2126 /* Backspace from cursor to start of line */
2127 backspace_until_column(0);
2128
2129 /* Insert new stuff into line again */
2130 ins_bytes(new_line);
2131
2132 vim_free(new_line);
2133 }
2134#endif
2135}
2136
2137/*
2138 * Truncate the space at the end of a line. This is to be used only in an
2139 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2140 * modes.
2141 */
2142 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002143truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 int i;
2146
2147 /* find start of trailing white space */
2148 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2149 {
2150 if (State & REPLACE_FLAG)
2151 replace_join(0); /* remove a NUL from the replace stack */
2152 }
2153 line[i + 1] = NUL;
2154}
2155
2156#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2157 || defined(FEAT_COMMENTS) || defined(PROTO)
2158/*
2159 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2160 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002161 * Will attempt not to go before "col" even when there is a composing
2162 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 */
2164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002165backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 while ((int)curwin->w_cursor.col > col)
2168 {
2169 curwin->w_cursor.col--;
2170 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002171 replace_do_bs(col);
2172 else if (!del_char_after_col(col))
2173 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175}
2176#endif
2177
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002178/*
2179 * Like del_char(), but make sure not to go before column "limit_col".
2180 * Only matters when there are composing characters.
2181 * Return TRUE when something was deleted.
2182 */
2183 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002184del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002185{
2186#ifdef FEAT_MBYTE
2187 if (enc_utf8 && limit_col >= 0)
2188 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002189 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002190
2191 /* Make sure the cursor is at the start of a character, but
2192 * skip forward again when going too far back because of a
2193 * composing character. */
2194 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002195 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002196 {
2197 int l = utf_ptr2len(ml_get_cursor());
2198
2199 if (l == 0) /* end of line */
2200 break;
2201 curwin->w_cursor.col += l;
2202 }
2203 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2204 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002205 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002206 }
2207 else
2208#endif
2209 (void)del_char(FALSE);
2210 return TRUE;
2211}
2212
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2214/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002215 * CTRL-X pressed in Insert mode.
2216 */
2217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002218ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002219{
2220 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2221 * CTRL-V works like CTRL-N */
2222 if (ctrl_x_mode != CTRL_X_CMDLINE)
2223 {
2224 /* if the next ^X<> won't ADD nothing, then reset
2225 * compl_cont_status */
2226 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002227 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002228 else
2229 compl_cont_status = 0;
2230 /* We're not sure which CTRL-X mode it will be yet */
2231 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2232 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2233 edit_submode_pre = NULL;
2234 showmode();
2235 }
2236}
2237
2238/*
2239 * Return TRUE if the 'dict' or 'tsr' option can be used.
2240 */
2241 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002242has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002243{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002244 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002245# ifdef FEAT_SPELL
2246 && !curwin->w_p_spell
2247# endif
2248 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002249 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2250 {
2251 ctrl_x_mode = 0;
2252 edit_submode = NULL;
2253 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2254 : (char_u *)_("'thesaurus' option is empty"),
2255 hl_attr(HLF_E));
2256 if (emsg_silent == 0)
2257 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002258 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002259 setcursor();
2260 out_flush();
2261 ui_delay(2000L, FALSE);
2262 }
2263 return FALSE;
2264 }
2265 return TRUE;
2266}
2267
2268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2270 * This depends on the current mode.
2271 */
2272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002273vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 /* Always allow ^R - let it's results then be checked */
2276 if (c == Ctrl_R)
2277 return TRUE;
2278
Bram Moolenaare3226be2005-12-18 22:10:00 +00002279 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002280 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002281 return TRUE;
2282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 switch (ctrl_x_mode)
2284 {
2285 case 0: /* Not in any CTRL-X mode */
2286 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2287 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002288 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2290 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2291 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002292 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002293 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 case CTRL_X_SCROLL:
2295 return (c == Ctrl_Y || c == Ctrl_E);
2296 case CTRL_X_WHOLE_LINE:
2297 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2298 case CTRL_X_FILES:
2299 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2300 case CTRL_X_DICTIONARY:
2301 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2302 case CTRL_X_THESAURUS:
2303 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2304 case CTRL_X_TAGS:
2305 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2306#ifdef FEAT_FIND_ID
2307 case CTRL_X_PATH_PATTERNS:
2308 return (c == Ctrl_P || c == Ctrl_N);
2309 case CTRL_X_PATH_DEFINES:
2310 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2311#endif
2312 case CTRL_X_CMDLINE:
2313 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2314 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002315#ifdef FEAT_COMPL_FUNC
2316 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002317 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002318 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002319 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002320#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002321 case CTRL_X_SPELL:
2322 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002323 case CTRL_X_EVAL:
2324 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002326 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 return FALSE;
2328}
2329
2330/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002331 * Return TRUE when character "c" is part of the item currently being
2332 * completed. Used to decide whether to abandon complete mode when the menu
2333 * is visible.
2334 */
2335 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002336ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002337{
2338 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2339 /* When expanding an identifier only accept identifier chars. */
2340 return vim_isIDc(c);
2341
2342 switch (ctrl_x_mode)
2343 {
2344 case CTRL_X_FILES:
2345 /* When expanding file name only accept file name chars. But not
2346 * path separators, so that "proto/<Tab>" expands files in
2347 * "proto", not "proto/" as a whole */
2348 return vim_isfilec(c) && !vim_ispathsep(c);
2349
2350 case CTRL_X_CMDLINE:
2351 case CTRL_X_OMNI:
2352 /* Command line and Omni completion can work with just about any
2353 * printable character, but do stop at white space. */
2354 return vim_isprintc(c) && !vim_iswhite(c);
2355
2356 case CTRL_X_WHOLE_LINE:
2357 /* For while line completion a space can be part of the line. */
2358 return vim_isprintc(c);
2359 }
2360 return vim_iswordc(c);
2361}
2362
2363/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002364 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002366 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 * the rest of the word to be in -- webb
2368 */
2369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002370ins_compl_add_infercase(
2371 char_u *str,
2372 int len,
2373 int icase,
2374 char_u *fname,
2375 int dir,
2376 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002378 char_u *p;
2379 int i, c;
2380 int actual_len; /* Take multi-byte characters */
2381 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002382 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002383 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 int has_lower = FALSE;
2385 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002387 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002389 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
Bram Moolenaara2993e12007-08-12 14:38:46 +00002391 /* Find actual length of completion. */
2392#ifdef FEAT_MBYTE
2393 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002395 p = str;
2396 actual_len = 0;
2397 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002399 mb_ptr_adv(p);
2400 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
2402 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002403 else
2404#endif
2405 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
Bram Moolenaara2993e12007-08-12 14:38:46 +00002407 /* Find actual length of original text. */
2408#ifdef FEAT_MBYTE
2409 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002411 p = compl_orig_text;
2412 actual_compl_length = 0;
2413 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002415 mb_ptr_adv(p);
2416 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002419 else
2420#endif
2421 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002423 /* "actual_len" may be smaller than "actual_compl_length" when using
2424 * thesaurus, only use the minimum when comparing. */
2425 min_len = actual_len < actual_compl_length
2426 ? actual_len : actual_compl_length;
2427
Bram Moolenaara2993e12007-08-12 14:38:46 +00002428 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002429 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002430 if (wca != NULL)
2431 {
2432 p = str;
2433 for (i = 0; i < actual_len; ++i)
2434#ifdef FEAT_MBYTE
2435 if (has_mbyte)
2436 wca[i] = mb_ptr2char_adv(&p);
2437 else
2438#endif
2439 wca[i] = *(p++);
2440
2441 /* Rule 1: Were any chars converted to lower? */
2442 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002443 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002444 {
2445#ifdef FEAT_MBYTE
2446 if (has_mbyte)
2447 c = mb_ptr2char_adv(&p);
2448 else
2449#endif
2450 c = *(p++);
2451 if (MB_ISLOWER(c))
2452 {
2453 has_lower = TRUE;
2454 if (MB_ISUPPER(wca[i]))
2455 {
2456 /* Rule 1 is satisfied. */
2457 for (i = actual_compl_length; i < actual_len; ++i)
2458 wca[i] = MB_TOLOWER(wca[i]);
2459 break;
2460 }
2461 }
2462 }
2463
2464 /*
2465 * Rule 2: No lower case, 2nd consecutive letter converted to
2466 * upper case.
2467 */
2468 if (!has_lower)
2469 {
2470 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002471 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002472 {
2473#ifdef FEAT_MBYTE
2474 if (has_mbyte)
2475 c = mb_ptr2char_adv(&p);
2476 else
2477#endif
2478 c = *(p++);
2479 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2480 {
2481 /* Rule 2 is satisfied. */
2482 for (i = actual_compl_length; i < actual_len; ++i)
2483 wca[i] = MB_TOUPPER(wca[i]);
2484 break;
2485 }
2486 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2487 }
2488 }
2489
2490 /* Copy the original case of the part we typed. */
2491 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002492 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002493 {
2494#ifdef FEAT_MBYTE
2495 if (has_mbyte)
2496 c = mb_ptr2char_adv(&p);
2497 else
2498#endif
2499 c = *(p++);
2500 if (MB_ISLOWER(c))
2501 wca[i] = MB_TOLOWER(wca[i]);
2502 else if (MB_ISUPPER(c))
2503 wca[i] = MB_TOUPPER(wca[i]);
2504 }
2505
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002506 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002507 * Generate encoding specific output from wide character array.
2508 * Multi-byte characters can occupy up to five bytes more than
2509 * ASCII characters, and we also need one byte for NUL, so stay
2510 * six bytes away from the edge of IObuff.
2511 */
2512 p = IObuff;
2513 i = 0;
2514 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2515#ifdef FEAT_MBYTE
2516 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002517 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002518 else
2519#endif
2520 *(p++) = wca[i++];
2521 *p = NUL;
2522
2523 vim_free(wca);
2524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002526 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2527 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002529 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530}
2531
2532/*
2533 * Add a match to the list of matches.
2534 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002535 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002536 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002539ins_compl_add(
2540 char_u *str,
2541 int len,
2542 int icase,
2543 char_u *fname,
2544 char_u **cptext, /* extra text for popup menu or NULL */
2545 int cdir,
2546 int flags,
2547 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002549 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002550 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552 ui_breakcheck();
2553 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002554 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 if (len < 0)
2556 len = (int)STRLEN(str);
2557
2558 /*
2559 * If the same match is already present, don't add it.
2560 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002561 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002563 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 do
2565 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002566 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002567 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002568 && match->cp_str[len] == NUL)
2569 return NOTDONE;
2570 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002571 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002574 /* Remove any popup menu before changing the list of matches. */
2575 ins_compl_del_pum();
2576
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 /*
2578 * Allocate a new match structure.
2579 * Copy the values to the new match structure.
2580 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002581 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002583 return FAIL;
2584 match->cp_number = -1;
2585 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002586 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002587 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
2589 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002590 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002592 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002593
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002595 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2597 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002598 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002599 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002600 && compl_curr_match->cp_fname != NULL
2601 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002602 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002603 else if (fname != NULL)
2604 {
2605 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002606 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002609 match->cp_fname = NULL;
2610 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002611
2612 if (cptext != NULL)
2613 {
2614 int i;
2615
2616 for (i = 0; i < CPT_COUNT; ++i)
2617 if (cptext[i] != NULL && *cptext[i] != NUL)
2618 match->cp_text[i] = vim_strsave(cptext[i]);
2619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621 /*
2622 * Link the new match structure in the list of matches.
2623 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002624 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002625 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 else if (dir == FORWARD)
2627 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002628 match->cp_next = compl_curr_match->cp_next;
2629 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631 else /* BACKWARD */
2632 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002633 match->cp_next = compl_curr_match;
2634 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002636 if (match->cp_next)
2637 match->cp_next->cp_prev = match;
2638 if (match->cp_prev)
2639 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002641 compl_first_match = match;
2642 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002644 /*
2645 * Find the longest common string if still doing that.
2646 */
2647 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2648 ins_compl_longest_match(match);
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 return OK;
2651}
2652
2653/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002654 * Return TRUE if "str[len]" matches with match->cp_str, considering
2655 * match->cp_icase.
2656 */
2657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002658ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002659{
2660 if (match->cp_icase)
2661 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2662 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2663}
2664
2665/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002666 * Reduce the longest common string for match "match".
2667 */
2668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002669ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002670{
2671 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002672 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002673 int had_match;
2674
2675 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002676 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002677 /* First match, use it as a whole. */
2678 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002679 if (compl_leader != NULL)
2680 {
2681 had_match = (curwin->w_cursor.col > compl_col);
2682 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002683 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002684 ins_redraw(FALSE);
2685
2686 /* When the match isn't there (to avoid matching itself) remove it
2687 * again after redrawing. */
2688 if (!had_match)
2689 ins_compl_delete();
2690 compl_used_match = FALSE;
2691 }
2692 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002693 else
2694 {
2695 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002696 p = compl_leader;
2697 s = match->cp_str;
2698 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002699 {
2700#ifdef FEAT_MBYTE
2701 if (has_mbyte)
2702 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002703 c1 = mb_ptr2char(p);
2704 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002705 }
2706 else
2707#endif
2708 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002709 c1 = *p;
2710 c2 = *s;
2711 }
2712 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2713 : (c1 != c2))
2714 break;
2715#ifdef FEAT_MBYTE
2716 if (has_mbyte)
2717 {
2718 mb_ptr_adv(p);
2719 mb_ptr_adv(s);
2720 }
2721 else
2722#endif
2723 {
2724 ++p;
2725 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002726 }
2727 }
2728
2729 if (*p != NUL)
2730 {
2731 /* Leader was shortened, need to change the inserted text. */
2732 *p = NUL;
2733 had_match = (curwin->w_cursor.col > compl_col);
2734 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002735 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002736 ins_redraw(FALSE);
2737
2738 /* When the match isn't there (to avoid matching itself) remove it
2739 * again after redrawing. */
2740 if (!had_match)
2741 ins_compl_delete();
2742 }
2743
2744 compl_used_match = FALSE;
2745 }
2746}
2747
2748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 * Add an array of matches to the list of matches.
2750 * Frees matches[].
2751 */
2752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002753ins_compl_add_matches(
2754 int num_matches,
2755 char_u **matches,
2756 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
2758 int i;
2759 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002760 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
Bram Moolenaar572cb562005-08-05 21:35:02 +00002762 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002763 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002764 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002766 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 FreeWild(num_matches, matches);
2768}
2769
2770/* Make the completion list cyclic.
2771 * Return the number of matches (excluding the original).
2772 */
2773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002774ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002776 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 int count = 0;
2778
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002779 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
2781 /*
2782 * Find the end of the list.
2783 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002784 match = compl_first_match;
2785 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002786 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002788 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 ++count;
2790 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002791 match->cp_next = compl_first_match;
2792 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794 return count;
2795}
2796
Bram Moolenaara94bc432006-03-10 21:42:59 +00002797/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002798 * Set variables that store noselect and noinsert behavior from the
2799 * 'completeopt' value.
2800 */
2801 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002802completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002803{
2804 compl_no_insert = FALSE;
2805 compl_no_select = FALSE;
2806 if (strstr((char *)p_cot, "noselect") != NULL)
2807 compl_no_select = TRUE;
2808 if (strstr((char *)p_cot, "noinsert") != NULL)
2809 compl_no_insert = TRUE;
2810}
2811
2812/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002813 * Start completion for the complete() function.
2814 * "startcol" is where the matched text starts (1 is first column).
2815 * "list" is the list of matches.
2816 */
2817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002818set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002819{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002820 int save_w_wrow = curwin->w_wrow;
2821
Bram Moolenaara94bc432006-03-10 21:42:59 +00002822 /* If already doing completions stop it. */
2823 if (ctrl_x_mode != 0)
2824 ins_compl_prep(' ');
2825 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002826 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002827
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002828 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002829 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002830 startcol = curwin->w_cursor.col;
2831 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002832 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002833 /* compl_pattern doesn't need to be set */
2834 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2835 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002836 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002837 return;
2838
Bram Moolenaare4214502015-03-05 18:08:43 +01002839 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002840
2841 ins_compl_add_list(list);
2842 compl_matches = ins_compl_make_cyclic();
2843 compl_started = TRUE;
2844 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002845 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002846
2847 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002848 if (compl_no_insert || compl_no_select)
2849 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002850 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002851 if (compl_no_select)
2852 /* Down/Up has no real effect. */
2853 ins_complete(K_UP, FALSE);
2854 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002855 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002856 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002857 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002858
2859 /* Lazily show the popup menu, unless we got interrupted. */
2860 if (!compl_interrupted)
2861 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002862 out_flush();
2863}
2864
2865
Bram Moolenaar9372a112005-12-06 19:59:18 +00002866/* "compl_match_array" points the currently displayed list of entries in the
2867 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002868static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002869static int compl_match_arraysize;
2870
2871/*
2872 * Update the screen and when there is any scrolling remove the popup menu.
2873 */
2874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002875ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002876{
2877 int h;
2878
2879 if (compl_match_array != NULL)
2880 {
2881 h = curwin->w_cline_height;
2882 update_screen(0);
2883 if (h != curwin->w_cline_height)
2884 ins_compl_del_pum();
2885 }
2886}
2887
2888/*
2889 * Remove any popup menu.
2890 */
2891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002892ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002893{
2894 if (compl_match_array != NULL)
2895 {
2896 pum_undisplay();
2897 vim_free(compl_match_array);
2898 compl_match_array = NULL;
2899 }
2900}
2901
2902/*
2903 * Return TRUE if the popup menu should be displayed.
2904 */
2905 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002906pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002907{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002908 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002909 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002910 return FALSE;
2911
2912 /* The display looks bad on a B&W display. */
2913 if (t_colors < 8
2914#ifdef FEAT_GUI
2915 && !gui.in_use
2916#endif
2917 )
2918 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002919 return TRUE;
2920}
2921
2922/*
2923 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002924 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002925 */
2926 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002927pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002928{
2929 compl_T *compl;
2930 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002931
2932 /* Don't display the popup menu if there are no matches or there is only
2933 * one (ignoring the original text). */
2934 compl = compl_first_match;
2935 i = 0;
2936 do
2937 {
2938 if (compl == NULL
2939 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2940 break;
2941 compl = compl->cp_next;
2942 } while (compl != compl_first_match);
2943
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002944 if (strstr((char *)p_cot, "menuone") != NULL)
2945 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002946 return (i >= 2);
2947}
2948
2949/*
2950 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002951 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002952 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002953 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002954ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002955{
2956 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002957 compl_T *shown_compl = NULL;
2958 int did_find_shown_match = FALSE;
2959 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002960 int i;
2961 int cur = -1;
2962 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002963 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002964
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002965 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002966 return;
2967
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002968#if defined(FEAT_EVAL)
2969 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2970 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2971#endif
2972
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002973 /* Update the screen before drawing the popup menu over it. */
2974 update_screen(0);
2975
2976 if (compl_match_array == NULL)
2977 {
2978 /* Need to build the popup menu list. */
2979 compl_match_arraysize = 0;
2980 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002981 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002982 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002983 do
2984 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002985 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2986 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002987 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002988 ++compl_match_arraysize;
2989 compl = compl->cp_next;
2990 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002991 if (compl_match_arraysize == 0)
2992 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002993 compl_match_array = (pumitem_T *)alloc_clear(
2994 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002995 * compl_match_arraysize));
2996 if (compl_match_array != NULL)
2997 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002998 /* If the current match is the original text don't find the first
2999 * match after it, don't highlight anything. */
3000 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3001 shown_match_ok = TRUE;
3002
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003003 i = 0;
3004 compl = compl_first_match;
3005 do
3006 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003007 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3008 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003009 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003010 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003011 if (!shown_match_ok)
3012 {
3013 if (compl == compl_shown_match || did_find_shown_match)
3014 {
3015 /* This item is the shown match or this is the
3016 * first displayed item after the shown match. */
3017 compl_shown_match = compl;
3018 did_find_shown_match = TRUE;
3019 shown_match_ok = TRUE;
3020 }
3021 else
3022 /* Remember this displayed match for when the
3023 * shown match is just below it. */
3024 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003025 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003026 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003027
3028 if (compl->cp_text[CPT_ABBR] != NULL)
3029 compl_match_array[i].pum_text =
3030 compl->cp_text[CPT_ABBR];
3031 else
3032 compl_match_array[i].pum_text = compl->cp_str;
3033 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3034 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3035 if (compl->cp_text[CPT_MENU] != NULL)
3036 compl_match_array[i++].pum_extra =
3037 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003038 else
3039 compl_match_array[i++].pum_extra = compl->cp_fname;
3040 }
3041
3042 if (compl == compl_shown_match)
3043 {
3044 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003045
3046 /* When the original text is the shown match don't set
3047 * compl_shown_match. */
3048 if (compl->cp_flags & ORIGINAL_TEXT)
3049 shown_match_ok = TRUE;
3050
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003051 if (!shown_match_ok && shown_compl != NULL)
3052 {
3053 /* The shown match isn't displayed, set it to the
3054 * previously displayed match. */
3055 compl_shown_match = shown_compl;
3056 shown_match_ok = TRUE;
3057 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003058 }
3059 compl = compl->cp_next;
3060 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003061
3062 if (!shown_match_ok) /* no displayed match at all */
3063 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003064 }
3065 }
3066 else
3067 {
3068 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003069 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003070 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3071 || compl_match_array[i].pum_text
3072 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003073 {
3074 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003075 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003076 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003077 }
3078
3079 if (compl_match_array != NULL)
3080 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003081 /* In Replace mode when a $ is displayed at the end of the line only
3082 * part of the screen would be updated. We do need to redraw here. */
3083 dollar_vcol = -1;
3084
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003085 /* Compute the screen column of the start of the completed text.
3086 * Use the cursor to get all wrapping and other settings right. */
3087 col = curwin->w_cursor.col;
3088 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003089 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003090 curwin->w_cursor.col = col;
3091 }
3092}
3093
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094#define DICT_FIRST (1) /* use just first element in "dict" */
3095#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003096
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003098 * Add any identifiers that match the given pattern in the list of dictionary
3099 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 */
3101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003102ins_compl_dictionaries(
3103 char_u *dict_start,
3104 char_u *pat,
3105 int flags, /* DICT_FIRST and/or DICT_EXACT */
3106 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003108 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 char_u *ptr;
3110 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 char_u **files;
3113 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003115 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
Bram Moolenaar0b238792006-03-02 22:49:12 +00003117 if (*dict == NUL)
3118 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003119#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003120 /* When 'dictionary' is empty and spell checking is enabled use
3121 * "spell". */
3122 if (!thesaurus && curwin->w_p_spell)
3123 dict = (char_u *)"spell";
3124 else
3125#endif
3126 return;
3127 }
3128
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003130 if (buf == NULL)
3131 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003132 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003133
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 /* If 'infercase' is set, don't use 'smartcase' here */
3135 save_p_scs = p_scs;
3136 if (curbuf->b_p_inf)
3137 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003138
3139 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3140 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003141 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003142 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003143 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003144 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003145 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003146
3147 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003148 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003149 len = STRLEN(pat_esc) + 10;
3150 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003151 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003152 {
3153 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003154 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003155 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003156 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003157 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3158 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003159 vim_free(ptr);
3160 }
3161 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003162 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003163 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003164 if (regmatch.regprog == NULL)
3165 goto theend;
3166 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003167
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3169 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003170 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
3172 /* copy one dictionary file name into buf */
3173 if (flags == DICT_EXACT)
3174 {
3175 count = 1;
3176 files = &dict;
3177 }
3178 else
3179 {
3180 /* Expand wildcards in the dictionary name, but do not allow
3181 * backticks (for security, the 'dict' option may have been set in
3182 * a modeline). */
3183 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003184# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003185 if (!thesaurus && STRCMP(buf, "spell") == 0)
3186 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003187 else
3188# endif
3189 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 || expand_wildcards(1, &buf, &count, &files,
3191 EW_FILE|EW_SILENT) != OK)
3192 count = 0;
3193 }
3194
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003195# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003196 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003198 /* Complete from active spelling. Skip "\<" in the pattern, we
3199 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003200 if (pat[0] == '\\' && pat[1] == '<')
3201 ptr = pat + 2;
3202 else
3203 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003204 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003206 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003207# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003208 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003209 {
3210 ins_compl_files(count, files, thesaurus, flags,
3211 &regmatch, buf, &dir);
3212 if (flags != DICT_EXACT)
3213 FreeWild(count, files);
3214 }
3215 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 break;
3217 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003218
3219theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003221 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 vim_free(buf);
3223}
3224
Bram Moolenaar0b238792006-03-02 22:49:12 +00003225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003226ins_compl_files(
3227 int count,
3228 char_u **files,
3229 int thesaurus,
3230 int flags,
3231 regmatch_T *regmatch,
3232 char_u *buf,
3233 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003234{
3235 char_u *ptr;
3236 int i;
3237 FILE *fp;
3238 int add_r;
3239
3240 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3241 {
3242 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3243 if (flags != DICT_EXACT)
3244 {
3245 vim_snprintf((char *)IObuff, IOSIZE,
3246 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003247 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003248 }
3249
3250 if (fp != NULL)
3251 {
3252 /*
3253 * Read dictionary file line by line.
3254 * Check each line for a match.
3255 */
3256 while (!got_int && !compl_interrupted
3257 && !vim_fgets(buf, LSIZE, fp))
3258 {
3259 ptr = buf;
3260 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3261 {
3262 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003263 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003264 ptr = find_line_end(ptr);
3265 else
3266 ptr = find_word_end(ptr);
3267 add_r = ins_compl_add_infercase(regmatch->startp[0],
3268 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003269 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003270 if (thesaurus)
3271 {
3272 char_u *wstart;
3273
3274 /*
3275 * Add the other matches on the line
3276 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003277 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003278 while (!got_int)
3279 {
3280 /* Find start of the next word. Skip white
3281 * space and punctuation. */
3282 ptr = find_word_start(ptr);
3283 if (*ptr == NUL || *ptr == NL)
3284 break;
3285 wstart = ptr;
3286
Bram Moolenaara2993e12007-08-12 14:38:46 +00003287 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003288#ifdef FEAT_MBYTE
3289 if (has_mbyte)
3290 /* Japanese words may have characters in
3291 * different classes, only separate words
3292 * with single-byte non-word characters. */
3293 while (*ptr != NUL)
3294 {
3295 int l = (*mb_ptr2len)(ptr);
3296
3297 if (l < 2 && !vim_iswordc(*ptr))
3298 break;
3299 ptr += l;
3300 }
3301 else
3302#endif
3303 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003304
3305 /* Add the word. Skip the regexp match. */
3306 if (wstart != regmatch->startp[0])
3307 add_r = ins_compl_add_infercase(wstart,
3308 (int)(ptr - wstart),
3309 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003310 }
3311 }
3312 if (add_r == OK)
3313 /* if dir was BACKWARD then honor it just once */
3314 *dir = FORWARD;
3315 else if (add_r == FAIL)
3316 break;
3317 /* avoid expensive call to vim_regexec() when at end
3318 * of line */
3319 if (*ptr == '\n' || got_int)
3320 break;
3321 }
3322 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003323 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003324 }
3325 fclose(fp);
3326 }
3327 }
3328}
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330/*
3331 * Find the start of the next word.
3332 * Returns a pointer to the first char of the word. Also stops at a NUL.
3333 */
3334 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003335find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
3337#ifdef FEAT_MBYTE
3338 if (has_mbyte)
3339 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003340 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 else
3342#endif
3343 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3344 ++ptr;
3345 return ptr;
3346}
3347
3348/*
3349 * Find the end of the word. Assumes it starts inside a word.
3350 * Returns a pointer to just after the word.
3351 */
3352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003353find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355#ifdef FEAT_MBYTE
3356 int start_class;
3357
3358 if (has_mbyte)
3359 {
3360 start_class = mb_get_class(ptr);
3361 if (start_class > 1)
3362 while (*ptr != NUL)
3363 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003364 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (mb_get_class(ptr) != start_class)
3366 break;
3367 }
3368 }
3369 else
3370#endif
3371 while (vim_iswordc(*ptr))
3372 ++ptr;
3373 return ptr;
3374}
3375
3376/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003377 * Find the end of the line, omitting CR and NL at the end.
3378 * Returns a pointer to just after the line.
3379 */
3380 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003381find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003382{
3383 char_u *s;
3384
3385 s = ptr + STRLEN(ptr);
3386 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3387 --s;
3388 return s;
3389}
3390
3391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 * Free the list of completions
3393 */
3394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003395ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003397 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003398 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003400 vim_free(compl_pattern);
3401 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003402 vim_free(compl_leader);
3403 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003405 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003407
3408 ins_compl_del_pum();
3409 pum_clear();
3410
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003411 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 do
3413 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003414 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003415 compl_curr_match = compl_curr_match->cp_next;
3416 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003418 if (match->cp_flags & FREE_FNAME)
3419 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003420 for (i = 0; i < CPT_COUNT; ++i)
3421 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003423 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3424 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003425 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426}
3427
3428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003429ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003431 compl_cont_status = 0;
3432 compl_started = FALSE;
3433 compl_matches = 0;
3434 vim_free(compl_pattern);
3435 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003436 vim_free(compl_leader);
3437 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003439 vim_free(compl_orig_text);
3440 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003441 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003442 /* clear v:completed_item */
3443 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444}
3445
3446/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003447 * Return TRUE when Insert completion is active.
3448 */
3449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003450ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003451{
3452 return compl_started;
3453}
3454
3455/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003456 * Delete one character before the cursor and show the subset of the matches
3457 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003458 * Returns the character to be used, NUL if the work is done and another char
3459 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003460 */
3461 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003462ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003463{
3464 char_u *line;
3465 char_u *p;
3466
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003467 line = ml_get_curline();
3468 p = line + curwin->w_cursor.col;
3469 mb_ptr_back(line, p);
3470
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003471 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003472 * allow the word to be deleted, we won't match everything.
3473 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003474 if ((int)(p - line) - (int)compl_col < 0
3475 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003476 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3477 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3478 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003479 return K_BS;
3480
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003481 /* Deleted more than what was used to find matches or didn't finish
3482 * finding all matches: need to look for matches all over again. */
3483 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003484 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003485 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003486
Bram Moolenaara6557602006-02-04 22:43:20 +00003487 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003488 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003489 if (compl_leader != NULL)
3490 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003491 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003492 if (compl_shown_match != NULL)
3493 /* Make sure current match is not a hidden item. */
3494 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003495 return NUL;
3496 }
3497 return K_BS;
3498}
Bram Moolenaara6557602006-02-04 22:43:20 +00003499
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003500/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003501 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3502 * be called.
3503 */
3504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003505ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003506{
3507 /* Return TRUE if we didn't complete finding matches or when the
3508 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3509 return compl_was_interrupted
3510 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3511 && compl_opt_refresh_always);
3512}
3513
3514/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003515 * Called after changing "compl_leader".
3516 * Show the popup menu with a different set of matches.
3517 * May also search for matches again if the previous search was interrupted.
3518 */
3519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003520ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003521{
3522 ins_compl_del_pum();
3523 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003524 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003525 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003526
3527 if (compl_started)
3528 ins_compl_set_original_text(compl_leader);
3529 else
3530 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003531#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003532 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003533#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003534 /*
3535 * Matches were cleared, need to search for them now. First display
3536 * the changed text before the cursor. Set "compl_restarting" to
3537 * avoid that the first match is inserted.
3538 */
3539 update_screen(0);
3540#ifdef FEAT_GUI
3541 if (gui.in_use)
3542 {
3543 /* Show the cursor after the match, not after the redrawn text. */
3544 setcursor();
3545 out_flush();
3546 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003547 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003548#endif
3549 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003550 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003551 compl_cont_status = 0;
3552 compl_restarting = FALSE;
3553 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003554
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003555 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003556
3557 /* Show the popup menu with a different set of matches. */
3558 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003559
3560 /* Don't let Enter select the original text when there is no popup menu. */
3561 if (compl_match_array == NULL)
3562 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003563}
3564
3565/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003566 * Return the length of the completion, from the completion start column to
3567 * the cursor column. Making sure it never goes below zero.
3568 */
3569 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003570ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003571{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003572 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003573
3574 if (off < 0)
3575 return 0;
3576 return off;
3577}
3578
3579/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003580 * Append one character to the match leader. May reduce the number of
3581 * matches.
3582 */
3583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003584ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003585{
3586#ifdef FEAT_MBYTE
3587 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003588#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003589
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003590 if (stop_arrow() == FAIL)
3591 return;
3592#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003593 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3594 {
3595 char_u buf[MB_MAXBYTES + 1];
3596
3597 (*mb_char2bytes)(c, buf);
3598 buf[cc] = NUL;
3599 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003600 if (compl_opt_refresh_always)
3601 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003602 }
3603 else
3604#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003605 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003606 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003607 if (compl_opt_refresh_always)
3608 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003609 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003610
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003611 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003612 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003613 ins_compl_restart();
3614
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003615 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003616 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003617 * break redo. */
3618 if (!compl_opt_refresh_always)
3619 {
3620 vim_free(compl_leader);
3621 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003622 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003623 if (compl_leader != NULL)
3624 ins_compl_new_leader();
3625 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003626}
3627
3628/*
3629 * Setup for finding completions again without leaving CTRL-X mode. Used when
3630 * BS or a key was typed while still searching for matches.
3631 */
3632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003633ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003634{
3635 ins_compl_free();
3636 compl_started = FALSE;
3637 compl_matches = 0;
3638 compl_cont_status = 0;
3639 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003640}
3641
3642/*
3643 * Set the first match, the original text.
3644 */
3645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003646ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003647{
3648 char_u *p;
3649
3650 /* Replace the original text entry. */
3651 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3652 {
3653 p = vim_strsave(str);
3654 if (p != NULL)
3655 {
3656 vim_free(compl_first_match->cp_str);
3657 compl_first_match->cp_str = p;
3658 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003659 }
3660}
3661
3662/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003663 * Append one character to the match leader. May reduce the number of
3664 * matches.
3665 */
3666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003667ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003668{
3669 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003670 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003671 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003672 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003673
3674 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003675 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003676 {
3677 /* When still at the original match use the first entry that matches
3678 * the leader. */
3679 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3680 {
3681 p = NULL;
3682 for (cp = compl_shown_match->cp_next; cp != NULL
3683 && cp != compl_first_match; cp = cp->cp_next)
3684 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003685 if (compl_leader == NULL
3686 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003687 (int)STRLEN(compl_leader)))
3688 {
3689 p = cp->cp_str;
3690 break;
3691 }
3692 }
3693 if (p == NULL || (int)STRLEN(p) <= len)
3694 return;
3695 }
3696 else
3697 return;
3698 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003699 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003700 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003701 ins_compl_addleader(c);
3702}
3703
3704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003706 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003707 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003710ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003714 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
3716 /* Forget any previous 'special' messages if this is actually
3717 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3718 */
3719 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3720 edit_submode_extra = NULL;
3721
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003722 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003723 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3724 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003725 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003727 /* Set "compl_get_longest" when finding the first matches. */
3728 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3729 || (ctrl_x_mode == 0 && !compl_started))
3730 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003731 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003732 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003733
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003734 }
3735
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3737 {
3738 /*
3739 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3740 * it will be yet. Now we decide.
3741 */
3742 switch (c)
3743 {
3744 case Ctrl_E:
3745 case Ctrl_Y:
3746 ctrl_x_mode = CTRL_X_SCROLL;
3747 if (!(State & REPLACE_FLAG))
3748 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3749 else
3750 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3751 edit_submode_pre = NULL;
3752 showmode();
3753 break;
3754 case Ctrl_L:
3755 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3756 break;
3757 case Ctrl_F:
3758 ctrl_x_mode = CTRL_X_FILES;
3759 break;
3760 case Ctrl_K:
3761 ctrl_x_mode = CTRL_X_DICTIONARY;
3762 break;
3763 case Ctrl_R:
3764 /* Simply allow ^R to happen without affecting ^X mode */
3765 break;
3766 case Ctrl_T:
3767 ctrl_x_mode = CTRL_X_THESAURUS;
3768 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003769#ifdef FEAT_COMPL_FUNC
3770 case Ctrl_U:
3771 ctrl_x_mode = CTRL_X_FUNCTION;
3772 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003773 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003774 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003775 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003776#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003777 case 's':
3778 case Ctrl_S:
3779 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003780#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003781 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003782 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003783 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003784#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003785 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 case Ctrl_RSB:
3787 ctrl_x_mode = CTRL_X_TAGS;
3788 break;
3789#ifdef FEAT_FIND_ID
3790 case Ctrl_I:
3791 case K_S_TAB:
3792 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3793 break;
3794 case Ctrl_D:
3795 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3796 break;
3797#endif
3798 case Ctrl_V:
3799 case Ctrl_Q:
3800 ctrl_x_mode = CTRL_X_CMDLINE;
3801 break;
3802 case Ctrl_P:
3803 case Ctrl_N:
3804 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3805 * just started ^X mode, or there were enough ^X's to cancel
3806 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3807 * do normal expansion when interrupting a different mode (say
3808 * ^X^F^X^P or ^P^X^X^P, see below)
3809 * nothing changes if interrupting mode 0, (eg, the flag
3810 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003811 if (!(compl_cont_status & CONT_INTRPT))
3812 compl_cont_status |= CONT_LOCAL;
3813 else if (compl_cont_mode != 0)
3814 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 /* FALLTHROUGH */
3816 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003817 /* If we have typed at least 2 ^X's... for modes != 0, we set
3818 * compl_cont_status = 0 (eg, as if we had just started ^X
3819 * mode).
3820 * For mode 0, we set "compl_cont_mode" to an impossible
3821 * value, in both cases ^X^X can be used to restart the same
3822 * mode (avoiding ADDING mode).
3823 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3824 * 'complete' and local ^P expansions respectively.
3825 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3826 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (c == Ctrl_X)
3828 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003829 if (compl_cont_mode != 0)
3830 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003832 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834 ctrl_x_mode = 0;
3835 edit_submode = NULL;
3836 showmode();
3837 break;
3838 }
3839 }
3840 else if (ctrl_x_mode != 0)
3841 {
3842 /* We're already in CTRL-X mode, do we stay in it? */
3843 if (!vim_is_ctrl_x_key(c))
3844 {
3845 if (ctrl_x_mode == CTRL_X_SCROLL)
3846 ctrl_x_mode = 0;
3847 else
3848 ctrl_x_mode = CTRL_X_FINISHED;
3849 edit_submode = NULL;
3850 }
3851 showmode();
3852 }
3853
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003854 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
3856 /* Show error message from attempted keyword completion (probably
3857 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003858 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003860 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3861 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 || ctrl_x_mode == CTRL_X_FINISHED)
3863 {
3864 /* Get here when we have finished typing a sequence of ^N and
3865 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003866 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003867 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
3869 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003870 * If any of the original typed text has been changed, eg when
3871 * ignorecase is set, we must add back-spaces to the redo
3872 * buffer. We add as few as necessary to delete just the part
3873 * of the original text that has changed.
3874 * When using the longest match, edited the match or used
3875 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003877 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3878 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003879 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003880 ptr = NULL;
3881 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883
3884#ifdef FEAT_CINDENT
3885 want_cindent = (can_cindent && cindent_on());
3886#endif
3887 /*
3888 * When completing whole lines: fix indent for 'cindent'.
3889 * Otherwise, break line if it's too long.
3890 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003891 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
3893#ifdef FEAT_CINDENT
3894 /* re-indent the current line */
3895 if (want_cindent)
3896 {
3897 do_c_expr_indent();
3898 want_cindent = FALSE; /* don't do it again */
3899 }
3900#endif
3901 }
3902 else
3903 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003904 int prev_col = curwin->w_cursor.col;
3905
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003907 if (prev_col > 0)
3908 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003909 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003910 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003912 if (prev_col > 0
3913 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3914 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003917 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003918 * the selection without inserting anything. When
3919 * compl_enter_selects is set the Enter key does the same. */
3920 if ((c == Ctrl_Y || (compl_enter_selects
3921 && (c == CAR || c == K_KENTER || c == NL)))
3922 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003923 retval = TRUE;
3924
Bram Moolenaar47247282016-08-02 22:36:02 +02003925 /* CTRL-E means completion is Ended, go back to the typed text.
3926 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003927 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003928 {
3929 ins_compl_delete();
3930 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003931 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003932 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003933 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003934 retval = TRUE;
3935 }
3936
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003937 auto_format(FALSE, TRUE);
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003940 compl_started = FALSE;
3941 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003942 if (!shortmess(SHM_COMPLETIONMENU))
3943 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003945 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if (edit_submode != NULL)
3947 {
3948 edit_submode = NULL;
3949 showmode();
3950 }
3951
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003952#ifdef FEAT_CMDWIN
3953 if (c == Ctrl_C && cmdwin_type != 0)
3954 /* Avoid the popup menu remains displayed when leaving the
3955 * command line window. */
3956 update_screen(0);
3957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958#ifdef FEAT_CINDENT
3959 /*
3960 * Indent now if a key was typed that is in 'cinkeys'.
3961 */
3962 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3963 do_c_expr_indent();
3964#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003965#ifdef FEAT_AUTOCMD
3966 /* Trigger the CompleteDone event to give scripts a chance to act
3967 * upon the completion. */
3968 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003972#ifdef FEAT_AUTOCMD
3973 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3974 /* Trigger the CompleteDone event to give scripts a chance to act
3975 * upon the (possibly failed) completion. */
3976 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
3979 /* reset continue_* if we left expansion-mode, if we stay they'll be
3980 * (re)set properly in ins_complete() */
3981 if (!vim_is_ctrl_x_key(c))
3982 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003983 compl_cont_status = 0;
3984 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003986
3987 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988}
3989
3990/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003991 * Fix the redo buffer for the completion leader replacing some of the typed
3992 * text. This inserts backspaces and appends the changed text.
3993 * "ptr" is the known leader text or NUL.
3994 */
3995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003996ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003997{
3998 int len;
3999 char_u *p;
4000 char_u *ptr = ptr_arg;
4001
4002 if (ptr == NULL)
4003 {
4004 if (compl_leader != NULL)
4005 ptr = compl_leader;
4006 else
4007 return; /* nothing to do */
4008 }
4009 if (compl_orig_text != NULL)
4010 {
4011 p = compl_orig_text;
4012 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4013 ;
4014#ifdef FEAT_MBYTE
4015 if (len > 0)
4016 len -= (*mb_head_off)(p, p + len);
4017#endif
4018 for (p += len; *p != NUL; mb_ptr_adv(p))
4019 AppendCharToRedobuff(K_BS);
4020 }
4021 else
4022 len = 0;
4023 if (ptr != NULL)
4024 AppendToRedobuffLit(ptr + len, -1);
4025}
4026
4027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4029 * (depending on flag) starting from buf and looking for a non-scanned
4030 * buffer (other than curbuf). curbuf is special, if it is called with
4031 * buf=curbuf then it has to be the first call for a given flag/expansion.
4032 *
4033 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4034 */
4035 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004036ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037{
4038#ifdef FEAT_WINDOWS
4039 static win_T *wp;
4040#endif
4041
4042 if (flag == 'w') /* just windows */
4043 {
4044#ifdef FEAT_WINDOWS
4045 if (buf == curbuf) /* first call for this flag/expansion */
4046 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004047 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 && wp->w_buffer->b_scanned)
4049 ;
4050 buf = wp->w_buffer;
4051#else
4052 buf = curbuf;
4053#endif
4054 }
4055 else
4056 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4057 * (unlisted buffers)
4058 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004059 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 && ((flag == 'U'
4061 ? buf->b_p_bl
4062 : (!buf->b_p_bl
4063 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004064 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 ;
4066 return buf;
4067}
4068
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004069#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004070/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004071 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004072 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004073 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004075expand_by_function(
4076 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4077 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004078{
Bram Moolenaar82139082011-09-14 16:52:09 +02004079 list_T *matchlist = NULL;
4080 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004081 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004082 char_u *funcname;
4083 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004084 win_T *curwin_save;
4085 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004086 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004087
Bram Moolenaare344bea2005-09-01 20:46:49 +00004088 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4089 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004090 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004091
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004092 /* Call 'completefunc' to obtain the list of matches. */
4093 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004094 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004095
Bram Moolenaare344bea2005-09-01 20:46:49 +00004096 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004097 curwin_save = curwin;
4098 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004099
4100 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004101 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004102 {
4103 switch (rettv.v_type)
4104 {
4105 case VAR_LIST:
4106 matchlist = rettv.vval.v_list;
4107 break;
4108 case VAR_DICT:
4109 matchdict = rettv.vval.v_dict;
4110 break;
4111 default:
4112 /* TODO: Give error message? */
4113 clear_tv(&rettv);
4114 break;
4115 }
4116 }
4117
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004118 if (curwin_save != curwin || curbuf_save != curbuf)
4119 {
4120 EMSG(_(e_complwin));
4121 goto theend;
4122 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004123 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004124 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004125 if (!equalpos(curwin->w_cursor, pos))
4126 {
4127 EMSG(_(e_compldel));
4128 goto theend;
4129 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004130
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004131 if (matchlist != NULL)
4132 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004133 else if (matchdict != NULL)
4134 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004135
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004136theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004137 if (matchdict != NULL)
4138 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004139 if (matchlist != NULL)
4140 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004141}
4142#endif /* FEAT_COMPL_FUNC */
4143
Bram Moolenaar39f05632006-03-19 22:15:26 +00004144#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004145/*
4146 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004147 */
4148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004149ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004150{
4151 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004152 int dir = compl_direction;
4153
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004154 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004155 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004156 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004157 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4158 /* if dir was BACKWARD then honor it just once */
4159 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004160 else if (did_emsg)
4161 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004162 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004163}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004164
4165/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004166 * Add completions from a dict.
4167 */
4168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004169ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004170{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004171 dictitem_T *di_refresh;
4172 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004173
4174 /* Check for optional "refresh" item. */
4175 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004176 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4177 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004178 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004179 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004180
4181 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4182 compl_opt_refresh_always = TRUE;
4183 }
4184
4185 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004186 di_words = dict_find(dict, (char_u *)"words", 5);
4187 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4188 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004189}
4190
4191/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004192 * Add a match to the list of matches from a typeval_T.
4193 * If the given string is already in the list of completions, then return
4194 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4195 * maybe because alloc() returns NULL, then FAIL is returned.
4196 */
4197 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004198ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004199{
4200 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004201 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004202 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004203 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004204 char_u *(cptext[CPT_COUNT]);
4205
4206 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4207 {
4208 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4209 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4210 (char_u *)"abbr", FALSE);
4211 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4212 (char_u *)"menu", FALSE);
4213 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4214 (char_u *)"kind", FALSE);
4215 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4216 (char_u *)"info", FALSE);
4217 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4218 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004219 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004220 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004221 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4222 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004223 }
4224 else
4225 {
4226 word = get_tv_string_chk(tv);
4227 vim_memset(cptext, 0, sizeof(cptext));
4228 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004229 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004230 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004231 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004232}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004233#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004234
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004235/*
4236 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004237 * The search starts at position "ini" in curbuf and in the direction
4238 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004239 * When "compl_started" is FALSE start at that position, otherwise continue
4240 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241 * This may return before finding all the matches.
4242 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 */
4244 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004245ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246{
4247 static pos_T first_match_pos;
4248 static pos_T last_match_pos;
4249 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004250 static int found_all = FALSE; /* Found all matches of a
4251 certain type. */
4252 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
Bram Moolenaar572cb562005-08-05 21:35:02 +00004254 pos_T *pos;
4255 char_u **matches;
4256 int save_p_scs;
4257 int save_p_ws;
4258 int save_p_ic;
4259 int i;
4260 int num_matches;
4261 int len;
4262 int found_new_match;
4263 int type = ctrl_x_mode;
4264 char_u *ptr;
4265 char_u *dict = NULL;
4266 int dict_f = 0;
4267 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004268 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004270 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004272 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 ins_buf->b_scanned = 0;
4274 found_all = FALSE;
4275 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004276 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004277 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 last_match_pos = first_match_pos = *ini;
4279 }
4280
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004281 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004282 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4284 for (;;)
4285 {
4286 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004287 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004289 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 * or if found_all says this entry is done. For ^X^L only use the
4291 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004292 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004293 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 {
4295 found_all = FALSE;
4296 while (*e_cpt == ',' || *e_cpt == ' ')
4297 e_cpt++;
4298 if (*e_cpt == '.' && !curbuf->b_scanned)
4299 {
4300 ins_buf = curbuf;
4301 first_match_pos = *ini;
4302 /* So that ^N can match word immediately after cursor */
4303 if (ctrl_x_mode == 0)
4304 dec(&first_match_pos);
4305 last_match_pos = first_match_pos;
4306 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004307
4308 /* Remember the first match so that the loop stops when we
4309 * wrap and come back there a second time. */
4310 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
4312 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4313 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4314 {
4315 /* Scan a buffer, but not the current one. */
4316 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4317 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004318 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 first_match_pos.col = last_match_pos.col = 0;
4320 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4321 last_match_pos.lnum = 0;
4322 type = 0;
4323 }
4324 else /* unloaded buffer, scan like dictionary */
4325 {
4326 found_all = TRUE;
4327 if (ins_buf->b_fname == NULL)
4328 continue;
4329 type = CTRL_X_DICTIONARY;
4330 dict = ins_buf->b_fname;
4331 dict_f = DICT_EXACT;
4332 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004333 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 ins_buf->b_fname == NULL
4335 ? buf_spname(ins_buf)
4336 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004337 ? ins_buf->b_fname
4338 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004339 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341 else if (*e_cpt == NUL)
4342 break;
4343 else
4344 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004345 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 type = -1;
4347 else if (*e_cpt == 'k' || *e_cpt == 's')
4348 {
4349 if (*e_cpt == 'k')
4350 type = CTRL_X_DICTIONARY;
4351 else
4352 type = CTRL_X_THESAURUS;
4353 if (*++e_cpt != ',' && *e_cpt != NUL)
4354 {
4355 dict = e_cpt;
4356 dict_f = DICT_FIRST;
4357 }
4358 }
4359#ifdef FEAT_FIND_ID
4360 else if (*e_cpt == 'i')
4361 type = CTRL_X_PATH_PATTERNS;
4362 else if (*e_cpt == 'd')
4363 type = CTRL_X_PATH_DEFINES;
4364#endif
4365 else if (*e_cpt == ']' || *e_cpt == 't')
4366 {
4367 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004368 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004369 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371 else
4372 type = -1;
4373
4374 /* in any case e_cpt is advanced to the next entry */
4375 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4376
4377 found_all = TRUE;
4378 if (type == -1)
4379 continue;
4380 }
4381 }
4382
4383 switch (type)
4384 {
4385 case -1:
4386 break;
4387#ifdef FEAT_FIND_ID
4388 case CTRL_X_PATH_PATTERNS:
4389 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004390 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4395 (linenr_T)1, (linenr_T)MAXLNUM);
4396 break;
4397#endif
4398
4399 case CTRL_X_DICTIONARY:
4400 case CTRL_X_THESAURUS:
4401 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004402 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 : (type == CTRL_X_THESAURUS
4404 ? (*curbuf->b_p_tsr == NUL
4405 ? p_tsr
4406 : curbuf->b_p_tsr)
4407 : (*curbuf->b_p_dict == NUL
4408 ? p_dict
4409 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004410 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004411 dict != NULL ? dict_f
4412 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 dict = NULL;
4414 break;
4415
4416 case CTRL_X_TAGS:
4417 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4418 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004419 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004421 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004422 * of matches is found when compl_pattern is empty */
4423 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4425 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4426 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4427 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004428 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
4430 p_ic = save_p_ic;
4431 break;
4432
4433 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004434 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4436 {
4437
4438 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004439 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004440 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
4442 break;
4443
4444 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004445 if (expand_cmdline(&compl_xp, compl_pattern,
4446 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004448 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 break;
4450
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004451#ifdef FEAT_COMPL_FUNC
4452 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004453 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004454 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004455 break;
4456#endif
4457
Bram Moolenaar488c6512005-08-11 20:09:58 +00004458 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004459#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004460 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004461 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004462 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004463 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004464#endif
4465 break;
4466
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 default: /* normal ^P/^N and ^X^L */
4468 /*
4469 * If 'infercase' is set, don't use 'smartcase' here
4470 */
4471 save_p_scs = p_scs;
4472 if (ins_buf->b_p_inf)
4473 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004474
Bram Moolenaar361aa502014-01-23 22:45:58 +01004475 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 * end but never from the middle, thus setting nowrapscan in this
4477 * buffers is a good idea, on the other hand, we always set
4478 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4479 save_p_ws = p_ws;
4480 if (ins_buf != curbuf)
4481 p_ws = FALSE;
4482 else if (*e_cpt == '.')
4483 p_ws = TRUE;
4484 for (;;)
4485 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004486 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004488 ++msg_silent; /* Don't want messages for wrapscan. */
4489
Bram Moolenaare4214502015-03-05 18:08:43 +01004490 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4491 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004492 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004493 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004494 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004496 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004498 found_new_match = searchit(NULL, ins_buf, pos,
4499 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004500 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004501 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004502 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004503 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004505 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004506 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 first_match_pos = *pos;
4508 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004509 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
4511 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004512 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 found_new_match = FAIL;
4514 if (found_new_match == FAIL)
4515 {
4516 if (ins_buf == curbuf)
4517 found_all = TRUE;
4518 break;
4519 }
4520
4521 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004522 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 && ini->lnum == pos->lnum
4524 && ini->col == pos->col)
4525 continue;
4526 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004527 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004529 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
4531 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4532 continue;
4533 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4534 if (!p_paste)
4535 ptr = skipwhite(ptr);
4536 }
4537 len = (int)STRLEN(ptr);
4538 }
4539 else
4540 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004541 char_u *tmp_ptr = ptr;
4542
4543 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004545 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 /* Skip if already inside a word. */
4547 if (vim_iswordp(tmp_ptr))
4548 continue;
4549 /* Find start of next word. */
4550 tmp_ptr = find_word_start(tmp_ptr);
4551 }
4552 /* Find end of this word. */
4553 tmp_ptr = find_word_end(tmp_ptr);
4554 len = (int)(tmp_ptr - ptr);
4555
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004556 if ((compl_cont_status & CONT_ADDING)
4557 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
4559 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4560 {
4561 /* Try next line, if any. the new word will be
4562 * "join" as if the normal command "J" was used.
4563 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 * works -- Acevedo */
4566 STRNCPY(IObuff, ptr, len);
4567 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4568 tmp_ptr = ptr = skipwhite(ptr);
4569 /* Find start of next word. */
4570 tmp_ptr = find_word_start(tmp_ptr);
4571 /* Find end of next word. */
4572 tmp_ptr = find_word_end(tmp_ptr);
4573 if (tmp_ptr > ptr)
4574 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004575 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004577 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 IObuff[len++] = ' ';
4579 /* IObuf =~ "\k.* ", thus len >= 2 */
4580 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004581 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 || (vim_strchr(p_cpo, CPO_JOINSP)
4583 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004584 && (IObuff[len - 2] == '?'
4585 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 IObuff[len++] = ' ';
4587 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004588 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (tmp_ptr - ptr >= IOSIZE - len)
4590 tmp_ptr = ptr + IOSIZE - len - 1;
4591 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4592 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004593 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
4595 IObuff[len] = NUL;
4596 ptr = IObuff;
4597 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004598 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 continue;
4600 }
4601 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004602 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004603 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004604 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 found_new_match = OK;
4607 break;
4608 }
4609 }
4610 p_scs = save_p_scs;
4611 p_ws = save_p_ws;
4612 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004613
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004614 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004615 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004616 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 found_new_match = OK;
4618
4619 /* break the loop for specialized modes (use 'complete' just for the
4620 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004621 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004622 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004623 {
4624 if (got_int)
4625 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004626 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004627 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004628 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004629
Bram Moolenaare4214502015-03-05 18:08:43 +01004630 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004631 || compl_interrupted)
4632 break;
4633 compl_started = TRUE;
4634 }
4635 else
4636 {
4637 /* Mark a buffer scanned when it has been scanned completely */
4638 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4639 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004641 compl_started = FALSE;
4642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004644 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
Bram Moolenaare4214502015-03-05 18:08:43 +01004646 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 && *e_cpt == NUL) /* Got to end of 'complete' */
4648 found_new_match = FAIL;
4649
4650 i = -1; /* total of matches, unknown */
4651 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004652 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 i = ins_compl_make_cyclic();
4654
4655 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004656 * just been made cyclic then we have to move compl_curr_match to the next
4657 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004658 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4659 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004660 if (compl_curr_match == NULL)
4661 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 return i;
4663}
4664
4665/* Delete the old text being completed. */
4666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004667ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004669 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
4671 /*
4672 * In insert mode: Delete the typed part.
4673 * In replace mode: Put the old characters back, if any.
4674 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004675 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4676 if ((int)curwin->w_cursor.col > col)
4677 {
4678 if (stop_arrow() == FAIL)
4679 return;
4680 backspace_until_column(col);
4681 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004682
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004683 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4684 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004686 /* clear v:completed_item */
4687 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688}
4689
Bram Moolenaar472e8592016-10-15 17:06:47 +02004690/*
4691 * Insert the new text being completed.
4692 * "in_compl_func" is TRUE when called from complete_check().
4693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004695ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004697 dict_T *dict;
4698
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004699 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004700 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4701 compl_used_match = FALSE;
4702 else
4703 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004704
4705 /* Set completed item. */
4706 /* { word, abbr, menu, kind, info } */
4707 dict = dict_alloc();
4708 if (dict != NULL)
4709 {
4710 dict_add_nr_str(dict, "word", 0L,
4711 EMPTY_IF_NULL(compl_shown_match->cp_str));
4712 dict_add_nr_str(dict, "abbr", 0L,
4713 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4714 dict_add_nr_str(dict, "menu", 0L,
4715 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4716 dict_add_nr_str(dict, "kind", 0L,
4717 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4718 dict_add_nr_str(dict, "info", 0L,
4719 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4720 }
4721 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004722 if (!in_compl_func)
4723 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724}
4725
4726/*
4727 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004728 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4729 * get more completions. If it is FALSE, then we just do nothing when there
4730 * are no more completions in a given direction. The latter case is used when
4731 * we are still in the middle of finding completions, to allow browsing
4732 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 * Return the total number of matches, or -1 if still unknown -- webb.
4734 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004735 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4736 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 *
4738 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004739 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4740 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 */
4742 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004743ins_compl_next(
4744 int allow_get_expansion,
4745 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004746 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004747 int insert_match, /* Insert the newly selected match */
4748 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 int num_matches = -1;
4751 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004752 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004753 compl_T *found_compl = NULL;
4754 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004755 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004756 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004758 /* When user complete function return -1 for findstart which is next
4759 * time of 'always', compl_shown_match become NULL. */
4760 if (compl_shown_match == NULL)
4761 return -1;
4762
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004763 if (compl_leader != NULL
4764 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004766 /* Set "compl_shown_match" to the actually shown match, it may differ
4767 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004768 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004769 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004770 && compl_shown_match->cp_next != NULL
4771 && compl_shown_match->cp_next != compl_first_match)
4772 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004773
4774 /* If we didn't find it searching forward, and compl_shows_dir is
4775 * backward, find the last match. */
4776 if (compl_shows_dir == BACKWARD
4777 && !ins_compl_equal(compl_shown_match,
4778 compl_leader, (int)STRLEN(compl_leader))
4779 && (compl_shown_match->cp_next == NULL
4780 || compl_shown_match->cp_next == compl_first_match))
4781 {
4782 while (!ins_compl_equal(compl_shown_match,
4783 compl_leader, (int)STRLEN(compl_leader))
4784 && compl_shown_match->cp_prev != NULL
4785 && compl_shown_match->cp_prev != compl_first_match)
4786 compl_shown_match = compl_shown_match->cp_prev;
4787 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004788 }
4789
4790 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004791 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 /* Delete old text to be replaced */
4793 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004794
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004795 /* When finding the longest common text we stick at the original text,
4796 * don't let CTRL-N or CTRL-P move to the first match. */
4797 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4798
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004799 /* When restarting the search don't insert the first match either. */
4800 if (compl_restarting)
4801 {
4802 advance = FALSE;
4803 compl_restarting = FALSE;
4804 }
4805
Bram Moolenaare3226be2005-12-18 22:10:00 +00004806 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4807 * around. */
4808 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004810 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004812 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004813 found_end = (compl_first_match != NULL
4814 && (compl_shown_match->cp_next == compl_first_match
4815 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004816 }
4817 else if (compl_shows_dir == BACKWARD
4818 && compl_shown_match->cp_prev != NULL)
4819 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004820 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004821 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004822 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
4824 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004825 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004826 if (!allow_get_expansion)
4827 {
4828 if (advance)
4829 {
4830 if (compl_shows_dir == BACKWARD)
4831 compl_pending -= todo + 1;
4832 else
4833 compl_pending += todo + 1;
4834 }
4835 return -1;
4836 }
4837
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004838 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004839 {
4840 if (compl_shows_dir == BACKWARD)
4841 --compl_pending;
4842 else
4843 ++compl_pending;
4844 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004845
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004846 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004847 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004848
4849 /* handle any pending completions */
4850 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004851 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004852 {
4853 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4854 {
4855 compl_shown_match = compl_shown_match->cp_next;
4856 --compl_pending;
4857 }
4858 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4859 {
4860 compl_shown_match = compl_shown_match->cp_prev;
4861 ++compl_pending;
4862 }
4863 else
4864 break;
4865 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004866 found_end = FALSE;
4867 }
4868 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4869 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004870 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004871 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004872 ++todo;
4873 else
4874 /* Remember a matching item. */
4875 found_compl = compl_shown_match;
4876
4877 /* Stop at the end of the list when we found a usable match. */
4878 if (found_end)
4879 {
4880 if (found_compl != NULL)
4881 {
4882 compl_shown_match = found_compl;
4883 break;
4884 }
4885 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004889 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004890 if (compl_no_insert && !started)
4891 {
4892 ins_bytes(compl_orig_text + ins_compl_len());
4893 compl_used_match = FALSE;
4894 }
4895 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004896 {
4897 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004898 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004899 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004900 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004901 }
4902 else
4903 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904
4905 if (!allow_get_expansion)
4906 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004907 /* may undisplay the popup menu first */
4908 ins_compl_upd_pum();
4909
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004910 /* redraw to show the user what was inserted */
4911 update_screen(0);
4912
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004913 /* display the updated popup menu */
4914 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004915#ifdef FEAT_GUI
4916 if (gui.in_use)
4917 {
4918 /* Show the cursor after the match, not after the redrawn text. */
4919 setcursor();
4920 out_flush();
4921 gui_update_cursor(FALSE, FALSE);
4922 }
4923#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004924
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 /* Delete old text to be replaced, since we're still searching and
4926 * don't want to match ourselves! */
4927 ins_compl_delete();
4928 }
4929
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004930 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004931 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004932 if (compl_no_insert && !started)
4933 compl_enter_selects = TRUE;
4934 else
4935 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004936
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 /*
4938 * Show the file name for the match (if any)
4939 * Truncate the file name to avoid a wait for return.
4940 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004941 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
4943 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004944 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 if (i <= 0)
4946 i = 0;
4947 else
4948 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004949 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 msg(IObuff);
4951 redraw_cmdline = FALSE; /* don't overwrite! */
4952 }
4953
4954 return num_matches;
4955}
4956
4957/*
4958 * Call this while finding completions, to check whether the user has hit a key
4959 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004960 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004962 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004963 * "in_compl_func" is TRUE when called from complete_check(), don't set
4964 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 */
4966 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004967ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968{
4969 static int count = 0;
4970
4971 int c;
4972
4973 /* Don't check when reading keys from a script. That would break the test
4974 * scripts */
4975 if (using_script())
4976 return;
4977
4978 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004979 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 return;
4981 count = 0;
4982
Bram Moolenaara260a972006-06-23 19:36:29 +00004983 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4984 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 if (c != NUL)
4987 {
4988 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4989 {
4990 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004991 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004992 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02004993 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004995 else
4996 {
4997 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004998 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004999 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005000 if (c != K_IGNORE)
5001 {
5002 /* Don't interrupt completion when the character wasn't typed,
5003 * e.g., when doing @q to replay keys. */
5004 if (c != Ctrl_R && KeyTyped)
5005 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005006
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005007 vungetc(c);
5008 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005011 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005012 {
5013 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5014
5015 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005016 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005017 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005018}
5019
5020/*
5021 * Decide the direction of Insert mode complete from the key typed.
5022 * Returns BACKWARD or FORWARD.
5023 */
5024 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005025ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005026{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005027 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005028 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005029 return BACKWARD;
5030 return FORWARD;
5031}
5032
5033/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005034 * Return TRUE for keys that are used for completion only when the popup menu
5035 * is visible.
5036 */
5037 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005038ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005039{
5040 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005041 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5042 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005043}
5044
5045/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005046 * Decide the number of completions to move forward.
5047 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5048 */
5049 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005050ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005051{
5052 int h;
5053
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005054 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005055 {
5056 h = pum_get_height();
5057 if (h > 3)
5058 h -= 2; /* keep some context */
5059 return h;
5060 }
5061 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062}
5063
5064/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005065 * Return TRUE if completion with "c" should insert the match, FALSE if only
5066 * to change the currently selected completion.
5067 */
5068 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005069ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005070{
5071 switch (c)
5072 {
5073 case K_UP:
5074 case K_DOWN:
5075 case K_PAGEDOWN:
5076 case K_KPAGEDOWN:
5077 case K_S_DOWN:
5078 case K_PAGEUP:
5079 case K_KPAGEUP:
5080 case K_S_UP:
5081 return FALSE;
5082 }
5083 return TRUE;
5084}
5085
5086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 * Do Insert mode completion.
5088 * Called when character "c" was typed, which has a meaning for completion.
5089 * Returns OK if completion was done, FAIL if something failed (out of mem).
5090 */
5091 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005092ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005094 char_u *line;
5095 int startcol = 0; /* column where searched text starts */
5096 colnr_T curs_col; /* cursor column */
5097 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005098 int save_w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005099 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005100 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
Bram Moolenaare3226be2005-12-18 22:10:00 +00005102 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005103 insert_match = ins_compl_use_match(c);
5104
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005105 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
5107 /* First time we hit ^N or ^P (in a row, I mean) */
5108
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 did_ai = FALSE;
5110#ifdef FEAT_SMARTINDENT
5111 did_si = FALSE;
5112 can_si = FALSE;
5113 can_si_back = FALSE;
5114#endif
5115 if (stop_arrow() == FAIL)
5116 return FAIL;
5117
5118 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005119 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005120 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005122 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005123 * "compl_startpos" to the cursor as a pattern to add a new word
5124 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005125 * "compl_startpos" is not in the same line as the cursor then fix it
5126 * (the line has been split because it was longer than 'tw'). if SOL
5127 * is set then skip the previous pattern, a word at the beginning of
5128 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005129 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5130 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {
5132 /*
5133 * it is a continued search
5134 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005135 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5137 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5138 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005139 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005141 /* line (probably) wrapped, set compl_startpos to the
5142 * first non_blank in the line, if it is not a wordchar
5143 * include it to get a better pattern, but then we don't
5144 * want the "\\<" prefix, check it bellow */
5145 compl_col = (colnr_T)(skipwhite(line) - line);
5146 compl_startpos.col = compl_col;
5147 compl_startpos.lnum = curwin->w_cursor.lnum;
5148 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150 else
5151 {
5152 /* S_IPOS was set when we inserted a word that was at the
5153 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005154 * mode but first we need to redefine compl_startpos */
5155 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005157 compl_cont_status |= CONT_SOL;
5158 compl_startpos.col = (colnr_T)(skipwhite(
5159 line + compl_length
5160 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005162 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005164 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005165 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005166 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005168 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005170 compl_cont_status &= ~CONT_SOL;
5171 compl_length = (IOSIZE - MIN_SPACE);
5172 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005174 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5175 if (compl_length < 1)
5176 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005178 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005179 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005181 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
5183 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005184 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005186 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005188 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 compl_cont_status = 0;
5191 compl_cont_status |= CONT_N_ADDS;
5192 compl_startpos = curwin->w_cursor;
5193 startcol = (int)curs_col;
5194 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196
5197 /* Work out completion pattern and original text -- webb */
5198 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5199 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005200 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5202 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005203 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005205 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005207 compl_col += ++startcol;
5208 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005211 compl_pattern = str_foldcase(line + compl_col,
5212 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005214 compl_pattern = vim_strnsave(line + compl_col,
5215 compl_length);
5216 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 return FAIL;
5218 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005219 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
5221 char_u *prefix = (char_u *)"\\<";
5222
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005223 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005224 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005225 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005226 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 if (!vim_iswordp(line + compl_col)
5229 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 && (
5231#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005232 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005234 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235#endif
5236 )))
5237 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005238 STRCPY((char *)compl_pattern, prefix);
5239 (void)quote_meta(compl_pattern + STRLEN(prefix),
5240 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005242 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005246 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247#endif
5248 )
5249 {
5250 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005251 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5252 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005254 compl_col += curs_col;
5255 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
5257 else
5258 {
5259#ifdef FEAT_MBYTE
5260 /* Search the point of change class of multibyte character
5261 * or not a word single byte character backward. */
5262 if (has_mbyte)
5263 {
5264 int base_class;
5265 int head_off;
5266
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005267 startcol -= (*mb_head_off)(line, line + startcol);
5268 base_class = mb_get_class(line + startcol);
5269 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005271 head_off = (*mb_head_off)(line, line + startcol);
5272 if (base_class != mb_get_class(line + startcol
5273 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005275 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 }
5277 }
5278 else
5279#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005282 compl_col += ++startcol;
5283 compl_length = (int)curs_col - startcol;
5284 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 {
5286 /* Only match word with at least two chars -- webb
5287 * there's no need to call quote_meta,
5288 * alloc(7) is enough -- Acevedo
5289 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005290 compl_pattern = alloc(7);
5291 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005293 STRCPY((char *)compl_pattern, "\\<");
5294 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5295 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
5297 else
5298 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005299 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005300 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005303 STRCPY((char *)compl_pattern, "\\<");
5304 (void)quote_meta(compl_pattern + 2, line + compl_col,
5305 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 }
5307 }
5308 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005309 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005311 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005312 compl_length = (int)curs_col - (int)compl_col;
5313 if (compl_length < 0) /* cursor in indent: empty pattern */
5314 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005316 compl_pattern = str_foldcase(line + compl_col, compl_length,
5317 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005319 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5320 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 return FAIL;
5322 }
5323 else if (ctrl_x_mode == CTRL_X_FILES)
5324 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005325 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005326 if (startcol > 0)
5327 {
5328 char_u *p = line + startcol;
5329
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005330 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005331 while (p > line && vim_isfilec(PTR2CHAR(p)))
5332 mb_ptr_back(line, p);
5333 if (p == line && vim_isfilec(PTR2CHAR(p)))
5334 startcol = 0;
5335 else
5336 startcol = (int)(p - line) + 1;
5337 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005338
Bram Moolenaar0300e462013-09-08 16:03:45 +02005339 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005340 compl_length = (int)curs_col - startcol;
5341 compl_pattern = addstar(line + compl_col, compl_length,
5342 EXPAND_FILES);
5343 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 return FAIL;
5345 }
5346 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5347 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 compl_pattern = vim_strnsave(line, curs_col);
5349 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005351 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005352 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005353 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5354 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005355 /* No completion possible, use an empty pattern to get a
5356 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005357 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005358 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005359 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5360 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005362 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005363 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005364#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005365 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005366 * Call user defined function 'completefunc' with "a:findstart"
5367 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005368 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005369 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005370 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005371 char_u *funcname;
5372 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005373 win_T *curwin_save;
5374 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005375
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005376 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005377 * string */
5378 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5379 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5380 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005381 {
5382 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5383 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005384 /* restore did_ai, so that adding comment leader works */
5385 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005386 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005387 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005388
5389 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005390 args[1] = NULL;
5391 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005392 curwin_save = curwin;
5393 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005394 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005395 if (curwin_save != curwin || curbuf_save != curbuf)
5396 {
5397 EMSG(_(e_complwin));
5398 return FAIL;
5399 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005400 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005401 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005402 if (!equalpos(curwin->w_cursor, pos))
5403 {
5404 EMSG(_(e_compldel));
5405 return FAIL;
5406 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005407
Bram Moolenaar6110a002012-01-26 18:58:38 +01005408 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005409 * cancel the complete without an error.
5410 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005411 if (col == -2)
5412 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005413 if (col == -3)
5414 {
5415 ctrl_x_mode = 0;
5416 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005417 if (!shortmess(SHM_COMPLETIONMENU))
5418 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005419 return FAIL;
5420 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005421
Bram Moolenaar82139082011-09-14 16:52:09 +02005422 /*
5423 * Reset extended parameters of completion, when start new
5424 * completion.
5425 */
5426 compl_opt_refresh_always = FALSE;
5427
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005428 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005429 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005430 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005431 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005432 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005433
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005434 /* Setup variables for completion. Need to obtain "line" again,
5435 * it may have become invalid. */
5436 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005437 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005438 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5439 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005440#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005441 return FAIL;
5442 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005443 else if (ctrl_x_mode == CTRL_X_SPELL)
5444 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005445#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005446 if (spell_bad_len > 0)
5447 compl_col = curs_col - spell_bad_len;
5448 else
5449 compl_col = spell_word_start(startcol);
5450 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005451 {
5452 compl_length = 0;
5453 compl_col = curs_col;
5454 }
5455 else
5456 {
5457 spell_expand_check_cap(compl_col);
5458 compl_length = (int)curs_col - compl_col;
5459 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005460 /* Need to obtain "line" again, it may have become invalid. */
5461 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005462 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5463 if (compl_pattern == NULL)
5464#endif
5465 return FAIL;
5466 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005467 else
5468 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005469 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005470 return FAIL;
5471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005473 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 {
5475 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005476 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 {
5478 /* Insert a new line, keep indentation but ignore 'comments' */
5479#ifdef FEAT_COMMENTS
5480 char_u *old = curbuf->b_p_com;
5481
5482 curbuf->b_p_com = (char_u *)"";
5483#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005484 compl_startpos.lnum = curwin->w_cursor.lnum;
5485 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 ins_eol('\r');
5487#ifdef FEAT_COMMENTS
5488 curbuf->b_p_com = old;
5489#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005490 compl_length = 0;
5491 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
5493 }
5494 else
5495 {
5496 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005497 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
5499
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005500 if (compl_cont_status & CONT_LOCAL)
5501 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 else
5503 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5504
Bram Moolenaar62951b12011-09-21 18:23:05 +02005505 /* If any of the original typed text has been changed we need to fix
5506 * the redo buffer. */
5507 ins_compl_fixRedoBufForLeader(NULL);
5508
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005509 /* Always add completion for the original text. */
5510 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005511 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5512 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005513 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005515 vim_free(compl_pattern);
5516 compl_pattern = NULL;
5517 vim_free(compl_orig_text);
5518 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 return FAIL;
5520 }
5521
5522 /* showmode might reset the internal line pointers, so it must
5523 * be called before line = ml_get(), or when this address is no
5524 * longer needed. -- Acevedo.
5525 */
5526 edit_submode_extra = (char_u *)_("-- Searching...");
5527 edit_submode_highl = HLF_COUNT;
5528 showmode();
5529 edit_submode_extra = NULL;
5530 out_flush();
5531 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005532 else if (insert_match && stop_arrow() == FAIL)
5533 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005535 compl_shown_match = compl_curr_match;
5536 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
5538 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005539 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005541 save_w_wrow = curwin->w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005542 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005544 /* may undisplay the popup menu */
5545 ins_compl_upd_pum();
5546
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005547 if (n > 1) /* all matches have been found */
5548 compl_matches = n;
5549 compl_curr_match = compl_shown_match;
5550 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
Bram Moolenaard68071d2006-05-02 22:08:30 +00005552 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5553 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 if (got_int && !global_busy)
5555 {
5556 (void)vgetc();
5557 got_int = FALSE;
5558 }
5559
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005560 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005561 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005563 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5564 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5566 edit_submode_highl = HLF_E;
5567 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5568 * because we couldn't expand anything at first place, but if we used
5569 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5570 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005571 if ( compl_length > 1
5572 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 || (ctrl_x_mode != 0
5574 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5575 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005576 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578
Bram Moolenaar572cb562005-08-05 21:35:02 +00005579 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005580 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005582 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583
5584 if (edit_submode_extra == NULL)
5585 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005586 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
5588 edit_submode_extra = (char_u *)_("Back at original");
5589 edit_submode_highl = HLF_W;
5590 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005591 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 {
5593 edit_submode_extra = (char_u *)_("Word from other line");
5594 edit_submode_highl = HLF_COUNT;
5595 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005596 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 {
5598 edit_submode_extra = (char_u *)_("The only match");
5599 edit_submode_highl = HLF_COUNT;
5600 }
5601 else
5602 {
5603 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005604 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005606 int number = 0;
5607 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005609 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 {
5611 /* search backwards for the first valid (!= -1) number.
5612 * This should normally succeed already at the first loop
5613 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005614 for (match = compl_curr_match->cp_prev; match != NULL
5615 && match != compl_first_match;
5616 match = match->cp_prev)
5617 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005619 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 break;
5621 }
5622 if (match != NULL)
5623 /* go up and assign all numbers which are not assigned
5624 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005625 for (match = match->cp_next;
5626 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005627 match = match->cp_next)
5628 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630 else /* BACKWARD */
5631 {
5632 /* search forwards (upwards) for the first valid (!= -1)
5633 * number. This should normally succeed already at the
5634 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005635 for (match = compl_curr_match->cp_next; match != NULL
5636 && match != compl_first_match;
5637 match = match->cp_next)
5638 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005640 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 break;
5642 }
5643 if (match != NULL)
5644 /* go down and assign all numbers which are not
5645 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005646 for (match = match->cp_prev; match
5647 && match->cp_number == -1;
5648 match = match->cp_prev)
5649 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 }
5651 }
5652
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005653 /* The match should always have a sequence number now, this is
5654 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005655 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005657 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5658 * Translations may need more than twice that. */
5659 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005661 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005662 vim_snprintf((char *)match_ref, sizeof(match_ref),
5663 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005664 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005666 vim_snprintf((char *)match_ref, sizeof(match_ref),
5667 _("match %d"),
5668 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 edit_submode_extra = match_ref;
5670 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005671 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 curs_columns(FALSE);
5673 }
5674 }
5675 }
5676
5677 /* Show a message about what (completion) mode we're in. */
5678 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005679 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005681 if (edit_submode_extra != NULL)
5682 {
5683 if (!p_smd)
5684 msg_attr(edit_submode_extra,
5685 edit_submode_highl < HLF_COUNT
5686 ? hl_attr(edit_submode_highl) : 0);
5687 }
5688 else
5689 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691
Bram Moolenaard68071d2006-05-02 22:08:30 +00005692 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005693 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005694 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005695 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005696 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005697 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005698 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005699
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 return OK;
5701}
5702
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005703 static void
5704show_pum(int save_w_wrow)
5705{
5706 /* RedrawingDisabled may be set when invoked through complete(). */
5707 int n = RedrawingDisabled;
5708
5709 RedrawingDisabled = 0;
5710
5711 /* If the cursor moved we need to remove the pum first. */
5712 setcursor();
5713 if (save_w_wrow != curwin->w_wrow)
5714 ins_compl_del_pum();
5715
5716 ins_compl_show_pum();
5717 setcursor();
5718 RedrawingDisabled = n;
5719}
5720
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721/*
5722 * Looks in the first "len" chars. of "src" for search-metachars.
5723 * If dest is not NULL the chars. are copied there quoting (with
5724 * a backslash) the metachars, and dest would be NUL terminated.
5725 * Returns the length (needed) of dest
5726 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005727 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005728quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005730 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005732 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 {
5734 switch (*src)
5735 {
5736 case '.':
5737 case '*':
5738 case '[':
5739 if (ctrl_x_mode == CTRL_X_DICTIONARY
5740 || ctrl_x_mode == CTRL_X_THESAURUS)
5741 break;
5742 case '~':
5743 if (!p_magic) /* quote these only if magic is set */
5744 break;
5745 case '\\':
5746 if (ctrl_x_mode == CTRL_X_DICTIONARY
5747 || ctrl_x_mode == CTRL_X_THESAURUS)
5748 break;
5749 case '^': /* currently it's not needed. */
5750 case '$':
5751 m++;
5752 if (dest != NULL)
5753 *dest++ = '\\';
5754 break;
5755 }
5756 if (dest != NULL)
5757 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005758# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 /* Copy remaining bytes of a multibyte character. */
5760 if (has_mbyte)
5761 {
5762 int i, mb_len;
5763
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005764 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 if (mb_len > 0 && len >= mb_len)
5766 for (i = 0; i < mb_len; ++i)
5767 {
5768 --len;
5769 ++src;
5770 if (dest != NULL)
5771 *dest++ = *src;
5772 }
5773 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
5776 if (dest != NULL)
5777 *dest = NUL;
5778
5779 return m;
5780}
5781#endif /* FEAT_INS_EXPAND */
5782
5783/*
5784 * Next character is interpreted literally.
5785 * A one, two or three digit decimal number is interpreted as its byte value.
5786 * If one or two digits are entered, the next character is given to vungetc().
5787 * For Unicode a character > 255 may be returned.
5788 */
5789 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005790get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791{
5792 int cc;
5793 int nc;
5794 int i;
5795 int hex = FALSE;
5796 int octal = FALSE;
5797#ifdef FEAT_MBYTE
5798 int unicode = 0;
5799#endif
5800
5801 if (got_int)
5802 return Ctrl_C;
5803
5804#ifdef FEAT_GUI
5805 /*
5806 * In GUI there is no point inserting the internal code for a special key.
5807 * It is more useful to insert the string "<KEY>" instead. This would
5808 * probably be useful in a text window too, but it would not be
5809 * vi-compatible (maybe there should be an option for it?) -- webb
5810 */
5811 if (gui.in_use)
5812 ++allow_keys;
5813#endif
5814#ifdef USE_ON_FLY_SCROLL
5815 dont_scroll = TRUE; /* disallow scrolling here */
5816#endif
5817 ++no_mapping; /* don't map the next key hits */
5818 cc = 0;
5819 i = 0;
5820 for (;;)
5821 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005822 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823#ifdef FEAT_CMDL_INFO
5824 if (!(State & CMDLINE)
5825# ifdef FEAT_MBYTE
5826 && MB_BYTE2LEN_CHECK(nc) == 1
5827# endif
5828 )
5829 add_to_showcmd(nc);
5830#endif
5831 if (nc == 'x' || nc == 'X')
5832 hex = TRUE;
5833 else if (nc == 'o' || nc == 'O')
5834 octal = TRUE;
5835#ifdef FEAT_MBYTE
5836 else if (nc == 'u' || nc == 'U')
5837 unicode = nc;
5838#endif
5839 else
5840 {
5841 if (hex
5842#ifdef FEAT_MBYTE
5843 || unicode != 0
5844#endif
5845 )
5846 {
5847 if (!vim_isxdigit(nc))
5848 break;
5849 cc = cc * 16 + hex2nr(nc);
5850 }
5851 else if (octal)
5852 {
5853 if (nc < '0' || nc > '7')
5854 break;
5855 cc = cc * 8 + nc - '0';
5856 }
5857 else
5858 {
5859 if (!VIM_ISDIGIT(nc))
5860 break;
5861 cc = cc * 10 + nc - '0';
5862 }
5863
5864 ++i;
5865 }
5866
5867 if (cc > 255
5868#ifdef FEAT_MBYTE
5869 && unicode == 0
5870#endif
5871 )
5872 cc = 255; /* limit range to 0-255 */
5873 nc = 0;
5874
5875 if (hex) /* hex: up to two chars */
5876 {
5877 if (i >= 2)
5878 break;
5879 }
5880#ifdef FEAT_MBYTE
5881 else if (unicode) /* Unicode: up to four or eight chars */
5882 {
5883 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5884 break;
5885 }
5886#endif
5887 else if (i >= 3) /* decimal or octal: up to three chars */
5888 break;
5889 }
5890 if (i == 0) /* no number entered */
5891 {
5892 if (nc == K_ZERO) /* NUL is stored as NL */
5893 {
5894 cc = '\n';
5895 nc = 0;
5896 }
5897 else
5898 {
5899 cc = nc;
5900 nc = 0;
5901 }
5902 }
5903
5904 if (cc == 0) /* NUL is stored as NL */
5905 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005906#ifdef FEAT_MBYTE
5907 if (enc_dbcs && (cc & 0xff) == 0)
5908 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5909 second byte will cause trouble! */
5910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
5912 --no_mapping;
5913#ifdef FEAT_GUI
5914 if (gui.in_use)
5915 --allow_keys;
5916#endif
5917 if (nc)
5918 vungetc(nc);
5919 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5920 return cc;
5921}
5922
5923/*
5924 * Insert character, taking care of special keys and mod_mask
5925 */
5926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005927insert_special(
5928 int c,
5929 int allow_modmask,
5930 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
5932 char_u *p;
5933 int len;
5934
5935 /*
5936 * Special function key, translate into "<Key>". Up to the last '>' is
5937 * inserted with ins_str(), so as not to replace characters in replace
5938 * mode.
5939 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5940 * unless 'allow_modmask' is TRUE.
5941 */
5942#ifdef MACOS
5943 /* Command-key never produces a normal key */
5944 if (mod_mask & MOD_MASK_CMD)
5945 allow_modmask = TRUE;
5946#endif
5947 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5948 {
5949 p = get_special_key_name(c, mod_mask);
5950 len = (int)STRLEN(p);
5951 c = p[len - 1];
5952 if (len > 2)
5953 {
5954 if (stop_arrow() == FAIL)
5955 return;
5956 p[len - 1] = NUL;
5957 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005958 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 ctrlv = FALSE;
5960 }
5961 }
5962 if (stop_arrow() == OK)
5963 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5964}
5965
5966/*
5967 * Special characters in this context are those that need processing other
5968 * than the simple insertion that can be performed here. This includes ESC
5969 * which terminates the insert, and CR/NL which need special processing to
5970 * open up a new line. This routine tries to optimize insertions performed by
5971 * the "redo", "undo" or "put" commands, so it needs to know when it should
5972 * stop and defer processing to the "normal" mechanism.
5973 * '0' and '^' are special, because they can be followed by CTRL-D.
5974 */
5975#ifdef EBCDIC
5976# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5977#else
5978# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5979#endif
5980
5981#ifdef FEAT_MBYTE
5982# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5983#else
5984# define WHITECHAR(cc) vim_iswhite(cc)
5985#endif
5986
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005987/*
5988 * "flags": INSCHAR_FORMAT - force formatting
5989 * INSCHAR_CTRLV - char typed just after CTRL-V
5990 * INSCHAR_NO_FEX - don't use 'formatexpr'
5991 *
5992 * NOTE: passes the flags value straight through to internal_format() which,
5993 * beside INSCHAR_FORMAT (above), is also looking for these:
5994 * INSCHAR_DO_COM - format comments
5995 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005998insertchar(
5999 int c, /* character to insert or NUL */
6000 int flags, /* INSCHAR_FORMAT, etc. */
6001 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 int textwidth;
6004#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006008 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006010 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012
6013 /*
6014 * Try to break the line in two or more pieces when:
6015 * - Always do this if we have been called to do formatting only.
6016 * - Always do this when 'formatoptions' has the 'a' flag and the line
6017 * ends in white space.
6018 * - Otherwise:
6019 * - Don't do this if inserting a blank
6020 * - Don't do this if an existing character is being replaced, unless
6021 * we're in VREPLACE mode.
6022 * - Do this if the cursor is not on the line where insert started
6023 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6024 * before the insert.
6025 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6026 * before 'textwidth'
6027 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006028 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006029 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 || (!vim_iswhite(c)
6031 && !((State & REPLACE_FLAG)
6032#ifdef FEAT_VREPLACE
6033 && !(State & VREPLACE_FLAG)
6034#endif
6035 && *ml_get_cursor() != NUL)
6036 && (curwin->w_cursor.lnum != Insstart.lnum
6037 || ((!has_format_option(FO_INS_LONG)
6038 || Insstart_textlen <= (colnr_T)textwidth)
6039 && (!fo_ins_blank
6040 || Insstart_blank_vcol <= (colnr_T)textwidth
6041 ))))))
6042 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006043 /* Format with 'formatexpr' when it's set. Use internal formatting
6044 * when 'formatexpr' isn't set or it returns non-zero. */
6045#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006046 int do_internal = TRUE;
6047 colnr_T virtcol = get_nolist_virtcol()
6048 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006049
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006050 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6051 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006052 {
6053 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6054 /* It may be required to save for undo again, e.g. when setline()
6055 * was called. */
6056 ins_need_undo = TRUE;
6057 }
6058 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006060 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 if (c == NUL) /* only formatting was wanted */
6064 return;
6065
6066#ifdef FEAT_COMMENTS
6067 /* Check whether this character should end a comment. */
6068 if (did_ai && (int)c == end_comment_pending)
6069 {
6070 char_u *line;
6071 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6072 int middle_len, end_len;
6073 int i;
6074
6075 /*
6076 * Need to remove existing (middle) comment leader and insert end
6077 * comment leader. First, check what comment leader we can find.
6078 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006079 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6081 {
6082 /* Skip middle-comment string */
6083 while (*p && p[-1] != ':') /* find end of middle flags */
6084 ++p;
6085 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6086 /* Don't count trailing white space for middle_len */
6087 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6088 --middle_len;
6089
6090 /* Find the end-comment string */
6091 while (*p && p[-1] != ':') /* find end of end flags */
6092 ++p;
6093 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6094
6095 /* Skip white space before the cursor */
6096 i = curwin->w_cursor.col;
6097 while (--i >= 0 && vim_iswhite(line[i]))
6098 ;
6099 i++;
6100
6101 /* Skip to before the middle leader */
6102 i -= middle_len;
6103
6104 /* Check some expected things before we go on */
6105 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6106 {
6107 /* Backspace over all the stuff we want to replace */
6108 backspace_until_column(i);
6109
6110 /*
6111 * Insert the end-comment string, except for the last
6112 * character, which will get inserted as normal later.
6113 */
6114 ins_bytes_len(lead_end, end_len - 1);
6115 }
6116 }
6117 }
6118 end_comment_pending = NUL;
6119#endif
6120
6121 did_ai = FALSE;
6122#ifdef FEAT_SMARTINDENT
6123 did_si = FALSE;
6124 can_si = FALSE;
6125 can_si_back = FALSE;
6126#endif
6127
6128 /*
6129 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6130 * This speeds up normal text input considerably.
6131 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6132 * need to re-indent at a ':', or any other character (but not what
6133 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006134 * Don't do this when there an InsertCharPre autocommand is defined,
6135 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 */
6137#ifdef USE_ON_FLY_SCROLL
6138 dont_scroll = FALSE; /* allow scrolling here */
6139#endif
6140
6141 if ( !ISSPECIAL(c)
6142#ifdef FEAT_MBYTE
6143 && (!has_mbyte || (*mb_char2len)(c) == 1)
6144#endif
6145 && vpeekc() != NUL
6146 && !(State & REPLACE_FLAG)
6147#ifdef FEAT_CINDENT
6148 && !cindent_on()
6149#endif
6150#ifdef FEAT_RIGHTLEFT
6151 && !p_ri
6152#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006153#ifdef FEAT_AUTOCMD
6154 && !has_insertcharpre()
6155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 )
6157 {
6158#define INPUT_BUFLEN 100
6159 char_u buf[INPUT_BUFLEN + 1];
6160 int i;
6161 colnr_T virtcol = 0;
6162
6163 buf[0] = c;
6164 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006165 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 virtcol = get_nolist_virtcol();
6167 /*
6168 * Stop the string when:
6169 * - no more chars available
6170 * - finding a special character (command key)
6171 * - buffer is full
6172 * - running into the 'textwidth' boundary
6173 * - need to check for abbreviation: A non-word char after a word-char
6174 */
6175 while ( (c = vpeekc()) != NUL
6176 && !ISSPECIAL(c)
6177#ifdef FEAT_MBYTE
6178 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6179#endif
6180 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006181# ifdef FEAT_FKMAP
6182 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 && (textwidth == 0
6185 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6186 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6187 {
6188#ifdef FEAT_RIGHTLEFT
6189 c = vgetc();
6190 if (p_hkmap && KeyTyped)
6191 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 buf[i++] = c;
6193#else
6194 buf[i++] = vgetc();
6195#endif
6196 }
6197
6198#ifdef FEAT_DIGRAPHS
6199 do_digraph(-1); /* clear digraphs */
6200 do_digraph(buf[i-1]); /* may be the start of a digraph */
6201#endif
6202 buf[i] = NUL;
6203 ins_str(buf);
6204 if (flags & INSCHAR_CTRLV)
6205 {
6206 redo_literal(*buf);
6207 i = 1;
6208 }
6209 else
6210 i = 0;
6211 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006212 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 }
6214 else
6215 {
6216#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006217 int cc;
6218
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6220 {
6221 char_u buf[MB_MAXBYTES + 1];
6222
6223 (*mb_char2bytes)(c, buf);
6224 buf[cc] = NUL;
6225 ins_char_bytes(buf, cc);
6226 AppendCharToRedobuff(c);
6227 }
6228 else
6229#endif
6230 {
6231 ins_char(c);
6232 if (flags & INSCHAR_CTRLV)
6233 redo_literal(c);
6234 else
6235 AppendCharToRedobuff(c);
6236 }
6237 }
6238}
6239
6240/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006241 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006242 *
6243 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6244 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006245 */
6246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006247internal_format(
6248 int textwidth,
6249 int second_indent,
6250 int flags,
6251 int format_only,
6252 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006253{
6254 int cc;
6255 int save_char = NUL;
6256 int haveto_redraw = FALSE;
6257 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6258#ifdef FEAT_MBYTE
6259 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6260#endif
6261 int fo_white_par = has_format_option(FO_WHITE_PAR);
6262 int first_line = TRUE;
6263#ifdef FEAT_COMMENTS
6264 colnr_T leader_len;
6265 int no_leader = FALSE;
6266 int do_comments = (flags & INSCHAR_DO_COM);
6267#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006268#ifdef FEAT_LINEBREAK
6269 int has_lbr = curwin->w_p_lbr;
6270
6271 /* make sure win_lbr_chartabsize() counts correctly */
6272 curwin->w_p_lbr = FALSE;
6273#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006274
6275 /*
6276 * When 'ai' is off we don't want a space under the cursor to be
6277 * deleted. Replace it with an 'x' temporarily.
6278 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006279 if (!curbuf->b_p_ai
6280#ifdef FEAT_VREPLACE
6281 && !(State & VREPLACE_FLAG)
6282#endif
6283 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006284 {
6285 cc = gchar_cursor();
6286 if (vim_iswhite(cc))
6287 {
6288 save_char = cc;
6289 pchar_cursor('x');
6290 }
6291 }
6292
6293 /*
6294 * Repeat breaking lines, until the current line is not too long.
6295 */
6296 while (!got_int)
6297 {
6298 int startcol; /* Cursor column at entry */
6299 int wantcol; /* column at textwidth border */
6300 int foundcol; /* column for start of spaces */
6301 int end_foundcol = 0; /* column for start of word */
6302 colnr_T len;
6303 colnr_T virtcol;
6304#ifdef FEAT_VREPLACE
6305 int orig_col = 0;
6306 char_u *saved_text = NULL;
6307#endif
6308 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006309 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006310
Bram Moolenaar97b98102009-11-17 16:41:01 +00006311 virtcol = get_nolist_virtcol()
6312 + char2cells(c != NUL ? c : gchar_cursor());
6313 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006314 break;
6315
6316#ifdef FEAT_COMMENTS
6317 if (no_leader)
6318 do_comments = FALSE;
6319 else if (!(flags & INSCHAR_FORMAT)
6320 && has_format_option(FO_WRAP_COMS))
6321 do_comments = TRUE;
6322
6323 /* Don't break until after the comment leader */
6324 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006325 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006326 else
6327 leader_len = 0;
6328
6329 /* If the line doesn't start with a comment leader, then don't
6330 * start one in a following broken line. Avoids that a %word
6331 * moved to the start of the next line causes all following lines
6332 * to start with %. */
6333 if (leader_len == 0)
6334 no_leader = TRUE;
6335#endif
6336 if (!(flags & INSCHAR_FORMAT)
6337#ifdef FEAT_COMMENTS
6338 && leader_len == 0
6339#endif
6340 && !has_format_option(FO_WRAP))
6341
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006342 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006343 if ((startcol = curwin->w_cursor.col) == 0)
6344 break;
6345
6346 /* find column of textwidth border */
6347 coladvance((colnr_T)textwidth);
6348 wantcol = curwin->w_cursor.col;
6349
Bram Moolenaar97b98102009-11-17 16:41:01 +00006350 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006351 foundcol = 0;
6352
6353 /*
6354 * Find position to break at.
6355 * Stop at first entered white when 'formatoptions' has 'v'
6356 */
6357 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006358 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006359 || curwin->w_cursor.lnum != Insstart.lnum
6360 || curwin->w_cursor.col >= Insstart.col)
6361 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006362 if (curwin->w_cursor.col == startcol && c != NUL)
6363 cc = c;
6364 else
6365 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006366 if (WHITECHAR(cc))
6367 {
6368 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006369 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006370
6371 /* find start of sequence of blanks */
6372 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6373 {
6374 dec_cursor();
6375 cc = gchar_cursor();
6376 }
6377 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6378 break; /* only spaces in front of text */
6379#ifdef FEAT_COMMENTS
6380 /* Don't break until after the comment leader */
6381 if (curwin->w_cursor.col < leader_len)
6382 break;
6383#endif
6384 if (has_format_option(FO_ONE_LETTER))
6385 {
6386 /* do not break after one-letter words */
6387 if (curwin->w_cursor.col == 0)
6388 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006389#ifdef FEAT_COMMENTS
6390 /* do not break "#a b" when 'tw' is 2 */
6391 if (curwin->w_cursor.col <= leader_len)
6392 break;
6393#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006394 col = curwin->w_cursor.col;
6395 dec_cursor();
6396 cc = gchar_cursor();
6397
6398 if (WHITECHAR(cc))
6399 continue; /* one-letter, continue */
6400 curwin->w_cursor.col = col;
6401 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006402
6403 inc_cursor();
6404
6405 end_foundcol = end_col + 1;
6406 foundcol = curwin->w_cursor.col;
6407 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006408 break;
6409 }
6410#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006411 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006412 {
6413 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006414 if (curwin->w_cursor.col != startcol)
6415 {
6416#ifdef FEAT_COMMENTS
6417 /* Don't break until after the comment leader */
6418 if (curwin->w_cursor.col < leader_len)
6419 break;
6420#endif
6421 col = curwin->w_cursor.col;
6422 inc_cursor();
6423 /* Don't change end_foundcol if already set. */
6424 if (foundcol != curwin->w_cursor.col)
6425 {
6426 foundcol = curwin->w_cursor.col;
6427 end_foundcol = foundcol;
6428 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6429 break;
6430 }
6431 curwin->w_cursor.col = col;
6432 }
6433
6434 if (curwin->w_cursor.col == 0)
6435 break;
6436
6437 col = curwin->w_cursor.col;
6438
6439 dec_cursor();
6440 cc = gchar_cursor();
6441
6442 if (WHITECHAR(cc))
6443 continue; /* break with space */
6444#ifdef FEAT_COMMENTS
6445 /* Don't break until after the comment leader */
6446 if (curwin->w_cursor.col < leader_len)
6447 break;
6448#endif
6449
6450 curwin->w_cursor.col = col;
6451
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006452 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006453 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006454 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6455 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006456 }
6457#endif
6458 if (curwin->w_cursor.col == 0)
6459 break;
6460 dec_cursor();
6461 }
6462
6463 if (foundcol == 0) /* no spaces, cannot break line */
6464 {
6465 curwin->w_cursor.col = startcol;
6466 break;
6467 }
6468
6469 /* Going to break the line, remove any "$" now. */
6470 undisplay_dollar();
6471
6472 /*
6473 * Offset between cursor position and line break is used by replace
6474 * stack functions. VREPLACE does not use this, and backspaces
6475 * over the text instead.
6476 */
6477#ifdef FEAT_VREPLACE
6478 if (State & VREPLACE_FLAG)
6479 orig_col = startcol; /* Will start backspacing from here */
6480 else
6481#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006482 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006483
6484 /*
6485 * adjust startcol for spaces that will be deleted and
6486 * characters that will remain on top line
6487 */
6488 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006489 while ((cc = gchar_cursor(), WHITECHAR(cc))
6490 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006491 inc_cursor();
6492 startcol -= curwin->w_cursor.col;
6493 if (startcol < 0)
6494 startcol = 0;
6495
6496#ifdef FEAT_VREPLACE
6497 if (State & VREPLACE_FLAG)
6498 {
6499 /*
6500 * In VREPLACE mode, we will backspace over the text to be
6501 * wrapped, so save a copy now to put on the next line.
6502 */
6503 saved_text = vim_strsave(ml_get_cursor());
6504 curwin->w_cursor.col = orig_col;
6505 if (saved_text == NULL)
6506 break; /* Can't do it, out of memory */
6507 saved_text[startcol] = NUL;
6508
6509 /* Backspace over characters that will move to the next line */
6510 if (!fo_white_par)
6511 backspace_until_column(foundcol);
6512 }
6513 else
6514#endif
6515 {
6516 /* put cursor after pos. to break line */
6517 if (!fo_white_par)
6518 curwin->w_cursor.col = foundcol;
6519 }
6520
6521 /*
6522 * Split the line just before the margin.
6523 * Only insert/delete lines, but don't really redraw the window.
6524 */
6525 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6526 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6527#ifdef FEAT_COMMENTS
6528 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006529 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006530#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006531 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6532 if (!(flags & INSCHAR_COM_LIST))
6533 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006534
6535 replace_offset = 0;
6536 if (first_line)
6537 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006538 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006539 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006540 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006541 * This section is for auto-wrap of numeric lists. When not
6542 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6543 * flag will be set and open_line() will handle it (as seen
6544 * above). The code here (and in get_number_indent()) will
6545 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006546 */
6547 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006548 second_indent =
6549 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006550 if (second_indent >= 0)
6551 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006552#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006553 if (State & VREPLACE_FLAG)
6554 change_indent(INDENT_SET, second_indent,
6555 FALSE, NUL, TRUE);
6556 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006557#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006558#ifdef FEAT_COMMENTS
6559 if (leader_len > 0 && second_indent - leader_len > 0)
6560 {
6561 int i;
6562 int padding = second_indent - leader_len;
6563
6564 /* We started at the first_line of a numbered list
6565 * that has a comment. the open_line() function has
6566 * inserted the proper comment leader and positioned
6567 * the cursor at the end of the split line. Now we
6568 * add the additional whitespace needed after the
6569 * comment leader for the numbered list. */
6570 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006571 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006572 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006573 }
6574 else
6575 {
6576#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006577 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006578#ifdef FEAT_COMMENTS
6579 }
6580#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006581 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006582 }
6583 first_line = FALSE;
6584 }
6585
6586#ifdef FEAT_VREPLACE
6587 if (State & VREPLACE_FLAG)
6588 {
6589 /*
6590 * In VREPLACE mode we have backspaced over the text to be
6591 * moved, now we re-insert it into the new line.
6592 */
6593 ins_bytes(saved_text);
6594 vim_free(saved_text);
6595 }
6596 else
6597#endif
6598 {
6599 /*
6600 * Check if cursor is not past the NUL off the line, cindent
6601 * may have added or removed indent.
6602 */
6603 curwin->w_cursor.col += startcol;
6604 len = (colnr_T)STRLEN(ml_get_curline());
6605 if (curwin->w_cursor.col > len)
6606 curwin->w_cursor.col = len;
6607 }
6608
6609 haveto_redraw = TRUE;
6610#ifdef FEAT_CINDENT
6611 can_cindent = TRUE;
6612#endif
6613 /* moved the cursor, don't autoindent or cindent now */
6614 did_ai = FALSE;
6615#ifdef FEAT_SMARTINDENT
6616 did_si = FALSE;
6617 can_si = FALSE;
6618 can_si_back = FALSE;
6619#endif
6620 line_breakcheck();
6621 }
6622
6623 if (save_char != NUL) /* put back space after cursor */
6624 pchar_cursor(save_char);
6625
Bram Moolenaar0026d472014-09-09 16:32:39 +02006626#ifdef FEAT_LINEBREAK
6627 curwin->w_p_lbr = has_lbr;
6628#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006629 if (!format_only && haveto_redraw)
6630 {
6631 update_topline();
6632 redraw_curbuf_later(VALID);
6633 }
6634}
6635
6636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 * Called after inserting or deleting text: When 'formatoptions' includes the
6638 * 'a' flag format from the current line until the end of the paragraph.
6639 * Keep the cursor at the same position relative to the text.
6640 * The caller must have saved the cursor line for undo, following ones will be
6641 * saved here.
6642 */
6643 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006644auto_format(
6645 int trailblank, /* when TRUE also format with trailing blank */
6646 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647{
6648 pos_T pos;
6649 colnr_T len;
6650 char_u *old;
6651 char_u *new, *pnew;
6652 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006653 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654
6655 if (!has_format_option(FO_AUTO))
6656 return;
6657
6658 pos = curwin->w_cursor;
6659 old = ml_get_curline();
6660
6661 /* may remove added space */
6662 check_auto_format(FALSE);
6663
6664 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6665 * user might insert normal text next. Also skip formatting when "1" is
6666 * in 'formatoptions' and there is a single character before the cursor.
6667 * Otherwise the line would be broken and when typing another non-white
6668 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006669 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 if (*old != NUL && !trailblank && wasatend)
6671 {
6672 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006673 cc = gchar_cursor();
6674 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6675 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006677 cc = gchar_cursor();
6678 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
6680 curwin->w_cursor = pos;
6681 return;
6682 }
6683 curwin->w_cursor = pos;
6684 }
6685
6686#ifdef FEAT_COMMENTS
6687 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6688 * comments. */
6689 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006690 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 return;
6692#endif
6693
6694 /*
6695 * May start formatting in a previous line, so that after "x" a word is
6696 * moved to the previous line if it fits there now. Only when this is not
6697 * the start of a paragraph.
6698 */
6699 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6700 {
6701 --curwin->w_cursor.lnum;
6702 if (u_save_cursor() == FAIL)
6703 return;
6704 }
6705
6706 /*
6707 * Do the formatting and restore the cursor position. "saved_cursor" will
6708 * be adjusted for the text formatting.
6709 */
6710 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006711 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 curwin->w_cursor = saved_cursor;
6713 saved_cursor.lnum = 0;
6714
6715 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6716 {
6717 /* "cannot happen" */
6718 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6719 coladvance((colnr_T)MAXCOL);
6720 }
6721 else
6722 check_cursor_col();
6723
6724 /* Insert mode: If the cursor is now after the end of the line while it
6725 * previously wasn't, the line was broken. Because of the rule above we
6726 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6727 * formatted. */
6728 if (!wasatend && has_format_option(FO_WHITE_PAR))
6729 {
6730 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006731 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 if (curwin->w_cursor.col == len)
6733 {
6734 pnew = vim_strnsave(new, len + 2);
6735 pnew[len] = ' ';
6736 pnew[len + 1] = NUL;
6737 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6738 /* remove the space later */
6739 did_add_space = TRUE;
6740 }
6741 else
6742 /* may remove added space */
6743 check_auto_format(FALSE);
6744 }
6745
6746 check_cursor();
6747}
6748
6749/*
6750 * When an extra space was added to continue a paragraph for auto-formatting,
6751 * delete it now. The space must be under the cursor, just after the insert
6752 * position.
6753 */
6754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006755check_auto_format(
6756 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757{
6758 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006759 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760
6761 if (did_add_space)
6762 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006763 cc = gchar_cursor();
6764 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 /* Somehow the space was removed already. */
6766 did_add_space = FALSE;
6767 else
6768 {
6769 if (!end_insert)
6770 {
6771 inc_cursor();
6772 c = gchar_cursor();
6773 dec_cursor();
6774 }
6775 if (c != NUL)
6776 {
6777 /* The space is no longer at the end of the line, delete it. */
6778 del_char(FALSE);
6779 did_add_space = FALSE;
6780 }
6781 }
6782 }
6783}
6784
6785/*
6786 * Find out textwidth to be used for formatting:
6787 * if 'textwidth' option is set, use it
6788 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6789 * if invalid value, use 0.
6790 * Set default to window width (maximum 79) for "gq" operator.
6791 */
6792 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006793comp_textwidth(
6794 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795{
6796 int textwidth;
6797
6798 textwidth = curbuf->b_p_tw;
6799 if (textwidth == 0 && curbuf->b_p_wm)
6800 {
6801 /* The width is the window width minus 'wrapmargin' minus all the
6802 * things that add to the margin. */
6803 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6804#ifdef FEAT_CMDWIN
6805 if (cmdwin_type != 0)
6806 textwidth -= 1;
6807#endif
6808#ifdef FEAT_FOLDING
6809 textwidth -= curwin->w_p_fdc;
6810#endif
6811#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006812 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 textwidth -= 1;
6814#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006815 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 textwidth -= 8;
6817 }
6818 if (textwidth < 0)
6819 textwidth = 0;
6820 if (ff && textwidth == 0)
6821 {
6822 textwidth = W_WIDTH(curwin) - 1;
6823 if (textwidth > 79)
6824 textwidth = 79;
6825 }
6826 return textwidth;
6827}
6828
6829/*
6830 * Put a character in the redo buffer, for when just after a CTRL-V.
6831 */
6832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006833redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834{
6835 char_u buf[10];
6836
6837 /* Only digits need special treatment. Translate them into a string of
6838 * three digits. */
6839 if (VIM_ISDIGIT(c))
6840 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006841 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 AppendToRedobuff(buf);
6843 }
6844 else
6845 AppendCharToRedobuff(c);
6846}
6847
6848/*
6849 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006850 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 */
6852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006853start_arrow(
6854 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006856 start_arrow_common(end_insert_pos, TRUE);
6857}
6858
6859/*
6860 * Like start_arrow() but with end_change argument.
6861 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6862 */
6863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006864start_arrow_with_change(
6865 pos_T *end_insert_pos, /* can be NULL */
6866 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006867{
6868 start_arrow_common(end_insert_pos, end_change);
6869 if (!end_change)
6870 {
6871 AppendCharToRedobuff(Ctrl_G);
6872 AppendCharToRedobuff('U');
6873 }
6874}
6875
6876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006877start_arrow_common(
6878 pos_T *end_insert_pos, /* can be NULL */
6879 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006880{
6881 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
6883 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006884 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 arrow_used = TRUE; /* this means we stopped the current insert */
6886 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006887#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006888 check_spell_redraw();
6889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890}
6891
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006892#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006893/*
6894 * If we skipped highlighting word at cursor, do it now.
6895 * It may be skipped again, thus reset spell_redraw_lnum first.
6896 */
6897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006898check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006899{
6900 if (spell_redraw_lnum != 0)
6901 {
6902 linenr_T lnum = spell_redraw_lnum;
6903
6904 spell_redraw_lnum = 0;
6905 redrawWinline(lnum, FALSE);
6906 }
6907}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006908
6909/*
6910 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6911 * spelled word, if there is one.
6912 */
6913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006914spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006915{
6916 pos_T tpos = curwin->w_cursor;
6917
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006918 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006919 if (curwin->w_cursor.col != tpos.col)
6920 start_arrow(&tpos);
6921}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006922#endif
6923
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924/*
6925 * stop_arrow() is called before a change is made in insert mode.
6926 * If an arrow key has been used, start a new insertion.
6927 * Returns FAIL if undo is impossible, shouldn't insert then.
6928 */
6929 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006930stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931{
6932 if (arrow_used)
6933 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006934 Insstart = curwin->w_cursor; /* new insertion starts here */
6935 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6936 /* Don't update the original insert position when moved to the
6937 * right, except when nothing was inserted yet. */
6938 update_Insstart_orig = FALSE;
6939 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6940
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 if (u_save_cursor() == OK)
6942 {
6943 arrow_used = FALSE;
6944 ins_need_undo = FALSE;
6945 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 ai_col = 0;
6948#ifdef FEAT_VREPLACE
6949 if (State & VREPLACE_FLAG)
6950 {
6951 orig_line_count = curbuf->b_ml.ml_line_count;
6952 vr_lines_changed = 1;
6953 }
6954#endif
6955 ResetRedobuff();
6956 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006957 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 }
6959 else if (ins_need_undo)
6960 {
6961 if (u_save_cursor() == OK)
6962 ins_need_undo = FALSE;
6963 }
6964
6965#ifdef FEAT_FOLDING
6966 /* Always open fold at the cursor line when inserting something. */
6967 foldOpenCursor();
6968#endif
6969
6970 return (arrow_used || ins_need_undo ? FAIL : OK);
6971}
6972
6973/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006974 * Do a few things to stop inserting.
6975 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6976 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 */
6978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006979stop_insert(
6980 pos_T *end_insert_pos,
6981 int esc, /* called by ins_esc() */
6982 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006984 int cc;
6985 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986
6987 stop_redo_ins();
6988 replace_flush(); /* abandon replace stack */
6989
6990 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006991 * Save the inserted text for later redo with ^@ and CTRL-A.
6992 * Don't do it when "restart_edit" was set and nothing was inserted,
6993 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006995 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006996 if (did_restart_edit == 0 || (ptr != NULL
6997 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006998 {
6999 vim_free(last_insert);
7000 last_insert = ptr;
7001 last_insert_skip = new_insert_skip;
7002 }
7003 else
7004 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007006 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {
7008 /* Auto-format now. It may seem strange to do this when stopping an
7009 * insertion (or moving the cursor), but it's required when appending
7010 * a line and having it end in a space. But only do it when something
7011 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007012 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007014 pos_T tpos = curwin->w_cursor;
7015
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 /* When the cursor is at the end of the line after a space the
7017 * formatting will move it to the following word. Avoid that by
7018 * moving the cursor onto the space. */
7019 cc = 'x';
7020 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7021 {
7022 dec_cursor();
7023 cc = gchar_cursor();
7024 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007025 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 }
7027
7028 auto_format(TRUE, FALSE);
7029
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007030 if (vim_iswhite(cc))
7031 {
7032 if (gchar_cursor() != NUL)
7033 inc_cursor();
7034#ifdef FEAT_VIRTUALEDIT
7035 /* If the cursor is still at the same character, also keep
7036 * the "coladd". */
7037 if (gchar_cursor() == NUL
7038 && curwin->w_cursor.lnum == tpos.lnum
7039 && curwin->w_cursor.col == tpos.col)
7040 curwin->w_cursor.coladd = tpos.coladd;
7041#endif
7042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 }
7044
7045 /* If a space was inserted for auto-formatting, remove it now. */
7046 check_auto_format(TRUE);
7047
7048 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007049 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007050 * Do this when ESC was used or moving the cursor up/down.
7051 * Check for the old position still being valid, just in case the text
7052 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007053 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007054 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7055 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007057 pos_T tpos = curwin->w_cursor;
7058
7059 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007060 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007061 for (;;)
7062 {
7063 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7064 --curwin->w_cursor.col;
7065 cc = gchar_cursor();
7066 if (!vim_iswhite(cc))
7067 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007068 if (del_char(TRUE) == FAIL)
7069 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007070 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007071 if (curwin->w_cursor.lnum != tpos.lnum)
7072 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007073 else
7074 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007075 /* reset tpos, could have been invalidated in the loop above */
7076 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007077 tpos.col++;
7078 if (cc != NUL && gchar_pos(&tpos) == NUL)
7079 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 /* <C-S-Right> may have started Visual mode, adjust the position for
7083 * deleted characters. */
7084 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7085 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007086 int len = (int)STRLEN(ml_get_curline());
7087
7088 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007090 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007091#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 }
7095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 }
7097 }
7098 did_ai = FALSE;
7099#ifdef FEAT_SMARTINDENT
7100 did_si = FALSE;
7101 can_si = FALSE;
7102 can_si_back = FALSE;
7103#endif
7104
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007105 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7106 * now in a different buffer. */
7107 if (end_insert_pos != NULL)
7108 {
7109 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007110 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007111 curbuf->b_op_end = *end_insert_pos;
7112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113}
7114
7115/*
7116 * Set the last inserted text to a single character.
7117 * Used for the replace command.
7118 */
7119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007120set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 char_u *s;
7123
7124 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 if (last_insert != NULL)
7127 {
7128 s = last_insert;
7129 /* Use the CTRL-V only when entering a special char */
7130 if (c < ' ' || c == DEL)
7131 *s++ = Ctrl_V;
7132 s = add_char2buf(c, s);
7133 *s++ = ESC;
7134 *s++ = NUL;
7135 last_insert_skip = 0;
7136 }
7137}
7138
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007139#if defined(EXITFREE) || defined(PROTO)
7140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007141free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007142{
7143 vim_free(last_insert);
7144 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007145# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007146 vim_free(compl_orig_text);
7147 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007148# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007149}
7150#endif
7151
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152/*
7153 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7154 * and CSI. Handle multi-byte characters.
7155 * Returns a pointer to after the added bytes.
7156 */
7157 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007158add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159{
7160#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007161 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 int i;
7163 int len;
7164
7165 len = (*mb_char2bytes)(c, temp);
7166 for (i = 0; i < len; ++i)
7167 {
7168 c = temp[i];
7169#endif
7170 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7171 if (c == K_SPECIAL)
7172 {
7173 *s++ = K_SPECIAL;
7174 *s++ = KS_SPECIAL;
7175 *s++ = KE_FILLER;
7176 }
7177#ifdef FEAT_GUI
7178 else if (c == CSI)
7179 {
7180 *s++ = CSI;
7181 *s++ = KS_EXTRA;
7182 *s++ = (int)KE_CSI;
7183 }
7184#endif
7185 else
7186 *s++ = c;
7187#ifdef FEAT_MBYTE
7188 }
7189#endif
7190 return s;
7191}
7192
7193/*
7194 * move cursor to start of line
7195 * if flags & BL_WHITE move to first non-white
7196 * if flags & BL_SOL move to first non-white if startofline is set,
7197 * otherwise keep "curswant" column
7198 * if flags & BL_FIX don't leave the cursor on a NUL.
7199 */
7200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007201beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202{
7203 if ((flags & BL_SOL) && !p_sol)
7204 coladvance(curwin->w_curswant);
7205 else
7206 {
7207 curwin->w_cursor.col = 0;
7208#ifdef FEAT_VIRTUALEDIT
7209 curwin->w_cursor.coladd = 0;
7210#endif
7211
7212 if (flags & (BL_WHITE | BL_SOL))
7213 {
7214 char_u *ptr;
7215
7216 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7217 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7218 ++curwin->w_cursor.col;
7219 }
7220 curwin->w_set_curswant = TRUE;
7221 }
7222}
7223
7224/*
7225 * oneright oneleft cursor_down cursor_up
7226 *
7227 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007228 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 * Return OK when successful, FAIL when we hit a line of file boundary.
7230 */
7231
7232 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007233oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234{
7235 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
7238#ifdef FEAT_VIRTUALEDIT
7239 if (virtual_active())
7240 {
7241 pos_T prevpos = curwin->w_cursor;
7242
7243 /* Adjust for multi-wide char (excluding TAB) */
7244 ptr = ml_get_cursor();
7245 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007246# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007248# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007250# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 ))
7252 ? ptr2cells(ptr) : 1));
7253 curwin->w_set_curswant = TRUE;
7254 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7255 return (prevpos.col != curwin->w_cursor.col
7256 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7257 }
7258#endif
7259
7260 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007261 if (*ptr == NUL)
7262 return FAIL; /* already at the very end */
7263
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007265 if (has_mbyte)
7266 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 else
7268#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007269 l = 1;
7270
7271 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7272 * contains "onemore". */
7273 if (ptr[l] == NUL
7274#ifdef FEAT_VIRTUALEDIT
7275 && (ve_flags & VE_ONEMORE) == 0
7276#endif
7277 )
7278 return FAIL;
7279 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280
7281 curwin->w_set_curswant = TRUE;
7282 return OK;
7283}
7284
7285 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007286oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287{
7288#ifdef FEAT_VIRTUALEDIT
7289 if (virtual_active())
7290 {
7291 int width;
7292 int v = getviscol();
7293
7294 if (v == 0)
7295 return FAIL;
7296
7297# ifdef FEAT_LINEBREAK
7298 /* We might get stuck on 'showbreak', skip over it. */
7299 width = 1;
7300 for (;;)
7301 {
7302 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007303 /* getviscol() is slow, skip it when 'showbreak' is empty,
7304 * 'breakindent' is not set and there are no multi-byte
7305 * characters */
7306 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307# ifdef FEAT_MBYTE
7308 && !has_mbyte
7309# endif
7310 ) || getviscol() < v)
7311 break;
7312 ++width;
7313 }
7314# else
7315 coladvance(v - 1);
7316# endif
7317
7318 if (curwin->w_cursor.coladd == 1)
7319 {
7320 char_u *ptr;
7321
7322 /* Adjust for multi-wide char (not a TAB) */
7323 ptr = ml_get_cursor();
7324 if (*ptr != TAB && vim_isprintc(
7325# ifdef FEAT_MBYTE
7326 (*mb_ptr2char)(ptr)
7327# else
7328 *ptr
7329# endif
7330 ) && ptr2cells(ptr) > 1)
7331 curwin->w_cursor.coladd = 0;
7332 }
7333
7334 curwin->w_set_curswant = TRUE;
7335 return OK;
7336 }
7337#endif
7338
7339 if (curwin->w_cursor.col == 0)
7340 return FAIL;
7341
7342 curwin->w_set_curswant = TRUE;
7343 --curwin->w_cursor.col;
7344
7345#ifdef FEAT_MBYTE
7346 /* if the character on the left of the current cursor is a multi-byte
7347 * character, move to its first byte */
7348 if (has_mbyte)
7349 mb_adjust_cursor();
7350#endif
7351 return OK;
7352}
7353
7354 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007355cursor_up(
7356 long n,
7357 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358{
7359 linenr_T lnum;
7360
7361 if (n > 0)
7362 {
7363 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007364 /* This fails if the cursor is already in the first line or the count
7365 * is larger than the line number and '-' is in 'cpoptions' */
7366 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 return FAIL;
7368 if (n >= lnum)
7369 lnum = 1;
7370 else
7371#ifdef FEAT_FOLDING
7372 if (hasAnyFolding(curwin))
7373 {
7374 /*
7375 * Count each sequence of folded lines as one logical line.
7376 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007377 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 (void)hasFolding(lnum, &lnum, NULL);
7379
7380 while (n--)
7381 {
7382 /* move up one line */
7383 --lnum;
7384 if (lnum <= 1)
7385 break;
7386 /* If we entered a fold, move to the beginning, unless in
7387 * Insert mode or when 'foldopen' contains "all": it will open
7388 * in a moment. */
7389 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7390 (void)hasFolding(lnum, &lnum, NULL);
7391 }
7392 if (lnum < 1)
7393 lnum = 1;
7394 }
7395 else
7396#endif
7397 lnum -= n;
7398 curwin->w_cursor.lnum = lnum;
7399 }
7400
7401 /* try to advance to the column we want to be at */
7402 coladvance(curwin->w_curswant);
7403
7404 if (upd_topline)
7405 update_topline(); /* make sure curwin->w_topline is valid */
7406
7407 return OK;
7408}
7409
7410/*
7411 * Cursor down a number of logical lines.
7412 */
7413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007414cursor_down(
7415 long n,
7416 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
7418 linenr_T lnum;
7419
7420 if (n > 0)
7421 {
7422 lnum = curwin->w_cursor.lnum;
7423#ifdef FEAT_FOLDING
7424 /* Move to last line of fold, will fail if it's the end-of-file. */
7425 (void)hasFolding(lnum, NULL, &lnum);
7426#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007427 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007428 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007429 if (lnum >= curbuf->b_ml.ml_line_count
7430 || (lnum + n > curbuf->b_ml.ml_line_count
7431 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 return FAIL;
7433 if (lnum + n >= curbuf->b_ml.ml_line_count)
7434 lnum = curbuf->b_ml.ml_line_count;
7435 else
7436#ifdef FEAT_FOLDING
7437 if (hasAnyFolding(curwin))
7438 {
7439 linenr_T last;
7440
7441 /* count each sequence of folded lines as one logical line */
7442 while (n--)
7443 {
7444 if (hasFolding(lnum, NULL, &last))
7445 lnum = last + 1;
7446 else
7447 ++lnum;
7448 if (lnum >= curbuf->b_ml.ml_line_count)
7449 break;
7450 }
7451 if (lnum > curbuf->b_ml.ml_line_count)
7452 lnum = curbuf->b_ml.ml_line_count;
7453 }
7454 else
7455#endif
7456 lnum += n;
7457 curwin->w_cursor.lnum = lnum;
7458 }
7459
7460 /* try to advance to the column we want to be at */
7461 coladvance(curwin->w_curswant);
7462
7463 if (upd_topline)
7464 update_topline(); /* make sure curwin->w_topline is valid */
7465
7466 return OK;
7467}
7468
7469/*
7470 * Stuff the last inserted text in the read buffer.
7471 * Last_insert actually is a copy of the redo buffer, so we
7472 * first have to remove the command.
7473 */
7474 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007475stuff_inserted(
7476 int c, /* Command character to be inserted */
7477 long count, /* Repeat this many times */
7478 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479{
7480 char_u *esc_ptr;
7481 char_u *ptr;
7482 char_u *last_ptr;
7483 char_u last = NUL;
7484
7485 ptr = get_last_insert();
7486 if (ptr == NULL)
7487 {
7488 EMSG(_(e_noinstext));
7489 return FAIL;
7490 }
7491
7492 /* may want to stuff the command character, to start Insert mode */
7493 if (c != NUL)
7494 stuffcharReadbuff(c);
7495 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7496 *esc_ptr = NUL; /* remove the ESC */
7497
7498 /* when the last char is either "0" or "^" it will be quoted if no ESC
7499 * comes after it OR if it will inserted more than once and "ptr"
7500 * starts with ^D. -- Acevedo
7501 */
7502 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7503 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7504 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7505 {
7506 last = *last_ptr;
7507 *last_ptr = NUL;
7508 }
7509
7510 do
7511 {
7512 stuffReadbuff(ptr);
7513 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7514 if (last)
7515 stuffReadbuff((char_u *)(last == '0'
7516 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7517 : IF_EB("\026^", CTRL_V_STR "^")));
7518 }
7519 while (--count > 0);
7520
7521 if (last)
7522 *last_ptr = last;
7523
7524 if (esc_ptr != NULL)
7525 *esc_ptr = ESC; /* put the ESC back */
7526
7527 /* may want to stuff a trailing ESC, to get out of Insert mode */
7528 if (!no_esc)
7529 stuffcharReadbuff(ESC);
7530
7531 return OK;
7532}
7533
7534 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007535get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536{
7537 if (last_insert == NULL)
7538 return NULL;
7539 return last_insert + last_insert_skip;
7540}
7541
7542/*
7543 * Get last inserted string, and remove trailing <Esc>.
7544 * Returns pointer to allocated memory (must be freed) or NULL.
7545 */
7546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007547get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548{
7549 char_u *s;
7550 int len;
7551
7552 if (last_insert == NULL)
7553 return NULL;
7554 s = vim_strsave(last_insert + last_insert_skip);
7555 if (s != NULL)
7556 {
7557 len = (int)STRLEN(s);
7558 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7559 s[len - 1] = NUL;
7560 }
7561 return s;
7562}
7563
7564/*
7565 * Check the word in front of the cursor for an abbreviation.
7566 * Called when the non-id character "c" has been entered.
7567 * When an abbreviation is recognized it is removed from the text and
7568 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7569 */
7570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007571echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572{
7573 /* Don't check for abbreviation in paste mode, when disabled and just
7574 * after moving around with cursor keys. */
7575 if (p_paste || no_abbr || arrow_used)
7576 return FALSE;
7577
7578 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7579 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7580}
7581
7582/*
7583 * replace-stack functions
7584 *
7585 * When replacing characters, the replaced characters are remembered for each
7586 * new character. This is used to re-insert the old text when backspacing.
7587 *
7588 * There is a NUL headed list of characters for each character that is
7589 * currently in the file after the insertion point. When BS is used, one NUL
7590 * headed list is put back for the deleted character.
7591 *
7592 * For a newline, there are two NUL headed lists. One contains the characters
7593 * that the NL replaced. The extra one stores the characters after the cursor
7594 * that were deleted (always white space).
7595 *
7596 * Replace_offset is normally 0, in which case replace_push will add a new
7597 * character at the end of the stack. If replace_offset is not 0, that many
7598 * characters will be left on the stack above the newly inserted character.
7599 */
7600
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007601static char_u *replace_stack = NULL;
7602static long replace_stack_nr = 0; /* next entry in replace stack */
7603static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604
7605 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007606replace_push(
7607 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608{
7609 char_u *p;
7610
7611 if (replace_stack_nr < replace_offset) /* nothing to do */
7612 return;
7613 if (replace_stack_len <= replace_stack_nr)
7614 {
7615 replace_stack_len += 50;
7616 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7617 if (p == NULL) /* out of memory */
7618 {
7619 replace_stack_len -= 50;
7620 return;
7621 }
7622 if (replace_stack != NULL)
7623 {
7624 mch_memmove(p, replace_stack,
7625 (size_t)(replace_stack_nr * sizeof(char_u)));
7626 vim_free(replace_stack);
7627 }
7628 replace_stack = p;
7629 }
7630 p = replace_stack + replace_stack_nr - replace_offset;
7631 if (replace_offset)
7632 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7633 *p = c;
7634 ++replace_stack_nr;
7635}
7636
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007637#if defined(FEAT_MBYTE) || defined(PROTO)
7638/*
7639 * Push a character onto the replace stack. Handles a multi-byte character in
7640 * reverse byte order, so that the first byte is popped off first.
7641 * Return the number of bytes done (includes composing characters).
7642 */
7643 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007644replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007645{
7646 int l = (*mb_ptr2len)(p);
7647 int j;
7648
7649 for (j = l - 1; j >= 0; --j)
7650 replace_push(p[j]);
7651 return l;
7652}
7653#endif
7654
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655/*
7656 * Pop one item from the replace stack.
7657 * return -1 if stack empty
7658 * return replaced character or NUL otherwise
7659 */
7660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007661replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
7663 if (replace_stack_nr == 0)
7664 return -1;
7665 return (int)replace_stack[--replace_stack_nr];
7666}
7667
7668/*
7669 * Join the top two items on the replace stack. This removes to "off"'th NUL
7670 * encountered.
7671 */
7672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007673replace_join(
7674 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
7676 int i;
7677
7678 for (i = replace_stack_nr; --i >= 0; )
7679 if (replace_stack[i] == NUL && off-- <= 0)
7680 {
7681 --replace_stack_nr;
7682 mch_memmove(replace_stack + i, replace_stack + i + 1,
7683 (size_t)(replace_stack_nr - i));
7684 return;
7685 }
7686}
7687
7688/*
7689 * Pop bytes from the replace stack until a NUL is found, and insert them
7690 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7691 */
7692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007693replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
7695 int cc;
7696 int oldState = State;
7697
7698 State = NORMAL; /* don't want REPLACE here */
7699 while ((cc = replace_pop()) > 0)
7700 {
7701#ifdef FEAT_MBYTE
7702 mb_replace_pop_ins(cc);
7703#else
7704 ins_char(cc);
7705#endif
7706 dec_cursor();
7707 }
7708 State = oldState;
7709}
7710
7711#ifdef FEAT_MBYTE
7712/*
7713 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7714 * indicates a multi-byte char, pop the other bytes too.
7715 */
7716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007717mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
7719 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007720 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 int i;
7722 int c;
7723
7724 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7725 {
7726 buf[0] = cc;
7727 for (i = 1; i < n; ++i)
7728 buf[i] = replace_pop();
7729 ins_bytes_len(buf, n);
7730 }
7731 else
7732 ins_char(cc);
7733
7734 if (enc_utf8)
7735 /* Handle composing chars. */
7736 for (;;)
7737 {
7738 c = replace_pop();
7739 if (c == -1) /* stack empty */
7740 break;
7741 if ((n = MB_BYTE2LEN(c)) == 1)
7742 {
7743 /* Not a multi-byte char, put it back. */
7744 replace_push(c);
7745 break;
7746 }
7747 else
7748 {
7749 buf[0] = c;
7750 for (i = 1; i < n; ++i)
7751 buf[i] = replace_pop();
7752 if (utf_iscomposing(utf_ptr2char(buf)))
7753 ins_bytes_len(buf, n);
7754 else
7755 {
7756 /* Not a composing char, put it back. */
7757 for (i = n - 1; i >= 0; --i)
7758 replace_push(buf[i]);
7759 break;
7760 }
7761 }
7762 }
7763}
7764#endif
7765
7766/*
7767 * make the replace stack empty
7768 * (called when exiting replace mode)
7769 */
7770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007771replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773 vim_free(replace_stack);
7774 replace_stack = NULL;
7775 replace_stack_len = 0;
7776 replace_stack_nr = 0;
7777}
7778
7779/*
7780 * Handle doing a BS for one character.
7781 * cc < 0: replace stack empty, just move cursor
7782 * cc == 0: character was inserted, delete it
7783 * cc > 0: character was replaced, put cc (first byte of original char) back
7784 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007785 * When "limit_col" is >= 0, don't delete before this column. Matters when
7786 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 */
7788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007789replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
7791 int cc;
7792#ifdef FEAT_VREPLACE
7793 int orig_len = 0;
7794 int ins_len;
7795 int orig_vcols = 0;
7796 colnr_T start_vcol;
7797 char_u *p;
7798 int i;
7799 int vcol;
7800#endif
7801
7802 cc = replace_pop();
7803 if (cc > 0)
7804 {
7805#ifdef FEAT_VREPLACE
7806 if (State & VREPLACE_FLAG)
7807 {
7808 /* Get the number of screen cells used by the character we are
7809 * going to delete. */
7810 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7811 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7812 }
7813#endif
7814#ifdef FEAT_MBYTE
7815 if (has_mbyte)
7816 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007817 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818# ifdef FEAT_VREPLACE
7819 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007820 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821# endif
7822 replace_push(cc);
7823 }
7824 else
7825#endif
7826 {
7827 pchar_cursor(cc);
7828#ifdef FEAT_VREPLACE
7829 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007830 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831#endif
7832 }
7833 replace_pop_ins();
7834
7835#ifdef FEAT_VREPLACE
7836 if (State & VREPLACE_FLAG)
7837 {
7838 /* Get the number of screen cells used by the inserted characters */
7839 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007840 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 vcol = start_vcol;
7842 for (i = 0; i < ins_len; ++i)
7843 {
7844 vcol += chartabsize(p + i, vcol);
7845#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007846 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847#endif
7848 }
7849 vcol -= start_vcol;
7850
7851 /* Delete spaces that were inserted after the cursor to keep the
7852 * text aligned. */
7853 curwin->w_cursor.col += ins_len;
7854 while (vcol > orig_vcols && gchar_cursor() == ' ')
7855 {
7856 del_char(FALSE);
7857 ++orig_vcols;
7858 }
7859 curwin->w_cursor.col -= ins_len;
7860 }
7861#endif
7862
7863 /* mark the buffer as changed and prepare for displaying */
7864 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7865 }
7866 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007867 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868}
7869
7870#ifdef FEAT_CINDENT
7871/*
7872 * Return TRUE if C-indenting is on.
7873 */
7874 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007875cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876{
7877 return (!p_paste && (curbuf->b_p_cin
7878# ifdef FEAT_EVAL
7879 || *curbuf->b_p_inde != NUL
7880# endif
7881 ));
7882}
7883#endif
7884
7885#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7886/*
7887 * Re-indent the current line, based on the current contents of it and the
7888 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7889 * confused what all the part that handles Control-T is doing that I'm not.
7890 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7891 */
7892
7893 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007894fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007896 int amount = get_the_indent();
7897
7898 if (amount >= 0)
7899 {
7900 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7901 if (linewhite(curwin->w_cursor.lnum))
7902 did_ai = TRUE; /* delete the indent if the line stays empty */
7903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904}
7905
7906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007907fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908{
7909 if (p_paste)
7910 return;
7911# ifdef FEAT_LISP
7912 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7913 fixthisline(get_lisp_indent);
7914# endif
7915# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7916 else
7917# endif
7918# ifdef FEAT_CINDENT
7919 if (cindent_on())
7920 do_c_expr_indent();
7921# endif
7922}
7923
7924#endif
7925
7926#ifdef FEAT_CINDENT
7927/*
7928 * return TRUE if 'cinkeys' contains the key "keytyped",
7929 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007930 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7932 *
7933 * "keytyped" can have a few special values:
7934 * KEY_OPEN_FORW
7935 * KEY_OPEN_BACK
7936 * KEY_COMPLETE just finished completion.
7937 *
7938 * If line_is_empty is TRUE accept keys with '0' before them.
7939 */
7940 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007941in_cinkeys(
7942 int keytyped,
7943 int when,
7944 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
7946 char_u *look;
7947 int try_match;
7948 int try_match_word;
7949 char_u *p;
7950 char_u *line;
7951 int icase;
7952 int i;
7953
Bram Moolenaar30848942009-12-24 14:46:12 +00007954 if (keytyped == NUL)
7955 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7956 return FALSE;
7957
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958#ifdef FEAT_EVAL
7959 if (*curbuf->b_p_inde != NUL)
7960 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7961 else
7962#endif
7963 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7964 while (*look)
7965 {
7966 /*
7967 * Find out if we want to try a match with this key, depending on
7968 * 'when' and a '*' or '!' before the key.
7969 */
7970 switch (when)
7971 {
7972 case '*': try_match = (*look == '*'); break;
7973 case '!': try_match = (*look == '!'); break;
7974 default: try_match = (*look != '*'); break;
7975 }
7976 if (*look == '*' || *look == '!')
7977 ++look;
7978
7979 /*
7980 * If there is a '0', only accept a match if the line is empty.
7981 * But may still match when typing last char of a word.
7982 */
7983 if (*look == '0')
7984 {
7985 try_match_word = try_match;
7986 if (!line_is_empty)
7987 try_match = FALSE;
7988 ++look;
7989 }
7990 else
7991 try_match_word = FALSE;
7992
7993 /*
7994 * does it look like a control character?
7995 */
7996 if (*look == '^'
7997#ifdef EBCDIC
7998 && (Ctrl_chr(look[1]) != 0)
7999#else
8000 && look[1] >= '?' && look[1] <= '_'
8001#endif
8002 )
8003 {
8004 if (try_match && keytyped == Ctrl_chr(look[1]))
8005 return TRUE;
8006 look += 2;
8007 }
8008 /*
8009 * 'o' means "o" command, open forward.
8010 * 'O' means "O" command, open backward.
8011 */
8012 else if (*look == 'o')
8013 {
8014 if (try_match && keytyped == KEY_OPEN_FORW)
8015 return TRUE;
8016 ++look;
8017 }
8018 else if (*look == 'O')
8019 {
8020 if (try_match && keytyped == KEY_OPEN_BACK)
8021 return TRUE;
8022 ++look;
8023 }
8024
8025 /*
8026 * 'e' means to check for "else" at start of line and just before the
8027 * cursor.
8028 */
8029 else if (*look == 'e')
8030 {
8031 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8032 {
8033 p = ml_get_curline();
8034 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8035 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8036 return TRUE;
8037 }
8038 ++look;
8039 }
8040
8041 /*
8042 * ':' only causes an indent if it is at the end of a label or case
8043 * statement, or when it was before typing the ':' (to fix
8044 * class::method for C++).
8045 */
8046 else if (*look == ':')
8047 {
8048 if (try_match && keytyped == ':')
8049 {
8050 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008051 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008053 /* Need to get the line again after cin_islabel(). */
8054 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 if (curwin->w_cursor.col > 2
8056 && p[curwin->w_cursor.col - 1] == ':'
8057 && p[curwin->w_cursor.col - 2] == ':')
8058 {
8059 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008060 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008061 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 p = ml_get_curline();
8063 p[curwin->w_cursor.col - 1] = ':';
8064 if (i)
8065 return TRUE;
8066 }
8067 }
8068 ++look;
8069 }
8070
8071
8072 /*
8073 * Is it a key in <>, maybe?
8074 */
8075 else if (*look == '<')
8076 {
8077 if (try_match)
8078 {
8079 /*
8080 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8081 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8082 * >, *, : and ! keys if they really really want to.
8083 */
8084 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8085 && keytyped == look[1])
8086 return TRUE;
8087
8088 if (keytyped == get_special_key_code(look + 1))
8089 return TRUE;
8090 }
8091 while (*look && *look != '>')
8092 look++;
8093 while (*look == '>')
8094 look++;
8095 }
8096
8097 /*
8098 * Is it a word: "=word"?
8099 */
8100 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8101 {
8102 ++look;
8103 if (*look == '~')
8104 {
8105 icase = TRUE;
8106 ++look;
8107 }
8108 else
8109 icase = FALSE;
8110 p = vim_strchr(look, ',');
8111 if (p == NULL)
8112 p = look + STRLEN(look);
8113 if ((try_match || try_match_word)
8114 && curwin->w_cursor.col >= (colnr_T)(p - look))
8115 {
8116 int match = FALSE;
8117
8118#ifdef FEAT_INS_EXPAND
8119 if (keytyped == KEY_COMPLETE)
8120 {
8121 char_u *s;
8122
8123 /* Just completed a word, check if it starts with "look".
8124 * search back for the start of a word. */
8125 line = ml_get_curline();
8126# ifdef FEAT_MBYTE
8127 if (has_mbyte)
8128 {
8129 char_u *n;
8130
8131 for (s = line + curwin->w_cursor.col; s > line; s = n)
8132 {
8133 n = mb_prevptr(line, s);
8134 if (!vim_iswordp(n))
8135 break;
8136 }
8137 }
8138 else
8139# endif
8140 for (s = line + curwin->w_cursor.col; s > line; --s)
8141 if (!vim_iswordc(s[-1]))
8142 break;
8143 if (s + (p - look) <= line + curwin->w_cursor.col
8144 && (icase
8145 ? MB_STRNICMP(s, look, p - look)
8146 : STRNCMP(s, look, p - look)) == 0)
8147 match = TRUE;
8148 }
8149 else
8150#endif
8151 /* TODO: multi-byte */
8152 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8153 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8154 {
8155 line = ml_get_cursor();
8156 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8157 || !vim_iswordc(line[-(p - look) - 1]))
8158 && (icase
8159 ? MB_STRNICMP(line - (p - look), look, p - look)
8160 : STRNCMP(line - (p - look), look, p - look))
8161 == 0)
8162 match = TRUE;
8163 }
8164 if (match && try_match_word && !try_match)
8165 {
8166 /* "0=word": Check if there are only blanks before the
8167 * word. */
8168 line = ml_get_curline();
8169 if ((int)(skipwhite(line) - line) !=
8170 (int)(curwin->w_cursor.col - (p - look)))
8171 match = FALSE;
8172 }
8173 if (match)
8174 return TRUE;
8175 }
8176 look = p;
8177 }
8178
8179 /*
8180 * ok, it's a boring generic character.
8181 */
8182 else
8183 {
8184 if (try_match && *look == keytyped)
8185 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008186 if (*look != NUL)
8187 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 }
8189
8190 /*
8191 * Skip over ", ".
8192 */
8193 look = skip_to_option_part(look);
8194 }
8195 return FALSE;
8196}
8197#endif /* FEAT_CINDENT */
8198
8199#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8200/*
8201 * Map Hebrew keyboard when in hkmap mode.
8202 */
8203 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008204hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205{
8206 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8207 {
8208 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8209 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8210 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8211 static char_u map[26] =
8212 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8213 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8214 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8215 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8216 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8217 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8218 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8219 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8220 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8221
8222 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8223 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8224 /* '-1'='sofit' */
8225 else if (c == 'x')
8226 return 'X';
8227 else if (c == 'q')
8228 return '\''; /* {geresh}={'} */
8229 else if (c == 246)
8230 return ' '; /* \"o --> ' ' for a german keyboard */
8231 else if (c == 228)
8232 return ' '; /* \"a --> ' ' -- / -- */
8233 else if (c == 252)
8234 return ' '; /* \"u --> ' ' -- / -- */
8235#ifdef EBCDIC
8236 else if (islower(c))
8237#else
8238 /* NOTE: islower() does not do the right thing for us on Linux so we
8239 * do this the same was as 5.7 and previous, so it works correctly on
8240 * all systems. Specifically, the e.g. Delete and Arrow keys are
8241 * munged and won't work if e.g. searching for Hebrew text.
8242 */
8243 else if (c >= 'a' && c <= 'z')
8244#endif
8245 return (int)(map[CharOrdLow(c)] + p_aleph);
8246 else
8247 return c;
8248 }
8249 else
8250 {
8251 switch (c)
8252 {
8253 case '`': return ';';
8254 case '/': return '.';
8255 case '\'': return ',';
8256 case 'q': return '/';
8257 case 'w': return '\'';
8258
8259 /* Hebrew letters - set offset from 'a' */
8260 case ',': c = '{'; break;
8261 case '.': c = 'v'; break;
8262 case ';': c = 't'; break;
8263 default: {
8264 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8265
8266#ifdef EBCDIC
8267 /* see note about islower() above */
8268 if (!islower(c))
8269#else
8270 if (c < 'a' || c > 'z')
8271#endif
8272 return c;
8273 c = str[CharOrdLow(c)];
8274 break;
8275 }
8276 }
8277
8278 return (int)(CharOrdLow(c) + p_aleph);
8279 }
8280}
8281#endif
8282
8283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008284ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285{
8286 int need_redraw = FALSE;
8287 int regname;
8288 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008289 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290
8291 /*
8292 * If we are going to wait for a character, show a '"'.
8293 */
8294 pc_status = PC_STATUS_UNSET;
8295 if (redrawing() && !char_avail())
8296 {
8297 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008298 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299
8300 edit_putchar('"', TRUE);
8301#ifdef FEAT_CMDL_INFO
8302 add_to_showcmd_c(Ctrl_R);
8303#endif
8304 }
8305
8306#ifdef USE_ON_FLY_SCROLL
8307 dont_scroll = TRUE; /* disallow scrolling here */
8308#endif
8309
8310 /*
8311 * Don't map the register name. This also prevents the mode message to be
8312 * deleted when ESC is hit.
8313 */
8314 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008315 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8318 {
8319 /* Get a third key for literal register insertion */
8320 literally = regname;
8321#ifdef FEAT_CMDL_INFO
8322 add_to_showcmd_c(literally);
8323#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008324 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 }
8327 --no_mapping;
8328
8329#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008330 /* Don't call u_sync() while typing the expression or giving an error
8331 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 ++no_u_sync;
8333 if (regname == '=')
8334 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008335# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008337# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008338 /* Sync undo when evaluating the expression calls setline() or
8339 * append(), so that it can be undone separately. */
8340 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008341
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008343# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 /* Restore the Input Method. */
8345 if (im_on)
8346 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008347# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008349 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8350 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008351 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 else
8355 {
8356#endif
8357 if (literally == Ctrl_O || literally == Ctrl_P)
8358 {
8359 /* Append the command to the redo buffer. */
8360 AppendCharToRedobuff(Ctrl_R);
8361 AppendCharToRedobuff(literally);
8362 AppendCharToRedobuff(regname);
8363
8364 do_put(regname, BACKWARD, 1L,
8365 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8366 }
8367 else if (insert_reg(regname, literally) == FAIL)
8368 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008369 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 need_redraw = TRUE; /* remove the '"' */
8371 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008372 else if (stop_insert_mode)
8373 /* When the '=' register was used and a function was invoked that
8374 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8375 * insert anything, need to remove the '"' */
8376 need_redraw = TRUE;
8377
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378#ifdef FEAT_EVAL
8379 }
8380 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008381 if (u_sync_once == 1)
8382 ins_need_undo = TRUE;
8383 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384#endif
8385#ifdef FEAT_CMDL_INFO
8386 clear_showcmd();
8387#endif
8388
8389 /* If the inserted register is empty, we need to remove the '"' */
8390 if (need_redraw || stuff_empty())
8391 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008392
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008393 /* Disallow starting Visual mode here, would get a weird mode. */
8394 if (!vis_active && VIsual_active)
8395 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396}
8397
8398/*
8399 * CTRL-G commands in Insert mode.
8400 */
8401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008402ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 int c;
8405
8406#ifdef FEAT_INS_EXPAND
8407 /* Right after CTRL-X the cursor will be after the ruler. */
8408 setcursor();
8409#endif
8410
8411 /*
8412 * Don't map the second key. This also prevents the mode message to be
8413 * deleted when ESC is hit.
8414 */
8415 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008416 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 --no_mapping;
8418 switch (c)
8419 {
8420 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8421 case K_UP:
8422 case Ctrl_K:
8423 case 'k': ins_up(TRUE);
8424 break;
8425
8426 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8427 case K_DOWN:
8428 case Ctrl_J:
8429 case 'j': ins_down(TRUE);
8430 break;
8431
8432 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008433 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008435
8436 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008437 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008438 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008439 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 break;
8441
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008442 /* CTRL-G U: do not break undo with the next char */
8443 case 'U':
8444 /* Allow one left/right cursor movement with the next char,
8445 * without breaking undo. */
8446 dont_sync_undo = MAYBE;
8447 break;
8448
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008450 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 }
8452}
8453
8454/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008455 * CTRL-^ in Insert mode.
8456 */
8457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008458ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008459{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008460 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008461 {
8462 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8463 if (State & LANGMAP)
8464 {
8465 curbuf->b_p_iminsert = B_IMODE_NONE;
8466 State &= ~LANGMAP;
8467 }
8468 else
8469 {
8470 curbuf->b_p_iminsert = B_IMODE_LMAP;
8471 State |= LANGMAP;
8472#ifdef USE_IM_CONTROL
8473 im_set_active(FALSE);
8474#endif
8475 }
8476 }
8477#ifdef USE_IM_CONTROL
8478 else
8479 {
8480 /* There are no ":lmap" mappings, toggle IM */
8481 if (im_get_status())
8482 {
8483 curbuf->b_p_iminsert = B_IMODE_NONE;
8484 im_set_active(FALSE);
8485 }
8486 else
8487 {
8488 curbuf->b_p_iminsert = B_IMODE_IM;
8489 State &= ~LANGMAP;
8490 im_set_active(TRUE);
8491 }
8492 }
8493#endif
8494 set_iminsert_global();
8495 showmode();
8496#ifdef FEAT_GUI
8497 /* may show different cursor shape or color */
8498 if (gui.in_use)
8499 gui_update_cursor(TRUE, FALSE);
8500#endif
8501#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8502 /* Show/unshow value of 'keymap' in status lines. */
8503 status_redraw_curbuf();
8504#endif
8505}
8506
8507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 * Handle ESC in insert mode.
8509 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8510 * insert.
8511 */
8512 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008513ins_esc(
8514 long *count,
8515 int cmdchar,
8516 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517{
8518 int temp;
8519 static int disabled_redraw = FALSE;
8520
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008521#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008522 check_spell_redraw();
8523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524#if defined(FEAT_HANGULIN)
8525# if defined(ESC_CHG_TO_ENG_MODE)
8526 hangul_input_state_set(0);
8527# endif
8528 if (composing_hangul)
8529 {
8530 push_raw_key(composing_hangul_buffer, 2);
8531 composing_hangul = 0;
8532 }
8533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534
8535 temp = curwin->w_cursor.col;
8536 if (disabled_redraw)
8537 {
8538 --RedrawingDisabled;
8539 disabled_redraw = FALSE;
8540 }
8541 if (!arrow_used)
8542 {
8543 /*
8544 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008545 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8546 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 */
8548 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008549 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550
8551 /*
8552 * Repeating insert may take a long time. Check for
8553 * interrupt now and then.
8554 */
8555 if (*count > 0)
8556 {
8557 line_breakcheck();
8558 if (got_int)
8559 *count = 0;
8560 }
8561
8562 if (--*count > 0) /* repeat what was typed */
8563 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008564 /* Vi repeats the insert without replacing characters. */
8565 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8566 State &= ~REPLACE_FLAG;
8567
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 (void)start_redo_ins();
8569 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008570 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 ++RedrawingDisabled;
8572 disabled_redraw = TRUE;
8573 return FALSE; /* repeat the insert */
8574 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008575 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 undisplay_dollar();
8577 }
8578
8579 /* When an autoindent was removed, curswant stays after the
8580 * indent */
8581 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8582 curwin->w_set_curswant = TRUE;
8583
8584 /* Remember the last Insert position in the '^ mark. */
8585 if (!cmdmod.keepjumps)
8586 curbuf->b_last_insert = curwin->w_cursor;
8587
8588 /*
8589 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008590 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008592 if (!nomove
8593 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594#ifdef FEAT_VIRTUALEDIT
8595 || curwin->w_cursor.coladd > 0
8596#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008597 )
8598 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008599 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600#ifdef FEAT_RIGHTLEFT
8601 && !revins_on
8602#endif
8603 )
8604 {
8605#ifdef FEAT_VIRTUALEDIT
8606 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8607 {
8608 oneleft();
8609 if (restart_edit != NUL)
8610 ++curwin->w_cursor.coladd;
8611 }
8612 else
8613#endif
8614 {
8615 --curwin->w_cursor.col;
8616#ifdef FEAT_MBYTE
8617 /* Correct cursor for multi-byte character. */
8618 if (has_mbyte)
8619 mb_adjust_cursor();
8620#endif
8621 }
8622 }
8623
8624#ifdef USE_IM_CONTROL
8625 /* Disable IM to allow typing English directly for Normal mode commands.
8626 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8627 * well). */
8628 if (!(State & LANGMAP))
8629 im_save_status(&curbuf->b_p_iminsert);
8630 im_set_active(FALSE);
8631#endif
8632
8633 State = NORMAL;
8634 /* need to position cursor again (e.g. when on a TAB ) */
8635 changed_cline_bef_curs();
8636
8637#ifdef FEAT_MOUSE
8638 setmouse();
8639#endif
8640#ifdef CURSOR_SHAPE
8641 ui_cursor_shape(); /* may show different cursor shape */
8642#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008643 if (!p_ek)
8644 /* Re-enable bracketed paste mode. */
8645 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646
8647 /*
8648 * When recording or for CTRL-O, need to display the new mode.
8649 * Otherwise remove the mode message.
8650 */
8651 if (Recording || restart_edit != NUL)
8652 showmode();
8653 else if (p_smd)
8654 MSG("");
8655
8656 return TRUE; /* exit Insert mode */
8657}
8658
8659#ifdef FEAT_RIGHTLEFT
8660/*
8661 * Toggle language: hkmap and revins_on.
8662 * Move to end of reverse inserted text.
8663 */
8664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008665ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666{
8667 if (revins_on && revins_chars && revins_scol >= 0)
8668 {
8669 while (gchar_cursor() != NUL && revins_chars--)
8670 ++curwin->w_cursor.col;
8671 }
8672 p_ri = !p_ri;
8673 revins_on = (State == INSERT && p_ri);
8674 if (revins_on)
8675 {
8676 revins_scol = curwin->w_cursor.col;
8677 revins_legal++;
8678 revins_chars = 0;
8679 undisplay_dollar();
8680 }
8681 else
8682 revins_scol = -1;
8683#ifdef FEAT_FKMAP
8684 if (p_altkeymap)
8685 {
8686 /*
8687 * to be consistent also for redo command, using '.'
8688 * set arrow_used to true and stop it - causing to redo
8689 * characters entered in one mode (normal/reverse insert).
8690 */
8691 arrow_used = TRUE;
8692 (void)stop_arrow();
8693 p_fkmap = curwin->w_p_rl ^ p_ri;
8694 if (p_fkmap && p_ri)
8695 State = INSERT;
8696 }
8697 else
8698#endif
8699 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8700 showmode();
8701}
8702#endif
8703
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704/*
8705 * If 'keymodel' contains "startsel", may start selection.
8706 * Returns TRUE when a CTRL-O and other keys stuffed.
8707 */
8708 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008709ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710{
8711 if (km_startsel)
8712 switch (c)
8713 {
8714 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 case K_PAGEUP:
8717 case K_KPAGEUP:
8718 case K_PAGEDOWN:
8719 case K_KPAGEDOWN:
8720# ifdef MACOS
8721 case K_LEFT:
8722 case K_RIGHT:
8723 case K_UP:
8724 case K_DOWN:
8725 case K_END:
8726 case K_HOME:
8727# endif
8728 if (!(mod_mask & MOD_MASK_SHIFT))
8729 break;
8730 /* FALLTHROUGH */
8731 case K_S_LEFT:
8732 case K_S_RIGHT:
8733 case K_S_UP:
8734 case K_S_DOWN:
8735 case K_S_END:
8736 case K_S_HOME:
8737 /* Start selection right away, the cursor can move with
8738 * CTRL-O when beyond the end of the line. */
8739 start_selection();
8740
8741 /* Execute the key in (insert) Select mode. */
8742 stuffcharReadbuff(Ctrl_O);
8743 if (mod_mask)
8744 {
8745 char_u buf[4];
8746
8747 buf[0] = K_SPECIAL;
8748 buf[1] = KS_MODIFIER;
8749 buf[2] = mod_mask;
8750 buf[3] = NUL;
8751 stuffReadbuff(buf);
8752 }
8753 stuffcharReadbuff(c);
8754 return TRUE;
8755 }
8756 return FALSE;
8757}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758
8759/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008760 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008761 */
8762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008763ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008764{
8765#ifdef FEAT_FKMAP
8766 if (p_fkmap && p_ri)
8767 {
8768 beep_flush();
8769 EMSG(farsi_text_3); /* encoded in Farsi */
8770 return;
8771 }
8772#endif
8773
8774#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008775# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008776 set_vim_var_string(VV_INSERTMODE,
8777 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008778# ifdef FEAT_VREPLACE
8779 replaceState == VREPLACE ? "v" :
8780# endif
8781 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008782# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008783 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8784#endif
8785 if (State & REPLACE_FLAG)
8786 State = INSERT | (State & LANGMAP);
8787 else
8788 State = replaceState | (State & LANGMAP);
8789 AppendCharToRedobuff(K_INS);
8790 showmode();
8791#ifdef CURSOR_SHAPE
8792 ui_cursor_shape(); /* may show different cursor shape */
8793#endif
8794}
8795
8796/*
8797 * Pressed CTRL-O in Insert mode.
8798 */
8799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008800ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008801{
8802#ifdef FEAT_VREPLACE
8803 if (State & VREPLACE_FLAG)
8804 restart_edit = 'V';
8805 else
8806#endif
8807 if (State & REPLACE_FLAG)
8808 restart_edit = 'R';
8809 else
8810 restart_edit = 'I';
8811#ifdef FEAT_VIRTUALEDIT
8812 if (virtual_active())
8813 ins_at_eol = FALSE; /* cursor always keeps its column */
8814 else
8815#endif
8816 ins_at_eol = (gchar_cursor() == NUL);
8817}
8818
8819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 * If the cursor is on an indent, ^T/^D insert/delete one
8821 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008822 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 * with vi. But vi only supports ^T and ^D after an
8824 * autoindent, we support it everywhere.
8825 */
8826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008827ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828{
8829 if (stop_arrow() == FAIL)
8830 return;
8831 AppendCharToRedobuff(c);
8832
8833 /*
8834 * 0^D and ^^D: remove all indent.
8835 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008836 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8837 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {
8839 --curwin->w_cursor.col;
8840 (void)del_char(FALSE); /* delete the '^' or '0' */
8841 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8842 if (State & REPLACE_FLAG)
8843 replace_pop_ins();
8844 if (lastc == '^')
8845 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008846 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 }
8848 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008849 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850
8851 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8852 did_ai = FALSE;
8853#ifdef FEAT_SMARTINDENT
8854 did_si = FALSE;
8855 can_si = FALSE;
8856 can_si_back = FALSE;
8857#endif
8858#ifdef FEAT_CINDENT
8859 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8860#endif
8861}
8862
8863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008864ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
8866 int temp;
8867
8868 if (stop_arrow() == FAIL)
8869 return;
8870 if (gchar_cursor() == NUL) /* delete newline */
8871 {
8872 temp = curwin->w_cursor.col;
8873 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008874 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008875 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 else
8877 curwin->w_cursor.col = temp;
8878 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008879 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8880 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 did_ai = FALSE;
8882#ifdef FEAT_SMARTINDENT
8883 did_si = FALSE;
8884 can_si = FALSE;
8885 can_si_back = FALSE;
8886#endif
8887 AppendCharToRedobuff(K_DEL);
8888}
8889
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008890static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008891
8892/*
8893 * Delete one character for ins_bs().
8894 */
8895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008896ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008897{
8898 dec_cursor();
8899 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8900 if (State & REPLACE_FLAG)
8901 {
8902 /* Don't delete characters before the insert point when in
8903 * Replace mode */
8904 if (curwin->w_cursor.lnum != Insstart.lnum
8905 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008906 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008907 }
8908 else
8909 (void)del_char(FALSE);
8910}
8911
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912/*
8913 * Handle Backspace, delete-word and delete-line in Insert mode.
8914 * Return TRUE when backspace was actually used.
8915 */
8916 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008917ins_bs(
8918 int c,
8919 int mode,
8920 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921{
8922 linenr_T lnum;
8923 int cc;
8924 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008925 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 colnr_T mincol;
8927 int did_backspace = FALSE;
8928 int in_indent;
8929 int oldState;
8930#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008931 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932#endif
8933
8934 /*
8935 * can't delete anything in an empty file
8936 * can't backup past first character in buffer
8937 * can't backup past starting point unless 'backspace' > 1
8938 * can backup to a previous line if 'backspace' == 0
8939 */
8940 if ( bufempty()
8941 || (
8942#ifdef FEAT_RIGHTLEFT
8943 !revins_on &&
8944#endif
8945 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8946 || (!can_bs(BS_START)
8947 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008948 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8949 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8951 && curwin->w_cursor.col <= ai_col)
8952 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8953 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008954 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 return FALSE;
8956 }
8957
8958 if (stop_arrow() == FAIL)
8959 return FALSE;
8960 in_indent = inindent(0);
8961#ifdef FEAT_CINDENT
8962 if (in_indent)
8963 can_cindent = FALSE;
8964#endif
8965#ifdef FEAT_COMMENTS
8966 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8967#endif
8968#ifdef FEAT_RIGHTLEFT
8969 if (revins_on) /* put cursor after last inserted char */
8970 inc_cursor();
8971#endif
8972
8973#ifdef FEAT_VIRTUALEDIT
8974 /* Virtualedit:
8975 * BACKSPACE_CHAR eats a virtual space
8976 * BACKSPACE_WORD eats all coladd
8977 * BACKSPACE_LINE eats all coladd and keeps going
8978 */
8979 if (curwin->w_cursor.coladd > 0)
8980 {
8981 if (mode == BACKSPACE_CHAR)
8982 {
8983 --curwin->w_cursor.coladd;
8984 return TRUE;
8985 }
8986 if (mode == BACKSPACE_WORD)
8987 {
8988 curwin->w_cursor.coladd = 0;
8989 return TRUE;
8990 }
8991 curwin->w_cursor.coladd = 0;
8992 }
8993#endif
8994
8995 /*
8996 * delete newline!
8997 */
8998 if (curwin->w_cursor.col == 0)
8999 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009000 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009001 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002#ifdef FEAT_RIGHTLEFT
9003 || revins_on
9004#endif
9005 )
9006 {
9007 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9008 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9009 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009010 --Insstart.lnum;
9011 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 }
9013 /*
9014 * In replace mode:
9015 * cc < 0: NL was inserted, delete it
9016 * cc >= 0: NL was replaced, put original characters back
9017 */
9018 cc = -1;
9019 if (State & REPLACE_FLAG)
9020 cc = replace_pop(); /* returns -1 if NL was inserted */
9021 /*
9022 * In replace mode, in the line we started replacing, we only move the
9023 * cursor.
9024 */
9025 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9026 {
9027 dec_cursor();
9028 }
9029 else
9030 {
9031#ifdef FEAT_VREPLACE
9032 if (!(State & VREPLACE_FLAG)
9033 || curwin->w_cursor.lnum > orig_line_count)
9034#endif
9035 {
9036 temp = gchar_cursor(); /* remember current char */
9037 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009038
9039 /* When "aw" is in 'formatoptions' we must delete the space at
9040 * the end of the line, otherwise the line will be broken
9041 * again when auto-formatting. */
9042 if (has_format_option(FO_AUTO)
9043 && has_format_option(FO_WHITE_PAR))
9044 {
9045 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9046 TRUE);
9047 int len;
9048
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009049 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009050 if (len > 0 && ptr[len - 1] == ' ')
9051 ptr[len - 1] = NUL;
9052 }
9053
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009054 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 if (temp == NUL && gchar_cursor() != NUL)
9056 inc_cursor();
9057 }
9058#ifdef FEAT_VREPLACE
9059 else
9060 dec_cursor();
9061#endif
9062
9063 /*
9064 * In REPLACE mode we have to put back the text that was replaced
9065 * by the NL. On the replace stack is first a NUL-terminated
9066 * sequence of characters that were deleted and then the
9067 * characters that NL replaced.
9068 */
9069 if (State & REPLACE_FLAG)
9070 {
9071 /*
9072 * Do the next ins_char() in NORMAL state, to
9073 * prevent ins_char() from replacing characters and
9074 * avoiding showmatch().
9075 */
9076 oldState = State;
9077 State = NORMAL;
9078 /*
9079 * restore characters (blanks) deleted after cursor
9080 */
9081 while (cc > 0)
9082 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009083 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084#ifdef FEAT_MBYTE
9085 mb_replace_pop_ins(cc);
9086#else
9087 ins_char(cc);
9088#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009089 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 cc = replace_pop();
9091 }
9092 /* restore the characters that NL replaced */
9093 replace_pop_ins();
9094 State = oldState;
9095 }
9096 }
9097 did_ai = FALSE;
9098 }
9099 else
9100 {
9101 /*
9102 * Delete character(s) before the cursor.
9103 */
9104#ifdef FEAT_RIGHTLEFT
9105 if (revins_on) /* put cursor on last inserted char */
9106 dec_cursor();
9107#endif
9108 mincol = 0;
9109 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009110 if (mode == BACKSPACE_LINE
9111 && (curbuf->b_p_ai
9112#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009113 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009114#endif
9115 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116#ifdef FEAT_RIGHTLEFT
9117 && !revins_on
9118#endif
9119 )
9120 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009121 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009123 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009125 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 }
9127
9128 /*
9129 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9130 */
9131 if ( mode == BACKSPACE_CHAR
9132 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009133 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009134 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 && (*(ml_get_cursor() - 1) == TAB
9136 || (*(ml_get_cursor() - 1) == ' '
9137 && (!*inserted_space_p
9138 || arrow_used))))))
9139 {
9140 int ts;
9141 colnr_T vcol;
9142 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009143 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144
9145 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009146 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009147 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009149 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 /* Compute the virtual column where we want to be. Since
9151 * 'showbreak' may get in the way, need to get the last column of
9152 * the previous character. */
9153 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009154 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 dec_cursor();
9156 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9157 inc_cursor();
9158 want_vcol = (want_vcol / ts) * ts;
9159
9160 /* delete characters until we are at or before want_vcol */
9161 while (vcol > want_vcol
9162 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009163 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164
9165 /* insert extra spaces until we are at want_vcol */
9166 while (vcol < want_vcol)
9167 {
9168 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009169 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9170 && curwin->w_cursor.col < Insstart_orig.col)
9171 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172
9173#ifdef FEAT_VREPLACE
9174 if (State & VREPLACE_FLAG)
9175 ins_char(' ');
9176 else
9177#endif
9178 {
9179 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009180 if ((State & REPLACE_FLAG))
9181 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 }
9183 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9184 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009185
9186 /* If we are now back where we started delete one character. Can
9187 * happen when using 'sts' and 'linebreak'. */
9188 if (vcol >= start_vcol)
9189 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 }
9191
9192 /*
9193 * Delete upto starting point, start of line or previous word.
9194 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009195 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009197#ifdef FEAT_MBYTE
9198 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009200 if (has_mbyte)
9201 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009203 do
9204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009206 if (!revins_on) /* put cursor on char to be deleted */
9207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009209
9210 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009212 /* look multi-byte character class */
9213 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009215 prev_cclass = cclass;
9216 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009219
9220 /* start of word? */
9221 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9222 {
9223 mode = BACKSPACE_WORD_NOT_SPACE;
9224 temp = vim_iswordc(cc);
9225 }
9226 /* end of word? */
9227 else if (mode == BACKSPACE_WORD_NOT_SPACE
9228 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9229#ifdef FEAT_MBYTE
9230 || prev_cclass != cclass
9231#endif
9232 ))
9233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009235 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009237 inc_cursor();
9238#ifdef FEAT_RIGHTLEFT
9239 else if (State & REPLACE_FLAG)
9240 dec_cursor();
9241#endif
9242 break;
9243 }
9244 if (State & REPLACE_FLAG)
9245 replace_do_bs(-1);
9246 else
9247 {
9248#ifdef FEAT_MBYTE
9249 if (enc_utf8 && p_deco)
9250 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9251#endif
9252 (void)del_char(FALSE);
9253#ifdef FEAT_MBYTE
9254 /*
9255 * If there are combining characters and 'delcombine' is set
9256 * move the cursor back. Don't back up before the base
9257 * character.
9258 */
9259 if (enc_utf8 && p_deco && cpc[0] != NUL)
9260 inc_cursor();
9261#endif
9262#ifdef FEAT_RIGHTLEFT
9263 if (revins_chars)
9264 {
9265 revins_chars--;
9266 revins_legal++;
9267 }
9268 if (revins_on && gchar_cursor() == NUL)
9269 break;
9270#endif
9271 }
9272 /* Just a single backspace?: */
9273 if (mode == BACKSPACE_CHAR)
9274 break;
9275 } while (
9276#ifdef FEAT_RIGHTLEFT
9277 revins_on ||
9278#endif
9279 (curwin->w_cursor.col > mincol
9280 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9281 || curwin->w_cursor.col != Insstart_orig.col)));
9282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 did_backspace = TRUE;
9284 }
9285#ifdef FEAT_SMARTINDENT
9286 did_si = FALSE;
9287 can_si = FALSE;
9288 can_si_back = FALSE;
9289#endif
9290 if (curwin->w_cursor.col <= 1)
9291 did_ai = FALSE;
9292 /*
9293 * It's a little strange to put backspaces into the redo
9294 * buffer, but it makes auto-indent a lot easier to deal
9295 * with.
9296 */
9297 AppendCharToRedobuff(c);
9298
9299 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009300 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9301 && curwin->w_cursor.col < Insstart_orig.col)
9302 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303
9304 /* vi behaviour: the cursor moves backward but the character that
9305 * was there remains visible
9306 * Vim behaviour: the cursor moves backward and the character that
9307 * was there is erased from the screen.
9308 * We can emulate the vi behaviour by pretending there is a dollar
9309 * displayed even when there isn't.
9310 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009311 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 dollar_vcol = curwin->w_virtcol;
9313
Bram Moolenaarce3be472008-01-14 19:12:28 +00009314#ifdef FEAT_FOLDING
9315 /* When deleting a char the cursor line must never be in a closed fold.
9316 * E.g., when 'foldmethod' is indent and deleting the first non-white
9317 * char before a Tab. */
9318 if (did_backspace)
9319 foldOpenCursor();
9320#endif
9321
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 return did_backspace;
9323}
9324
9325#ifdef FEAT_MOUSE
9326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009327ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328{
9329 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009330 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331
9332# ifdef FEAT_GUI
9333 /* When GUI is active, also move/paste when 'mouse' is empty */
9334 if (!gui.in_use)
9335# endif
9336 if (!mouse_has(MOUSE_INSERT))
9337 return;
9338
9339 undisplay_dollar();
9340 tpos = curwin->w_cursor;
9341 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9342 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009343#ifdef FEAT_WINDOWS
9344 win_T *new_curwin = curwin;
9345
9346 if (curwin != old_curwin && win_valid(old_curwin))
9347 {
9348 /* Mouse took us to another window. We need to go back to the
9349 * previous one to stop insert there properly. */
9350 curwin = old_curwin;
9351 curbuf = curwin->w_buffer;
9352 }
9353#endif
9354 start_arrow(curwin == old_curwin ? &tpos : NULL);
9355#ifdef FEAT_WINDOWS
9356 if (curwin != new_curwin && win_valid(new_curwin))
9357 {
9358 curwin = new_curwin;
9359 curbuf = curwin->w_buffer;
9360 }
9361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362# ifdef FEAT_CINDENT
9363 can_cindent = TRUE;
9364# endif
9365 }
9366
9367#ifdef FEAT_WINDOWS
9368 /* redraw status lines (in case another window became active) */
9369 redraw_statuslines();
9370#endif
9371}
9372
9373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009374ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
9376 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009377# if defined(FEAT_WINDOWS)
9378 win_T *old_curwin = curwin;
9379# endif
9380# ifdef FEAT_INS_EXPAND
9381 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382# endif
9383
9384 tpos = curwin->w_cursor;
9385
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009386# ifdef FEAT_WINDOWS
9387 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 {
9389 int row, col;
9390
9391 row = mouse_row;
9392 col = mouse_col;
9393
9394 /* find the window at the pointer coordinates */
9395 curwin = mouse_find_win(&row, &col);
9396 curbuf = curwin->w_buffer;
9397 }
9398 if (curwin == old_curwin)
9399# endif
9400 undisplay_dollar();
9401
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009402# ifdef FEAT_INS_EXPAND
9403 /* Don't scroll the window in which completion is being done. */
9404 if (!pum_visible()
9405# if defined(FEAT_WINDOWS)
9406 || curwin != old_curwin
9407# endif
9408 )
9409# endif
9410 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009411 if (dir == MSCR_DOWN || dir == MSCR_UP)
9412 {
9413 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9414 scroll_redraw(dir,
9415 (long)(curwin->w_botline - curwin->w_topline));
9416 else
9417 scroll_redraw(dir, 3L);
9418 }
9419#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009420 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009421 {
9422 int val, step = 6;
9423
9424 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9425 step = W_WIDTH(curwin);
9426 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9427 if (val < 0)
9428 val = 0;
9429 gui_do_horiz_scroll(val, TRUE);
9430 }
9431#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009432# ifdef FEAT_INS_EXPAND
9433 did_scroll = TRUE;
9434# endif
9435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009437# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 curwin->w_redr_status = TRUE;
9439
9440 curwin = old_curwin;
9441 curbuf = curwin->w_buffer;
9442# endif
9443
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009444# ifdef FEAT_INS_EXPAND
9445 /* The popup menu may overlay the window, need to redraw it.
9446 * TODO: Would be more efficient to only redraw the windows that are
9447 * overlapped by the popup menu. */
9448 if (pum_visible() && did_scroll)
9449 {
9450 redraw_all_later(NOT_VALID);
9451 ins_compl_show_pum();
9452 }
9453# endif
9454
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 if (!equalpos(curwin->w_cursor, tpos))
9456 {
9457 start_arrow(&tpos);
9458# ifdef FEAT_CINDENT
9459 can_cindent = TRUE;
9460# endif
9461 }
9462}
9463#endif
9464
Bram Moolenaarec2da362017-01-21 20:04:22 +01009465/*
9466 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9467 * P_PE literally.
9468 * When "drop" is TRUE then consume the text and drop it.
9469 */
9470 int
9471bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9472{
9473 int c;
9474 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9475 int idx = 0;
9476 char_u *end = find_termcode((char_u *)"PE");
9477 int ret_char = -1;
9478 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009479 int save_paste = p_paste;
9480 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009481
9482 /* If the end code is too long we can't detect it, read everything. */
9483 if (STRLEN(end) >= NUMBUFLEN)
9484 end = NULL;
9485 ++no_mapping;
9486 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009487 p_paste = TRUE;
9488 curbuf->b_p_ai = FALSE;
9489
Bram Moolenaarec2da362017-01-21 20:04:22 +01009490 for (;;)
9491 {
9492 /* When the end is not defined read everything. */
9493 if (end == NULL && vpeekc() == NUL)
9494 break;
9495 c = plain_vgetc();
9496#ifdef FEAT_MBYTE
9497 if (has_mbyte)
9498 idx += (*mb_char2bytes)(c, buf + idx);
9499 else
9500#endif
9501 buf[idx++] = c;
9502 buf[idx] = NUL;
9503 if (end != NUL && STRNCMP(buf, end, idx) == 0)
9504 {
9505 if (end[idx] == NUL)
9506 break; /* Found the end of paste code. */
9507 continue;
9508 }
9509 if (!drop)
9510 {
9511 switch (mode)
9512 {
9513 case PASTE_CMDLINE:
9514 put_on_cmdline(buf, idx, TRUE);
9515 break;
9516
9517 case PASTE_EX:
9518 if (gap != NULL && ga_grow(gap, idx) == OK)
9519 {
9520 mch_memmove((char *)gap->ga_data + gap->ga_len,
9521 buf, (size_t)idx);
9522 gap->ga_len += idx;
9523 }
9524 break;
9525
9526 case PASTE_INSERT:
9527 if (stop_arrow() == OK)
9528 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009529 c = buf[0];
9530 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9531 ins_eol(c);
9532 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009533 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009534 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009535 AppendToRedobuffLit(buf, idx);
9536 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009537 }
9538 break;
9539
9540 case PASTE_ONE_CHAR:
9541 if (ret_char == -1)
9542 {
9543#ifdef FEAT_MBYTE
9544 if (has_mbyte)
9545 ret_char = (*mb_ptr2char)(buf);
9546 else
9547#endif
9548 ret_char = buf[0];
9549 }
9550 break;
9551 }
9552 }
9553 idx = 0;
9554 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009555
Bram Moolenaarec2da362017-01-21 20:04:22 +01009556 --no_mapping;
9557 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009558 p_paste = save_paste;
9559 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009560
9561 return ret_char;
9562}
9563
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009564#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009566ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009567{
9568 /* We will be leaving the current window, unless closing another tab. */
9569 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9570 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9571 {
9572 undisplay_dollar();
9573 start_arrow(&curwin->w_cursor);
9574# ifdef FEAT_CINDENT
9575 can_cindent = TRUE;
9576# endif
9577 }
9578
9579 if (c == K_TABLINE)
9580 goto_tabpage(current_tab);
9581 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009582 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009583 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009584 redraw_statuslines(); /* will redraw the tabline when needed */
9585 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009586}
9587#endif
9588
9589#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009591ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592{
9593 pos_T tpos;
9594
9595 undisplay_dollar();
9596 tpos = curwin->w_cursor;
9597 if (gui_do_scroll())
9598 {
9599 start_arrow(&tpos);
9600# ifdef FEAT_CINDENT
9601 can_cindent = TRUE;
9602# endif
9603 }
9604}
9605
9606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009607ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
9609 pos_T tpos;
9610
9611 undisplay_dollar();
9612 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009613 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 {
9615 start_arrow(&tpos);
9616# ifdef FEAT_CINDENT
9617 can_cindent = TRUE;
9618# endif
9619 }
9620}
9621#endif
9622
9623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009624ins_left(
9625 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626{
9627 pos_T tpos;
9628
9629#ifdef FEAT_FOLDING
9630 if ((fdo_flags & FDO_HOR) && KeyTyped)
9631 foldOpenCursor();
9632#endif
9633 undisplay_dollar();
9634 tpos = curwin->w_cursor;
9635 if (oneleft() == OK)
9636 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009637#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9638 /* Only call start_arrow() when not busy with preediting, it will
9639 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9640 if (!im_is_preediting())
9641#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009642 {
9643 start_arrow_with_change(&tpos, end_change);
9644 if (!end_change)
9645 AppendCharToRedobuff(K_LEFT);
9646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647#ifdef FEAT_RIGHTLEFT
9648 /* If exit reversed string, position is fixed */
9649 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9650 revins_legal++;
9651 revins_chars++;
9652#endif
9653 }
9654
9655 /*
9656 * if 'whichwrap' set for cursor in insert mode may go to
9657 * previous line
9658 */
9659 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9660 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009661 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 start_arrow(&tpos);
9663 --(curwin->w_cursor.lnum);
9664 coladvance((colnr_T)MAXCOL);
9665 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9666 }
9667 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009668 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009669 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670}
9671
9672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009673ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674{
9675 pos_T tpos;
9676
9677#ifdef FEAT_FOLDING
9678 if ((fdo_flags & FDO_HOR) && KeyTyped)
9679 foldOpenCursor();
9680#endif
9681 undisplay_dollar();
9682 tpos = curwin->w_cursor;
9683 if (c == K_C_HOME)
9684 curwin->w_cursor.lnum = 1;
9685 curwin->w_cursor.col = 0;
9686#ifdef FEAT_VIRTUALEDIT
9687 curwin->w_cursor.coladd = 0;
9688#endif
9689 curwin->w_curswant = 0;
9690 start_arrow(&tpos);
9691}
9692
9693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009694ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695{
9696 pos_T tpos;
9697
9698#ifdef FEAT_FOLDING
9699 if ((fdo_flags & FDO_HOR) && KeyTyped)
9700 foldOpenCursor();
9701#endif
9702 undisplay_dollar();
9703 tpos = curwin->w_cursor;
9704 if (c == K_C_END)
9705 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9706 coladvance((colnr_T)MAXCOL);
9707 curwin->w_curswant = MAXCOL;
9708
9709 start_arrow(&tpos);
9710}
9711
9712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009713ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714{
9715#ifdef FEAT_FOLDING
9716 if ((fdo_flags & FDO_HOR) && KeyTyped)
9717 foldOpenCursor();
9718#endif
9719 undisplay_dollar();
9720 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9721 {
9722 start_arrow(&curwin->w_cursor);
9723 (void)bck_word(1L, FALSE, FALSE);
9724 curwin->w_set_curswant = TRUE;
9725 }
9726 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009727 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728}
9729
9730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009731ins_right(
9732 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733{
9734#ifdef FEAT_FOLDING
9735 if ((fdo_flags & FDO_HOR) && KeyTyped)
9736 foldOpenCursor();
9737#endif
9738 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009739 if (gchar_cursor() != NUL
9740#ifdef FEAT_VIRTUALEDIT
9741 || virtual_active()
9742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 )
9744 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009745 start_arrow_with_change(&curwin->w_cursor, end_change);
9746 if (!end_change)
9747 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 curwin->w_set_curswant = TRUE;
9749#ifdef FEAT_VIRTUALEDIT
9750 if (virtual_active())
9751 oneright();
9752 else
9753#endif
9754 {
9755#ifdef FEAT_MBYTE
9756 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009757 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 else
9759#endif
9760 ++curwin->w_cursor.col;
9761 }
9762
9763#ifdef FEAT_RIGHTLEFT
9764 revins_legal++;
9765 if (revins_chars)
9766 revins_chars--;
9767#endif
9768 }
9769 /* if 'whichwrap' set for cursor in insert mode, may move the
9770 * cursor to the next line */
9771 else if (vim_strchr(p_ww, ']') != NULL
9772 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9773 {
9774 start_arrow(&curwin->w_cursor);
9775 curwin->w_set_curswant = TRUE;
9776 ++curwin->w_cursor.lnum;
9777 curwin->w_cursor.col = 0;
9778 }
9779 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009780 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009781 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782}
9783
9784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009785ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786{
9787#ifdef FEAT_FOLDING
9788 if ((fdo_flags & FDO_HOR) && KeyTyped)
9789 foldOpenCursor();
9790#endif
9791 undisplay_dollar();
9792 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9793 || gchar_cursor() != NUL)
9794 {
9795 start_arrow(&curwin->w_cursor);
9796 (void)fwd_word(1L, FALSE, 0);
9797 curwin->w_set_curswant = TRUE;
9798 }
9799 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009800 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801}
9802
9803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009804ins_up(
9805 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806{
9807 pos_T tpos;
9808 linenr_T old_topline = curwin->w_topline;
9809#ifdef FEAT_DIFF
9810 int old_topfill = curwin->w_topfill;
9811#endif
9812
9813 undisplay_dollar();
9814 tpos = curwin->w_cursor;
9815 if (cursor_up(1L, TRUE) == OK)
9816 {
9817 if (startcol)
9818 coladvance(getvcol_nolist(&Insstart));
9819 if (old_topline != curwin->w_topline
9820#ifdef FEAT_DIFF
9821 || old_topfill != curwin->w_topfill
9822#endif
9823 )
9824 redraw_later(VALID);
9825 start_arrow(&tpos);
9826#ifdef FEAT_CINDENT
9827 can_cindent = TRUE;
9828#endif
9829 }
9830 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009831 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832}
9833
9834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009835ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837 pos_T tpos;
9838
9839 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009840
9841#ifdef FEAT_WINDOWS
9842 if (mod_mask & MOD_MASK_CTRL)
9843 {
9844 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009845 if (first_tabpage->tp_next != NULL)
9846 {
9847 start_arrow(&curwin->w_cursor);
9848 goto_tabpage(-1);
9849 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009850 return;
9851 }
9852#endif
9853
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 tpos = curwin->w_cursor;
9855 if (onepage(BACKWARD, 1L) == OK)
9856 {
9857 start_arrow(&tpos);
9858#ifdef FEAT_CINDENT
9859 can_cindent = TRUE;
9860#endif
9861 }
9862 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009863 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864}
9865
9866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009867ins_down(
9868 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869{
9870 pos_T tpos;
9871 linenr_T old_topline = curwin->w_topline;
9872#ifdef FEAT_DIFF
9873 int old_topfill = curwin->w_topfill;
9874#endif
9875
9876 undisplay_dollar();
9877 tpos = curwin->w_cursor;
9878 if (cursor_down(1L, TRUE) == OK)
9879 {
9880 if (startcol)
9881 coladvance(getvcol_nolist(&Insstart));
9882 if (old_topline != curwin->w_topline
9883#ifdef FEAT_DIFF
9884 || old_topfill != curwin->w_topfill
9885#endif
9886 )
9887 redraw_later(VALID);
9888 start_arrow(&tpos);
9889#ifdef FEAT_CINDENT
9890 can_cindent = TRUE;
9891#endif
9892 }
9893 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009894 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895}
9896
9897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009898ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899{
9900 pos_T tpos;
9901
9902 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009903
9904#ifdef FEAT_WINDOWS
9905 if (mod_mask & MOD_MASK_CTRL)
9906 {
9907 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009908 if (first_tabpage->tp_next != NULL)
9909 {
9910 start_arrow(&curwin->w_cursor);
9911 goto_tabpage(0);
9912 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009913 return;
9914 }
9915#endif
9916
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 tpos = curwin->w_cursor;
9918 if (onepage(FORWARD, 1L) == OK)
9919 {
9920 start_arrow(&tpos);
9921#ifdef FEAT_CINDENT
9922 can_cindent = TRUE;
9923#endif
9924 }
9925 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009926 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927}
9928
9929#ifdef FEAT_DND
9930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009931ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932{
9933 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9934}
9935#endif
9936
9937/*
9938 * Handle TAB in Insert or Replace mode.
9939 * Return TRUE when the TAB needs to be inserted like a normal character.
9940 */
9941 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009942ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943{
9944 int ind;
9945 int i;
9946 int temp;
9947
9948 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9949 Insstart_blank_vcol = get_nolist_virtcol();
9950 if (echeck_abbr(TAB + ABBR_OFF))
9951 return FALSE;
9952
9953 ind = inindent(0);
9954#ifdef FEAT_CINDENT
9955 if (ind)
9956 can_cindent = FALSE;
9957#endif
9958
9959 /*
9960 * When nothing special, insert TAB like a normal character
9961 */
9962 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009963 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009964 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 return TRUE;
9966
9967 if (stop_arrow() == FAIL)
9968 return TRUE;
9969
9970 did_ai = FALSE;
9971#ifdef FEAT_SMARTINDENT
9972 did_si = FALSE;
9973 can_si = FALSE;
9974 can_si_back = FALSE;
9975#endif
9976 AppendToRedobuff((char_u *)"\t");
9977
9978 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009979 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009980 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9981 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 else /* otherwise use 'tabstop' */
9983 temp = (int)curbuf->b_p_ts;
9984 temp -= get_nolist_virtcol() % temp;
9985
9986 /*
9987 * Insert the first space with ins_char(). It will delete one char in
9988 * replace mode. Insert the rest with ins_str(); it will not delete any
9989 * chars. For VREPLACE mode, we use ins_char() for all characters.
9990 */
9991 ins_char(' ');
9992 while (--temp > 0)
9993 {
9994#ifdef FEAT_VREPLACE
9995 if (State & VREPLACE_FLAG)
9996 ins_char(' ');
9997 else
9998#endif
9999 {
10000 ins_str((char_u *)" ");
10001 if (State & REPLACE_FLAG) /* no char replaced */
10002 replace_push(NUL);
10003 }
10004 }
10005
10006 /*
10007 * When 'expandtab' not set: Replace spaces by TABs where possible.
10008 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010009 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 {
10011 char_u *ptr;
10012#ifdef FEAT_VREPLACE
10013 char_u *saved_line = NULL; /* init for GCC */
10014 pos_T pos;
10015#endif
10016 pos_T fpos;
10017 pos_T *cursor;
10018 colnr_T want_vcol, vcol;
10019 int change_col = -1;
10020 int save_list = curwin->w_p_list;
10021
10022 /*
10023 * Get the current line. For VREPLACE mode, don't make real changes
10024 * yet, just work on a copy of the line.
10025 */
10026#ifdef FEAT_VREPLACE
10027 if (State & VREPLACE_FLAG)
10028 {
10029 pos = curwin->w_cursor;
10030 cursor = &pos;
10031 saved_line = vim_strsave(ml_get_curline());
10032 if (saved_line == NULL)
10033 return FALSE;
10034 ptr = saved_line + pos.col;
10035 }
10036 else
10037#endif
10038 {
10039 ptr = ml_get_cursor();
10040 cursor = &curwin->w_cursor;
10041 }
10042
10043 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10044 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10045 curwin->w_p_list = FALSE;
10046
10047 /* Find first white before the cursor */
10048 fpos = curwin->w_cursor;
10049 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
10050 {
10051 --fpos.col;
10052 --ptr;
10053 }
10054
10055 /* In Replace mode, don't change characters before the insert point. */
10056 if ((State & REPLACE_FLAG)
10057 && fpos.lnum == Insstart.lnum
10058 && fpos.col < Insstart.col)
10059 {
10060 ptr += Insstart.col - fpos.col;
10061 fpos.col = Insstart.col;
10062 }
10063
10064 /* compute virtual column numbers of first white and cursor */
10065 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10066 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10067
Bram Moolenaar597a4222014-06-25 14:39:50 +020010068 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10069 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 while (vim_iswhite(*ptr))
10071 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010072 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 if (vcol + i > want_vcol)
10074 break;
10075 if (*ptr != TAB)
10076 {
10077 *ptr = TAB;
10078 if (change_col < 0)
10079 {
10080 change_col = fpos.col; /* Column of first change */
10081 /* May have to adjust Insstart */
10082 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10083 Insstart.col = fpos.col;
10084 }
10085 }
10086 ++fpos.col;
10087 ++ptr;
10088 vcol += i;
10089 }
10090
10091 if (change_col >= 0)
10092 {
10093 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010094 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095
10096 /* Skip over the spaces we need. */
10097 while (vcol < want_vcol && *ptr == ' ')
10098 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010099 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 ++ptr;
10101 ++repl_off;
10102 }
10103 if (vcol > want_vcol)
10104 {
10105 /* Must have a char with 'showbreak' just before it. */
10106 --ptr;
10107 --repl_off;
10108 }
10109 fpos.col += repl_off;
10110
10111 /* Delete following spaces. */
10112 i = cursor->col - fpos.col;
10113 if (i > 0)
10114 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010115 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 /* correct replace stack. */
10117 if ((State & REPLACE_FLAG)
10118#ifdef FEAT_VREPLACE
10119 && !(State & VREPLACE_FLAG)
10120#endif
10121 )
10122 for (temp = i; --temp >= 0; )
10123 replace_join(repl_off);
10124 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010125#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010126 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010127 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010128 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010129 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10130 (char_u *)"\t", 1);
10131 }
10132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 cursor->col -= i;
10134
10135#ifdef FEAT_VREPLACE
10136 /*
10137 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10138 * backspacing over the changed spacing and then inserting the new
10139 * spacing.
10140 */
10141 if (State & VREPLACE_FLAG)
10142 {
10143 /* Backspace from real cursor to change_col */
10144 backspace_until_column(change_col);
10145
10146 /* Insert each char in saved_line from changed_col to
10147 * ptr-cursor */
10148 ins_bytes_len(saved_line + change_col,
10149 cursor->col - change_col);
10150 }
10151#endif
10152 }
10153
10154#ifdef FEAT_VREPLACE
10155 if (State & VREPLACE_FLAG)
10156 vim_free(saved_line);
10157#endif
10158 curwin->w_p_list = save_list;
10159 }
10160
10161 return FALSE;
10162}
10163
10164/*
10165 * Handle CR or NL in insert mode.
10166 * Return TRUE when out of memory or can't undo.
10167 */
10168 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010169ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170{
10171 int i;
10172
10173 if (echeck_abbr(c + ABBR_OFF))
10174 return FALSE;
10175 if (stop_arrow() == FAIL)
10176 return TRUE;
10177 undisplay_dollar();
10178
10179 /*
10180 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10181 * character under the cursor. Only push a NUL on the replace stack,
10182 * nothing to put back when the NL is deleted.
10183 */
10184 if ((State & REPLACE_FLAG)
10185#ifdef FEAT_VREPLACE
10186 && !(State & VREPLACE_FLAG)
10187#endif
10188 )
10189 replace_push(NUL);
10190
10191 /*
10192 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10193 * replacing the next line, so we push all of the characters left on the
10194 * line onto the replace stack. This is not done here though, it is done
10195 * in open_line().
10196 */
10197
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010198#ifdef FEAT_VIRTUALEDIT
10199 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10200 * CTRL-O). */
10201 if (virtual_active() && curwin->w_cursor.coladd > 0)
10202 coladvance(getviscol());
10203#endif
10204
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205#ifdef FEAT_RIGHTLEFT
10206# ifdef FEAT_FKMAP
10207 if (p_altkeymap && p_fkmap)
10208 fkmap(NL);
10209# endif
10210 /* NL in reverse insert will always start in the end of
10211 * current line. */
10212 if (revins_on)
10213 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10214#endif
10215
10216 AppendToRedobuff(NL_STR);
10217 i = open_line(FORWARD,
10218#ifdef FEAT_COMMENTS
10219 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10220#endif
10221 0, old_indent);
10222 old_indent = 0;
10223#ifdef FEAT_CINDENT
10224 can_cindent = TRUE;
10225#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010226#ifdef FEAT_FOLDING
10227 /* When inserting a line the cursor line must never be in a closed fold. */
10228 foldOpenCursor();
10229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230
10231 return (!i);
10232}
10233
10234#ifdef FEAT_DIGRAPHS
10235/*
10236 * Handle digraph in insert mode.
10237 * Returns character still to be inserted, or NUL when nothing remaining to be
10238 * done.
10239 */
10240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010241ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242{
10243 int c;
10244 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010245 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246
10247 pc_status = PC_STATUS_UNSET;
10248 if (redrawing() && !char_avail())
10249 {
10250 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010251 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252
10253 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010254 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255#ifdef FEAT_CMDL_INFO
10256 add_to_showcmd_c(Ctrl_K);
10257#endif
10258 }
10259
10260#ifdef USE_ON_FLY_SCROLL
10261 dont_scroll = TRUE; /* disallow scrolling here */
10262#endif
10263
10264 /* don't map the digraph chars. This also prevents the
10265 * mode message to be deleted when ESC is hit */
10266 ++no_mapping;
10267 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010268 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 --no_mapping;
10270 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010271 if (did_putchar)
10272 /* when the line fits in 'columns' the '?' is at the start of the next
10273 * line and will not be removed by the redraw */
10274 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010275
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 if (IS_SPECIAL(c) || mod_mask) /* special key */
10277 {
10278#ifdef FEAT_CMDL_INFO
10279 clear_showcmd();
10280#endif
10281 insert_special(c, TRUE, FALSE);
10282 return NUL;
10283 }
10284 if (c != ESC)
10285 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010286 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 if (redrawing() && !char_avail())
10288 {
10289 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010290 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291
10292 if (char2cells(c) == 1)
10293 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010294 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010296 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 }
10298#ifdef FEAT_CMDL_INFO
10299 add_to_showcmd_c(c);
10300#endif
10301 }
10302 ++no_mapping;
10303 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010304 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 --no_mapping;
10306 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010307 if (did_putchar)
10308 /* when the line fits in 'columns' the '?' is at the start of the
10309 * next line and will not be removed by a redraw */
10310 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 if (cc != ESC)
10312 {
10313 AppendToRedobuff((char_u *)CTRL_V_STR);
10314 c = getdigraph(c, cc, TRUE);
10315#ifdef FEAT_CMDL_INFO
10316 clear_showcmd();
10317#endif
10318 return c;
10319 }
10320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321#ifdef FEAT_CMDL_INFO
10322 clear_showcmd();
10323#endif
10324 return NUL;
10325}
10326#endif
10327
10328/*
10329 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10330 * Returns the char to be inserted, or NUL if none found.
10331 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010332 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010333ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334{
10335 int c;
10336 int temp;
10337 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010338 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339
10340 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10341 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010342 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 return NUL;
10344 }
10345
10346 /* try to advance to the cursor column */
10347 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010348 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 prev_ptr = ptr;
10350 validate_virtcol();
10351 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10352 {
10353 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010354 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 }
10356 if ((colnr_T)temp > curwin->w_virtcol)
10357 ptr = prev_ptr;
10358
10359#ifdef FEAT_MBYTE
10360 c = (*mb_ptr2char)(ptr);
10361#else
10362 c = *ptr;
10363#endif
10364 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010365 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 return c;
10367}
10368
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010369/*
10370 * CTRL-Y or CTRL-E typed in Insert mode.
10371 */
10372 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010373ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010374{
10375 int c = tc;
10376
10377#ifdef FEAT_INS_EXPAND
10378 if (ctrl_x_mode == CTRL_X_SCROLL)
10379 {
10380 if (c == Ctrl_Y)
10381 scrolldown_clamp();
10382 else
10383 scrollup_clamp();
10384 redraw_later(VALID);
10385 }
10386 else
10387#endif
10388 {
10389 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10390 if (c != NUL)
10391 {
10392 long tw_save;
10393
10394 /* The character must be taken literally, insert like it
10395 * was typed after a CTRL-V, and pretend 'textwidth'
10396 * wasn't set. Digits, 'o' and 'x' are special after a
10397 * CTRL-V, don't use it for these. */
10398 if (c < 256 && !isalnum(c))
10399 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10400 tw_save = curbuf->b_p_tw;
10401 curbuf->b_p_tw = -1;
10402 insert_special(c, TRUE, FALSE);
10403 curbuf->b_p_tw = tw_save;
10404#ifdef FEAT_RIGHTLEFT
10405 revins_chars++;
10406 revins_legal++;
10407#endif
10408 c = Ctrl_V; /* pretend CTRL-V is last character */
10409 auto_format(FALSE, TRUE);
10410 }
10411 }
10412 return c;
10413}
10414
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415#ifdef FEAT_SMARTINDENT
10416/*
10417 * Try to do some very smart auto-indenting.
10418 * Used when inserting a "normal" character.
10419 */
10420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010421ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422{
10423 pos_T *pos, old_pos;
10424 char_u *ptr;
10425 int i;
10426 int temp;
10427
10428 /*
10429 * do some very smart indenting when entering '{' or '}'
10430 */
10431 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10432 {
10433 /*
10434 * for '}' set indent equal to indent of line containing matching '{'
10435 */
10436 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10437 {
10438 old_pos = curwin->w_cursor;
10439 /*
10440 * If the matching '{' has a ')' immediately before it (ignoring
10441 * white-space), then line up with the start of the line
10442 * containing the matching '(' if there is one. This handles the
10443 * case where an "if (..\n..) {" statement continues over multiple
10444 * lines -- webb
10445 */
10446 ptr = ml_get(pos->lnum);
10447 i = pos->col;
10448 if (i > 0) /* skip blanks before '{' */
10449 while (--i > 0 && vim_iswhite(ptr[i]))
10450 ;
10451 curwin->w_cursor.lnum = pos->lnum;
10452 curwin->w_cursor.col = i;
10453 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10454 curwin->w_cursor = *pos;
10455 i = get_indent();
10456 curwin->w_cursor = old_pos;
10457#ifdef FEAT_VREPLACE
10458 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010459 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 else
10461#endif
10462 (void)set_indent(i, SIN_CHANGED);
10463 }
10464 else if (curwin->w_cursor.col > 0)
10465 {
10466 /*
10467 * when inserting '{' after "O" reduce indent, but not
10468 * more than indent of previous line
10469 */
10470 temp = TRUE;
10471 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10472 {
10473 old_pos = curwin->w_cursor;
10474 i = get_indent();
10475 while (curwin->w_cursor.lnum > 1)
10476 {
10477 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10478
10479 /* ignore empty lines and lines starting with '#'. */
10480 if (*ptr != '#' && *ptr != NUL)
10481 break;
10482 }
10483 if (get_indent() >= i)
10484 temp = FALSE;
10485 curwin->w_cursor = old_pos;
10486 }
10487 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010488 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 }
10490 }
10491
10492 /*
10493 * set indent of '#' always to 0
10494 */
10495 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10496 {
10497 /* remember current indent for next line */
10498 old_indent = get_indent();
10499 (void)set_indent(0, SIN_CHANGED);
10500 }
10501
10502 /* Adjust ai_col, the char at this position can be deleted. */
10503 if (ai_col > curwin->w_cursor.col)
10504 ai_col = curwin->w_cursor.col;
10505}
10506#endif
10507
10508/*
10509 * Get the value that w_virtcol would have when 'list' is off.
10510 * Unless 'cpo' contains the 'L' flag.
10511 */
10512 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010513get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514{
10515 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10516 return getvcol_nolist(&curwin->w_cursor);
10517 validate_virtcol();
10518 return curwin->w_virtcol;
10519}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010520
10521#ifdef FEAT_AUTOCMD
10522/*
10523 * Handle the InsertCharPre autocommand.
10524 * "c" is the character that was typed.
10525 * Return a pointer to allocated memory with the replacement string.
10526 * Return NULL to continue inserting "c".
10527 */
10528 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010529do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010530{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010531 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010532 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010533
10534 /* Return quickly when there is nothing to do. */
10535 if (!has_insertcharpre())
10536 return NULL;
10537
Bram Moolenaar704984a2012-06-01 14:57:51 +020010538#ifdef FEAT_MBYTE
10539 if (has_mbyte)
10540 buf[(*mb_char2bytes)(c, buf)] = NUL;
10541 else
10542#endif
10543 {
10544 buf[0] = c;
10545 buf[1] = NUL;
10546 }
10547
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010548 /* Lock the text to avoid weird things from happening. */
10549 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010550 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010551
Bram Moolenaar704984a2012-06-01 14:57:51 +020010552 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010553 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010554 {
10555 /* Get the value of v:char. It may be empty or more than one
10556 * character. Only use it when changed, otherwise continue with the
10557 * original character to avoid breaking autoindent. */
10558 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10559 res = vim_strsave(get_vim_var_str(VV_CHAR));
10560 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010561
10562 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10563 --textlock;
10564
10565 return res;
10566}
10567#endif