blob: 0d9e9d4a48064a3a3730c25be6d47e9cf2131b28 [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
312 * 'R' replace command
313 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
314 * but still only one <CR> is inserted. The <Esc> is not used for redo.
315 * 'g' "gI" command.
316 * 'V' "gR" command for Virtual Replace mode.
317 * 'v' "gr" command for single character Virtual Replace mode.
318 *
319 * This function is not called recursively. For CTRL-O commands, it returns
320 * and lets the caller handle the Normal-mode command.
321 *
322 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
323 */
324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100325edit(
326 int cmdchar,
327 int startln, /* if set, insert at start of line */
328 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329{
330 int c = 0;
331 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100332 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000333 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 int i;
336 int did_backspace = TRUE; /* previous char was backspace */
337#ifdef FEAT_CINDENT
338 int line_is_white = FALSE; /* line is empty before insert */
339#endif
340 linenr_T old_topline = 0; /* topline before insertion */
341#ifdef FEAT_DIFF
342 int old_topfill = -1;
343#endif
344 int inserted_space = FALSE; /* just inserted a space */
345 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000346 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000348 /* Remember whether editing was restarted after CTRL-O. */
349 did_restart_edit = restart_edit;
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 /* sleep before redrawing, needed for "CTRL-O :" that results in an
352 * error message */
353 check_for_delay(TRUE);
354
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100355 /* set Insstart_orig to Insstart */
356 update_Insstart_orig = TRUE;
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifdef HAVE_SANDBOX
359 /* Don't allow inserting in the sandbox. */
360 if (sandbox != 0)
361 {
362 EMSG(_(e_sandbox));
363 return FALSE;
364 }
365#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000366 /* Don't allow changes in the buffer while editing the cmdline. The
367 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000368 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000369 {
370 EMSG(_(e_secure));
371 return FALSE;
372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
374#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000375 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000376 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000377 {
378 EMSG(_(e_secure));
379 return FALSE;
380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 ins_compl_clear(); /* clear stuff for CTRL-X mode */
382#endif
383
Bram Moolenaar843ee412004-06-30 16:16:41 +0000384#ifdef FEAT_AUTOCMD
385 /*
386 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
387 */
388 if (cmdchar != 'r' && cmdchar != 'v')
389 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100390 pos_T save_cursor = curwin->w_cursor;
391
Bram Moolenaar1e015462005-09-25 22:16:38 +0000392# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000393 if (cmdchar == 'R')
394 ptr = (char_u *)"r";
395 else if (cmdchar == 'V')
396 ptr = (char_u *)"v";
397 else
398 ptr = (char_u *)"i";
399 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200400 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000401# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000402 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100403
Bram Moolenaar097c9922013-05-19 21:15:15 +0200404 /* Make sure the cursor didn't move. Do call check_cursor_col() in
405 * case the text was modified. Since Insert mode was not started yet
406 * a call to check_cursor_col() may move the cursor, especially with
407 * the "A" command, thus set State to avoid that. Also check that the
408 * line number is still valid (lines may have been deleted).
409 * Do not restore if v:char was set to a non-empty string. */
410 if (!equalpos(curwin->w_cursor, save_cursor)
411# ifdef FEAT_EVAL
412 && *get_vim_var_str(VV_CHAR) == NUL
413# endif
414 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100415 {
416 int save_state = State;
417
418 curwin->w_cursor = save_cursor;
419 State = INSERT;
420 check_cursor_col();
421 State = save_state;
422 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000423 }
424#endif
425
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200426#ifdef FEAT_CONCEAL
427 /* Check if the cursor line needs redrawing before changing State. If
428 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200429 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200430#endif
431
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#ifdef FEAT_MOUSE
433 /*
434 * When doing a paste with the middle mouse button, Insstart is set to
435 * where the paste started.
436 */
437 if (where_paste_started.lnum != 0)
438 Insstart = where_paste_started;
439 else
440#endif
441 {
442 Insstart = curwin->w_cursor;
443 if (startln)
444 Insstart.col = 0;
445 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000446 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 Insstart_blank_vcol = MAXCOL;
448 if (!did_ai)
449 ai_col = 0;
450
451 if (cmdchar != NUL && restart_edit == 0)
452 {
453 ResetRedobuff();
454 AppendNumberToRedobuff(count);
455#ifdef FEAT_VREPLACE
456 if (cmdchar == 'V' || cmdchar == 'v')
457 {
458 /* "gR" or "gr" command */
459 AppendCharToRedobuff('g');
460 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
461 }
462 else
463#endif
464 {
465 AppendCharToRedobuff(cmdchar);
466 if (cmdchar == 'g') /* "gI" command */
467 AppendCharToRedobuff('I');
468 else if (cmdchar == 'r') /* "r<CR>" command */
469 count = 1; /* insert only one <CR> */
470 }
471 }
472
473 if (cmdchar == 'R')
474 {
475#ifdef FEAT_FKMAP
476 if (p_fkmap && p_ri)
477 {
478 beep_flush();
479 EMSG(farsi_text_3); /* encoded in Farsi */
480 State = INSERT;
481 }
482 else
483#endif
484 State = REPLACE;
485 }
486#ifdef FEAT_VREPLACE
487 else if (cmdchar == 'V' || cmdchar == 'v')
488 {
489 State = VREPLACE;
490 replaceState = VREPLACE;
491 orig_line_count = curbuf->b_ml.ml_line_count;
492 vr_lines_changed = 1;
493 }
494#endif
495 else
496 State = INSERT;
497
498 stop_insert_mode = FALSE;
499
500 /*
501 * Need to recompute the cursor position, it might move when the cursor is
502 * on a TAB or special character.
503 */
504 curs_columns(TRUE);
505
506 /*
507 * Enable langmap or IME, indicated by 'iminsert'.
508 * Note that IME may enabled/disabled without us noticing here, thus the
509 * 'iminsert' value may not reflect what is actually used. It is updated
510 * when hitting <Esc>.
511 */
512 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
513 State |= LANGMAP;
514#ifdef USE_IM_CONTROL
515 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
516#endif
517
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518#ifdef FEAT_MOUSE
519 setmouse();
520#endif
521#ifdef FEAT_CMDL_INFO
522 clear_showcmd();
523#endif
524#ifdef FEAT_RIGHTLEFT
525 /* there is no reverse replace mode */
526 revins_on = (State == INSERT && p_ri);
527 if (revins_on)
528 undisplay_dollar();
529 revins_chars = 0;
530 revins_legal = 0;
531 revins_scol = -1;
532#endif
533
534 /*
535 * Handle restarting Insert mode.
536 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
537 * restart_edit non-zero, and something in the stuff buffer.
538 */
539 if (restart_edit != 0 && stuff_empty())
540 {
541#ifdef FEAT_MOUSE
542 /*
543 * After a paste we consider text typed to be part of the insert for
544 * the pasted text. You can backspace over the pasted text too.
545 */
546 if (where_paste_started.lnum)
547 arrow_used = FALSE;
548 else
549#endif
550 arrow_used = TRUE;
551 restart_edit = 0;
552
553 /*
554 * If the cursor was after the end-of-line before the CTRL-O and it is
555 * now at the end-of-line, put it after the end-of-line (this is not
556 * correct in very rare cases).
557 * Also do this if curswant is greater than the current virtual
558 * column. Eg after "^O$" or "^O80|".
559 */
560 validate_virtcol();
561 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000562 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 || curwin->w_curswant > curwin->w_virtcol)
564 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
565 {
566 if (ptr[1] == NUL)
567 ++curwin->w_cursor.col;
568#ifdef FEAT_MBYTE
569 else if (has_mbyte)
570 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000571 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (ptr[i] == NUL)
573 curwin->w_cursor.col += i;
574 }
575#endif
576 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000577 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
579 else
580 arrow_used = FALSE;
581
582 /* we are in insert mode now, don't need to start it anymore */
583 need_start_insertmode = FALSE;
584
585 /* Need to save the line for undo before inserting the first char. */
586 ins_need_undo = TRUE;
587
588#ifdef FEAT_MOUSE
589 where_paste_started.lnum = 0;
590#endif
591#ifdef FEAT_CINDENT
592 can_cindent = TRUE;
593#endif
594#ifdef FEAT_FOLDING
595 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
596 * restarting. */
597 if (!p_im && did_restart_edit == 0)
598 foldOpenCursor();
599#endif
600
601 /*
602 * If 'showmode' is set, show the current (insert/replace/..) mode.
603 * A warning message for changing a readonly file is given here, before
604 * actually changing anything. It's put after the mode, if any.
605 */
606 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000607 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 i = showmode();
609
610 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000611 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613#ifdef CURSOR_SHAPE
614 ui_cursor_shape(); /* may show different cursor shape */
615#endif
616#ifdef FEAT_DIGRAPHS
617 do_digraph(-1); /* clear digraphs */
618#endif
619
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000620 /*
621 * Get the current length of the redo buffer, those characters have to be
622 * skipped if we want to get to the inserted characters.
623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 ptr = get_inserted();
625 if (ptr == NULL)
626 new_insert_skip = 0;
627 else
628 {
629 new_insert_skip = (int)STRLEN(ptr);
630 vim_free(ptr);
631 }
632
633 old_indent = 0;
634
635 /*
636 * Main loop in Insert mode: repeat until Insert mode is left.
637 */
638 for (;;)
639 {
640#ifdef FEAT_RIGHTLEFT
641 if (!revins_legal)
642 revins_scol = -1; /* reset on illegal motions */
643 else
644 revins_legal = 0;
645#endif
646 if (arrow_used) /* don't repeat insert when arrow key used */
647 count = 0;
648
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100649 if (update_Insstart_orig)
650 Insstart_orig = Insstart;
651
Bram Moolenaar00672e12016-06-26 18:38:13 +0200652 if (stop_insert_mode
653#ifdef FEAT_INS_EXPAND
654 && !pum_visible()
655#endif
656 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 {
658 /* ":stopinsert" used or 'insertmode' reset */
659 count = 0;
660 goto doESCkey;
661 }
662
663 /* set curwin->w_curswant for next K_DOWN or K_UP */
664 if (!arrow_used)
665 curwin->w_set_curswant = TRUE;
666
667 /* If there is no typeahead may check for timestamps (e.g., for when a
668 * menu invoked a shell command). */
669 if (stuff_empty())
670 {
671 did_check_timestamps = FALSE;
672 if (need_check_timestamps)
673 check_timestamps(FALSE);
674 }
675
676 /*
677 * When emsg() was called msg_scroll will have been set.
678 */
679 msg_scroll = FALSE;
680
681#ifdef FEAT_GUI
682 /* When 'mousefocus' is set a mouse movement may have taken us to
683 * another window. "need_mouse_correct" may then be set because of an
684 * autocommand. */
685 if (need_mouse_correct)
686 gui_mouse_correct();
687#endif
688
689#ifdef FEAT_FOLDING
690 /* Open fold at the cursor line, according to 'foldopen'. */
691 if (fdo_flags & FDO_INSERT)
692 foldOpenCursor();
693 /* Close folds where the cursor isn't, according to 'foldclose' */
694 if (!char_avail())
695 foldCheckClose();
696#endif
697
698 /*
699 * If we inserted a character at the last position of the last line in
700 * the window, scroll the window one line up. This avoids an extra
701 * redraw.
702 * This is detected when the cursor column is smaller after inserting
703 * something.
704 * Don't do this when the topline changed already, it has
705 * already been adjusted (by insertchar() calling open_line())).
706 */
707 if (curbuf->b_mod_set
708 && curwin->w_p_wrap
709 && !did_backspace
710 && curwin->w_topline == old_topline
711#ifdef FEAT_DIFF
712 && curwin->w_topfill == old_topfill
713#endif
714 )
715 {
716 mincol = curwin->w_wcol;
717 validate_cursor_col();
718
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000719 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 && curwin->w_wrow == W_WINROW(curwin)
721 + curwin->w_height - 1 - p_so
722 && (curwin->w_cursor.lnum != curwin->w_topline
723#ifdef FEAT_DIFF
724 || curwin->w_topfill > 0
725#endif
726 ))
727 {
728#ifdef FEAT_DIFF
729 if (curwin->w_topfill > 0)
730 --curwin->w_topfill;
731 else
732#endif
733#ifdef FEAT_FOLDING
734 if (hasFolding(curwin->w_topline, NULL, &old_topline))
735 set_topline(curwin, old_topline + 1);
736 else
737#endif
738 set_topline(curwin, curwin->w_topline + 1);
739 }
740 }
741
742 /* May need to adjust w_topline to show the cursor. */
743 update_topline();
744
745 did_backspace = FALSE;
746
747 validate_cursor(); /* may set must_redraw */
748
749 /*
750 * Redraw the display when no characters are waiting.
751 * Also shows mode, ruler and positions cursor.
752 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000753 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755#ifdef FEAT_SCROLLBIND
756 if (curwin->w_p_scb)
757 do_check_scrollbind(TRUE);
758#endif
759
Bram Moolenaar860cae12010-06-05 23:22:07 +0200760#ifdef FEAT_CURSORBIND
761 if (curwin->w_p_crb)
762 do_check_cursorbind();
763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 update_curswant();
765 old_topline = curwin->w_topline;
766#ifdef FEAT_DIFF
767 old_topfill = curwin->w_topfill;
768#endif
769
770#ifdef USE_ON_FLY_SCROLL
771 dont_scroll = FALSE; /* allow scrolling here */
772#endif
773
774 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000775 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100777 if (c != K_CURSORHOLD)
778 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200779
780 /* After using CTRL-G U the next cursor key will not break undo. */
781 if (dont_sync_undo == MAYBE)
782 dont_sync_undo = TRUE;
783 else
784 dont_sync_undo = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000785 do
786 {
787 c = safe_vgetc();
788 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000790#ifdef FEAT_AUTOCMD
791 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
792 did_cursorhold = TRUE;
793#endif
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795#ifdef FEAT_RIGHTLEFT
796 if (p_hkmap && KeyTyped)
797 c = hkmap(c); /* Hebrew mode mapping */
798#endif
799#ifdef FEAT_FKMAP
800 if (p_fkmap && KeyTyped)
801 c = fkmap(c); /* Farsi mode mapping */
802#endif
803
804#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000805 /*
806 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000807 * and the cursor is still in the completed word. Only when there is
808 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000809 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000810 if (compl_started
811 && pum_wanted()
812 && curwin->w_cursor.col >= compl_col
813 && (compl_shown_match == NULL
814 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000815 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000816 /* BS: Delete one character from "compl_leader". */
817 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000818 && curwin->w_cursor.col > compl_col
819 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000820 continue;
821
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000822 /* When no match was selected or it was edited. */
823 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000824 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000825 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000826 * "compl_leader". Except when at the original match and
827 * there is nothing to add, CTRL-L works like CTRL-P then. */
828 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100829 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000830 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000831 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000832 {
833 ins_compl_addfrommatch();
834 continue;
835 }
836
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000837 /* A non-white character that fits in with the current
838 * completion: Add to "compl_leader". */
839 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000840 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100841#ifdef FEAT_AUTOCMD
842 /* Trigger InsertCharPre. */
843 char_u *str = do_insert_char_pre(c);
844 char_u *p;
845
846 if (str != NULL)
847 {
848 for (p = str; *p != NUL; mb_ptr_adv(p))
849 ins_compl_addleader(PTR2CHAR(p));
850 vim_free(str);
851 }
852 else
853#endif
854 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000855 continue;
856 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000857
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000858 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000859 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200860 if ((c == Ctrl_Y || (compl_enter_selects
861 && (c == CAR || c == K_KENTER || c == NL)))
862 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000863 {
864 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200865 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000866 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000867 }
868 }
869
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
871 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000872 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000873 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000874 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875#endif
876
Bram Moolenaar488c6512005-08-11 20:09:58 +0000877 /* CTRL-\ CTRL-N goes to Normal mode,
878 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
879 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (c == Ctrl_BSL)
881 {
882 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000883 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 ++no_mapping;
885 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000886 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 --no_mapping;
888 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000889 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000891 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 vungetc(c);
893 c = Ctrl_BSL;
894 }
895 else if (c == Ctrl_G && p_im)
896 continue;
897 else
898 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000899 if (c == Ctrl_O)
900 {
901 ins_ctrl_o();
902 ins_at_eol = FALSE; /* cursor keeps its column */
903 nomove = TRUE;
904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 count = 0;
906 goto doESCkey;
907 }
908 }
909
910#ifdef FEAT_DIGRAPHS
911 c = do_digraph(c);
912#endif
913
914#ifdef FEAT_INS_EXPAND
915 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
916 goto docomplete;
917#endif
918 if (c == Ctrl_V || c == Ctrl_Q)
919 {
920 ins_ctrl_v();
921 c = Ctrl_V; /* pretend CTRL-V is last typed character */
922 continue;
923 }
924
925#ifdef FEAT_CINDENT
926 if (cindent_on()
927# ifdef FEAT_INS_EXPAND
928 && ctrl_x_mode == 0
929# endif
930 )
931 {
932 /* A key name preceded by a bang means this key is not to be
933 * inserted. Skip ahead to the re-indenting below.
934 * A key name preceded by a star means that indenting has to be
935 * done before inserting the key. */
936 line_is_white = inindent(0);
937 if (in_cinkeys(c, '!', line_is_white))
938 goto force_cindent;
939 if (can_cindent && in_cinkeys(c, '*', line_is_white)
940 && stop_arrow() == OK)
941 do_c_expr_indent();
942 }
943#endif
944
945#ifdef FEAT_RIGHTLEFT
946 if (curwin->w_p_rl)
947 switch (c)
948 {
949 case K_LEFT: c = K_RIGHT; break;
950 case K_S_LEFT: c = K_S_RIGHT; break;
951 case K_C_LEFT: c = K_C_RIGHT; break;
952 case K_RIGHT: c = K_LEFT; break;
953 case K_S_RIGHT: c = K_S_LEFT; break;
954 case K_C_RIGHT: c = K_C_LEFT; break;
955 }
956#endif
957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 /*
959 * If 'keymodel' contains "startsel", may start selection. If it
960 * does, a CTRL-O and c will be stuffed, we need to get these
961 * characters.
962 */
963 if (ins_start_select(c))
964 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
966 /*
967 * The big switch to handle a character in insert mode.
968 */
969 switch (c)
970 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000971 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 if (echeck_abbr(ESC + ABBR_OFF))
973 break;
974 /*FALLTHROUGH*/
975
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000976 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977#ifdef FEAT_CMDWIN
978 if (c == Ctrl_C && cmdwin_type != 0)
979 {
980 /* Close the cmdline window. */
981 cmdwin_result = K_IGNORE;
982 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000983 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 goto doESCkey;
985 }
986#endif
987
988#ifdef UNIX
989do_intr:
990#endif
991 /* when 'insertmode' set, and not halfway a mapping, don't leave
992 * Insert mode */
993 if (goto_im())
994 {
995 if (got_int)
996 {
997 (void)vgetc(); /* flush all buffers */
998 got_int = FALSE;
999 }
1000 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001001 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 break;
1003 }
1004doESCkey:
1005 /*
1006 * This is the ONLY return from edit()!
1007 */
1008 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1009 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001010 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 o_lnum = curwin->w_cursor.lnum;
1012
Bram Moolenaar488c6512005-08-11 20:09:58 +00001013 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001014 {
1015#ifdef FEAT_AUTOCMD
1016 if (cmdchar != 'r' && cmdchar != 'v')
1017 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1018 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001019 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 continue;
1024
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001025 case Ctrl_Z: /* suspend when 'insertmode' set */
1026 if (!p_im)
1027 goto normalchar; /* insert CTRL-Z as normal char */
1028 stuffReadbuff((char_u *)":st\r");
1029 c = Ctrl_O;
1030 /*FALLTHROUGH*/
1031
1032 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001033#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001034 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001035 goto docomplete;
1036#endif
1037 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1038 break;
1039 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001040
1041#ifdef FEAT_VIRTUALEDIT
1042 /* don't move the cursor left when 'virtualedit' has "onemore". */
1043 if (ve_flags & VE_ONEMORE)
1044 {
1045 ins_at_eol = FALSE;
1046 nomove = TRUE;
1047 }
1048#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 count = 0;
1050 goto doESCkey;
1051
Bram Moolenaar572cb562005-08-05 21:35:02 +00001052 case K_INS: /* toggle insert/replace mode */
1053 case K_KINS:
1054 ins_insert(replaceState);
1055 break;
1056
1057 case K_SELECT: /* end of Select mode mapping - ignore */
1058 break;
1059
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001060 case K_HELP: /* Help key works like <ESC> <Help> */
1061 case K_F1:
1062 case K_XF1:
1063 stuffcharReadbuff(K_HELP);
1064 if (p_im)
1065 need_start_insertmode = TRUE;
1066 goto doESCkey;
1067
1068#ifdef FEAT_NETBEANS_INTG
1069 case K_F21: /* NetBeans command */
1070 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001071 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 --no_mapping;
1073 netbeans_keycommand(i);
1074 break;
1075#endif
1076
1077 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 case NUL:
1079 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 /* For ^@ the trailing ESC will end the insert, unless there is an
1081 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1083 && c != Ctrl_A && !p_im)
1084 goto doESCkey; /* quit insert mode */
1085 inserted_space = FALSE;
1086 break;
1087
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 ins_reg();
1090 auto_format(FALSE, TRUE);
1091 inserted_space = FALSE;
1092 break;
1093
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 ins_ctrl_g();
1096 break;
1097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case Ctrl_HAT: /* switch input mode and/or langmap */
1099 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 break;
1101
1102#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001103 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 if (!p_ari)
1105 goto normalchar;
1106 ins_ctrl_();
1107 break;
1108#endif
1109
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001110 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1112 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1113 goto docomplete;
1114#endif
1115 /* FALLTHROUGH */
1116
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118# ifdef FEAT_INS_EXPAND
1119 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1120 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001121 if (has_compl_option(FALSE))
1122 goto docomplete;
1123 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 }
1125# endif
1126 ins_shift(c, lastc);
1127 auto_format(FALSE, TRUE);
1128 inserted_space = FALSE;
1129 break;
1130
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001131 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 case K_KDEL:
1133 ins_del();
1134 auto_format(FALSE, TRUE);
1135 break;
1136
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 case Ctrl_H:
1139 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1140 auto_format(FALSE, TRUE);
1141 break;
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1145 auto_format(FALSE, TRUE);
1146 break;
1147
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001148 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001149# ifdef FEAT_COMPL_FUNC
1150 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001152 goto docomplete;
1153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1155 auto_format(FALSE, TRUE);
1156 inserted_space = FALSE;
1157 break;
1158
1159#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001160 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 case K_LEFTMOUSE_NM:
1162 case K_LEFTDRAG:
1163 case K_LEFTRELEASE:
1164 case K_LEFTRELEASE_NM:
1165 case K_MIDDLEMOUSE:
1166 case K_MIDDLEDRAG:
1167 case K_MIDDLERELEASE:
1168 case K_RIGHTMOUSE:
1169 case K_RIGHTDRAG:
1170 case K_RIGHTRELEASE:
1171 case K_X1MOUSE:
1172 case K_X1DRAG:
1173 case K_X1RELEASE:
1174 case K_X2MOUSE:
1175 case K_X2DRAG:
1176 case K_X2RELEASE:
1177 ins_mouse(c);
1178 break;
1179
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001181 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 break;
1183
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001184 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001185 ins_mousescroll(MSCR_UP);
1186 break;
1187
1188 case K_MOUSELEFT: /* Scroll wheel left */
1189 ins_mousescroll(MSCR_LEFT);
1190 break;
1191
1192 case K_MOUSERIGHT: /* Scroll wheel right */
1193 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 break;
1195#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001196#ifdef FEAT_GUI_TABLINE
1197 case K_TABLINE:
1198 case K_TABMENU:
1199 ins_tabline(c);
1200 break;
1201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001203 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 break;
1205
Bram Moolenaar754b5602006-02-09 23:53:20 +00001206#ifdef FEAT_AUTOCMD
1207 case K_CURSORHOLD: /* Didn't type something for a while. */
1208 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1209 did_cursorhold = TRUE;
1210 break;
1211#endif
1212
Bram Moolenaar4770d092006-01-12 23:22:24 +00001213#ifdef FEAT_GUI_W32
1214 /* On Win32 ignore <M-F4>, we get it when closing the window was
1215 * cancelled. */
1216 case K_F4:
1217 if (mod_mask != MOD_MASK_ALT)
1218 goto normalchar;
1219 break;
1220#endif
1221
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#ifdef FEAT_GUI
1223 case K_VER_SCROLLBAR:
1224 ins_scroll();
1225 break;
1226
1227 case K_HOR_SCROLLBAR:
1228 ins_horscroll();
1229 break;
1230#endif
1231
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001232 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 case K_S_HOME:
1235 case K_C_HOME:
1236 ins_home(c);
1237 break;
1238
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001239 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 case K_S_END:
1242 case K_C_END:
1243 ins_end(c);
1244 break;
1245
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001246 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001247 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1248 ins_s_left();
1249 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001250 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 break;
1252
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001253 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 case K_C_LEFT:
1255 ins_s_left();
1256 break;
1257
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001258 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001259 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1260 ins_s_right();
1261 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001262 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 break;
1264
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001265 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 case K_C_RIGHT:
1267 ins_s_right();
1268 break;
1269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001270 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001271#ifdef FEAT_INS_EXPAND
1272 if (pum_visible())
1273 goto docomplete;
1274#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001275 if (mod_mask & MOD_MASK_SHIFT)
1276 ins_pageup();
1277 else
1278 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 break;
1280
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001281 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 case K_PAGEUP:
1283 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001284#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001285 if (pum_visible())
1286 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 ins_pageup();
1289 break;
1290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001292#ifdef FEAT_INS_EXPAND
1293 if (pum_visible())
1294 goto docomplete;
1295#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001296 if (mod_mask & MOD_MASK_SHIFT)
1297 ins_pagedown();
1298 else
1299 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 break;
1301
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001302 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 case K_PAGEDOWN:
1304 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001305#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001306 if (pum_visible())
1307 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 ins_pagedown();
1310 break;
1311
1312#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001313 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 ins_drop();
1315 break;
1316#endif
1317
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001318 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 c = TAB;
1320 /* FALLTHROUGH */
1321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001322 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1324 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1325 goto docomplete;
1326#endif
1327 inserted_space = FALSE;
1328 if (ins_tab())
1329 goto normalchar; /* insert TAB as a normal char */
1330 auto_format(FALSE, TRUE);
1331 break;
1332
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001333 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 c = CAR;
1335 /* FALLTHROUGH */
1336 case CAR:
1337 case NL:
1338#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1339 /* In a quickfix window a <CR> jumps to the error under the
1340 * cursor. */
1341 if (bt_quickfix(curbuf) && c == CAR)
1342 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001343 if (curwin->w_llist_ref == NULL) /* quickfix window */
1344 do_cmdline_cmd((char_u *)".cc");
1345 else /* location list window */
1346 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 break;
1348 }
1349#endif
1350#ifdef FEAT_CMDWIN
1351 if (cmdwin_type != 0)
1352 {
1353 /* Execute the command in the cmdline window. */
1354 cmdwin_result = CAR;
1355 goto doESCkey;
1356 }
1357#endif
1358 if (ins_eol(c) && !p_im)
1359 goto doESCkey; /* out of memory */
1360 auto_format(FALSE, FALSE);
1361 inserted_space = FALSE;
1362 break;
1363
Bram Moolenaar860cae12010-06-05 23:22:07 +02001364#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001365 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366# ifdef FEAT_INS_EXPAND
1367 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1368 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001369 if (has_compl_option(TRUE))
1370 goto docomplete;
1371 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373# endif
1374# ifdef FEAT_DIGRAPHS
1375 c = ins_digraph();
1376 if (c == NUL)
1377 break;
1378# endif
1379 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001383 case Ctrl_X: /* Enter CTRL-X mode */
1384 ins_ctrl_x();
1385 break;
1386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001387 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (ctrl_x_mode != CTRL_X_TAGS)
1389 goto normalchar;
1390 goto docomplete;
1391
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001392 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (ctrl_x_mode != CTRL_X_FILES)
1394 goto normalchar;
1395 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001396
1397 case 's': /* Spelling completion after ^X */
1398 case Ctrl_S:
1399 if (ctrl_x_mode != CTRL_X_SPELL)
1400 goto normalchar;
1401 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#endif
1403
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001404 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405#ifdef FEAT_INS_EXPAND
1406 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1407#endif
1408 {
1409 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1410 if (p_im)
1411 {
1412 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1413 break;
1414 goto doESCkey;
1415 }
1416 goto normalchar;
1417 }
1418#ifdef FEAT_INS_EXPAND
1419 /* FALLTHROUGH */
1420
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001421 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 case Ctrl_N:
1423 /* if 'complete' is empty then plain ^P is no longer special,
1424 * but it is under other ^X modes */
1425 if (*curbuf->b_p_cpt == NUL
1426 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001427 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 goto normalchar;
1429
1430docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001431 compl_busy = TRUE;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001432 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001433 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001434 compl_cont_status = 0;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001435 disable_fold_update--;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001436 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 break;
1438#endif /* FEAT_INS_EXPAND */
1439
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001440 case Ctrl_Y: /* copy from previous line or scroll down */
1441 case Ctrl_E: /* copy from next line or scroll up */
1442 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 break;
1444
1445 default:
1446#ifdef UNIX
1447 if (c == intr_char) /* special interrupt char */
1448 goto do_intr;
1449#endif
1450
Bram Moolenaare659c952011-05-19 17:25:41 +02001451normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001453 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001455#ifdef FEAT_AUTOCMD
1456 if (!p_paste)
1457 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001458 /* Trigger InsertCharPre. */
1459 char_u *str = do_insert_char_pre(c);
1460 char_u *p;
1461
1462 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001463 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001464 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001465 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001466 /* Insert the new value of v:char literally. */
1467 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001468 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001469 c = PTR2CHAR(p);
1470 if (c == CAR || c == K_KENTER || c == NL)
1471 ins_eol(c);
1472 else
1473 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001474 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001475 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001476 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001477 vim_free(str);
1478 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001479 }
1480
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001481 /* If the new value is already inserted or an empty string
1482 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001483 if (c == NUL)
1484 break;
1485 }
1486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487#ifdef FEAT_SMARTINDENT
1488 /* Try to perform smart-indenting. */
1489 ins_try_si(c);
1490#endif
1491
1492 if (c == ' ')
1493 {
1494 inserted_space = TRUE;
1495#ifdef FEAT_CINDENT
1496 if (inindent(0))
1497 can_cindent = FALSE;
1498#endif
1499 if (Insstart_blank_vcol == MAXCOL
1500 && curwin->w_cursor.lnum == Insstart.lnum)
1501 Insstart_blank_vcol = get_nolist_virtcol();
1502 }
1503
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001504 /* Insert a normal character and check for abbreviations on a
1505 * special character. Let CTRL-] expand abbreviations without
1506 * inserting it. */
1507 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#ifdef FEAT_MBYTE
1509 /* Add ABBR_OFF for characters above 0x100, this is
1510 * what check_abbr() expects. */
1511 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1512#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001513 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 {
1515 insert_special(c, FALSE, FALSE);
1516#ifdef FEAT_RIGHTLEFT
1517 revins_legal++;
1518 revins_chars++;
1519#endif
1520 }
1521
1522 auto_format(FALSE, TRUE);
1523
1524#ifdef FEAT_FOLDING
1525 /* When inserting a character the cursor line must never be in a
1526 * closed fold. */
1527 foldOpenCursor();
1528#endif
1529 break;
1530 } /* end of switch (c) */
1531
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001532#ifdef FEAT_AUTOCMD
1533 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001534 if (c != K_CURSORHOLD
1535# ifdef FEAT_COMPL_FUNC
1536 /* but not in CTRL-X mode, a script can't restore the state */
1537 && ctrl_x_mode == 0
1538# endif
1539 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001540 did_cursorhold = FALSE;
1541#endif
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 /* If the cursor was moved we didn't just insert a space */
1544 if (arrow_used)
1545 inserted_space = FALSE;
1546
1547#ifdef FEAT_CINDENT
1548 if (can_cindent && cindent_on()
1549# ifdef FEAT_INS_EXPAND
1550 && ctrl_x_mode == 0
1551# endif
1552 )
1553 {
1554force_cindent:
1555 /*
1556 * Indent now if a key was typed that is in 'cinkeys'.
1557 */
1558 if (in_cinkeys(c, ' ', line_is_white))
1559 {
1560 if (stop_arrow() == OK)
1561 /* re-indent the current line */
1562 do_c_expr_indent();
1563 }
1564 }
1565#endif /* FEAT_CINDENT */
1566
1567 } /* for (;;) */
1568 /* NOTREACHED */
1569}
1570
1571/*
1572 * Redraw for Insert mode.
1573 * This is postponed until getting the next character to make '$' in the 'cpo'
1574 * option work correctly.
1575 * Only redraw when there are no characters available. This speeds up
1576 * inserting sequences of characters (e.g., for CTRL-R).
1577 */
1578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001579ins_redraw(
1580 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001582#ifdef FEAT_CONCEAL
1583 linenr_T conceal_old_cursor_line = 0;
1584 linenr_T conceal_new_cursor_line = 0;
1585 int conceal_update_lines = FALSE;
1586#endif
1587
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001588 if (char_avail())
1589 return;
1590
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001591#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001592 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1593 * visible, the command might delete it. */
1594 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001595# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001596 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001597# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001598# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001599 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001600# endif
1601# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001602 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001603# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001604 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001605# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001606 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001607# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001608# ifdef FEAT_INS_EXPAND
1609 && !pum_visible()
1610# endif
1611 )
1612 {
1613# ifdef FEAT_SYN_HL
1614 /* Need to update the screen first, to make sure syntax
1615 * highlighting is correct after making a change (e.g., inserting
1616 * a "(". The autocommand may also require a redraw, so it's done
1617 * again below, unfortunately. */
1618 if (syntax_present(curwin) && must_redraw)
1619 update_screen(0);
1620# endif
1621# ifdef FEAT_AUTOCMD
1622 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001623 {
1624 /* Make sure curswant is correct, an autocommand may call
1625 * getcurpos(). */
1626 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001627 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001628 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001629# endif
1630# ifdef FEAT_CONCEAL
1631 if (curwin->w_p_cole > 0)
1632 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001633# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001634 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001635# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001636 conceal_new_cursor_line = curwin->w_cursor.lnum;
1637 conceal_update_lines = TRUE;
1638 }
1639# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001640# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001641 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001642# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001643 }
1644#endif
1645
1646#ifdef FEAT_AUTOCMD
1647 /* Trigger TextChangedI if b_changedtick differs. */
1648 if (ready && has_textchangedI()
1649 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001650# ifdef FEAT_INS_EXPAND
1651 && !pum_visible()
1652# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001653 )
1654 {
1655 if (last_changedtick_buf == curbuf)
1656 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1657 last_changedtick_buf = curbuf;
1658 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001660#endif
1661
1662 if (must_redraw)
1663 update_screen(0);
1664 else if (clear_cmdline || redraw_cmdline)
1665 showmode(); /* clear cmdline and show mode */
1666# if defined(FEAT_CONCEAL)
1667 if ((conceal_update_lines
1668 && (conceal_old_cursor_line != conceal_new_cursor_line
1669 || conceal_cursor_line(curwin)))
1670 || need_cursor_line_redraw)
1671 {
1672 if (conceal_old_cursor_line != conceal_new_cursor_line)
1673 update_single_line(curwin, conceal_old_cursor_line);
1674 update_single_line(curwin, conceal_new_cursor_line == 0
1675 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1676 curwin->w_valid &= ~VALID_CROW;
1677 }
1678# endif
1679 showruler(FALSE);
1680 setcursor();
1681 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682}
1683
1684/*
1685 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1686 */
1687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001688ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001691 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001694 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
1696 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001699 did_putchar = TRUE;
1700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1702
1703#ifdef FEAT_CMDL_INFO
1704 add_to_showcmd_c(Ctrl_V);
1705#endif
1706
1707 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001708 if (did_putchar)
1709 /* when the line fits in 'columns' the '^' is at the start of the next
1710 * line and will not removed by the redraw */
1711 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712#ifdef FEAT_CMDL_INFO
1713 clear_showcmd();
1714#endif
1715 insert_special(c, FALSE, TRUE);
1716#ifdef FEAT_RIGHTLEFT
1717 revins_chars++;
1718 revins_legal++;
1719#endif
1720}
1721
1722/*
1723 * Put a character directly onto the screen. It's not stored in a buffer.
1724 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1725 */
1726static int pc_status;
1727#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1728#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1729#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1730#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732static int pc_attr;
1733static int pc_row;
1734static int pc_col;
1735
1736 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001737edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 int attr;
1740
1741 if (ScreenLines != NULL)
1742 {
1743 update_topline(); /* just in case w_topline isn't valid */
1744 validate_cursor();
1745 if (highlight)
1746 attr = hl_attr(HLF_8);
1747 else
1748 attr = 0;
1749 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1750 pc_col = W_WINCOL(curwin);
1751#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1752 pc_status = PC_STATUS_UNSET;
1753#endif
1754#ifdef FEAT_RIGHTLEFT
1755 if (curwin->w_p_rl)
1756 {
1757 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1758# ifdef FEAT_MBYTE
1759 if (has_mbyte)
1760 {
1761 int fix_col = mb_fix_col(pc_col, pc_row);
1762
1763 if (fix_col != pc_col)
1764 {
1765 screen_putchar(' ', pc_row, fix_col, attr);
1766 --curwin->w_wcol;
1767 pc_status = PC_STATUS_RIGHT;
1768 }
1769 }
1770# endif
1771 }
1772 else
1773#endif
1774 {
1775 pc_col += curwin->w_wcol;
1776#ifdef FEAT_MBYTE
1777 if (mb_lefthalve(pc_row, pc_col))
1778 pc_status = PC_STATUS_LEFT;
1779#endif
1780 }
1781
1782 /* save the character to be able to put it back */
1783#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1784 if (pc_status == PC_STATUS_UNSET)
1785#endif
1786 {
1787 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1788 pc_status = PC_STATUS_SET;
1789 }
1790 screen_putchar(c, pc_row, pc_col, attr);
1791 }
1792}
1793
1794/*
1795 * Undo the previous edit_putchar().
1796 */
1797 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001798edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
1800 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1801 {
1802#if defined(FEAT_MBYTE)
1803 if (pc_status == PC_STATUS_RIGHT)
1804 ++curwin->w_wcol;
1805 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1806 redrawWinline(curwin->w_cursor.lnum, FALSE);
1807 else
1808#endif
1809 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1810 }
1811}
1812
1813/*
1814 * Called when p_dollar is set: display a '$' at the end of the changed text
1815 * Only works when cursor is in the line that changes.
1816 */
1817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819{
1820 colnr_T save_col;
1821
1822 if (!redrawing())
1823 return;
1824
1825 cursor_off();
1826 save_col = curwin->w_cursor.col;
1827 curwin->w_cursor.col = col;
1828#ifdef FEAT_MBYTE
1829 if (has_mbyte)
1830 {
1831 char_u *p;
1832
1833 /* If on the last byte of a multi-byte move to the first byte. */
1834 p = ml_get_curline();
1835 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1836 }
1837#endif
1838 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1839 if (curwin->w_wcol < W_WIDTH(curwin))
1840 {
1841 edit_putchar('$', FALSE);
1842 dollar_vcol = curwin->w_virtcol;
1843 }
1844 curwin->w_cursor.col = save_col;
1845}
1846
1847/*
1848 * Call this function before moving the cursor from the normal insert position
1849 * in insert mode.
1850 */
1851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001852undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001854 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001856 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 redrawWinline(curwin->w_cursor.lnum, FALSE);
1858 }
1859}
1860
1861/*
1862 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1863 * Keep the cursor on the same character.
1864 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1865 * type == INDENT_DEC decrease indent (for CTRL-D)
1866 * type == INDENT_SET set indent to "amount"
1867 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1868 */
1869 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001870change_indent(
1871 int type,
1872 int amount,
1873 int round,
1874 int replaced, /* replaced character, put on replace stack */
1875 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876{
1877 int vcol;
1878 int last_vcol;
1879 int insstart_less; /* reduction for Insstart.col */
1880 int new_cursor_col;
1881 int i;
1882 char_u *ptr;
1883 int save_p_list;
1884 int start_col;
1885 colnr_T vc;
1886#ifdef FEAT_VREPLACE
1887 colnr_T orig_col = 0; /* init for GCC */
1888 char_u *new_line, *orig_line = NULL; /* init for GCC */
1889
1890 /* VREPLACE mode needs to know what the line was like before changing */
1891 if (State & VREPLACE_FLAG)
1892 {
1893 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1894 orig_col = curwin->w_cursor.col;
1895 }
1896#endif
1897
1898 /* for the following tricks we don't want list mode */
1899 save_p_list = curwin->w_p_list;
1900 curwin->w_p_list = FALSE;
1901 vc = getvcol_nolist(&curwin->w_cursor);
1902 vcol = vc;
1903
1904 /*
1905 * For Replace mode we need to fix the replace stack later, which is only
1906 * possible when the cursor is in the indent. Remember the number of
1907 * characters before the cursor if it's possible.
1908 */
1909 start_col = curwin->w_cursor.col;
1910
1911 /* determine offset from first non-blank */
1912 new_cursor_col = curwin->w_cursor.col;
1913 beginline(BL_WHITE);
1914 new_cursor_col -= curwin->w_cursor.col;
1915
1916 insstart_less = curwin->w_cursor.col;
1917
1918 /*
1919 * If the cursor is in the indent, compute how many screen columns the
1920 * cursor is to the left of the first non-blank.
1921 */
1922 if (new_cursor_col < 0)
1923 vcol = get_indent() - vcol;
1924
1925 if (new_cursor_col > 0) /* can't fix replace stack */
1926 start_col = -1;
1927
1928 /*
1929 * Set the new indent. The cursor will be put on the first non-blank.
1930 */
1931 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001932 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 else
1934 {
1935#ifdef FEAT_VREPLACE
1936 int save_State = State;
1937
1938 /* Avoid being called recursively. */
1939 if (State & VREPLACE_FLAG)
1940 State = INSERT;
1941#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001942 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943#ifdef FEAT_VREPLACE
1944 State = save_State;
1945#endif
1946 }
1947 insstart_less -= curwin->w_cursor.col;
1948
1949 /*
1950 * Try to put cursor on same character.
1951 * If the cursor is at or after the first non-blank in the line,
1952 * compute the cursor column relative to the column of the first
1953 * non-blank character.
1954 * If we are not in insert mode, leave the cursor on the first non-blank.
1955 * If the cursor is before the first non-blank, position it relative
1956 * to the first non-blank, counted in screen columns.
1957 */
1958 if (new_cursor_col >= 0)
1959 {
1960 /*
1961 * When changing the indent while the cursor is touching it, reset
1962 * Insstart_col to 0.
1963 */
1964 if (new_cursor_col == 0)
1965 insstart_less = MAXCOL;
1966 new_cursor_col += curwin->w_cursor.col;
1967 }
1968 else if (!(State & INSERT))
1969 new_cursor_col = curwin->w_cursor.col;
1970 else
1971 {
1972 /*
1973 * Compute the screen column where the cursor should be.
1974 */
1975 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001976 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
1978 /*
1979 * Advance the cursor until we reach the right screen column.
1980 */
1981 vcol = last_vcol = 0;
1982 new_cursor_col = -1;
1983 ptr = ml_get_curline();
1984 while (vcol <= (int)curwin->w_virtcol)
1985 {
1986 last_vcol = vcol;
1987#ifdef FEAT_MBYTE
1988 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001989 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 else
1991#endif
1992 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001993 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 }
1995 vcol = last_vcol;
1996
1997 /*
1998 * May need to insert spaces to be able to position the cursor on
1999 * the right screen column.
2000 */
2001 if (vcol != (int)curwin->w_virtcol)
2002 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002003 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002005 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 if (ptr != NULL)
2007 {
2008 new_cursor_col += i;
2009 ptr[i] = NUL;
2010 while (--i >= 0)
2011 ptr[i] = ' ';
2012 ins_str(ptr);
2013 vim_free(ptr);
2014 }
2015 }
2016
2017 /*
2018 * When changing the indent while the cursor is in it, reset
2019 * Insstart_col to 0.
2020 */
2021 insstart_less = MAXCOL;
2022 }
2023
2024 curwin->w_p_list = save_p_list;
2025
2026 if (new_cursor_col <= 0)
2027 curwin->w_cursor.col = 0;
2028 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002029 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 curwin->w_set_curswant = TRUE;
2031 changed_cline_bef_curs();
2032
2033 /*
2034 * May have to adjust the start of the insert.
2035 */
2036 if (State & INSERT)
2037 {
2038 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2039 {
2040 if ((int)Insstart.col <= insstart_less)
2041 Insstart.col = 0;
2042 else
2043 Insstart.col -= insstart_less;
2044 }
2045 if ((int)ai_col <= insstart_less)
2046 ai_col = 0;
2047 else
2048 ai_col -= insstart_less;
2049 }
2050
2051 /*
2052 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2053 * If the number of characters before the cursor decreased, need to pop a
2054 * few characters from the replace stack.
2055 * If the number of characters before the cursor increased, need to push a
2056 * few NULs onto the replace stack.
2057 */
2058 if (REPLACE_NORMAL(State) && start_col >= 0)
2059 {
2060 while (start_col > (int)curwin->w_cursor.col)
2061 {
2062 replace_join(0); /* remove a NUL from the replace stack */
2063 --start_col;
2064 }
2065 while (start_col < (int)curwin->w_cursor.col || replaced)
2066 {
2067 replace_push(NUL);
2068 if (replaced)
2069 {
2070 replace_push(replaced);
2071 replaced = NUL;
2072 }
2073 ++start_col;
2074 }
2075 }
2076
2077#ifdef FEAT_VREPLACE
2078 /*
2079 * For VREPLACE mode, we also have to fix the replace stack. In this case
2080 * it is always possible because we backspace over the whole line and then
2081 * put it back again the way we wanted it.
2082 */
2083 if (State & VREPLACE_FLAG)
2084 {
2085 /* If orig_line didn't allocate, just return. At least we did the job,
2086 * even if you can't backspace. */
2087 if (orig_line == NULL)
2088 return;
2089
2090 /* Save new line */
2091 new_line = vim_strsave(ml_get_curline());
2092 if (new_line == NULL)
2093 return;
2094
2095 /* We only put back the new line up to the cursor */
2096 new_line[curwin->w_cursor.col] = NUL;
2097
2098 /* Put back original line */
2099 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2100 curwin->w_cursor.col = orig_col;
2101
2102 /* Backspace from cursor to start of line */
2103 backspace_until_column(0);
2104
2105 /* Insert new stuff into line again */
2106 ins_bytes(new_line);
2107
2108 vim_free(new_line);
2109 }
2110#endif
2111}
2112
2113/*
2114 * Truncate the space at the end of a line. This is to be used only in an
2115 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2116 * modes.
2117 */
2118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002119truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120{
2121 int i;
2122
2123 /* find start of trailing white space */
2124 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2125 {
2126 if (State & REPLACE_FLAG)
2127 replace_join(0); /* remove a NUL from the replace stack */
2128 }
2129 line[i + 1] = NUL;
2130}
2131
2132#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2133 || defined(FEAT_COMMENTS) || defined(PROTO)
2134/*
2135 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2136 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002137 * Will attempt not to go before "col" even when there is a composing
2138 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 */
2140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002141backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142{
2143 while ((int)curwin->w_cursor.col > col)
2144 {
2145 curwin->w_cursor.col--;
2146 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002147 replace_do_bs(col);
2148 else if (!del_char_after_col(col))
2149 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151}
2152#endif
2153
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002154/*
2155 * Like del_char(), but make sure not to go before column "limit_col".
2156 * Only matters when there are composing characters.
2157 * Return TRUE when something was deleted.
2158 */
2159 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002160del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002161{
2162#ifdef FEAT_MBYTE
2163 if (enc_utf8 && limit_col >= 0)
2164 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002165 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002166
2167 /* Make sure the cursor is at the start of a character, but
2168 * skip forward again when going too far back because of a
2169 * composing character. */
2170 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002171 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002172 {
2173 int l = utf_ptr2len(ml_get_cursor());
2174
2175 if (l == 0) /* end of line */
2176 break;
2177 curwin->w_cursor.col += l;
2178 }
2179 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2180 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002181 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002182 }
2183 else
2184#endif
2185 (void)del_char(FALSE);
2186 return TRUE;
2187}
2188
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2190/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002191 * CTRL-X pressed in Insert mode.
2192 */
2193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002194ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002195{
2196 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2197 * CTRL-V works like CTRL-N */
2198 if (ctrl_x_mode != CTRL_X_CMDLINE)
2199 {
2200 /* if the next ^X<> won't ADD nothing, then reset
2201 * compl_cont_status */
2202 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002203 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002204 else
2205 compl_cont_status = 0;
2206 /* We're not sure which CTRL-X mode it will be yet */
2207 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2208 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2209 edit_submode_pre = NULL;
2210 showmode();
2211 }
2212}
2213
2214/*
2215 * Return TRUE if the 'dict' or 'tsr' option can be used.
2216 */
2217 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002218has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002219{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002220 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002221# ifdef FEAT_SPELL
2222 && !curwin->w_p_spell
2223# endif
2224 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002225 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2226 {
2227 ctrl_x_mode = 0;
2228 edit_submode = NULL;
2229 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2230 : (char_u *)_("'thesaurus' option is empty"),
2231 hl_attr(HLF_E));
2232 if (emsg_silent == 0)
2233 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002234 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002235 setcursor();
2236 out_flush();
2237 ui_delay(2000L, FALSE);
2238 }
2239 return FALSE;
2240 }
2241 return TRUE;
2242}
2243
2244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2246 * This depends on the current mode.
2247 */
2248 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002249vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
2251 /* Always allow ^R - let it's results then be checked */
2252 if (c == Ctrl_R)
2253 return TRUE;
2254
Bram Moolenaare3226be2005-12-18 22:10:00 +00002255 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002256 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002257 return TRUE;
2258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 switch (ctrl_x_mode)
2260 {
2261 case 0: /* Not in any CTRL-X mode */
2262 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2263 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002264 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2266 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2267 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002268 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002269 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 case CTRL_X_SCROLL:
2271 return (c == Ctrl_Y || c == Ctrl_E);
2272 case CTRL_X_WHOLE_LINE:
2273 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2274 case CTRL_X_FILES:
2275 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2276 case CTRL_X_DICTIONARY:
2277 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2278 case CTRL_X_THESAURUS:
2279 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2280 case CTRL_X_TAGS:
2281 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2282#ifdef FEAT_FIND_ID
2283 case CTRL_X_PATH_PATTERNS:
2284 return (c == Ctrl_P || c == Ctrl_N);
2285 case CTRL_X_PATH_DEFINES:
2286 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2287#endif
2288 case CTRL_X_CMDLINE:
2289 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2290 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002291#ifdef FEAT_COMPL_FUNC
2292 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002293 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002294 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002295 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002296#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002297 case CTRL_X_SPELL:
2298 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002299 case CTRL_X_EVAL:
2300 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002302 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 return FALSE;
2304}
2305
2306/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002307 * Return TRUE when character "c" is part of the item currently being
2308 * completed. Used to decide whether to abandon complete mode when the menu
2309 * is visible.
2310 */
2311 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002312ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002313{
2314 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2315 /* When expanding an identifier only accept identifier chars. */
2316 return vim_isIDc(c);
2317
2318 switch (ctrl_x_mode)
2319 {
2320 case CTRL_X_FILES:
2321 /* When expanding file name only accept file name chars. But not
2322 * path separators, so that "proto/<Tab>" expands files in
2323 * "proto", not "proto/" as a whole */
2324 return vim_isfilec(c) && !vim_ispathsep(c);
2325
2326 case CTRL_X_CMDLINE:
2327 case CTRL_X_OMNI:
2328 /* Command line and Omni completion can work with just about any
2329 * printable character, but do stop at white space. */
2330 return vim_isprintc(c) && !vim_iswhite(c);
2331
2332 case CTRL_X_WHOLE_LINE:
2333 /* For while line completion a space can be part of the line. */
2334 return vim_isprintc(c);
2335 }
2336 return vim_iswordc(c);
2337}
2338
2339/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002340 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002342 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 * the rest of the word to be in -- webb
2344 */
2345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002346ins_compl_add_infercase(
2347 char_u *str,
2348 int len,
2349 int icase,
2350 char_u *fname,
2351 int dir,
2352 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002354 char_u *p;
2355 int i, c;
2356 int actual_len; /* Take multi-byte characters */
2357 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002358 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002359 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 int has_lower = FALSE;
2361 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002363 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002365 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
Bram Moolenaara2993e12007-08-12 14:38:46 +00002367 /* Find actual length of completion. */
2368#ifdef FEAT_MBYTE
2369 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002371 p = str;
2372 actual_len = 0;
2373 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002375 mb_ptr_adv(p);
2376 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
2378 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002379 else
2380#endif
2381 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382
Bram Moolenaara2993e12007-08-12 14:38:46 +00002383 /* Find actual length of original text. */
2384#ifdef FEAT_MBYTE
2385 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002387 p = compl_orig_text;
2388 actual_compl_length = 0;
2389 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002391 mb_ptr_adv(p);
2392 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
2394 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002395 else
2396#endif
2397 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002399 /* "actual_len" may be smaller than "actual_compl_length" when using
2400 * thesaurus, only use the minimum when comparing. */
2401 min_len = actual_len < actual_compl_length
2402 ? actual_len : actual_compl_length;
2403
Bram Moolenaara2993e12007-08-12 14:38:46 +00002404 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002405 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002406 if (wca != NULL)
2407 {
2408 p = str;
2409 for (i = 0; i < actual_len; ++i)
2410#ifdef FEAT_MBYTE
2411 if (has_mbyte)
2412 wca[i] = mb_ptr2char_adv(&p);
2413 else
2414#endif
2415 wca[i] = *(p++);
2416
2417 /* Rule 1: Were any chars converted to lower? */
2418 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002419 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002420 {
2421#ifdef FEAT_MBYTE
2422 if (has_mbyte)
2423 c = mb_ptr2char_adv(&p);
2424 else
2425#endif
2426 c = *(p++);
2427 if (MB_ISLOWER(c))
2428 {
2429 has_lower = TRUE;
2430 if (MB_ISUPPER(wca[i]))
2431 {
2432 /* Rule 1 is satisfied. */
2433 for (i = actual_compl_length; i < actual_len; ++i)
2434 wca[i] = MB_TOLOWER(wca[i]);
2435 break;
2436 }
2437 }
2438 }
2439
2440 /*
2441 * Rule 2: No lower case, 2nd consecutive letter converted to
2442 * upper case.
2443 */
2444 if (!has_lower)
2445 {
2446 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002447 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002448 {
2449#ifdef FEAT_MBYTE
2450 if (has_mbyte)
2451 c = mb_ptr2char_adv(&p);
2452 else
2453#endif
2454 c = *(p++);
2455 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2456 {
2457 /* Rule 2 is satisfied. */
2458 for (i = actual_compl_length; i < actual_len; ++i)
2459 wca[i] = MB_TOUPPER(wca[i]);
2460 break;
2461 }
2462 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2463 }
2464 }
2465
2466 /* Copy the original case of the part we typed. */
2467 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002468 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002469 {
2470#ifdef FEAT_MBYTE
2471 if (has_mbyte)
2472 c = mb_ptr2char_adv(&p);
2473 else
2474#endif
2475 c = *(p++);
2476 if (MB_ISLOWER(c))
2477 wca[i] = MB_TOLOWER(wca[i]);
2478 else if (MB_ISUPPER(c))
2479 wca[i] = MB_TOUPPER(wca[i]);
2480 }
2481
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002482 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002483 * Generate encoding specific output from wide character array.
2484 * Multi-byte characters can occupy up to five bytes more than
2485 * ASCII characters, and we also need one byte for NUL, so stay
2486 * six bytes away from the edge of IObuff.
2487 */
2488 p = IObuff;
2489 i = 0;
2490 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2491#ifdef FEAT_MBYTE
2492 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002493 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002494 else
2495#endif
2496 *(p++) = wca[i++];
2497 *p = NUL;
2498
2499 vim_free(wca);
2500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002502 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2503 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002505 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506}
2507
2508/*
2509 * Add a match to the list of matches.
2510 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002511 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002512 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002514 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002515ins_compl_add(
2516 char_u *str,
2517 int len,
2518 int icase,
2519 char_u *fname,
2520 char_u **cptext, /* extra text for popup menu or NULL */
2521 int cdir,
2522 int flags,
2523 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002525 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002526 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528 ui_breakcheck();
2529 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002530 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (len < 0)
2532 len = (int)STRLEN(str);
2533
2534 /*
2535 * If the same match is already present, don't add it.
2536 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002537 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002539 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 do
2541 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002542 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002543 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002544 && match->cp_str[len] == NUL)
2545 return NOTDONE;
2546 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002547 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002550 /* Remove any popup menu before changing the list of matches. */
2551 ins_compl_del_pum();
2552
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 /*
2554 * Allocate a new match structure.
2555 * Copy the values to the new match structure.
2556 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002557 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002559 return FAIL;
2560 match->cp_number = -1;
2561 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002562 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002563 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
2565 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002566 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002568 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002569
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002571 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2573 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002574 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002575 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002576 && compl_curr_match->cp_fname != NULL
2577 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002578 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002579 else if (fname != NULL)
2580 {
2581 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002582 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002585 match->cp_fname = NULL;
2586 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002587
2588 if (cptext != NULL)
2589 {
2590 int i;
2591
2592 for (i = 0; i < CPT_COUNT; ++i)
2593 if (cptext[i] != NULL && *cptext[i] != NUL)
2594 match->cp_text[i] = vim_strsave(cptext[i]);
2595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596
2597 /*
2598 * Link the new match structure in the list of matches.
2599 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002600 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002601 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 else if (dir == FORWARD)
2603 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002604 match->cp_next = compl_curr_match->cp_next;
2605 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
2607 else /* BACKWARD */
2608 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002609 match->cp_next = compl_curr_match;
2610 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002612 if (match->cp_next)
2613 match->cp_next->cp_prev = match;
2614 if (match->cp_prev)
2615 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002617 compl_first_match = match;
2618 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002620 /*
2621 * Find the longest common string if still doing that.
2622 */
2623 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2624 ins_compl_longest_match(match);
2625
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 return OK;
2627}
2628
2629/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002630 * Return TRUE if "str[len]" matches with match->cp_str, considering
2631 * match->cp_icase.
2632 */
2633 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002634ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002635{
2636 if (match->cp_icase)
2637 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2638 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2639}
2640
2641/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002642 * Reduce the longest common string for match "match".
2643 */
2644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002645ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002646{
2647 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002648 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002649 int had_match;
2650
2651 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002652 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002653 /* First match, use it as a whole. */
2654 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002655 if (compl_leader != NULL)
2656 {
2657 had_match = (curwin->w_cursor.col > compl_col);
2658 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002659 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002660 ins_redraw(FALSE);
2661
2662 /* When the match isn't there (to avoid matching itself) remove it
2663 * again after redrawing. */
2664 if (!had_match)
2665 ins_compl_delete();
2666 compl_used_match = FALSE;
2667 }
2668 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002669 else
2670 {
2671 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002672 p = compl_leader;
2673 s = match->cp_str;
2674 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002675 {
2676#ifdef FEAT_MBYTE
2677 if (has_mbyte)
2678 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002679 c1 = mb_ptr2char(p);
2680 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002681 }
2682 else
2683#endif
2684 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002685 c1 = *p;
2686 c2 = *s;
2687 }
2688 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2689 : (c1 != c2))
2690 break;
2691#ifdef FEAT_MBYTE
2692 if (has_mbyte)
2693 {
2694 mb_ptr_adv(p);
2695 mb_ptr_adv(s);
2696 }
2697 else
2698#endif
2699 {
2700 ++p;
2701 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002702 }
2703 }
2704
2705 if (*p != NUL)
2706 {
2707 /* Leader was shortened, need to change the inserted text. */
2708 *p = NUL;
2709 had_match = (curwin->w_cursor.col > compl_col);
2710 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002711 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002712 ins_redraw(FALSE);
2713
2714 /* When the match isn't there (to avoid matching itself) remove it
2715 * again after redrawing. */
2716 if (!had_match)
2717 ins_compl_delete();
2718 }
2719
2720 compl_used_match = FALSE;
2721 }
2722}
2723
2724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 * Add an array of matches to the list of matches.
2726 * Frees matches[].
2727 */
2728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002729ins_compl_add_matches(
2730 int num_matches,
2731 char_u **matches,
2732 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
2734 int i;
2735 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002736 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
Bram Moolenaar572cb562005-08-05 21:35:02 +00002738 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002739 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002740 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002742 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 FreeWild(num_matches, matches);
2744}
2745
2746/* Make the completion list cyclic.
2747 * Return the number of matches (excluding the original).
2748 */
2749 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002750ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002752 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 int count = 0;
2754
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002755 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
2757 /*
2758 * Find the end of the list.
2759 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002760 match = compl_first_match;
2761 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002762 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002764 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 ++count;
2766 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002767 match->cp_next = compl_first_match;
2768 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770 return count;
2771}
2772
Bram Moolenaara94bc432006-03-10 21:42:59 +00002773/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002774 * Set variables that store noselect and noinsert behavior from the
2775 * 'completeopt' value.
2776 */
2777 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002778completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002779{
2780 compl_no_insert = FALSE;
2781 compl_no_select = FALSE;
2782 if (strstr((char *)p_cot, "noselect") != NULL)
2783 compl_no_select = TRUE;
2784 if (strstr((char *)p_cot, "noinsert") != NULL)
2785 compl_no_insert = TRUE;
2786}
2787
2788/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002789 * Start completion for the complete() function.
2790 * "startcol" is where the matched text starts (1 is first column).
2791 * "list" is the list of matches.
2792 */
2793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002794set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002795{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002796 int save_w_wrow = curwin->w_wrow;
2797
Bram Moolenaara94bc432006-03-10 21:42:59 +00002798 /* If already doing completions stop it. */
2799 if (ctrl_x_mode != 0)
2800 ins_compl_prep(' ');
2801 ins_compl_clear();
2802
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002803 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002804 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002805 startcol = curwin->w_cursor.col;
2806 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002807 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002808 /* compl_pattern doesn't need to be set */
2809 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2810 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002811 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002812 return;
2813
Bram Moolenaare4214502015-03-05 18:08:43 +01002814 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002815
2816 ins_compl_add_list(list);
2817 compl_matches = ins_compl_make_cyclic();
2818 compl_started = TRUE;
2819 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002820 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002821
2822 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002823 if (compl_no_insert || compl_no_select)
2824 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002825 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002826 if (compl_no_select)
2827 /* Down/Up has no real effect. */
2828 ins_complete(K_UP, FALSE);
2829 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002830 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002831 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002832 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002833
2834 /* Lazily show the popup menu, unless we got interrupted. */
2835 if (!compl_interrupted)
2836 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002837 out_flush();
2838}
2839
2840
Bram Moolenaar9372a112005-12-06 19:59:18 +00002841/* "compl_match_array" points the currently displayed list of entries in the
2842 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002843static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002844static int compl_match_arraysize;
2845
2846/*
2847 * Update the screen and when there is any scrolling remove the popup menu.
2848 */
2849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002850ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002851{
2852 int h;
2853
2854 if (compl_match_array != NULL)
2855 {
2856 h = curwin->w_cline_height;
2857 update_screen(0);
2858 if (h != curwin->w_cline_height)
2859 ins_compl_del_pum();
2860 }
2861}
2862
2863/*
2864 * Remove any popup menu.
2865 */
2866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002867ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002868{
2869 if (compl_match_array != NULL)
2870 {
2871 pum_undisplay();
2872 vim_free(compl_match_array);
2873 compl_match_array = NULL;
2874 }
2875}
2876
2877/*
2878 * Return TRUE if the popup menu should be displayed.
2879 */
2880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002881pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002882{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002883 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002884 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002885 return FALSE;
2886
2887 /* The display looks bad on a B&W display. */
2888 if (t_colors < 8
2889#ifdef FEAT_GUI
2890 && !gui.in_use
2891#endif
2892 )
2893 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002894 return TRUE;
2895}
2896
2897/*
2898 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002899 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002900 */
2901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002902pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002903{
2904 compl_T *compl;
2905 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002906
2907 /* Don't display the popup menu if there are no matches or there is only
2908 * one (ignoring the original text). */
2909 compl = compl_first_match;
2910 i = 0;
2911 do
2912 {
2913 if (compl == NULL
2914 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2915 break;
2916 compl = compl->cp_next;
2917 } while (compl != compl_first_match);
2918
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002919 if (strstr((char *)p_cot, "menuone") != NULL)
2920 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002921 return (i >= 2);
2922}
2923
2924/*
2925 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002926 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002927 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002928 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002929ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002930{
2931 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002932 compl_T *shown_compl = NULL;
2933 int did_find_shown_match = FALSE;
2934 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002935 int i;
2936 int cur = -1;
2937 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002938 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002939
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002940 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002941 return;
2942
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002943#if defined(FEAT_EVAL)
2944 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2945 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2946#endif
2947
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002948 /* Update the screen before drawing the popup menu over it. */
2949 update_screen(0);
2950
2951 if (compl_match_array == NULL)
2952 {
2953 /* Need to build the popup menu list. */
2954 compl_match_arraysize = 0;
2955 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002956 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002957 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002958 do
2959 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002960 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2961 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002962 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002963 ++compl_match_arraysize;
2964 compl = compl->cp_next;
2965 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002966 if (compl_match_arraysize == 0)
2967 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002968 compl_match_array = (pumitem_T *)alloc_clear(
2969 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002970 * compl_match_arraysize));
2971 if (compl_match_array != NULL)
2972 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002973 /* If the current match is the original text don't find the first
2974 * match after it, don't highlight anything. */
2975 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2976 shown_match_ok = TRUE;
2977
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002978 i = 0;
2979 compl = compl_first_match;
2980 do
2981 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002982 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2983 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002984 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002985 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002986 if (!shown_match_ok)
2987 {
2988 if (compl == compl_shown_match || did_find_shown_match)
2989 {
2990 /* This item is the shown match or this is the
2991 * first displayed item after the shown match. */
2992 compl_shown_match = compl;
2993 did_find_shown_match = TRUE;
2994 shown_match_ok = TRUE;
2995 }
2996 else
2997 /* Remember this displayed match for when the
2998 * shown match is just below it. */
2999 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003000 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003001 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003002
3003 if (compl->cp_text[CPT_ABBR] != NULL)
3004 compl_match_array[i].pum_text =
3005 compl->cp_text[CPT_ABBR];
3006 else
3007 compl_match_array[i].pum_text = compl->cp_str;
3008 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3009 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3010 if (compl->cp_text[CPT_MENU] != NULL)
3011 compl_match_array[i++].pum_extra =
3012 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003013 else
3014 compl_match_array[i++].pum_extra = compl->cp_fname;
3015 }
3016
3017 if (compl == compl_shown_match)
3018 {
3019 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003020
3021 /* When the original text is the shown match don't set
3022 * compl_shown_match. */
3023 if (compl->cp_flags & ORIGINAL_TEXT)
3024 shown_match_ok = TRUE;
3025
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003026 if (!shown_match_ok && shown_compl != NULL)
3027 {
3028 /* The shown match isn't displayed, set it to the
3029 * previously displayed match. */
3030 compl_shown_match = shown_compl;
3031 shown_match_ok = TRUE;
3032 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003033 }
3034 compl = compl->cp_next;
3035 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003036
3037 if (!shown_match_ok) /* no displayed match at all */
3038 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003039 }
3040 }
3041 else
3042 {
3043 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003044 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003045 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3046 || compl_match_array[i].pum_text
3047 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003048 {
3049 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003050 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003051 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003052 }
3053
3054 if (compl_match_array != NULL)
3055 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003056 /* In Replace mode when a $ is displayed at the end of the line only
3057 * part of the screen would be updated. We do need to redraw here. */
3058 dollar_vcol = -1;
3059
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003060 /* Compute the screen column of the start of the completed text.
3061 * Use the cursor to get all wrapping and other settings right. */
3062 col = curwin->w_cursor.col;
3063 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003064 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003065 curwin->w_cursor.col = col;
3066 }
3067}
3068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069#define DICT_FIRST (1) /* use just first element in "dict" */
3070#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003071
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003073 * Add any identifiers that match the given pattern in the list of dictionary
3074 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 */
3076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003077ins_compl_dictionaries(
3078 char_u *dict_start,
3079 char_u *pat,
3080 int flags, /* DICT_FIRST and/or DICT_EXACT */
3081 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003083 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 char_u *ptr;
3085 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 char_u **files;
3088 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003090 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
Bram Moolenaar0b238792006-03-02 22:49:12 +00003092 if (*dict == NUL)
3093 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003094#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003095 /* When 'dictionary' is empty and spell checking is enabled use
3096 * "spell". */
3097 if (!thesaurus && curwin->w_p_spell)
3098 dict = (char_u *)"spell";
3099 else
3100#endif
3101 return;
3102 }
3103
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003105 if (buf == NULL)
3106 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003107 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 /* If 'infercase' is set, don't use 'smartcase' here */
3110 save_p_scs = p_scs;
3111 if (curbuf->b_p_inf)
3112 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003113
3114 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3115 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003116 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003117 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003118 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003119 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003120 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003121
3122 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003123 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003124 len = STRLEN(pat_esc) + 10;
3125 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003126 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003127 {
3128 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003129 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003130 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003131 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003132 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3133 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003134 vim_free(ptr);
3135 }
3136 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003137 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003138 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003139 if (regmatch.regprog == NULL)
3140 goto theend;
3141 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3144 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003145 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
3147 /* copy one dictionary file name into buf */
3148 if (flags == DICT_EXACT)
3149 {
3150 count = 1;
3151 files = &dict;
3152 }
3153 else
3154 {
3155 /* Expand wildcards in the dictionary name, but do not allow
3156 * backticks (for security, the 'dict' option may have been set in
3157 * a modeline). */
3158 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003159# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003160 if (!thesaurus && STRCMP(buf, "spell") == 0)
3161 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003162 else
3163# endif
3164 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 || expand_wildcards(1, &buf, &count, &files,
3166 EW_FILE|EW_SILENT) != OK)
3167 count = 0;
3168 }
3169
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003170# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003171 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003173 /* Complete from active spelling. Skip "\<" in the pattern, we
3174 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003175 if (pat[0] == '\\' && pat[1] == '<')
3176 ptr = pat + 2;
3177 else
3178 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003179 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003181 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003182# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003183 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003184 {
3185 ins_compl_files(count, files, thesaurus, flags,
3186 &regmatch, buf, &dir);
3187 if (flags != DICT_EXACT)
3188 FreeWild(count, files);
3189 }
3190 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 break;
3192 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003193
3194theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003196 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 vim_free(buf);
3198}
3199
Bram Moolenaar0b238792006-03-02 22:49:12 +00003200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003201ins_compl_files(
3202 int count,
3203 char_u **files,
3204 int thesaurus,
3205 int flags,
3206 regmatch_T *regmatch,
3207 char_u *buf,
3208 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003209{
3210 char_u *ptr;
3211 int i;
3212 FILE *fp;
3213 int add_r;
3214
3215 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3216 {
3217 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3218 if (flags != DICT_EXACT)
3219 {
3220 vim_snprintf((char *)IObuff, IOSIZE,
3221 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003222 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003223 }
3224
3225 if (fp != NULL)
3226 {
3227 /*
3228 * Read dictionary file line by line.
3229 * Check each line for a match.
3230 */
3231 while (!got_int && !compl_interrupted
3232 && !vim_fgets(buf, LSIZE, fp))
3233 {
3234 ptr = buf;
3235 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3236 {
3237 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003238 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003239 ptr = find_line_end(ptr);
3240 else
3241 ptr = find_word_end(ptr);
3242 add_r = ins_compl_add_infercase(regmatch->startp[0],
3243 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003244 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003245 if (thesaurus)
3246 {
3247 char_u *wstart;
3248
3249 /*
3250 * Add the other matches on the line
3251 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003252 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003253 while (!got_int)
3254 {
3255 /* Find start of the next word. Skip white
3256 * space and punctuation. */
3257 ptr = find_word_start(ptr);
3258 if (*ptr == NUL || *ptr == NL)
3259 break;
3260 wstart = ptr;
3261
Bram Moolenaara2993e12007-08-12 14:38:46 +00003262 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003263#ifdef FEAT_MBYTE
3264 if (has_mbyte)
3265 /* Japanese words may have characters in
3266 * different classes, only separate words
3267 * with single-byte non-word characters. */
3268 while (*ptr != NUL)
3269 {
3270 int l = (*mb_ptr2len)(ptr);
3271
3272 if (l < 2 && !vim_iswordc(*ptr))
3273 break;
3274 ptr += l;
3275 }
3276 else
3277#endif
3278 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003279
3280 /* Add the word. Skip the regexp match. */
3281 if (wstart != regmatch->startp[0])
3282 add_r = ins_compl_add_infercase(wstart,
3283 (int)(ptr - wstart),
3284 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003285 }
3286 }
3287 if (add_r == OK)
3288 /* if dir was BACKWARD then honor it just once */
3289 *dir = FORWARD;
3290 else if (add_r == FAIL)
3291 break;
3292 /* avoid expensive call to vim_regexec() when at end
3293 * of line */
3294 if (*ptr == '\n' || got_int)
3295 break;
3296 }
3297 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003298 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003299 }
3300 fclose(fp);
3301 }
3302 }
3303}
3304
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305/*
3306 * Find the start of the next word.
3307 * Returns a pointer to the first char of the word. Also stops at a NUL.
3308 */
3309 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003310find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311{
3312#ifdef FEAT_MBYTE
3313 if (has_mbyte)
3314 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003315 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 else
3317#endif
3318 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3319 ++ptr;
3320 return ptr;
3321}
3322
3323/*
3324 * Find the end of the word. Assumes it starts inside a word.
3325 * Returns a pointer to just after the word.
3326 */
3327 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003328find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329{
3330#ifdef FEAT_MBYTE
3331 int start_class;
3332
3333 if (has_mbyte)
3334 {
3335 start_class = mb_get_class(ptr);
3336 if (start_class > 1)
3337 while (*ptr != NUL)
3338 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003339 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 if (mb_get_class(ptr) != start_class)
3341 break;
3342 }
3343 }
3344 else
3345#endif
3346 while (vim_iswordc(*ptr))
3347 ++ptr;
3348 return ptr;
3349}
3350
3351/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003352 * Find the end of the line, omitting CR and NL at the end.
3353 * Returns a pointer to just after the line.
3354 */
3355 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003356find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003357{
3358 char_u *s;
3359
3360 s = ptr + STRLEN(ptr);
3361 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3362 --s;
3363 return s;
3364}
3365
3366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 * Free the list of completions
3368 */
3369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003370ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003372 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003373 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003375 vim_free(compl_pattern);
3376 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003377 vim_free(compl_leader);
3378 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003380 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003382
3383 ins_compl_del_pum();
3384 pum_clear();
3385
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003386 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 do
3388 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003389 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003390 compl_curr_match = compl_curr_match->cp_next;
3391 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003393 if (match->cp_flags & FREE_FNAME)
3394 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003395 for (i = 0; i < CPT_COUNT; ++i)
3396 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003398 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3399 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003400 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401}
3402
3403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003404ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003406 compl_cont_status = 0;
3407 compl_started = FALSE;
3408 compl_matches = 0;
3409 vim_free(compl_pattern);
3410 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003411 vim_free(compl_leader);
3412 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003414 vim_free(compl_orig_text);
3415 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003416 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003417 /* clear v:completed_item */
3418 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419}
3420
3421/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003422 * Return TRUE when Insert completion is active.
3423 */
3424 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003425ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003426{
3427 return compl_started;
3428}
3429
3430/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003431 * Delete one character before the cursor and show the subset of the matches
3432 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003433 * Returns the character to be used, NUL if the work is done and another char
3434 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003435 */
3436 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003437ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003438{
3439 char_u *line;
3440 char_u *p;
3441
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003442 line = ml_get_curline();
3443 p = line + curwin->w_cursor.col;
3444 mb_ptr_back(line, p);
3445
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003446 /* Stop completion when the whole word was deleted. For Omni completion
3447 * allow the word to be deleted, we won't match everything. */
3448 if ((int)(p - line) - (int)compl_col < 0
3449 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003450 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003451 return K_BS;
3452
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003453 /* Deleted more than what was used to find matches or didn't finish
3454 * finding all matches: need to look for matches all over again. */
3455 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003456 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003457 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003458
Bram Moolenaara6557602006-02-04 22:43:20 +00003459 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003460 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003461 if (compl_leader != NULL)
3462 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003463 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003464 if (compl_shown_match != NULL)
3465 /* Make sure current match is not a hidden item. */
3466 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003467 return NUL;
3468 }
3469 return K_BS;
3470}
Bram Moolenaara6557602006-02-04 22:43:20 +00003471
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003472/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003473 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3474 * be called.
3475 */
3476 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003477ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003478{
3479 /* Return TRUE if we didn't complete finding matches or when the
3480 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3481 return compl_was_interrupted
3482 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3483 && compl_opt_refresh_always);
3484}
3485
3486/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003487 * Called after changing "compl_leader".
3488 * Show the popup menu with a different set of matches.
3489 * May also search for matches again if the previous search was interrupted.
3490 */
3491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003492ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003493{
3494 ins_compl_del_pum();
3495 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003496 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003497 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003498
3499 if (compl_started)
3500 ins_compl_set_original_text(compl_leader);
3501 else
3502 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003503#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003504 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003505#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003506 /*
3507 * Matches were cleared, need to search for them now. First display
3508 * the changed text before the cursor. Set "compl_restarting" to
3509 * avoid that the first match is inserted.
3510 */
3511 update_screen(0);
3512#ifdef FEAT_GUI
3513 if (gui.in_use)
3514 {
3515 /* Show the cursor after the match, not after the redrawn text. */
3516 setcursor();
3517 out_flush();
3518 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003519 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003520#endif
3521 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003522 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003523 compl_cont_status = 0;
3524 compl_restarting = FALSE;
3525 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003526
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003527 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003528
3529 /* Show the popup menu with a different set of matches. */
3530 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003531
3532 /* Don't let Enter select the original text when there is no popup menu. */
3533 if (compl_match_array == NULL)
3534 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003535}
3536
3537/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003538 * Return the length of the completion, from the completion start column to
3539 * the cursor column. Making sure it never goes below zero.
3540 */
3541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003542ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003543{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003544 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003545
3546 if (off < 0)
3547 return 0;
3548 return off;
3549}
3550
3551/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003552 * Append one character to the match leader. May reduce the number of
3553 * matches.
3554 */
3555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003556ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003557{
3558#ifdef FEAT_MBYTE
3559 int cc;
3560
3561 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3562 {
3563 char_u buf[MB_MAXBYTES + 1];
3564
3565 (*mb_char2bytes)(c, buf);
3566 buf[cc] = NUL;
3567 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003568 if (compl_opt_refresh_always)
3569 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003570 }
3571 else
3572#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003573 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003574 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003575 if (compl_opt_refresh_always)
3576 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003577 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003578
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003579 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003580 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003581 ins_compl_restart();
3582
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003583 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003584 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003585 * break redo. */
3586 if (!compl_opt_refresh_always)
3587 {
3588 vim_free(compl_leader);
3589 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003590 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003591 if (compl_leader != NULL)
3592 ins_compl_new_leader();
3593 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003594}
3595
3596/*
3597 * Setup for finding completions again without leaving CTRL-X mode. Used when
3598 * BS or a key was typed while still searching for matches.
3599 */
3600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003602{
3603 ins_compl_free();
3604 compl_started = FALSE;
3605 compl_matches = 0;
3606 compl_cont_status = 0;
3607 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003608}
3609
3610/*
3611 * Set the first match, the original text.
3612 */
3613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003614ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003615{
3616 char_u *p;
3617
3618 /* Replace the original text entry. */
3619 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3620 {
3621 p = vim_strsave(str);
3622 if (p != NULL)
3623 {
3624 vim_free(compl_first_match->cp_str);
3625 compl_first_match->cp_str = p;
3626 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003627 }
3628}
3629
3630/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003631 * Append one character to the match leader. May reduce the number of
3632 * matches.
3633 */
3634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003636{
3637 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003638 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003639 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003640 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003641
3642 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003643 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003644 {
3645 /* When still at the original match use the first entry that matches
3646 * the leader. */
3647 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3648 {
3649 p = NULL;
3650 for (cp = compl_shown_match->cp_next; cp != NULL
3651 && cp != compl_first_match; cp = cp->cp_next)
3652 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003653 if (compl_leader == NULL
3654 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003655 (int)STRLEN(compl_leader)))
3656 {
3657 p = cp->cp_str;
3658 break;
3659 }
3660 }
3661 if (p == NULL || (int)STRLEN(p) <= len)
3662 return;
3663 }
3664 else
3665 return;
3666 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003667 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003668 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003669 ins_compl_addleader(c);
3670}
3671
3672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003674 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003675 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003677 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003678ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679{
3680 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003682 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
3684 /* Forget any previous 'special' messages if this is actually
3685 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3686 */
3687 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3688 edit_submode_extra = NULL;
3689
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003690 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003691 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3692 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003693 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003695 /* Set "compl_get_longest" when finding the first matches. */
3696 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3697 || (ctrl_x_mode == 0 && !compl_started))
3698 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003699 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003700 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003701
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003702 }
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3705 {
3706 /*
3707 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3708 * it will be yet. Now we decide.
3709 */
3710 switch (c)
3711 {
3712 case Ctrl_E:
3713 case Ctrl_Y:
3714 ctrl_x_mode = CTRL_X_SCROLL;
3715 if (!(State & REPLACE_FLAG))
3716 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3717 else
3718 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3719 edit_submode_pre = NULL;
3720 showmode();
3721 break;
3722 case Ctrl_L:
3723 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3724 break;
3725 case Ctrl_F:
3726 ctrl_x_mode = CTRL_X_FILES;
3727 break;
3728 case Ctrl_K:
3729 ctrl_x_mode = CTRL_X_DICTIONARY;
3730 break;
3731 case Ctrl_R:
3732 /* Simply allow ^R to happen without affecting ^X mode */
3733 break;
3734 case Ctrl_T:
3735 ctrl_x_mode = CTRL_X_THESAURUS;
3736 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003737#ifdef FEAT_COMPL_FUNC
3738 case Ctrl_U:
3739 ctrl_x_mode = CTRL_X_FUNCTION;
3740 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003741 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003742 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003743 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003744#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003745 case 's':
3746 case Ctrl_S:
3747 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003748#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003749 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003750 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003751 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003752#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003753 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 case Ctrl_RSB:
3755 ctrl_x_mode = CTRL_X_TAGS;
3756 break;
3757#ifdef FEAT_FIND_ID
3758 case Ctrl_I:
3759 case K_S_TAB:
3760 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3761 break;
3762 case Ctrl_D:
3763 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3764 break;
3765#endif
3766 case Ctrl_V:
3767 case Ctrl_Q:
3768 ctrl_x_mode = CTRL_X_CMDLINE;
3769 break;
3770 case Ctrl_P:
3771 case Ctrl_N:
3772 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3773 * just started ^X mode, or there were enough ^X's to cancel
3774 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3775 * do normal expansion when interrupting a different mode (say
3776 * ^X^F^X^P or ^P^X^X^P, see below)
3777 * nothing changes if interrupting mode 0, (eg, the flag
3778 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003779 if (!(compl_cont_status & CONT_INTRPT))
3780 compl_cont_status |= CONT_LOCAL;
3781 else if (compl_cont_mode != 0)
3782 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 /* FALLTHROUGH */
3784 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003785 /* If we have typed at least 2 ^X's... for modes != 0, we set
3786 * compl_cont_status = 0 (eg, as if we had just started ^X
3787 * mode).
3788 * For mode 0, we set "compl_cont_mode" to an impossible
3789 * value, in both cases ^X^X can be used to restart the same
3790 * mode (avoiding ADDING mode).
3791 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3792 * 'complete' and local ^P expansions respectively.
3793 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3794 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (c == Ctrl_X)
3796 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003797 if (compl_cont_mode != 0)
3798 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003800 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802 ctrl_x_mode = 0;
3803 edit_submode = NULL;
3804 showmode();
3805 break;
3806 }
3807 }
3808 else if (ctrl_x_mode != 0)
3809 {
3810 /* We're already in CTRL-X mode, do we stay in it? */
3811 if (!vim_is_ctrl_x_key(c))
3812 {
3813 if (ctrl_x_mode == CTRL_X_SCROLL)
3814 ctrl_x_mode = 0;
3815 else
3816 ctrl_x_mode = CTRL_X_FINISHED;
3817 edit_submode = NULL;
3818 }
3819 showmode();
3820 }
3821
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003822 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
3824 /* Show error message from attempted keyword completion (probably
3825 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003826 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003828 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3829 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 || ctrl_x_mode == CTRL_X_FINISHED)
3831 {
3832 /* Get here when we have finished typing a sequence of ^N and
3833 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003834 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003835 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 {
3837 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003838 * If any of the original typed text has been changed, eg when
3839 * ignorecase is set, we must add back-spaces to the redo
3840 * buffer. We add as few as necessary to delete just the part
3841 * of the original text that has changed.
3842 * When using the longest match, edited the match or used
3843 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003845 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3846 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003847 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003848 ptr = NULL;
3849 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851
3852#ifdef FEAT_CINDENT
3853 want_cindent = (can_cindent && cindent_on());
3854#endif
3855 /*
3856 * When completing whole lines: fix indent for 'cindent'.
3857 * Otherwise, break line if it's too long.
3858 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003859 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
3861#ifdef FEAT_CINDENT
3862 /* re-indent the current line */
3863 if (want_cindent)
3864 {
3865 do_c_expr_indent();
3866 want_cindent = FALSE; /* don't do it again */
3867 }
3868#endif
3869 }
3870 else
3871 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003872 int prev_col = curwin->w_cursor.col;
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003875 if (prev_col > 0)
3876 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003877 /* only format when something was inserted */
3878 if (!arrow_used && !ins_need_undo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003880 if (prev_col > 0
3881 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3882 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003885 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003886 * the selection without inserting anything. When
3887 * compl_enter_selects is set the Enter key does the same. */
3888 if ((c == Ctrl_Y || (compl_enter_selects
3889 && (c == CAR || c == K_KENTER || c == NL)))
3890 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003891 retval = TRUE;
3892
Bram Moolenaar47247282016-08-02 22:36:02 +02003893 /* CTRL-E means completion is Ended, go back to the typed text.
3894 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003895 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003896 {
3897 ins_compl_delete();
3898 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003899 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003900 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003901 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003902 retval = TRUE;
3903 }
3904
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003905 auto_format(FALSE, TRUE);
3906
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003908 compl_started = FALSE;
3909 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003910 if (!shortmess(SHM_COMPLETIONMENU))
3911 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003913 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (edit_submode != NULL)
3915 {
3916 edit_submode = NULL;
3917 showmode();
3918 }
3919
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003920#ifdef FEAT_CMDWIN
3921 if (c == Ctrl_C && cmdwin_type != 0)
3922 /* Avoid the popup menu remains displayed when leaving the
3923 * command line window. */
3924 update_screen(0);
3925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926#ifdef FEAT_CINDENT
3927 /*
3928 * Indent now if a key was typed that is in 'cinkeys'.
3929 */
3930 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3931 do_c_expr_indent();
3932#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003933#ifdef FEAT_AUTOCMD
3934 /* Trigger the CompleteDone event to give scripts a chance to act
3935 * upon the completion. */
3936 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
3939 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003940#ifdef FEAT_AUTOCMD
3941 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3942 /* Trigger the CompleteDone event to give scripts a chance to act
3943 * upon the (possibly failed) completion. */
3944 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
3947 /* reset continue_* if we left expansion-mode, if we stay they'll be
3948 * (re)set properly in ins_complete() */
3949 if (!vim_is_ctrl_x_key(c))
3950 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003951 compl_cont_status = 0;
3952 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003954
3955 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956}
3957
3958/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003959 * Fix the redo buffer for the completion leader replacing some of the typed
3960 * text. This inserts backspaces and appends the changed text.
3961 * "ptr" is the known leader text or NUL.
3962 */
3963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003964ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003965{
3966 int len;
3967 char_u *p;
3968 char_u *ptr = ptr_arg;
3969
3970 if (ptr == NULL)
3971 {
3972 if (compl_leader != NULL)
3973 ptr = compl_leader;
3974 else
3975 return; /* nothing to do */
3976 }
3977 if (compl_orig_text != NULL)
3978 {
3979 p = compl_orig_text;
3980 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3981 ;
3982#ifdef FEAT_MBYTE
3983 if (len > 0)
3984 len -= (*mb_head_off)(p, p + len);
3985#endif
3986 for (p += len; *p != NUL; mb_ptr_adv(p))
3987 AppendCharToRedobuff(K_BS);
3988 }
3989 else
3990 len = 0;
3991 if (ptr != NULL)
3992 AppendToRedobuffLit(ptr + len, -1);
3993}
3994
3995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3997 * (depending on flag) starting from buf and looking for a non-scanned
3998 * buffer (other than curbuf). curbuf is special, if it is called with
3999 * buf=curbuf then it has to be the first call for a given flag/expansion.
4000 *
4001 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4002 */
4003 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004004ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
4006#ifdef FEAT_WINDOWS
4007 static win_T *wp;
4008#endif
4009
4010 if (flag == 'w') /* just windows */
4011 {
4012#ifdef FEAT_WINDOWS
4013 if (buf == curbuf) /* first call for this flag/expansion */
4014 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004015 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 && wp->w_buffer->b_scanned)
4017 ;
4018 buf = wp->w_buffer;
4019#else
4020 buf = curbuf;
4021#endif
4022 }
4023 else
4024 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4025 * (unlisted buffers)
4026 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004027 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 && ((flag == 'U'
4029 ? buf->b_p_bl
4030 : (!buf->b_p_bl
4031 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004032 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 ;
4034 return buf;
4035}
4036
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004037#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004038/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004039 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004040 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004041 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004043expand_by_function(
4044 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4045 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004046{
Bram Moolenaar82139082011-09-14 16:52:09 +02004047 list_T *matchlist = NULL;
4048 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004049 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004050 char_u *funcname;
4051 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004052 win_T *curwin_save;
4053 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004054 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004055
Bram Moolenaare344bea2005-09-01 20:46:49 +00004056 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4057 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004058 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004059
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004060 /* Call 'completefunc' to obtain the list of matches. */
4061 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004062 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004063
Bram Moolenaare344bea2005-09-01 20:46:49 +00004064 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004065 curwin_save = curwin;
4066 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004067
4068 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004069 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004070 {
4071 switch (rettv.v_type)
4072 {
4073 case VAR_LIST:
4074 matchlist = rettv.vval.v_list;
4075 break;
4076 case VAR_DICT:
4077 matchdict = rettv.vval.v_dict;
4078 break;
4079 default:
4080 /* TODO: Give error message? */
4081 clear_tv(&rettv);
4082 break;
4083 }
4084 }
4085
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004086 if (curwin_save != curwin || curbuf_save != curbuf)
4087 {
4088 EMSG(_(e_complwin));
4089 goto theend;
4090 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004091 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004092 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004093 if (!equalpos(curwin->w_cursor, pos))
4094 {
4095 EMSG(_(e_compldel));
4096 goto theend;
4097 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004098
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004099 if (matchlist != NULL)
4100 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004101 else if (matchdict != NULL)
4102 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004103
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004104theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004105 if (matchdict != NULL)
4106 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004107 if (matchlist != NULL)
4108 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004109}
4110#endif /* FEAT_COMPL_FUNC */
4111
Bram Moolenaar39f05632006-03-19 22:15:26 +00004112#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004113/*
4114 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004115 */
4116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004117ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004118{
4119 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004120 int dir = compl_direction;
4121
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004122 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004123 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004124 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004125 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4126 /* if dir was BACKWARD then honor it just once */
4127 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004128 else if (did_emsg)
4129 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004130 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004131}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004132
4133/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004134 * Add completions from a dict.
4135 */
4136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004137ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004138{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004139 dictitem_T *di_refresh;
4140 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004141
4142 /* Check for optional "refresh" item. */
4143 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004144 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4145 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004146 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004147 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004148
4149 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4150 compl_opt_refresh_always = TRUE;
4151 }
4152
4153 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004154 di_words = dict_find(dict, (char_u *)"words", 5);
4155 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4156 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004157}
4158
4159/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004160 * Add a match to the list of matches from a typeval_T.
4161 * If the given string is already in the list of completions, then return
4162 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4163 * maybe because alloc() returns NULL, then FAIL is returned.
4164 */
4165 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004166ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004167{
4168 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004169 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004170 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004171 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004172 char_u *(cptext[CPT_COUNT]);
4173
4174 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4175 {
4176 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4177 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4178 (char_u *)"abbr", FALSE);
4179 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4180 (char_u *)"menu", FALSE);
4181 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4182 (char_u *)"kind", FALSE);
4183 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4184 (char_u *)"info", FALSE);
4185 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4186 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004187 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004188 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004189 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4190 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004191 }
4192 else
4193 {
4194 word = get_tv_string_chk(tv);
4195 vim_memset(cptext, 0, sizeof(cptext));
4196 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004197 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004198 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004199 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004200}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004201#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004202
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004203/*
4204 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004205 * The search starts at position "ini" in curbuf and in the direction
4206 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004207 * When "compl_started" is FALSE start at that position, otherwise continue
4208 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004209 * This may return before finding all the matches.
4210 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 */
4212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004213ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214{
4215 static pos_T first_match_pos;
4216 static pos_T last_match_pos;
4217 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004218 static int found_all = FALSE; /* Found all matches of a
4219 certain type. */
4220 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221
Bram Moolenaar572cb562005-08-05 21:35:02 +00004222 pos_T *pos;
4223 char_u **matches;
4224 int save_p_scs;
4225 int save_p_ws;
4226 int save_p_ic;
4227 int i;
4228 int num_matches;
4229 int len;
4230 int found_new_match;
4231 int type = ctrl_x_mode;
4232 char_u *ptr;
4233 char_u *dict = NULL;
4234 int dict_f = 0;
4235 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004236 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004238 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004240 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 ins_buf->b_scanned = 0;
4242 found_all = FALSE;
4243 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004244 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004245 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 last_match_pos = first_match_pos = *ini;
4247 }
4248
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004249 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004250 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4252 for (;;)
4253 {
4254 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004255 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004257 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 * or if found_all says this entry is done. For ^X^L only use the
4259 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004260 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004261 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 {
4263 found_all = FALSE;
4264 while (*e_cpt == ',' || *e_cpt == ' ')
4265 e_cpt++;
4266 if (*e_cpt == '.' && !curbuf->b_scanned)
4267 {
4268 ins_buf = curbuf;
4269 first_match_pos = *ini;
4270 /* So that ^N can match word immediately after cursor */
4271 if (ctrl_x_mode == 0)
4272 dec(&first_match_pos);
4273 last_match_pos = first_match_pos;
4274 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004275
4276 /* Remember the first match so that the loop stops when we
4277 * wrap and come back there a second time. */
4278 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4281 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4282 {
4283 /* Scan a buffer, but not the current one. */
4284 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4285 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004286 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 first_match_pos.col = last_match_pos.col = 0;
4288 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4289 last_match_pos.lnum = 0;
4290 type = 0;
4291 }
4292 else /* unloaded buffer, scan like dictionary */
4293 {
4294 found_all = TRUE;
4295 if (ins_buf->b_fname == NULL)
4296 continue;
4297 type = CTRL_X_DICTIONARY;
4298 dict = ins_buf->b_fname;
4299 dict_f = DICT_EXACT;
4300 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004301 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 ins_buf->b_fname == NULL
4303 ? buf_spname(ins_buf)
4304 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004305 ? ins_buf->b_fname
4306 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004307 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
4309 else if (*e_cpt == NUL)
4310 break;
4311 else
4312 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004313 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 type = -1;
4315 else if (*e_cpt == 'k' || *e_cpt == 's')
4316 {
4317 if (*e_cpt == 'k')
4318 type = CTRL_X_DICTIONARY;
4319 else
4320 type = CTRL_X_THESAURUS;
4321 if (*++e_cpt != ',' && *e_cpt != NUL)
4322 {
4323 dict = e_cpt;
4324 dict_f = DICT_FIRST;
4325 }
4326 }
4327#ifdef FEAT_FIND_ID
4328 else if (*e_cpt == 'i')
4329 type = CTRL_X_PATH_PATTERNS;
4330 else if (*e_cpt == 'd')
4331 type = CTRL_X_PATH_DEFINES;
4332#endif
4333 else if (*e_cpt == ']' || *e_cpt == 't')
4334 {
4335 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004336 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004337 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 else
4340 type = -1;
4341
4342 /* in any case e_cpt is advanced to the next entry */
4343 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4344
4345 found_all = TRUE;
4346 if (type == -1)
4347 continue;
4348 }
4349 }
4350
4351 switch (type)
4352 {
4353 case -1:
4354 break;
4355#ifdef FEAT_FIND_ID
4356 case CTRL_X_PATH_PATTERNS:
4357 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004358 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004359 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004361 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4363 (linenr_T)1, (linenr_T)MAXLNUM);
4364 break;
4365#endif
4366
4367 case CTRL_X_DICTIONARY:
4368 case CTRL_X_THESAURUS:
4369 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004370 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 : (type == CTRL_X_THESAURUS
4372 ? (*curbuf->b_p_tsr == NUL
4373 ? p_tsr
4374 : curbuf->b_p_tsr)
4375 : (*curbuf->b_p_dict == NUL
4376 ? p_dict
4377 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004378 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004379 dict != NULL ? dict_f
4380 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 dict = NULL;
4382 break;
4383
4384 case CTRL_X_TAGS:
4385 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4386 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004389 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390 * of matches is found when compl_pattern is empty */
4391 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4393 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4394 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4395 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004396 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398 p_ic = save_p_ic;
4399 break;
4400
4401 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004402 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4404 {
4405
4406 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004407 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004408 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410 break;
4411
4412 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413 if (expand_cmdline(&compl_xp, compl_pattern,
4414 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004416 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 break;
4418
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004419#ifdef FEAT_COMPL_FUNC
4420 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004421 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004422 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004423 break;
4424#endif
4425
Bram Moolenaar488c6512005-08-11 20:09:58 +00004426 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004427#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004428 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004429 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004430 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004431 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004432#endif
4433 break;
4434
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 default: /* normal ^P/^N and ^X^L */
4436 /*
4437 * If 'infercase' is set, don't use 'smartcase' here
4438 */
4439 save_p_scs = p_scs;
4440 if (ins_buf->b_p_inf)
4441 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442
Bram Moolenaar361aa502014-01-23 22:45:58 +01004443 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 * end but never from the middle, thus setting nowrapscan in this
4445 * buffers is a good idea, on the other hand, we always set
4446 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4447 save_p_ws = p_ws;
4448 if (ins_buf != curbuf)
4449 p_ws = FALSE;
4450 else if (*e_cpt == '.')
4451 p_ws = TRUE;
4452 for (;;)
4453 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004454 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004456 ++msg_silent; /* Don't want messages for wrapscan. */
4457
Bram Moolenaare4214502015-03-05 18:08:43 +01004458 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4459 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004460 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004461 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004462 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004464 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004466 found_new_match = searchit(NULL, ins_buf, pos,
4467 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004468 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004469 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004470 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004471 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004473 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004474 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 first_match_pos = *pos;
4476 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004477 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 }
4479 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004480 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 found_new_match = FAIL;
4482 if (found_new_match == FAIL)
4483 {
4484 if (ins_buf == curbuf)
4485 found_all = TRUE;
4486 break;
4487 }
4488
4489 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004490 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 && ini->lnum == pos->lnum
4492 && ini->col == pos->col)
4493 continue;
4494 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004495 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004497 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
4499 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4500 continue;
4501 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4502 if (!p_paste)
4503 ptr = skipwhite(ptr);
4504 }
4505 len = (int)STRLEN(ptr);
4506 }
4507 else
4508 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004509 char_u *tmp_ptr = ptr;
4510
4511 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004513 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 /* Skip if already inside a word. */
4515 if (vim_iswordp(tmp_ptr))
4516 continue;
4517 /* Find start of next word. */
4518 tmp_ptr = find_word_start(tmp_ptr);
4519 }
4520 /* Find end of this word. */
4521 tmp_ptr = find_word_end(tmp_ptr);
4522 len = (int)(tmp_ptr - ptr);
4523
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004524 if ((compl_cont_status & CONT_ADDING)
4525 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
4527 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4528 {
4529 /* Try next line, if any. the new word will be
4530 * "join" as if the normal command "J" was used.
4531 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004532 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 * works -- Acevedo */
4534 STRNCPY(IObuff, ptr, len);
4535 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4536 tmp_ptr = ptr = skipwhite(ptr);
4537 /* Find start of next word. */
4538 tmp_ptr = find_word_start(tmp_ptr);
4539 /* Find end of next word. */
4540 tmp_ptr = find_word_end(tmp_ptr);
4541 if (tmp_ptr > ptr)
4542 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004543 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004545 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 IObuff[len++] = ' ';
4547 /* IObuf =~ "\k.* ", thus len >= 2 */
4548 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004549 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 || (vim_strchr(p_cpo, CPO_JOINSP)
4551 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004552 && (IObuff[len - 2] == '?'
4553 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 IObuff[len++] = ' ';
4555 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004556 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (tmp_ptr - ptr >= IOSIZE - len)
4558 tmp_ptr = ptr + IOSIZE - len - 1;
4559 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4560 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004561 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
4563 IObuff[len] = NUL;
4564 ptr = IObuff;
4565 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004566 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 continue;
4568 }
4569 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004570 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004571 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004572 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 {
4574 found_new_match = OK;
4575 break;
4576 }
4577 }
4578 p_scs = save_p_scs;
4579 p_ws = save_p_ws;
4580 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004581
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004582 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004583 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004584 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 found_new_match = OK;
4586
4587 /* break the loop for specialized modes (use 'complete' just for the
4588 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004589 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004590 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004591 {
4592 if (got_int)
4593 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004594 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004595 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004596 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004597
Bram Moolenaare4214502015-03-05 18:08:43 +01004598 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004599 || compl_interrupted)
4600 break;
4601 compl_started = TRUE;
4602 }
4603 else
4604 {
4605 /* Mark a buffer scanned when it has been scanned completely */
4606 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4607 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004609 compl_started = FALSE;
4610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613
Bram Moolenaare4214502015-03-05 18:08:43 +01004614 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 && *e_cpt == NUL) /* Got to end of 'complete' */
4616 found_new_match = FAIL;
4617
4618 i = -1; /* total of matches, unknown */
4619 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004620 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 i = ins_compl_make_cyclic();
4622
4623 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004624 * just been made cyclic then we have to move compl_curr_match to the next
4625 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004626 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4627 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004628 if (compl_curr_match == NULL)
4629 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 return i;
4631}
4632
4633/* Delete the old text being completed. */
4634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004635ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004637 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
4639 /*
4640 * In insert mode: Delete the typed part.
4641 * In replace mode: Put the old characters back, if any.
4642 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004643 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4644 if ((int)curwin->w_cursor.col > col)
4645 {
4646 if (stop_arrow() == FAIL)
4647 return;
4648 backspace_until_column(col);
4649 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004650
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004651 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4652 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004654 /* clear v:completed_item */
4655 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656}
4657
Bram Moolenaar472e8592016-10-15 17:06:47 +02004658/*
4659 * Insert the new text being completed.
4660 * "in_compl_func" is TRUE when called from complete_check().
4661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004663ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004665 dict_T *dict;
4666
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004667 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004668 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4669 compl_used_match = FALSE;
4670 else
4671 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004672
4673 /* Set completed item. */
4674 /* { word, abbr, menu, kind, info } */
4675 dict = dict_alloc();
4676 if (dict != NULL)
4677 {
4678 dict_add_nr_str(dict, "word", 0L,
4679 EMPTY_IF_NULL(compl_shown_match->cp_str));
4680 dict_add_nr_str(dict, "abbr", 0L,
4681 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4682 dict_add_nr_str(dict, "menu", 0L,
4683 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4684 dict_add_nr_str(dict, "kind", 0L,
4685 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4686 dict_add_nr_str(dict, "info", 0L,
4687 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4688 }
4689 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004690 if (!in_compl_func)
4691 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692}
4693
4694/*
4695 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004696 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4697 * get more completions. If it is FALSE, then we just do nothing when there
4698 * are no more completions in a given direction. The latter case is used when
4699 * we are still in the middle of finding completions, to allow browsing
4700 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 * Return the total number of matches, or -1 if still unknown -- webb.
4702 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004703 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4704 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 *
4706 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004707 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4708 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 */
4710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004711ins_compl_next(
4712 int allow_get_expansion,
4713 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004714 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004715 int insert_match, /* Insert the newly selected match */
4716 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717{
4718 int num_matches = -1;
4719 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004720 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004721 compl_T *found_compl = NULL;
4722 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004723 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004724 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004726 /* When user complete function return -1 for findstart which is next
4727 * time of 'always', compl_shown_match become NULL. */
4728 if (compl_shown_match == NULL)
4729 return -1;
4730
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004731 if (compl_leader != NULL
4732 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004734 /* Set "compl_shown_match" to the actually shown match, it may differ
4735 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004736 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004737 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004738 && compl_shown_match->cp_next != NULL
4739 && compl_shown_match->cp_next != compl_first_match)
4740 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004741
4742 /* If we didn't find it searching forward, and compl_shows_dir is
4743 * backward, find the last match. */
4744 if (compl_shows_dir == BACKWARD
4745 && !ins_compl_equal(compl_shown_match,
4746 compl_leader, (int)STRLEN(compl_leader))
4747 && (compl_shown_match->cp_next == NULL
4748 || compl_shown_match->cp_next == compl_first_match))
4749 {
4750 while (!ins_compl_equal(compl_shown_match,
4751 compl_leader, (int)STRLEN(compl_leader))
4752 && compl_shown_match->cp_prev != NULL
4753 && compl_shown_match->cp_prev != compl_first_match)
4754 compl_shown_match = compl_shown_match->cp_prev;
4755 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004756 }
4757
4758 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004759 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 /* Delete old text to be replaced */
4761 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004762
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004763 /* When finding the longest common text we stick at the original text,
4764 * don't let CTRL-N or CTRL-P move to the first match. */
4765 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4766
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004767 /* When restarting the search don't insert the first match either. */
4768 if (compl_restarting)
4769 {
4770 advance = FALSE;
4771 compl_restarting = FALSE;
4772 }
4773
Bram Moolenaare3226be2005-12-18 22:10:00 +00004774 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4775 * around. */
4776 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004778 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004780 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004781 found_end = (compl_first_match != NULL
4782 && (compl_shown_match->cp_next == compl_first_match
4783 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004784 }
4785 else if (compl_shows_dir == BACKWARD
4786 && compl_shown_match->cp_prev != NULL)
4787 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004788 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004789 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004790 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004793 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004794 if (!allow_get_expansion)
4795 {
4796 if (advance)
4797 {
4798 if (compl_shows_dir == BACKWARD)
4799 compl_pending -= todo + 1;
4800 else
4801 compl_pending += todo + 1;
4802 }
4803 return -1;
4804 }
4805
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004806 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004807 {
4808 if (compl_shows_dir == BACKWARD)
4809 --compl_pending;
4810 else
4811 ++compl_pending;
4812 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004813
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004814 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004815 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004816
4817 /* handle any pending completions */
4818 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004819 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004820 {
4821 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4822 {
4823 compl_shown_match = compl_shown_match->cp_next;
4824 --compl_pending;
4825 }
4826 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4827 {
4828 compl_shown_match = compl_shown_match->cp_prev;
4829 ++compl_pending;
4830 }
4831 else
4832 break;
4833 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004834 found_end = FALSE;
4835 }
4836 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4837 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004838 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004839 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004840 ++todo;
4841 else
4842 /* Remember a matching item. */
4843 found_compl = compl_shown_match;
4844
4845 /* Stop at the end of the list when we found a usable match. */
4846 if (found_end)
4847 {
4848 if (found_compl != NULL)
4849 {
4850 compl_shown_match = found_compl;
4851 break;
4852 }
4853 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004857 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004858 if (compl_no_insert && !started)
4859 {
4860 ins_bytes(compl_orig_text + ins_compl_len());
4861 compl_used_match = FALSE;
4862 }
4863 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004864 {
4865 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004866 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004867 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004868 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004869 }
4870 else
4871 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872
4873 if (!allow_get_expansion)
4874 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004875 /* may undisplay the popup menu first */
4876 ins_compl_upd_pum();
4877
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004878 /* redraw to show the user what was inserted */
4879 update_screen(0);
4880
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004881 /* display the updated popup menu */
4882 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004883#ifdef FEAT_GUI
4884 if (gui.in_use)
4885 {
4886 /* Show the cursor after the match, not after the redrawn text. */
4887 setcursor();
4888 out_flush();
4889 gui_update_cursor(FALSE, FALSE);
4890 }
4891#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004892
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 /* Delete old text to be replaced, since we're still searching and
4894 * don't want to match ourselves! */
4895 ins_compl_delete();
4896 }
4897
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004898 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004899 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004900 if (compl_no_insert && !started)
4901 compl_enter_selects = TRUE;
4902 else
4903 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004904
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 /*
4906 * Show the file name for the match (if any)
4907 * Truncate the file name to avoid a wait for return.
4908 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004909 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
4911 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004912 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 if (i <= 0)
4914 i = 0;
4915 else
4916 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004917 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 msg(IObuff);
4919 redraw_cmdline = FALSE; /* don't overwrite! */
4920 }
4921
4922 return num_matches;
4923}
4924
4925/*
4926 * Call this while finding completions, to check whether the user has hit a key
4927 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004928 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004930 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004931 * "in_compl_func" is TRUE when called from complete_check(), don't set
4932 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 */
4934 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004935ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
4937 static int count = 0;
4938
4939 int c;
4940
4941 /* Don't check when reading keys from a script. That would break the test
4942 * scripts */
4943 if (using_script())
4944 return;
4945
4946 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004947 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 return;
4949 count = 0;
4950
Bram Moolenaara260a972006-06-23 19:36:29 +00004951 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4952 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (c != NUL)
4955 {
4956 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4957 {
4958 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004959 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004960 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02004961 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004963 else
4964 {
4965 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004966 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004967 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004968 if (c != K_IGNORE)
4969 {
4970 /* Don't interrupt completion when the character wasn't typed,
4971 * e.g., when doing @q to replay keys. */
4972 if (c != Ctrl_R && KeyTyped)
4973 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004974
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004975 vungetc(c);
4976 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004979 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00004980 {
4981 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4982
4983 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02004984 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00004985 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004986}
4987
4988/*
4989 * Decide the direction of Insert mode complete from the key typed.
4990 * Returns BACKWARD or FORWARD.
4991 */
4992 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004993ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004994{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004995 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02004996 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004997 return BACKWARD;
4998 return FORWARD;
4999}
5000
5001/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005002 * Return TRUE for keys that are used for completion only when the popup menu
5003 * is visible.
5004 */
5005 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005006ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005007{
5008 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005009 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5010 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005011}
5012
5013/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005014 * Decide the number of completions to move forward.
5015 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5016 */
5017 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005018ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005019{
5020 int h;
5021
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005022 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005023 {
5024 h = pum_get_height();
5025 if (h > 3)
5026 h -= 2; /* keep some context */
5027 return h;
5028 }
5029 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030}
5031
5032/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005033 * Return TRUE if completion with "c" should insert the match, FALSE if only
5034 * to change the currently selected completion.
5035 */
5036 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005037ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005038{
5039 switch (c)
5040 {
5041 case K_UP:
5042 case K_DOWN:
5043 case K_PAGEDOWN:
5044 case K_KPAGEDOWN:
5045 case K_S_DOWN:
5046 case K_PAGEUP:
5047 case K_KPAGEUP:
5048 case K_S_UP:
5049 return FALSE;
5050 }
5051 return TRUE;
5052}
5053
5054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 * Do Insert mode completion.
5056 * Called when character "c" was typed, which has a meaning for completion.
5057 * Returns OK if completion was done, FAIL if something failed (out of mem).
5058 */
5059 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005060ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005062 char_u *line;
5063 int startcol = 0; /* column where searched text starts */
5064 colnr_T curs_col; /* cursor column */
5065 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005066 int save_w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005067 int insert_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
Bram Moolenaare3226be2005-12-18 22:10:00 +00005069 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005070 insert_match = ins_compl_use_match(c);
5071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005072 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 {
5074 /* First time we hit ^N or ^P (in a row, I mean) */
5075
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 did_ai = FALSE;
5077#ifdef FEAT_SMARTINDENT
5078 did_si = FALSE;
5079 can_si = FALSE;
5080 can_si_back = FALSE;
5081#endif
5082 if (stop_arrow() == FAIL)
5083 return FAIL;
5084
5085 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005086 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005087 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005089 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005090 * "compl_startpos" to the cursor as a pattern to add a new word
5091 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005092 * "compl_startpos" is not in the same line as the cursor then fix it
5093 * (the line has been split because it was longer than 'tw'). if SOL
5094 * is set then skip the previous pattern, a word at the beginning of
5095 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005096 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5097 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
5099 /*
5100 * it is a continued search
5101 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005102 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5104 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5105 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005106 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005108 /* line (probably) wrapped, set compl_startpos to the
5109 * first non_blank in the line, if it is not a wordchar
5110 * include it to get a better pattern, but then we don't
5111 * want the "\\<" prefix, check it bellow */
5112 compl_col = (colnr_T)(skipwhite(line) - line);
5113 compl_startpos.col = compl_col;
5114 compl_startpos.lnum = curwin->w_cursor.lnum;
5115 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
5117 else
5118 {
5119 /* S_IPOS was set when we inserted a word that was at the
5120 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005121 * mode but first we need to redefine compl_startpos */
5122 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005124 compl_cont_status |= CONT_SOL;
5125 compl_startpos.col = (colnr_T)(skipwhite(
5126 line + compl_length
5127 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005131 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005132 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005133 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005135 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005137 compl_cont_status &= ~CONT_SOL;
5138 compl_length = (IOSIZE - MIN_SPACE);
5139 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005141 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5142 if (compl_length < 1)
5143 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005145 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005146 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005148 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005151 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005153 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005155 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005157 compl_cont_status = 0;
5158 compl_cont_status |= CONT_N_ADDS;
5159 compl_startpos = curwin->w_cursor;
5160 startcol = (int)curs_col;
5161 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163
5164 /* Work out completion pattern and original text -- webb */
5165 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5166 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5169 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005170 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005172 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005174 compl_col += ++startcol;
5175 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005178 compl_pattern = str_foldcase(line + compl_col,
5179 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005181 compl_pattern = vim_strnsave(line + compl_col,
5182 compl_length);
5183 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 return FAIL;
5185 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005186 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
5188 char_u *prefix = (char_u *)"\\<";
5189
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005190 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005191 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005192 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005193 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005195 if (!vim_iswordp(line + compl_col)
5196 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 && (
5198#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005199 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005201 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202#endif
5203 )))
5204 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005205 STRCPY((char *)compl_pattern, prefix);
5206 (void)quote_meta(compl_pattern + STRLEN(prefix),
5207 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005209 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005211 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005213 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214#endif
5215 )
5216 {
5217 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005218 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5219 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005221 compl_col += curs_col;
5222 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
5224 else
5225 {
5226#ifdef FEAT_MBYTE
5227 /* Search the point of change class of multibyte character
5228 * or not a word single byte character backward. */
5229 if (has_mbyte)
5230 {
5231 int base_class;
5232 int head_off;
5233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005234 startcol -= (*mb_head_off)(line, line + startcol);
5235 base_class = mb_get_class(line + startcol);
5236 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005238 head_off = (*mb_head_off)(line, line + startcol);
5239 if (base_class != mb_get_class(line + startcol
5240 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005242 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244 }
5245 else
5246#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005247 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005249 compl_col += ++startcol;
5250 compl_length = (int)curs_col - startcol;
5251 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 {
5253 /* Only match word with at least two chars -- webb
5254 * there's no need to call quote_meta,
5255 * alloc(7) is enough -- Acevedo
5256 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 compl_pattern = alloc(7);
5258 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005260 STRCPY((char *)compl_pattern, "\\<");
5261 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5262 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
5264 else
5265 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005266 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005267 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005268 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005270 STRCPY((char *)compl_pattern, "\\<");
5271 (void)quote_meta(compl_pattern + 2, line + compl_col,
5272 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 }
5274 }
5275 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005276 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005278 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005279 compl_length = (int)curs_col - (int)compl_col;
5280 if (compl_length < 0) /* cursor in indent: empty pattern */
5281 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005283 compl_pattern = str_foldcase(line + compl_col, compl_length,
5284 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005286 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5287 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 return FAIL;
5289 }
5290 else if (ctrl_x_mode == CTRL_X_FILES)
5291 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005292 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005293 if (startcol > 0)
5294 {
5295 char_u *p = line + startcol;
5296
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005297 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005298 while (p > line && vim_isfilec(PTR2CHAR(p)))
5299 mb_ptr_back(line, p);
5300 if (p == line && vim_isfilec(PTR2CHAR(p)))
5301 startcol = 0;
5302 else
5303 startcol = (int)(p - line) + 1;
5304 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005305
Bram Moolenaar0300e462013-09-08 16:03:45 +02005306 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005307 compl_length = (int)curs_col - startcol;
5308 compl_pattern = addstar(line + compl_col, compl_length,
5309 EXPAND_FILES);
5310 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 return FAIL;
5312 }
5313 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5314 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 compl_pattern = vim_strnsave(line, curs_col);
5316 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005318 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005319 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005320 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5321 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005322 /* No completion possible, use an empty pattern to get a
5323 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005324 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005325 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005326 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5327 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005329 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005330 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005331#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005332 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005333 * Call user defined function 'completefunc' with "a:findstart"
5334 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005335 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005336 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005337 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005338 char_u *funcname;
5339 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005340 win_T *curwin_save;
5341 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005342
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005343 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005344 * string */
5345 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5346 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5347 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005348 {
5349 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5350 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005351 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005352 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005353
5354 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005355 args[1] = NULL;
5356 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005357 curwin_save = curwin;
5358 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005359 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005360 if (curwin_save != curwin || curbuf_save != curbuf)
5361 {
5362 EMSG(_(e_complwin));
5363 return FAIL;
5364 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005365 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005366 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005367 if (!equalpos(curwin->w_cursor, pos))
5368 {
5369 EMSG(_(e_compldel));
5370 return FAIL;
5371 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005372
Bram Moolenaar6110a002012-01-26 18:58:38 +01005373 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005374 * cancel the complete without an error.
5375 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005376 if (col == -2)
5377 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005378 if (col == -3)
5379 {
5380 ctrl_x_mode = 0;
5381 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005382 if (!shortmess(SHM_COMPLETIONMENU))
5383 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005384 return FAIL;
5385 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005386
Bram Moolenaar82139082011-09-14 16:52:09 +02005387 /*
5388 * Reset extended parameters of completion, when start new
5389 * completion.
5390 */
5391 compl_opt_refresh_always = FALSE;
5392
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005393 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005394 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005395 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005396 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005397 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005398
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005399 /* Setup variables for completion. Need to obtain "line" again,
5400 * it may have become invalid. */
5401 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005402 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005403 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5404 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005405#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005406 return FAIL;
5407 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005408 else if (ctrl_x_mode == CTRL_X_SPELL)
5409 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005410#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005411 if (spell_bad_len > 0)
5412 compl_col = curs_col - spell_bad_len;
5413 else
5414 compl_col = spell_word_start(startcol);
5415 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005416 {
5417 compl_length = 0;
5418 compl_col = curs_col;
5419 }
5420 else
5421 {
5422 spell_expand_check_cap(compl_col);
5423 compl_length = (int)curs_col - compl_col;
5424 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005425 /* Need to obtain "line" again, it may have become invalid. */
5426 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005427 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5428 if (compl_pattern == NULL)
5429#endif
5430 return FAIL;
5431 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005432 else
5433 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005434 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005435 return FAIL;
5436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005438 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {
5440 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005441 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
5443 /* Insert a new line, keep indentation but ignore 'comments' */
5444#ifdef FEAT_COMMENTS
5445 char_u *old = curbuf->b_p_com;
5446
5447 curbuf->b_p_com = (char_u *)"";
5448#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005449 compl_startpos.lnum = curwin->w_cursor.lnum;
5450 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 ins_eol('\r');
5452#ifdef FEAT_COMMENTS
5453 curbuf->b_p_com = old;
5454#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005455 compl_length = 0;
5456 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
5458 }
5459 else
5460 {
5461 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005462 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
5464
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005465 if (compl_cont_status & CONT_LOCAL)
5466 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 else
5468 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5469
Bram Moolenaar62951b12011-09-21 18:23:05 +02005470 /* If any of the original typed text has been changed we need to fix
5471 * the redo buffer. */
5472 ins_compl_fixRedoBufForLeader(NULL);
5473
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005474 /* Always add completion for the original text. */
5475 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005476 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5477 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005478 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005480 vim_free(compl_pattern);
5481 compl_pattern = NULL;
5482 vim_free(compl_orig_text);
5483 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 return FAIL;
5485 }
5486
5487 /* showmode might reset the internal line pointers, so it must
5488 * be called before line = ml_get(), or when this address is no
5489 * longer needed. -- Acevedo.
5490 */
5491 edit_submode_extra = (char_u *)_("-- Searching...");
5492 edit_submode_highl = HLF_COUNT;
5493 showmode();
5494 edit_submode_extra = NULL;
5495 out_flush();
5496 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005497 else if (insert_match && stop_arrow() == FAIL)
5498 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005500 compl_shown_match = compl_curr_match;
5501 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502
5503 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005504 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005506 save_w_wrow = curwin->w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005507 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005509 /* may undisplay the popup menu */
5510 ins_compl_upd_pum();
5511
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005512 if (n > 1) /* all matches have been found */
5513 compl_matches = n;
5514 compl_curr_match = compl_shown_match;
5515 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
Bram Moolenaard68071d2006-05-02 22:08:30 +00005517 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5518 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 if (got_int && !global_busy)
5520 {
5521 (void)vgetc();
5522 got_int = FALSE;
5523 }
5524
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005525 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005526 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005528 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5529 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5531 edit_submode_highl = HLF_E;
5532 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5533 * because we couldn't expand anything at first place, but if we used
5534 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5535 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005536 if ( compl_length > 1
5537 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 || (ctrl_x_mode != 0
5539 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5540 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005541 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543
Bram Moolenaar572cb562005-08-05 21:35:02 +00005544 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005545 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005547 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
5549 if (edit_submode_extra == NULL)
5550 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005551 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 {
5553 edit_submode_extra = (char_u *)_("Back at original");
5554 edit_submode_highl = HLF_W;
5555 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005556 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
5558 edit_submode_extra = (char_u *)_("Word from other line");
5559 edit_submode_highl = HLF_COUNT;
5560 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005561 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
5563 edit_submode_extra = (char_u *)_("The only match");
5564 edit_submode_highl = HLF_COUNT;
5565 }
5566 else
5567 {
5568 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005569 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005571 int number = 0;
5572 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005574 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
5576 /* search backwards for the first valid (!= -1) number.
5577 * This should normally succeed already at the first loop
5578 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005579 for (match = compl_curr_match->cp_prev; match != NULL
5580 && match != compl_first_match;
5581 match = match->cp_prev)
5582 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005584 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 break;
5586 }
5587 if (match != NULL)
5588 /* go up and assign all numbers which are not assigned
5589 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005590 for (match = match->cp_next;
5591 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005592 match = match->cp_next)
5593 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595 else /* BACKWARD */
5596 {
5597 /* search forwards (upwards) for the first valid (!= -1)
5598 * number. This should normally succeed already at the
5599 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005600 for (match = compl_curr_match->cp_next; match != NULL
5601 && match != compl_first_match;
5602 match = match->cp_next)
5603 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005605 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 break;
5607 }
5608 if (match != NULL)
5609 /* go down and assign all numbers which are not
5610 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005611 for (match = match->cp_prev; match
5612 && match->cp_number == -1;
5613 match = match->cp_prev)
5614 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 }
5616 }
5617
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005618 /* The match should always have a sequence number now, this is
5619 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005620 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005622 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5623 * Translations may need more than twice that. */
5624 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005626 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005627 vim_snprintf((char *)match_ref, sizeof(match_ref),
5628 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005629 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005631 vim_snprintf((char *)match_ref, sizeof(match_ref),
5632 _("match %d"),
5633 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 edit_submode_extra = match_ref;
5635 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005636 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 curs_columns(FALSE);
5638 }
5639 }
5640 }
5641
5642 /* Show a message about what (completion) mode we're in. */
5643 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005644 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005646 if (edit_submode_extra != NULL)
5647 {
5648 if (!p_smd)
5649 msg_attr(edit_submode_extra,
5650 edit_submode_highl < HLF_COUNT
5651 ? hl_attr(edit_submode_highl) : 0);
5652 }
5653 else
5654 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
Bram Moolenaard68071d2006-05-02 22:08:30 +00005657 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005658 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005659 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005660 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005661 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005662 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005663 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005664
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 return OK;
5666}
5667
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005668 static void
5669show_pum(int save_w_wrow)
5670{
5671 /* RedrawingDisabled may be set when invoked through complete(). */
5672 int n = RedrawingDisabled;
5673
5674 RedrawingDisabled = 0;
5675
5676 /* If the cursor moved we need to remove the pum first. */
5677 setcursor();
5678 if (save_w_wrow != curwin->w_wrow)
5679 ins_compl_del_pum();
5680
5681 ins_compl_show_pum();
5682 setcursor();
5683 RedrawingDisabled = n;
5684}
5685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686/*
5687 * Looks in the first "len" chars. of "src" for search-metachars.
5688 * If dest is not NULL the chars. are copied there quoting (with
5689 * a backslash) the metachars, and dest would be NUL terminated.
5690 * Returns the length (needed) of dest
5691 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005692 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005693quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005695 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005697 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 {
5699 switch (*src)
5700 {
5701 case '.':
5702 case '*':
5703 case '[':
5704 if (ctrl_x_mode == CTRL_X_DICTIONARY
5705 || ctrl_x_mode == CTRL_X_THESAURUS)
5706 break;
5707 case '~':
5708 if (!p_magic) /* quote these only if magic is set */
5709 break;
5710 case '\\':
5711 if (ctrl_x_mode == CTRL_X_DICTIONARY
5712 || ctrl_x_mode == CTRL_X_THESAURUS)
5713 break;
5714 case '^': /* currently it's not needed. */
5715 case '$':
5716 m++;
5717 if (dest != NULL)
5718 *dest++ = '\\';
5719 break;
5720 }
5721 if (dest != NULL)
5722 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005723# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 /* Copy remaining bytes of a multibyte character. */
5725 if (has_mbyte)
5726 {
5727 int i, mb_len;
5728
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005729 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 if (mb_len > 0 && len >= mb_len)
5731 for (i = 0; i < mb_len; ++i)
5732 {
5733 --len;
5734 ++src;
5735 if (dest != NULL)
5736 *dest++ = *src;
5737 }
5738 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 }
5741 if (dest != NULL)
5742 *dest = NUL;
5743
5744 return m;
5745}
5746#endif /* FEAT_INS_EXPAND */
5747
5748/*
5749 * Next character is interpreted literally.
5750 * A one, two or three digit decimal number is interpreted as its byte value.
5751 * If one or two digits are entered, the next character is given to vungetc().
5752 * For Unicode a character > 255 may be returned.
5753 */
5754 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005755get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
5757 int cc;
5758 int nc;
5759 int i;
5760 int hex = FALSE;
5761 int octal = FALSE;
5762#ifdef FEAT_MBYTE
5763 int unicode = 0;
5764#endif
5765
5766 if (got_int)
5767 return Ctrl_C;
5768
5769#ifdef FEAT_GUI
5770 /*
5771 * In GUI there is no point inserting the internal code for a special key.
5772 * It is more useful to insert the string "<KEY>" instead. This would
5773 * probably be useful in a text window too, but it would not be
5774 * vi-compatible (maybe there should be an option for it?) -- webb
5775 */
5776 if (gui.in_use)
5777 ++allow_keys;
5778#endif
5779#ifdef USE_ON_FLY_SCROLL
5780 dont_scroll = TRUE; /* disallow scrolling here */
5781#endif
5782 ++no_mapping; /* don't map the next key hits */
5783 cc = 0;
5784 i = 0;
5785 for (;;)
5786 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005787 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788#ifdef FEAT_CMDL_INFO
5789 if (!(State & CMDLINE)
5790# ifdef FEAT_MBYTE
5791 && MB_BYTE2LEN_CHECK(nc) == 1
5792# endif
5793 )
5794 add_to_showcmd(nc);
5795#endif
5796 if (nc == 'x' || nc == 'X')
5797 hex = TRUE;
5798 else if (nc == 'o' || nc == 'O')
5799 octal = TRUE;
5800#ifdef FEAT_MBYTE
5801 else if (nc == 'u' || nc == 'U')
5802 unicode = nc;
5803#endif
5804 else
5805 {
5806 if (hex
5807#ifdef FEAT_MBYTE
5808 || unicode != 0
5809#endif
5810 )
5811 {
5812 if (!vim_isxdigit(nc))
5813 break;
5814 cc = cc * 16 + hex2nr(nc);
5815 }
5816 else if (octal)
5817 {
5818 if (nc < '0' || nc > '7')
5819 break;
5820 cc = cc * 8 + nc - '0';
5821 }
5822 else
5823 {
5824 if (!VIM_ISDIGIT(nc))
5825 break;
5826 cc = cc * 10 + nc - '0';
5827 }
5828
5829 ++i;
5830 }
5831
5832 if (cc > 255
5833#ifdef FEAT_MBYTE
5834 && unicode == 0
5835#endif
5836 )
5837 cc = 255; /* limit range to 0-255 */
5838 nc = 0;
5839
5840 if (hex) /* hex: up to two chars */
5841 {
5842 if (i >= 2)
5843 break;
5844 }
5845#ifdef FEAT_MBYTE
5846 else if (unicode) /* Unicode: up to four or eight chars */
5847 {
5848 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5849 break;
5850 }
5851#endif
5852 else if (i >= 3) /* decimal or octal: up to three chars */
5853 break;
5854 }
5855 if (i == 0) /* no number entered */
5856 {
5857 if (nc == K_ZERO) /* NUL is stored as NL */
5858 {
5859 cc = '\n';
5860 nc = 0;
5861 }
5862 else
5863 {
5864 cc = nc;
5865 nc = 0;
5866 }
5867 }
5868
5869 if (cc == 0) /* NUL is stored as NL */
5870 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005871#ifdef FEAT_MBYTE
5872 if (enc_dbcs && (cc & 0xff) == 0)
5873 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5874 second byte will cause trouble! */
5875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876
5877 --no_mapping;
5878#ifdef FEAT_GUI
5879 if (gui.in_use)
5880 --allow_keys;
5881#endif
5882 if (nc)
5883 vungetc(nc);
5884 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5885 return cc;
5886}
5887
5888/*
5889 * Insert character, taking care of special keys and mod_mask
5890 */
5891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005892insert_special(
5893 int c,
5894 int allow_modmask,
5895 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
5897 char_u *p;
5898 int len;
5899
5900 /*
5901 * Special function key, translate into "<Key>". Up to the last '>' is
5902 * inserted with ins_str(), so as not to replace characters in replace
5903 * mode.
5904 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5905 * unless 'allow_modmask' is TRUE.
5906 */
5907#ifdef MACOS
5908 /* Command-key never produces a normal key */
5909 if (mod_mask & MOD_MASK_CMD)
5910 allow_modmask = TRUE;
5911#endif
5912 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5913 {
5914 p = get_special_key_name(c, mod_mask);
5915 len = (int)STRLEN(p);
5916 c = p[len - 1];
5917 if (len > 2)
5918 {
5919 if (stop_arrow() == FAIL)
5920 return;
5921 p[len - 1] = NUL;
5922 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005923 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 ctrlv = FALSE;
5925 }
5926 }
5927 if (stop_arrow() == OK)
5928 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5929}
5930
5931/*
5932 * Special characters in this context are those that need processing other
5933 * than the simple insertion that can be performed here. This includes ESC
5934 * which terminates the insert, and CR/NL which need special processing to
5935 * open up a new line. This routine tries to optimize insertions performed by
5936 * the "redo", "undo" or "put" commands, so it needs to know when it should
5937 * stop and defer processing to the "normal" mechanism.
5938 * '0' and '^' are special, because they can be followed by CTRL-D.
5939 */
5940#ifdef EBCDIC
5941# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5942#else
5943# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5944#endif
5945
5946#ifdef FEAT_MBYTE
5947# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5948#else
5949# define WHITECHAR(cc) vim_iswhite(cc)
5950#endif
5951
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005952/*
5953 * "flags": INSCHAR_FORMAT - force formatting
5954 * INSCHAR_CTRLV - char typed just after CTRL-V
5955 * INSCHAR_NO_FEX - don't use 'formatexpr'
5956 *
5957 * NOTE: passes the flags value straight through to internal_format() which,
5958 * beside INSCHAR_FORMAT (above), is also looking for these:
5959 * INSCHAR_DO_COM - format comments
5960 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005963insertchar(
5964 int c, /* character to insert or NUL */
5965 int flags, /* INSCHAR_FORMAT, etc. */
5966 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 int textwidth;
5969#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005973 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005975 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
5978 /*
5979 * Try to break the line in two or more pieces when:
5980 * - Always do this if we have been called to do formatting only.
5981 * - Always do this when 'formatoptions' has the 'a' flag and the line
5982 * ends in white space.
5983 * - Otherwise:
5984 * - Don't do this if inserting a blank
5985 * - Don't do this if an existing character is being replaced, unless
5986 * we're in VREPLACE mode.
5987 * - Do this if the cursor is not on the line where insert started
5988 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5989 * before the insert.
5990 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5991 * before 'textwidth'
5992 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005993 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005994 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 || (!vim_iswhite(c)
5996 && !((State & REPLACE_FLAG)
5997#ifdef FEAT_VREPLACE
5998 && !(State & VREPLACE_FLAG)
5999#endif
6000 && *ml_get_cursor() != NUL)
6001 && (curwin->w_cursor.lnum != Insstart.lnum
6002 || ((!has_format_option(FO_INS_LONG)
6003 || Insstart_textlen <= (colnr_T)textwidth)
6004 && (!fo_ins_blank
6005 || Insstart_blank_vcol <= (colnr_T)textwidth
6006 ))))))
6007 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006008 /* Format with 'formatexpr' when it's set. Use internal formatting
6009 * when 'formatexpr' isn't set or it returns non-zero. */
6010#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006011 int do_internal = TRUE;
6012 colnr_T virtcol = get_nolist_virtcol()
6013 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006014
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006015 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6016 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006017 {
6018 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6019 /* It may be required to save for undo again, e.g. when setline()
6020 * was called. */
6021 ins_need_undo = TRUE;
6022 }
6023 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006025 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006027
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 if (c == NUL) /* only formatting was wanted */
6029 return;
6030
6031#ifdef FEAT_COMMENTS
6032 /* Check whether this character should end a comment. */
6033 if (did_ai && (int)c == end_comment_pending)
6034 {
6035 char_u *line;
6036 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6037 int middle_len, end_len;
6038 int i;
6039
6040 /*
6041 * Need to remove existing (middle) comment leader and insert end
6042 * comment leader. First, check what comment leader we can find.
6043 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006044 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6046 {
6047 /* Skip middle-comment string */
6048 while (*p && p[-1] != ':') /* find end of middle flags */
6049 ++p;
6050 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6051 /* Don't count trailing white space for middle_len */
6052 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6053 --middle_len;
6054
6055 /* Find the end-comment string */
6056 while (*p && p[-1] != ':') /* find end of end flags */
6057 ++p;
6058 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6059
6060 /* Skip white space before the cursor */
6061 i = curwin->w_cursor.col;
6062 while (--i >= 0 && vim_iswhite(line[i]))
6063 ;
6064 i++;
6065
6066 /* Skip to before the middle leader */
6067 i -= middle_len;
6068
6069 /* Check some expected things before we go on */
6070 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6071 {
6072 /* Backspace over all the stuff we want to replace */
6073 backspace_until_column(i);
6074
6075 /*
6076 * Insert the end-comment string, except for the last
6077 * character, which will get inserted as normal later.
6078 */
6079 ins_bytes_len(lead_end, end_len - 1);
6080 }
6081 }
6082 }
6083 end_comment_pending = NUL;
6084#endif
6085
6086 did_ai = FALSE;
6087#ifdef FEAT_SMARTINDENT
6088 did_si = FALSE;
6089 can_si = FALSE;
6090 can_si_back = FALSE;
6091#endif
6092
6093 /*
6094 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6095 * This speeds up normal text input considerably.
6096 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6097 * need to re-indent at a ':', or any other character (but not what
6098 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006099 * Don't do this when there an InsertCharPre autocommand is defined,
6100 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 */
6102#ifdef USE_ON_FLY_SCROLL
6103 dont_scroll = FALSE; /* allow scrolling here */
6104#endif
6105
6106 if ( !ISSPECIAL(c)
6107#ifdef FEAT_MBYTE
6108 && (!has_mbyte || (*mb_char2len)(c) == 1)
6109#endif
6110 && vpeekc() != NUL
6111 && !(State & REPLACE_FLAG)
6112#ifdef FEAT_CINDENT
6113 && !cindent_on()
6114#endif
6115#ifdef FEAT_RIGHTLEFT
6116 && !p_ri
6117#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006118#ifdef FEAT_AUTOCMD
6119 && !has_insertcharpre()
6120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 )
6122 {
6123#define INPUT_BUFLEN 100
6124 char_u buf[INPUT_BUFLEN + 1];
6125 int i;
6126 colnr_T virtcol = 0;
6127
6128 buf[0] = c;
6129 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006130 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 virtcol = get_nolist_virtcol();
6132 /*
6133 * Stop the string when:
6134 * - no more chars available
6135 * - finding a special character (command key)
6136 * - buffer is full
6137 * - running into the 'textwidth' boundary
6138 * - need to check for abbreviation: A non-word char after a word-char
6139 */
6140 while ( (c = vpeekc()) != NUL
6141 && !ISSPECIAL(c)
6142#ifdef FEAT_MBYTE
6143 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6144#endif
6145 && i < INPUT_BUFLEN
6146 && (textwidth == 0
6147 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6148 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6149 {
6150#ifdef FEAT_RIGHTLEFT
6151 c = vgetc();
6152 if (p_hkmap && KeyTyped)
6153 c = hkmap(c); /* Hebrew mode mapping */
6154# ifdef FEAT_FKMAP
6155 if (p_fkmap && KeyTyped)
6156 c = fkmap(c); /* Farsi mode mapping */
6157# endif
6158 buf[i++] = c;
6159#else
6160 buf[i++] = vgetc();
6161#endif
6162 }
6163
6164#ifdef FEAT_DIGRAPHS
6165 do_digraph(-1); /* clear digraphs */
6166 do_digraph(buf[i-1]); /* may be the start of a digraph */
6167#endif
6168 buf[i] = NUL;
6169 ins_str(buf);
6170 if (flags & INSCHAR_CTRLV)
6171 {
6172 redo_literal(*buf);
6173 i = 1;
6174 }
6175 else
6176 i = 0;
6177 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006178 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 }
6180 else
6181 {
6182#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006183 int cc;
6184
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6186 {
6187 char_u buf[MB_MAXBYTES + 1];
6188
6189 (*mb_char2bytes)(c, buf);
6190 buf[cc] = NUL;
6191 ins_char_bytes(buf, cc);
6192 AppendCharToRedobuff(c);
6193 }
6194 else
6195#endif
6196 {
6197 ins_char(c);
6198 if (flags & INSCHAR_CTRLV)
6199 redo_literal(c);
6200 else
6201 AppendCharToRedobuff(c);
6202 }
6203 }
6204}
6205
6206/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006207 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006208 *
6209 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6210 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006211 */
6212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006213internal_format(
6214 int textwidth,
6215 int second_indent,
6216 int flags,
6217 int format_only,
6218 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006219{
6220 int cc;
6221 int save_char = NUL;
6222 int haveto_redraw = FALSE;
6223 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6224#ifdef FEAT_MBYTE
6225 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6226#endif
6227 int fo_white_par = has_format_option(FO_WHITE_PAR);
6228 int first_line = TRUE;
6229#ifdef FEAT_COMMENTS
6230 colnr_T leader_len;
6231 int no_leader = FALSE;
6232 int do_comments = (flags & INSCHAR_DO_COM);
6233#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006234#ifdef FEAT_LINEBREAK
6235 int has_lbr = curwin->w_p_lbr;
6236
6237 /* make sure win_lbr_chartabsize() counts correctly */
6238 curwin->w_p_lbr = FALSE;
6239#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006240
6241 /*
6242 * When 'ai' is off we don't want a space under the cursor to be
6243 * deleted. Replace it with an 'x' temporarily.
6244 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006245 if (!curbuf->b_p_ai
6246#ifdef FEAT_VREPLACE
6247 && !(State & VREPLACE_FLAG)
6248#endif
6249 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006250 {
6251 cc = gchar_cursor();
6252 if (vim_iswhite(cc))
6253 {
6254 save_char = cc;
6255 pchar_cursor('x');
6256 }
6257 }
6258
6259 /*
6260 * Repeat breaking lines, until the current line is not too long.
6261 */
6262 while (!got_int)
6263 {
6264 int startcol; /* Cursor column at entry */
6265 int wantcol; /* column at textwidth border */
6266 int foundcol; /* column for start of spaces */
6267 int end_foundcol = 0; /* column for start of word */
6268 colnr_T len;
6269 colnr_T virtcol;
6270#ifdef FEAT_VREPLACE
6271 int orig_col = 0;
6272 char_u *saved_text = NULL;
6273#endif
6274 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006275 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006276
Bram Moolenaar97b98102009-11-17 16:41:01 +00006277 virtcol = get_nolist_virtcol()
6278 + char2cells(c != NUL ? c : gchar_cursor());
6279 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006280 break;
6281
6282#ifdef FEAT_COMMENTS
6283 if (no_leader)
6284 do_comments = FALSE;
6285 else if (!(flags & INSCHAR_FORMAT)
6286 && has_format_option(FO_WRAP_COMS))
6287 do_comments = TRUE;
6288
6289 /* Don't break until after the comment leader */
6290 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006291 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006292 else
6293 leader_len = 0;
6294
6295 /* If the line doesn't start with a comment leader, then don't
6296 * start one in a following broken line. Avoids that a %word
6297 * moved to the start of the next line causes all following lines
6298 * to start with %. */
6299 if (leader_len == 0)
6300 no_leader = TRUE;
6301#endif
6302 if (!(flags & INSCHAR_FORMAT)
6303#ifdef FEAT_COMMENTS
6304 && leader_len == 0
6305#endif
6306 && !has_format_option(FO_WRAP))
6307
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006308 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006309 if ((startcol = curwin->w_cursor.col) == 0)
6310 break;
6311
6312 /* find column of textwidth border */
6313 coladvance((colnr_T)textwidth);
6314 wantcol = curwin->w_cursor.col;
6315
Bram Moolenaar97b98102009-11-17 16:41:01 +00006316 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006317 foundcol = 0;
6318
6319 /*
6320 * Find position to break at.
6321 * Stop at first entered white when 'formatoptions' has 'v'
6322 */
6323 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006324 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006325 || curwin->w_cursor.lnum != Insstart.lnum
6326 || curwin->w_cursor.col >= Insstart.col)
6327 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006328 if (curwin->w_cursor.col == startcol && c != NUL)
6329 cc = c;
6330 else
6331 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006332 if (WHITECHAR(cc))
6333 {
6334 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006335 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006336
6337 /* find start of sequence of blanks */
6338 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6339 {
6340 dec_cursor();
6341 cc = gchar_cursor();
6342 }
6343 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6344 break; /* only spaces in front of text */
6345#ifdef FEAT_COMMENTS
6346 /* Don't break until after the comment leader */
6347 if (curwin->w_cursor.col < leader_len)
6348 break;
6349#endif
6350 if (has_format_option(FO_ONE_LETTER))
6351 {
6352 /* do not break after one-letter words */
6353 if (curwin->w_cursor.col == 0)
6354 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006355#ifdef FEAT_COMMENTS
6356 /* do not break "#a b" when 'tw' is 2 */
6357 if (curwin->w_cursor.col <= leader_len)
6358 break;
6359#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006360 col = curwin->w_cursor.col;
6361 dec_cursor();
6362 cc = gchar_cursor();
6363
6364 if (WHITECHAR(cc))
6365 continue; /* one-letter, continue */
6366 curwin->w_cursor.col = col;
6367 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006368
6369 inc_cursor();
6370
6371 end_foundcol = end_col + 1;
6372 foundcol = curwin->w_cursor.col;
6373 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006374 break;
6375 }
6376#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006377 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006378 {
6379 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006380 if (curwin->w_cursor.col != startcol)
6381 {
6382#ifdef FEAT_COMMENTS
6383 /* Don't break until after the comment leader */
6384 if (curwin->w_cursor.col < leader_len)
6385 break;
6386#endif
6387 col = curwin->w_cursor.col;
6388 inc_cursor();
6389 /* Don't change end_foundcol if already set. */
6390 if (foundcol != curwin->w_cursor.col)
6391 {
6392 foundcol = curwin->w_cursor.col;
6393 end_foundcol = foundcol;
6394 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6395 break;
6396 }
6397 curwin->w_cursor.col = col;
6398 }
6399
6400 if (curwin->w_cursor.col == 0)
6401 break;
6402
6403 col = curwin->w_cursor.col;
6404
6405 dec_cursor();
6406 cc = gchar_cursor();
6407
6408 if (WHITECHAR(cc))
6409 continue; /* break with space */
6410#ifdef FEAT_COMMENTS
6411 /* Don't break until after the comment leader */
6412 if (curwin->w_cursor.col < leader_len)
6413 break;
6414#endif
6415
6416 curwin->w_cursor.col = col;
6417
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006418 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006419 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006420 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6421 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006422 }
6423#endif
6424 if (curwin->w_cursor.col == 0)
6425 break;
6426 dec_cursor();
6427 }
6428
6429 if (foundcol == 0) /* no spaces, cannot break line */
6430 {
6431 curwin->w_cursor.col = startcol;
6432 break;
6433 }
6434
6435 /* Going to break the line, remove any "$" now. */
6436 undisplay_dollar();
6437
6438 /*
6439 * Offset between cursor position and line break is used by replace
6440 * stack functions. VREPLACE does not use this, and backspaces
6441 * over the text instead.
6442 */
6443#ifdef FEAT_VREPLACE
6444 if (State & VREPLACE_FLAG)
6445 orig_col = startcol; /* Will start backspacing from here */
6446 else
6447#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006448 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006449
6450 /*
6451 * adjust startcol for spaces that will be deleted and
6452 * characters that will remain on top line
6453 */
6454 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006455 while ((cc = gchar_cursor(), WHITECHAR(cc))
6456 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006457 inc_cursor();
6458 startcol -= curwin->w_cursor.col;
6459 if (startcol < 0)
6460 startcol = 0;
6461
6462#ifdef FEAT_VREPLACE
6463 if (State & VREPLACE_FLAG)
6464 {
6465 /*
6466 * In VREPLACE mode, we will backspace over the text to be
6467 * wrapped, so save a copy now to put on the next line.
6468 */
6469 saved_text = vim_strsave(ml_get_cursor());
6470 curwin->w_cursor.col = orig_col;
6471 if (saved_text == NULL)
6472 break; /* Can't do it, out of memory */
6473 saved_text[startcol] = NUL;
6474
6475 /* Backspace over characters that will move to the next line */
6476 if (!fo_white_par)
6477 backspace_until_column(foundcol);
6478 }
6479 else
6480#endif
6481 {
6482 /* put cursor after pos. to break line */
6483 if (!fo_white_par)
6484 curwin->w_cursor.col = foundcol;
6485 }
6486
6487 /*
6488 * Split the line just before the margin.
6489 * Only insert/delete lines, but don't really redraw the window.
6490 */
6491 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6492 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6493#ifdef FEAT_COMMENTS
6494 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006495 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006496#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006497 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6498 if (!(flags & INSCHAR_COM_LIST))
6499 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006500
6501 replace_offset = 0;
6502 if (first_line)
6503 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006504 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006505 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006506 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006507 * This section is for auto-wrap of numeric lists. When not
6508 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6509 * flag will be set and open_line() will handle it (as seen
6510 * above). The code here (and in get_number_indent()) will
6511 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006512 */
6513 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006514 second_indent =
6515 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006516 if (second_indent >= 0)
6517 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006518#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006519 if (State & VREPLACE_FLAG)
6520 change_indent(INDENT_SET, second_indent,
6521 FALSE, NUL, TRUE);
6522 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006523#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006524#ifdef FEAT_COMMENTS
6525 if (leader_len > 0 && second_indent - leader_len > 0)
6526 {
6527 int i;
6528 int padding = second_indent - leader_len;
6529
6530 /* We started at the first_line of a numbered list
6531 * that has a comment. the open_line() function has
6532 * inserted the proper comment leader and positioned
6533 * the cursor at the end of the split line. Now we
6534 * add the additional whitespace needed after the
6535 * comment leader for the numbered list. */
6536 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006537 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006538 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006539 }
6540 else
6541 {
6542#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006543 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006544#ifdef FEAT_COMMENTS
6545 }
6546#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006547 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006548 }
6549 first_line = FALSE;
6550 }
6551
6552#ifdef FEAT_VREPLACE
6553 if (State & VREPLACE_FLAG)
6554 {
6555 /*
6556 * In VREPLACE mode we have backspaced over the text to be
6557 * moved, now we re-insert it into the new line.
6558 */
6559 ins_bytes(saved_text);
6560 vim_free(saved_text);
6561 }
6562 else
6563#endif
6564 {
6565 /*
6566 * Check if cursor is not past the NUL off the line, cindent
6567 * may have added or removed indent.
6568 */
6569 curwin->w_cursor.col += startcol;
6570 len = (colnr_T)STRLEN(ml_get_curline());
6571 if (curwin->w_cursor.col > len)
6572 curwin->w_cursor.col = len;
6573 }
6574
6575 haveto_redraw = TRUE;
6576#ifdef FEAT_CINDENT
6577 can_cindent = TRUE;
6578#endif
6579 /* moved the cursor, don't autoindent or cindent now */
6580 did_ai = FALSE;
6581#ifdef FEAT_SMARTINDENT
6582 did_si = FALSE;
6583 can_si = FALSE;
6584 can_si_back = FALSE;
6585#endif
6586 line_breakcheck();
6587 }
6588
6589 if (save_char != NUL) /* put back space after cursor */
6590 pchar_cursor(save_char);
6591
Bram Moolenaar0026d472014-09-09 16:32:39 +02006592#ifdef FEAT_LINEBREAK
6593 curwin->w_p_lbr = has_lbr;
6594#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006595 if (!format_only && haveto_redraw)
6596 {
6597 update_topline();
6598 redraw_curbuf_later(VALID);
6599 }
6600}
6601
6602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 * Called after inserting or deleting text: When 'formatoptions' includes the
6604 * 'a' flag format from the current line until the end of the paragraph.
6605 * Keep the cursor at the same position relative to the text.
6606 * The caller must have saved the cursor line for undo, following ones will be
6607 * saved here.
6608 */
6609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006610auto_format(
6611 int trailblank, /* when TRUE also format with trailing blank */
6612 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
6614 pos_T pos;
6615 colnr_T len;
6616 char_u *old;
6617 char_u *new, *pnew;
6618 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006619 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620
6621 if (!has_format_option(FO_AUTO))
6622 return;
6623
6624 pos = curwin->w_cursor;
6625 old = ml_get_curline();
6626
6627 /* may remove added space */
6628 check_auto_format(FALSE);
6629
6630 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6631 * user might insert normal text next. Also skip formatting when "1" is
6632 * in 'formatoptions' and there is a single character before the cursor.
6633 * Otherwise the line would be broken and when typing another non-white
6634 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006635 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 if (*old != NUL && !trailblank && wasatend)
6637 {
6638 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006639 cc = gchar_cursor();
6640 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6641 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006643 cc = gchar_cursor();
6644 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
6646 curwin->w_cursor = pos;
6647 return;
6648 }
6649 curwin->w_cursor = pos;
6650 }
6651
6652#ifdef FEAT_COMMENTS
6653 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6654 * comments. */
6655 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006656 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 return;
6658#endif
6659
6660 /*
6661 * May start formatting in a previous line, so that after "x" a word is
6662 * moved to the previous line if it fits there now. Only when this is not
6663 * the start of a paragraph.
6664 */
6665 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6666 {
6667 --curwin->w_cursor.lnum;
6668 if (u_save_cursor() == FAIL)
6669 return;
6670 }
6671
6672 /*
6673 * Do the formatting and restore the cursor position. "saved_cursor" will
6674 * be adjusted for the text formatting.
6675 */
6676 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006677 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 curwin->w_cursor = saved_cursor;
6679 saved_cursor.lnum = 0;
6680
6681 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6682 {
6683 /* "cannot happen" */
6684 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6685 coladvance((colnr_T)MAXCOL);
6686 }
6687 else
6688 check_cursor_col();
6689
6690 /* Insert mode: If the cursor is now after the end of the line while it
6691 * previously wasn't, the line was broken. Because of the rule above we
6692 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6693 * formatted. */
6694 if (!wasatend && has_format_option(FO_WHITE_PAR))
6695 {
6696 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006697 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if (curwin->w_cursor.col == len)
6699 {
6700 pnew = vim_strnsave(new, len + 2);
6701 pnew[len] = ' ';
6702 pnew[len + 1] = NUL;
6703 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6704 /* remove the space later */
6705 did_add_space = TRUE;
6706 }
6707 else
6708 /* may remove added space */
6709 check_auto_format(FALSE);
6710 }
6711
6712 check_cursor();
6713}
6714
6715/*
6716 * When an extra space was added to continue a paragraph for auto-formatting,
6717 * delete it now. The space must be under the cursor, just after the insert
6718 * position.
6719 */
6720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006721check_auto_format(
6722 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
6724 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006725 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726
6727 if (did_add_space)
6728 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006729 cc = gchar_cursor();
6730 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 /* Somehow the space was removed already. */
6732 did_add_space = FALSE;
6733 else
6734 {
6735 if (!end_insert)
6736 {
6737 inc_cursor();
6738 c = gchar_cursor();
6739 dec_cursor();
6740 }
6741 if (c != NUL)
6742 {
6743 /* The space is no longer at the end of the line, delete it. */
6744 del_char(FALSE);
6745 did_add_space = FALSE;
6746 }
6747 }
6748 }
6749}
6750
6751/*
6752 * Find out textwidth to be used for formatting:
6753 * if 'textwidth' option is set, use it
6754 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6755 * if invalid value, use 0.
6756 * Set default to window width (maximum 79) for "gq" operator.
6757 */
6758 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006759comp_textwidth(
6760 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
6762 int textwidth;
6763
6764 textwidth = curbuf->b_p_tw;
6765 if (textwidth == 0 && curbuf->b_p_wm)
6766 {
6767 /* The width is the window width minus 'wrapmargin' minus all the
6768 * things that add to the margin. */
6769 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6770#ifdef FEAT_CMDWIN
6771 if (cmdwin_type != 0)
6772 textwidth -= 1;
6773#endif
6774#ifdef FEAT_FOLDING
6775 textwidth -= curwin->w_p_fdc;
6776#endif
6777#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006778 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 textwidth -= 1;
6780#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006781 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 textwidth -= 8;
6783 }
6784 if (textwidth < 0)
6785 textwidth = 0;
6786 if (ff && textwidth == 0)
6787 {
6788 textwidth = W_WIDTH(curwin) - 1;
6789 if (textwidth > 79)
6790 textwidth = 79;
6791 }
6792 return textwidth;
6793}
6794
6795/*
6796 * Put a character in the redo buffer, for when just after a CTRL-V.
6797 */
6798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006799redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800{
6801 char_u buf[10];
6802
6803 /* Only digits need special treatment. Translate them into a string of
6804 * three digits. */
6805 if (VIM_ISDIGIT(c))
6806 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006807 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 AppendToRedobuff(buf);
6809 }
6810 else
6811 AppendCharToRedobuff(c);
6812}
6813
6814/*
6815 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006816 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 */
6818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006819start_arrow(
6820 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006822 start_arrow_common(end_insert_pos, TRUE);
6823}
6824
6825/*
6826 * Like start_arrow() but with end_change argument.
6827 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6828 */
6829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006830start_arrow_with_change(
6831 pos_T *end_insert_pos, /* can be NULL */
6832 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006833{
6834 start_arrow_common(end_insert_pos, end_change);
6835 if (!end_change)
6836 {
6837 AppendCharToRedobuff(Ctrl_G);
6838 AppendCharToRedobuff('U');
6839 }
6840}
6841
6842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006843start_arrow_common(
6844 pos_T *end_insert_pos, /* can be NULL */
6845 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006846{
6847 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {
6849 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006850 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 arrow_used = TRUE; /* this means we stopped the current insert */
6852 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006853#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006854 check_spell_redraw();
6855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856}
6857
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006858#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006859/*
6860 * If we skipped highlighting word at cursor, do it now.
6861 * It may be skipped again, thus reset spell_redraw_lnum first.
6862 */
6863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006864check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006865{
6866 if (spell_redraw_lnum != 0)
6867 {
6868 linenr_T lnum = spell_redraw_lnum;
6869
6870 spell_redraw_lnum = 0;
6871 redrawWinline(lnum, FALSE);
6872 }
6873}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006874
6875/*
6876 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6877 * spelled word, if there is one.
6878 */
6879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006880spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006881{
6882 pos_T tpos = curwin->w_cursor;
6883
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006884 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006885 if (curwin->w_cursor.col != tpos.col)
6886 start_arrow(&tpos);
6887}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006888#endif
6889
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890/*
6891 * stop_arrow() is called before a change is made in insert mode.
6892 * If an arrow key has been used, start a new insertion.
6893 * Returns FAIL if undo is impossible, shouldn't insert then.
6894 */
6895 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006896stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
6898 if (arrow_used)
6899 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006900 Insstart = curwin->w_cursor; /* new insertion starts here */
6901 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6902 /* Don't update the original insert position when moved to the
6903 * right, except when nothing was inserted yet. */
6904 update_Insstart_orig = FALSE;
6905 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6906
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 if (u_save_cursor() == OK)
6908 {
6909 arrow_used = FALSE;
6910 ins_need_undo = FALSE;
6911 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006912
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 ai_col = 0;
6914#ifdef FEAT_VREPLACE
6915 if (State & VREPLACE_FLAG)
6916 {
6917 orig_line_count = curbuf->b_ml.ml_line_count;
6918 vr_lines_changed = 1;
6919 }
6920#endif
6921 ResetRedobuff();
6922 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006923 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 }
6925 else if (ins_need_undo)
6926 {
6927 if (u_save_cursor() == OK)
6928 ins_need_undo = FALSE;
6929 }
6930
6931#ifdef FEAT_FOLDING
6932 /* Always open fold at the cursor line when inserting something. */
6933 foldOpenCursor();
6934#endif
6935
6936 return (arrow_used || ins_need_undo ? FAIL : OK);
6937}
6938
6939/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006940 * Do a few things to stop inserting.
6941 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6942 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 */
6944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006945stop_insert(
6946 pos_T *end_insert_pos,
6947 int esc, /* called by ins_esc() */
6948 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006950 int cc;
6951 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
6953 stop_redo_ins();
6954 replace_flush(); /* abandon replace stack */
6955
6956 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006957 * Save the inserted text for later redo with ^@ and CTRL-A.
6958 * Don't do it when "restart_edit" was set and nothing was inserted,
6959 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006961 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006962 if (did_restart_edit == 0 || (ptr != NULL
6963 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006964 {
6965 vim_free(last_insert);
6966 last_insert = ptr;
6967 last_insert_skip = new_insert_skip;
6968 }
6969 else
6970 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006972 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {
6974 /* Auto-format now. It may seem strange to do this when stopping an
6975 * insertion (or moving the cursor), but it's required when appending
6976 * a line and having it end in a space. But only do it when something
6977 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006978 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006980 pos_T tpos = curwin->w_cursor;
6981
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 /* When the cursor is at the end of the line after a space the
6983 * formatting will move it to the following word. Avoid that by
6984 * moving the cursor onto the space. */
6985 cc = 'x';
6986 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6987 {
6988 dec_cursor();
6989 cc = gchar_cursor();
6990 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006991 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 }
6993
6994 auto_format(TRUE, FALSE);
6995
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006996 if (vim_iswhite(cc))
6997 {
6998 if (gchar_cursor() != NUL)
6999 inc_cursor();
7000#ifdef FEAT_VIRTUALEDIT
7001 /* If the cursor is still at the same character, also keep
7002 * the "coladd". */
7003 if (gchar_cursor() == NUL
7004 && curwin->w_cursor.lnum == tpos.lnum
7005 && curwin->w_cursor.col == tpos.col)
7006 curwin->w_cursor.coladd = tpos.coladd;
7007#endif
7008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 }
7010
7011 /* If a space was inserted for auto-formatting, remove it now. */
7012 check_auto_format(TRUE);
7013
7014 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007015 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007016 * Do this when ESC was used or moving the cursor up/down.
7017 * Check for the old position still being valid, just in case the text
7018 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007019 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007020 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7021 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007023 pos_T tpos = curwin->w_cursor;
7024
7025 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007026 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007027 for (;;)
7028 {
7029 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7030 --curwin->w_cursor.col;
7031 cc = gchar_cursor();
7032 if (!vim_iswhite(cc))
7033 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007034 if (del_char(TRUE) == FAIL)
7035 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007036 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007037 if (curwin->w_cursor.lnum != tpos.lnum)
7038 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007039 else
7040 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007041 /* reset tpos, could have been invalidated in the loop above */
7042 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007043 tpos.col++;
7044 if (cc != NUL && gchar_pos(&tpos) == NUL)
7045 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 /* <C-S-Right> may have started Visual mode, adjust the position for
7049 * deleted characters. */
7050 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7051 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007052 int len = (int)STRLEN(ml_get_curline());
7053
7054 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007056 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007057#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 }
7061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 }
7063 }
7064 did_ai = FALSE;
7065#ifdef FEAT_SMARTINDENT
7066 did_si = FALSE;
7067 can_si = FALSE;
7068 can_si_back = FALSE;
7069#endif
7070
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007071 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7072 * now in a different buffer. */
7073 if (end_insert_pos != NULL)
7074 {
7075 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007076 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007077 curbuf->b_op_end = *end_insert_pos;
7078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079}
7080
7081/*
7082 * Set the last inserted text to a single character.
7083 * Used for the replace command.
7084 */
7085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007086set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
7088 char_u *s;
7089
7090 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 if (last_insert != NULL)
7093 {
7094 s = last_insert;
7095 /* Use the CTRL-V only when entering a special char */
7096 if (c < ' ' || c == DEL)
7097 *s++ = Ctrl_V;
7098 s = add_char2buf(c, s);
7099 *s++ = ESC;
7100 *s++ = NUL;
7101 last_insert_skip = 0;
7102 }
7103}
7104
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007105#if defined(EXITFREE) || defined(PROTO)
7106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007107free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007108{
7109 vim_free(last_insert);
7110 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007111# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007112 vim_free(compl_orig_text);
7113 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007114# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007115}
7116#endif
7117
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118/*
7119 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7120 * and CSI. Handle multi-byte characters.
7121 * Returns a pointer to after the added bytes.
7122 */
7123 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007124add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
7126#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007127 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 int i;
7129 int len;
7130
7131 len = (*mb_char2bytes)(c, temp);
7132 for (i = 0; i < len; ++i)
7133 {
7134 c = temp[i];
7135#endif
7136 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7137 if (c == K_SPECIAL)
7138 {
7139 *s++ = K_SPECIAL;
7140 *s++ = KS_SPECIAL;
7141 *s++ = KE_FILLER;
7142 }
7143#ifdef FEAT_GUI
7144 else if (c == CSI)
7145 {
7146 *s++ = CSI;
7147 *s++ = KS_EXTRA;
7148 *s++ = (int)KE_CSI;
7149 }
7150#endif
7151 else
7152 *s++ = c;
7153#ifdef FEAT_MBYTE
7154 }
7155#endif
7156 return s;
7157}
7158
7159/*
7160 * move cursor to start of line
7161 * if flags & BL_WHITE move to first non-white
7162 * if flags & BL_SOL move to first non-white if startofline is set,
7163 * otherwise keep "curswant" column
7164 * if flags & BL_FIX don't leave the cursor on a NUL.
7165 */
7166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007167beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
7169 if ((flags & BL_SOL) && !p_sol)
7170 coladvance(curwin->w_curswant);
7171 else
7172 {
7173 curwin->w_cursor.col = 0;
7174#ifdef FEAT_VIRTUALEDIT
7175 curwin->w_cursor.coladd = 0;
7176#endif
7177
7178 if (flags & (BL_WHITE | BL_SOL))
7179 {
7180 char_u *ptr;
7181
7182 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7183 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7184 ++curwin->w_cursor.col;
7185 }
7186 curwin->w_set_curswant = TRUE;
7187 }
7188}
7189
7190/*
7191 * oneright oneleft cursor_down cursor_up
7192 *
7193 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007194 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 * Return OK when successful, FAIL when we hit a line of file boundary.
7196 */
7197
7198 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007199oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203
7204#ifdef FEAT_VIRTUALEDIT
7205 if (virtual_active())
7206 {
7207 pos_T prevpos = curwin->w_cursor;
7208
7209 /* Adjust for multi-wide char (excluding TAB) */
7210 ptr = ml_get_cursor();
7211 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007212# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007214# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 ))
7218 ? ptr2cells(ptr) : 1));
7219 curwin->w_set_curswant = TRUE;
7220 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7221 return (prevpos.col != curwin->w_cursor.col
7222 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7223 }
7224#endif
7225
7226 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007227 if (*ptr == NUL)
7228 return FAIL; /* already at the very end */
7229
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007231 if (has_mbyte)
7232 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 else
7234#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007235 l = 1;
7236
7237 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7238 * contains "onemore". */
7239 if (ptr[l] == NUL
7240#ifdef FEAT_VIRTUALEDIT
7241 && (ve_flags & VE_ONEMORE) == 0
7242#endif
7243 )
7244 return FAIL;
7245 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
7247 curwin->w_set_curswant = TRUE;
7248 return OK;
7249}
7250
7251 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007252oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253{
7254#ifdef FEAT_VIRTUALEDIT
7255 if (virtual_active())
7256 {
7257 int width;
7258 int v = getviscol();
7259
7260 if (v == 0)
7261 return FAIL;
7262
7263# ifdef FEAT_LINEBREAK
7264 /* We might get stuck on 'showbreak', skip over it. */
7265 width = 1;
7266 for (;;)
7267 {
7268 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007269 /* getviscol() is slow, skip it when 'showbreak' is empty,
7270 * 'breakindent' is not set and there are no multi-byte
7271 * characters */
7272 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273# ifdef FEAT_MBYTE
7274 && !has_mbyte
7275# endif
7276 ) || getviscol() < v)
7277 break;
7278 ++width;
7279 }
7280# else
7281 coladvance(v - 1);
7282# endif
7283
7284 if (curwin->w_cursor.coladd == 1)
7285 {
7286 char_u *ptr;
7287
7288 /* Adjust for multi-wide char (not a TAB) */
7289 ptr = ml_get_cursor();
7290 if (*ptr != TAB && vim_isprintc(
7291# ifdef FEAT_MBYTE
7292 (*mb_ptr2char)(ptr)
7293# else
7294 *ptr
7295# endif
7296 ) && ptr2cells(ptr) > 1)
7297 curwin->w_cursor.coladd = 0;
7298 }
7299
7300 curwin->w_set_curswant = TRUE;
7301 return OK;
7302 }
7303#endif
7304
7305 if (curwin->w_cursor.col == 0)
7306 return FAIL;
7307
7308 curwin->w_set_curswant = TRUE;
7309 --curwin->w_cursor.col;
7310
7311#ifdef FEAT_MBYTE
7312 /* if the character on the left of the current cursor is a multi-byte
7313 * character, move to its first byte */
7314 if (has_mbyte)
7315 mb_adjust_cursor();
7316#endif
7317 return OK;
7318}
7319
7320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007321cursor_up(
7322 long n,
7323 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
7325 linenr_T lnum;
7326
7327 if (n > 0)
7328 {
7329 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007330 /* This fails if the cursor is already in the first line or the count
7331 * is larger than the line number and '-' is in 'cpoptions' */
7332 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 return FAIL;
7334 if (n >= lnum)
7335 lnum = 1;
7336 else
7337#ifdef FEAT_FOLDING
7338 if (hasAnyFolding(curwin))
7339 {
7340 /*
7341 * Count each sequence of folded lines as one logical line.
7342 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007343 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 (void)hasFolding(lnum, &lnum, NULL);
7345
7346 while (n--)
7347 {
7348 /* move up one line */
7349 --lnum;
7350 if (lnum <= 1)
7351 break;
7352 /* If we entered a fold, move to the beginning, unless in
7353 * Insert mode or when 'foldopen' contains "all": it will open
7354 * in a moment. */
7355 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7356 (void)hasFolding(lnum, &lnum, NULL);
7357 }
7358 if (lnum < 1)
7359 lnum = 1;
7360 }
7361 else
7362#endif
7363 lnum -= n;
7364 curwin->w_cursor.lnum = lnum;
7365 }
7366
7367 /* try to advance to the column we want to be at */
7368 coladvance(curwin->w_curswant);
7369
7370 if (upd_topline)
7371 update_topline(); /* make sure curwin->w_topline is valid */
7372
7373 return OK;
7374}
7375
7376/*
7377 * Cursor down a number of logical lines.
7378 */
7379 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007380cursor_down(
7381 long n,
7382 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
7384 linenr_T lnum;
7385
7386 if (n > 0)
7387 {
7388 lnum = curwin->w_cursor.lnum;
7389#ifdef FEAT_FOLDING
7390 /* Move to last line of fold, will fail if it's the end-of-file. */
7391 (void)hasFolding(lnum, NULL, &lnum);
7392#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007393 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007394 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007395 if (lnum >= curbuf->b_ml.ml_line_count
7396 || (lnum + n > curbuf->b_ml.ml_line_count
7397 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 return FAIL;
7399 if (lnum + n >= curbuf->b_ml.ml_line_count)
7400 lnum = curbuf->b_ml.ml_line_count;
7401 else
7402#ifdef FEAT_FOLDING
7403 if (hasAnyFolding(curwin))
7404 {
7405 linenr_T last;
7406
7407 /* count each sequence of folded lines as one logical line */
7408 while (n--)
7409 {
7410 if (hasFolding(lnum, NULL, &last))
7411 lnum = last + 1;
7412 else
7413 ++lnum;
7414 if (lnum >= curbuf->b_ml.ml_line_count)
7415 break;
7416 }
7417 if (lnum > curbuf->b_ml.ml_line_count)
7418 lnum = curbuf->b_ml.ml_line_count;
7419 }
7420 else
7421#endif
7422 lnum += n;
7423 curwin->w_cursor.lnum = lnum;
7424 }
7425
7426 /* try to advance to the column we want to be at */
7427 coladvance(curwin->w_curswant);
7428
7429 if (upd_topline)
7430 update_topline(); /* make sure curwin->w_topline is valid */
7431
7432 return OK;
7433}
7434
7435/*
7436 * Stuff the last inserted text in the read buffer.
7437 * Last_insert actually is a copy of the redo buffer, so we
7438 * first have to remove the command.
7439 */
7440 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007441stuff_inserted(
7442 int c, /* Command character to be inserted */
7443 long count, /* Repeat this many times */
7444 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
7446 char_u *esc_ptr;
7447 char_u *ptr;
7448 char_u *last_ptr;
7449 char_u last = NUL;
7450
7451 ptr = get_last_insert();
7452 if (ptr == NULL)
7453 {
7454 EMSG(_(e_noinstext));
7455 return FAIL;
7456 }
7457
7458 /* may want to stuff the command character, to start Insert mode */
7459 if (c != NUL)
7460 stuffcharReadbuff(c);
7461 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7462 *esc_ptr = NUL; /* remove the ESC */
7463
7464 /* when the last char is either "0" or "^" it will be quoted if no ESC
7465 * comes after it OR if it will inserted more than once and "ptr"
7466 * starts with ^D. -- Acevedo
7467 */
7468 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7469 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7470 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7471 {
7472 last = *last_ptr;
7473 *last_ptr = NUL;
7474 }
7475
7476 do
7477 {
7478 stuffReadbuff(ptr);
7479 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7480 if (last)
7481 stuffReadbuff((char_u *)(last == '0'
7482 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7483 : IF_EB("\026^", CTRL_V_STR "^")));
7484 }
7485 while (--count > 0);
7486
7487 if (last)
7488 *last_ptr = last;
7489
7490 if (esc_ptr != NULL)
7491 *esc_ptr = ESC; /* put the ESC back */
7492
7493 /* may want to stuff a trailing ESC, to get out of Insert mode */
7494 if (!no_esc)
7495 stuffcharReadbuff(ESC);
7496
7497 return OK;
7498}
7499
7500 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007501get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502{
7503 if (last_insert == NULL)
7504 return NULL;
7505 return last_insert + last_insert_skip;
7506}
7507
7508/*
7509 * Get last inserted string, and remove trailing <Esc>.
7510 * Returns pointer to allocated memory (must be freed) or NULL.
7511 */
7512 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007513get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514{
7515 char_u *s;
7516 int len;
7517
7518 if (last_insert == NULL)
7519 return NULL;
7520 s = vim_strsave(last_insert + last_insert_skip);
7521 if (s != NULL)
7522 {
7523 len = (int)STRLEN(s);
7524 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7525 s[len - 1] = NUL;
7526 }
7527 return s;
7528}
7529
7530/*
7531 * Check the word in front of the cursor for an abbreviation.
7532 * Called when the non-id character "c" has been entered.
7533 * When an abbreviation is recognized it is removed from the text and
7534 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7535 */
7536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007537echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538{
7539 /* Don't check for abbreviation in paste mode, when disabled and just
7540 * after moving around with cursor keys. */
7541 if (p_paste || no_abbr || arrow_used)
7542 return FALSE;
7543
7544 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7545 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7546}
7547
7548/*
7549 * replace-stack functions
7550 *
7551 * When replacing characters, the replaced characters are remembered for each
7552 * new character. This is used to re-insert the old text when backspacing.
7553 *
7554 * There is a NUL headed list of characters for each character that is
7555 * currently in the file after the insertion point. When BS is used, one NUL
7556 * headed list is put back for the deleted character.
7557 *
7558 * For a newline, there are two NUL headed lists. One contains the characters
7559 * that the NL replaced. The extra one stores the characters after the cursor
7560 * that were deleted (always white space).
7561 *
7562 * Replace_offset is normally 0, in which case replace_push will add a new
7563 * character at the end of the stack. If replace_offset is not 0, that many
7564 * characters will be left on the stack above the newly inserted character.
7565 */
7566
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007567static char_u *replace_stack = NULL;
7568static long replace_stack_nr = 0; /* next entry in replace stack */
7569static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570
7571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007572replace_push(
7573 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574{
7575 char_u *p;
7576
7577 if (replace_stack_nr < replace_offset) /* nothing to do */
7578 return;
7579 if (replace_stack_len <= replace_stack_nr)
7580 {
7581 replace_stack_len += 50;
7582 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7583 if (p == NULL) /* out of memory */
7584 {
7585 replace_stack_len -= 50;
7586 return;
7587 }
7588 if (replace_stack != NULL)
7589 {
7590 mch_memmove(p, replace_stack,
7591 (size_t)(replace_stack_nr * sizeof(char_u)));
7592 vim_free(replace_stack);
7593 }
7594 replace_stack = p;
7595 }
7596 p = replace_stack + replace_stack_nr - replace_offset;
7597 if (replace_offset)
7598 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7599 *p = c;
7600 ++replace_stack_nr;
7601}
7602
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007603#if defined(FEAT_MBYTE) || defined(PROTO)
7604/*
7605 * Push a character onto the replace stack. Handles a multi-byte character in
7606 * reverse byte order, so that the first byte is popped off first.
7607 * Return the number of bytes done (includes composing characters).
7608 */
7609 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007610replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007611{
7612 int l = (*mb_ptr2len)(p);
7613 int j;
7614
7615 for (j = l - 1; j >= 0; --j)
7616 replace_push(p[j]);
7617 return l;
7618}
7619#endif
7620
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621/*
7622 * Pop one item from the replace stack.
7623 * return -1 if stack empty
7624 * return replaced character or NUL otherwise
7625 */
7626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007627replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
7629 if (replace_stack_nr == 0)
7630 return -1;
7631 return (int)replace_stack[--replace_stack_nr];
7632}
7633
7634/*
7635 * Join the top two items on the replace stack. This removes to "off"'th NUL
7636 * encountered.
7637 */
7638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007639replace_join(
7640 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
7642 int i;
7643
7644 for (i = replace_stack_nr; --i >= 0; )
7645 if (replace_stack[i] == NUL && off-- <= 0)
7646 {
7647 --replace_stack_nr;
7648 mch_memmove(replace_stack + i, replace_stack + i + 1,
7649 (size_t)(replace_stack_nr - i));
7650 return;
7651 }
7652}
7653
7654/*
7655 * Pop bytes from the replace stack until a NUL is found, and insert them
7656 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7657 */
7658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007659replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660{
7661 int cc;
7662 int oldState = State;
7663
7664 State = NORMAL; /* don't want REPLACE here */
7665 while ((cc = replace_pop()) > 0)
7666 {
7667#ifdef FEAT_MBYTE
7668 mb_replace_pop_ins(cc);
7669#else
7670 ins_char(cc);
7671#endif
7672 dec_cursor();
7673 }
7674 State = oldState;
7675}
7676
7677#ifdef FEAT_MBYTE
7678/*
7679 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7680 * indicates a multi-byte char, pop the other bytes too.
7681 */
7682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007683mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
7685 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007686 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 int i;
7688 int c;
7689
7690 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7691 {
7692 buf[0] = cc;
7693 for (i = 1; i < n; ++i)
7694 buf[i] = replace_pop();
7695 ins_bytes_len(buf, n);
7696 }
7697 else
7698 ins_char(cc);
7699
7700 if (enc_utf8)
7701 /* Handle composing chars. */
7702 for (;;)
7703 {
7704 c = replace_pop();
7705 if (c == -1) /* stack empty */
7706 break;
7707 if ((n = MB_BYTE2LEN(c)) == 1)
7708 {
7709 /* Not a multi-byte char, put it back. */
7710 replace_push(c);
7711 break;
7712 }
7713 else
7714 {
7715 buf[0] = c;
7716 for (i = 1; i < n; ++i)
7717 buf[i] = replace_pop();
7718 if (utf_iscomposing(utf_ptr2char(buf)))
7719 ins_bytes_len(buf, n);
7720 else
7721 {
7722 /* Not a composing char, put it back. */
7723 for (i = n - 1; i >= 0; --i)
7724 replace_push(buf[i]);
7725 break;
7726 }
7727 }
7728 }
7729}
7730#endif
7731
7732/*
7733 * make the replace stack empty
7734 * (called when exiting replace mode)
7735 */
7736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007737replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
7739 vim_free(replace_stack);
7740 replace_stack = NULL;
7741 replace_stack_len = 0;
7742 replace_stack_nr = 0;
7743}
7744
7745/*
7746 * Handle doing a BS for one character.
7747 * cc < 0: replace stack empty, just move cursor
7748 * cc == 0: character was inserted, delete it
7749 * cc > 0: character was replaced, put cc (first byte of original char) back
7750 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007751 * When "limit_col" is >= 0, don't delete before this column. Matters when
7752 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 */
7754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007755replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
7757 int cc;
7758#ifdef FEAT_VREPLACE
7759 int orig_len = 0;
7760 int ins_len;
7761 int orig_vcols = 0;
7762 colnr_T start_vcol;
7763 char_u *p;
7764 int i;
7765 int vcol;
7766#endif
7767
7768 cc = replace_pop();
7769 if (cc > 0)
7770 {
7771#ifdef FEAT_VREPLACE
7772 if (State & VREPLACE_FLAG)
7773 {
7774 /* Get the number of screen cells used by the character we are
7775 * going to delete. */
7776 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7777 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7778 }
7779#endif
7780#ifdef FEAT_MBYTE
7781 if (has_mbyte)
7782 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007783 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784# ifdef FEAT_VREPLACE
7785 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007786 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787# endif
7788 replace_push(cc);
7789 }
7790 else
7791#endif
7792 {
7793 pchar_cursor(cc);
7794#ifdef FEAT_VREPLACE
7795 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007796 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797#endif
7798 }
7799 replace_pop_ins();
7800
7801#ifdef FEAT_VREPLACE
7802 if (State & VREPLACE_FLAG)
7803 {
7804 /* Get the number of screen cells used by the inserted characters */
7805 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007806 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 vcol = start_vcol;
7808 for (i = 0; i < ins_len; ++i)
7809 {
7810 vcol += chartabsize(p + i, vcol);
7811#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007812 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813#endif
7814 }
7815 vcol -= start_vcol;
7816
7817 /* Delete spaces that were inserted after the cursor to keep the
7818 * text aligned. */
7819 curwin->w_cursor.col += ins_len;
7820 while (vcol > orig_vcols && gchar_cursor() == ' ')
7821 {
7822 del_char(FALSE);
7823 ++orig_vcols;
7824 }
7825 curwin->w_cursor.col -= ins_len;
7826 }
7827#endif
7828
7829 /* mark the buffer as changed and prepare for displaying */
7830 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7831 }
7832 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007833 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834}
7835
7836#ifdef FEAT_CINDENT
7837/*
7838 * Return TRUE if C-indenting is on.
7839 */
7840 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007841cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843 return (!p_paste && (curbuf->b_p_cin
7844# ifdef FEAT_EVAL
7845 || *curbuf->b_p_inde != NUL
7846# endif
7847 ));
7848}
7849#endif
7850
7851#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7852/*
7853 * Re-indent the current line, based on the current contents of it and the
7854 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7855 * confused what all the part that handles Control-T is doing that I'm not.
7856 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7857 */
7858
7859 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007860fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007862 int amount = get_the_indent();
7863
7864 if (amount >= 0)
7865 {
7866 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7867 if (linewhite(curwin->w_cursor.lnum))
7868 did_ai = TRUE; /* delete the indent if the line stays empty */
7869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870}
7871
7872 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007873fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 if (p_paste)
7876 return;
7877# ifdef FEAT_LISP
7878 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7879 fixthisline(get_lisp_indent);
7880# endif
7881# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7882 else
7883# endif
7884# ifdef FEAT_CINDENT
7885 if (cindent_on())
7886 do_c_expr_indent();
7887# endif
7888}
7889
7890#endif
7891
7892#ifdef FEAT_CINDENT
7893/*
7894 * return TRUE if 'cinkeys' contains the key "keytyped",
7895 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007896 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7898 *
7899 * "keytyped" can have a few special values:
7900 * KEY_OPEN_FORW
7901 * KEY_OPEN_BACK
7902 * KEY_COMPLETE just finished completion.
7903 *
7904 * If line_is_empty is TRUE accept keys with '0' before them.
7905 */
7906 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007907in_cinkeys(
7908 int keytyped,
7909 int when,
7910 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 char_u *look;
7913 int try_match;
7914 int try_match_word;
7915 char_u *p;
7916 char_u *line;
7917 int icase;
7918 int i;
7919
Bram Moolenaar30848942009-12-24 14:46:12 +00007920 if (keytyped == NUL)
7921 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7922 return FALSE;
7923
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924#ifdef FEAT_EVAL
7925 if (*curbuf->b_p_inde != NUL)
7926 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7927 else
7928#endif
7929 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7930 while (*look)
7931 {
7932 /*
7933 * Find out if we want to try a match with this key, depending on
7934 * 'when' and a '*' or '!' before the key.
7935 */
7936 switch (when)
7937 {
7938 case '*': try_match = (*look == '*'); break;
7939 case '!': try_match = (*look == '!'); break;
7940 default: try_match = (*look != '*'); break;
7941 }
7942 if (*look == '*' || *look == '!')
7943 ++look;
7944
7945 /*
7946 * If there is a '0', only accept a match if the line is empty.
7947 * But may still match when typing last char of a word.
7948 */
7949 if (*look == '0')
7950 {
7951 try_match_word = try_match;
7952 if (!line_is_empty)
7953 try_match = FALSE;
7954 ++look;
7955 }
7956 else
7957 try_match_word = FALSE;
7958
7959 /*
7960 * does it look like a control character?
7961 */
7962 if (*look == '^'
7963#ifdef EBCDIC
7964 && (Ctrl_chr(look[1]) != 0)
7965#else
7966 && look[1] >= '?' && look[1] <= '_'
7967#endif
7968 )
7969 {
7970 if (try_match && keytyped == Ctrl_chr(look[1]))
7971 return TRUE;
7972 look += 2;
7973 }
7974 /*
7975 * 'o' means "o" command, open forward.
7976 * 'O' means "O" command, open backward.
7977 */
7978 else if (*look == 'o')
7979 {
7980 if (try_match && keytyped == KEY_OPEN_FORW)
7981 return TRUE;
7982 ++look;
7983 }
7984 else if (*look == 'O')
7985 {
7986 if (try_match && keytyped == KEY_OPEN_BACK)
7987 return TRUE;
7988 ++look;
7989 }
7990
7991 /*
7992 * 'e' means to check for "else" at start of line and just before the
7993 * cursor.
7994 */
7995 else if (*look == 'e')
7996 {
7997 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7998 {
7999 p = ml_get_curline();
8000 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8001 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8002 return TRUE;
8003 }
8004 ++look;
8005 }
8006
8007 /*
8008 * ':' only causes an indent if it is at the end of a label or case
8009 * statement, or when it was before typing the ':' (to fix
8010 * class::method for C++).
8011 */
8012 else if (*look == ':')
8013 {
8014 if (try_match && keytyped == ':')
8015 {
8016 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008017 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008019 /* Need to get the line again after cin_islabel(). */
8020 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 if (curwin->w_cursor.col > 2
8022 && p[curwin->w_cursor.col - 1] == ':'
8023 && p[curwin->w_cursor.col - 2] == ':')
8024 {
8025 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008026 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008027 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 p = ml_get_curline();
8029 p[curwin->w_cursor.col - 1] = ':';
8030 if (i)
8031 return TRUE;
8032 }
8033 }
8034 ++look;
8035 }
8036
8037
8038 /*
8039 * Is it a key in <>, maybe?
8040 */
8041 else if (*look == '<')
8042 {
8043 if (try_match)
8044 {
8045 /*
8046 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8047 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8048 * >, *, : and ! keys if they really really want to.
8049 */
8050 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8051 && keytyped == look[1])
8052 return TRUE;
8053
8054 if (keytyped == get_special_key_code(look + 1))
8055 return TRUE;
8056 }
8057 while (*look && *look != '>')
8058 look++;
8059 while (*look == '>')
8060 look++;
8061 }
8062
8063 /*
8064 * Is it a word: "=word"?
8065 */
8066 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8067 {
8068 ++look;
8069 if (*look == '~')
8070 {
8071 icase = TRUE;
8072 ++look;
8073 }
8074 else
8075 icase = FALSE;
8076 p = vim_strchr(look, ',');
8077 if (p == NULL)
8078 p = look + STRLEN(look);
8079 if ((try_match || try_match_word)
8080 && curwin->w_cursor.col >= (colnr_T)(p - look))
8081 {
8082 int match = FALSE;
8083
8084#ifdef FEAT_INS_EXPAND
8085 if (keytyped == KEY_COMPLETE)
8086 {
8087 char_u *s;
8088
8089 /* Just completed a word, check if it starts with "look".
8090 * search back for the start of a word. */
8091 line = ml_get_curline();
8092# ifdef FEAT_MBYTE
8093 if (has_mbyte)
8094 {
8095 char_u *n;
8096
8097 for (s = line + curwin->w_cursor.col; s > line; s = n)
8098 {
8099 n = mb_prevptr(line, s);
8100 if (!vim_iswordp(n))
8101 break;
8102 }
8103 }
8104 else
8105# endif
8106 for (s = line + curwin->w_cursor.col; s > line; --s)
8107 if (!vim_iswordc(s[-1]))
8108 break;
8109 if (s + (p - look) <= line + curwin->w_cursor.col
8110 && (icase
8111 ? MB_STRNICMP(s, look, p - look)
8112 : STRNCMP(s, look, p - look)) == 0)
8113 match = TRUE;
8114 }
8115 else
8116#endif
8117 /* TODO: multi-byte */
8118 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8119 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8120 {
8121 line = ml_get_cursor();
8122 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8123 || !vim_iswordc(line[-(p - look) - 1]))
8124 && (icase
8125 ? MB_STRNICMP(line - (p - look), look, p - look)
8126 : STRNCMP(line - (p - look), look, p - look))
8127 == 0)
8128 match = TRUE;
8129 }
8130 if (match && try_match_word && !try_match)
8131 {
8132 /* "0=word": Check if there are only blanks before the
8133 * word. */
8134 line = ml_get_curline();
8135 if ((int)(skipwhite(line) - line) !=
8136 (int)(curwin->w_cursor.col - (p - look)))
8137 match = FALSE;
8138 }
8139 if (match)
8140 return TRUE;
8141 }
8142 look = p;
8143 }
8144
8145 /*
8146 * ok, it's a boring generic character.
8147 */
8148 else
8149 {
8150 if (try_match && *look == keytyped)
8151 return TRUE;
8152 ++look;
8153 }
8154
8155 /*
8156 * Skip over ", ".
8157 */
8158 look = skip_to_option_part(look);
8159 }
8160 return FALSE;
8161}
8162#endif /* FEAT_CINDENT */
8163
8164#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8165/*
8166 * Map Hebrew keyboard when in hkmap mode.
8167 */
8168 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008169hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170{
8171 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8172 {
8173 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8174 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8175 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8176 static char_u map[26] =
8177 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8178 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8179 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8180 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8181 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8182 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8183 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8184 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8185 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8186
8187 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8188 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8189 /* '-1'='sofit' */
8190 else if (c == 'x')
8191 return 'X';
8192 else if (c == 'q')
8193 return '\''; /* {geresh}={'} */
8194 else if (c == 246)
8195 return ' '; /* \"o --> ' ' for a german keyboard */
8196 else if (c == 228)
8197 return ' '; /* \"a --> ' ' -- / -- */
8198 else if (c == 252)
8199 return ' '; /* \"u --> ' ' -- / -- */
8200#ifdef EBCDIC
8201 else if (islower(c))
8202#else
8203 /* NOTE: islower() does not do the right thing for us on Linux so we
8204 * do this the same was as 5.7 and previous, so it works correctly on
8205 * all systems. Specifically, the e.g. Delete and Arrow keys are
8206 * munged and won't work if e.g. searching for Hebrew text.
8207 */
8208 else if (c >= 'a' && c <= 'z')
8209#endif
8210 return (int)(map[CharOrdLow(c)] + p_aleph);
8211 else
8212 return c;
8213 }
8214 else
8215 {
8216 switch (c)
8217 {
8218 case '`': return ';';
8219 case '/': return '.';
8220 case '\'': return ',';
8221 case 'q': return '/';
8222 case 'w': return '\'';
8223
8224 /* Hebrew letters - set offset from 'a' */
8225 case ',': c = '{'; break;
8226 case '.': c = 'v'; break;
8227 case ';': c = 't'; break;
8228 default: {
8229 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8230
8231#ifdef EBCDIC
8232 /* see note about islower() above */
8233 if (!islower(c))
8234#else
8235 if (c < 'a' || c > 'z')
8236#endif
8237 return c;
8238 c = str[CharOrdLow(c)];
8239 break;
8240 }
8241 }
8242
8243 return (int)(CharOrdLow(c) + p_aleph);
8244 }
8245}
8246#endif
8247
8248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008249ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250{
8251 int need_redraw = FALSE;
8252 int regname;
8253 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008254 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255
8256 /*
8257 * If we are going to wait for a character, show a '"'.
8258 */
8259 pc_status = PC_STATUS_UNSET;
8260 if (redrawing() && !char_avail())
8261 {
8262 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008263 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264
8265 edit_putchar('"', TRUE);
8266#ifdef FEAT_CMDL_INFO
8267 add_to_showcmd_c(Ctrl_R);
8268#endif
8269 }
8270
8271#ifdef USE_ON_FLY_SCROLL
8272 dont_scroll = TRUE; /* disallow scrolling here */
8273#endif
8274
8275 /*
8276 * Don't map the register name. This also prevents the mode message to be
8277 * deleted when ESC is hit.
8278 */
8279 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008280 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8283 {
8284 /* Get a third key for literal register insertion */
8285 literally = regname;
8286#ifdef FEAT_CMDL_INFO
8287 add_to_showcmd_c(literally);
8288#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008289 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 }
8292 --no_mapping;
8293
8294#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008295 /* Don't call u_sync() while typing the expression or giving an error
8296 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 ++no_u_sync;
8298 if (regname == '=')
8299 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008300# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008302# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008303 /* Sync undo when evaluating the expression calls setline() or
8304 * append(), so that it can be undone separately. */
8305 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008306
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008308# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 /* Restore the Input Method. */
8310 if (im_on)
8311 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008314 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8315 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008316 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 else
8320 {
8321#endif
8322 if (literally == Ctrl_O || literally == Ctrl_P)
8323 {
8324 /* Append the command to the redo buffer. */
8325 AppendCharToRedobuff(Ctrl_R);
8326 AppendCharToRedobuff(literally);
8327 AppendCharToRedobuff(regname);
8328
8329 do_put(regname, BACKWARD, 1L,
8330 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8331 }
8332 else if (insert_reg(regname, literally) == FAIL)
8333 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008334 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 need_redraw = TRUE; /* remove the '"' */
8336 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008337 else if (stop_insert_mode)
8338 /* When the '=' register was used and a function was invoked that
8339 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8340 * insert anything, need to remove the '"' */
8341 need_redraw = TRUE;
8342
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343#ifdef FEAT_EVAL
8344 }
8345 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008346 if (u_sync_once == 1)
8347 ins_need_undo = TRUE;
8348 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349#endif
8350#ifdef FEAT_CMDL_INFO
8351 clear_showcmd();
8352#endif
8353
8354 /* If the inserted register is empty, we need to remove the '"' */
8355 if (need_redraw || stuff_empty())
8356 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008357
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008358 /* Disallow starting Visual mode here, would get a weird mode. */
8359 if (!vis_active && VIsual_active)
8360 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361}
8362
8363/*
8364 * CTRL-G commands in Insert mode.
8365 */
8366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008367ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368{
8369 int c;
8370
8371#ifdef FEAT_INS_EXPAND
8372 /* Right after CTRL-X the cursor will be after the ruler. */
8373 setcursor();
8374#endif
8375
8376 /*
8377 * Don't map the second key. This also prevents the mode message to be
8378 * deleted when ESC is hit.
8379 */
8380 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008381 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 --no_mapping;
8383 switch (c)
8384 {
8385 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8386 case K_UP:
8387 case Ctrl_K:
8388 case 'k': ins_up(TRUE);
8389 break;
8390
8391 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8392 case K_DOWN:
8393 case Ctrl_J:
8394 case 'j': ins_down(TRUE);
8395 break;
8396
8397 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008398 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008400
8401 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008402 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008403 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008404 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 break;
8406
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008407 /* CTRL-G U: do not break undo with the next char */
8408 case 'U':
8409 /* Allow one left/right cursor movement with the next char,
8410 * without breaking undo. */
8411 dont_sync_undo = MAYBE;
8412 break;
8413
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008415 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 }
8417}
8418
8419/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008420 * CTRL-^ in Insert mode.
8421 */
8422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008423ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008424{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008425 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008426 {
8427 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8428 if (State & LANGMAP)
8429 {
8430 curbuf->b_p_iminsert = B_IMODE_NONE;
8431 State &= ~LANGMAP;
8432 }
8433 else
8434 {
8435 curbuf->b_p_iminsert = B_IMODE_LMAP;
8436 State |= LANGMAP;
8437#ifdef USE_IM_CONTROL
8438 im_set_active(FALSE);
8439#endif
8440 }
8441 }
8442#ifdef USE_IM_CONTROL
8443 else
8444 {
8445 /* There are no ":lmap" mappings, toggle IM */
8446 if (im_get_status())
8447 {
8448 curbuf->b_p_iminsert = B_IMODE_NONE;
8449 im_set_active(FALSE);
8450 }
8451 else
8452 {
8453 curbuf->b_p_iminsert = B_IMODE_IM;
8454 State &= ~LANGMAP;
8455 im_set_active(TRUE);
8456 }
8457 }
8458#endif
8459 set_iminsert_global();
8460 showmode();
8461#ifdef FEAT_GUI
8462 /* may show different cursor shape or color */
8463 if (gui.in_use)
8464 gui_update_cursor(TRUE, FALSE);
8465#endif
8466#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8467 /* Show/unshow value of 'keymap' in status lines. */
8468 status_redraw_curbuf();
8469#endif
8470}
8471
8472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 * Handle ESC in insert mode.
8474 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8475 * insert.
8476 */
8477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008478ins_esc(
8479 long *count,
8480 int cmdchar,
8481 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
8483 int temp;
8484 static int disabled_redraw = FALSE;
8485
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008486#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008487 check_spell_redraw();
8488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489#if defined(FEAT_HANGULIN)
8490# if defined(ESC_CHG_TO_ENG_MODE)
8491 hangul_input_state_set(0);
8492# endif
8493 if (composing_hangul)
8494 {
8495 push_raw_key(composing_hangul_buffer, 2);
8496 composing_hangul = 0;
8497 }
8498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499
8500 temp = curwin->w_cursor.col;
8501 if (disabled_redraw)
8502 {
8503 --RedrawingDisabled;
8504 disabled_redraw = FALSE;
8505 }
8506 if (!arrow_used)
8507 {
8508 /*
8509 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008510 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8511 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 */
8513 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008514 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515
8516 /*
8517 * Repeating insert may take a long time. Check for
8518 * interrupt now and then.
8519 */
8520 if (*count > 0)
8521 {
8522 line_breakcheck();
8523 if (got_int)
8524 *count = 0;
8525 }
8526
8527 if (--*count > 0) /* repeat what was typed */
8528 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008529 /* Vi repeats the insert without replacing characters. */
8530 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8531 State &= ~REPLACE_FLAG;
8532
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 (void)start_redo_ins();
8534 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008535 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 ++RedrawingDisabled;
8537 disabled_redraw = TRUE;
8538 return FALSE; /* repeat the insert */
8539 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008540 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 undisplay_dollar();
8542 }
8543
8544 /* When an autoindent was removed, curswant stays after the
8545 * indent */
8546 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8547 curwin->w_set_curswant = TRUE;
8548
8549 /* Remember the last Insert position in the '^ mark. */
8550 if (!cmdmod.keepjumps)
8551 curbuf->b_last_insert = curwin->w_cursor;
8552
8553 /*
8554 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008555 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008557 if (!nomove
8558 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559#ifdef FEAT_VIRTUALEDIT
8560 || curwin->w_cursor.coladd > 0
8561#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008562 )
8563 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008564 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565#ifdef FEAT_RIGHTLEFT
8566 && !revins_on
8567#endif
8568 )
8569 {
8570#ifdef FEAT_VIRTUALEDIT
8571 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8572 {
8573 oneleft();
8574 if (restart_edit != NUL)
8575 ++curwin->w_cursor.coladd;
8576 }
8577 else
8578#endif
8579 {
8580 --curwin->w_cursor.col;
8581#ifdef FEAT_MBYTE
8582 /* Correct cursor for multi-byte character. */
8583 if (has_mbyte)
8584 mb_adjust_cursor();
8585#endif
8586 }
8587 }
8588
8589#ifdef USE_IM_CONTROL
8590 /* Disable IM to allow typing English directly for Normal mode commands.
8591 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8592 * well). */
8593 if (!(State & LANGMAP))
8594 im_save_status(&curbuf->b_p_iminsert);
8595 im_set_active(FALSE);
8596#endif
8597
8598 State = NORMAL;
8599 /* need to position cursor again (e.g. when on a TAB ) */
8600 changed_cline_bef_curs();
8601
8602#ifdef FEAT_MOUSE
8603 setmouse();
8604#endif
8605#ifdef CURSOR_SHAPE
8606 ui_cursor_shape(); /* may show different cursor shape */
8607#endif
8608
8609 /*
8610 * When recording or for CTRL-O, need to display the new mode.
8611 * Otherwise remove the mode message.
8612 */
8613 if (Recording || restart_edit != NUL)
8614 showmode();
8615 else if (p_smd)
8616 MSG("");
8617
8618 return TRUE; /* exit Insert mode */
8619}
8620
8621#ifdef FEAT_RIGHTLEFT
8622/*
8623 * Toggle language: hkmap and revins_on.
8624 * Move to end of reverse inserted text.
8625 */
8626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008627ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628{
8629 if (revins_on && revins_chars && revins_scol >= 0)
8630 {
8631 while (gchar_cursor() != NUL && revins_chars--)
8632 ++curwin->w_cursor.col;
8633 }
8634 p_ri = !p_ri;
8635 revins_on = (State == INSERT && p_ri);
8636 if (revins_on)
8637 {
8638 revins_scol = curwin->w_cursor.col;
8639 revins_legal++;
8640 revins_chars = 0;
8641 undisplay_dollar();
8642 }
8643 else
8644 revins_scol = -1;
8645#ifdef FEAT_FKMAP
8646 if (p_altkeymap)
8647 {
8648 /*
8649 * to be consistent also for redo command, using '.'
8650 * set arrow_used to true and stop it - causing to redo
8651 * characters entered in one mode (normal/reverse insert).
8652 */
8653 arrow_used = TRUE;
8654 (void)stop_arrow();
8655 p_fkmap = curwin->w_p_rl ^ p_ri;
8656 if (p_fkmap && p_ri)
8657 State = INSERT;
8658 }
8659 else
8660#endif
8661 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8662 showmode();
8663}
8664#endif
8665
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666/*
8667 * If 'keymodel' contains "startsel", may start selection.
8668 * Returns TRUE when a CTRL-O and other keys stuffed.
8669 */
8670 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008671ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672{
8673 if (km_startsel)
8674 switch (c)
8675 {
8676 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 case K_PAGEUP:
8679 case K_KPAGEUP:
8680 case K_PAGEDOWN:
8681 case K_KPAGEDOWN:
8682# ifdef MACOS
8683 case K_LEFT:
8684 case K_RIGHT:
8685 case K_UP:
8686 case K_DOWN:
8687 case K_END:
8688 case K_HOME:
8689# endif
8690 if (!(mod_mask & MOD_MASK_SHIFT))
8691 break;
8692 /* FALLTHROUGH */
8693 case K_S_LEFT:
8694 case K_S_RIGHT:
8695 case K_S_UP:
8696 case K_S_DOWN:
8697 case K_S_END:
8698 case K_S_HOME:
8699 /* Start selection right away, the cursor can move with
8700 * CTRL-O when beyond the end of the line. */
8701 start_selection();
8702
8703 /* Execute the key in (insert) Select mode. */
8704 stuffcharReadbuff(Ctrl_O);
8705 if (mod_mask)
8706 {
8707 char_u buf[4];
8708
8709 buf[0] = K_SPECIAL;
8710 buf[1] = KS_MODIFIER;
8711 buf[2] = mod_mask;
8712 buf[3] = NUL;
8713 stuffReadbuff(buf);
8714 }
8715 stuffcharReadbuff(c);
8716 return TRUE;
8717 }
8718 return FALSE;
8719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720
8721/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008722 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008723 */
8724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008725ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008726{
8727#ifdef FEAT_FKMAP
8728 if (p_fkmap && p_ri)
8729 {
8730 beep_flush();
8731 EMSG(farsi_text_3); /* encoded in Farsi */
8732 return;
8733 }
8734#endif
8735
8736#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008737# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008738 set_vim_var_string(VV_INSERTMODE,
8739 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008740# ifdef FEAT_VREPLACE
8741 replaceState == VREPLACE ? "v" :
8742# endif
8743 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008744# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008745 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8746#endif
8747 if (State & REPLACE_FLAG)
8748 State = INSERT | (State & LANGMAP);
8749 else
8750 State = replaceState | (State & LANGMAP);
8751 AppendCharToRedobuff(K_INS);
8752 showmode();
8753#ifdef CURSOR_SHAPE
8754 ui_cursor_shape(); /* may show different cursor shape */
8755#endif
8756}
8757
8758/*
8759 * Pressed CTRL-O in Insert mode.
8760 */
8761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008762ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008763{
8764#ifdef FEAT_VREPLACE
8765 if (State & VREPLACE_FLAG)
8766 restart_edit = 'V';
8767 else
8768#endif
8769 if (State & REPLACE_FLAG)
8770 restart_edit = 'R';
8771 else
8772 restart_edit = 'I';
8773#ifdef FEAT_VIRTUALEDIT
8774 if (virtual_active())
8775 ins_at_eol = FALSE; /* cursor always keeps its column */
8776 else
8777#endif
8778 ins_at_eol = (gchar_cursor() == NUL);
8779}
8780
8781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 * If the cursor is on an indent, ^T/^D insert/delete one
8783 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008784 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 * with vi. But vi only supports ^T and ^D after an
8786 * autoindent, we support it everywhere.
8787 */
8788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008789ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790{
8791 if (stop_arrow() == FAIL)
8792 return;
8793 AppendCharToRedobuff(c);
8794
8795 /*
8796 * 0^D and ^^D: remove all indent.
8797 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008798 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8799 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 {
8801 --curwin->w_cursor.col;
8802 (void)del_char(FALSE); /* delete the '^' or '0' */
8803 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8804 if (State & REPLACE_FLAG)
8805 replace_pop_ins();
8806 if (lastc == '^')
8807 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008808 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 }
8810 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008811 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812
8813 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8814 did_ai = FALSE;
8815#ifdef FEAT_SMARTINDENT
8816 did_si = FALSE;
8817 can_si = FALSE;
8818 can_si_back = FALSE;
8819#endif
8820#ifdef FEAT_CINDENT
8821 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8822#endif
8823}
8824
8825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008826ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827{
8828 int temp;
8829
8830 if (stop_arrow() == FAIL)
8831 return;
8832 if (gchar_cursor() == NUL) /* delete newline */
8833 {
8834 temp = curwin->w_cursor.col;
8835 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008836 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008837 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 else
8839 curwin->w_cursor.col = temp;
8840 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008841 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8842 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 did_ai = FALSE;
8844#ifdef FEAT_SMARTINDENT
8845 did_si = FALSE;
8846 can_si = FALSE;
8847 can_si_back = FALSE;
8848#endif
8849 AppendCharToRedobuff(K_DEL);
8850}
8851
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008852static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008853
8854/*
8855 * Delete one character for ins_bs().
8856 */
8857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008858ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008859{
8860 dec_cursor();
8861 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8862 if (State & REPLACE_FLAG)
8863 {
8864 /* Don't delete characters before the insert point when in
8865 * Replace mode */
8866 if (curwin->w_cursor.lnum != Insstart.lnum
8867 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008868 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008869 }
8870 else
8871 (void)del_char(FALSE);
8872}
8873
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874/*
8875 * Handle Backspace, delete-word and delete-line in Insert mode.
8876 * Return TRUE when backspace was actually used.
8877 */
8878 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008879ins_bs(
8880 int c,
8881 int mode,
8882 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883{
8884 linenr_T lnum;
8885 int cc;
8886 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008887 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 colnr_T mincol;
8889 int did_backspace = FALSE;
8890 int in_indent;
8891 int oldState;
8892#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008893 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894#endif
8895
8896 /*
8897 * can't delete anything in an empty file
8898 * can't backup past first character in buffer
8899 * can't backup past starting point unless 'backspace' > 1
8900 * can backup to a previous line if 'backspace' == 0
8901 */
8902 if ( bufempty()
8903 || (
8904#ifdef FEAT_RIGHTLEFT
8905 !revins_on &&
8906#endif
8907 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8908 || (!can_bs(BS_START)
8909 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008910 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8911 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8913 && curwin->w_cursor.col <= ai_col)
8914 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8915 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008916 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 return FALSE;
8918 }
8919
8920 if (stop_arrow() == FAIL)
8921 return FALSE;
8922 in_indent = inindent(0);
8923#ifdef FEAT_CINDENT
8924 if (in_indent)
8925 can_cindent = FALSE;
8926#endif
8927#ifdef FEAT_COMMENTS
8928 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8929#endif
8930#ifdef FEAT_RIGHTLEFT
8931 if (revins_on) /* put cursor after last inserted char */
8932 inc_cursor();
8933#endif
8934
8935#ifdef FEAT_VIRTUALEDIT
8936 /* Virtualedit:
8937 * BACKSPACE_CHAR eats a virtual space
8938 * BACKSPACE_WORD eats all coladd
8939 * BACKSPACE_LINE eats all coladd and keeps going
8940 */
8941 if (curwin->w_cursor.coladd > 0)
8942 {
8943 if (mode == BACKSPACE_CHAR)
8944 {
8945 --curwin->w_cursor.coladd;
8946 return TRUE;
8947 }
8948 if (mode == BACKSPACE_WORD)
8949 {
8950 curwin->w_cursor.coladd = 0;
8951 return TRUE;
8952 }
8953 curwin->w_cursor.coladd = 0;
8954 }
8955#endif
8956
8957 /*
8958 * delete newline!
8959 */
8960 if (curwin->w_cursor.col == 0)
8961 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008962 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008963 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964#ifdef FEAT_RIGHTLEFT
8965 || revins_on
8966#endif
8967 )
8968 {
8969 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8970 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8971 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008972 --Insstart.lnum;
8973 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 }
8975 /*
8976 * In replace mode:
8977 * cc < 0: NL was inserted, delete it
8978 * cc >= 0: NL was replaced, put original characters back
8979 */
8980 cc = -1;
8981 if (State & REPLACE_FLAG)
8982 cc = replace_pop(); /* returns -1 if NL was inserted */
8983 /*
8984 * In replace mode, in the line we started replacing, we only move the
8985 * cursor.
8986 */
8987 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8988 {
8989 dec_cursor();
8990 }
8991 else
8992 {
8993#ifdef FEAT_VREPLACE
8994 if (!(State & VREPLACE_FLAG)
8995 || curwin->w_cursor.lnum > orig_line_count)
8996#endif
8997 {
8998 temp = gchar_cursor(); /* remember current char */
8999 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009000
9001 /* When "aw" is in 'formatoptions' we must delete the space at
9002 * the end of the line, otherwise the line will be broken
9003 * again when auto-formatting. */
9004 if (has_format_option(FO_AUTO)
9005 && has_format_option(FO_WHITE_PAR))
9006 {
9007 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9008 TRUE);
9009 int len;
9010
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009011 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009012 if (len > 0 && ptr[len - 1] == ' ')
9013 ptr[len - 1] = NUL;
9014 }
9015
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009016 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 if (temp == NUL && gchar_cursor() != NUL)
9018 inc_cursor();
9019 }
9020#ifdef FEAT_VREPLACE
9021 else
9022 dec_cursor();
9023#endif
9024
9025 /*
9026 * In REPLACE mode we have to put back the text that was replaced
9027 * by the NL. On the replace stack is first a NUL-terminated
9028 * sequence of characters that were deleted and then the
9029 * characters that NL replaced.
9030 */
9031 if (State & REPLACE_FLAG)
9032 {
9033 /*
9034 * Do the next ins_char() in NORMAL state, to
9035 * prevent ins_char() from replacing characters and
9036 * avoiding showmatch().
9037 */
9038 oldState = State;
9039 State = NORMAL;
9040 /*
9041 * restore characters (blanks) deleted after cursor
9042 */
9043 while (cc > 0)
9044 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009045 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046#ifdef FEAT_MBYTE
9047 mb_replace_pop_ins(cc);
9048#else
9049 ins_char(cc);
9050#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009051 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 cc = replace_pop();
9053 }
9054 /* restore the characters that NL replaced */
9055 replace_pop_ins();
9056 State = oldState;
9057 }
9058 }
9059 did_ai = FALSE;
9060 }
9061 else
9062 {
9063 /*
9064 * Delete character(s) before the cursor.
9065 */
9066#ifdef FEAT_RIGHTLEFT
9067 if (revins_on) /* put cursor on last inserted char */
9068 dec_cursor();
9069#endif
9070 mincol = 0;
9071 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009072 if (mode == BACKSPACE_LINE
9073 && (curbuf->b_p_ai
9074#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009075 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009076#endif
9077 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078#ifdef FEAT_RIGHTLEFT
9079 && !revins_on
9080#endif
9081 )
9082 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009083 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009085 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009087 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 }
9089
9090 /*
9091 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9092 */
9093 if ( mode == BACKSPACE_CHAR
9094 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009095 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009096 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 && (*(ml_get_cursor() - 1) == TAB
9098 || (*(ml_get_cursor() - 1) == ' '
9099 && (!*inserted_space_p
9100 || arrow_used))))))
9101 {
9102 int ts;
9103 colnr_T vcol;
9104 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009105 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106
9107 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009108 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009109 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009111 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 /* Compute the virtual column where we want to be. Since
9113 * 'showbreak' may get in the way, need to get the last column of
9114 * the previous character. */
9115 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009116 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 dec_cursor();
9118 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9119 inc_cursor();
9120 want_vcol = (want_vcol / ts) * ts;
9121
9122 /* delete characters until we are at or before want_vcol */
9123 while (vcol > want_vcol
9124 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009125 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126
9127 /* insert extra spaces until we are at want_vcol */
9128 while (vcol < want_vcol)
9129 {
9130 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009131 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9132 && curwin->w_cursor.col < Insstart_orig.col)
9133 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134
9135#ifdef FEAT_VREPLACE
9136 if (State & VREPLACE_FLAG)
9137 ins_char(' ');
9138 else
9139#endif
9140 {
9141 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009142 if ((State & REPLACE_FLAG))
9143 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 }
9145 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9146 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009147
9148 /* If we are now back where we started delete one character. Can
9149 * happen when using 'sts' and 'linebreak'. */
9150 if (vcol >= start_vcol)
9151 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 }
9153
9154 /*
9155 * Delete upto starting point, start of line or previous word.
9156 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009157 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009159#ifdef FEAT_MBYTE
9160 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009162 if (has_mbyte)
9163 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009165 do
9166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009168 if (!revins_on) /* put cursor on char to be deleted */
9169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009171
9172 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009174 /* look multi-byte character class */
9175 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009177 prev_cclass = cclass;
9178 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009181
9182 /* start of word? */
9183 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9184 {
9185 mode = BACKSPACE_WORD_NOT_SPACE;
9186 temp = vim_iswordc(cc);
9187 }
9188 /* end of word? */
9189 else if (mode == BACKSPACE_WORD_NOT_SPACE
9190 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9191#ifdef FEAT_MBYTE
9192 || prev_cclass != cclass
9193#endif
9194 ))
9195 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009197 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009199 inc_cursor();
9200#ifdef FEAT_RIGHTLEFT
9201 else if (State & REPLACE_FLAG)
9202 dec_cursor();
9203#endif
9204 break;
9205 }
9206 if (State & REPLACE_FLAG)
9207 replace_do_bs(-1);
9208 else
9209 {
9210#ifdef FEAT_MBYTE
9211 if (enc_utf8 && p_deco)
9212 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9213#endif
9214 (void)del_char(FALSE);
9215#ifdef FEAT_MBYTE
9216 /*
9217 * If there are combining characters and 'delcombine' is set
9218 * move the cursor back. Don't back up before the base
9219 * character.
9220 */
9221 if (enc_utf8 && p_deco && cpc[0] != NUL)
9222 inc_cursor();
9223#endif
9224#ifdef FEAT_RIGHTLEFT
9225 if (revins_chars)
9226 {
9227 revins_chars--;
9228 revins_legal++;
9229 }
9230 if (revins_on && gchar_cursor() == NUL)
9231 break;
9232#endif
9233 }
9234 /* Just a single backspace?: */
9235 if (mode == BACKSPACE_CHAR)
9236 break;
9237 } while (
9238#ifdef FEAT_RIGHTLEFT
9239 revins_on ||
9240#endif
9241 (curwin->w_cursor.col > mincol
9242 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9243 || curwin->w_cursor.col != Insstart_orig.col)));
9244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 did_backspace = TRUE;
9246 }
9247#ifdef FEAT_SMARTINDENT
9248 did_si = FALSE;
9249 can_si = FALSE;
9250 can_si_back = FALSE;
9251#endif
9252 if (curwin->w_cursor.col <= 1)
9253 did_ai = FALSE;
9254 /*
9255 * It's a little strange to put backspaces into the redo
9256 * buffer, but it makes auto-indent a lot easier to deal
9257 * with.
9258 */
9259 AppendCharToRedobuff(c);
9260
9261 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009262 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9263 && curwin->w_cursor.col < Insstart_orig.col)
9264 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265
9266 /* vi behaviour: the cursor moves backward but the character that
9267 * was there remains visible
9268 * Vim behaviour: the cursor moves backward and the character that
9269 * was there is erased from the screen.
9270 * We can emulate the vi behaviour by pretending there is a dollar
9271 * displayed even when there isn't.
9272 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009273 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 dollar_vcol = curwin->w_virtcol;
9275
Bram Moolenaarce3be472008-01-14 19:12:28 +00009276#ifdef FEAT_FOLDING
9277 /* When deleting a char the cursor line must never be in a closed fold.
9278 * E.g., when 'foldmethod' is indent and deleting the first non-white
9279 * char before a Tab. */
9280 if (did_backspace)
9281 foldOpenCursor();
9282#endif
9283
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 return did_backspace;
9285}
9286
9287#ifdef FEAT_MOUSE
9288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009289ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290{
9291 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009292 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293
9294# ifdef FEAT_GUI
9295 /* When GUI is active, also move/paste when 'mouse' is empty */
9296 if (!gui.in_use)
9297# endif
9298 if (!mouse_has(MOUSE_INSERT))
9299 return;
9300
9301 undisplay_dollar();
9302 tpos = curwin->w_cursor;
9303 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9304 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009305#ifdef FEAT_WINDOWS
9306 win_T *new_curwin = curwin;
9307
9308 if (curwin != old_curwin && win_valid(old_curwin))
9309 {
9310 /* Mouse took us to another window. We need to go back to the
9311 * previous one to stop insert there properly. */
9312 curwin = old_curwin;
9313 curbuf = curwin->w_buffer;
9314 }
9315#endif
9316 start_arrow(curwin == old_curwin ? &tpos : NULL);
9317#ifdef FEAT_WINDOWS
9318 if (curwin != new_curwin && win_valid(new_curwin))
9319 {
9320 curwin = new_curwin;
9321 curbuf = curwin->w_buffer;
9322 }
9323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324# ifdef FEAT_CINDENT
9325 can_cindent = TRUE;
9326# endif
9327 }
9328
9329#ifdef FEAT_WINDOWS
9330 /* redraw status lines (in case another window became active) */
9331 redraw_statuslines();
9332#endif
9333}
9334
9335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009336ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337{
9338 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009339# if defined(FEAT_WINDOWS)
9340 win_T *old_curwin = curwin;
9341# endif
9342# ifdef FEAT_INS_EXPAND
9343 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344# endif
9345
9346 tpos = curwin->w_cursor;
9347
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009348# ifdef FEAT_WINDOWS
9349 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 {
9351 int row, col;
9352
9353 row = mouse_row;
9354 col = mouse_col;
9355
9356 /* find the window at the pointer coordinates */
9357 curwin = mouse_find_win(&row, &col);
9358 curbuf = curwin->w_buffer;
9359 }
9360 if (curwin == old_curwin)
9361# endif
9362 undisplay_dollar();
9363
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009364# ifdef FEAT_INS_EXPAND
9365 /* Don't scroll the window in which completion is being done. */
9366 if (!pum_visible()
9367# if defined(FEAT_WINDOWS)
9368 || curwin != old_curwin
9369# endif
9370 )
9371# endif
9372 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009373 if (dir == MSCR_DOWN || dir == MSCR_UP)
9374 {
9375 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9376 scroll_redraw(dir,
9377 (long)(curwin->w_botline - curwin->w_topline));
9378 else
9379 scroll_redraw(dir, 3L);
9380 }
9381#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009382 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009383 {
9384 int val, step = 6;
9385
9386 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9387 step = W_WIDTH(curwin);
9388 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9389 if (val < 0)
9390 val = 0;
9391 gui_do_horiz_scroll(val, TRUE);
9392 }
9393#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009394# ifdef FEAT_INS_EXPAND
9395 did_scroll = TRUE;
9396# endif
9397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009399# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 curwin->w_redr_status = TRUE;
9401
9402 curwin = old_curwin;
9403 curbuf = curwin->w_buffer;
9404# endif
9405
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009406# ifdef FEAT_INS_EXPAND
9407 /* The popup menu may overlay the window, need to redraw it.
9408 * TODO: Would be more efficient to only redraw the windows that are
9409 * overlapped by the popup menu. */
9410 if (pum_visible() && did_scroll)
9411 {
9412 redraw_all_later(NOT_VALID);
9413 ins_compl_show_pum();
9414 }
9415# endif
9416
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 if (!equalpos(curwin->w_cursor, tpos))
9418 {
9419 start_arrow(&tpos);
9420# ifdef FEAT_CINDENT
9421 can_cindent = TRUE;
9422# endif
9423 }
9424}
9425#endif
9426
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009427#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009429ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009430{
9431 /* We will be leaving the current window, unless closing another tab. */
9432 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9433 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9434 {
9435 undisplay_dollar();
9436 start_arrow(&curwin->w_cursor);
9437# ifdef FEAT_CINDENT
9438 can_cindent = TRUE;
9439# endif
9440 }
9441
9442 if (c == K_TABLINE)
9443 goto_tabpage(current_tab);
9444 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009445 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009446 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009447 redraw_statuslines(); /* will redraw the tabline when needed */
9448 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009449}
9450#endif
9451
9452#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009454ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
9456 pos_T tpos;
9457
9458 undisplay_dollar();
9459 tpos = curwin->w_cursor;
9460 if (gui_do_scroll())
9461 {
9462 start_arrow(&tpos);
9463# ifdef FEAT_CINDENT
9464 can_cindent = TRUE;
9465# endif
9466 }
9467}
9468
9469 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009470ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471{
9472 pos_T tpos;
9473
9474 undisplay_dollar();
9475 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009476 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 {
9478 start_arrow(&tpos);
9479# ifdef FEAT_CINDENT
9480 can_cindent = TRUE;
9481# endif
9482 }
9483}
9484#endif
9485
9486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009487ins_left(
9488 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489{
9490 pos_T tpos;
9491
9492#ifdef FEAT_FOLDING
9493 if ((fdo_flags & FDO_HOR) && KeyTyped)
9494 foldOpenCursor();
9495#endif
9496 undisplay_dollar();
9497 tpos = curwin->w_cursor;
9498 if (oneleft() == OK)
9499 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009500#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9501 /* Only call start_arrow() when not busy with preediting, it will
9502 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9503 if (!im_is_preediting())
9504#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009505 {
9506 start_arrow_with_change(&tpos, end_change);
9507 if (!end_change)
9508 AppendCharToRedobuff(K_LEFT);
9509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510#ifdef FEAT_RIGHTLEFT
9511 /* If exit reversed string, position is fixed */
9512 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9513 revins_legal++;
9514 revins_chars++;
9515#endif
9516 }
9517
9518 /*
9519 * if 'whichwrap' set for cursor in insert mode may go to
9520 * previous line
9521 */
9522 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9523 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009524 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 start_arrow(&tpos);
9526 --(curwin->w_cursor.lnum);
9527 coladvance((colnr_T)MAXCOL);
9528 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9529 }
9530 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009531 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009532 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533}
9534
9535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009536ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537{
9538 pos_T tpos;
9539
9540#ifdef FEAT_FOLDING
9541 if ((fdo_flags & FDO_HOR) && KeyTyped)
9542 foldOpenCursor();
9543#endif
9544 undisplay_dollar();
9545 tpos = curwin->w_cursor;
9546 if (c == K_C_HOME)
9547 curwin->w_cursor.lnum = 1;
9548 curwin->w_cursor.col = 0;
9549#ifdef FEAT_VIRTUALEDIT
9550 curwin->w_cursor.coladd = 0;
9551#endif
9552 curwin->w_curswant = 0;
9553 start_arrow(&tpos);
9554}
9555
9556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009557ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558{
9559 pos_T tpos;
9560
9561#ifdef FEAT_FOLDING
9562 if ((fdo_flags & FDO_HOR) && KeyTyped)
9563 foldOpenCursor();
9564#endif
9565 undisplay_dollar();
9566 tpos = curwin->w_cursor;
9567 if (c == K_C_END)
9568 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9569 coladvance((colnr_T)MAXCOL);
9570 curwin->w_curswant = MAXCOL;
9571
9572 start_arrow(&tpos);
9573}
9574
9575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009576ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
9578#ifdef FEAT_FOLDING
9579 if ((fdo_flags & FDO_HOR) && KeyTyped)
9580 foldOpenCursor();
9581#endif
9582 undisplay_dollar();
9583 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9584 {
9585 start_arrow(&curwin->w_cursor);
9586 (void)bck_word(1L, FALSE, FALSE);
9587 curwin->w_set_curswant = TRUE;
9588 }
9589 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009590 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591}
9592
9593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009594ins_right(
9595 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
9597#ifdef FEAT_FOLDING
9598 if ((fdo_flags & FDO_HOR) && KeyTyped)
9599 foldOpenCursor();
9600#endif
9601 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009602 if (gchar_cursor() != NUL
9603#ifdef FEAT_VIRTUALEDIT
9604 || virtual_active()
9605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 )
9607 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009608 start_arrow_with_change(&curwin->w_cursor, end_change);
9609 if (!end_change)
9610 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 curwin->w_set_curswant = TRUE;
9612#ifdef FEAT_VIRTUALEDIT
9613 if (virtual_active())
9614 oneright();
9615 else
9616#endif
9617 {
9618#ifdef FEAT_MBYTE
9619 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009620 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 else
9622#endif
9623 ++curwin->w_cursor.col;
9624 }
9625
9626#ifdef FEAT_RIGHTLEFT
9627 revins_legal++;
9628 if (revins_chars)
9629 revins_chars--;
9630#endif
9631 }
9632 /* if 'whichwrap' set for cursor in insert mode, may move the
9633 * cursor to the next line */
9634 else if (vim_strchr(p_ww, ']') != NULL
9635 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9636 {
9637 start_arrow(&curwin->w_cursor);
9638 curwin->w_set_curswant = TRUE;
9639 ++curwin->w_cursor.lnum;
9640 curwin->w_cursor.col = 0;
9641 }
9642 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009643 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009644 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645}
9646
9647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009648ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649{
9650#ifdef FEAT_FOLDING
9651 if ((fdo_flags & FDO_HOR) && KeyTyped)
9652 foldOpenCursor();
9653#endif
9654 undisplay_dollar();
9655 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9656 || gchar_cursor() != NUL)
9657 {
9658 start_arrow(&curwin->w_cursor);
9659 (void)fwd_word(1L, FALSE, 0);
9660 curwin->w_set_curswant = TRUE;
9661 }
9662 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009663 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664}
9665
9666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009667ins_up(
9668 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
9670 pos_T tpos;
9671 linenr_T old_topline = curwin->w_topline;
9672#ifdef FEAT_DIFF
9673 int old_topfill = curwin->w_topfill;
9674#endif
9675
9676 undisplay_dollar();
9677 tpos = curwin->w_cursor;
9678 if (cursor_up(1L, TRUE) == OK)
9679 {
9680 if (startcol)
9681 coladvance(getvcol_nolist(&Insstart));
9682 if (old_topline != curwin->w_topline
9683#ifdef FEAT_DIFF
9684 || old_topfill != curwin->w_topfill
9685#endif
9686 )
9687 redraw_later(VALID);
9688 start_arrow(&tpos);
9689#ifdef FEAT_CINDENT
9690 can_cindent = TRUE;
9691#endif
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_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
9700 pos_T tpos;
9701
9702 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009703
9704#ifdef FEAT_WINDOWS
9705 if (mod_mask & MOD_MASK_CTRL)
9706 {
9707 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009708 if (first_tabpage->tp_next != NULL)
9709 {
9710 start_arrow(&curwin->w_cursor);
9711 goto_tabpage(-1);
9712 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009713 return;
9714 }
9715#endif
9716
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 tpos = curwin->w_cursor;
9718 if (onepage(BACKWARD, 1L) == OK)
9719 {
9720 start_arrow(&tpos);
9721#ifdef FEAT_CINDENT
9722 can_cindent = TRUE;
9723#endif
9724 }
9725 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009726 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727}
9728
9729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009730ins_down(
9731 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732{
9733 pos_T tpos;
9734 linenr_T old_topline = curwin->w_topline;
9735#ifdef FEAT_DIFF
9736 int old_topfill = curwin->w_topfill;
9737#endif
9738
9739 undisplay_dollar();
9740 tpos = curwin->w_cursor;
9741 if (cursor_down(1L, TRUE) == OK)
9742 {
9743 if (startcol)
9744 coladvance(getvcol_nolist(&Insstart));
9745 if (old_topline != curwin->w_topline
9746#ifdef FEAT_DIFF
9747 || old_topfill != curwin->w_topfill
9748#endif
9749 )
9750 redraw_later(VALID);
9751 start_arrow(&tpos);
9752#ifdef FEAT_CINDENT
9753 can_cindent = TRUE;
9754#endif
9755 }
9756 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009757 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758}
9759
9760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009761ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762{
9763 pos_T tpos;
9764
9765 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009766
9767#ifdef FEAT_WINDOWS
9768 if (mod_mask & MOD_MASK_CTRL)
9769 {
9770 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009771 if (first_tabpage->tp_next != NULL)
9772 {
9773 start_arrow(&curwin->w_cursor);
9774 goto_tabpage(0);
9775 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009776 return;
9777 }
9778#endif
9779
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 tpos = curwin->w_cursor;
9781 if (onepage(FORWARD, 1L) == OK)
9782 {
9783 start_arrow(&tpos);
9784#ifdef FEAT_CINDENT
9785 can_cindent = TRUE;
9786#endif
9787 }
9788 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009789 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790}
9791
9792#ifdef FEAT_DND
9793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009794ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795{
9796 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9797}
9798#endif
9799
9800/*
9801 * Handle TAB in Insert or Replace mode.
9802 * Return TRUE when the TAB needs to be inserted like a normal character.
9803 */
9804 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009805ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806{
9807 int ind;
9808 int i;
9809 int temp;
9810
9811 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9812 Insstart_blank_vcol = get_nolist_virtcol();
9813 if (echeck_abbr(TAB + ABBR_OFF))
9814 return FALSE;
9815
9816 ind = inindent(0);
9817#ifdef FEAT_CINDENT
9818 if (ind)
9819 can_cindent = FALSE;
9820#endif
9821
9822 /*
9823 * When nothing special, insert TAB like a normal character
9824 */
9825 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009826 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009827 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 return TRUE;
9829
9830 if (stop_arrow() == FAIL)
9831 return TRUE;
9832
9833 did_ai = FALSE;
9834#ifdef FEAT_SMARTINDENT
9835 did_si = FALSE;
9836 can_si = FALSE;
9837 can_si_back = FALSE;
9838#endif
9839 AppendToRedobuff((char_u *)"\t");
9840
9841 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009842 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009843 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9844 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 else /* otherwise use 'tabstop' */
9846 temp = (int)curbuf->b_p_ts;
9847 temp -= get_nolist_virtcol() % temp;
9848
9849 /*
9850 * Insert the first space with ins_char(). It will delete one char in
9851 * replace mode. Insert the rest with ins_str(); it will not delete any
9852 * chars. For VREPLACE mode, we use ins_char() for all characters.
9853 */
9854 ins_char(' ');
9855 while (--temp > 0)
9856 {
9857#ifdef FEAT_VREPLACE
9858 if (State & VREPLACE_FLAG)
9859 ins_char(' ');
9860 else
9861#endif
9862 {
9863 ins_str((char_u *)" ");
9864 if (State & REPLACE_FLAG) /* no char replaced */
9865 replace_push(NUL);
9866 }
9867 }
9868
9869 /*
9870 * When 'expandtab' not set: Replace spaces by TABs where possible.
9871 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009872 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 {
9874 char_u *ptr;
9875#ifdef FEAT_VREPLACE
9876 char_u *saved_line = NULL; /* init for GCC */
9877 pos_T pos;
9878#endif
9879 pos_T fpos;
9880 pos_T *cursor;
9881 colnr_T want_vcol, vcol;
9882 int change_col = -1;
9883 int save_list = curwin->w_p_list;
9884
9885 /*
9886 * Get the current line. For VREPLACE mode, don't make real changes
9887 * yet, just work on a copy of the line.
9888 */
9889#ifdef FEAT_VREPLACE
9890 if (State & VREPLACE_FLAG)
9891 {
9892 pos = curwin->w_cursor;
9893 cursor = &pos;
9894 saved_line = vim_strsave(ml_get_curline());
9895 if (saved_line == NULL)
9896 return FALSE;
9897 ptr = saved_line + pos.col;
9898 }
9899 else
9900#endif
9901 {
9902 ptr = ml_get_cursor();
9903 cursor = &curwin->w_cursor;
9904 }
9905
9906 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9907 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9908 curwin->w_p_list = FALSE;
9909
9910 /* Find first white before the cursor */
9911 fpos = curwin->w_cursor;
9912 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9913 {
9914 --fpos.col;
9915 --ptr;
9916 }
9917
9918 /* In Replace mode, don't change characters before the insert point. */
9919 if ((State & REPLACE_FLAG)
9920 && fpos.lnum == Insstart.lnum
9921 && fpos.col < Insstart.col)
9922 {
9923 ptr += Insstart.col - fpos.col;
9924 fpos.col = Insstart.col;
9925 }
9926
9927 /* compute virtual column numbers of first white and cursor */
9928 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9929 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9930
Bram Moolenaar597a4222014-06-25 14:39:50 +02009931 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9932 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 while (vim_iswhite(*ptr))
9934 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009935 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 if (vcol + i > want_vcol)
9937 break;
9938 if (*ptr != TAB)
9939 {
9940 *ptr = TAB;
9941 if (change_col < 0)
9942 {
9943 change_col = fpos.col; /* Column of first change */
9944 /* May have to adjust Insstart */
9945 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9946 Insstart.col = fpos.col;
9947 }
9948 }
9949 ++fpos.col;
9950 ++ptr;
9951 vcol += i;
9952 }
9953
9954 if (change_col >= 0)
9955 {
9956 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009957 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958
9959 /* Skip over the spaces we need. */
9960 while (vcol < want_vcol && *ptr == ' ')
9961 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009962 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 ++ptr;
9964 ++repl_off;
9965 }
9966 if (vcol > want_vcol)
9967 {
9968 /* Must have a char with 'showbreak' just before it. */
9969 --ptr;
9970 --repl_off;
9971 }
9972 fpos.col += repl_off;
9973
9974 /* Delete following spaces. */
9975 i = cursor->col - fpos.col;
9976 if (i > 0)
9977 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009978 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 /* correct replace stack. */
9980 if ((State & REPLACE_FLAG)
9981#ifdef FEAT_VREPLACE
9982 && !(State & VREPLACE_FLAG)
9983#endif
9984 )
9985 for (temp = i; --temp >= 0; )
9986 replace_join(repl_off);
9987 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009988#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009989 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009990 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009991 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009992 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9993 (char_u *)"\t", 1);
9994 }
9995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 cursor->col -= i;
9997
9998#ifdef FEAT_VREPLACE
9999 /*
10000 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10001 * backspacing over the changed spacing and then inserting the new
10002 * spacing.
10003 */
10004 if (State & VREPLACE_FLAG)
10005 {
10006 /* Backspace from real cursor to change_col */
10007 backspace_until_column(change_col);
10008
10009 /* Insert each char in saved_line from changed_col to
10010 * ptr-cursor */
10011 ins_bytes_len(saved_line + change_col,
10012 cursor->col - change_col);
10013 }
10014#endif
10015 }
10016
10017#ifdef FEAT_VREPLACE
10018 if (State & VREPLACE_FLAG)
10019 vim_free(saved_line);
10020#endif
10021 curwin->w_p_list = save_list;
10022 }
10023
10024 return FALSE;
10025}
10026
10027/*
10028 * Handle CR or NL in insert mode.
10029 * Return TRUE when out of memory or can't undo.
10030 */
10031 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010032ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033{
10034 int i;
10035
10036 if (echeck_abbr(c + ABBR_OFF))
10037 return FALSE;
10038 if (stop_arrow() == FAIL)
10039 return TRUE;
10040 undisplay_dollar();
10041
10042 /*
10043 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10044 * character under the cursor. Only push a NUL on the replace stack,
10045 * nothing to put back when the NL is deleted.
10046 */
10047 if ((State & REPLACE_FLAG)
10048#ifdef FEAT_VREPLACE
10049 && !(State & VREPLACE_FLAG)
10050#endif
10051 )
10052 replace_push(NUL);
10053
10054 /*
10055 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10056 * replacing the next line, so we push all of the characters left on the
10057 * line onto the replace stack. This is not done here though, it is done
10058 * in open_line().
10059 */
10060
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010061#ifdef FEAT_VIRTUALEDIT
10062 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10063 * CTRL-O). */
10064 if (virtual_active() && curwin->w_cursor.coladd > 0)
10065 coladvance(getviscol());
10066#endif
10067
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068#ifdef FEAT_RIGHTLEFT
10069# ifdef FEAT_FKMAP
10070 if (p_altkeymap && p_fkmap)
10071 fkmap(NL);
10072# endif
10073 /* NL in reverse insert will always start in the end of
10074 * current line. */
10075 if (revins_on)
10076 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10077#endif
10078
10079 AppendToRedobuff(NL_STR);
10080 i = open_line(FORWARD,
10081#ifdef FEAT_COMMENTS
10082 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10083#endif
10084 0, old_indent);
10085 old_indent = 0;
10086#ifdef FEAT_CINDENT
10087 can_cindent = TRUE;
10088#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010089#ifdef FEAT_FOLDING
10090 /* When inserting a line the cursor line must never be in a closed fold. */
10091 foldOpenCursor();
10092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093
10094 return (!i);
10095}
10096
10097#ifdef FEAT_DIGRAPHS
10098/*
10099 * Handle digraph in insert mode.
10100 * Returns character still to be inserted, or NUL when nothing remaining to be
10101 * done.
10102 */
10103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010104ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105{
10106 int c;
10107 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010108 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109
10110 pc_status = PC_STATUS_UNSET;
10111 if (redrawing() && !char_avail())
10112 {
10113 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010114 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115
10116 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010117 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118#ifdef FEAT_CMDL_INFO
10119 add_to_showcmd_c(Ctrl_K);
10120#endif
10121 }
10122
10123#ifdef USE_ON_FLY_SCROLL
10124 dont_scroll = TRUE; /* disallow scrolling here */
10125#endif
10126
10127 /* don't map the digraph chars. This also prevents the
10128 * mode message to be deleted when ESC is hit */
10129 ++no_mapping;
10130 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010131 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 --no_mapping;
10133 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010134 if (did_putchar)
10135 /* when the line fits in 'columns' the '?' is at the start of the next
10136 * line and will not be removed by the redraw */
10137 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010138
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 if (IS_SPECIAL(c) || mod_mask) /* special key */
10140 {
10141#ifdef FEAT_CMDL_INFO
10142 clear_showcmd();
10143#endif
10144 insert_special(c, TRUE, FALSE);
10145 return NUL;
10146 }
10147 if (c != ESC)
10148 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010149 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 if (redrawing() && !char_avail())
10151 {
10152 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010153 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154
10155 if (char2cells(c) == 1)
10156 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010157 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010159 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 }
10161#ifdef FEAT_CMDL_INFO
10162 add_to_showcmd_c(c);
10163#endif
10164 }
10165 ++no_mapping;
10166 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010167 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 --no_mapping;
10169 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010170 if (did_putchar)
10171 /* when the line fits in 'columns' the '?' is at the start of the
10172 * next line and will not be removed by a redraw */
10173 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 if (cc != ESC)
10175 {
10176 AppendToRedobuff((char_u *)CTRL_V_STR);
10177 c = getdigraph(c, cc, TRUE);
10178#ifdef FEAT_CMDL_INFO
10179 clear_showcmd();
10180#endif
10181 return c;
10182 }
10183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184#ifdef FEAT_CMDL_INFO
10185 clear_showcmd();
10186#endif
10187 return NUL;
10188}
10189#endif
10190
10191/*
10192 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10193 * Returns the char to be inserted, or NUL if none found.
10194 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010195 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010196ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197{
10198 int c;
10199 int temp;
10200 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010201 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202
10203 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10204 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010205 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 return NUL;
10207 }
10208
10209 /* try to advance to the cursor column */
10210 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010211 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 prev_ptr = ptr;
10213 validate_virtcol();
10214 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10215 {
10216 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010217 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 }
10219 if ((colnr_T)temp > curwin->w_virtcol)
10220 ptr = prev_ptr;
10221
10222#ifdef FEAT_MBYTE
10223 c = (*mb_ptr2char)(ptr);
10224#else
10225 c = *ptr;
10226#endif
10227 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010228 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 return c;
10230}
10231
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010232/*
10233 * CTRL-Y or CTRL-E typed in Insert mode.
10234 */
10235 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010236ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010237{
10238 int c = tc;
10239
10240#ifdef FEAT_INS_EXPAND
10241 if (ctrl_x_mode == CTRL_X_SCROLL)
10242 {
10243 if (c == Ctrl_Y)
10244 scrolldown_clamp();
10245 else
10246 scrollup_clamp();
10247 redraw_later(VALID);
10248 }
10249 else
10250#endif
10251 {
10252 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10253 if (c != NUL)
10254 {
10255 long tw_save;
10256
10257 /* The character must be taken literally, insert like it
10258 * was typed after a CTRL-V, and pretend 'textwidth'
10259 * wasn't set. Digits, 'o' and 'x' are special after a
10260 * CTRL-V, don't use it for these. */
10261 if (c < 256 && !isalnum(c))
10262 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10263 tw_save = curbuf->b_p_tw;
10264 curbuf->b_p_tw = -1;
10265 insert_special(c, TRUE, FALSE);
10266 curbuf->b_p_tw = tw_save;
10267#ifdef FEAT_RIGHTLEFT
10268 revins_chars++;
10269 revins_legal++;
10270#endif
10271 c = Ctrl_V; /* pretend CTRL-V is last character */
10272 auto_format(FALSE, TRUE);
10273 }
10274 }
10275 return c;
10276}
10277
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278#ifdef FEAT_SMARTINDENT
10279/*
10280 * Try to do some very smart auto-indenting.
10281 * Used when inserting a "normal" character.
10282 */
10283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010284ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285{
10286 pos_T *pos, old_pos;
10287 char_u *ptr;
10288 int i;
10289 int temp;
10290
10291 /*
10292 * do some very smart indenting when entering '{' or '}'
10293 */
10294 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10295 {
10296 /*
10297 * for '}' set indent equal to indent of line containing matching '{'
10298 */
10299 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10300 {
10301 old_pos = curwin->w_cursor;
10302 /*
10303 * If the matching '{' has a ')' immediately before it (ignoring
10304 * white-space), then line up with the start of the line
10305 * containing the matching '(' if there is one. This handles the
10306 * case where an "if (..\n..) {" statement continues over multiple
10307 * lines -- webb
10308 */
10309 ptr = ml_get(pos->lnum);
10310 i = pos->col;
10311 if (i > 0) /* skip blanks before '{' */
10312 while (--i > 0 && vim_iswhite(ptr[i]))
10313 ;
10314 curwin->w_cursor.lnum = pos->lnum;
10315 curwin->w_cursor.col = i;
10316 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10317 curwin->w_cursor = *pos;
10318 i = get_indent();
10319 curwin->w_cursor = old_pos;
10320#ifdef FEAT_VREPLACE
10321 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010322 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 else
10324#endif
10325 (void)set_indent(i, SIN_CHANGED);
10326 }
10327 else if (curwin->w_cursor.col > 0)
10328 {
10329 /*
10330 * when inserting '{' after "O" reduce indent, but not
10331 * more than indent of previous line
10332 */
10333 temp = TRUE;
10334 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10335 {
10336 old_pos = curwin->w_cursor;
10337 i = get_indent();
10338 while (curwin->w_cursor.lnum > 1)
10339 {
10340 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10341
10342 /* ignore empty lines and lines starting with '#'. */
10343 if (*ptr != '#' && *ptr != NUL)
10344 break;
10345 }
10346 if (get_indent() >= i)
10347 temp = FALSE;
10348 curwin->w_cursor = old_pos;
10349 }
10350 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010351 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 }
10353 }
10354
10355 /*
10356 * set indent of '#' always to 0
10357 */
10358 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10359 {
10360 /* remember current indent for next line */
10361 old_indent = get_indent();
10362 (void)set_indent(0, SIN_CHANGED);
10363 }
10364
10365 /* Adjust ai_col, the char at this position can be deleted. */
10366 if (ai_col > curwin->w_cursor.col)
10367 ai_col = curwin->w_cursor.col;
10368}
10369#endif
10370
10371/*
10372 * Get the value that w_virtcol would have when 'list' is off.
10373 * Unless 'cpo' contains the 'L' flag.
10374 */
10375 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010376get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377{
10378 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10379 return getvcol_nolist(&curwin->w_cursor);
10380 validate_virtcol();
10381 return curwin->w_virtcol;
10382}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010383
10384#ifdef FEAT_AUTOCMD
10385/*
10386 * Handle the InsertCharPre autocommand.
10387 * "c" is the character that was typed.
10388 * Return a pointer to allocated memory with the replacement string.
10389 * Return NULL to continue inserting "c".
10390 */
10391 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010392do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010393{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010394 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010395 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010396
10397 /* Return quickly when there is nothing to do. */
10398 if (!has_insertcharpre())
10399 return NULL;
10400
Bram Moolenaar704984a2012-06-01 14:57:51 +020010401#ifdef FEAT_MBYTE
10402 if (has_mbyte)
10403 buf[(*mb_char2bytes)(c, buf)] = NUL;
10404 else
10405#endif
10406 {
10407 buf[0] = c;
10408 buf[1] = NUL;
10409 }
10410
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010411 /* Lock the text to avoid weird things from happening. */
10412 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010413 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010414
Bram Moolenaar704984a2012-06-01 14:57:51 +020010415 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010416 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010417 {
10418 /* Get the value of v:char. It may be empty or more than one
10419 * character. Only use it when changed, otherwise continue with the
10420 * original character to avoid breaking autoindent. */
10421 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10422 res = vim_strsave(get_vim_var_str(VV_CHAR));
10423 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010424
10425 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10426 --textlock;
10427
10428 return res;
10429}
10430#endif