blob: ce6abaf209336ccb9ed8e22be0d8ad6da18746bd [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 {
466 AppendCharToRedobuff(cmdchar);
467 if (cmdchar == 'g') /* "gI" command */
468 AppendCharToRedobuff('I');
469 else if (cmdchar == 'r') /* "r<CR>" command */
470 count = 1; /* insert only one <CR> */
471 }
472 }
473
474 if (cmdchar == 'R')
475 {
476#ifdef FEAT_FKMAP
477 if (p_fkmap && p_ri)
478 {
479 beep_flush();
480 EMSG(farsi_text_3); /* encoded in Farsi */
481 State = INSERT;
482 }
483 else
484#endif
485 State = REPLACE;
486 }
487#ifdef FEAT_VREPLACE
488 else if (cmdchar == 'V' || cmdchar == 'v')
489 {
490 State = VREPLACE;
491 replaceState = VREPLACE;
492 orig_line_count = curbuf->b_ml.ml_line_count;
493 vr_lines_changed = 1;
494 }
495#endif
496 else
497 State = INSERT;
498
499 stop_insert_mode = FALSE;
500
501 /*
502 * Need to recompute the cursor position, it might move when the cursor is
503 * on a TAB or special character.
504 */
505 curs_columns(TRUE);
506
507 /*
508 * Enable langmap or IME, indicated by 'iminsert'.
509 * Note that IME may enabled/disabled without us noticing here, thus the
510 * 'iminsert' value may not reflect what is actually used. It is updated
511 * when hitting <Esc>.
512 */
513 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
514 State |= LANGMAP;
515#ifdef USE_IM_CONTROL
516 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
517#endif
518
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519#ifdef FEAT_MOUSE
520 setmouse();
521#endif
522#ifdef FEAT_CMDL_INFO
523 clear_showcmd();
524#endif
525#ifdef FEAT_RIGHTLEFT
526 /* there is no reverse replace mode */
527 revins_on = (State == INSERT && p_ri);
528 if (revins_on)
529 undisplay_dollar();
530 revins_chars = 0;
531 revins_legal = 0;
532 revins_scol = -1;
533#endif
534
535 /*
536 * Handle restarting Insert mode.
537 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
538 * restart_edit non-zero, and something in the stuff buffer.
539 */
540 if (restart_edit != 0 && stuff_empty())
541 {
542#ifdef FEAT_MOUSE
543 /*
544 * After a paste we consider text typed to be part of the insert for
545 * the pasted text. You can backspace over the pasted text too.
546 */
547 if (where_paste_started.lnum)
548 arrow_used = FALSE;
549 else
550#endif
551 arrow_used = TRUE;
552 restart_edit = 0;
553
554 /*
555 * If the cursor was after the end-of-line before the CTRL-O and it is
556 * now at the end-of-line, put it after the end-of-line (this is not
557 * correct in very rare cases).
558 * Also do this if curswant is greater than the current virtual
559 * column. Eg after "^O$" or "^O80|".
560 */
561 validate_virtcol();
562 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000563 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 || curwin->w_curswant > curwin->w_virtcol)
565 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
566 {
567 if (ptr[1] == NUL)
568 ++curwin->w_cursor.col;
569#ifdef FEAT_MBYTE
570 else if (has_mbyte)
571 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000572 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 if (ptr[i] == NUL)
574 curwin->w_cursor.col += i;
575 }
576#endif
577 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000578 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580 else
581 arrow_used = FALSE;
582
583 /* we are in insert mode now, don't need to start it anymore */
584 need_start_insertmode = FALSE;
585
586 /* Need to save the line for undo before inserting the first char. */
587 ins_need_undo = TRUE;
588
589#ifdef FEAT_MOUSE
590 where_paste_started.lnum = 0;
591#endif
592#ifdef FEAT_CINDENT
593 can_cindent = TRUE;
594#endif
595#ifdef FEAT_FOLDING
596 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
597 * restarting. */
598 if (!p_im && did_restart_edit == 0)
599 foldOpenCursor();
600#endif
601
602 /*
603 * If 'showmode' is set, show the current (insert/replace/..) mode.
604 * A warning message for changing a readonly file is given here, before
605 * actually changing anything. It's put after the mode, if any.
606 */
607 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000608 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 i = showmode();
610
611 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000612 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
614#ifdef CURSOR_SHAPE
615 ui_cursor_shape(); /* may show different cursor shape */
616#endif
617#ifdef FEAT_DIGRAPHS
618 do_digraph(-1); /* clear digraphs */
619#endif
620
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000621 /*
622 * Get the current length of the redo buffer, those characters have to be
623 * skipped if we want to get to the inserted characters.
624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 ptr = get_inserted();
626 if (ptr == NULL)
627 new_insert_skip = 0;
628 else
629 {
630 new_insert_skip = (int)STRLEN(ptr);
631 vim_free(ptr);
632 }
633
634 old_indent = 0;
635
636 /*
637 * Main loop in Insert mode: repeat until Insert mode is left.
638 */
639 for (;;)
640 {
641#ifdef FEAT_RIGHTLEFT
642 if (!revins_legal)
643 revins_scol = -1; /* reset on illegal motions */
644 else
645 revins_legal = 0;
646#endif
647 if (arrow_used) /* don't repeat insert when arrow key used */
648 count = 0;
649
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100650 if (update_Insstart_orig)
651 Insstart_orig = Insstart;
652
Bram Moolenaar00672e12016-06-26 18:38:13 +0200653 if (stop_insert_mode
654#ifdef FEAT_INS_EXPAND
655 && !pum_visible()
656#endif
657 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {
659 /* ":stopinsert" used or 'insertmode' reset */
660 count = 0;
661 goto doESCkey;
662 }
663
664 /* set curwin->w_curswant for next K_DOWN or K_UP */
665 if (!arrow_used)
666 curwin->w_set_curswant = TRUE;
667
668 /* If there is no typeahead may check for timestamps (e.g., for when a
669 * menu invoked a shell command). */
670 if (stuff_empty())
671 {
672 did_check_timestamps = FALSE;
673 if (need_check_timestamps)
674 check_timestamps(FALSE);
675 }
676
677 /*
678 * When emsg() was called msg_scroll will have been set.
679 */
680 msg_scroll = FALSE;
681
682#ifdef FEAT_GUI
683 /* When 'mousefocus' is set a mouse movement may have taken us to
684 * another window. "need_mouse_correct" may then be set because of an
685 * autocommand. */
686 if (need_mouse_correct)
687 gui_mouse_correct();
688#endif
689
690#ifdef FEAT_FOLDING
691 /* Open fold at the cursor line, according to 'foldopen'. */
692 if (fdo_flags & FDO_INSERT)
693 foldOpenCursor();
694 /* Close folds where the cursor isn't, according to 'foldclose' */
695 if (!char_avail())
696 foldCheckClose();
697#endif
698
699 /*
700 * If we inserted a character at the last position of the last line in
701 * the window, scroll the window one line up. This avoids an extra
702 * redraw.
703 * This is detected when the cursor column is smaller after inserting
704 * something.
705 * Don't do this when the topline changed already, it has
706 * already been adjusted (by insertchar() calling open_line())).
707 */
708 if (curbuf->b_mod_set
709 && curwin->w_p_wrap
710 && !did_backspace
711 && curwin->w_topline == old_topline
712#ifdef FEAT_DIFF
713 && curwin->w_topfill == old_topfill
714#endif
715 )
716 {
717 mincol = curwin->w_wcol;
718 validate_cursor_col();
719
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000720 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 && curwin->w_wrow == W_WINROW(curwin)
722 + curwin->w_height - 1 - p_so
723 && (curwin->w_cursor.lnum != curwin->w_topline
724#ifdef FEAT_DIFF
725 || curwin->w_topfill > 0
726#endif
727 ))
728 {
729#ifdef FEAT_DIFF
730 if (curwin->w_topfill > 0)
731 --curwin->w_topfill;
732 else
733#endif
734#ifdef FEAT_FOLDING
735 if (hasFolding(curwin->w_topline, NULL, &old_topline))
736 set_topline(curwin, old_topline + 1);
737 else
738#endif
739 set_topline(curwin, curwin->w_topline + 1);
740 }
741 }
742
743 /* May need to adjust w_topline to show the cursor. */
744 update_topline();
745
746 did_backspace = FALSE;
747
748 validate_cursor(); /* may set must_redraw */
749
750 /*
751 * Redraw the display when no characters are waiting.
752 * Also shows mode, ruler and positions cursor.
753 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000754 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
756#ifdef FEAT_SCROLLBIND
757 if (curwin->w_p_scb)
758 do_check_scrollbind(TRUE);
759#endif
760
Bram Moolenaar860cae12010-06-05 23:22:07 +0200761#ifdef FEAT_CURSORBIND
762 if (curwin->w_p_crb)
763 do_check_cursorbind();
764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 update_curswant();
766 old_topline = curwin->w_topline;
767#ifdef FEAT_DIFF
768 old_topfill = curwin->w_topfill;
769#endif
770
771#ifdef USE_ON_FLY_SCROLL
772 dont_scroll = FALSE; /* allow scrolling here */
773#endif
774
775 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000776 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100778 if (c != K_CURSORHOLD)
779 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200780
781 /* After using CTRL-G U the next cursor key will not break undo. */
782 if (dont_sync_undo == MAYBE)
783 dont_sync_undo = TRUE;
784 else
785 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100786 if (cmdchar == K_PS)
787 /* Got here from normal mode when bracketed paste started. */
788 c = K_PS;
789 else
790 do
791 {
792 c = safe_vgetc();
793 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000795#ifdef FEAT_AUTOCMD
796 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
797 did_cursorhold = TRUE;
798#endif
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800#ifdef FEAT_RIGHTLEFT
801 if (p_hkmap && KeyTyped)
802 c = hkmap(c); /* Hebrew mode mapping */
803#endif
804#ifdef FEAT_FKMAP
805 if (p_fkmap && KeyTyped)
806 c = fkmap(c); /* Farsi mode mapping */
807#endif
808
809#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000810 /*
811 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000812 * and the cursor is still in the completed word. Only when there is
813 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000814 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000815 if (compl_started
816 && pum_wanted()
817 && curwin->w_cursor.col >= compl_col
818 && (compl_shown_match == NULL
819 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000820 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000821 /* BS: Delete one character from "compl_leader". */
822 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000823 && curwin->w_cursor.col > compl_col
824 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000825 continue;
826
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000827 /* When no match was selected or it was edited. */
828 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000829 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000830 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000831 * "compl_leader". Except when at the original match and
832 * there is nothing to add, CTRL-L works like CTRL-P then. */
833 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100834 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000835 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000836 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000837 {
838 ins_compl_addfrommatch();
839 continue;
840 }
841
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000842 /* A non-white character that fits in with the current
843 * completion: Add to "compl_leader". */
844 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000845 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100846#ifdef FEAT_AUTOCMD
847 /* Trigger InsertCharPre. */
848 char_u *str = do_insert_char_pre(c);
849 char_u *p;
850
851 if (str != NULL)
852 {
853 for (p = str; *p != NUL; mb_ptr_adv(p))
854 ins_compl_addleader(PTR2CHAR(p));
855 vim_free(str);
856 }
857 else
858#endif
859 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000860 continue;
861 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000862
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000863 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000864 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200865 if ((c == Ctrl_Y || (compl_enter_selects
866 && (c == CAR || c == K_KENTER || c == NL)))
867 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000868 {
869 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200870 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000871 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000872 }
873 }
874
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
876 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000877 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000878 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000879 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880#endif
881
Bram Moolenaar488c6512005-08-11 20:09:58 +0000882 /* CTRL-\ CTRL-N goes to Normal mode,
883 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
884 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 if (c == Ctrl_BSL)
886 {
887 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000888 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 ++no_mapping;
890 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000891 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 --no_mapping;
893 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000894 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000896 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 vungetc(c);
898 c = Ctrl_BSL;
899 }
900 else if (c == Ctrl_G && p_im)
901 continue;
902 else
903 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000904 if (c == Ctrl_O)
905 {
906 ins_ctrl_o();
907 ins_at_eol = FALSE; /* cursor keeps its column */
908 nomove = TRUE;
909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 count = 0;
911 goto doESCkey;
912 }
913 }
914
915#ifdef FEAT_DIGRAPHS
916 c = do_digraph(c);
917#endif
918
919#ifdef FEAT_INS_EXPAND
920 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
921 goto docomplete;
922#endif
923 if (c == Ctrl_V || c == Ctrl_Q)
924 {
925 ins_ctrl_v();
926 c = Ctrl_V; /* pretend CTRL-V is last typed character */
927 continue;
928 }
929
930#ifdef FEAT_CINDENT
931 if (cindent_on()
932# ifdef FEAT_INS_EXPAND
933 && ctrl_x_mode == 0
934# endif
935 )
936 {
937 /* A key name preceded by a bang means this key is not to be
938 * inserted. Skip ahead to the re-indenting below.
939 * A key name preceded by a star means that indenting has to be
940 * done before inserting the key. */
941 line_is_white = inindent(0);
942 if (in_cinkeys(c, '!', line_is_white))
943 goto force_cindent;
944 if (can_cindent && in_cinkeys(c, '*', line_is_white)
945 && stop_arrow() == OK)
946 do_c_expr_indent();
947 }
948#endif
949
950#ifdef FEAT_RIGHTLEFT
951 if (curwin->w_p_rl)
952 switch (c)
953 {
954 case K_LEFT: c = K_RIGHT; break;
955 case K_S_LEFT: c = K_S_RIGHT; break;
956 case K_C_LEFT: c = K_C_RIGHT; break;
957 case K_RIGHT: c = K_LEFT; break;
958 case K_S_RIGHT: c = K_S_LEFT; break;
959 case K_C_RIGHT: c = K_C_LEFT; break;
960 }
961#endif
962
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 /*
964 * If 'keymodel' contains "startsel", may start selection. If it
965 * does, a CTRL-O and c will be stuffed, we need to get these
966 * characters.
967 */
968 if (ins_start_select(c))
969 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
971 /*
972 * The big switch to handle a character in insert mode.
973 */
974 switch (c)
975 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000976 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (echeck_abbr(ESC + ABBR_OFF))
978 break;
979 /*FALLTHROUGH*/
980
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000981 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982#ifdef FEAT_CMDWIN
983 if (c == Ctrl_C && cmdwin_type != 0)
984 {
985 /* Close the cmdline window. */
986 cmdwin_result = K_IGNORE;
987 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000988 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 goto doESCkey;
990 }
991#endif
992
993#ifdef UNIX
994do_intr:
995#endif
996 /* when 'insertmode' set, and not halfway a mapping, don't leave
997 * Insert mode */
998 if (goto_im())
999 {
1000 if (got_int)
1001 {
1002 (void)vgetc(); /* flush all buffers */
1003 got_int = FALSE;
1004 }
1005 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001006 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 break;
1008 }
1009doESCkey:
1010 /*
1011 * This is the ONLY return from edit()!
1012 */
1013 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1014 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001015 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 o_lnum = curwin->w_cursor.lnum;
1017
Bram Moolenaar488c6512005-08-11 20:09:58 +00001018 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001019 {
1020#ifdef FEAT_AUTOCMD
1021 if (cmdchar != 'r' && cmdchar != 'v')
1022 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1023 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001024 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 continue;
1029
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001030 case Ctrl_Z: /* suspend when 'insertmode' set */
1031 if (!p_im)
1032 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001033 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001034 c = Ctrl_O;
1035 /*FALLTHROUGH*/
1036
1037 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001038#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001039 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001040 goto docomplete;
1041#endif
1042 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1043 break;
1044 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001045
1046#ifdef FEAT_VIRTUALEDIT
1047 /* don't move the cursor left when 'virtualedit' has "onemore". */
1048 if (ve_flags & VE_ONEMORE)
1049 {
1050 ins_at_eol = FALSE;
1051 nomove = TRUE;
1052 }
1053#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001054 count = 0;
1055 goto doESCkey;
1056
Bram Moolenaar572cb562005-08-05 21:35:02 +00001057 case K_INS: /* toggle insert/replace mode */
1058 case K_KINS:
1059 ins_insert(replaceState);
1060 break;
1061
1062 case K_SELECT: /* end of Select mode mapping - ignore */
1063 break;
1064
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065 case K_HELP: /* Help key works like <ESC> <Help> */
1066 case K_F1:
1067 case K_XF1:
1068 stuffcharReadbuff(K_HELP);
1069 if (p_im)
1070 need_start_insertmode = TRUE;
1071 goto doESCkey;
1072
1073#ifdef FEAT_NETBEANS_INTG
1074 case K_F21: /* NetBeans command */
1075 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001076 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001077 --no_mapping;
1078 netbeans_keycommand(i);
1079 break;
1080#endif
1081
1082 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 case NUL:
1084 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001085 /* For ^@ the trailing ESC will end the insert, unless there is an
1086 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1088 && c != Ctrl_A && !p_im)
1089 goto doESCkey; /* quit insert mode */
1090 inserted_space = FALSE;
1091 break;
1092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001093 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 ins_reg();
1095 auto_format(FALSE, TRUE);
1096 inserted_space = FALSE;
1097 break;
1098
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001099 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 ins_ctrl_g();
1101 break;
1102
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001103 case Ctrl_HAT: /* switch input mode and/or langmap */
1104 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 break;
1106
1107#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if (!p_ari)
1110 goto normalchar;
1111 ins_ctrl_();
1112 break;
1113#endif
1114
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1117 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1118 goto docomplete;
1119#endif
1120 /* FALLTHROUGH */
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123# ifdef FEAT_INS_EXPAND
1124 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1125 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 if (has_compl_option(FALSE))
1127 goto docomplete;
1128 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130# endif
1131 ins_shift(c, lastc);
1132 auto_format(FALSE, TRUE);
1133 inserted_space = FALSE;
1134 break;
1135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 case K_KDEL:
1138 ins_del();
1139 auto_format(FALSE, TRUE);
1140 break;
1141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 case Ctrl_H:
1144 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1145 auto_format(FALSE, TRUE);
1146 break;
1147
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001148 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1150 auto_format(FALSE, TRUE);
1151 break;
1152
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001153 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001154# ifdef FEAT_COMPL_FUNC
1155 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001156 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001157 goto docomplete;
1158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1160 auto_format(FALSE, TRUE);
1161 inserted_space = FALSE;
1162 break;
1163
1164#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 case K_LEFTMOUSE_NM:
1167 case K_LEFTDRAG:
1168 case K_LEFTRELEASE:
1169 case K_LEFTRELEASE_NM:
1170 case K_MIDDLEMOUSE:
1171 case K_MIDDLEDRAG:
1172 case K_MIDDLERELEASE:
1173 case K_RIGHTMOUSE:
1174 case K_RIGHTDRAG:
1175 case K_RIGHTRELEASE:
1176 case K_X1MOUSE:
1177 case K_X1DRAG:
1178 case K_X1RELEASE:
1179 case K_X2MOUSE:
1180 case K_X2DRAG:
1181 case K_X2RELEASE:
1182 ins_mouse(c);
1183 break;
1184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001185 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001186 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 break;
1188
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001189 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001190 ins_mousescroll(MSCR_UP);
1191 break;
1192
1193 case K_MOUSELEFT: /* Scroll wheel left */
1194 ins_mousescroll(MSCR_LEFT);
1195 break;
1196
1197 case K_MOUSERIGHT: /* Scroll wheel right */
1198 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 break;
1200#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001201 case K_PS:
1202 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1203 if (cmdchar == K_PS)
1204 /* invoked from normal mode, bail out */
1205 goto doESCkey;
1206 break;
1207 case K_PE:
1208 /* Got K_PE without K_PS, ignore. */
1209 break;
1210
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001211#ifdef FEAT_GUI_TABLINE
1212 case K_TABLINE:
1213 case K_TABMENU:
1214 ins_tabline(c);
1215 break;
1216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001218 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 break;
1220
Bram Moolenaar754b5602006-02-09 23:53:20 +00001221#ifdef FEAT_AUTOCMD
1222 case K_CURSORHOLD: /* Didn't type something for a while. */
1223 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1224 did_cursorhold = TRUE;
1225 break;
1226#endif
1227
Bram Moolenaar4770d092006-01-12 23:22:24 +00001228#ifdef FEAT_GUI_W32
1229 /* On Win32 ignore <M-F4>, we get it when closing the window was
1230 * cancelled. */
1231 case K_F4:
1232 if (mod_mask != MOD_MASK_ALT)
1233 goto normalchar;
1234 break;
1235#endif
1236
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237#ifdef FEAT_GUI
1238 case K_VER_SCROLLBAR:
1239 ins_scroll();
1240 break;
1241
1242 case K_HOR_SCROLLBAR:
1243 ins_horscroll();
1244 break;
1245#endif
1246
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001247 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 case K_S_HOME:
1250 case K_C_HOME:
1251 ins_home(c);
1252 break;
1253
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001254 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 case K_S_END:
1257 case K_C_END:
1258 ins_end(c);
1259 break;
1260
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001261 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001262 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1263 ins_s_left();
1264 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001265 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 break;
1267
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001268 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 case K_C_LEFT:
1270 ins_s_left();
1271 break;
1272
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001273 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001274 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1275 ins_s_right();
1276 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001277 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 break;
1279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001280 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 case K_C_RIGHT:
1282 ins_s_right();
1283 break;
1284
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001286#ifdef FEAT_INS_EXPAND
1287 if (pum_visible())
1288 goto docomplete;
1289#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001290 if (mod_mask & MOD_MASK_SHIFT)
1291 ins_pageup();
1292 else
1293 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 break;
1295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 case K_PAGEUP:
1298 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001299#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001300 if (pum_visible())
1301 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 ins_pageup();
1304 break;
1305
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001306 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001307#ifdef FEAT_INS_EXPAND
1308 if (pum_visible())
1309 goto docomplete;
1310#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001311 if (mod_mask & MOD_MASK_SHIFT)
1312 ins_pagedown();
1313 else
1314 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 break;
1316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001317 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 case K_PAGEDOWN:
1319 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001320#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001321 if (pum_visible())
1322 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 ins_pagedown();
1325 break;
1326
1327#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001328 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 ins_drop();
1330 break;
1331#endif
1332
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001333 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 c = TAB;
1335 /* FALLTHROUGH */
1336
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001337 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1339 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1340 goto docomplete;
1341#endif
1342 inserted_space = FALSE;
1343 if (ins_tab())
1344 goto normalchar; /* insert TAB as a normal char */
1345 auto_format(FALSE, TRUE);
1346 break;
1347
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001348 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 c = CAR;
1350 /* FALLTHROUGH */
1351 case CAR:
1352 case NL:
1353#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1354 /* In a quickfix window a <CR> jumps to the error under the
1355 * cursor. */
1356 if (bt_quickfix(curbuf) && c == CAR)
1357 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001358 if (curwin->w_llist_ref == NULL) /* quickfix window */
1359 do_cmdline_cmd((char_u *)".cc");
1360 else /* location list window */
1361 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 break;
1363 }
1364#endif
1365#ifdef FEAT_CMDWIN
1366 if (cmdwin_type != 0)
1367 {
1368 /* Execute the command in the cmdline window. */
1369 cmdwin_result = CAR;
1370 goto doESCkey;
1371 }
1372#endif
1373 if (ins_eol(c) && !p_im)
1374 goto doESCkey; /* out of memory */
1375 auto_format(FALSE, FALSE);
1376 inserted_space = FALSE;
1377 break;
1378
Bram Moolenaar860cae12010-06-05 23:22:07 +02001379#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001380 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381# ifdef FEAT_INS_EXPAND
1382 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1383 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001384 if (has_compl_option(TRUE))
1385 goto docomplete;
1386 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388# endif
1389# ifdef FEAT_DIGRAPHS
1390 c = ins_digraph();
1391 if (c == NUL)
1392 break;
1393# endif
1394 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
1397#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001398 case Ctrl_X: /* Enter CTRL-X mode */
1399 ins_ctrl_x();
1400 break;
1401
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001402 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 if (ctrl_x_mode != CTRL_X_TAGS)
1404 goto normalchar;
1405 goto docomplete;
1406
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001407 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (ctrl_x_mode != CTRL_X_FILES)
1409 goto normalchar;
1410 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001411
1412 case 's': /* Spelling completion after ^X */
1413 case Ctrl_S:
1414 if (ctrl_x_mode != CTRL_X_SPELL)
1415 goto normalchar;
1416 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417#endif
1418
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001419 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420#ifdef FEAT_INS_EXPAND
1421 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1422#endif
1423 {
1424 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1425 if (p_im)
1426 {
1427 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1428 break;
1429 goto doESCkey;
1430 }
1431 goto normalchar;
1432 }
1433#ifdef FEAT_INS_EXPAND
1434 /* FALLTHROUGH */
1435
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001436 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 case Ctrl_N:
1438 /* if 'complete' is empty then plain ^P is no longer special,
1439 * but it is under other ^X modes */
1440 if (*curbuf->b_p_cpt == NUL
1441 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001442 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 goto normalchar;
1444
1445docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001446 compl_busy = TRUE;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001447 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001448 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001449 compl_cont_status = 0;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001450 disable_fold_update--;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001451 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 break;
1453#endif /* FEAT_INS_EXPAND */
1454
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001455 case Ctrl_Y: /* copy from previous line or scroll down */
1456 case Ctrl_E: /* copy from next line or scroll up */
1457 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 break;
1459
1460 default:
1461#ifdef UNIX
1462 if (c == intr_char) /* special interrupt char */
1463 goto do_intr;
1464#endif
1465
Bram Moolenaare659c952011-05-19 17:25:41 +02001466normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001468 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001470#ifdef FEAT_AUTOCMD
1471 if (!p_paste)
1472 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001473 /* Trigger InsertCharPre. */
1474 char_u *str = do_insert_char_pre(c);
1475 char_u *p;
1476
1477 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001478 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001479 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001480 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001481 /* Insert the new value of v:char literally. */
1482 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001483 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001484 c = PTR2CHAR(p);
1485 if (c == CAR || c == K_KENTER || c == NL)
1486 ins_eol(c);
1487 else
1488 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001489 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001490 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001491 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001492 vim_free(str);
1493 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001494 }
1495
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001496 /* If the new value is already inserted or an empty string
1497 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001498 if (c == NUL)
1499 break;
1500 }
1501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502#ifdef FEAT_SMARTINDENT
1503 /* Try to perform smart-indenting. */
1504 ins_try_si(c);
1505#endif
1506
1507 if (c == ' ')
1508 {
1509 inserted_space = TRUE;
1510#ifdef FEAT_CINDENT
1511 if (inindent(0))
1512 can_cindent = FALSE;
1513#endif
1514 if (Insstart_blank_vcol == MAXCOL
1515 && curwin->w_cursor.lnum == Insstart.lnum)
1516 Insstart_blank_vcol = get_nolist_virtcol();
1517 }
1518
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001519 /* Insert a normal character and check for abbreviations on a
1520 * special character. Let CTRL-] expand abbreviations without
1521 * inserting it. */
1522 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523#ifdef FEAT_MBYTE
1524 /* Add ABBR_OFF for characters above 0x100, this is
1525 * what check_abbr() expects. */
1526 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1527#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001528 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 insert_special(c, FALSE, FALSE);
1531#ifdef FEAT_RIGHTLEFT
1532 revins_legal++;
1533 revins_chars++;
1534#endif
1535 }
1536
1537 auto_format(FALSE, TRUE);
1538
1539#ifdef FEAT_FOLDING
1540 /* When inserting a character the cursor line must never be in a
1541 * closed fold. */
1542 foldOpenCursor();
1543#endif
1544 break;
1545 } /* end of switch (c) */
1546
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001547#ifdef FEAT_AUTOCMD
1548 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001549 if (c != K_CURSORHOLD
1550# ifdef FEAT_COMPL_FUNC
1551 /* but not in CTRL-X mode, a script can't restore the state */
1552 && ctrl_x_mode == 0
1553# endif
1554 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001555 did_cursorhold = FALSE;
1556#endif
1557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 /* If the cursor was moved we didn't just insert a space */
1559 if (arrow_used)
1560 inserted_space = FALSE;
1561
1562#ifdef FEAT_CINDENT
1563 if (can_cindent && cindent_on()
1564# ifdef FEAT_INS_EXPAND
1565 && ctrl_x_mode == 0
1566# endif
1567 )
1568 {
1569force_cindent:
1570 /*
1571 * Indent now if a key was typed that is in 'cinkeys'.
1572 */
1573 if (in_cinkeys(c, ' ', line_is_white))
1574 {
1575 if (stop_arrow() == OK)
1576 /* re-indent the current line */
1577 do_c_expr_indent();
1578 }
1579 }
1580#endif /* FEAT_CINDENT */
1581
1582 } /* for (;;) */
1583 /* NOTREACHED */
1584}
1585
1586/*
1587 * Redraw for Insert mode.
1588 * This is postponed until getting the next character to make '$' in the 'cpo'
1589 * option work correctly.
1590 * Only redraw when there are no characters available. This speeds up
1591 * inserting sequences of characters (e.g., for CTRL-R).
1592 */
1593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001594ins_redraw(
1595 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001597#ifdef FEAT_CONCEAL
1598 linenr_T conceal_old_cursor_line = 0;
1599 linenr_T conceal_new_cursor_line = 0;
1600 int conceal_update_lines = FALSE;
1601#endif
1602
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001603 if (char_avail())
1604 return;
1605
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001606#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001607 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1608 * visible, the command might delete it. */
1609 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001610# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001611 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001612# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001613# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001614 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001615# endif
1616# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001617 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001618# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001619 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001620# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001621 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001622# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001623# ifdef FEAT_INS_EXPAND
1624 && !pum_visible()
1625# endif
1626 )
1627 {
1628# ifdef FEAT_SYN_HL
1629 /* Need to update the screen first, to make sure syntax
1630 * highlighting is correct after making a change (e.g., inserting
1631 * a "(". The autocommand may also require a redraw, so it's done
1632 * again below, unfortunately. */
1633 if (syntax_present(curwin) && must_redraw)
1634 update_screen(0);
1635# endif
1636# ifdef FEAT_AUTOCMD
1637 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001638 {
1639 /* Make sure curswant is correct, an autocommand may call
1640 * getcurpos(). */
1641 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001642 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001643 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001644# endif
1645# ifdef FEAT_CONCEAL
1646 if (curwin->w_p_cole > 0)
1647 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001648# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001649 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001650# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001651 conceal_new_cursor_line = curwin->w_cursor.lnum;
1652 conceal_update_lines = TRUE;
1653 }
1654# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001655# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001656 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001657# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001658 }
1659#endif
1660
1661#ifdef FEAT_AUTOCMD
1662 /* Trigger TextChangedI if b_changedtick differs. */
1663 if (ready && has_textchangedI()
1664 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001665# ifdef FEAT_INS_EXPAND
1666 && !pum_visible()
1667# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001668 )
1669 {
1670 if (last_changedtick_buf == curbuf)
1671 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1672 last_changedtick_buf = curbuf;
1673 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001675#endif
1676
1677 if (must_redraw)
1678 update_screen(0);
1679 else if (clear_cmdline || redraw_cmdline)
1680 showmode(); /* clear cmdline and show mode */
1681# if defined(FEAT_CONCEAL)
1682 if ((conceal_update_lines
1683 && (conceal_old_cursor_line != conceal_new_cursor_line
1684 || conceal_cursor_line(curwin)))
1685 || need_cursor_line_redraw)
1686 {
1687 if (conceal_old_cursor_line != conceal_new_cursor_line)
1688 update_single_line(curwin, conceal_old_cursor_line);
1689 update_single_line(curwin, conceal_new_cursor_line == 0
1690 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1691 curwin->w_valid &= ~VALID_CROW;
1692 }
1693# endif
1694 showruler(FALSE);
1695 setcursor();
1696 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697}
1698
1699/*
1700 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1701 */
1702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001703ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
1705 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001706 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
1708 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001709 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001712 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001714 did_putchar = TRUE;
1715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1717
1718#ifdef FEAT_CMDL_INFO
1719 add_to_showcmd_c(Ctrl_V);
1720#endif
1721
1722 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001723 if (did_putchar)
1724 /* when the line fits in 'columns' the '^' is at the start of the next
1725 * line and will not removed by the redraw */
1726 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#ifdef FEAT_CMDL_INFO
1728 clear_showcmd();
1729#endif
1730 insert_special(c, FALSE, TRUE);
1731#ifdef FEAT_RIGHTLEFT
1732 revins_chars++;
1733 revins_legal++;
1734#endif
1735}
1736
1737/*
1738 * Put a character directly onto the screen. It's not stored in a buffer.
1739 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1740 */
1741static int pc_status;
1742#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1743#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1744#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1745#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747static int pc_attr;
1748static int pc_row;
1749static int pc_col;
1750
1751 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001752edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 int attr;
1755
1756 if (ScreenLines != NULL)
1757 {
1758 update_topline(); /* just in case w_topline isn't valid */
1759 validate_cursor();
1760 if (highlight)
1761 attr = hl_attr(HLF_8);
1762 else
1763 attr = 0;
1764 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1765 pc_col = W_WINCOL(curwin);
1766#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1767 pc_status = PC_STATUS_UNSET;
1768#endif
1769#ifdef FEAT_RIGHTLEFT
1770 if (curwin->w_p_rl)
1771 {
1772 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1773# ifdef FEAT_MBYTE
1774 if (has_mbyte)
1775 {
1776 int fix_col = mb_fix_col(pc_col, pc_row);
1777
1778 if (fix_col != pc_col)
1779 {
1780 screen_putchar(' ', pc_row, fix_col, attr);
1781 --curwin->w_wcol;
1782 pc_status = PC_STATUS_RIGHT;
1783 }
1784 }
1785# endif
1786 }
1787 else
1788#endif
1789 {
1790 pc_col += curwin->w_wcol;
1791#ifdef FEAT_MBYTE
1792 if (mb_lefthalve(pc_row, pc_col))
1793 pc_status = PC_STATUS_LEFT;
1794#endif
1795 }
1796
1797 /* save the character to be able to put it back */
1798#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1799 if (pc_status == PC_STATUS_UNSET)
1800#endif
1801 {
1802 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1803 pc_status = PC_STATUS_SET;
1804 }
1805 screen_putchar(c, pc_row, pc_col, attr);
1806 }
1807}
1808
1809/*
1810 * Undo the previous edit_putchar().
1811 */
1812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001813edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
1815 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1816 {
1817#if defined(FEAT_MBYTE)
1818 if (pc_status == PC_STATUS_RIGHT)
1819 ++curwin->w_wcol;
1820 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1821 redrawWinline(curwin->w_cursor.lnum, FALSE);
1822 else
1823#endif
1824 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1825 }
1826}
1827
1828/*
1829 * Called when p_dollar is set: display a '$' at the end of the changed text
1830 * Only works when cursor is in the line that changes.
1831 */
1832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001833display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 colnr_T save_col;
1836
1837 if (!redrawing())
1838 return;
1839
1840 cursor_off();
1841 save_col = curwin->w_cursor.col;
1842 curwin->w_cursor.col = col;
1843#ifdef FEAT_MBYTE
1844 if (has_mbyte)
1845 {
1846 char_u *p;
1847
1848 /* If on the last byte of a multi-byte move to the first byte. */
1849 p = ml_get_curline();
1850 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1851 }
1852#endif
1853 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1854 if (curwin->w_wcol < W_WIDTH(curwin))
1855 {
1856 edit_putchar('$', FALSE);
1857 dollar_vcol = curwin->w_virtcol;
1858 }
1859 curwin->w_cursor.col = save_col;
1860}
1861
1862/*
1863 * Call this function before moving the cursor from the normal insert position
1864 * in insert mode.
1865 */
1866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001867undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001869 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001871 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 redrawWinline(curwin->w_cursor.lnum, FALSE);
1873 }
1874}
1875
1876/*
1877 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1878 * Keep the cursor on the same character.
1879 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1880 * type == INDENT_DEC decrease indent (for CTRL-D)
1881 * type == INDENT_SET set indent to "amount"
1882 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1883 */
1884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001885change_indent(
1886 int type,
1887 int amount,
1888 int round,
1889 int replaced, /* replaced character, put on replace stack */
1890 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 int vcol;
1893 int last_vcol;
1894 int insstart_less; /* reduction for Insstart.col */
1895 int new_cursor_col;
1896 int i;
1897 char_u *ptr;
1898 int save_p_list;
1899 int start_col;
1900 colnr_T vc;
1901#ifdef FEAT_VREPLACE
1902 colnr_T orig_col = 0; /* init for GCC */
1903 char_u *new_line, *orig_line = NULL; /* init for GCC */
1904
1905 /* VREPLACE mode needs to know what the line was like before changing */
1906 if (State & VREPLACE_FLAG)
1907 {
1908 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1909 orig_col = curwin->w_cursor.col;
1910 }
1911#endif
1912
1913 /* for the following tricks we don't want list mode */
1914 save_p_list = curwin->w_p_list;
1915 curwin->w_p_list = FALSE;
1916 vc = getvcol_nolist(&curwin->w_cursor);
1917 vcol = vc;
1918
1919 /*
1920 * For Replace mode we need to fix the replace stack later, which is only
1921 * possible when the cursor is in the indent. Remember the number of
1922 * characters before the cursor if it's possible.
1923 */
1924 start_col = curwin->w_cursor.col;
1925
1926 /* determine offset from first non-blank */
1927 new_cursor_col = curwin->w_cursor.col;
1928 beginline(BL_WHITE);
1929 new_cursor_col -= curwin->w_cursor.col;
1930
1931 insstart_less = curwin->w_cursor.col;
1932
1933 /*
1934 * If the cursor is in the indent, compute how many screen columns the
1935 * cursor is to the left of the first non-blank.
1936 */
1937 if (new_cursor_col < 0)
1938 vcol = get_indent() - vcol;
1939
1940 if (new_cursor_col > 0) /* can't fix replace stack */
1941 start_col = -1;
1942
1943 /*
1944 * Set the new indent. The cursor will be put on the first non-blank.
1945 */
1946 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001947 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 else
1949 {
1950#ifdef FEAT_VREPLACE
1951 int save_State = State;
1952
1953 /* Avoid being called recursively. */
1954 if (State & VREPLACE_FLAG)
1955 State = INSERT;
1956#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001957 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958#ifdef FEAT_VREPLACE
1959 State = save_State;
1960#endif
1961 }
1962 insstart_less -= curwin->w_cursor.col;
1963
1964 /*
1965 * Try to put cursor on same character.
1966 * If the cursor is at or after the first non-blank in the line,
1967 * compute the cursor column relative to the column of the first
1968 * non-blank character.
1969 * If we are not in insert mode, leave the cursor on the first non-blank.
1970 * If the cursor is before the first non-blank, position it relative
1971 * to the first non-blank, counted in screen columns.
1972 */
1973 if (new_cursor_col >= 0)
1974 {
1975 /*
1976 * When changing the indent while the cursor is touching it, reset
1977 * Insstart_col to 0.
1978 */
1979 if (new_cursor_col == 0)
1980 insstart_less = MAXCOL;
1981 new_cursor_col += curwin->w_cursor.col;
1982 }
1983 else if (!(State & INSERT))
1984 new_cursor_col = curwin->w_cursor.col;
1985 else
1986 {
1987 /*
1988 * Compute the screen column where the cursor should be.
1989 */
1990 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001991 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992
1993 /*
1994 * Advance the cursor until we reach the right screen column.
1995 */
1996 vcol = last_vcol = 0;
1997 new_cursor_col = -1;
1998 ptr = ml_get_curline();
1999 while (vcol <= (int)curwin->w_virtcol)
2000 {
2001 last_vcol = vcol;
2002#ifdef FEAT_MBYTE
2003 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002004 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 else
2006#endif
2007 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002008 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010 vcol = last_vcol;
2011
2012 /*
2013 * May need to insert spaces to be able to position the cursor on
2014 * the right screen column.
2015 */
2016 if (vcol != (int)curwin->w_virtcol)
2017 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002018 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002020 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 if (ptr != NULL)
2022 {
2023 new_cursor_col += i;
2024 ptr[i] = NUL;
2025 while (--i >= 0)
2026 ptr[i] = ' ';
2027 ins_str(ptr);
2028 vim_free(ptr);
2029 }
2030 }
2031
2032 /*
2033 * When changing the indent while the cursor is in it, reset
2034 * Insstart_col to 0.
2035 */
2036 insstart_less = MAXCOL;
2037 }
2038
2039 curwin->w_p_list = save_p_list;
2040
2041 if (new_cursor_col <= 0)
2042 curwin->w_cursor.col = 0;
2043 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002044 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 curwin->w_set_curswant = TRUE;
2046 changed_cline_bef_curs();
2047
2048 /*
2049 * May have to adjust the start of the insert.
2050 */
2051 if (State & INSERT)
2052 {
2053 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2054 {
2055 if ((int)Insstart.col <= insstart_less)
2056 Insstart.col = 0;
2057 else
2058 Insstart.col -= insstart_less;
2059 }
2060 if ((int)ai_col <= insstart_less)
2061 ai_col = 0;
2062 else
2063 ai_col -= insstart_less;
2064 }
2065
2066 /*
2067 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2068 * If the number of characters before the cursor decreased, need to pop a
2069 * few characters from the replace stack.
2070 * If the number of characters before the cursor increased, need to push a
2071 * few NULs onto the replace stack.
2072 */
2073 if (REPLACE_NORMAL(State) && start_col >= 0)
2074 {
2075 while (start_col > (int)curwin->w_cursor.col)
2076 {
2077 replace_join(0); /* remove a NUL from the replace stack */
2078 --start_col;
2079 }
2080 while (start_col < (int)curwin->w_cursor.col || replaced)
2081 {
2082 replace_push(NUL);
2083 if (replaced)
2084 {
2085 replace_push(replaced);
2086 replaced = NUL;
2087 }
2088 ++start_col;
2089 }
2090 }
2091
2092#ifdef FEAT_VREPLACE
2093 /*
2094 * For VREPLACE mode, we also have to fix the replace stack. In this case
2095 * it is always possible because we backspace over the whole line and then
2096 * put it back again the way we wanted it.
2097 */
2098 if (State & VREPLACE_FLAG)
2099 {
2100 /* If orig_line didn't allocate, just return. At least we did the job,
2101 * even if you can't backspace. */
2102 if (orig_line == NULL)
2103 return;
2104
2105 /* Save new line */
2106 new_line = vim_strsave(ml_get_curline());
2107 if (new_line == NULL)
2108 return;
2109
2110 /* We only put back the new line up to the cursor */
2111 new_line[curwin->w_cursor.col] = NUL;
2112
2113 /* Put back original line */
2114 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2115 curwin->w_cursor.col = orig_col;
2116
2117 /* Backspace from cursor to start of line */
2118 backspace_until_column(0);
2119
2120 /* Insert new stuff into line again */
2121 ins_bytes(new_line);
2122
2123 vim_free(new_line);
2124 }
2125#endif
2126}
2127
2128/*
2129 * Truncate the space at the end of a line. This is to be used only in an
2130 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2131 * modes.
2132 */
2133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002134truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
2136 int i;
2137
2138 /* find start of trailing white space */
2139 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2140 {
2141 if (State & REPLACE_FLAG)
2142 replace_join(0); /* remove a NUL from the replace stack */
2143 }
2144 line[i + 1] = NUL;
2145}
2146
2147#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2148 || defined(FEAT_COMMENTS) || defined(PROTO)
2149/*
2150 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2151 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002152 * Will attempt not to go before "col" even when there is a composing
2153 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 */
2155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002156backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157{
2158 while ((int)curwin->w_cursor.col > col)
2159 {
2160 curwin->w_cursor.col--;
2161 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002162 replace_do_bs(col);
2163 else if (!del_char_after_col(col))
2164 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166}
2167#endif
2168
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002169/*
2170 * Like del_char(), but make sure not to go before column "limit_col".
2171 * Only matters when there are composing characters.
2172 * Return TRUE when something was deleted.
2173 */
2174 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002176{
2177#ifdef FEAT_MBYTE
2178 if (enc_utf8 && limit_col >= 0)
2179 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002180 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002181
2182 /* Make sure the cursor is at the start of a character, but
2183 * skip forward again when going too far back because of a
2184 * composing character. */
2185 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002186 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002187 {
2188 int l = utf_ptr2len(ml_get_cursor());
2189
2190 if (l == 0) /* end of line */
2191 break;
2192 curwin->w_cursor.col += l;
2193 }
2194 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2195 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002196 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002197 }
2198 else
2199#endif
2200 (void)del_char(FALSE);
2201 return TRUE;
2202}
2203
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2205/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002206 * CTRL-X pressed in Insert mode.
2207 */
2208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002209ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002210{
2211 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2212 * CTRL-V works like CTRL-N */
2213 if (ctrl_x_mode != CTRL_X_CMDLINE)
2214 {
2215 /* if the next ^X<> won't ADD nothing, then reset
2216 * compl_cont_status */
2217 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002218 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002219 else
2220 compl_cont_status = 0;
2221 /* We're not sure which CTRL-X mode it will be yet */
2222 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2223 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2224 edit_submode_pre = NULL;
2225 showmode();
2226 }
2227}
2228
2229/*
2230 * Return TRUE if the 'dict' or 'tsr' option can be used.
2231 */
2232 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002233has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002234{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002235 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002236# ifdef FEAT_SPELL
2237 && !curwin->w_p_spell
2238# endif
2239 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002240 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2241 {
2242 ctrl_x_mode = 0;
2243 edit_submode = NULL;
2244 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2245 : (char_u *)_("'thesaurus' option is empty"),
2246 hl_attr(HLF_E));
2247 if (emsg_silent == 0)
2248 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002249 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002250 setcursor();
2251 out_flush();
2252 ui_delay(2000L, FALSE);
2253 }
2254 return FALSE;
2255 }
2256 return TRUE;
2257}
2258
2259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2261 * This depends on the current mode.
2262 */
2263 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002264vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266 /* Always allow ^R - let it's results then be checked */
2267 if (c == Ctrl_R)
2268 return TRUE;
2269
Bram Moolenaare3226be2005-12-18 22:10:00 +00002270 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002271 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002272 return TRUE;
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 switch (ctrl_x_mode)
2275 {
2276 case 0: /* Not in any CTRL-X mode */
2277 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2278 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002279 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2281 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2282 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002283 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002284 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 case CTRL_X_SCROLL:
2286 return (c == Ctrl_Y || c == Ctrl_E);
2287 case CTRL_X_WHOLE_LINE:
2288 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2289 case CTRL_X_FILES:
2290 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2291 case CTRL_X_DICTIONARY:
2292 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2293 case CTRL_X_THESAURUS:
2294 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2295 case CTRL_X_TAGS:
2296 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2297#ifdef FEAT_FIND_ID
2298 case CTRL_X_PATH_PATTERNS:
2299 return (c == Ctrl_P || c == Ctrl_N);
2300 case CTRL_X_PATH_DEFINES:
2301 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2302#endif
2303 case CTRL_X_CMDLINE:
2304 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2305 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002306#ifdef FEAT_COMPL_FUNC
2307 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002308 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002309 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002310 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002311#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002312 case CTRL_X_SPELL:
2313 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002314 case CTRL_X_EVAL:
2315 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002317 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 return FALSE;
2319}
2320
2321/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002322 * Return TRUE when character "c" is part of the item currently being
2323 * completed. Used to decide whether to abandon complete mode when the menu
2324 * is visible.
2325 */
2326 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002327ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002328{
2329 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2330 /* When expanding an identifier only accept identifier chars. */
2331 return vim_isIDc(c);
2332
2333 switch (ctrl_x_mode)
2334 {
2335 case CTRL_X_FILES:
2336 /* When expanding file name only accept file name chars. But not
2337 * path separators, so that "proto/<Tab>" expands files in
2338 * "proto", not "proto/" as a whole */
2339 return vim_isfilec(c) && !vim_ispathsep(c);
2340
2341 case CTRL_X_CMDLINE:
2342 case CTRL_X_OMNI:
2343 /* Command line and Omni completion can work with just about any
2344 * printable character, but do stop at white space. */
2345 return vim_isprintc(c) && !vim_iswhite(c);
2346
2347 case CTRL_X_WHOLE_LINE:
2348 /* For while line completion a space can be part of the line. */
2349 return vim_isprintc(c);
2350 }
2351 return vim_iswordc(c);
2352}
2353
2354/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002355 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002357 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 * the rest of the word to be in -- webb
2359 */
2360 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002361ins_compl_add_infercase(
2362 char_u *str,
2363 int len,
2364 int icase,
2365 char_u *fname,
2366 int dir,
2367 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002369 char_u *p;
2370 int i, c;
2371 int actual_len; /* Take multi-byte characters */
2372 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002373 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002374 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 int has_lower = FALSE;
2376 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002378 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002380 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
Bram Moolenaara2993e12007-08-12 14:38:46 +00002382 /* Find actual length of completion. */
2383#ifdef FEAT_MBYTE
2384 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002386 p = str;
2387 actual_len = 0;
2388 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002390 mb_ptr_adv(p);
2391 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 }
2393 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002394 else
2395#endif
2396 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
Bram Moolenaara2993e12007-08-12 14:38:46 +00002398 /* Find actual length of original text. */
2399#ifdef FEAT_MBYTE
2400 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002402 p = compl_orig_text;
2403 actual_compl_length = 0;
2404 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002406 mb_ptr_adv(p);
2407 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002410 else
2411#endif
2412 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002414 /* "actual_len" may be smaller than "actual_compl_length" when using
2415 * thesaurus, only use the minimum when comparing. */
2416 min_len = actual_len < actual_compl_length
2417 ? actual_len : actual_compl_length;
2418
Bram Moolenaara2993e12007-08-12 14:38:46 +00002419 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002420 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002421 if (wca != NULL)
2422 {
2423 p = str;
2424 for (i = 0; i < actual_len; ++i)
2425#ifdef FEAT_MBYTE
2426 if (has_mbyte)
2427 wca[i] = mb_ptr2char_adv(&p);
2428 else
2429#endif
2430 wca[i] = *(p++);
2431
2432 /* Rule 1: Were any chars converted to lower? */
2433 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002434 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002435 {
2436#ifdef FEAT_MBYTE
2437 if (has_mbyte)
2438 c = mb_ptr2char_adv(&p);
2439 else
2440#endif
2441 c = *(p++);
2442 if (MB_ISLOWER(c))
2443 {
2444 has_lower = TRUE;
2445 if (MB_ISUPPER(wca[i]))
2446 {
2447 /* Rule 1 is satisfied. */
2448 for (i = actual_compl_length; i < actual_len; ++i)
2449 wca[i] = MB_TOLOWER(wca[i]);
2450 break;
2451 }
2452 }
2453 }
2454
2455 /*
2456 * Rule 2: No lower case, 2nd consecutive letter converted to
2457 * upper case.
2458 */
2459 if (!has_lower)
2460 {
2461 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002462 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002463 {
2464#ifdef FEAT_MBYTE
2465 if (has_mbyte)
2466 c = mb_ptr2char_adv(&p);
2467 else
2468#endif
2469 c = *(p++);
2470 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2471 {
2472 /* Rule 2 is satisfied. */
2473 for (i = actual_compl_length; i < actual_len; ++i)
2474 wca[i] = MB_TOUPPER(wca[i]);
2475 break;
2476 }
2477 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2478 }
2479 }
2480
2481 /* Copy the original case of the part we typed. */
2482 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002483 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002484 {
2485#ifdef FEAT_MBYTE
2486 if (has_mbyte)
2487 c = mb_ptr2char_adv(&p);
2488 else
2489#endif
2490 c = *(p++);
2491 if (MB_ISLOWER(c))
2492 wca[i] = MB_TOLOWER(wca[i]);
2493 else if (MB_ISUPPER(c))
2494 wca[i] = MB_TOUPPER(wca[i]);
2495 }
2496
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002497 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002498 * Generate encoding specific output from wide character array.
2499 * Multi-byte characters can occupy up to five bytes more than
2500 * ASCII characters, and we also need one byte for NUL, so stay
2501 * six bytes away from the edge of IObuff.
2502 */
2503 p = IObuff;
2504 i = 0;
2505 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2506#ifdef FEAT_MBYTE
2507 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002508 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002509 else
2510#endif
2511 *(p++) = wca[i++];
2512 *p = NUL;
2513
2514 vim_free(wca);
2515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002517 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2518 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002520 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521}
2522
2523/*
2524 * Add a match to the list of matches.
2525 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002526 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002527 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002529 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002530ins_compl_add(
2531 char_u *str,
2532 int len,
2533 int icase,
2534 char_u *fname,
2535 char_u **cptext, /* extra text for popup menu or NULL */
2536 int cdir,
2537 int flags,
2538 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002540 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002541 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
2543 ui_breakcheck();
2544 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002545 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 if (len < 0)
2547 len = (int)STRLEN(str);
2548
2549 /*
2550 * If the same match is already present, don't add it.
2551 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002552 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002554 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 do
2556 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002557 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002558 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002559 && match->cp_str[len] == NUL)
2560 return NOTDONE;
2561 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002562 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002565 /* Remove any popup menu before changing the list of matches. */
2566 ins_compl_del_pum();
2567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 /*
2569 * Allocate a new match structure.
2570 * Copy the values to the new match structure.
2571 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002572 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002574 return FAIL;
2575 match->cp_number = -1;
2576 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002577 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002578 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
2580 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002581 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002583 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002586 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2588 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002589 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002590 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002591 && compl_curr_match->cp_fname != NULL
2592 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002593 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002594 else if (fname != NULL)
2595 {
2596 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002597 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002600 match->cp_fname = NULL;
2601 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002602
2603 if (cptext != NULL)
2604 {
2605 int i;
2606
2607 for (i = 0; i < CPT_COUNT; ++i)
2608 if (cptext[i] != NULL && *cptext[i] != NUL)
2609 match->cp_text[i] = vim_strsave(cptext[i]);
2610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
2612 /*
2613 * Link the new match structure in the list of matches.
2614 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002615 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002616 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 else if (dir == FORWARD)
2618 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002619 match->cp_next = compl_curr_match->cp_next;
2620 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
2622 else /* BACKWARD */
2623 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002624 match->cp_next = compl_curr_match;
2625 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002627 if (match->cp_next)
2628 match->cp_next->cp_prev = match;
2629 if (match->cp_prev)
2630 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002632 compl_first_match = match;
2633 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002635 /*
2636 * Find the longest common string if still doing that.
2637 */
2638 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2639 ins_compl_longest_match(match);
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 return OK;
2642}
2643
2644/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002645 * Return TRUE if "str[len]" matches with match->cp_str, considering
2646 * match->cp_icase.
2647 */
2648 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002649ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002650{
2651 if (match->cp_icase)
2652 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2653 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2654}
2655
2656/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002657 * Reduce the longest common string for match "match".
2658 */
2659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002660ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002661{
2662 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002663 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002664 int had_match;
2665
2666 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002667 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002668 /* First match, use it as a whole. */
2669 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002670 if (compl_leader != NULL)
2671 {
2672 had_match = (curwin->w_cursor.col > compl_col);
2673 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002674 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002675 ins_redraw(FALSE);
2676
2677 /* When the match isn't there (to avoid matching itself) remove it
2678 * again after redrawing. */
2679 if (!had_match)
2680 ins_compl_delete();
2681 compl_used_match = FALSE;
2682 }
2683 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002684 else
2685 {
2686 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002687 p = compl_leader;
2688 s = match->cp_str;
2689 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002690 {
2691#ifdef FEAT_MBYTE
2692 if (has_mbyte)
2693 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002694 c1 = mb_ptr2char(p);
2695 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002696 }
2697 else
2698#endif
2699 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002700 c1 = *p;
2701 c2 = *s;
2702 }
2703 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2704 : (c1 != c2))
2705 break;
2706#ifdef FEAT_MBYTE
2707 if (has_mbyte)
2708 {
2709 mb_ptr_adv(p);
2710 mb_ptr_adv(s);
2711 }
2712 else
2713#endif
2714 {
2715 ++p;
2716 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002717 }
2718 }
2719
2720 if (*p != NUL)
2721 {
2722 /* Leader was shortened, need to change the inserted text. */
2723 *p = NUL;
2724 had_match = (curwin->w_cursor.col > compl_col);
2725 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002726 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002727 ins_redraw(FALSE);
2728
2729 /* When the match isn't there (to avoid matching itself) remove it
2730 * again after redrawing. */
2731 if (!had_match)
2732 ins_compl_delete();
2733 }
2734
2735 compl_used_match = FALSE;
2736 }
2737}
2738
2739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 * Add an array of matches to the list of matches.
2741 * Frees matches[].
2742 */
2743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002744ins_compl_add_matches(
2745 int num_matches,
2746 char_u **matches,
2747 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748{
2749 int i;
2750 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002751 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
Bram Moolenaar572cb562005-08-05 21:35:02 +00002753 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002754 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002755 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002757 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 FreeWild(num_matches, matches);
2759}
2760
2761/* Make the completion list cyclic.
2762 * Return the number of matches (excluding the original).
2763 */
2764 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002765ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002767 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 int count = 0;
2769
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002770 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 {
2772 /*
2773 * Find the end of the list.
2774 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002775 match = compl_first_match;
2776 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002777 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002779 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 ++count;
2781 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002782 match->cp_next = compl_first_match;
2783 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 }
2785 return count;
2786}
2787
Bram Moolenaara94bc432006-03-10 21:42:59 +00002788/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002789 * Set variables that store noselect and noinsert behavior from the
2790 * 'completeopt' value.
2791 */
2792 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002793completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002794{
2795 compl_no_insert = FALSE;
2796 compl_no_select = FALSE;
2797 if (strstr((char *)p_cot, "noselect") != NULL)
2798 compl_no_select = TRUE;
2799 if (strstr((char *)p_cot, "noinsert") != NULL)
2800 compl_no_insert = TRUE;
2801}
2802
2803/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002804 * Start completion for the complete() function.
2805 * "startcol" is where the matched text starts (1 is first column).
2806 * "list" is the list of matches.
2807 */
2808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002809set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002810{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002811 int save_w_wrow = curwin->w_wrow;
2812
Bram Moolenaara94bc432006-03-10 21:42:59 +00002813 /* If already doing completions stop it. */
2814 if (ctrl_x_mode != 0)
2815 ins_compl_prep(' ');
2816 ins_compl_clear();
2817
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002818 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002819 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002820 startcol = curwin->w_cursor.col;
2821 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002822 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002823 /* compl_pattern doesn't need to be set */
2824 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2825 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002826 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002827 return;
2828
Bram Moolenaare4214502015-03-05 18:08:43 +01002829 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002830
2831 ins_compl_add_list(list);
2832 compl_matches = ins_compl_make_cyclic();
2833 compl_started = TRUE;
2834 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002835 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002836
2837 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002838 if (compl_no_insert || compl_no_select)
2839 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002840 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002841 if (compl_no_select)
2842 /* Down/Up has no real effect. */
2843 ins_complete(K_UP, FALSE);
2844 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002845 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002846 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002847 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002848
2849 /* Lazily show the popup menu, unless we got interrupted. */
2850 if (!compl_interrupted)
2851 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002852 out_flush();
2853}
2854
2855
Bram Moolenaar9372a112005-12-06 19:59:18 +00002856/* "compl_match_array" points the currently displayed list of entries in the
2857 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002858static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002859static int compl_match_arraysize;
2860
2861/*
2862 * Update the screen and when there is any scrolling remove the popup menu.
2863 */
2864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002865ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002866{
2867 int h;
2868
2869 if (compl_match_array != NULL)
2870 {
2871 h = curwin->w_cline_height;
2872 update_screen(0);
2873 if (h != curwin->w_cline_height)
2874 ins_compl_del_pum();
2875 }
2876}
2877
2878/*
2879 * Remove any popup menu.
2880 */
2881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002882ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002883{
2884 if (compl_match_array != NULL)
2885 {
2886 pum_undisplay();
2887 vim_free(compl_match_array);
2888 compl_match_array = NULL;
2889 }
2890}
2891
2892/*
2893 * Return TRUE if the popup menu should be displayed.
2894 */
2895 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002896pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002897{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002898 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002899 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002900 return FALSE;
2901
2902 /* The display looks bad on a B&W display. */
2903 if (t_colors < 8
2904#ifdef FEAT_GUI
2905 && !gui.in_use
2906#endif
2907 )
2908 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002909 return TRUE;
2910}
2911
2912/*
2913 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002914 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002915 */
2916 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002917pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002918{
2919 compl_T *compl;
2920 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002921
2922 /* Don't display the popup menu if there are no matches or there is only
2923 * one (ignoring the original text). */
2924 compl = compl_first_match;
2925 i = 0;
2926 do
2927 {
2928 if (compl == NULL
2929 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2930 break;
2931 compl = compl->cp_next;
2932 } while (compl != compl_first_match);
2933
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002934 if (strstr((char *)p_cot, "menuone") != NULL)
2935 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002936 return (i >= 2);
2937}
2938
2939/*
2940 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002941 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002942 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002944ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002945{
2946 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002947 compl_T *shown_compl = NULL;
2948 int did_find_shown_match = FALSE;
2949 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002950 int i;
2951 int cur = -1;
2952 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002953 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002954
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002955 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002956 return;
2957
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002958#if defined(FEAT_EVAL)
2959 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2960 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2961#endif
2962
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002963 /* Update the screen before drawing the popup menu over it. */
2964 update_screen(0);
2965
2966 if (compl_match_array == NULL)
2967 {
2968 /* Need to build the popup menu list. */
2969 compl_match_arraysize = 0;
2970 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002971 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002972 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002973 do
2974 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002975 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2976 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002977 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002978 ++compl_match_arraysize;
2979 compl = compl->cp_next;
2980 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002981 if (compl_match_arraysize == 0)
2982 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002983 compl_match_array = (pumitem_T *)alloc_clear(
2984 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002985 * compl_match_arraysize));
2986 if (compl_match_array != NULL)
2987 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002988 /* If the current match is the original text don't find the first
2989 * match after it, don't highlight anything. */
2990 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2991 shown_match_ok = TRUE;
2992
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002993 i = 0;
2994 compl = compl_first_match;
2995 do
2996 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002997 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2998 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002999 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003000 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003001 if (!shown_match_ok)
3002 {
3003 if (compl == compl_shown_match || did_find_shown_match)
3004 {
3005 /* This item is the shown match or this is the
3006 * first displayed item after the shown match. */
3007 compl_shown_match = compl;
3008 did_find_shown_match = TRUE;
3009 shown_match_ok = TRUE;
3010 }
3011 else
3012 /* Remember this displayed match for when the
3013 * shown match is just below it. */
3014 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003015 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003016 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003017
3018 if (compl->cp_text[CPT_ABBR] != NULL)
3019 compl_match_array[i].pum_text =
3020 compl->cp_text[CPT_ABBR];
3021 else
3022 compl_match_array[i].pum_text = compl->cp_str;
3023 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3024 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3025 if (compl->cp_text[CPT_MENU] != NULL)
3026 compl_match_array[i++].pum_extra =
3027 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003028 else
3029 compl_match_array[i++].pum_extra = compl->cp_fname;
3030 }
3031
3032 if (compl == compl_shown_match)
3033 {
3034 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003035
3036 /* When the original text is the shown match don't set
3037 * compl_shown_match. */
3038 if (compl->cp_flags & ORIGINAL_TEXT)
3039 shown_match_ok = TRUE;
3040
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003041 if (!shown_match_ok && shown_compl != NULL)
3042 {
3043 /* The shown match isn't displayed, set it to the
3044 * previously displayed match. */
3045 compl_shown_match = shown_compl;
3046 shown_match_ok = TRUE;
3047 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003048 }
3049 compl = compl->cp_next;
3050 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003051
3052 if (!shown_match_ok) /* no displayed match at all */
3053 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003054 }
3055 }
3056 else
3057 {
3058 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003059 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003060 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3061 || compl_match_array[i].pum_text
3062 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003063 {
3064 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003065 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003066 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003067 }
3068
3069 if (compl_match_array != NULL)
3070 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003071 /* In Replace mode when a $ is displayed at the end of the line only
3072 * part of the screen would be updated. We do need to redraw here. */
3073 dollar_vcol = -1;
3074
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003075 /* Compute the screen column of the start of the completed text.
3076 * Use the cursor to get all wrapping and other settings right. */
3077 col = curwin->w_cursor.col;
3078 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003079 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003080 curwin->w_cursor.col = col;
3081 }
3082}
3083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084#define DICT_FIRST (1) /* use just first element in "dict" */
3085#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003086
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003088 * Add any identifiers that match the given pattern in the list of dictionary
3089 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 */
3091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003092ins_compl_dictionaries(
3093 char_u *dict_start,
3094 char_u *pat,
3095 int flags, /* DICT_FIRST and/or DICT_EXACT */
3096 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003098 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 char_u *ptr;
3100 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 char_u **files;
3103 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003105 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
Bram Moolenaar0b238792006-03-02 22:49:12 +00003107 if (*dict == NUL)
3108 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003109#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003110 /* When 'dictionary' is empty and spell checking is enabled use
3111 * "spell". */
3112 if (!thesaurus && curwin->w_p_spell)
3113 dict = (char_u *)"spell";
3114 else
3115#endif
3116 return;
3117 }
3118
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003120 if (buf == NULL)
3121 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003122 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003123
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 /* If 'infercase' is set, don't use 'smartcase' here */
3125 save_p_scs = p_scs;
3126 if (curbuf->b_p_inf)
3127 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003128
3129 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3130 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003131 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003132 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003133 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003134 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003135 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003136
3137 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003138 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003139 len = STRLEN(pat_esc) + 10;
3140 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003141 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003142 {
3143 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003144 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003145 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003146 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003147 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3148 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003149 vim_free(ptr);
3150 }
3151 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003152 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003153 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003154 if (regmatch.regprog == NULL)
3155 goto theend;
3156 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003157
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3159 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003160 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 {
3162 /* copy one dictionary file name into buf */
3163 if (flags == DICT_EXACT)
3164 {
3165 count = 1;
3166 files = &dict;
3167 }
3168 else
3169 {
3170 /* Expand wildcards in the dictionary name, but do not allow
3171 * backticks (for security, the 'dict' option may have been set in
3172 * a modeline). */
3173 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003174# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003175 if (!thesaurus && STRCMP(buf, "spell") == 0)
3176 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003177 else
3178# endif
3179 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 || expand_wildcards(1, &buf, &count, &files,
3181 EW_FILE|EW_SILENT) != OK)
3182 count = 0;
3183 }
3184
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003185# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003186 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003188 /* Complete from active spelling. Skip "\<" in the pattern, we
3189 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003190 if (pat[0] == '\\' && pat[1] == '<')
3191 ptr = pat + 2;
3192 else
3193 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003194 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003196 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003197# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003198 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003199 {
3200 ins_compl_files(count, files, thesaurus, flags,
3201 &regmatch, buf, &dir);
3202 if (flags != DICT_EXACT)
3203 FreeWild(count, files);
3204 }
3205 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 break;
3207 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003208
3209theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003211 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 vim_free(buf);
3213}
3214
Bram Moolenaar0b238792006-03-02 22:49:12 +00003215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003216ins_compl_files(
3217 int count,
3218 char_u **files,
3219 int thesaurus,
3220 int flags,
3221 regmatch_T *regmatch,
3222 char_u *buf,
3223 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003224{
3225 char_u *ptr;
3226 int i;
3227 FILE *fp;
3228 int add_r;
3229
3230 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3231 {
3232 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3233 if (flags != DICT_EXACT)
3234 {
3235 vim_snprintf((char *)IObuff, IOSIZE,
3236 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003237 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003238 }
3239
3240 if (fp != NULL)
3241 {
3242 /*
3243 * Read dictionary file line by line.
3244 * Check each line for a match.
3245 */
3246 while (!got_int && !compl_interrupted
3247 && !vim_fgets(buf, LSIZE, fp))
3248 {
3249 ptr = buf;
3250 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3251 {
3252 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003253 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003254 ptr = find_line_end(ptr);
3255 else
3256 ptr = find_word_end(ptr);
3257 add_r = ins_compl_add_infercase(regmatch->startp[0],
3258 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003259 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003260 if (thesaurus)
3261 {
3262 char_u *wstart;
3263
3264 /*
3265 * Add the other matches on the line
3266 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003267 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003268 while (!got_int)
3269 {
3270 /* Find start of the next word. Skip white
3271 * space and punctuation. */
3272 ptr = find_word_start(ptr);
3273 if (*ptr == NUL || *ptr == NL)
3274 break;
3275 wstart = ptr;
3276
Bram Moolenaara2993e12007-08-12 14:38:46 +00003277 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003278#ifdef FEAT_MBYTE
3279 if (has_mbyte)
3280 /* Japanese words may have characters in
3281 * different classes, only separate words
3282 * with single-byte non-word characters. */
3283 while (*ptr != NUL)
3284 {
3285 int l = (*mb_ptr2len)(ptr);
3286
3287 if (l < 2 && !vim_iswordc(*ptr))
3288 break;
3289 ptr += l;
3290 }
3291 else
3292#endif
3293 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003294
3295 /* Add the word. Skip the regexp match. */
3296 if (wstart != regmatch->startp[0])
3297 add_r = ins_compl_add_infercase(wstart,
3298 (int)(ptr - wstart),
3299 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003300 }
3301 }
3302 if (add_r == OK)
3303 /* if dir was BACKWARD then honor it just once */
3304 *dir = FORWARD;
3305 else if (add_r == FAIL)
3306 break;
3307 /* avoid expensive call to vim_regexec() when at end
3308 * of line */
3309 if (*ptr == '\n' || got_int)
3310 break;
3311 }
3312 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003313 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003314 }
3315 fclose(fp);
3316 }
3317 }
3318}
3319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320/*
3321 * Find the start of the next word.
3322 * Returns a pointer to the first char of the word. Also stops at a NUL.
3323 */
3324 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003325find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
3327#ifdef FEAT_MBYTE
3328 if (has_mbyte)
3329 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003330 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 else
3332#endif
3333 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3334 ++ptr;
3335 return ptr;
3336}
3337
3338/*
3339 * Find the end of the word. Assumes it starts inside a word.
3340 * Returns a pointer to just after the word.
3341 */
3342 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003343find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345#ifdef FEAT_MBYTE
3346 int start_class;
3347
3348 if (has_mbyte)
3349 {
3350 start_class = mb_get_class(ptr);
3351 if (start_class > 1)
3352 while (*ptr != NUL)
3353 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003354 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (mb_get_class(ptr) != start_class)
3356 break;
3357 }
3358 }
3359 else
3360#endif
3361 while (vim_iswordc(*ptr))
3362 ++ptr;
3363 return ptr;
3364}
3365
3366/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003367 * Find the end of the line, omitting CR and NL at the end.
3368 * Returns a pointer to just after the line.
3369 */
3370 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003371find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003372{
3373 char_u *s;
3374
3375 s = ptr + STRLEN(ptr);
3376 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3377 --s;
3378 return s;
3379}
3380
3381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 * Free the list of completions
3383 */
3384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003385ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003387 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003388 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003390 vim_free(compl_pattern);
3391 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003392 vim_free(compl_leader);
3393 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003395 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003397
3398 ins_compl_del_pum();
3399 pum_clear();
3400
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003401 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 do
3403 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003404 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003405 compl_curr_match = compl_curr_match->cp_next;
3406 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003408 if (match->cp_flags & FREE_FNAME)
3409 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003410 for (i = 0; i < CPT_COUNT; ++i)
3411 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003413 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3414 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003415 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416}
3417
3418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003419ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003421 compl_cont_status = 0;
3422 compl_started = FALSE;
3423 compl_matches = 0;
3424 vim_free(compl_pattern);
3425 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003426 vim_free(compl_leader);
3427 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003429 vim_free(compl_orig_text);
3430 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003431 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003432 /* clear v:completed_item */
3433 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434}
3435
3436/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003437 * Return TRUE when Insert completion is active.
3438 */
3439 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003440ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003441{
3442 return compl_started;
3443}
3444
3445/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003446 * Delete one character before the cursor and show the subset of the matches
3447 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003448 * Returns the character to be used, NUL if the work is done and another char
3449 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003450 */
3451 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003452ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003453{
3454 char_u *line;
3455 char_u *p;
3456
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003457 line = ml_get_curline();
3458 p = line + curwin->w_cursor.col;
3459 mb_ptr_back(line, p);
3460
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003461 /* Stop completion when the whole word was deleted. For Omni completion
3462 * allow the word to be deleted, we won't match everything. */
3463 if ((int)(p - line) - (int)compl_col < 0
3464 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003465 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003466 return K_BS;
3467
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003468 /* Deleted more than what was used to find matches or didn't finish
3469 * finding all matches: need to look for matches all over again. */
3470 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003471 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003472 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003473
Bram Moolenaara6557602006-02-04 22:43:20 +00003474 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003475 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003476 if (compl_leader != NULL)
3477 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003478 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003479 if (compl_shown_match != NULL)
3480 /* Make sure current match is not a hidden item. */
3481 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003482 return NUL;
3483 }
3484 return K_BS;
3485}
Bram Moolenaara6557602006-02-04 22:43:20 +00003486
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003487/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003488 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3489 * be called.
3490 */
3491 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003492ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003493{
3494 /* Return TRUE if we didn't complete finding matches or when the
3495 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3496 return compl_was_interrupted
3497 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3498 && compl_opt_refresh_always);
3499}
3500
3501/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003502 * Called after changing "compl_leader".
3503 * Show the popup menu with a different set of matches.
3504 * May also search for matches again if the previous search was interrupted.
3505 */
3506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003507ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003508{
3509 ins_compl_del_pum();
3510 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003511 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003512 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003513
3514 if (compl_started)
3515 ins_compl_set_original_text(compl_leader);
3516 else
3517 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003518#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003519 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003520#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003521 /*
3522 * Matches were cleared, need to search for them now. First display
3523 * the changed text before the cursor. Set "compl_restarting" to
3524 * avoid that the first match is inserted.
3525 */
3526 update_screen(0);
3527#ifdef FEAT_GUI
3528 if (gui.in_use)
3529 {
3530 /* Show the cursor after the match, not after the redrawn text. */
3531 setcursor();
3532 out_flush();
3533 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003534 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003535#endif
3536 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003537 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003538 compl_cont_status = 0;
3539 compl_restarting = FALSE;
3540 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003541
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003542 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003543
3544 /* Show the popup menu with a different set of matches. */
3545 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003546
3547 /* Don't let Enter select the original text when there is no popup menu. */
3548 if (compl_match_array == NULL)
3549 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003550}
3551
3552/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003553 * Return the length of the completion, from the completion start column to
3554 * the cursor column. Making sure it never goes below zero.
3555 */
3556 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003557ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003558{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003559 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003560
3561 if (off < 0)
3562 return 0;
3563 return off;
3564}
3565
3566/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003567 * Append one character to the match leader. May reduce the number of
3568 * matches.
3569 */
3570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003571ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003572{
3573#ifdef FEAT_MBYTE
3574 int cc;
3575
3576 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3577 {
3578 char_u buf[MB_MAXBYTES + 1];
3579
3580 (*mb_char2bytes)(c, buf);
3581 buf[cc] = NUL;
3582 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003583 if (compl_opt_refresh_always)
3584 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003585 }
3586 else
3587#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003588 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003589 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003590 if (compl_opt_refresh_always)
3591 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003592 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003593
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003594 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003595 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003596 ins_compl_restart();
3597
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003598 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003599 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003600 * break redo. */
3601 if (!compl_opt_refresh_always)
3602 {
3603 vim_free(compl_leader);
3604 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003605 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003606 if (compl_leader != NULL)
3607 ins_compl_new_leader();
3608 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003609}
3610
3611/*
3612 * Setup for finding completions again without leaving CTRL-X mode. Used when
3613 * BS or a key was typed while still searching for matches.
3614 */
3615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003616ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003617{
3618 ins_compl_free();
3619 compl_started = FALSE;
3620 compl_matches = 0;
3621 compl_cont_status = 0;
3622 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003623}
3624
3625/*
3626 * Set the first match, the original text.
3627 */
3628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003629ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003630{
3631 char_u *p;
3632
3633 /* Replace the original text entry. */
3634 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3635 {
3636 p = vim_strsave(str);
3637 if (p != NULL)
3638 {
3639 vim_free(compl_first_match->cp_str);
3640 compl_first_match->cp_str = p;
3641 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003642 }
3643}
3644
3645/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003646 * Append one character to the match leader. May reduce the number of
3647 * matches.
3648 */
3649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003650ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003651{
3652 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003653 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003654 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003655 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003656
3657 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003658 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003659 {
3660 /* When still at the original match use the first entry that matches
3661 * the leader. */
3662 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3663 {
3664 p = NULL;
3665 for (cp = compl_shown_match->cp_next; cp != NULL
3666 && cp != compl_first_match; cp = cp->cp_next)
3667 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003668 if (compl_leader == NULL
3669 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003670 (int)STRLEN(compl_leader)))
3671 {
3672 p = cp->cp_str;
3673 break;
3674 }
3675 }
3676 if (p == NULL || (int)STRLEN(p) <= len)
3677 return;
3678 }
3679 else
3680 return;
3681 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003682 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003683 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003684 ins_compl_addleader(c);
3685}
3686
3687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003689 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003690 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003692 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003693ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694{
3695 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003697 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698
3699 /* Forget any previous 'special' messages if this is actually
3700 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3701 */
3702 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3703 edit_submode_extra = NULL;
3704
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003705 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003706 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3707 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003708 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003710 /* Set "compl_get_longest" when finding the first matches. */
3711 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3712 || (ctrl_x_mode == 0 && !compl_started))
3713 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003714 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003715 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003716
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003717 }
3718
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3720 {
3721 /*
3722 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3723 * it will be yet. Now we decide.
3724 */
3725 switch (c)
3726 {
3727 case Ctrl_E:
3728 case Ctrl_Y:
3729 ctrl_x_mode = CTRL_X_SCROLL;
3730 if (!(State & REPLACE_FLAG))
3731 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3732 else
3733 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3734 edit_submode_pre = NULL;
3735 showmode();
3736 break;
3737 case Ctrl_L:
3738 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3739 break;
3740 case Ctrl_F:
3741 ctrl_x_mode = CTRL_X_FILES;
3742 break;
3743 case Ctrl_K:
3744 ctrl_x_mode = CTRL_X_DICTIONARY;
3745 break;
3746 case Ctrl_R:
3747 /* Simply allow ^R to happen without affecting ^X mode */
3748 break;
3749 case Ctrl_T:
3750 ctrl_x_mode = CTRL_X_THESAURUS;
3751 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003752#ifdef FEAT_COMPL_FUNC
3753 case Ctrl_U:
3754 ctrl_x_mode = CTRL_X_FUNCTION;
3755 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003756 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003757 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003758 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003759#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003760 case 's':
3761 case Ctrl_S:
3762 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003763#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003764 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003765 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003766 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003767#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003768 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 case Ctrl_RSB:
3770 ctrl_x_mode = CTRL_X_TAGS;
3771 break;
3772#ifdef FEAT_FIND_ID
3773 case Ctrl_I:
3774 case K_S_TAB:
3775 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3776 break;
3777 case Ctrl_D:
3778 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3779 break;
3780#endif
3781 case Ctrl_V:
3782 case Ctrl_Q:
3783 ctrl_x_mode = CTRL_X_CMDLINE;
3784 break;
3785 case Ctrl_P:
3786 case Ctrl_N:
3787 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3788 * just started ^X mode, or there were enough ^X's to cancel
3789 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3790 * do normal expansion when interrupting a different mode (say
3791 * ^X^F^X^P or ^P^X^X^P, see below)
3792 * nothing changes if interrupting mode 0, (eg, the flag
3793 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003794 if (!(compl_cont_status & CONT_INTRPT))
3795 compl_cont_status |= CONT_LOCAL;
3796 else if (compl_cont_mode != 0)
3797 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 /* FALLTHROUGH */
3799 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003800 /* If we have typed at least 2 ^X's... for modes != 0, we set
3801 * compl_cont_status = 0 (eg, as if we had just started ^X
3802 * mode).
3803 * For mode 0, we set "compl_cont_mode" to an impossible
3804 * value, in both cases ^X^X can be used to restart the same
3805 * mode (avoiding ADDING mode).
3806 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3807 * 'complete' and local ^P expansions respectively.
3808 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3809 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if (c == Ctrl_X)
3811 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003812 if (compl_cont_mode != 0)
3813 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003815 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817 ctrl_x_mode = 0;
3818 edit_submode = NULL;
3819 showmode();
3820 break;
3821 }
3822 }
3823 else if (ctrl_x_mode != 0)
3824 {
3825 /* We're already in CTRL-X mode, do we stay in it? */
3826 if (!vim_is_ctrl_x_key(c))
3827 {
3828 if (ctrl_x_mode == CTRL_X_SCROLL)
3829 ctrl_x_mode = 0;
3830 else
3831 ctrl_x_mode = CTRL_X_FINISHED;
3832 edit_submode = NULL;
3833 }
3834 showmode();
3835 }
3836
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003837 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
3839 /* Show error message from attempted keyword completion (probably
3840 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003841 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003843 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3844 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 || ctrl_x_mode == CTRL_X_FINISHED)
3846 {
3847 /* Get here when we have finished typing a sequence of ^N and
3848 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003849 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003850 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
3852 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003853 * If any of the original typed text has been changed, eg when
3854 * ignorecase is set, we must add back-spaces to the redo
3855 * buffer. We add as few as necessary to delete just the part
3856 * of the original text that has changed.
3857 * When using the longest match, edited the match or used
3858 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003860 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3861 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003862 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003863 ptr = NULL;
3864 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866
3867#ifdef FEAT_CINDENT
3868 want_cindent = (can_cindent && cindent_on());
3869#endif
3870 /*
3871 * When completing whole lines: fix indent for 'cindent'.
3872 * Otherwise, break line if it's too long.
3873 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003874 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
3876#ifdef FEAT_CINDENT
3877 /* re-indent the current line */
3878 if (want_cindent)
3879 {
3880 do_c_expr_indent();
3881 want_cindent = FALSE; /* don't do it again */
3882 }
3883#endif
3884 }
3885 else
3886 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003887 int prev_col = curwin->w_cursor.col;
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003890 if (prev_col > 0)
3891 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003892 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003893 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003895 if (prev_col > 0
3896 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3897 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003900 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003901 * the selection without inserting anything. When
3902 * compl_enter_selects is set the Enter key does the same. */
3903 if ((c == Ctrl_Y || (compl_enter_selects
3904 && (c == CAR || c == K_KENTER || c == NL)))
3905 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003906 retval = TRUE;
3907
Bram Moolenaar47247282016-08-02 22:36:02 +02003908 /* CTRL-E means completion is Ended, go back to the typed text.
3909 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003910 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003911 {
3912 ins_compl_delete();
3913 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003914 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003915 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003916 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003917 retval = TRUE;
3918 }
3919
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003920 auto_format(FALSE, TRUE);
3921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003923 compl_started = FALSE;
3924 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003925 if (!shortmess(SHM_COMPLETIONMENU))
3926 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003928 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (edit_submode != NULL)
3930 {
3931 edit_submode = NULL;
3932 showmode();
3933 }
3934
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003935#ifdef FEAT_CMDWIN
3936 if (c == Ctrl_C && cmdwin_type != 0)
3937 /* Avoid the popup menu remains displayed when leaving the
3938 * command line window. */
3939 update_screen(0);
3940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941#ifdef FEAT_CINDENT
3942 /*
3943 * Indent now if a key was typed that is in 'cinkeys'.
3944 */
3945 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3946 do_c_expr_indent();
3947#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003948#ifdef FEAT_AUTOCMD
3949 /* Trigger the CompleteDone event to give scripts a chance to act
3950 * upon the completion. */
3951 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
3954 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003955#ifdef FEAT_AUTOCMD
3956 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3957 /* Trigger the CompleteDone event to give scripts a chance to act
3958 * upon the (possibly failed) completion. */
3959 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
3962 /* reset continue_* if we left expansion-mode, if we stay they'll be
3963 * (re)set properly in ins_complete() */
3964 if (!vim_is_ctrl_x_key(c))
3965 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003966 compl_cont_status = 0;
3967 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003969
3970 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971}
3972
3973/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003974 * Fix the redo buffer for the completion leader replacing some of the typed
3975 * text. This inserts backspaces and appends the changed text.
3976 * "ptr" is the known leader text or NUL.
3977 */
3978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003979ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003980{
3981 int len;
3982 char_u *p;
3983 char_u *ptr = ptr_arg;
3984
3985 if (ptr == NULL)
3986 {
3987 if (compl_leader != NULL)
3988 ptr = compl_leader;
3989 else
3990 return; /* nothing to do */
3991 }
3992 if (compl_orig_text != NULL)
3993 {
3994 p = compl_orig_text;
3995 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3996 ;
3997#ifdef FEAT_MBYTE
3998 if (len > 0)
3999 len -= (*mb_head_off)(p, p + len);
4000#endif
4001 for (p += len; *p != NUL; mb_ptr_adv(p))
4002 AppendCharToRedobuff(K_BS);
4003 }
4004 else
4005 len = 0;
4006 if (ptr != NULL)
4007 AppendToRedobuffLit(ptr + len, -1);
4008}
4009
4010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4012 * (depending on flag) starting from buf and looking for a non-scanned
4013 * buffer (other than curbuf). curbuf is special, if it is called with
4014 * buf=curbuf then it has to be the first call for a given flag/expansion.
4015 *
4016 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4017 */
4018 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004019ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020{
4021#ifdef FEAT_WINDOWS
4022 static win_T *wp;
4023#endif
4024
4025 if (flag == 'w') /* just windows */
4026 {
4027#ifdef FEAT_WINDOWS
4028 if (buf == curbuf) /* first call for this flag/expansion */
4029 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004030 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 && wp->w_buffer->b_scanned)
4032 ;
4033 buf = wp->w_buffer;
4034#else
4035 buf = curbuf;
4036#endif
4037 }
4038 else
4039 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4040 * (unlisted buffers)
4041 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004042 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 && ((flag == 'U'
4044 ? buf->b_p_bl
4045 : (!buf->b_p_bl
4046 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004047 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 ;
4049 return buf;
4050}
4051
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004052#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004053/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004054 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004055 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004056 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004058expand_by_function(
4059 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4060 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004061{
Bram Moolenaar82139082011-09-14 16:52:09 +02004062 list_T *matchlist = NULL;
4063 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004064 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004065 char_u *funcname;
4066 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004067 win_T *curwin_save;
4068 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004069 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004070
Bram Moolenaare344bea2005-09-01 20:46:49 +00004071 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4072 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004073 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004074
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004075 /* Call 'completefunc' to obtain the list of matches. */
4076 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004077 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004078
Bram Moolenaare344bea2005-09-01 20:46:49 +00004079 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004080 curwin_save = curwin;
4081 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004082
4083 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004084 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004085 {
4086 switch (rettv.v_type)
4087 {
4088 case VAR_LIST:
4089 matchlist = rettv.vval.v_list;
4090 break;
4091 case VAR_DICT:
4092 matchdict = rettv.vval.v_dict;
4093 break;
4094 default:
4095 /* TODO: Give error message? */
4096 clear_tv(&rettv);
4097 break;
4098 }
4099 }
4100
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004101 if (curwin_save != curwin || curbuf_save != curbuf)
4102 {
4103 EMSG(_(e_complwin));
4104 goto theend;
4105 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004106 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004107 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004108 if (!equalpos(curwin->w_cursor, pos))
4109 {
4110 EMSG(_(e_compldel));
4111 goto theend;
4112 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004113
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004114 if (matchlist != NULL)
4115 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004116 else if (matchdict != NULL)
4117 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004118
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004119theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004120 if (matchdict != NULL)
4121 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004122 if (matchlist != NULL)
4123 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004124}
4125#endif /* FEAT_COMPL_FUNC */
4126
Bram Moolenaar39f05632006-03-19 22:15:26 +00004127#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004128/*
4129 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004130 */
4131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004132ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004133{
4134 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004135 int dir = compl_direction;
4136
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004137 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004138 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004139 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004140 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4141 /* if dir was BACKWARD then honor it just once */
4142 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004143 else if (did_emsg)
4144 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004145 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004146}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004147
4148/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004149 * Add completions from a dict.
4150 */
4151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004152ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004153{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004154 dictitem_T *di_refresh;
4155 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004156
4157 /* Check for optional "refresh" item. */
4158 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004159 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4160 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004161 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004162 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004163
4164 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4165 compl_opt_refresh_always = TRUE;
4166 }
4167
4168 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004169 di_words = dict_find(dict, (char_u *)"words", 5);
4170 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4171 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004172}
4173
4174/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004175 * Add a match to the list of matches from a typeval_T.
4176 * If the given string is already in the list of completions, then return
4177 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4178 * maybe because alloc() returns NULL, then FAIL is returned.
4179 */
4180 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004181ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004182{
4183 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004184 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004185 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004186 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004187 char_u *(cptext[CPT_COUNT]);
4188
4189 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4190 {
4191 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4192 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4193 (char_u *)"abbr", FALSE);
4194 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4195 (char_u *)"menu", FALSE);
4196 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4197 (char_u *)"kind", FALSE);
4198 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4199 (char_u *)"info", FALSE);
4200 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4201 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004202 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004203 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004204 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4205 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004206 }
4207 else
4208 {
4209 word = get_tv_string_chk(tv);
4210 vim_memset(cptext, 0, sizeof(cptext));
4211 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004212 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004213 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004214 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004215}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004216#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004217
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004218/*
4219 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004220 * The search starts at position "ini" in curbuf and in the direction
4221 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004222 * When "compl_started" is FALSE start at that position, otherwise continue
4223 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004224 * This may return before finding all the matches.
4225 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 */
4227 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004228ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229{
4230 static pos_T first_match_pos;
4231 static pos_T last_match_pos;
4232 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004233 static int found_all = FALSE; /* Found all matches of a
4234 certain type. */
4235 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
Bram Moolenaar572cb562005-08-05 21:35:02 +00004237 pos_T *pos;
4238 char_u **matches;
4239 int save_p_scs;
4240 int save_p_ws;
4241 int save_p_ic;
4242 int i;
4243 int num_matches;
4244 int len;
4245 int found_new_match;
4246 int type = ctrl_x_mode;
4247 char_u *ptr;
4248 char_u *dict = NULL;
4249 int dict_f = 0;
4250 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004251 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004253 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004255 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 ins_buf->b_scanned = 0;
4257 found_all = FALSE;
4258 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004259 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004260 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 last_match_pos = first_match_pos = *ini;
4262 }
4263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004264 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004265 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4267 for (;;)
4268 {
4269 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004270 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 * or if found_all says this entry is done. For ^X^L only use the
4274 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004275 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004276 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 {
4278 found_all = FALSE;
4279 while (*e_cpt == ',' || *e_cpt == ' ')
4280 e_cpt++;
4281 if (*e_cpt == '.' && !curbuf->b_scanned)
4282 {
4283 ins_buf = curbuf;
4284 first_match_pos = *ini;
4285 /* So that ^N can match word immediately after cursor */
4286 if (ctrl_x_mode == 0)
4287 dec(&first_match_pos);
4288 last_match_pos = first_match_pos;
4289 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004290
4291 /* Remember the first match so that the loop stops when we
4292 * wrap and come back there a second time. */
4293 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4296 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4297 {
4298 /* Scan a buffer, but not the current one. */
4299 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4300 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004301 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 first_match_pos.col = last_match_pos.col = 0;
4303 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4304 last_match_pos.lnum = 0;
4305 type = 0;
4306 }
4307 else /* unloaded buffer, scan like dictionary */
4308 {
4309 found_all = TRUE;
4310 if (ins_buf->b_fname == NULL)
4311 continue;
4312 type = CTRL_X_DICTIONARY;
4313 dict = ins_buf->b_fname;
4314 dict_f = DICT_EXACT;
4315 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004316 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 ins_buf->b_fname == NULL
4318 ? buf_spname(ins_buf)
4319 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004320 ? ins_buf->b_fname
4321 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004322 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 else if (*e_cpt == NUL)
4325 break;
4326 else
4327 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004328 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 type = -1;
4330 else if (*e_cpt == 'k' || *e_cpt == 's')
4331 {
4332 if (*e_cpt == 'k')
4333 type = CTRL_X_DICTIONARY;
4334 else
4335 type = CTRL_X_THESAURUS;
4336 if (*++e_cpt != ',' && *e_cpt != NUL)
4337 {
4338 dict = e_cpt;
4339 dict_f = DICT_FIRST;
4340 }
4341 }
4342#ifdef FEAT_FIND_ID
4343 else if (*e_cpt == 'i')
4344 type = CTRL_X_PATH_PATTERNS;
4345 else if (*e_cpt == 'd')
4346 type = CTRL_X_PATH_DEFINES;
4347#endif
4348 else if (*e_cpt == ']' || *e_cpt == 't')
4349 {
4350 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004351 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004352 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 else
4355 type = -1;
4356
4357 /* in any case e_cpt is advanced to the next entry */
4358 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4359
4360 found_all = TRUE;
4361 if (type == -1)
4362 continue;
4363 }
4364 }
4365
4366 switch (type)
4367 {
4368 case -1:
4369 break;
4370#ifdef FEAT_FIND_ID
4371 case CTRL_X_PATH_PATTERNS:
4372 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004373 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004374 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004376 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4378 (linenr_T)1, (linenr_T)MAXLNUM);
4379 break;
4380#endif
4381
4382 case CTRL_X_DICTIONARY:
4383 case CTRL_X_THESAURUS:
4384 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004385 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 : (type == CTRL_X_THESAURUS
4387 ? (*curbuf->b_p_tsr == NUL
4388 ? p_tsr
4389 : curbuf->b_p_tsr)
4390 : (*curbuf->b_p_dict == NUL
4391 ? p_dict
4392 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004393 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004394 dict != NULL ? dict_f
4395 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 dict = NULL;
4397 break;
4398
4399 case CTRL_X_TAGS:
4400 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4401 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004402 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004404 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004405 * of matches is found when compl_pattern is empty */
4406 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4408 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4409 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4410 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004411 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413 p_ic = save_p_ic;
4414 break;
4415
4416 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4419 {
4420
4421 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004422 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004423 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 break;
4426
4427 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428 if (expand_cmdline(&compl_xp, compl_pattern,
4429 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004431 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 break;
4433
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004434#ifdef FEAT_COMPL_FUNC
4435 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004436 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004437 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004438 break;
4439#endif
4440
Bram Moolenaar488c6512005-08-11 20:09:58 +00004441 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004442#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004443 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004444 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004445 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004446 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004447#endif
4448 break;
4449
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 default: /* normal ^P/^N and ^X^L */
4451 /*
4452 * If 'infercase' is set, don't use 'smartcase' here
4453 */
4454 save_p_scs = p_scs;
4455 if (ins_buf->b_p_inf)
4456 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457
Bram Moolenaar361aa502014-01-23 22:45:58 +01004458 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 * end but never from the middle, thus setting nowrapscan in this
4460 * buffers is a good idea, on the other hand, we always set
4461 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4462 save_p_ws = p_ws;
4463 if (ins_buf != curbuf)
4464 p_ws = FALSE;
4465 else if (*e_cpt == '.')
4466 p_ws = TRUE;
4467 for (;;)
4468 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004469 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004471 ++msg_silent; /* Don't want messages for wrapscan. */
4472
Bram Moolenaare4214502015-03-05 18:08:43 +01004473 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4474 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004475 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004476 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004477 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004479 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004481 found_new_match = searchit(NULL, ins_buf, pos,
4482 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004483 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004484 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004485 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004486 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004488 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004489 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 first_match_pos = *pos;
4491 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004492 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004495 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 found_new_match = FAIL;
4497 if (found_new_match == FAIL)
4498 {
4499 if (ins_buf == curbuf)
4500 found_all = TRUE;
4501 break;
4502 }
4503
4504 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 && ini->lnum == pos->lnum
4507 && ini->col == pos->col)
4508 continue;
4509 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004510 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004512 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 {
4514 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4515 continue;
4516 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4517 if (!p_paste)
4518 ptr = skipwhite(ptr);
4519 }
4520 len = (int)STRLEN(ptr);
4521 }
4522 else
4523 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004524 char_u *tmp_ptr = ptr;
4525
4526 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004528 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 /* Skip if already inside a word. */
4530 if (vim_iswordp(tmp_ptr))
4531 continue;
4532 /* Find start of next word. */
4533 tmp_ptr = find_word_start(tmp_ptr);
4534 }
4535 /* Find end of this word. */
4536 tmp_ptr = find_word_end(tmp_ptr);
4537 len = (int)(tmp_ptr - ptr);
4538
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539 if ((compl_cont_status & CONT_ADDING)
4540 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
4542 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4543 {
4544 /* Try next line, if any. the new word will be
4545 * "join" as if the normal command "J" was used.
4546 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004547 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 * works -- Acevedo */
4549 STRNCPY(IObuff, ptr, len);
4550 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4551 tmp_ptr = ptr = skipwhite(ptr);
4552 /* Find start of next word. */
4553 tmp_ptr = find_word_start(tmp_ptr);
4554 /* Find end of next word. */
4555 tmp_ptr = find_word_end(tmp_ptr);
4556 if (tmp_ptr > ptr)
4557 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004558 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004560 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 IObuff[len++] = ' ';
4562 /* IObuf =~ "\k.* ", thus len >= 2 */
4563 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004564 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 || (vim_strchr(p_cpo, CPO_JOINSP)
4566 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004567 && (IObuff[len - 2] == '?'
4568 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 IObuff[len++] = ' ';
4570 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004571 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if (tmp_ptr - ptr >= IOSIZE - len)
4573 tmp_ptr = ptr + IOSIZE - len - 1;
4574 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4575 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004576 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 }
4578 IObuff[len] = NUL;
4579 ptr = IObuff;
4580 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004581 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 continue;
4583 }
4584 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004585 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004586 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004587 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 {
4589 found_new_match = OK;
4590 break;
4591 }
4592 }
4593 p_scs = save_p_scs;
4594 p_ws = save_p_ws;
4595 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004596
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004597 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004598 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004599 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 found_new_match = OK;
4601
4602 /* break the loop for specialized modes (use 'complete' just for the
4603 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004604 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004605 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004606 {
4607 if (got_int)
4608 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004609 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004610 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004611 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004612
Bram Moolenaare4214502015-03-05 18:08:43 +01004613 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004614 || compl_interrupted)
4615 break;
4616 compl_started = TRUE;
4617 }
4618 else
4619 {
4620 /* Mark a buffer scanned when it has been scanned completely */
4621 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4622 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004624 compl_started = FALSE;
4625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004627 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
Bram Moolenaare4214502015-03-05 18:08:43 +01004629 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 && *e_cpt == NUL) /* Got to end of 'complete' */
4631 found_new_match = FAIL;
4632
4633 i = -1; /* total of matches, unknown */
4634 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004635 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 i = ins_compl_make_cyclic();
4637
4638 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004639 * just been made cyclic then we have to move compl_curr_match to the next
4640 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004641 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4642 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004643 if (compl_curr_match == NULL)
4644 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 return i;
4646}
4647
4648/* Delete the old text being completed. */
4649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004650ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004652 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
4654 /*
4655 * In insert mode: Delete the typed part.
4656 * In replace mode: Put the old characters back, if any.
4657 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004658 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4659 if ((int)curwin->w_cursor.col > col)
4660 {
4661 if (stop_arrow() == FAIL)
4662 return;
4663 backspace_until_column(col);
4664 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004665
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004666 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4667 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004669 /* clear v:completed_item */
4670 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671}
4672
Bram Moolenaar472e8592016-10-15 17:06:47 +02004673/*
4674 * Insert the new text being completed.
4675 * "in_compl_func" is TRUE when called from complete_check().
4676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004678ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004680 dict_T *dict;
4681
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004682 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004683 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4684 compl_used_match = FALSE;
4685 else
4686 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004687
4688 /* Set completed item. */
4689 /* { word, abbr, menu, kind, info } */
4690 dict = dict_alloc();
4691 if (dict != NULL)
4692 {
4693 dict_add_nr_str(dict, "word", 0L,
4694 EMPTY_IF_NULL(compl_shown_match->cp_str));
4695 dict_add_nr_str(dict, "abbr", 0L,
4696 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4697 dict_add_nr_str(dict, "menu", 0L,
4698 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4699 dict_add_nr_str(dict, "kind", 0L,
4700 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4701 dict_add_nr_str(dict, "info", 0L,
4702 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4703 }
4704 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004705 if (!in_compl_func)
4706 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707}
4708
4709/*
4710 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004711 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4712 * get more completions. If it is FALSE, then we just do nothing when there
4713 * are no more completions in a given direction. The latter case is used when
4714 * we are still in the middle of finding completions, to allow browsing
4715 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 * Return the total number of matches, or -1 if still unknown -- webb.
4717 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004718 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4719 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 *
4721 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004722 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4723 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 */
4725 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004726ins_compl_next(
4727 int allow_get_expansion,
4728 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004729 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004730 int insert_match, /* Insert the newly selected match */
4731 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
4733 int num_matches = -1;
4734 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004735 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004736 compl_T *found_compl = NULL;
4737 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004738 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004739 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004741 /* When user complete function return -1 for findstart which is next
4742 * time of 'always', compl_shown_match become NULL. */
4743 if (compl_shown_match == NULL)
4744 return -1;
4745
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004746 if (compl_leader != NULL
4747 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004749 /* Set "compl_shown_match" to the actually shown match, it may differ
4750 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004751 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004752 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004753 && compl_shown_match->cp_next != NULL
4754 && compl_shown_match->cp_next != compl_first_match)
4755 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004756
4757 /* If we didn't find it searching forward, and compl_shows_dir is
4758 * backward, find the last match. */
4759 if (compl_shows_dir == BACKWARD
4760 && !ins_compl_equal(compl_shown_match,
4761 compl_leader, (int)STRLEN(compl_leader))
4762 && (compl_shown_match->cp_next == NULL
4763 || compl_shown_match->cp_next == compl_first_match))
4764 {
4765 while (!ins_compl_equal(compl_shown_match,
4766 compl_leader, (int)STRLEN(compl_leader))
4767 && compl_shown_match->cp_prev != NULL
4768 && compl_shown_match->cp_prev != compl_first_match)
4769 compl_shown_match = compl_shown_match->cp_prev;
4770 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004771 }
4772
4773 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004774 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 /* Delete old text to be replaced */
4776 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004777
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004778 /* When finding the longest common text we stick at the original text,
4779 * don't let CTRL-N or CTRL-P move to the first match. */
4780 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4781
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004782 /* When restarting the search don't insert the first match either. */
4783 if (compl_restarting)
4784 {
4785 advance = FALSE;
4786 compl_restarting = FALSE;
4787 }
4788
Bram Moolenaare3226be2005-12-18 22:10:00 +00004789 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4790 * around. */
4791 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004793 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004795 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004796 found_end = (compl_first_match != NULL
4797 && (compl_shown_match->cp_next == compl_first_match
4798 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004799 }
4800 else if (compl_shows_dir == BACKWARD
4801 && compl_shown_match->cp_prev != NULL)
4802 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004803 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004804 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004805 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004808 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004809 if (!allow_get_expansion)
4810 {
4811 if (advance)
4812 {
4813 if (compl_shows_dir == BACKWARD)
4814 compl_pending -= todo + 1;
4815 else
4816 compl_pending += todo + 1;
4817 }
4818 return -1;
4819 }
4820
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004821 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004822 {
4823 if (compl_shows_dir == BACKWARD)
4824 --compl_pending;
4825 else
4826 ++compl_pending;
4827 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004828
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004829 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004830 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004831
4832 /* handle any pending completions */
4833 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004834 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004835 {
4836 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4837 {
4838 compl_shown_match = compl_shown_match->cp_next;
4839 --compl_pending;
4840 }
4841 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4842 {
4843 compl_shown_match = compl_shown_match->cp_prev;
4844 ++compl_pending;
4845 }
4846 else
4847 break;
4848 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004849 found_end = FALSE;
4850 }
4851 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4852 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004853 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004854 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004855 ++todo;
4856 else
4857 /* Remember a matching item. */
4858 found_compl = compl_shown_match;
4859
4860 /* Stop at the end of the list when we found a usable match. */
4861 if (found_end)
4862 {
4863 if (found_compl != NULL)
4864 {
4865 compl_shown_match = found_compl;
4866 break;
4867 }
4868 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004872 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004873 if (compl_no_insert && !started)
4874 {
4875 ins_bytes(compl_orig_text + ins_compl_len());
4876 compl_used_match = FALSE;
4877 }
4878 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004879 {
4880 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004881 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004882 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004883 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004884 }
4885 else
4886 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
4888 if (!allow_get_expansion)
4889 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004890 /* may undisplay the popup menu first */
4891 ins_compl_upd_pum();
4892
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004893 /* redraw to show the user what was inserted */
4894 update_screen(0);
4895
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004896 /* display the updated popup menu */
4897 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004898#ifdef FEAT_GUI
4899 if (gui.in_use)
4900 {
4901 /* Show the cursor after the match, not after the redrawn text. */
4902 setcursor();
4903 out_flush();
4904 gui_update_cursor(FALSE, FALSE);
4905 }
4906#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004907
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 /* Delete old text to be replaced, since we're still searching and
4909 * don't want to match ourselves! */
4910 ins_compl_delete();
4911 }
4912
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004913 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004914 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004915 if (compl_no_insert && !started)
4916 compl_enter_selects = TRUE;
4917 else
4918 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 /*
4921 * Show the file name for the match (if any)
4922 * Truncate the file name to avoid a wait for return.
4923 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004924 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
4926 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004927 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (i <= 0)
4929 i = 0;
4930 else
4931 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004932 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 msg(IObuff);
4934 redraw_cmdline = FALSE; /* don't overwrite! */
4935 }
4936
4937 return num_matches;
4938}
4939
4940/*
4941 * Call this while finding completions, to check whether the user has hit a key
4942 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004943 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004945 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004946 * "in_compl_func" is TRUE when called from complete_check(), don't set
4947 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 */
4949 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004950ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
4952 static int count = 0;
4953
4954 int c;
4955
4956 /* Don't check when reading keys from a script. That would break the test
4957 * scripts */
4958 if (using_script())
4959 return;
4960
4961 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004962 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 return;
4964 count = 0;
4965
Bram Moolenaara260a972006-06-23 19:36:29 +00004966 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4967 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (c != NUL)
4970 {
4971 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4972 {
4973 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004974 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004975 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02004976 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004978 else
4979 {
4980 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004981 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004982 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004983 if (c != K_IGNORE)
4984 {
4985 /* Don't interrupt completion when the character wasn't typed,
4986 * e.g., when doing @q to replay keys. */
4987 if (c != Ctrl_R && KeyTyped)
4988 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004989
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004990 vungetc(c);
4991 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004994 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00004995 {
4996 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4997
4998 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02004999 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005000 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005001}
5002
5003/*
5004 * Decide the direction of Insert mode complete from the key typed.
5005 * Returns BACKWARD or FORWARD.
5006 */
5007 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005008ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005009{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005010 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005011 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005012 return BACKWARD;
5013 return FORWARD;
5014}
5015
5016/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005017 * Return TRUE for keys that are used for completion only when the popup menu
5018 * is visible.
5019 */
5020 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005021ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005022{
5023 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005024 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5025 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005026}
5027
5028/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005029 * Decide the number of completions to move forward.
5030 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5031 */
5032 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005033ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005034{
5035 int h;
5036
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005037 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005038 {
5039 h = pum_get_height();
5040 if (h > 3)
5041 h -= 2; /* keep some context */
5042 return h;
5043 }
5044 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045}
5046
5047/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005048 * Return TRUE if completion with "c" should insert the match, FALSE if only
5049 * to change the currently selected completion.
5050 */
5051 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005052ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005053{
5054 switch (c)
5055 {
5056 case K_UP:
5057 case K_DOWN:
5058 case K_PAGEDOWN:
5059 case K_KPAGEDOWN:
5060 case K_S_DOWN:
5061 case K_PAGEUP:
5062 case K_KPAGEUP:
5063 case K_S_UP:
5064 return FALSE;
5065 }
5066 return TRUE;
5067}
5068
5069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 * Do Insert mode completion.
5071 * Called when character "c" was typed, which has a meaning for completion.
5072 * Returns OK if completion was done, FAIL if something failed (out of mem).
5073 */
5074 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005075ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005077 char_u *line;
5078 int startcol = 0; /* column where searched text starts */
5079 colnr_T curs_col; /* cursor column */
5080 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005081 int save_w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005082 int insert_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
Bram Moolenaare3226be2005-12-18 22:10:00 +00005084 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005085 insert_match = ins_compl_use_match(c);
5086
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005087 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
5089 /* First time we hit ^N or ^P (in a row, I mean) */
5090
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 did_ai = FALSE;
5092#ifdef FEAT_SMARTINDENT
5093 did_si = FALSE;
5094 can_si = FALSE;
5095 can_si_back = FALSE;
5096#endif
5097 if (stop_arrow() == FAIL)
5098 return FAIL;
5099
5100 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005101 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005102 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005104 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005105 * "compl_startpos" to the cursor as a pattern to add a new word
5106 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005107 * "compl_startpos" is not in the same line as the cursor then fix it
5108 * (the line has been split because it was longer than 'tw'). if SOL
5109 * is set then skip the previous pattern, a word at the beginning of
5110 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005111 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5112 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
5114 /*
5115 * it is a continued search
5116 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005117 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5119 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5120 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005121 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005123 /* line (probably) wrapped, set compl_startpos to the
5124 * first non_blank in the line, if it is not a wordchar
5125 * include it to get a better pattern, but then we don't
5126 * want the "\\<" prefix, check it bellow */
5127 compl_col = (colnr_T)(skipwhite(line) - line);
5128 compl_startpos.col = compl_col;
5129 compl_startpos.lnum = curwin->w_cursor.lnum;
5130 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
5132 else
5133 {
5134 /* S_IPOS was set when we inserted a word that was at the
5135 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005136 * mode but first we need to redefine compl_startpos */
5137 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005139 compl_cont_status |= CONT_SOL;
5140 compl_startpos.col = (colnr_T)(skipwhite(
5141 line + compl_length
5142 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005144 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005146 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005147 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005148 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005150 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 compl_cont_status &= ~CONT_SOL;
5153 compl_length = (IOSIZE - MIN_SPACE);
5154 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005156 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5157 if (compl_length < 1)
5158 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005160 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005161 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005163 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005166 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005168 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005170 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005172 compl_cont_status = 0;
5173 compl_cont_status |= CONT_N_ADDS;
5174 compl_startpos = curwin->w_cursor;
5175 startcol = (int)curs_col;
5176 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178
5179 /* Work out completion pattern and original text -- webb */
5180 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5181 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005182 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5184 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005185 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005187 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005189 compl_col += ++startcol;
5190 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 }
5192 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005193 compl_pattern = str_foldcase(line + compl_col,
5194 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005196 compl_pattern = vim_strnsave(line + compl_col,
5197 compl_length);
5198 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 return FAIL;
5200 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005201 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
5203 char_u *prefix = (char_u *)"\\<";
5204
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005205 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005206 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005207 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005208 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005210 if (!vim_iswordp(line + compl_col)
5211 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 && (
5213#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005214 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005216 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217#endif
5218 )))
5219 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005220 STRCPY((char *)compl_pattern, prefix);
5221 (void)quote_meta(compl_pattern + STRLEN(prefix),
5222 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005224 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005226 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229#endif
5230 )
5231 {
5232 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005233 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5234 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236 compl_col += curs_col;
5237 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
5239 else
5240 {
5241#ifdef FEAT_MBYTE
5242 /* Search the point of change class of multibyte character
5243 * or not a word single byte character backward. */
5244 if (has_mbyte)
5245 {
5246 int base_class;
5247 int head_off;
5248
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005249 startcol -= (*mb_head_off)(line, line + startcol);
5250 base_class = mb_get_class(line + startcol);
5251 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005253 head_off = (*mb_head_off)(line, line + startcol);
5254 if (base_class != mb_get_class(line + startcol
5255 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
5259 }
5260 else
5261#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005262 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005264 compl_col += ++startcol;
5265 compl_length = (int)curs_col - startcol;
5266 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 {
5268 /* Only match word with at least two chars -- webb
5269 * there's no need to call quote_meta,
5270 * alloc(7) is enough -- Acevedo
5271 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005272 compl_pattern = alloc(7);
5273 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005275 STRCPY((char *)compl_pattern, "\\<");
5276 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5277 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279 else
5280 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005282 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005283 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005285 STRCPY((char *)compl_pattern, "\\<");
5286 (void)quote_meta(compl_pattern + 2, line + compl_col,
5287 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
5289 }
5290 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005291 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005293 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005294 compl_length = (int)curs_col - (int)compl_col;
5295 if (compl_length < 0) /* cursor in indent: empty pattern */
5296 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005298 compl_pattern = str_foldcase(line + compl_col, compl_length,
5299 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5302 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 return FAIL;
5304 }
5305 else if (ctrl_x_mode == CTRL_X_FILES)
5306 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005307 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005308 if (startcol > 0)
5309 {
5310 char_u *p = line + startcol;
5311
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005312 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005313 while (p > line && vim_isfilec(PTR2CHAR(p)))
5314 mb_ptr_back(line, p);
5315 if (p == line && vim_isfilec(PTR2CHAR(p)))
5316 startcol = 0;
5317 else
5318 startcol = (int)(p - line) + 1;
5319 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005320
Bram Moolenaar0300e462013-09-08 16:03:45 +02005321 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005322 compl_length = (int)curs_col - startcol;
5323 compl_pattern = addstar(line + compl_col, compl_length,
5324 EXPAND_FILES);
5325 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 return FAIL;
5327 }
5328 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5329 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005330 compl_pattern = vim_strnsave(line, curs_col);
5331 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005333 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005334 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5336 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005337 /* No completion possible, use an empty pattern to get a
5338 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005339 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005340 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005341 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5342 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005344 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005345 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005346#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005347 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005348 * Call user defined function 'completefunc' with "a:findstart"
5349 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005350 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005351 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005352 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005353 char_u *funcname;
5354 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005355 win_T *curwin_save;
5356 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005357
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005358 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005359 * string */
5360 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5361 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5362 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005363 {
5364 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5365 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005366 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005367 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005368
5369 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005370 args[1] = NULL;
5371 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005372 curwin_save = curwin;
5373 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005374 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005375 if (curwin_save != curwin || curbuf_save != curbuf)
5376 {
5377 EMSG(_(e_complwin));
5378 return FAIL;
5379 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005380 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005381 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005382 if (!equalpos(curwin->w_cursor, pos))
5383 {
5384 EMSG(_(e_compldel));
5385 return FAIL;
5386 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005387
Bram Moolenaar6110a002012-01-26 18:58:38 +01005388 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005389 * cancel the complete without an error.
5390 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005391 if (col == -2)
5392 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005393 if (col == -3)
5394 {
5395 ctrl_x_mode = 0;
5396 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005397 if (!shortmess(SHM_COMPLETIONMENU))
5398 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005399 return FAIL;
5400 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005401
Bram Moolenaar82139082011-09-14 16:52:09 +02005402 /*
5403 * Reset extended parameters of completion, when start new
5404 * completion.
5405 */
5406 compl_opt_refresh_always = FALSE;
5407
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005408 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005409 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005410 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005411 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005412 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005413
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005414 /* Setup variables for completion. Need to obtain "line" again,
5415 * it may have become invalid. */
5416 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005417 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005418 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5419 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005420#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 return FAIL;
5422 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005423 else if (ctrl_x_mode == CTRL_X_SPELL)
5424 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005425#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005426 if (spell_bad_len > 0)
5427 compl_col = curs_col - spell_bad_len;
5428 else
5429 compl_col = spell_word_start(startcol);
5430 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005431 {
5432 compl_length = 0;
5433 compl_col = curs_col;
5434 }
5435 else
5436 {
5437 spell_expand_check_cap(compl_col);
5438 compl_length = (int)curs_col - compl_col;
5439 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005440 /* Need to obtain "line" again, it may have become invalid. */
5441 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005442 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5443 if (compl_pattern == NULL)
5444#endif
5445 return FAIL;
5446 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005447 else
5448 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005449 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005450 return FAIL;
5451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005453 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {
5455 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005456 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
5458 /* Insert a new line, keep indentation but ignore 'comments' */
5459#ifdef FEAT_COMMENTS
5460 char_u *old = curbuf->b_p_com;
5461
5462 curbuf->b_p_com = (char_u *)"";
5463#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005464 compl_startpos.lnum = curwin->w_cursor.lnum;
5465 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 ins_eol('\r');
5467#ifdef FEAT_COMMENTS
5468 curbuf->b_p_com = old;
5469#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005470 compl_length = 0;
5471 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
5473 }
5474 else
5475 {
5476 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005477 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 }
5479
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005480 if (compl_cont_status & CONT_LOCAL)
5481 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 else
5483 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5484
Bram Moolenaar62951b12011-09-21 18:23:05 +02005485 /* If any of the original typed text has been changed we need to fix
5486 * the redo buffer. */
5487 ins_compl_fixRedoBufForLeader(NULL);
5488
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005489 /* Always add completion for the original text. */
5490 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005491 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5492 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005493 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005495 vim_free(compl_pattern);
5496 compl_pattern = NULL;
5497 vim_free(compl_orig_text);
5498 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 return FAIL;
5500 }
5501
5502 /* showmode might reset the internal line pointers, so it must
5503 * be called before line = ml_get(), or when this address is no
5504 * longer needed. -- Acevedo.
5505 */
5506 edit_submode_extra = (char_u *)_("-- Searching...");
5507 edit_submode_highl = HLF_COUNT;
5508 showmode();
5509 edit_submode_extra = NULL;
5510 out_flush();
5511 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005512 else if (insert_match && stop_arrow() == FAIL)
5513 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005515 compl_shown_match = compl_curr_match;
5516 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
5518 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005519 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005521 save_w_wrow = curwin->w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005522 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005524 /* may undisplay the popup menu */
5525 ins_compl_upd_pum();
5526
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005527 if (n > 1) /* all matches have been found */
5528 compl_matches = n;
5529 compl_curr_match = compl_shown_match;
5530 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
Bram Moolenaard68071d2006-05-02 22:08:30 +00005532 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5533 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 if (got_int && !global_busy)
5535 {
5536 (void)vgetc();
5537 got_int = FALSE;
5538 }
5539
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005540 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005541 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005543 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5544 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5546 edit_submode_highl = HLF_E;
5547 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5548 * because we couldn't expand anything at first place, but if we used
5549 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5550 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005551 if ( compl_length > 1
5552 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 || (ctrl_x_mode != 0
5554 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5555 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005556 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558
Bram Moolenaar572cb562005-08-05 21:35:02 +00005559 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005560 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005562 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
5564 if (edit_submode_extra == NULL)
5565 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005566 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 {
5568 edit_submode_extra = (char_u *)_("Back at original");
5569 edit_submode_highl = HLF_W;
5570 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005571 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
5573 edit_submode_extra = (char_u *)_("Word from other line");
5574 edit_submode_highl = HLF_COUNT;
5575 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005576 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
5578 edit_submode_extra = (char_u *)_("The only match");
5579 edit_submode_highl = HLF_COUNT;
5580 }
5581 else
5582 {
5583 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005584 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005586 int number = 0;
5587 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005589 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
5591 /* search backwards for the first valid (!= -1) number.
5592 * This should normally succeed already at the first loop
5593 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005594 for (match = compl_curr_match->cp_prev; match != NULL
5595 && match != compl_first_match;
5596 match = match->cp_prev)
5597 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005599 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 break;
5601 }
5602 if (match != NULL)
5603 /* go up and assign all numbers which are not assigned
5604 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005605 for (match = match->cp_next;
5606 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005607 match = match->cp_next)
5608 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 }
5610 else /* BACKWARD */
5611 {
5612 /* search forwards (upwards) for the first valid (!= -1)
5613 * number. This should normally succeed already at the
5614 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005615 for (match = compl_curr_match->cp_next; match != NULL
5616 && match != compl_first_match;
5617 match = match->cp_next)
5618 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005620 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 break;
5622 }
5623 if (match != NULL)
5624 /* go down and assign all numbers which are not
5625 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005626 for (match = match->cp_prev; match
5627 && match->cp_number == -1;
5628 match = match->cp_prev)
5629 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
5631 }
5632
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005633 /* The match should always have a sequence number now, this is
5634 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005635 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005637 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5638 * Translations may need more than twice that. */
5639 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005641 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005642 vim_snprintf((char *)match_ref, sizeof(match_ref),
5643 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005644 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005646 vim_snprintf((char *)match_ref, sizeof(match_ref),
5647 _("match %d"),
5648 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 edit_submode_extra = match_ref;
5650 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005651 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 curs_columns(FALSE);
5653 }
5654 }
5655 }
5656
5657 /* Show a message about what (completion) mode we're in. */
5658 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005659 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005661 if (edit_submode_extra != NULL)
5662 {
5663 if (!p_smd)
5664 msg_attr(edit_submode_extra,
5665 edit_submode_highl < HLF_COUNT
5666 ? hl_attr(edit_submode_highl) : 0);
5667 }
5668 else
5669 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
Bram Moolenaard68071d2006-05-02 22:08:30 +00005672 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005673 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005674 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005675 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005676 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005677 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005678 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005679
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 return OK;
5681}
5682
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005683 static void
5684show_pum(int save_w_wrow)
5685{
5686 /* RedrawingDisabled may be set when invoked through complete(). */
5687 int n = RedrawingDisabled;
5688
5689 RedrawingDisabled = 0;
5690
5691 /* If the cursor moved we need to remove the pum first. */
5692 setcursor();
5693 if (save_w_wrow != curwin->w_wrow)
5694 ins_compl_del_pum();
5695
5696 ins_compl_show_pum();
5697 setcursor();
5698 RedrawingDisabled = n;
5699}
5700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701/*
5702 * Looks in the first "len" chars. of "src" for search-metachars.
5703 * If dest is not NULL the chars. are copied there quoting (with
5704 * a backslash) the metachars, and dest would be NUL terminated.
5705 * Returns the length (needed) of dest
5706 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005707 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005708quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005710 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005712 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
5714 switch (*src)
5715 {
5716 case '.':
5717 case '*':
5718 case '[':
5719 if (ctrl_x_mode == CTRL_X_DICTIONARY
5720 || ctrl_x_mode == CTRL_X_THESAURUS)
5721 break;
5722 case '~':
5723 if (!p_magic) /* quote these only if magic is set */
5724 break;
5725 case '\\':
5726 if (ctrl_x_mode == CTRL_X_DICTIONARY
5727 || ctrl_x_mode == CTRL_X_THESAURUS)
5728 break;
5729 case '^': /* currently it's not needed. */
5730 case '$':
5731 m++;
5732 if (dest != NULL)
5733 *dest++ = '\\';
5734 break;
5735 }
5736 if (dest != NULL)
5737 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005738# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 /* Copy remaining bytes of a multibyte character. */
5740 if (has_mbyte)
5741 {
5742 int i, mb_len;
5743
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005744 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 if (mb_len > 0 && len >= mb_len)
5746 for (i = 0; i < mb_len; ++i)
5747 {
5748 --len;
5749 ++src;
5750 if (dest != NULL)
5751 *dest++ = *src;
5752 }
5753 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 }
5756 if (dest != NULL)
5757 *dest = NUL;
5758
5759 return m;
5760}
5761#endif /* FEAT_INS_EXPAND */
5762
5763/*
5764 * Next character is interpreted literally.
5765 * A one, two or three digit decimal number is interpreted as its byte value.
5766 * If one or two digits are entered, the next character is given to vungetc().
5767 * For Unicode a character > 255 may be returned.
5768 */
5769 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005770get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
5772 int cc;
5773 int nc;
5774 int i;
5775 int hex = FALSE;
5776 int octal = FALSE;
5777#ifdef FEAT_MBYTE
5778 int unicode = 0;
5779#endif
5780
5781 if (got_int)
5782 return Ctrl_C;
5783
5784#ifdef FEAT_GUI
5785 /*
5786 * In GUI there is no point inserting the internal code for a special key.
5787 * It is more useful to insert the string "<KEY>" instead. This would
5788 * probably be useful in a text window too, but it would not be
5789 * vi-compatible (maybe there should be an option for it?) -- webb
5790 */
5791 if (gui.in_use)
5792 ++allow_keys;
5793#endif
5794#ifdef USE_ON_FLY_SCROLL
5795 dont_scroll = TRUE; /* disallow scrolling here */
5796#endif
5797 ++no_mapping; /* don't map the next key hits */
5798 cc = 0;
5799 i = 0;
5800 for (;;)
5801 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005802 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803#ifdef FEAT_CMDL_INFO
5804 if (!(State & CMDLINE)
5805# ifdef FEAT_MBYTE
5806 && MB_BYTE2LEN_CHECK(nc) == 1
5807# endif
5808 )
5809 add_to_showcmd(nc);
5810#endif
5811 if (nc == 'x' || nc == 'X')
5812 hex = TRUE;
5813 else if (nc == 'o' || nc == 'O')
5814 octal = TRUE;
5815#ifdef FEAT_MBYTE
5816 else if (nc == 'u' || nc == 'U')
5817 unicode = nc;
5818#endif
5819 else
5820 {
5821 if (hex
5822#ifdef FEAT_MBYTE
5823 || unicode != 0
5824#endif
5825 )
5826 {
5827 if (!vim_isxdigit(nc))
5828 break;
5829 cc = cc * 16 + hex2nr(nc);
5830 }
5831 else if (octal)
5832 {
5833 if (nc < '0' || nc > '7')
5834 break;
5835 cc = cc * 8 + nc - '0';
5836 }
5837 else
5838 {
5839 if (!VIM_ISDIGIT(nc))
5840 break;
5841 cc = cc * 10 + nc - '0';
5842 }
5843
5844 ++i;
5845 }
5846
5847 if (cc > 255
5848#ifdef FEAT_MBYTE
5849 && unicode == 0
5850#endif
5851 )
5852 cc = 255; /* limit range to 0-255 */
5853 nc = 0;
5854
5855 if (hex) /* hex: up to two chars */
5856 {
5857 if (i >= 2)
5858 break;
5859 }
5860#ifdef FEAT_MBYTE
5861 else if (unicode) /* Unicode: up to four or eight chars */
5862 {
5863 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5864 break;
5865 }
5866#endif
5867 else if (i >= 3) /* decimal or octal: up to three chars */
5868 break;
5869 }
5870 if (i == 0) /* no number entered */
5871 {
5872 if (nc == K_ZERO) /* NUL is stored as NL */
5873 {
5874 cc = '\n';
5875 nc = 0;
5876 }
5877 else
5878 {
5879 cc = nc;
5880 nc = 0;
5881 }
5882 }
5883
5884 if (cc == 0) /* NUL is stored as NL */
5885 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005886#ifdef FEAT_MBYTE
5887 if (enc_dbcs && (cc & 0xff) == 0)
5888 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5889 second byte will cause trouble! */
5890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
5892 --no_mapping;
5893#ifdef FEAT_GUI
5894 if (gui.in_use)
5895 --allow_keys;
5896#endif
5897 if (nc)
5898 vungetc(nc);
5899 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5900 return cc;
5901}
5902
5903/*
5904 * Insert character, taking care of special keys and mod_mask
5905 */
5906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005907insert_special(
5908 int c,
5909 int allow_modmask,
5910 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911{
5912 char_u *p;
5913 int len;
5914
5915 /*
5916 * Special function key, translate into "<Key>". Up to the last '>' is
5917 * inserted with ins_str(), so as not to replace characters in replace
5918 * mode.
5919 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5920 * unless 'allow_modmask' is TRUE.
5921 */
5922#ifdef MACOS
5923 /* Command-key never produces a normal key */
5924 if (mod_mask & MOD_MASK_CMD)
5925 allow_modmask = TRUE;
5926#endif
5927 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5928 {
5929 p = get_special_key_name(c, mod_mask);
5930 len = (int)STRLEN(p);
5931 c = p[len - 1];
5932 if (len > 2)
5933 {
5934 if (stop_arrow() == FAIL)
5935 return;
5936 p[len - 1] = NUL;
5937 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005938 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 ctrlv = FALSE;
5940 }
5941 }
5942 if (stop_arrow() == OK)
5943 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5944}
5945
5946/*
5947 * Special characters in this context are those that need processing other
5948 * than the simple insertion that can be performed here. This includes ESC
5949 * which terminates the insert, and CR/NL which need special processing to
5950 * open up a new line. This routine tries to optimize insertions performed by
5951 * the "redo", "undo" or "put" commands, so it needs to know when it should
5952 * stop and defer processing to the "normal" mechanism.
5953 * '0' and '^' are special, because they can be followed by CTRL-D.
5954 */
5955#ifdef EBCDIC
5956# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5957#else
5958# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5959#endif
5960
5961#ifdef FEAT_MBYTE
5962# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5963#else
5964# define WHITECHAR(cc) vim_iswhite(cc)
5965#endif
5966
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005967/*
5968 * "flags": INSCHAR_FORMAT - force formatting
5969 * INSCHAR_CTRLV - char typed just after CTRL-V
5970 * INSCHAR_NO_FEX - don't use 'formatexpr'
5971 *
5972 * NOTE: passes the flags value straight through to internal_format() which,
5973 * beside INSCHAR_FORMAT (above), is also looking for these:
5974 * INSCHAR_DO_COM - format comments
5975 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005978insertchar(
5979 int c, /* character to insert or NUL */
5980 int flags, /* INSCHAR_FORMAT, etc. */
5981 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 int textwidth;
5984#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005988 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005990 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992
5993 /*
5994 * Try to break the line in two or more pieces when:
5995 * - Always do this if we have been called to do formatting only.
5996 * - Always do this when 'formatoptions' has the 'a' flag and the line
5997 * ends in white space.
5998 * - Otherwise:
5999 * - Don't do this if inserting a blank
6000 * - Don't do this if an existing character is being replaced, unless
6001 * we're in VREPLACE mode.
6002 * - Do this if the cursor is not on the line where insert started
6003 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6004 * before the insert.
6005 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6006 * before 'textwidth'
6007 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006008 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006009 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 || (!vim_iswhite(c)
6011 && !((State & REPLACE_FLAG)
6012#ifdef FEAT_VREPLACE
6013 && !(State & VREPLACE_FLAG)
6014#endif
6015 && *ml_get_cursor() != NUL)
6016 && (curwin->w_cursor.lnum != Insstart.lnum
6017 || ((!has_format_option(FO_INS_LONG)
6018 || Insstart_textlen <= (colnr_T)textwidth)
6019 && (!fo_ins_blank
6020 || Insstart_blank_vcol <= (colnr_T)textwidth
6021 ))))))
6022 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006023 /* Format with 'formatexpr' when it's set. Use internal formatting
6024 * when 'formatexpr' isn't set or it returns non-zero. */
6025#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006026 int do_internal = TRUE;
6027 colnr_T virtcol = get_nolist_virtcol()
6028 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006029
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006030 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6031 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006032 {
6033 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6034 /* It may be required to save for undo again, e.g. when setline()
6035 * was called. */
6036 ins_need_undo = TRUE;
6037 }
6038 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006040 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006042
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 if (c == NUL) /* only formatting was wanted */
6044 return;
6045
6046#ifdef FEAT_COMMENTS
6047 /* Check whether this character should end a comment. */
6048 if (did_ai && (int)c == end_comment_pending)
6049 {
6050 char_u *line;
6051 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6052 int middle_len, end_len;
6053 int i;
6054
6055 /*
6056 * Need to remove existing (middle) comment leader and insert end
6057 * comment leader. First, check what comment leader we can find.
6058 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006059 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6061 {
6062 /* Skip middle-comment string */
6063 while (*p && p[-1] != ':') /* find end of middle flags */
6064 ++p;
6065 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6066 /* Don't count trailing white space for middle_len */
6067 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6068 --middle_len;
6069
6070 /* Find the end-comment string */
6071 while (*p && p[-1] != ':') /* find end of end flags */
6072 ++p;
6073 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6074
6075 /* Skip white space before the cursor */
6076 i = curwin->w_cursor.col;
6077 while (--i >= 0 && vim_iswhite(line[i]))
6078 ;
6079 i++;
6080
6081 /* Skip to before the middle leader */
6082 i -= middle_len;
6083
6084 /* Check some expected things before we go on */
6085 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6086 {
6087 /* Backspace over all the stuff we want to replace */
6088 backspace_until_column(i);
6089
6090 /*
6091 * Insert the end-comment string, except for the last
6092 * character, which will get inserted as normal later.
6093 */
6094 ins_bytes_len(lead_end, end_len - 1);
6095 }
6096 }
6097 }
6098 end_comment_pending = NUL;
6099#endif
6100
6101 did_ai = FALSE;
6102#ifdef FEAT_SMARTINDENT
6103 did_si = FALSE;
6104 can_si = FALSE;
6105 can_si_back = FALSE;
6106#endif
6107
6108 /*
6109 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6110 * This speeds up normal text input considerably.
6111 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6112 * need to re-indent at a ':', or any other character (but not what
6113 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006114 * Don't do this when there an InsertCharPre autocommand is defined,
6115 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 */
6117#ifdef USE_ON_FLY_SCROLL
6118 dont_scroll = FALSE; /* allow scrolling here */
6119#endif
6120
6121 if ( !ISSPECIAL(c)
6122#ifdef FEAT_MBYTE
6123 && (!has_mbyte || (*mb_char2len)(c) == 1)
6124#endif
6125 && vpeekc() != NUL
6126 && !(State & REPLACE_FLAG)
6127#ifdef FEAT_CINDENT
6128 && !cindent_on()
6129#endif
6130#ifdef FEAT_RIGHTLEFT
6131 && !p_ri
6132#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006133#ifdef FEAT_AUTOCMD
6134 && !has_insertcharpre()
6135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 )
6137 {
6138#define INPUT_BUFLEN 100
6139 char_u buf[INPUT_BUFLEN + 1];
6140 int i;
6141 colnr_T virtcol = 0;
6142
6143 buf[0] = c;
6144 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006145 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 virtcol = get_nolist_virtcol();
6147 /*
6148 * Stop the string when:
6149 * - no more chars available
6150 * - finding a special character (command key)
6151 * - buffer is full
6152 * - running into the 'textwidth' boundary
6153 * - need to check for abbreviation: A non-word char after a word-char
6154 */
6155 while ( (c = vpeekc()) != NUL
6156 && !ISSPECIAL(c)
6157#ifdef FEAT_MBYTE
6158 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6159#endif
6160 && i < INPUT_BUFLEN
6161 && (textwidth == 0
6162 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6163 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6164 {
6165#ifdef FEAT_RIGHTLEFT
6166 c = vgetc();
6167 if (p_hkmap && KeyTyped)
6168 c = hkmap(c); /* Hebrew mode mapping */
6169# ifdef FEAT_FKMAP
6170 if (p_fkmap && KeyTyped)
6171 c = fkmap(c); /* Farsi mode mapping */
6172# endif
6173 buf[i++] = c;
6174#else
6175 buf[i++] = vgetc();
6176#endif
6177 }
6178
6179#ifdef FEAT_DIGRAPHS
6180 do_digraph(-1); /* clear digraphs */
6181 do_digraph(buf[i-1]); /* may be the start of a digraph */
6182#endif
6183 buf[i] = NUL;
6184 ins_str(buf);
6185 if (flags & INSCHAR_CTRLV)
6186 {
6187 redo_literal(*buf);
6188 i = 1;
6189 }
6190 else
6191 i = 0;
6192 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006193 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 }
6195 else
6196 {
6197#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006198 int cc;
6199
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6201 {
6202 char_u buf[MB_MAXBYTES + 1];
6203
6204 (*mb_char2bytes)(c, buf);
6205 buf[cc] = NUL;
6206 ins_char_bytes(buf, cc);
6207 AppendCharToRedobuff(c);
6208 }
6209 else
6210#endif
6211 {
6212 ins_char(c);
6213 if (flags & INSCHAR_CTRLV)
6214 redo_literal(c);
6215 else
6216 AppendCharToRedobuff(c);
6217 }
6218 }
6219}
6220
6221/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006222 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006223 *
6224 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6225 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006226 */
6227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006228internal_format(
6229 int textwidth,
6230 int second_indent,
6231 int flags,
6232 int format_only,
6233 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006234{
6235 int cc;
6236 int save_char = NUL;
6237 int haveto_redraw = FALSE;
6238 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6239#ifdef FEAT_MBYTE
6240 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6241#endif
6242 int fo_white_par = has_format_option(FO_WHITE_PAR);
6243 int first_line = TRUE;
6244#ifdef FEAT_COMMENTS
6245 colnr_T leader_len;
6246 int no_leader = FALSE;
6247 int do_comments = (flags & INSCHAR_DO_COM);
6248#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006249#ifdef FEAT_LINEBREAK
6250 int has_lbr = curwin->w_p_lbr;
6251
6252 /* make sure win_lbr_chartabsize() counts correctly */
6253 curwin->w_p_lbr = FALSE;
6254#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006255
6256 /*
6257 * When 'ai' is off we don't want a space under the cursor to be
6258 * deleted. Replace it with an 'x' temporarily.
6259 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006260 if (!curbuf->b_p_ai
6261#ifdef FEAT_VREPLACE
6262 && !(State & VREPLACE_FLAG)
6263#endif
6264 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006265 {
6266 cc = gchar_cursor();
6267 if (vim_iswhite(cc))
6268 {
6269 save_char = cc;
6270 pchar_cursor('x');
6271 }
6272 }
6273
6274 /*
6275 * Repeat breaking lines, until the current line is not too long.
6276 */
6277 while (!got_int)
6278 {
6279 int startcol; /* Cursor column at entry */
6280 int wantcol; /* column at textwidth border */
6281 int foundcol; /* column for start of spaces */
6282 int end_foundcol = 0; /* column for start of word */
6283 colnr_T len;
6284 colnr_T virtcol;
6285#ifdef FEAT_VREPLACE
6286 int orig_col = 0;
6287 char_u *saved_text = NULL;
6288#endif
6289 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006290 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006291
Bram Moolenaar97b98102009-11-17 16:41:01 +00006292 virtcol = get_nolist_virtcol()
6293 + char2cells(c != NUL ? c : gchar_cursor());
6294 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006295 break;
6296
6297#ifdef FEAT_COMMENTS
6298 if (no_leader)
6299 do_comments = FALSE;
6300 else if (!(flags & INSCHAR_FORMAT)
6301 && has_format_option(FO_WRAP_COMS))
6302 do_comments = TRUE;
6303
6304 /* Don't break until after the comment leader */
6305 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006306 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006307 else
6308 leader_len = 0;
6309
6310 /* If the line doesn't start with a comment leader, then don't
6311 * start one in a following broken line. Avoids that a %word
6312 * moved to the start of the next line causes all following lines
6313 * to start with %. */
6314 if (leader_len == 0)
6315 no_leader = TRUE;
6316#endif
6317 if (!(flags & INSCHAR_FORMAT)
6318#ifdef FEAT_COMMENTS
6319 && leader_len == 0
6320#endif
6321 && !has_format_option(FO_WRAP))
6322
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006323 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006324 if ((startcol = curwin->w_cursor.col) == 0)
6325 break;
6326
6327 /* find column of textwidth border */
6328 coladvance((colnr_T)textwidth);
6329 wantcol = curwin->w_cursor.col;
6330
Bram Moolenaar97b98102009-11-17 16:41:01 +00006331 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006332 foundcol = 0;
6333
6334 /*
6335 * Find position to break at.
6336 * Stop at first entered white when 'formatoptions' has 'v'
6337 */
6338 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006339 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006340 || curwin->w_cursor.lnum != Insstart.lnum
6341 || curwin->w_cursor.col >= Insstart.col)
6342 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006343 if (curwin->w_cursor.col == startcol && c != NUL)
6344 cc = c;
6345 else
6346 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006347 if (WHITECHAR(cc))
6348 {
6349 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006350 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006351
6352 /* find start of sequence of blanks */
6353 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6354 {
6355 dec_cursor();
6356 cc = gchar_cursor();
6357 }
6358 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6359 break; /* only spaces in front of text */
6360#ifdef FEAT_COMMENTS
6361 /* Don't break until after the comment leader */
6362 if (curwin->w_cursor.col < leader_len)
6363 break;
6364#endif
6365 if (has_format_option(FO_ONE_LETTER))
6366 {
6367 /* do not break after one-letter words */
6368 if (curwin->w_cursor.col == 0)
6369 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006370#ifdef FEAT_COMMENTS
6371 /* do not break "#a b" when 'tw' is 2 */
6372 if (curwin->w_cursor.col <= leader_len)
6373 break;
6374#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006375 col = curwin->w_cursor.col;
6376 dec_cursor();
6377 cc = gchar_cursor();
6378
6379 if (WHITECHAR(cc))
6380 continue; /* one-letter, continue */
6381 curwin->w_cursor.col = col;
6382 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006383
6384 inc_cursor();
6385
6386 end_foundcol = end_col + 1;
6387 foundcol = curwin->w_cursor.col;
6388 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006389 break;
6390 }
6391#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006392 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006393 {
6394 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006395 if (curwin->w_cursor.col != startcol)
6396 {
6397#ifdef FEAT_COMMENTS
6398 /* Don't break until after the comment leader */
6399 if (curwin->w_cursor.col < leader_len)
6400 break;
6401#endif
6402 col = curwin->w_cursor.col;
6403 inc_cursor();
6404 /* Don't change end_foundcol if already set. */
6405 if (foundcol != curwin->w_cursor.col)
6406 {
6407 foundcol = curwin->w_cursor.col;
6408 end_foundcol = foundcol;
6409 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6410 break;
6411 }
6412 curwin->w_cursor.col = col;
6413 }
6414
6415 if (curwin->w_cursor.col == 0)
6416 break;
6417
6418 col = curwin->w_cursor.col;
6419
6420 dec_cursor();
6421 cc = gchar_cursor();
6422
6423 if (WHITECHAR(cc))
6424 continue; /* break with space */
6425#ifdef FEAT_COMMENTS
6426 /* Don't break until after the comment leader */
6427 if (curwin->w_cursor.col < leader_len)
6428 break;
6429#endif
6430
6431 curwin->w_cursor.col = col;
6432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006433 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006434 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006435 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6436 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006437 }
6438#endif
6439 if (curwin->w_cursor.col == 0)
6440 break;
6441 dec_cursor();
6442 }
6443
6444 if (foundcol == 0) /* no spaces, cannot break line */
6445 {
6446 curwin->w_cursor.col = startcol;
6447 break;
6448 }
6449
6450 /* Going to break the line, remove any "$" now. */
6451 undisplay_dollar();
6452
6453 /*
6454 * Offset between cursor position and line break is used by replace
6455 * stack functions. VREPLACE does not use this, and backspaces
6456 * over the text instead.
6457 */
6458#ifdef FEAT_VREPLACE
6459 if (State & VREPLACE_FLAG)
6460 orig_col = startcol; /* Will start backspacing from here */
6461 else
6462#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006463 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006464
6465 /*
6466 * adjust startcol for spaces that will be deleted and
6467 * characters that will remain on top line
6468 */
6469 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006470 while ((cc = gchar_cursor(), WHITECHAR(cc))
6471 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006472 inc_cursor();
6473 startcol -= curwin->w_cursor.col;
6474 if (startcol < 0)
6475 startcol = 0;
6476
6477#ifdef FEAT_VREPLACE
6478 if (State & VREPLACE_FLAG)
6479 {
6480 /*
6481 * In VREPLACE mode, we will backspace over the text to be
6482 * wrapped, so save a copy now to put on the next line.
6483 */
6484 saved_text = vim_strsave(ml_get_cursor());
6485 curwin->w_cursor.col = orig_col;
6486 if (saved_text == NULL)
6487 break; /* Can't do it, out of memory */
6488 saved_text[startcol] = NUL;
6489
6490 /* Backspace over characters that will move to the next line */
6491 if (!fo_white_par)
6492 backspace_until_column(foundcol);
6493 }
6494 else
6495#endif
6496 {
6497 /* put cursor after pos. to break line */
6498 if (!fo_white_par)
6499 curwin->w_cursor.col = foundcol;
6500 }
6501
6502 /*
6503 * Split the line just before the margin.
6504 * Only insert/delete lines, but don't really redraw the window.
6505 */
6506 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6507 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6508#ifdef FEAT_COMMENTS
6509 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006510 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006511#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006512 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6513 if (!(flags & INSCHAR_COM_LIST))
6514 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006515
6516 replace_offset = 0;
6517 if (first_line)
6518 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006519 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006520 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006521 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006522 * This section is for auto-wrap of numeric lists. When not
6523 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6524 * flag will be set and open_line() will handle it (as seen
6525 * above). The code here (and in get_number_indent()) will
6526 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006527 */
6528 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006529 second_indent =
6530 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006531 if (second_indent >= 0)
6532 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006533#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006534 if (State & VREPLACE_FLAG)
6535 change_indent(INDENT_SET, second_indent,
6536 FALSE, NUL, TRUE);
6537 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006538#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006539#ifdef FEAT_COMMENTS
6540 if (leader_len > 0 && second_indent - leader_len > 0)
6541 {
6542 int i;
6543 int padding = second_indent - leader_len;
6544
6545 /* We started at the first_line of a numbered list
6546 * that has a comment. the open_line() function has
6547 * inserted the proper comment leader and positioned
6548 * the cursor at the end of the split line. Now we
6549 * add the additional whitespace needed after the
6550 * comment leader for the numbered list. */
6551 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006552 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006553 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006554 }
6555 else
6556 {
6557#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006558 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006559#ifdef FEAT_COMMENTS
6560 }
6561#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006562 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006563 }
6564 first_line = FALSE;
6565 }
6566
6567#ifdef FEAT_VREPLACE
6568 if (State & VREPLACE_FLAG)
6569 {
6570 /*
6571 * In VREPLACE mode we have backspaced over the text to be
6572 * moved, now we re-insert it into the new line.
6573 */
6574 ins_bytes(saved_text);
6575 vim_free(saved_text);
6576 }
6577 else
6578#endif
6579 {
6580 /*
6581 * Check if cursor is not past the NUL off the line, cindent
6582 * may have added or removed indent.
6583 */
6584 curwin->w_cursor.col += startcol;
6585 len = (colnr_T)STRLEN(ml_get_curline());
6586 if (curwin->w_cursor.col > len)
6587 curwin->w_cursor.col = len;
6588 }
6589
6590 haveto_redraw = TRUE;
6591#ifdef FEAT_CINDENT
6592 can_cindent = TRUE;
6593#endif
6594 /* moved the cursor, don't autoindent or cindent now */
6595 did_ai = FALSE;
6596#ifdef FEAT_SMARTINDENT
6597 did_si = FALSE;
6598 can_si = FALSE;
6599 can_si_back = FALSE;
6600#endif
6601 line_breakcheck();
6602 }
6603
6604 if (save_char != NUL) /* put back space after cursor */
6605 pchar_cursor(save_char);
6606
Bram Moolenaar0026d472014-09-09 16:32:39 +02006607#ifdef FEAT_LINEBREAK
6608 curwin->w_p_lbr = has_lbr;
6609#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006610 if (!format_only && haveto_redraw)
6611 {
6612 update_topline();
6613 redraw_curbuf_later(VALID);
6614 }
6615}
6616
6617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 * Called after inserting or deleting text: When 'formatoptions' includes the
6619 * 'a' flag format from the current line until the end of the paragraph.
6620 * Keep the cursor at the same position relative to the text.
6621 * The caller must have saved the cursor line for undo, following ones will be
6622 * saved here.
6623 */
6624 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006625auto_format(
6626 int trailblank, /* when TRUE also format with trailing blank */
6627 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
6629 pos_T pos;
6630 colnr_T len;
6631 char_u *old;
6632 char_u *new, *pnew;
6633 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006634 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
6636 if (!has_format_option(FO_AUTO))
6637 return;
6638
6639 pos = curwin->w_cursor;
6640 old = ml_get_curline();
6641
6642 /* may remove added space */
6643 check_auto_format(FALSE);
6644
6645 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6646 * user might insert normal text next. Also skip formatting when "1" is
6647 * in 'formatoptions' and there is a single character before the cursor.
6648 * Otherwise the line would be broken and when typing another non-white
6649 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006650 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 if (*old != NUL && !trailblank && wasatend)
6652 {
6653 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006654 cc = gchar_cursor();
6655 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6656 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006658 cc = gchar_cursor();
6659 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 {
6661 curwin->w_cursor = pos;
6662 return;
6663 }
6664 curwin->w_cursor = pos;
6665 }
6666
6667#ifdef FEAT_COMMENTS
6668 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6669 * comments. */
6670 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006671 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 return;
6673#endif
6674
6675 /*
6676 * May start formatting in a previous line, so that after "x" a word is
6677 * moved to the previous line if it fits there now. Only when this is not
6678 * the start of a paragraph.
6679 */
6680 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6681 {
6682 --curwin->w_cursor.lnum;
6683 if (u_save_cursor() == FAIL)
6684 return;
6685 }
6686
6687 /*
6688 * Do the formatting and restore the cursor position. "saved_cursor" will
6689 * be adjusted for the text formatting.
6690 */
6691 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006692 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 curwin->w_cursor = saved_cursor;
6694 saved_cursor.lnum = 0;
6695
6696 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6697 {
6698 /* "cannot happen" */
6699 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6700 coladvance((colnr_T)MAXCOL);
6701 }
6702 else
6703 check_cursor_col();
6704
6705 /* Insert mode: If the cursor is now after the end of the line while it
6706 * previously wasn't, the line was broken. Because of the rule above we
6707 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6708 * formatted. */
6709 if (!wasatend && has_format_option(FO_WHITE_PAR))
6710 {
6711 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006712 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 if (curwin->w_cursor.col == len)
6714 {
6715 pnew = vim_strnsave(new, len + 2);
6716 pnew[len] = ' ';
6717 pnew[len + 1] = NUL;
6718 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6719 /* remove the space later */
6720 did_add_space = TRUE;
6721 }
6722 else
6723 /* may remove added space */
6724 check_auto_format(FALSE);
6725 }
6726
6727 check_cursor();
6728}
6729
6730/*
6731 * When an extra space was added to continue a paragraph for auto-formatting,
6732 * delete it now. The space must be under the cursor, just after the insert
6733 * position.
6734 */
6735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006736check_auto_format(
6737 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006740 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741
6742 if (did_add_space)
6743 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006744 cc = gchar_cursor();
6745 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 /* Somehow the space was removed already. */
6747 did_add_space = FALSE;
6748 else
6749 {
6750 if (!end_insert)
6751 {
6752 inc_cursor();
6753 c = gchar_cursor();
6754 dec_cursor();
6755 }
6756 if (c != NUL)
6757 {
6758 /* The space is no longer at the end of the line, delete it. */
6759 del_char(FALSE);
6760 did_add_space = FALSE;
6761 }
6762 }
6763 }
6764}
6765
6766/*
6767 * Find out textwidth to be used for formatting:
6768 * if 'textwidth' option is set, use it
6769 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6770 * if invalid value, use 0.
6771 * Set default to window width (maximum 79) for "gq" operator.
6772 */
6773 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006774comp_textwidth(
6775 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 int textwidth;
6778
6779 textwidth = curbuf->b_p_tw;
6780 if (textwidth == 0 && curbuf->b_p_wm)
6781 {
6782 /* The width is the window width minus 'wrapmargin' minus all the
6783 * things that add to the margin. */
6784 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6785#ifdef FEAT_CMDWIN
6786 if (cmdwin_type != 0)
6787 textwidth -= 1;
6788#endif
6789#ifdef FEAT_FOLDING
6790 textwidth -= curwin->w_p_fdc;
6791#endif
6792#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006793 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 textwidth -= 1;
6795#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006796 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 textwidth -= 8;
6798 }
6799 if (textwidth < 0)
6800 textwidth = 0;
6801 if (ff && textwidth == 0)
6802 {
6803 textwidth = W_WIDTH(curwin) - 1;
6804 if (textwidth > 79)
6805 textwidth = 79;
6806 }
6807 return textwidth;
6808}
6809
6810/*
6811 * Put a character in the redo buffer, for when just after a CTRL-V.
6812 */
6813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006814redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
6816 char_u buf[10];
6817
6818 /* Only digits need special treatment. Translate them into a string of
6819 * three digits. */
6820 if (VIM_ISDIGIT(c))
6821 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006822 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 AppendToRedobuff(buf);
6824 }
6825 else
6826 AppendCharToRedobuff(c);
6827}
6828
6829/*
6830 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006831 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 */
6833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006834start_arrow(
6835 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006837 start_arrow_common(end_insert_pos, TRUE);
6838}
6839
6840/*
6841 * Like start_arrow() but with end_change argument.
6842 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6843 */
6844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006845start_arrow_with_change(
6846 pos_T *end_insert_pos, /* can be NULL */
6847 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006848{
6849 start_arrow_common(end_insert_pos, end_change);
6850 if (!end_change)
6851 {
6852 AppendCharToRedobuff(Ctrl_G);
6853 AppendCharToRedobuff('U');
6854 }
6855}
6856
6857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006858start_arrow_common(
6859 pos_T *end_insert_pos, /* can be NULL */
6860 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006861{
6862 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {
6864 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006865 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 arrow_used = TRUE; /* this means we stopped the current insert */
6867 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006868#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006869 check_spell_redraw();
6870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871}
6872
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006873#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006874/*
6875 * If we skipped highlighting word at cursor, do it now.
6876 * It may be skipped again, thus reset spell_redraw_lnum first.
6877 */
6878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006879check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006880{
6881 if (spell_redraw_lnum != 0)
6882 {
6883 linenr_T lnum = spell_redraw_lnum;
6884
6885 spell_redraw_lnum = 0;
6886 redrawWinline(lnum, FALSE);
6887 }
6888}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006889
6890/*
6891 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6892 * spelled word, if there is one.
6893 */
6894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006895spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006896{
6897 pos_T tpos = curwin->w_cursor;
6898
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006899 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006900 if (curwin->w_cursor.col != tpos.col)
6901 start_arrow(&tpos);
6902}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006903#endif
6904
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905/*
6906 * stop_arrow() is called before a change is made in insert mode.
6907 * If an arrow key has been used, start a new insertion.
6908 * Returns FAIL if undo is impossible, shouldn't insert then.
6909 */
6910 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006911stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912{
6913 if (arrow_used)
6914 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006915 Insstart = curwin->w_cursor; /* new insertion starts here */
6916 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6917 /* Don't update the original insert position when moved to the
6918 * right, except when nothing was inserted yet. */
6919 update_Insstart_orig = FALSE;
6920 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6921
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 if (u_save_cursor() == OK)
6923 {
6924 arrow_used = FALSE;
6925 ins_need_undo = FALSE;
6926 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006927
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 ai_col = 0;
6929#ifdef FEAT_VREPLACE
6930 if (State & VREPLACE_FLAG)
6931 {
6932 orig_line_count = curbuf->b_ml.ml_line_count;
6933 vr_lines_changed = 1;
6934 }
6935#endif
6936 ResetRedobuff();
6937 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006938 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 }
6940 else if (ins_need_undo)
6941 {
6942 if (u_save_cursor() == OK)
6943 ins_need_undo = FALSE;
6944 }
6945
6946#ifdef FEAT_FOLDING
6947 /* Always open fold at the cursor line when inserting something. */
6948 foldOpenCursor();
6949#endif
6950
6951 return (arrow_used || ins_need_undo ? FAIL : OK);
6952}
6953
6954/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006955 * Do a few things to stop inserting.
6956 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6957 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 */
6959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006960stop_insert(
6961 pos_T *end_insert_pos,
6962 int esc, /* called by ins_esc() */
6963 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006965 int cc;
6966 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967
6968 stop_redo_ins();
6969 replace_flush(); /* abandon replace stack */
6970
6971 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006972 * Save the inserted text for later redo with ^@ and CTRL-A.
6973 * Don't do it when "restart_edit" was set and nothing was inserted,
6974 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006976 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006977 if (did_restart_edit == 0 || (ptr != NULL
6978 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006979 {
6980 vim_free(last_insert);
6981 last_insert = ptr;
6982 last_insert_skip = new_insert_skip;
6983 }
6984 else
6985 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006987 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {
6989 /* Auto-format now. It may seem strange to do this when stopping an
6990 * insertion (or moving the cursor), but it's required when appending
6991 * a line and having it end in a space. But only do it when something
6992 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006993 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006995 pos_T tpos = curwin->w_cursor;
6996
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 /* When the cursor is at the end of the line after a space the
6998 * formatting will move it to the following word. Avoid that by
6999 * moving the cursor onto the space. */
7000 cc = 'x';
7001 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7002 {
7003 dec_cursor();
7004 cc = gchar_cursor();
7005 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007006 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 }
7008
7009 auto_format(TRUE, FALSE);
7010
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007011 if (vim_iswhite(cc))
7012 {
7013 if (gchar_cursor() != NUL)
7014 inc_cursor();
7015#ifdef FEAT_VIRTUALEDIT
7016 /* If the cursor is still at the same character, also keep
7017 * the "coladd". */
7018 if (gchar_cursor() == NUL
7019 && curwin->w_cursor.lnum == tpos.lnum
7020 && curwin->w_cursor.col == tpos.col)
7021 curwin->w_cursor.coladd = tpos.coladd;
7022#endif
7023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 }
7025
7026 /* If a space was inserted for auto-formatting, remove it now. */
7027 check_auto_format(TRUE);
7028
7029 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007030 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007031 * Do this when ESC was used or moving the cursor up/down.
7032 * Check for the old position still being valid, just in case the text
7033 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007034 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007035 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7036 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007038 pos_T tpos = curwin->w_cursor;
7039
7040 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007041 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007042 for (;;)
7043 {
7044 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7045 --curwin->w_cursor.col;
7046 cc = gchar_cursor();
7047 if (!vim_iswhite(cc))
7048 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007049 if (del_char(TRUE) == FAIL)
7050 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007051 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007052 if (curwin->w_cursor.lnum != tpos.lnum)
7053 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007054 else
7055 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007056 /* reset tpos, could have been invalidated in the loop above */
7057 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007058 tpos.col++;
7059 if (cc != NUL && gchar_pos(&tpos) == NUL)
7060 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 /* <C-S-Right> may have started Visual mode, adjust the position for
7064 * deleted characters. */
7065 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7066 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007067 int len = (int)STRLEN(ml_get_curline());
7068
7069 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007071 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007072#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 }
7076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 }
7078 }
7079 did_ai = FALSE;
7080#ifdef FEAT_SMARTINDENT
7081 did_si = FALSE;
7082 can_si = FALSE;
7083 can_si_back = FALSE;
7084#endif
7085
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007086 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7087 * now in a different buffer. */
7088 if (end_insert_pos != NULL)
7089 {
7090 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007091 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007092 curbuf->b_op_end = *end_insert_pos;
7093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094}
7095
7096/*
7097 * Set the last inserted text to a single character.
7098 * Used for the replace command.
7099 */
7100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007101set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
7103 char_u *s;
7104
7105 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 if (last_insert != NULL)
7108 {
7109 s = last_insert;
7110 /* Use the CTRL-V only when entering a special char */
7111 if (c < ' ' || c == DEL)
7112 *s++ = Ctrl_V;
7113 s = add_char2buf(c, s);
7114 *s++ = ESC;
7115 *s++ = NUL;
7116 last_insert_skip = 0;
7117 }
7118}
7119
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007120#if defined(EXITFREE) || defined(PROTO)
7121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007122free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007123{
7124 vim_free(last_insert);
7125 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007126# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007127 vim_free(compl_orig_text);
7128 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007129# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007130}
7131#endif
7132
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133/*
7134 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7135 * and CSI. Handle multi-byte characters.
7136 * Returns a pointer to after the added bytes.
7137 */
7138 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007139add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
7141#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007142 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 int i;
7144 int len;
7145
7146 len = (*mb_char2bytes)(c, temp);
7147 for (i = 0; i < len; ++i)
7148 {
7149 c = temp[i];
7150#endif
7151 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7152 if (c == K_SPECIAL)
7153 {
7154 *s++ = K_SPECIAL;
7155 *s++ = KS_SPECIAL;
7156 *s++ = KE_FILLER;
7157 }
7158#ifdef FEAT_GUI
7159 else if (c == CSI)
7160 {
7161 *s++ = CSI;
7162 *s++ = KS_EXTRA;
7163 *s++ = (int)KE_CSI;
7164 }
7165#endif
7166 else
7167 *s++ = c;
7168#ifdef FEAT_MBYTE
7169 }
7170#endif
7171 return s;
7172}
7173
7174/*
7175 * move cursor to start of line
7176 * if flags & BL_WHITE move to first non-white
7177 * if flags & BL_SOL move to first non-white if startofline is set,
7178 * otherwise keep "curswant" column
7179 * if flags & BL_FIX don't leave the cursor on a NUL.
7180 */
7181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007182beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183{
7184 if ((flags & BL_SOL) && !p_sol)
7185 coladvance(curwin->w_curswant);
7186 else
7187 {
7188 curwin->w_cursor.col = 0;
7189#ifdef FEAT_VIRTUALEDIT
7190 curwin->w_cursor.coladd = 0;
7191#endif
7192
7193 if (flags & (BL_WHITE | BL_SOL))
7194 {
7195 char_u *ptr;
7196
7197 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7198 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7199 ++curwin->w_cursor.col;
7200 }
7201 curwin->w_set_curswant = TRUE;
7202 }
7203}
7204
7205/*
7206 * oneright oneleft cursor_down cursor_up
7207 *
7208 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007209 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 * Return OK when successful, FAIL when we hit a line of file boundary.
7211 */
7212
7213 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007214oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
7216 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
7219#ifdef FEAT_VIRTUALEDIT
7220 if (virtual_active())
7221 {
7222 pos_T prevpos = curwin->w_cursor;
7223
7224 /* Adjust for multi-wide char (excluding TAB) */
7225 ptr = ml_get_cursor();
7226 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007227# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007229# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 ))
7233 ? ptr2cells(ptr) : 1));
7234 curwin->w_set_curswant = TRUE;
7235 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7236 return (prevpos.col != curwin->w_cursor.col
7237 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7238 }
7239#endif
7240
7241 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007242 if (*ptr == NUL)
7243 return FAIL; /* already at the very end */
7244
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007246 if (has_mbyte)
7247 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 else
7249#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007250 l = 1;
7251
7252 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7253 * contains "onemore". */
7254 if (ptr[l] == NUL
7255#ifdef FEAT_VIRTUALEDIT
7256 && (ve_flags & VE_ONEMORE) == 0
7257#endif
7258 )
7259 return FAIL;
7260 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
7262 curwin->w_set_curswant = TRUE;
7263 return OK;
7264}
7265
7266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007267oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
7269#ifdef FEAT_VIRTUALEDIT
7270 if (virtual_active())
7271 {
7272 int width;
7273 int v = getviscol();
7274
7275 if (v == 0)
7276 return FAIL;
7277
7278# ifdef FEAT_LINEBREAK
7279 /* We might get stuck on 'showbreak', skip over it. */
7280 width = 1;
7281 for (;;)
7282 {
7283 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007284 /* getviscol() is slow, skip it when 'showbreak' is empty,
7285 * 'breakindent' is not set and there are no multi-byte
7286 * characters */
7287 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288# ifdef FEAT_MBYTE
7289 && !has_mbyte
7290# endif
7291 ) || getviscol() < v)
7292 break;
7293 ++width;
7294 }
7295# else
7296 coladvance(v - 1);
7297# endif
7298
7299 if (curwin->w_cursor.coladd == 1)
7300 {
7301 char_u *ptr;
7302
7303 /* Adjust for multi-wide char (not a TAB) */
7304 ptr = ml_get_cursor();
7305 if (*ptr != TAB && vim_isprintc(
7306# ifdef FEAT_MBYTE
7307 (*mb_ptr2char)(ptr)
7308# else
7309 *ptr
7310# endif
7311 ) && ptr2cells(ptr) > 1)
7312 curwin->w_cursor.coladd = 0;
7313 }
7314
7315 curwin->w_set_curswant = TRUE;
7316 return OK;
7317 }
7318#endif
7319
7320 if (curwin->w_cursor.col == 0)
7321 return FAIL;
7322
7323 curwin->w_set_curswant = TRUE;
7324 --curwin->w_cursor.col;
7325
7326#ifdef FEAT_MBYTE
7327 /* if the character on the left of the current cursor is a multi-byte
7328 * character, move to its first byte */
7329 if (has_mbyte)
7330 mb_adjust_cursor();
7331#endif
7332 return OK;
7333}
7334
7335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007336cursor_up(
7337 long n,
7338 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339{
7340 linenr_T lnum;
7341
7342 if (n > 0)
7343 {
7344 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007345 /* This fails if the cursor is already in the first line or the count
7346 * is larger than the line number and '-' is in 'cpoptions' */
7347 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 return FAIL;
7349 if (n >= lnum)
7350 lnum = 1;
7351 else
7352#ifdef FEAT_FOLDING
7353 if (hasAnyFolding(curwin))
7354 {
7355 /*
7356 * Count each sequence of folded lines as one logical line.
7357 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007358 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 (void)hasFolding(lnum, &lnum, NULL);
7360
7361 while (n--)
7362 {
7363 /* move up one line */
7364 --lnum;
7365 if (lnum <= 1)
7366 break;
7367 /* If we entered a fold, move to the beginning, unless in
7368 * Insert mode or when 'foldopen' contains "all": it will open
7369 * in a moment. */
7370 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7371 (void)hasFolding(lnum, &lnum, NULL);
7372 }
7373 if (lnum < 1)
7374 lnum = 1;
7375 }
7376 else
7377#endif
7378 lnum -= n;
7379 curwin->w_cursor.lnum = lnum;
7380 }
7381
7382 /* try to advance to the column we want to be at */
7383 coladvance(curwin->w_curswant);
7384
7385 if (upd_topline)
7386 update_topline(); /* make sure curwin->w_topline is valid */
7387
7388 return OK;
7389}
7390
7391/*
7392 * Cursor down a number of logical lines.
7393 */
7394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007395cursor_down(
7396 long n,
7397 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
7399 linenr_T lnum;
7400
7401 if (n > 0)
7402 {
7403 lnum = curwin->w_cursor.lnum;
7404#ifdef FEAT_FOLDING
7405 /* Move to last line of fold, will fail if it's the end-of-file. */
7406 (void)hasFolding(lnum, NULL, &lnum);
7407#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007408 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007409 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007410 if (lnum >= curbuf->b_ml.ml_line_count
7411 || (lnum + n > curbuf->b_ml.ml_line_count
7412 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 return FAIL;
7414 if (lnum + n >= curbuf->b_ml.ml_line_count)
7415 lnum = curbuf->b_ml.ml_line_count;
7416 else
7417#ifdef FEAT_FOLDING
7418 if (hasAnyFolding(curwin))
7419 {
7420 linenr_T last;
7421
7422 /* count each sequence of folded lines as one logical line */
7423 while (n--)
7424 {
7425 if (hasFolding(lnum, NULL, &last))
7426 lnum = last + 1;
7427 else
7428 ++lnum;
7429 if (lnum >= curbuf->b_ml.ml_line_count)
7430 break;
7431 }
7432 if (lnum > curbuf->b_ml.ml_line_count)
7433 lnum = curbuf->b_ml.ml_line_count;
7434 }
7435 else
7436#endif
7437 lnum += n;
7438 curwin->w_cursor.lnum = lnum;
7439 }
7440
7441 /* try to advance to the column we want to be at */
7442 coladvance(curwin->w_curswant);
7443
7444 if (upd_topline)
7445 update_topline(); /* make sure curwin->w_topline is valid */
7446
7447 return OK;
7448}
7449
7450/*
7451 * Stuff the last inserted text in the read buffer.
7452 * Last_insert actually is a copy of the redo buffer, so we
7453 * first have to remove the command.
7454 */
7455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007456stuff_inserted(
7457 int c, /* Command character to be inserted */
7458 long count, /* Repeat this many times */
7459 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460{
7461 char_u *esc_ptr;
7462 char_u *ptr;
7463 char_u *last_ptr;
7464 char_u last = NUL;
7465
7466 ptr = get_last_insert();
7467 if (ptr == NULL)
7468 {
7469 EMSG(_(e_noinstext));
7470 return FAIL;
7471 }
7472
7473 /* may want to stuff the command character, to start Insert mode */
7474 if (c != NUL)
7475 stuffcharReadbuff(c);
7476 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7477 *esc_ptr = NUL; /* remove the ESC */
7478
7479 /* when the last char is either "0" or "^" it will be quoted if no ESC
7480 * comes after it OR if it will inserted more than once and "ptr"
7481 * starts with ^D. -- Acevedo
7482 */
7483 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7484 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7485 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7486 {
7487 last = *last_ptr;
7488 *last_ptr = NUL;
7489 }
7490
7491 do
7492 {
7493 stuffReadbuff(ptr);
7494 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7495 if (last)
7496 stuffReadbuff((char_u *)(last == '0'
7497 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7498 : IF_EB("\026^", CTRL_V_STR "^")));
7499 }
7500 while (--count > 0);
7501
7502 if (last)
7503 *last_ptr = last;
7504
7505 if (esc_ptr != NULL)
7506 *esc_ptr = ESC; /* put the ESC back */
7507
7508 /* may want to stuff a trailing ESC, to get out of Insert mode */
7509 if (!no_esc)
7510 stuffcharReadbuff(ESC);
7511
7512 return OK;
7513}
7514
7515 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007516get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517{
7518 if (last_insert == NULL)
7519 return NULL;
7520 return last_insert + last_insert_skip;
7521}
7522
7523/*
7524 * Get last inserted string, and remove trailing <Esc>.
7525 * Returns pointer to allocated memory (must be freed) or NULL.
7526 */
7527 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007528get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
7530 char_u *s;
7531 int len;
7532
7533 if (last_insert == NULL)
7534 return NULL;
7535 s = vim_strsave(last_insert + last_insert_skip);
7536 if (s != NULL)
7537 {
7538 len = (int)STRLEN(s);
7539 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7540 s[len - 1] = NUL;
7541 }
7542 return s;
7543}
7544
7545/*
7546 * Check the word in front of the cursor for an abbreviation.
7547 * Called when the non-id character "c" has been entered.
7548 * When an abbreviation is recognized it is removed from the text and
7549 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7550 */
7551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007552echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
7554 /* Don't check for abbreviation in paste mode, when disabled and just
7555 * after moving around with cursor keys. */
7556 if (p_paste || no_abbr || arrow_used)
7557 return FALSE;
7558
7559 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7560 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7561}
7562
7563/*
7564 * replace-stack functions
7565 *
7566 * When replacing characters, the replaced characters are remembered for each
7567 * new character. This is used to re-insert the old text when backspacing.
7568 *
7569 * There is a NUL headed list of characters for each character that is
7570 * currently in the file after the insertion point. When BS is used, one NUL
7571 * headed list is put back for the deleted character.
7572 *
7573 * For a newline, there are two NUL headed lists. One contains the characters
7574 * that the NL replaced. The extra one stores the characters after the cursor
7575 * that were deleted (always white space).
7576 *
7577 * Replace_offset is normally 0, in which case replace_push will add a new
7578 * character at the end of the stack. If replace_offset is not 0, that many
7579 * characters will be left on the stack above the newly inserted character.
7580 */
7581
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007582static char_u *replace_stack = NULL;
7583static long replace_stack_nr = 0; /* next entry in replace stack */
7584static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585
7586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007587replace_push(
7588 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589{
7590 char_u *p;
7591
7592 if (replace_stack_nr < replace_offset) /* nothing to do */
7593 return;
7594 if (replace_stack_len <= replace_stack_nr)
7595 {
7596 replace_stack_len += 50;
7597 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7598 if (p == NULL) /* out of memory */
7599 {
7600 replace_stack_len -= 50;
7601 return;
7602 }
7603 if (replace_stack != NULL)
7604 {
7605 mch_memmove(p, replace_stack,
7606 (size_t)(replace_stack_nr * sizeof(char_u)));
7607 vim_free(replace_stack);
7608 }
7609 replace_stack = p;
7610 }
7611 p = replace_stack + replace_stack_nr - replace_offset;
7612 if (replace_offset)
7613 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7614 *p = c;
7615 ++replace_stack_nr;
7616}
7617
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007618#if defined(FEAT_MBYTE) || defined(PROTO)
7619/*
7620 * Push a character onto the replace stack. Handles a multi-byte character in
7621 * reverse byte order, so that the first byte is popped off first.
7622 * Return the number of bytes done (includes composing characters).
7623 */
7624 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007625replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007626{
7627 int l = (*mb_ptr2len)(p);
7628 int j;
7629
7630 for (j = l - 1; j >= 0; --j)
7631 replace_push(p[j]);
7632 return l;
7633}
7634#endif
7635
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636/*
7637 * Pop one item from the replace stack.
7638 * return -1 if stack empty
7639 * return replaced character or NUL otherwise
7640 */
7641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007642replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
7644 if (replace_stack_nr == 0)
7645 return -1;
7646 return (int)replace_stack[--replace_stack_nr];
7647}
7648
7649/*
7650 * Join the top two items on the replace stack. This removes to "off"'th NUL
7651 * encountered.
7652 */
7653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654replace_join(
7655 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
7657 int i;
7658
7659 for (i = replace_stack_nr; --i >= 0; )
7660 if (replace_stack[i] == NUL && off-- <= 0)
7661 {
7662 --replace_stack_nr;
7663 mch_memmove(replace_stack + i, replace_stack + i + 1,
7664 (size_t)(replace_stack_nr - i));
7665 return;
7666 }
7667}
7668
7669/*
7670 * Pop bytes from the replace stack until a NUL is found, and insert them
7671 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7672 */
7673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007674replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
7676 int cc;
7677 int oldState = State;
7678
7679 State = NORMAL; /* don't want REPLACE here */
7680 while ((cc = replace_pop()) > 0)
7681 {
7682#ifdef FEAT_MBYTE
7683 mb_replace_pop_ins(cc);
7684#else
7685 ins_char(cc);
7686#endif
7687 dec_cursor();
7688 }
7689 State = oldState;
7690}
7691
7692#ifdef FEAT_MBYTE
7693/*
7694 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7695 * indicates a multi-byte char, pop the other bytes too.
7696 */
7697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007698mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007701 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 int i;
7703 int c;
7704
7705 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7706 {
7707 buf[0] = cc;
7708 for (i = 1; i < n; ++i)
7709 buf[i] = replace_pop();
7710 ins_bytes_len(buf, n);
7711 }
7712 else
7713 ins_char(cc);
7714
7715 if (enc_utf8)
7716 /* Handle composing chars. */
7717 for (;;)
7718 {
7719 c = replace_pop();
7720 if (c == -1) /* stack empty */
7721 break;
7722 if ((n = MB_BYTE2LEN(c)) == 1)
7723 {
7724 /* Not a multi-byte char, put it back. */
7725 replace_push(c);
7726 break;
7727 }
7728 else
7729 {
7730 buf[0] = c;
7731 for (i = 1; i < n; ++i)
7732 buf[i] = replace_pop();
7733 if (utf_iscomposing(utf_ptr2char(buf)))
7734 ins_bytes_len(buf, n);
7735 else
7736 {
7737 /* Not a composing char, put it back. */
7738 for (i = n - 1; i >= 0; --i)
7739 replace_push(buf[i]);
7740 break;
7741 }
7742 }
7743 }
7744}
7745#endif
7746
7747/*
7748 * make the replace stack empty
7749 * (called when exiting replace mode)
7750 */
7751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007752replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754 vim_free(replace_stack);
7755 replace_stack = NULL;
7756 replace_stack_len = 0;
7757 replace_stack_nr = 0;
7758}
7759
7760/*
7761 * Handle doing a BS for one character.
7762 * cc < 0: replace stack empty, just move cursor
7763 * cc == 0: character was inserted, delete it
7764 * cc > 0: character was replaced, put cc (first byte of original char) back
7765 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007766 * When "limit_col" is >= 0, don't delete before this column. Matters when
7767 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 */
7769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007770replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
7772 int cc;
7773#ifdef FEAT_VREPLACE
7774 int orig_len = 0;
7775 int ins_len;
7776 int orig_vcols = 0;
7777 colnr_T start_vcol;
7778 char_u *p;
7779 int i;
7780 int vcol;
7781#endif
7782
7783 cc = replace_pop();
7784 if (cc > 0)
7785 {
7786#ifdef FEAT_VREPLACE
7787 if (State & VREPLACE_FLAG)
7788 {
7789 /* Get the number of screen cells used by the character we are
7790 * going to delete. */
7791 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7792 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7793 }
7794#endif
7795#ifdef FEAT_MBYTE
7796 if (has_mbyte)
7797 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007798 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799# ifdef FEAT_VREPLACE
7800 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007801 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802# endif
7803 replace_push(cc);
7804 }
7805 else
7806#endif
7807 {
7808 pchar_cursor(cc);
7809#ifdef FEAT_VREPLACE
7810 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007811 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812#endif
7813 }
7814 replace_pop_ins();
7815
7816#ifdef FEAT_VREPLACE
7817 if (State & VREPLACE_FLAG)
7818 {
7819 /* Get the number of screen cells used by the inserted characters */
7820 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007821 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 vcol = start_vcol;
7823 for (i = 0; i < ins_len; ++i)
7824 {
7825 vcol += chartabsize(p + i, vcol);
7826#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007827 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828#endif
7829 }
7830 vcol -= start_vcol;
7831
7832 /* Delete spaces that were inserted after the cursor to keep the
7833 * text aligned. */
7834 curwin->w_cursor.col += ins_len;
7835 while (vcol > orig_vcols && gchar_cursor() == ' ')
7836 {
7837 del_char(FALSE);
7838 ++orig_vcols;
7839 }
7840 curwin->w_cursor.col -= ins_len;
7841 }
7842#endif
7843
7844 /* mark the buffer as changed and prepare for displaying */
7845 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7846 }
7847 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007848 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849}
7850
7851#ifdef FEAT_CINDENT
7852/*
7853 * Return TRUE if C-indenting is on.
7854 */
7855 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007856cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
7858 return (!p_paste && (curbuf->b_p_cin
7859# ifdef FEAT_EVAL
7860 || *curbuf->b_p_inde != NUL
7861# endif
7862 ));
7863}
7864#endif
7865
7866#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7867/*
7868 * Re-indent the current line, based on the current contents of it and the
7869 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7870 * confused what all the part that handles Control-T is doing that I'm not.
7871 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7872 */
7873
7874 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007875fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007877 int amount = get_the_indent();
7878
7879 if (amount >= 0)
7880 {
7881 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7882 if (linewhite(curwin->w_cursor.lnum))
7883 did_ai = TRUE; /* delete the indent if the line stays empty */
7884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885}
7886
7887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007888fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
7890 if (p_paste)
7891 return;
7892# ifdef FEAT_LISP
7893 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7894 fixthisline(get_lisp_indent);
7895# endif
7896# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7897 else
7898# endif
7899# ifdef FEAT_CINDENT
7900 if (cindent_on())
7901 do_c_expr_indent();
7902# endif
7903}
7904
7905#endif
7906
7907#ifdef FEAT_CINDENT
7908/*
7909 * return TRUE if 'cinkeys' contains the key "keytyped",
7910 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007911 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7913 *
7914 * "keytyped" can have a few special values:
7915 * KEY_OPEN_FORW
7916 * KEY_OPEN_BACK
7917 * KEY_COMPLETE just finished completion.
7918 *
7919 * If line_is_empty is TRUE accept keys with '0' before them.
7920 */
7921 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007922in_cinkeys(
7923 int keytyped,
7924 int when,
7925 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927 char_u *look;
7928 int try_match;
7929 int try_match_word;
7930 char_u *p;
7931 char_u *line;
7932 int icase;
7933 int i;
7934
Bram Moolenaar30848942009-12-24 14:46:12 +00007935 if (keytyped == NUL)
7936 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7937 return FALSE;
7938
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939#ifdef FEAT_EVAL
7940 if (*curbuf->b_p_inde != NUL)
7941 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7942 else
7943#endif
7944 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7945 while (*look)
7946 {
7947 /*
7948 * Find out if we want to try a match with this key, depending on
7949 * 'when' and a '*' or '!' before the key.
7950 */
7951 switch (when)
7952 {
7953 case '*': try_match = (*look == '*'); break;
7954 case '!': try_match = (*look == '!'); break;
7955 default: try_match = (*look != '*'); break;
7956 }
7957 if (*look == '*' || *look == '!')
7958 ++look;
7959
7960 /*
7961 * If there is a '0', only accept a match if the line is empty.
7962 * But may still match when typing last char of a word.
7963 */
7964 if (*look == '0')
7965 {
7966 try_match_word = try_match;
7967 if (!line_is_empty)
7968 try_match = FALSE;
7969 ++look;
7970 }
7971 else
7972 try_match_word = FALSE;
7973
7974 /*
7975 * does it look like a control character?
7976 */
7977 if (*look == '^'
7978#ifdef EBCDIC
7979 && (Ctrl_chr(look[1]) != 0)
7980#else
7981 && look[1] >= '?' && look[1] <= '_'
7982#endif
7983 )
7984 {
7985 if (try_match && keytyped == Ctrl_chr(look[1]))
7986 return TRUE;
7987 look += 2;
7988 }
7989 /*
7990 * 'o' means "o" command, open forward.
7991 * 'O' means "O" command, open backward.
7992 */
7993 else if (*look == 'o')
7994 {
7995 if (try_match && keytyped == KEY_OPEN_FORW)
7996 return TRUE;
7997 ++look;
7998 }
7999 else if (*look == 'O')
8000 {
8001 if (try_match && keytyped == KEY_OPEN_BACK)
8002 return TRUE;
8003 ++look;
8004 }
8005
8006 /*
8007 * 'e' means to check for "else" at start of line and just before the
8008 * cursor.
8009 */
8010 else if (*look == 'e')
8011 {
8012 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8013 {
8014 p = ml_get_curline();
8015 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8016 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8017 return TRUE;
8018 }
8019 ++look;
8020 }
8021
8022 /*
8023 * ':' only causes an indent if it is at the end of a label or case
8024 * statement, or when it was before typing the ':' (to fix
8025 * class::method for C++).
8026 */
8027 else if (*look == ':')
8028 {
8029 if (try_match && keytyped == ':')
8030 {
8031 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008032 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008034 /* Need to get the line again after cin_islabel(). */
8035 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 if (curwin->w_cursor.col > 2
8037 && p[curwin->w_cursor.col - 1] == ':'
8038 && p[curwin->w_cursor.col - 2] == ':')
8039 {
8040 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008041 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008042 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 p = ml_get_curline();
8044 p[curwin->w_cursor.col - 1] = ':';
8045 if (i)
8046 return TRUE;
8047 }
8048 }
8049 ++look;
8050 }
8051
8052
8053 /*
8054 * Is it a key in <>, maybe?
8055 */
8056 else if (*look == '<')
8057 {
8058 if (try_match)
8059 {
8060 /*
8061 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8062 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8063 * >, *, : and ! keys if they really really want to.
8064 */
8065 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8066 && keytyped == look[1])
8067 return TRUE;
8068
8069 if (keytyped == get_special_key_code(look + 1))
8070 return TRUE;
8071 }
8072 while (*look && *look != '>')
8073 look++;
8074 while (*look == '>')
8075 look++;
8076 }
8077
8078 /*
8079 * Is it a word: "=word"?
8080 */
8081 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8082 {
8083 ++look;
8084 if (*look == '~')
8085 {
8086 icase = TRUE;
8087 ++look;
8088 }
8089 else
8090 icase = FALSE;
8091 p = vim_strchr(look, ',');
8092 if (p == NULL)
8093 p = look + STRLEN(look);
8094 if ((try_match || try_match_word)
8095 && curwin->w_cursor.col >= (colnr_T)(p - look))
8096 {
8097 int match = FALSE;
8098
8099#ifdef FEAT_INS_EXPAND
8100 if (keytyped == KEY_COMPLETE)
8101 {
8102 char_u *s;
8103
8104 /* Just completed a word, check if it starts with "look".
8105 * search back for the start of a word. */
8106 line = ml_get_curline();
8107# ifdef FEAT_MBYTE
8108 if (has_mbyte)
8109 {
8110 char_u *n;
8111
8112 for (s = line + curwin->w_cursor.col; s > line; s = n)
8113 {
8114 n = mb_prevptr(line, s);
8115 if (!vim_iswordp(n))
8116 break;
8117 }
8118 }
8119 else
8120# endif
8121 for (s = line + curwin->w_cursor.col; s > line; --s)
8122 if (!vim_iswordc(s[-1]))
8123 break;
8124 if (s + (p - look) <= line + curwin->w_cursor.col
8125 && (icase
8126 ? MB_STRNICMP(s, look, p - look)
8127 : STRNCMP(s, look, p - look)) == 0)
8128 match = TRUE;
8129 }
8130 else
8131#endif
8132 /* TODO: multi-byte */
8133 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8134 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8135 {
8136 line = ml_get_cursor();
8137 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8138 || !vim_iswordc(line[-(p - look) - 1]))
8139 && (icase
8140 ? MB_STRNICMP(line - (p - look), look, p - look)
8141 : STRNCMP(line - (p - look), look, p - look))
8142 == 0)
8143 match = TRUE;
8144 }
8145 if (match && try_match_word && !try_match)
8146 {
8147 /* "0=word": Check if there are only blanks before the
8148 * word. */
8149 line = ml_get_curline();
8150 if ((int)(skipwhite(line) - line) !=
8151 (int)(curwin->w_cursor.col - (p - look)))
8152 match = FALSE;
8153 }
8154 if (match)
8155 return TRUE;
8156 }
8157 look = p;
8158 }
8159
8160 /*
8161 * ok, it's a boring generic character.
8162 */
8163 else
8164 {
8165 if (try_match && *look == keytyped)
8166 return TRUE;
8167 ++look;
8168 }
8169
8170 /*
8171 * Skip over ", ".
8172 */
8173 look = skip_to_option_part(look);
8174 }
8175 return FALSE;
8176}
8177#endif /* FEAT_CINDENT */
8178
8179#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8180/*
8181 * Map Hebrew keyboard when in hkmap mode.
8182 */
8183 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008184hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185{
8186 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8187 {
8188 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8189 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8190 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8191 static char_u map[26] =
8192 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8193 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8194 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8195 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8196 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8197 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8198 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8199 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8200 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8201
8202 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8203 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8204 /* '-1'='sofit' */
8205 else if (c == 'x')
8206 return 'X';
8207 else if (c == 'q')
8208 return '\''; /* {geresh}={'} */
8209 else if (c == 246)
8210 return ' '; /* \"o --> ' ' for a german keyboard */
8211 else if (c == 228)
8212 return ' '; /* \"a --> ' ' -- / -- */
8213 else if (c == 252)
8214 return ' '; /* \"u --> ' ' -- / -- */
8215#ifdef EBCDIC
8216 else if (islower(c))
8217#else
8218 /* NOTE: islower() does not do the right thing for us on Linux so we
8219 * do this the same was as 5.7 and previous, so it works correctly on
8220 * all systems. Specifically, the e.g. Delete and Arrow keys are
8221 * munged and won't work if e.g. searching for Hebrew text.
8222 */
8223 else if (c >= 'a' && c <= 'z')
8224#endif
8225 return (int)(map[CharOrdLow(c)] + p_aleph);
8226 else
8227 return c;
8228 }
8229 else
8230 {
8231 switch (c)
8232 {
8233 case '`': return ';';
8234 case '/': return '.';
8235 case '\'': return ',';
8236 case 'q': return '/';
8237 case 'w': return '\'';
8238
8239 /* Hebrew letters - set offset from 'a' */
8240 case ',': c = '{'; break;
8241 case '.': c = 'v'; break;
8242 case ';': c = 't'; break;
8243 default: {
8244 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8245
8246#ifdef EBCDIC
8247 /* see note about islower() above */
8248 if (!islower(c))
8249#else
8250 if (c < 'a' || c > 'z')
8251#endif
8252 return c;
8253 c = str[CharOrdLow(c)];
8254 break;
8255 }
8256 }
8257
8258 return (int)(CharOrdLow(c) + p_aleph);
8259 }
8260}
8261#endif
8262
8263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008264ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
8266 int need_redraw = FALSE;
8267 int regname;
8268 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008269 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270
8271 /*
8272 * If we are going to wait for a character, show a '"'.
8273 */
8274 pc_status = PC_STATUS_UNSET;
8275 if (redrawing() && !char_avail())
8276 {
8277 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008278 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279
8280 edit_putchar('"', TRUE);
8281#ifdef FEAT_CMDL_INFO
8282 add_to_showcmd_c(Ctrl_R);
8283#endif
8284 }
8285
8286#ifdef USE_ON_FLY_SCROLL
8287 dont_scroll = TRUE; /* disallow scrolling here */
8288#endif
8289
8290 /*
8291 * Don't map the register name. This also prevents the mode message to be
8292 * deleted when ESC is hit.
8293 */
8294 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008295 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8298 {
8299 /* Get a third key for literal register insertion */
8300 literally = regname;
8301#ifdef FEAT_CMDL_INFO
8302 add_to_showcmd_c(literally);
8303#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008304 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
8307 --no_mapping;
8308
8309#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008310 /* Don't call u_sync() while typing the expression or giving an error
8311 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 ++no_u_sync;
8313 if (regname == '=')
8314 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008315# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008317# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008318 /* Sync undo when evaluating the expression calls setline() or
8319 * append(), so that it can be undone separately. */
8320 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008321
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008323# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 /* Restore the Input Method. */
8325 if (im_on)
8326 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008329 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8330 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008331 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 else
8335 {
8336#endif
8337 if (literally == Ctrl_O || literally == Ctrl_P)
8338 {
8339 /* Append the command to the redo buffer. */
8340 AppendCharToRedobuff(Ctrl_R);
8341 AppendCharToRedobuff(literally);
8342 AppendCharToRedobuff(regname);
8343
8344 do_put(regname, BACKWARD, 1L,
8345 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8346 }
8347 else if (insert_reg(regname, literally) == FAIL)
8348 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008349 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 need_redraw = TRUE; /* remove the '"' */
8351 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008352 else if (stop_insert_mode)
8353 /* When the '=' register was used and a function was invoked that
8354 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8355 * insert anything, need to remove the '"' */
8356 need_redraw = TRUE;
8357
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358#ifdef FEAT_EVAL
8359 }
8360 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008361 if (u_sync_once == 1)
8362 ins_need_undo = TRUE;
8363 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364#endif
8365#ifdef FEAT_CMDL_INFO
8366 clear_showcmd();
8367#endif
8368
8369 /* If the inserted register is empty, we need to remove the '"' */
8370 if (need_redraw || stuff_empty())
8371 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008372
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008373 /* Disallow starting Visual mode here, would get a weird mode. */
8374 if (!vis_active && VIsual_active)
8375 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376}
8377
8378/*
8379 * CTRL-G commands in Insert mode.
8380 */
8381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008382ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383{
8384 int c;
8385
8386#ifdef FEAT_INS_EXPAND
8387 /* Right after CTRL-X the cursor will be after the ruler. */
8388 setcursor();
8389#endif
8390
8391 /*
8392 * Don't map the second key. This also prevents the mode message to be
8393 * deleted when ESC is hit.
8394 */
8395 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008396 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 --no_mapping;
8398 switch (c)
8399 {
8400 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8401 case K_UP:
8402 case Ctrl_K:
8403 case 'k': ins_up(TRUE);
8404 break;
8405
8406 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8407 case K_DOWN:
8408 case Ctrl_J:
8409 case 'j': ins_down(TRUE);
8410 break;
8411
8412 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008413 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008415
8416 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008417 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008418 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008419 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 break;
8421
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008422 /* CTRL-G U: do not break undo with the next char */
8423 case 'U':
8424 /* Allow one left/right cursor movement with the next char,
8425 * without breaking undo. */
8426 dont_sync_undo = MAYBE;
8427 break;
8428
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008430 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 }
8432}
8433
8434/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008435 * CTRL-^ in Insert mode.
8436 */
8437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008438ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008439{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008440 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008441 {
8442 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8443 if (State & LANGMAP)
8444 {
8445 curbuf->b_p_iminsert = B_IMODE_NONE;
8446 State &= ~LANGMAP;
8447 }
8448 else
8449 {
8450 curbuf->b_p_iminsert = B_IMODE_LMAP;
8451 State |= LANGMAP;
8452#ifdef USE_IM_CONTROL
8453 im_set_active(FALSE);
8454#endif
8455 }
8456 }
8457#ifdef USE_IM_CONTROL
8458 else
8459 {
8460 /* There are no ":lmap" mappings, toggle IM */
8461 if (im_get_status())
8462 {
8463 curbuf->b_p_iminsert = B_IMODE_NONE;
8464 im_set_active(FALSE);
8465 }
8466 else
8467 {
8468 curbuf->b_p_iminsert = B_IMODE_IM;
8469 State &= ~LANGMAP;
8470 im_set_active(TRUE);
8471 }
8472 }
8473#endif
8474 set_iminsert_global();
8475 showmode();
8476#ifdef FEAT_GUI
8477 /* may show different cursor shape or color */
8478 if (gui.in_use)
8479 gui_update_cursor(TRUE, FALSE);
8480#endif
8481#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8482 /* Show/unshow value of 'keymap' in status lines. */
8483 status_redraw_curbuf();
8484#endif
8485}
8486
8487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 * Handle ESC in insert mode.
8489 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8490 * insert.
8491 */
8492 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008493ins_esc(
8494 long *count,
8495 int cmdchar,
8496 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497{
8498 int temp;
8499 static int disabled_redraw = FALSE;
8500
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008501#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008502 check_spell_redraw();
8503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504#if defined(FEAT_HANGULIN)
8505# if defined(ESC_CHG_TO_ENG_MODE)
8506 hangul_input_state_set(0);
8507# endif
8508 if (composing_hangul)
8509 {
8510 push_raw_key(composing_hangul_buffer, 2);
8511 composing_hangul = 0;
8512 }
8513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514
8515 temp = curwin->w_cursor.col;
8516 if (disabled_redraw)
8517 {
8518 --RedrawingDisabled;
8519 disabled_redraw = FALSE;
8520 }
8521 if (!arrow_used)
8522 {
8523 /*
8524 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008525 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8526 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 */
8528 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008529 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530
8531 /*
8532 * Repeating insert may take a long time. Check for
8533 * interrupt now and then.
8534 */
8535 if (*count > 0)
8536 {
8537 line_breakcheck();
8538 if (got_int)
8539 *count = 0;
8540 }
8541
8542 if (--*count > 0) /* repeat what was typed */
8543 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008544 /* Vi repeats the insert without replacing characters. */
8545 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8546 State &= ~REPLACE_FLAG;
8547
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 (void)start_redo_ins();
8549 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008550 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 ++RedrawingDisabled;
8552 disabled_redraw = TRUE;
8553 return FALSE; /* repeat the insert */
8554 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008555 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 undisplay_dollar();
8557 }
8558
8559 /* When an autoindent was removed, curswant stays after the
8560 * indent */
8561 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8562 curwin->w_set_curswant = TRUE;
8563
8564 /* Remember the last Insert position in the '^ mark. */
8565 if (!cmdmod.keepjumps)
8566 curbuf->b_last_insert = curwin->w_cursor;
8567
8568 /*
8569 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008570 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008572 if (!nomove
8573 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574#ifdef FEAT_VIRTUALEDIT
8575 || curwin->w_cursor.coladd > 0
8576#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008577 )
8578 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008579 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580#ifdef FEAT_RIGHTLEFT
8581 && !revins_on
8582#endif
8583 )
8584 {
8585#ifdef FEAT_VIRTUALEDIT
8586 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8587 {
8588 oneleft();
8589 if (restart_edit != NUL)
8590 ++curwin->w_cursor.coladd;
8591 }
8592 else
8593#endif
8594 {
8595 --curwin->w_cursor.col;
8596#ifdef FEAT_MBYTE
8597 /* Correct cursor for multi-byte character. */
8598 if (has_mbyte)
8599 mb_adjust_cursor();
8600#endif
8601 }
8602 }
8603
8604#ifdef USE_IM_CONTROL
8605 /* Disable IM to allow typing English directly for Normal mode commands.
8606 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8607 * well). */
8608 if (!(State & LANGMAP))
8609 im_save_status(&curbuf->b_p_iminsert);
8610 im_set_active(FALSE);
8611#endif
8612
8613 State = NORMAL;
8614 /* need to position cursor again (e.g. when on a TAB ) */
8615 changed_cline_bef_curs();
8616
8617#ifdef FEAT_MOUSE
8618 setmouse();
8619#endif
8620#ifdef CURSOR_SHAPE
8621 ui_cursor_shape(); /* may show different cursor shape */
8622#endif
8623
8624 /*
8625 * When recording or for CTRL-O, need to display the new mode.
8626 * Otherwise remove the mode message.
8627 */
8628 if (Recording || restart_edit != NUL)
8629 showmode();
8630 else if (p_smd)
8631 MSG("");
8632
8633 return TRUE; /* exit Insert mode */
8634}
8635
8636#ifdef FEAT_RIGHTLEFT
8637/*
8638 * Toggle language: hkmap and revins_on.
8639 * Move to end of reverse inserted text.
8640 */
8641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008642ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
8644 if (revins_on && revins_chars && revins_scol >= 0)
8645 {
8646 while (gchar_cursor() != NUL && revins_chars--)
8647 ++curwin->w_cursor.col;
8648 }
8649 p_ri = !p_ri;
8650 revins_on = (State == INSERT && p_ri);
8651 if (revins_on)
8652 {
8653 revins_scol = curwin->w_cursor.col;
8654 revins_legal++;
8655 revins_chars = 0;
8656 undisplay_dollar();
8657 }
8658 else
8659 revins_scol = -1;
8660#ifdef FEAT_FKMAP
8661 if (p_altkeymap)
8662 {
8663 /*
8664 * to be consistent also for redo command, using '.'
8665 * set arrow_used to true and stop it - causing to redo
8666 * characters entered in one mode (normal/reverse insert).
8667 */
8668 arrow_used = TRUE;
8669 (void)stop_arrow();
8670 p_fkmap = curwin->w_p_rl ^ p_ri;
8671 if (p_fkmap && p_ri)
8672 State = INSERT;
8673 }
8674 else
8675#endif
8676 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8677 showmode();
8678}
8679#endif
8680
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681/*
8682 * If 'keymodel' contains "startsel", may start selection.
8683 * Returns TRUE when a CTRL-O and other keys stuffed.
8684 */
8685 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008686ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
8688 if (km_startsel)
8689 switch (c)
8690 {
8691 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 case K_PAGEUP:
8694 case K_KPAGEUP:
8695 case K_PAGEDOWN:
8696 case K_KPAGEDOWN:
8697# ifdef MACOS
8698 case K_LEFT:
8699 case K_RIGHT:
8700 case K_UP:
8701 case K_DOWN:
8702 case K_END:
8703 case K_HOME:
8704# endif
8705 if (!(mod_mask & MOD_MASK_SHIFT))
8706 break;
8707 /* FALLTHROUGH */
8708 case K_S_LEFT:
8709 case K_S_RIGHT:
8710 case K_S_UP:
8711 case K_S_DOWN:
8712 case K_S_END:
8713 case K_S_HOME:
8714 /* Start selection right away, the cursor can move with
8715 * CTRL-O when beyond the end of the line. */
8716 start_selection();
8717
8718 /* Execute the key in (insert) Select mode. */
8719 stuffcharReadbuff(Ctrl_O);
8720 if (mod_mask)
8721 {
8722 char_u buf[4];
8723
8724 buf[0] = K_SPECIAL;
8725 buf[1] = KS_MODIFIER;
8726 buf[2] = mod_mask;
8727 buf[3] = NUL;
8728 stuffReadbuff(buf);
8729 }
8730 stuffcharReadbuff(c);
8731 return TRUE;
8732 }
8733 return FALSE;
8734}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
8736/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008737 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008738 */
8739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008740ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008741{
8742#ifdef FEAT_FKMAP
8743 if (p_fkmap && p_ri)
8744 {
8745 beep_flush();
8746 EMSG(farsi_text_3); /* encoded in Farsi */
8747 return;
8748 }
8749#endif
8750
8751#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008752# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008753 set_vim_var_string(VV_INSERTMODE,
8754 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008755# ifdef FEAT_VREPLACE
8756 replaceState == VREPLACE ? "v" :
8757# endif
8758 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008759# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008760 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8761#endif
8762 if (State & REPLACE_FLAG)
8763 State = INSERT | (State & LANGMAP);
8764 else
8765 State = replaceState | (State & LANGMAP);
8766 AppendCharToRedobuff(K_INS);
8767 showmode();
8768#ifdef CURSOR_SHAPE
8769 ui_cursor_shape(); /* may show different cursor shape */
8770#endif
8771}
8772
8773/*
8774 * Pressed CTRL-O in Insert mode.
8775 */
8776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008777ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008778{
8779#ifdef FEAT_VREPLACE
8780 if (State & VREPLACE_FLAG)
8781 restart_edit = 'V';
8782 else
8783#endif
8784 if (State & REPLACE_FLAG)
8785 restart_edit = 'R';
8786 else
8787 restart_edit = 'I';
8788#ifdef FEAT_VIRTUALEDIT
8789 if (virtual_active())
8790 ins_at_eol = FALSE; /* cursor always keeps its column */
8791 else
8792#endif
8793 ins_at_eol = (gchar_cursor() == NUL);
8794}
8795
8796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 * If the cursor is on an indent, ^T/^D insert/delete one
8798 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008799 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 * with vi. But vi only supports ^T and ^D after an
8801 * autoindent, we support it everywhere.
8802 */
8803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008804ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
8806 if (stop_arrow() == FAIL)
8807 return;
8808 AppendCharToRedobuff(c);
8809
8810 /*
8811 * 0^D and ^^D: remove all indent.
8812 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008813 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8814 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 {
8816 --curwin->w_cursor.col;
8817 (void)del_char(FALSE); /* delete the '^' or '0' */
8818 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8819 if (State & REPLACE_FLAG)
8820 replace_pop_ins();
8821 if (lastc == '^')
8822 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008823 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 }
8825 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008826 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827
8828 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8829 did_ai = FALSE;
8830#ifdef FEAT_SMARTINDENT
8831 did_si = FALSE;
8832 can_si = FALSE;
8833 can_si_back = FALSE;
8834#endif
8835#ifdef FEAT_CINDENT
8836 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8837#endif
8838}
8839
8840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008841ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842{
8843 int temp;
8844
8845 if (stop_arrow() == FAIL)
8846 return;
8847 if (gchar_cursor() == NUL) /* delete newline */
8848 {
8849 temp = curwin->w_cursor.col;
8850 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008851 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008852 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 else
8854 curwin->w_cursor.col = temp;
8855 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008856 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8857 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 did_ai = FALSE;
8859#ifdef FEAT_SMARTINDENT
8860 did_si = FALSE;
8861 can_si = FALSE;
8862 can_si_back = FALSE;
8863#endif
8864 AppendCharToRedobuff(K_DEL);
8865}
8866
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008867static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008868
8869/*
8870 * Delete one character for ins_bs().
8871 */
8872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008873ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008874{
8875 dec_cursor();
8876 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8877 if (State & REPLACE_FLAG)
8878 {
8879 /* Don't delete characters before the insert point when in
8880 * Replace mode */
8881 if (curwin->w_cursor.lnum != Insstart.lnum
8882 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008883 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008884 }
8885 else
8886 (void)del_char(FALSE);
8887}
8888
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889/*
8890 * Handle Backspace, delete-word and delete-line in Insert mode.
8891 * Return TRUE when backspace was actually used.
8892 */
8893 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008894ins_bs(
8895 int c,
8896 int mode,
8897 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898{
8899 linenr_T lnum;
8900 int cc;
8901 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008902 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 colnr_T mincol;
8904 int did_backspace = FALSE;
8905 int in_indent;
8906 int oldState;
8907#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008908 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909#endif
8910
8911 /*
8912 * can't delete anything in an empty file
8913 * can't backup past first character in buffer
8914 * can't backup past starting point unless 'backspace' > 1
8915 * can backup to a previous line if 'backspace' == 0
8916 */
8917 if ( bufempty()
8918 || (
8919#ifdef FEAT_RIGHTLEFT
8920 !revins_on &&
8921#endif
8922 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8923 || (!can_bs(BS_START)
8924 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008925 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8926 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8928 && curwin->w_cursor.col <= ai_col)
8929 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8930 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008931 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 return FALSE;
8933 }
8934
8935 if (stop_arrow() == FAIL)
8936 return FALSE;
8937 in_indent = inindent(0);
8938#ifdef FEAT_CINDENT
8939 if (in_indent)
8940 can_cindent = FALSE;
8941#endif
8942#ifdef FEAT_COMMENTS
8943 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8944#endif
8945#ifdef FEAT_RIGHTLEFT
8946 if (revins_on) /* put cursor after last inserted char */
8947 inc_cursor();
8948#endif
8949
8950#ifdef FEAT_VIRTUALEDIT
8951 /* Virtualedit:
8952 * BACKSPACE_CHAR eats a virtual space
8953 * BACKSPACE_WORD eats all coladd
8954 * BACKSPACE_LINE eats all coladd and keeps going
8955 */
8956 if (curwin->w_cursor.coladd > 0)
8957 {
8958 if (mode == BACKSPACE_CHAR)
8959 {
8960 --curwin->w_cursor.coladd;
8961 return TRUE;
8962 }
8963 if (mode == BACKSPACE_WORD)
8964 {
8965 curwin->w_cursor.coladd = 0;
8966 return TRUE;
8967 }
8968 curwin->w_cursor.coladd = 0;
8969 }
8970#endif
8971
8972 /*
8973 * delete newline!
8974 */
8975 if (curwin->w_cursor.col == 0)
8976 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008977 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008978 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979#ifdef FEAT_RIGHTLEFT
8980 || revins_on
8981#endif
8982 )
8983 {
8984 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8985 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8986 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008987 --Insstart.lnum;
8988 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 }
8990 /*
8991 * In replace mode:
8992 * cc < 0: NL was inserted, delete it
8993 * cc >= 0: NL was replaced, put original characters back
8994 */
8995 cc = -1;
8996 if (State & REPLACE_FLAG)
8997 cc = replace_pop(); /* returns -1 if NL was inserted */
8998 /*
8999 * In replace mode, in the line we started replacing, we only move the
9000 * cursor.
9001 */
9002 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9003 {
9004 dec_cursor();
9005 }
9006 else
9007 {
9008#ifdef FEAT_VREPLACE
9009 if (!(State & VREPLACE_FLAG)
9010 || curwin->w_cursor.lnum > orig_line_count)
9011#endif
9012 {
9013 temp = gchar_cursor(); /* remember current char */
9014 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009015
9016 /* When "aw" is in 'formatoptions' we must delete the space at
9017 * the end of the line, otherwise the line will be broken
9018 * again when auto-formatting. */
9019 if (has_format_option(FO_AUTO)
9020 && has_format_option(FO_WHITE_PAR))
9021 {
9022 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9023 TRUE);
9024 int len;
9025
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009026 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009027 if (len > 0 && ptr[len - 1] == ' ')
9028 ptr[len - 1] = NUL;
9029 }
9030
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009031 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 if (temp == NUL && gchar_cursor() != NUL)
9033 inc_cursor();
9034 }
9035#ifdef FEAT_VREPLACE
9036 else
9037 dec_cursor();
9038#endif
9039
9040 /*
9041 * In REPLACE mode we have to put back the text that was replaced
9042 * by the NL. On the replace stack is first a NUL-terminated
9043 * sequence of characters that were deleted and then the
9044 * characters that NL replaced.
9045 */
9046 if (State & REPLACE_FLAG)
9047 {
9048 /*
9049 * Do the next ins_char() in NORMAL state, to
9050 * prevent ins_char() from replacing characters and
9051 * avoiding showmatch().
9052 */
9053 oldState = State;
9054 State = NORMAL;
9055 /*
9056 * restore characters (blanks) deleted after cursor
9057 */
9058 while (cc > 0)
9059 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009060 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061#ifdef FEAT_MBYTE
9062 mb_replace_pop_ins(cc);
9063#else
9064 ins_char(cc);
9065#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009066 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 cc = replace_pop();
9068 }
9069 /* restore the characters that NL replaced */
9070 replace_pop_ins();
9071 State = oldState;
9072 }
9073 }
9074 did_ai = FALSE;
9075 }
9076 else
9077 {
9078 /*
9079 * Delete character(s) before the cursor.
9080 */
9081#ifdef FEAT_RIGHTLEFT
9082 if (revins_on) /* put cursor on last inserted char */
9083 dec_cursor();
9084#endif
9085 mincol = 0;
9086 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009087 if (mode == BACKSPACE_LINE
9088 && (curbuf->b_p_ai
9089#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009090 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009091#endif
9092 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093#ifdef FEAT_RIGHTLEFT
9094 && !revins_on
9095#endif
9096 )
9097 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009098 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009100 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009102 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 }
9104
9105 /*
9106 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9107 */
9108 if ( mode == BACKSPACE_CHAR
9109 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009110 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009111 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 && (*(ml_get_cursor() - 1) == TAB
9113 || (*(ml_get_cursor() - 1) == ' '
9114 && (!*inserted_space_p
9115 || arrow_used))))))
9116 {
9117 int ts;
9118 colnr_T vcol;
9119 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009120 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121
9122 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009123 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009124 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009126 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 /* Compute the virtual column where we want to be. Since
9128 * 'showbreak' may get in the way, need to get the last column of
9129 * the previous character. */
9130 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009131 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 dec_cursor();
9133 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9134 inc_cursor();
9135 want_vcol = (want_vcol / ts) * ts;
9136
9137 /* delete characters until we are at or before want_vcol */
9138 while (vcol > want_vcol
9139 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009140 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141
9142 /* insert extra spaces until we are at want_vcol */
9143 while (vcol < want_vcol)
9144 {
9145 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009146 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9147 && curwin->w_cursor.col < Insstart_orig.col)
9148 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149
9150#ifdef FEAT_VREPLACE
9151 if (State & VREPLACE_FLAG)
9152 ins_char(' ');
9153 else
9154#endif
9155 {
9156 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009157 if ((State & REPLACE_FLAG))
9158 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 }
9160 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9161 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009162
9163 /* If we are now back where we started delete one character. Can
9164 * happen when using 'sts' and 'linebreak'. */
9165 if (vcol >= start_vcol)
9166 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 }
9168
9169 /*
9170 * Delete upto starting point, start of line or previous word.
9171 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009172 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009174#ifdef FEAT_MBYTE
9175 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009177 if (has_mbyte)
9178 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009180 do
9181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009183 if (!revins_on) /* put cursor on char to be deleted */
9184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009186
9187 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009189 /* look multi-byte character class */
9190 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009192 prev_cclass = cclass;
9193 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009196
9197 /* start of word? */
9198 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9199 {
9200 mode = BACKSPACE_WORD_NOT_SPACE;
9201 temp = vim_iswordc(cc);
9202 }
9203 /* end of word? */
9204 else if (mode == BACKSPACE_WORD_NOT_SPACE
9205 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9206#ifdef FEAT_MBYTE
9207 || prev_cclass != cclass
9208#endif
9209 ))
9210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009212 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009214 inc_cursor();
9215#ifdef FEAT_RIGHTLEFT
9216 else if (State & REPLACE_FLAG)
9217 dec_cursor();
9218#endif
9219 break;
9220 }
9221 if (State & REPLACE_FLAG)
9222 replace_do_bs(-1);
9223 else
9224 {
9225#ifdef FEAT_MBYTE
9226 if (enc_utf8 && p_deco)
9227 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9228#endif
9229 (void)del_char(FALSE);
9230#ifdef FEAT_MBYTE
9231 /*
9232 * If there are combining characters and 'delcombine' is set
9233 * move the cursor back. Don't back up before the base
9234 * character.
9235 */
9236 if (enc_utf8 && p_deco && cpc[0] != NUL)
9237 inc_cursor();
9238#endif
9239#ifdef FEAT_RIGHTLEFT
9240 if (revins_chars)
9241 {
9242 revins_chars--;
9243 revins_legal++;
9244 }
9245 if (revins_on && gchar_cursor() == NUL)
9246 break;
9247#endif
9248 }
9249 /* Just a single backspace?: */
9250 if (mode == BACKSPACE_CHAR)
9251 break;
9252 } while (
9253#ifdef FEAT_RIGHTLEFT
9254 revins_on ||
9255#endif
9256 (curwin->w_cursor.col > mincol
9257 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9258 || curwin->w_cursor.col != Insstart_orig.col)));
9259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 did_backspace = TRUE;
9261 }
9262#ifdef FEAT_SMARTINDENT
9263 did_si = FALSE;
9264 can_si = FALSE;
9265 can_si_back = FALSE;
9266#endif
9267 if (curwin->w_cursor.col <= 1)
9268 did_ai = FALSE;
9269 /*
9270 * It's a little strange to put backspaces into the redo
9271 * buffer, but it makes auto-indent a lot easier to deal
9272 * with.
9273 */
9274 AppendCharToRedobuff(c);
9275
9276 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009277 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9278 && curwin->w_cursor.col < Insstart_orig.col)
9279 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280
9281 /* vi behaviour: the cursor moves backward but the character that
9282 * was there remains visible
9283 * Vim behaviour: the cursor moves backward and the character that
9284 * was there is erased from the screen.
9285 * We can emulate the vi behaviour by pretending there is a dollar
9286 * displayed even when there isn't.
9287 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009288 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 dollar_vcol = curwin->w_virtcol;
9290
Bram Moolenaarce3be472008-01-14 19:12:28 +00009291#ifdef FEAT_FOLDING
9292 /* When deleting a char the cursor line must never be in a closed fold.
9293 * E.g., when 'foldmethod' is indent and deleting the first non-white
9294 * char before a Tab. */
9295 if (did_backspace)
9296 foldOpenCursor();
9297#endif
9298
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 return did_backspace;
9300}
9301
9302#ifdef FEAT_MOUSE
9303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009304ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305{
9306 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009307 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308
9309# ifdef FEAT_GUI
9310 /* When GUI is active, also move/paste when 'mouse' is empty */
9311 if (!gui.in_use)
9312# endif
9313 if (!mouse_has(MOUSE_INSERT))
9314 return;
9315
9316 undisplay_dollar();
9317 tpos = curwin->w_cursor;
9318 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9319 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009320#ifdef FEAT_WINDOWS
9321 win_T *new_curwin = curwin;
9322
9323 if (curwin != old_curwin && win_valid(old_curwin))
9324 {
9325 /* Mouse took us to another window. We need to go back to the
9326 * previous one to stop insert there properly. */
9327 curwin = old_curwin;
9328 curbuf = curwin->w_buffer;
9329 }
9330#endif
9331 start_arrow(curwin == old_curwin ? &tpos : NULL);
9332#ifdef FEAT_WINDOWS
9333 if (curwin != new_curwin && win_valid(new_curwin))
9334 {
9335 curwin = new_curwin;
9336 curbuf = curwin->w_buffer;
9337 }
9338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339# ifdef FEAT_CINDENT
9340 can_cindent = TRUE;
9341# endif
9342 }
9343
9344#ifdef FEAT_WINDOWS
9345 /* redraw status lines (in case another window became active) */
9346 redraw_statuslines();
9347#endif
9348}
9349
9350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009351ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352{
9353 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009354# if defined(FEAT_WINDOWS)
9355 win_T *old_curwin = curwin;
9356# endif
9357# ifdef FEAT_INS_EXPAND
9358 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359# endif
9360
9361 tpos = curwin->w_cursor;
9362
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009363# ifdef FEAT_WINDOWS
9364 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 {
9366 int row, col;
9367
9368 row = mouse_row;
9369 col = mouse_col;
9370
9371 /* find the window at the pointer coordinates */
9372 curwin = mouse_find_win(&row, &col);
9373 curbuf = curwin->w_buffer;
9374 }
9375 if (curwin == old_curwin)
9376# endif
9377 undisplay_dollar();
9378
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009379# ifdef FEAT_INS_EXPAND
9380 /* Don't scroll the window in which completion is being done. */
9381 if (!pum_visible()
9382# if defined(FEAT_WINDOWS)
9383 || curwin != old_curwin
9384# endif
9385 )
9386# endif
9387 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009388 if (dir == MSCR_DOWN || dir == MSCR_UP)
9389 {
9390 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9391 scroll_redraw(dir,
9392 (long)(curwin->w_botline - curwin->w_topline));
9393 else
9394 scroll_redraw(dir, 3L);
9395 }
9396#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009397 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009398 {
9399 int val, step = 6;
9400
9401 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9402 step = W_WIDTH(curwin);
9403 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9404 if (val < 0)
9405 val = 0;
9406 gui_do_horiz_scroll(val, TRUE);
9407 }
9408#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009409# ifdef FEAT_INS_EXPAND
9410 did_scroll = TRUE;
9411# endif
9412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009414# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 curwin->w_redr_status = TRUE;
9416
9417 curwin = old_curwin;
9418 curbuf = curwin->w_buffer;
9419# endif
9420
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009421# ifdef FEAT_INS_EXPAND
9422 /* The popup menu may overlay the window, need to redraw it.
9423 * TODO: Would be more efficient to only redraw the windows that are
9424 * overlapped by the popup menu. */
9425 if (pum_visible() && did_scroll)
9426 {
9427 redraw_all_later(NOT_VALID);
9428 ins_compl_show_pum();
9429 }
9430# endif
9431
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 if (!equalpos(curwin->w_cursor, tpos))
9433 {
9434 start_arrow(&tpos);
9435# ifdef FEAT_CINDENT
9436 can_cindent = TRUE;
9437# endif
9438 }
9439}
9440#endif
9441
Bram Moolenaarec2da362017-01-21 20:04:22 +01009442/*
9443 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9444 * P_PE literally.
9445 * When "drop" is TRUE then consume the text and drop it.
9446 */
9447 int
9448bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9449{
9450 int c;
9451 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9452 int idx = 0;
9453 char_u *end = find_termcode((char_u *)"PE");
9454 int ret_char = -1;
9455 int save_allow_keys = allow_keys;
9456
9457 /* If the end code is too long we can't detect it, read everything. */
9458 if (STRLEN(end) >= NUMBUFLEN)
9459 end = NULL;
9460 ++no_mapping;
9461 allow_keys = 0;
9462 for (;;)
9463 {
9464 /* When the end is not defined read everything. */
9465 if (end == NULL && vpeekc() == NUL)
9466 break;
9467 c = plain_vgetc();
9468#ifdef FEAT_MBYTE
9469 if (has_mbyte)
9470 idx += (*mb_char2bytes)(c, buf + idx);
9471 else
9472#endif
9473 buf[idx++] = c;
9474 buf[idx] = NUL;
9475 if (end != NUL && STRNCMP(buf, end, idx) == 0)
9476 {
9477 if (end[idx] == NUL)
9478 break; /* Found the end of paste code. */
9479 continue;
9480 }
9481 if (!drop)
9482 {
9483 switch (mode)
9484 {
9485 case PASTE_CMDLINE:
9486 put_on_cmdline(buf, idx, TRUE);
9487 break;
9488
9489 case PASTE_EX:
9490 if (gap != NULL && ga_grow(gap, idx) == OK)
9491 {
9492 mch_memmove((char *)gap->ga_data + gap->ga_len,
9493 buf, (size_t)idx);
9494 gap->ga_len += idx;
9495 }
9496 break;
9497
9498 case PASTE_INSERT:
9499 if (stop_arrow() == OK)
9500 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009501 c = buf[0];
9502 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9503 ins_eol(c);
9504 else
9505 ins_char_bytes(buf, idx);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009506 AppendToRedobuffLit(buf, idx);
9507 }
9508 break;
9509
9510 case PASTE_ONE_CHAR:
9511 if (ret_char == -1)
9512 {
9513#ifdef FEAT_MBYTE
9514 if (has_mbyte)
9515 ret_char = (*mb_ptr2char)(buf);
9516 else
9517#endif
9518 ret_char = buf[0];
9519 }
9520 break;
9521 }
9522 }
9523 idx = 0;
9524 }
9525 --no_mapping;
9526 allow_keys = save_allow_keys;
9527
9528 return ret_char;
9529}
9530
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009531#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009533ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009534{
9535 /* We will be leaving the current window, unless closing another tab. */
9536 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9537 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9538 {
9539 undisplay_dollar();
9540 start_arrow(&curwin->w_cursor);
9541# ifdef FEAT_CINDENT
9542 can_cindent = TRUE;
9543# endif
9544 }
9545
9546 if (c == K_TABLINE)
9547 goto_tabpage(current_tab);
9548 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009549 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009550 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009551 redraw_statuslines(); /* will redraw the tabline when needed */
9552 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009553}
9554#endif
9555
9556#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009558ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559{
9560 pos_T tpos;
9561
9562 undisplay_dollar();
9563 tpos = curwin->w_cursor;
9564 if (gui_do_scroll())
9565 {
9566 start_arrow(&tpos);
9567# ifdef FEAT_CINDENT
9568 can_cindent = TRUE;
9569# endif
9570 }
9571}
9572
9573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009574ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575{
9576 pos_T tpos;
9577
9578 undisplay_dollar();
9579 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009580 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 {
9582 start_arrow(&tpos);
9583# ifdef FEAT_CINDENT
9584 can_cindent = TRUE;
9585# endif
9586 }
9587}
9588#endif
9589
9590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009591ins_left(
9592 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
9594 pos_T tpos;
9595
9596#ifdef FEAT_FOLDING
9597 if ((fdo_flags & FDO_HOR) && KeyTyped)
9598 foldOpenCursor();
9599#endif
9600 undisplay_dollar();
9601 tpos = curwin->w_cursor;
9602 if (oneleft() == OK)
9603 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009604#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9605 /* Only call start_arrow() when not busy with preediting, it will
9606 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9607 if (!im_is_preediting())
9608#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009609 {
9610 start_arrow_with_change(&tpos, end_change);
9611 if (!end_change)
9612 AppendCharToRedobuff(K_LEFT);
9613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614#ifdef FEAT_RIGHTLEFT
9615 /* If exit reversed string, position is fixed */
9616 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9617 revins_legal++;
9618 revins_chars++;
9619#endif
9620 }
9621
9622 /*
9623 * if 'whichwrap' set for cursor in insert mode may go to
9624 * previous line
9625 */
9626 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9627 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009628 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 start_arrow(&tpos);
9630 --(curwin->w_cursor.lnum);
9631 coladvance((colnr_T)MAXCOL);
9632 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9633 }
9634 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009635 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009636 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637}
9638
9639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009640ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
9642 pos_T tpos;
9643
9644#ifdef FEAT_FOLDING
9645 if ((fdo_flags & FDO_HOR) && KeyTyped)
9646 foldOpenCursor();
9647#endif
9648 undisplay_dollar();
9649 tpos = curwin->w_cursor;
9650 if (c == K_C_HOME)
9651 curwin->w_cursor.lnum = 1;
9652 curwin->w_cursor.col = 0;
9653#ifdef FEAT_VIRTUALEDIT
9654 curwin->w_cursor.coladd = 0;
9655#endif
9656 curwin->w_curswant = 0;
9657 start_arrow(&tpos);
9658}
9659
9660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009661ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662{
9663 pos_T tpos;
9664
9665#ifdef FEAT_FOLDING
9666 if ((fdo_flags & FDO_HOR) && KeyTyped)
9667 foldOpenCursor();
9668#endif
9669 undisplay_dollar();
9670 tpos = curwin->w_cursor;
9671 if (c == K_C_END)
9672 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9673 coladvance((colnr_T)MAXCOL);
9674 curwin->w_curswant = MAXCOL;
9675
9676 start_arrow(&tpos);
9677}
9678
9679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009680ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681{
9682#ifdef FEAT_FOLDING
9683 if ((fdo_flags & FDO_HOR) && KeyTyped)
9684 foldOpenCursor();
9685#endif
9686 undisplay_dollar();
9687 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9688 {
9689 start_arrow(&curwin->w_cursor);
9690 (void)bck_word(1L, FALSE, FALSE);
9691 curwin->w_set_curswant = TRUE;
9692 }
9693 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009694 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695}
9696
9697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009698ins_right(
9699 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700{
9701#ifdef FEAT_FOLDING
9702 if ((fdo_flags & FDO_HOR) && KeyTyped)
9703 foldOpenCursor();
9704#endif
9705 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009706 if (gchar_cursor() != NUL
9707#ifdef FEAT_VIRTUALEDIT
9708 || virtual_active()
9709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 )
9711 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009712 start_arrow_with_change(&curwin->w_cursor, end_change);
9713 if (!end_change)
9714 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 curwin->w_set_curswant = TRUE;
9716#ifdef FEAT_VIRTUALEDIT
9717 if (virtual_active())
9718 oneright();
9719 else
9720#endif
9721 {
9722#ifdef FEAT_MBYTE
9723 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009724 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 else
9726#endif
9727 ++curwin->w_cursor.col;
9728 }
9729
9730#ifdef FEAT_RIGHTLEFT
9731 revins_legal++;
9732 if (revins_chars)
9733 revins_chars--;
9734#endif
9735 }
9736 /* if 'whichwrap' set for cursor in insert mode, may move the
9737 * cursor to the next line */
9738 else if (vim_strchr(p_ww, ']') != NULL
9739 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9740 {
9741 start_arrow(&curwin->w_cursor);
9742 curwin->w_set_curswant = TRUE;
9743 ++curwin->w_cursor.lnum;
9744 curwin->w_cursor.col = 0;
9745 }
9746 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009747 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009748 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749}
9750
9751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009752ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753{
9754#ifdef FEAT_FOLDING
9755 if ((fdo_flags & FDO_HOR) && KeyTyped)
9756 foldOpenCursor();
9757#endif
9758 undisplay_dollar();
9759 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9760 || gchar_cursor() != NUL)
9761 {
9762 start_arrow(&curwin->w_cursor);
9763 (void)fwd_word(1L, FALSE, 0);
9764 curwin->w_set_curswant = TRUE;
9765 }
9766 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009767 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768}
9769
9770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009771ins_up(
9772 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773{
9774 pos_T tpos;
9775 linenr_T old_topline = curwin->w_topline;
9776#ifdef FEAT_DIFF
9777 int old_topfill = curwin->w_topfill;
9778#endif
9779
9780 undisplay_dollar();
9781 tpos = curwin->w_cursor;
9782 if (cursor_up(1L, TRUE) == OK)
9783 {
9784 if (startcol)
9785 coladvance(getvcol_nolist(&Insstart));
9786 if (old_topline != curwin->w_topline
9787#ifdef FEAT_DIFF
9788 || old_topfill != curwin->w_topfill
9789#endif
9790 )
9791 redraw_later(VALID);
9792 start_arrow(&tpos);
9793#ifdef FEAT_CINDENT
9794 can_cindent = TRUE;
9795#endif
9796 }
9797 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009798 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799}
9800
9801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009802ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803{
9804 pos_T tpos;
9805
9806 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009807
9808#ifdef FEAT_WINDOWS
9809 if (mod_mask & MOD_MASK_CTRL)
9810 {
9811 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009812 if (first_tabpage->tp_next != NULL)
9813 {
9814 start_arrow(&curwin->w_cursor);
9815 goto_tabpage(-1);
9816 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009817 return;
9818 }
9819#endif
9820
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 tpos = curwin->w_cursor;
9822 if (onepage(BACKWARD, 1L) == OK)
9823 {
9824 start_arrow(&tpos);
9825#ifdef FEAT_CINDENT
9826 can_cindent = TRUE;
9827#endif
9828 }
9829 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009830 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831}
9832
9833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009834ins_down(
9835 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837 pos_T tpos;
9838 linenr_T old_topline = curwin->w_topline;
9839#ifdef FEAT_DIFF
9840 int old_topfill = curwin->w_topfill;
9841#endif
9842
9843 undisplay_dollar();
9844 tpos = curwin->w_cursor;
9845 if (cursor_down(1L, TRUE) == OK)
9846 {
9847 if (startcol)
9848 coladvance(getvcol_nolist(&Insstart));
9849 if (old_topline != curwin->w_topline
9850#ifdef FEAT_DIFF
9851 || old_topfill != curwin->w_topfill
9852#endif
9853 )
9854 redraw_later(VALID);
9855 start_arrow(&tpos);
9856#ifdef FEAT_CINDENT
9857 can_cindent = TRUE;
9858#endif
9859 }
9860 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009861 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862}
9863
9864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009865ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
9867 pos_T tpos;
9868
9869 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009870
9871#ifdef FEAT_WINDOWS
9872 if (mod_mask & MOD_MASK_CTRL)
9873 {
9874 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009875 if (first_tabpage->tp_next != NULL)
9876 {
9877 start_arrow(&curwin->w_cursor);
9878 goto_tabpage(0);
9879 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009880 return;
9881 }
9882#endif
9883
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 tpos = curwin->w_cursor;
9885 if (onepage(FORWARD, 1L) == OK)
9886 {
9887 start_arrow(&tpos);
9888#ifdef FEAT_CINDENT
9889 can_cindent = TRUE;
9890#endif
9891 }
9892 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009893 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894}
9895
9896#ifdef FEAT_DND
9897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009898ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899{
9900 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9901}
9902#endif
9903
9904/*
9905 * Handle TAB in Insert or Replace mode.
9906 * Return TRUE when the TAB needs to be inserted like a normal character.
9907 */
9908 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009909ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910{
9911 int ind;
9912 int i;
9913 int temp;
9914
9915 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9916 Insstart_blank_vcol = get_nolist_virtcol();
9917 if (echeck_abbr(TAB + ABBR_OFF))
9918 return FALSE;
9919
9920 ind = inindent(0);
9921#ifdef FEAT_CINDENT
9922 if (ind)
9923 can_cindent = FALSE;
9924#endif
9925
9926 /*
9927 * When nothing special, insert TAB like a normal character
9928 */
9929 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009930 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009931 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 return TRUE;
9933
9934 if (stop_arrow() == FAIL)
9935 return TRUE;
9936
9937 did_ai = FALSE;
9938#ifdef FEAT_SMARTINDENT
9939 did_si = FALSE;
9940 can_si = FALSE;
9941 can_si_back = FALSE;
9942#endif
9943 AppendToRedobuff((char_u *)"\t");
9944
9945 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009946 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009947 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9948 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 else /* otherwise use 'tabstop' */
9950 temp = (int)curbuf->b_p_ts;
9951 temp -= get_nolist_virtcol() % temp;
9952
9953 /*
9954 * Insert the first space with ins_char(). It will delete one char in
9955 * replace mode. Insert the rest with ins_str(); it will not delete any
9956 * chars. For VREPLACE mode, we use ins_char() for all characters.
9957 */
9958 ins_char(' ');
9959 while (--temp > 0)
9960 {
9961#ifdef FEAT_VREPLACE
9962 if (State & VREPLACE_FLAG)
9963 ins_char(' ');
9964 else
9965#endif
9966 {
9967 ins_str((char_u *)" ");
9968 if (State & REPLACE_FLAG) /* no char replaced */
9969 replace_push(NUL);
9970 }
9971 }
9972
9973 /*
9974 * When 'expandtab' not set: Replace spaces by TABs where possible.
9975 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009976 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 {
9978 char_u *ptr;
9979#ifdef FEAT_VREPLACE
9980 char_u *saved_line = NULL; /* init for GCC */
9981 pos_T pos;
9982#endif
9983 pos_T fpos;
9984 pos_T *cursor;
9985 colnr_T want_vcol, vcol;
9986 int change_col = -1;
9987 int save_list = curwin->w_p_list;
9988
9989 /*
9990 * Get the current line. For VREPLACE mode, don't make real changes
9991 * yet, just work on a copy of the line.
9992 */
9993#ifdef FEAT_VREPLACE
9994 if (State & VREPLACE_FLAG)
9995 {
9996 pos = curwin->w_cursor;
9997 cursor = &pos;
9998 saved_line = vim_strsave(ml_get_curline());
9999 if (saved_line == NULL)
10000 return FALSE;
10001 ptr = saved_line + pos.col;
10002 }
10003 else
10004#endif
10005 {
10006 ptr = ml_get_cursor();
10007 cursor = &curwin->w_cursor;
10008 }
10009
10010 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10011 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10012 curwin->w_p_list = FALSE;
10013
10014 /* Find first white before the cursor */
10015 fpos = curwin->w_cursor;
10016 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
10017 {
10018 --fpos.col;
10019 --ptr;
10020 }
10021
10022 /* In Replace mode, don't change characters before the insert point. */
10023 if ((State & REPLACE_FLAG)
10024 && fpos.lnum == Insstart.lnum
10025 && fpos.col < Insstart.col)
10026 {
10027 ptr += Insstart.col - fpos.col;
10028 fpos.col = Insstart.col;
10029 }
10030
10031 /* compute virtual column numbers of first white and cursor */
10032 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10033 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10034
Bram Moolenaar597a4222014-06-25 14:39:50 +020010035 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10036 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 while (vim_iswhite(*ptr))
10038 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010039 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 if (vcol + i > want_vcol)
10041 break;
10042 if (*ptr != TAB)
10043 {
10044 *ptr = TAB;
10045 if (change_col < 0)
10046 {
10047 change_col = fpos.col; /* Column of first change */
10048 /* May have to adjust Insstart */
10049 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10050 Insstart.col = fpos.col;
10051 }
10052 }
10053 ++fpos.col;
10054 ++ptr;
10055 vcol += i;
10056 }
10057
10058 if (change_col >= 0)
10059 {
10060 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010061 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062
10063 /* Skip over the spaces we need. */
10064 while (vcol < want_vcol && *ptr == ' ')
10065 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010066 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 ++ptr;
10068 ++repl_off;
10069 }
10070 if (vcol > want_vcol)
10071 {
10072 /* Must have a char with 'showbreak' just before it. */
10073 --ptr;
10074 --repl_off;
10075 }
10076 fpos.col += repl_off;
10077
10078 /* Delete following spaces. */
10079 i = cursor->col - fpos.col;
10080 if (i > 0)
10081 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010082 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 /* correct replace stack. */
10084 if ((State & REPLACE_FLAG)
10085#ifdef FEAT_VREPLACE
10086 && !(State & VREPLACE_FLAG)
10087#endif
10088 )
10089 for (temp = i; --temp >= 0; )
10090 replace_join(repl_off);
10091 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010092#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010093 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010094 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010095 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010096 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10097 (char_u *)"\t", 1);
10098 }
10099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 cursor->col -= i;
10101
10102#ifdef FEAT_VREPLACE
10103 /*
10104 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10105 * backspacing over the changed spacing and then inserting the new
10106 * spacing.
10107 */
10108 if (State & VREPLACE_FLAG)
10109 {
10110 /* Backspace from real cursor to change_col */
10111 backspace_until_column(change_col);
10112
10113 /* Insert each char in saved_line from changed_col to
10114 * ptr-cursor */
10115 ins_bytes_len(saved_line + change_col,
10116 cursor->col - change_col);
10117 }
10118#endif
10119 }
10120
10121#ifdef FEAT_VREPLACE
10122 if (State & VREPLACE_FLAG)
10123 vim_free(saved_line);
10124#endif
10125 curwin->w_p_list = save_list;
10126 }
10127
10128 return FALSE;
10129}
10130
10131/*
10132 * Handle CR or NL in insert mode.
10133 * Return TRUE when out of memory or can't undo.
10134 */
10135 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010136ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137{
10138 int i;
10139
10140 if (echeck_abbr(c + ABBR_OFF))
10141 return FALSE;
10142 if (stop_arrow() == FAIL)
10143 return TRUE;
10144 undisplay_dollar();
10145
10146 /*
10147 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10148 * character under the cursor. Only push a NUL on the replace stack,
10149 * nothing to put back when the NL is deleted.
10150 */
10151 if ((State & REPLACE_FLAG)
10152#ifdef FEAT_VREPLACE
10153 && !(State & VREPLACE_FLAG)
10154#endif
10155 )
10156 replace_push(NUL);
10157
10158 /*
10159 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10160 * replacing the next line, so we push all of the characters left on the
10161 * line onto the replace stack. This is not done here though, it is done
10162 * in open_line().
10163 */
10164
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010165#ifdef FEAT_VIRTUALEDIT
10166 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10167 * CTRL-O). */
10168 if (virtual_active() && curwin->w_cursor.coladd > 0)
10169 coladvance(getviscol());
10170#endif
10171
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172#ifdef FEAT_RIGHTLEFT
10173# ifdef FEAT_FKMAP
10174 if (p_altkeymap && p_fkmap)
10175 fkmap(NL);
10176# endif
10177 /* NL in reverse insert will always start in the end of
10178 * current line. */
10179 if (revins_on)
10180 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10181#endif
10182
10183 AppendToRedobuff(NL_STR);
10184 i = open_line(FORWARD,
10185#ifdef FEAT_COMMENTS
10186 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10187#endif
10188 0, old_indent);
10189 old_indent = 0;
10190#ifdef FEAT_CINDENT
10191 can_cindent = TRUE;
10192#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010193#ifdef FEAT_FOLDING
10194 /* When inserting a line the cursor line must never be in a closed fold. */
10195 foldOpenCursor();
10196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197
10198 return (!i);
10199}
10200
10201#ifdef FEAT_DIGRAPHS
10202/*
10203 * Handle digraph in insert mode.
10204 * Returns character still to be inserted, or NUL when nothing remaining to be
10205 * done.
10206 */
10207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010208ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209{
10210 int c;
10211 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010212 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213
10214 pc_status = PC_STATUS_UNSET;
10215 if (redrawing() && !char_avail())
10216 {
10217 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010218 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219
10220 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010221 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222#ifdef FEAT_CMDL_INFO
10223 add_to_showcmd_c(Ctrl_K);
10224#endif
10225 }
10226
10227#ifdef USE_ON_FLY_SCROLL
10228 dont_scroll = TRUE; /* disallow scrolling here */
10229#endif
10230
10231 /* don't map the digraph chars. This also prevents the
10232 * mode message to be deleted when ESC is hit */
10233 ++no_mapping;
10234 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010235 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 --no_mapping;
10237 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010238 if (did_putchar)
10239 /* when the line fits in 'columns' the '?' is at the start of the next
10240 * line and will not be removed by the redraw */
10241 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010242
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 if (IS_SPECIAL(c) || mod_mask) /* special key */
10244 {
10245#ifdef FEAT_CMDL_INFO
10246 clear_showcmd();
10247#endif
10248 insert_special(c, TRUE, FALSE);
10249 return NUL;
10250 }
10251 if (c != ESC)
10252 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010253 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 if (redrawing() && !char_avail())
10255 {
10256 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010257 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258
10259 if (char2cells(c) == 1)
10260 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010261 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010263 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 }
10265#ifdef FEAT_CMDL_INFO
10266 add_to_showcmd_c(c);
10267#endif
10268 }
10269 ++no_mapping;
10270 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010271 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 --no_mapping;
10273 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010274 if (did_putchar)
10275 /* when the line fits in 'columns' the '?' is at the start of the
10276 * next line and will not be removed by a redraw */
10277 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 if (cc != ESC)
10279 {
10280 AppendToRedobuff((char_u *)CTRL_V_STR);
10281 c = getdigraph(c, cc, TRUE);
10282#ifdef FEAT_CMDL_INFO
10283 clear_showcmd();
10284#endif
10285 return c;
10286 }
10287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288#ifdef FEAT_CMDL_INFO
10289 clear_showcmd();
10290#endif
10291 return NUL;
10292}
10293#endif
10294
10295/*
10296 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10297 * Returns the char to be inserted, or NUL if none found.
10298 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010299 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010300ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301{
10302 int c;
10303 int temp;
10304 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010305 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306
10307 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10308 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010309 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 return NUL;
10311 }
10312
10313 /* try to advance to the cursor column */
10314 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010315 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 prev_ptr = ptr;
10317 validate_virtcol();
10318 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10319 {
10320 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010321 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 }
10323 if ((colnr_T)temp > curwin->w_virtcol)
10324 ptr = prev_ptr;
10325
10326#ifdef FEAT_MBYTE
10327 c = (*mb_ptr2char)(ptr);
10328#else
10329 c = *ptr;
10330#endif
10331 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010332 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 return c;
10334}
10335
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010336/*
10337 * CTRL-Y or CTRL-E typed in Insert mode.
10338 */
10339 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010340ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010341{
10342 int c = tc;
10343
10344#ifdef FEAT_INS_EXPAND
10345 if (ctrl_x_mode == CTRL_X_SCROLL)
10346 {
10347 if (c == Ctrl_Y)
10348 scrolldown_clamp();
10349 else
10350 scrollup_clamp();
10351 redraw_later(VALID);
10352 }
10353 else
10354#endif
10355 {
10356 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10357 if (c != NUL)
10358 {
10359 long tw_save;
10360
10361 /* The character must be taken literally, insert like it
10362 * was typed after a CTRL-V, and pretend 'textwidth'
10363 * wasn't set. Digits, 'o' and 'x' are special after a
10364 * CTRL-V, don't use it for these. */
10365 if (c < 256 && !isalnum(c))
10366 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10367 tw_save = curbuf->b_p_tw;
10368 curbuf->b_p_tw = -1;
10369 insert_special(c, TRUE, FALSE);
10370 curbuf->b_p_tw = tw_save;
10371#ifdef FEAT_RIGHTLEFT
10372 revins_chars++;
10373 revins_legal++;
10374#endif
10375 c = Ctrl_V; /* pretend CTRL-V is last character */
10376 auto_format(FALSE, TRUE);
10377 }
10378 }
10379 return c;
10380}
10381
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382#ifdef FEAT_SMARTINDENT
10383/*
10384 * Try to do some very smart auto-indenting.
10385 * Used when inserting a "normal" character.
10386 */
10387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010388ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389{
10390 pos_T *pos, old_pos;
10391 char_u *ptr;
10392 int i;
10393 int temp;
10394
10395 /*
10396 * do some very smart indenting when entering '{' or '}'
10397 */
10398 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10399 {
10400 /*
10401 * for '}' set indent equal to indent of line containing matching '{'
10402 */
10403 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10404 {
10405 old_pos = curwin->w_cursor;
10406 /*
10407 * If the matching '{' has a ')' immediately before it (ignoring
10408 * white-space), then line up with the start of the line
10409 * containing the matching '(' if there is one. This handles the
10410 * case where an "if (..\n..) {" statement continues over multiple
10411 * lines -- webb
10412 */
10413 ptr = ml_get(pos->lnum);
10414 i = pos->col;
10415 if (i > 0) /* skip blanks before '{' */
10416 while (--i > 0 && vim_iswhite(ptr[i]))
10417 ;
10418 curwin->w_cursor.lnum = pos->lnum;
10419 curwin->w_cursor.col = i;
10420 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10421 curwin->w_cursor = *pos;
10422 i = get_indent();
10423 curwin->w_cursor = old_pos;
10424#ifdef FEAT_VREPLACE
10425 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010426 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 else
10428#endif
10429 (void)set_indent(i, SIN_CHANGED);
10430 }
10431 else if (curwin->w_cursor.col > 0)
10432 {
10433 /*
10434 * when inserting '{' after "O" reduce indent, but not
10435 * more than indent of previous line
10436 */
10437 temp = TRUE;
10438 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10439 {
10440 old_pos = curwin->w_cursor;
10441 i = get_indent();
10442 while (curwin->w_cursor.lnum > 1)
10443 {
10444 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10445
10446 /* ignore empty lines and lines starting with '#'. */
10447 if (*ptr != '#' && *ptr != NUL)
10448 break;
10449 }
10450 if (get_indent() >= i)
10451 temp = FALSE;
10452 curwin->w_cursor = old_pos;
10453 }
10454 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010455 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 }
10457 }
10458
10459 /*
10460 * set indent of '#' always to 0
10461 */
10462 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10463 {
10464 /* remember current indent for next line */
10465 old_indent = get_indent();
10466 (void)set_indent(0, SIN_CHANGED);
10467 }
10468
10469 /* Adjust ai_col, the char at this position can be deleted. */
10470 if (ai_col > curwin->w_cursor.col)
10471 ai_col = curwin->w_cursor.col;
10472}
10473#endif
10474
10475/*
10476 * Get the value that w_virtcol would have when 'list' is off.
10477 * Unless 'cpo' contains the 'L' flag.
10478 */
10479 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010480get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481{
10482 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10483 return getvcol_nolist(&curwin->w_cursor);
10484 validate_virtcol();
10485 return curwin->w_virtcol;
10486}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010487
10488#ifdef FEAT_AUTOCMD
10489/*
10490 * Handle the InsertCharPre autocommand.
10491 * "c" is the character that was typed.
10492 * Return a pointer to allocated memory with the replacement string.
10493 * Return NULL to continue inserting "c".
10494 */
10495 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010496do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010497{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010498 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010499 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010500
10501 /* Return quickly when there is nothing to do. */
10502 if (!has_insertcharpre())
10503 return NULL;
10504
Bram Moolenaar704984a2012-06-01 14:57:51 +020010505#ifdef FEAT_MBYTE
10506 if (has_mbyte)
10507 buf[(*mb_char2bytes)(c, buf)] = NUL;
10508 else
10509#endif
10510 {
10511 buf[0] = c;
10512 buf[1] = NUL;
10513 }
10514
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010515 /* Lock the text to avoid weird things from happening. */
10516 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010517 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010518
Bram Moolenaar704984a2012-06-01 14:57:51 +020010519 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010520 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010521 {
10522 /* Get the value of v:char. It may be empty or more than one
10523 * character. Only use it when changed, otherwise continue with the
10524 * original character to avoid breaking autoindent. */
10525 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10526 res = vim_strsave(get_vim_var_str(VV_CHAR));
10527 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010528
10529 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10530 --textlock;
10531
10532 return res;
10533}
10534#endif