blob: 626d3272c208a429d1f0eba77d3fccf008d781db [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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);
182static void ins_compl_insert(void);
183static int ins_compl_next(int allow_get_expansion, int count, int insert_match);
184static 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 Moolenaar071d4272004-06-13 20:20:40 +0000652 if (stop_insert_mode)
653 {
654 /* ":stopinsert" used or 'insertmode' reset */
655 count = 0;
656 goto doESCkey;
657 }
658
659 /* set curwin->w_curswant for next K_DOWN or K_UP */
660 if (!arrow_used)
661 curwin->w_set_curswant = TRUE;
662
663 /* If there is no typeahead may check for timestamps (e.g., for when a
664 * menu invoked a shell command). */
665 if (stuff_empty())
666 {
667 did_check_timestamps = FALSE;
668 if (need_check_timestamps)
669 check_timestamps(FALSE);
670 }
671
672 /*
673 * When emsg() was called msg_scroll will have been set.
674 */
675 msg_scroll = FALSE;
676
677#ifdef FEAT_GUI
678 /* When 'mousefocus' is set a mouse movement may have taken us to
679 * another window. "need_mouse_correct" may then be set because of an
680 * autocommand. */
681 if (need_mouse_correct)
682 gui_mouse_correct();
683#endif
684
685#ifdef FEAT_FOLDING
686 /* Open fold at the cursor line, according to 'foldopen'. */
687 if (fdo_flags & FDO_INSERT)
688 foldOpenCursor();
689 /* Close folds where the cursor isn't, according to 'foldclose' */
690 if (!char_avail())
691 foldCheckClose();
692#endif
693
694 /*
695 * If we inserted a character at the last position of the last line in
696 * the window, scroll the window one line up. This avoids an extra
697 * redraw.
698 * This is detected when the cursor column is smaller after inserting
699 * something.
700 * Don't do this when the topline changed already, it has
701 * already been adjusted (by insertchar() calling open_line())).
702 */
703 if (curbuf->b_mod_set
704 && curwin->w_p_wrap
705 && !did_backspace
706 && curwin->w_topline == old_topline
707#ifdef FEAT_DIFF
708 && curwin->w_topfill == old_topfill
709#endif
710 )
711 {
712 mincol = curwin->w_wcol;
713 validate_cursor_col();
714
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000715 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 && curwin->w_wrow == W_WINROW(curwin)
717 + curwin->w_height - 1 - p_so
718 && (curwin->w_cursor.lnum != curwin->w_topline
719#ifdef FEAT_DIFF
720 || curwin->w_topfill > 0
721#endif
722 ))
723 {
724#ifdef FEAT_DIFF
725 if (curwin->w_topfill > 0)
726 --curwin->w_topfill;
727 else
728#endif
729#ifdef FEAT_FOLDING
730 if (hasFolding(curwin->w_topline, NULL, &old_topline))
731 set_topline(curwin, old_topline + 1);
732 else
733#endif
734 set_topline(curwin, curwin->w_topline + 1);
735 }
736 }
737
738 /* May need to adjust w_topline to show the cursor. */
739 update_topline();
740
741 did_backspace = FALSE;
742
743 validate_cursor(); /* may set must_redraw */
744
745 /*
746 * Redraw the display when no characters are waiting.
747 * Also shows mode, ruler and positions cursor.
748 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000749 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
751#ifdef FEAT_SCROLLBIND
752 if (curwin->w_p_scb)
753 do_check_scrollbind(TRUE);
754#endif
755
Bram Moolenaar860cae12010-06-05 23:22:07 +0200756#ifdef FEAT_CURSORBIND
757 if (curwin->w_p_crb)
758 do_check_cursorbind();
759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 update_curswant();
761 old_topline = curwin->w_topline;
762#ifdef FEAT_DIFF
763 old_topfill = curwin->w_topfill;
764#endif
765
766#ifdef USE_ON_FLY_SCROLL
767 dont_scroll = FALSE; /* allow scrolling here */
768#endif
769
770 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000771 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100773 if (c != K_CURSORHOLD)
774 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200775
776 /* After using CTRL-G U the next cursor key will not break undo. */
777 if (dont_sync_undo == MAYBE)
778 dont_sync_undo = TRUE;
779 else
780 dont_sync_undo = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000781 do
782 {
783 c = safe_vgetc();
784 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000786#ifdef FEAT_AUTOCMD
787 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
788 did_cursorhold = TRUE;
789#endif
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791#ifdef FEAT_RIGHTLEFT
792 if (p_hkmap && KeyTyped)
793 c = hkmap(c); /* Hebrew mode mapping */
794#endif
795#ifdef FEAT_FKMAP
796 if (p_fkmap && KeyTyped)
797 c = fkmap(c); /* Farsi mode mapping */
798#endif
799
800#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000801 /*
802 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000803 * and the cursor is still in the completed word. Only when there is
804 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000805 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000806 if (compl_started
807 && pum_wanted()
808 && curwin->w_cursor.col >= compl_col
809 && (compl_shown_match == NULL
810 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000811 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000812 /* BS: Delete one character from "compl_leader". */
813 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000814 && curwin->w_cursor.col > compl_col
815 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000816 continue;
817
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000818 /* When no match was selected or it was edited. */
819 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000820 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000821 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000822 * "compl_leader". Except when at the original match and
823 * there is nothing to add, CTRL-L works like CTRL-P then. */
824 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100825 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000826 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000827 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000828 {
829 ins_compl_addfrommatch();
830 continue;
831 }
832
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000833 /* A non-white character that fits in with the current
834 * completion: Add to "compl_leader". */
835 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000836 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100837#ifdef FEAT_AUTOCMD
838 /* Trigger InsertCharPre. */
839 char_u *str = do_insert_char_pre(c);
840 char_u *p;
841
842 if (str != NULL)
843 {
844 for (p = str; *p != NUL; mb_ptr_adv(p))
845 ins_compl_addleader(PTR2CHAR(p));
846 vim_free(str);
847 }
848 else
849#endif
850 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000851 continue;
852 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000853
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000854 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000855 * compl_enter_selects is set the Enter key does the same. */
856 if (c == Ctrl_Y || (compl_enter_selects
857 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000858 {
859 ins_compl_delete();
860 ins_compl_insert();
861 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000862 }
863 }
864
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
866 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000867 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000868 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000869 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870#endif
871
Bram Moolenaar488c6512005-08-11 20:09:58 +0000872 /* CTRL-\ CTRL-N goes to Normal mode,
873 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
874 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (c == Ctrl_BSL)
876 {
877 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000878 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 ++no_mapping;
880 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000881 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 --no_mapping;
883 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000884 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000886 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 vungetc(c);
888 c = Ctrl_BSL;
889 }
890 else if (c == Ctrl_G && p_im)
891 continue;
892 else
893 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000894 if (c == Ctrl_O)
895 {
896 ins_ctrl_o();
897 ins_at_eol = FALSE; /* cursor keeps its column */
898 nomove = TRUE;
899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 count = 0;
901 goto doESCkey;
902 }
903 }
904
905#ifdef FEAT_DIGRAPHS
906 c = do_digraph(c);
907#endif
908
909#ifdef FEAT_INS_EXPAND
910 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
911 goto docomplete;
912#endif
913 if (c == Ctrl_V || c == Ctrl_Q)
914 {
915 ins_ctrl_v();
916 c = Ctrl_V; /* pretend CTRL-V is last typed character */
917 continue;
918 }
919
920#ifdef FEAT_CINDENT
921 if (cindent_on()
922# ifdef FEAT_INS_EXPAND
923 && ctrl_x_mode == 0
924# endif
925 )
926 {
927 /* A key name preceded by a bang means this key is not to be
928 * inserted. Skip ahead to the re-indenting below.
929 * A key name preceded by a star means that indenting has to be
930 * done before inserting the key. */
931 line_is_white = inindent(0);
932 if (in_cinkeys(c, '!', line_is_white))
933 goto force_cindent;
934 if (can_cindent && in_cinkeys(c, '*', line_is_white)
935 && stop_arrow() == OK)
936 do_c_expr_indent();
937 }
938#endif
939
940#ifdef FEAT_RIGHTLEFT
941 if (curwin->w_p_rl)
942 switch (c)
943 {
944 case K_LEFT: c = K_RIGHT; break;
945 case K_S_LEFT: c = K_S_RIGHT; break;
946 case K_C_LEFT: c = K_C_RIGHT; break;
947 case K_RIGHT: c = K_LEFT; break;
948 case K_S_RIGHT: c = K_S_LEFT; break;
949 case K_C_RIGHT: c = K_C_LEFT; break;
950 }
951#endif
952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 /*
954 * If 'keymodel' contains "startsel", may start selection. If it
955 * does, a CTRL-O and c will be stuffed, we need to get these
956 * characters.
957 */
958 if (ins_start_select(c))
959 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 /*
962 * The big switch to handle a character in insert mode.
963 */
964 switch (c)
965 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000966 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 if (echeck_abbr(ESC + ABBR_OFF))
968 break;
969 /*FALLTHROUGH*/
970
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000971 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972#ifdef FEAT_CMDWIN
973 if (c == Ctrl_C && cmdwin_type != 0)
974 {
975 /* Close the cmdline window. */
976 cmdwin_result = K_IGNORE;
977 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000978 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 goto doESCkey;
980 }
981#endif
982
983#ifdef UNIX
984do_intr:
985#endif
986 /* when 'insertmode' set, and not halfway a mapping, don't leave
987 * Insert mode */
988 if (goto_im())
989 {
990 if (got_int)
991 {
992 (void)vgetc(); /* flush all buffers */
993 got_int = FALSE;
994 }
995 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200996 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 break;
998 }
999doESCkey:
1000 /*
1001 * This is the ONLY return from edit()!
1002 */
1003 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1004 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001005 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 o_lnum = curwin->w_cursor.lnum;
1007
Bram Moolenaar488c6512005-08-11 20:09:58 +00001008 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001009 {
1010#ifdef FEAT_AUTOCMD
1011 if (cmdchar != 'r' && cmdchar != 'v')
1012 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1013 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001014 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 continue;
1019
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001020 case Ctrl_Z: /* suspend when 'insertmode' set */
1021 if (!p_im)
1022 goto normalchar; /* insert CTRL-Z as normal char */
1023 stuffReadbuff((char_u *)":st\r");
1024 c = Ctrl_O;
1025 /*FALLTHROUGH*/
1026
1027 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001028#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001029 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001030 goto docomplete;
1031#endif
1032 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1033 break;
1034 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001035
1036#ifdef FEAT_VIRTUALEDIT
1037 /* don't move the cursor left when 'virtualedit' has "onemore". */
1038 if (ve_flags & VE_ONEMORE)
1039 {
1040 ins_at_eol = FALSE;
1041 nomove = TRUE;
1042 }
1043#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001044 count = 0;
1045 goto doESCkey;
1046
Bram Moolenaar572cb562005-08-05 21:35:02 +00001047 case K_INS: /* toggle insert/replace mode */
1048 case K_KINS:
1049 ins_insert(replaceState);
1050 break;
1051
1052 case K_SELECT: /* end of Select mode mapping - ignore */
1053 break;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case K_HELP: /* Help key works like <ESC> <Help> */
1056 case K_F1:
1057 case K_XF1:
1058 stuffcharReadbuff(K_HELP);
1059 if (p_im)
1060 need_start_insertmode = TRUE;
1061 goto doESCkey;
1062
1063#ifdef FEAT_NETBEANS_INTG
1064 case K_F21: /* NetBeans command */
1065 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001066 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001067 --no_mapping;
1068 netbeans_keycommand(i);
1069 break;
1070#endif
1071
1072 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 case NUL:
1074 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001075 /* For ^@ the trailing ESC will end the insert, unless there is an
1076 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1078 && c != Ctrl_A && !p_im)
1079 goto doESCkey; /* quit insert mode */
1080 inserted_space = FALSE;
1081 break;
1082
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 ins_reg();
1085 auto_format(FALSE, TRUE);
1086 inserted_space = FALSE;
1087 break;
1088
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001089 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 ins_ctrl_g();
1091 break;
1092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001093 case Ctrl_HAT: /* switch input mode and/or langmap */
1094 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 break;
1096
1097#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (!p_ari)
1100 goto normalchar;
1101 ins_ctrl_();
1102 break;
1103#endif
1104
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001105 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1107 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1108 goto docomplete;
1109#endif
1110 /* FALLTHROUGH */
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113# ifdef FEAT_INS_EXPAND
1114 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1115 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001116 if (has_compl_option(FALSE))
1117 goto docomplete;
1118 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120# endif
1121 ins_shift(c, lastc);
1122 auto_format(FALSE, TRUE);
1123 inserted_space = FALSE;
1124 break;
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 case K_KDEL:
1128 ins_del();
1129 auto_format(FALSE, TRUE);
1130 break;
1131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 case Ctrl_H:
1134 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1135 auto_format(FALSE, TRUE);
1136 break;
1137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1140 auto_format(FALSE, TRUE);
1141 break;
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001144# ifdef FEAT_COMPL_FUNC
1145 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001147 goto docomplete;
1148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1150 auto_format(FALSE, TRUE);
1151 inserted_space = FALSE;
1152 break;
1153
1154#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 case K_LEFTMOUSE_NM:
1157 case K_LEFTDRAG:
1158 case K_LEFTRELEASE:
1159 case K_LEFTRELEASE_NM:
1160 case K_MIDDLEMOUSE:
1161 case K_MIDDLEDRAG:
1162 case K_MIDDLERELEASE:
1163 case K_RIGHTMOUSE:
1164 case K_RIGHTDRAG:
1165 case K_RIGHTRELEASE:
1166 case K_X1MOUSE:
1167 case K_X1DRAG:
1168 case K_X1RELEASE:
1169 case K_X2MOUSE:
1170 case K_X2DRAG:
1171 case K_X2RELEASE:
1172 ins_mouse(c);
1173 break;
1174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001175 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001176 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 break;
1178
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001180 ins_mousescroll(MSCR_UP);
1181 break;
1182
1183 case K_MOUSELEFT: /* Scroll wheel left */
1184 ins_mousescroll(MSCR_LEFT);
1185 break;
1186
1187 case K_MOUSERIGHT: /* Scroll wheel right */
1188 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 break;
1190#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001191#ifdef FEAT_GUI_TABLINE
1192 case K_TABLINE:
1193 case K_TABMENU:
1194 ins_tabline(c);
1195 break;
1196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 break;
1200
Bram Moolenaar754b5602006-02-09 23:53:20 +00001201#ifdef FEAT_AUTOCMD
1202 case K_CURSORHOLD: /* Didn't type something for a while. */
1203 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1204 did_cursorhold = TRUE;
1205 break;
1206#endif
1207
Bram Moolenaar4770d092006-01-12 23:22:24 +00001208#ifdef FEAT_GUI_W32
1209 /* On Win32 ignore <M-F4>, we get it when closing the window was
1210 * cancelled. */
1211 case K_F4:
1212 if (mod_mask != MOD_MASK_ALT)
1213 goto normalchar;
1214 break;
1215#endif
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#ifdef FEAT_GUI
1218 case K_VER_SCROLLBAR:
1219 ins_scroll();
1220 break;
1221
1222 case K_HOR_SCROLLBAR:
1223 ins_horscroll();
1224 break;
1225#endif
1226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001227 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 case K_S_HOME:
1230 case K_C_HOME:
1231 ins_home(c);
1232 break;
1233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 case K_S_END:
1237 case K_C_END:
1238 ins_end(c);
1239 break;
1240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001241 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001242 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1243 ins_s_left();
1244 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001245 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 break;
1247
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001248 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 case K_C_LEFT:
1250 ins_s_left();
1251 break;
1252
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001253 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001254 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1255 ins_s_right();
1256 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001257 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 break;
1259
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001260 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 case K_C_RIGHT:
1262 ins_s_right();
1263 break;
1264
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001265 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001266#ifdef FEAT_INS_EXPAND
1267 if (pum_visible())
1268 goto docomplete;
1269#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001270 if (mod_mask & MOD_MASK_SHIFT)
1271 ins_pageup();
1272 else
1273 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 break;
1275
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001276 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 case K_PAGEUP:
1278 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001279#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001280 if (pum_visible())
1281 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 ins_pageup();
1284 break;
1285
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001286 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001287#ifdef FEAT_INS_EXPAND
1288 if (pum_visible())
1289 goto docomplete;
1290#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001291 if (mod_mask & MOD_MASK_SHIFT)
1292 ins_pagedown();
1293 else
1294 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 break;
1296
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001297 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 case K_PAGEDOWN:
1299 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001300#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001301 if (pum_visible())
1302 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 ins_pagedown();
1305 break;
1306
1307#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 ins_drop();
1310 break;
1311#endif
1312
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001313 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 c = TAB;
1315 /* FALLTHROUGH */
1316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001317 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1319 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1320 goto docomplete;
1321#endif
1322 inserted_space = FALSE;
1323 if (ins_tab())
1324 goto normalchar; /* insert TAB as a normal char */
1325 auto_format(FALSE, TRUE);
1326 break;
1327
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001328 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 c = CAR;
1330 /* FALLTHROUGH */
1331 case CAR:
1332 case NL:
1333#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1334 /* In a quickfix window a <CR> jumps to the error under the
1335 * cursor. */
1336 if (bt_quickfix(curbuf) && c == CAR)
1337 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001338 if (curwin->w_llist_ref == NULL) /* quickfix window */
1339 do_cmdline_cmd((char_u *)".cc");
1340 else /* location list window */
1341 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 break;
1343 }
1344#endif
1345#ifdef FEAT_CMDWIN
1346 if (cmdwin_type != 0)
1347 {
1348 /* Execute the command in the cmdline window. */
1349 cmdwin_result = CAR;
1350 goto doESCkey;
1351 }
1352#endif
1353 if (ins_eol(c) && !p_im)
1354 goto doESCkey; /* out of memory */
1355 auto_format(FALSE, FALSE);
1356 inserted_space = FALSE;
1357 break;
1358
Bram Moolenaar860cae12010-06-05 23:22:07 +02001359#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001360 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361# ifdef FEAT_INS_EXPAND
1362 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1363 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001364 if (has_compl_option(TRUE))
1365 goto docomplete;
1366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368# endif
1369# ifdef FEAT_DIGRAPHS
1370 c = ins_digraph();
1371 if (c == NUL)
1372 break;
1373# endif
1374 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001378 case Ctrl_X: /* Enter CTRL-X mode */
1379 ins_ctrl_x();
1380 break;
1381
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001382 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (ctrl_x_mode != CTRL_X_TAGS)
1384 goto normalchar;
1385 goto docomplete;
1386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001387 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (ctrl_x_mode != CTRL_X_FILES)
1389 goto normalchar;
1390 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001391
1392 case 's': /* Spelling completion after ^X */
1393 case Ctrl_S:
1394 if (ctrl_x_mode != CTRL_X_SPELL)
1395 goto normalchar;
1396 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397#endif
1398
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001399 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400#ifdef FEAT_INS_EXPAND
1401 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1402#endif
1403 {
1404 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1405 if (p_im)
1406 {
1407 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1408 break;
1409 goto doESCkey;
1410 }
1411 goto normalchar;
1412 }
1413#ifdef FEAT_INS_EXPAND
1414 /* FALLTHROUGH */
1415
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001416 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 case Ctrl_N:
1418 /* if 'complete' is empty then plain ^P is no longer special,
1419 * but it is under other ^X modes */
1420 if (*curbuf->b_p_cpt == NUL
1421 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001422 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 goto normalchar;
1424
1425docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001426 compl_busy = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001427 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001428 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001429 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 break;
1431#endif /* FEAT_INS_EXPAND */
1432
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001433 case Ctrl_Y: /* copy from previous line or scroll down */
1434 case Ctrl_E: /* copy from next line or scroll up */
1435 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 break;
1437
1438 default:
1439#ifdef UNIX
1440 if (c == intr_char) /* special interrupt char */
1441 goto do_intr;
1442#endif
1443
Bram Moolenaare659c952011-05-19 17:25:41 +02001444normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001446 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001448#ifdef FEAT_AUTOCMD
1449 if (!p_paste)
1450 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001451 /* Trigger InsertCharPre. */
1452 char_u *str = do_insert_char_pre(c);
1453 char_u *p;
1454
1455 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001456 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001457 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001458 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001459 /* Insert the new value of v:char literally. */
1460 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001461 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001462 c = PTR2CHAR(p);
1463 if (c == CAR || c == K_KENTER || c == NL)
1464 ins_eol(c);
1465 else
1466 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001467 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001468 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001469 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001470 vim_free(str);
1471 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001472 }
1473
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001474 /* If the new value is already inserted or an empty string
1475 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001476 if (c == NUL)
1477 break;
1478 }
1479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480#ifdef FEAT_SMARTINDENT
1481 /* Try to perform smart-indenting. */
1482 ins_try_si(c);
1483#endif
1484
1485 if (c == ' ')
1486 {
1487 inserted_space = TRUE;
1488#ifdef FEAT_CINDENT
1489 if (inindent(0))
1490 can_cindent = FALSE;
1491#endif
1492 if (Insstart_blank_vcol == MAXCOL
1493 && curwin->w_cursor.lnum == Insstart.lnum)
1494 Insstart_blank_vcol = get_nolist_virtcol();
1495 }
1496
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001497 /* Insert a normal character and check for abbreviations on a
1498 * special character. Let CTRL-] expand abbreviations without
1499 * inserting it. */
1500 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501#ifdef FEAT_MBYTE
1502 /* Add ABBR_OFF for characters above 0x100, this is
1503 * what check_abbr() expects. */
1504 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1505#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001506 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
1508 insert_special(c, FALSE, FALSE);
1509#ifdef FEAT_RIGHTLEFT
1510 revins_legal++;
1511 revins_chars++;
1512#endif
1513 }
1514
1515 auto_format(FALSE, TRUE);
1516
1517#ifdef FEAT_FOLDING
1518 /* When inserting a character the cursor line must never be in a
1519 * closed fold. */
1520 foldOpenCursor();
1521#endif
1522 break;
1523 } /* end of switch (c) */
1524
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001525#ifdef FEAT_AUTOCMD
1526 /* If typed something may trigger CursorHoldI again. */
1527 if (c != K_CURSORHOLD)
1528 did_cursorhold = FALSE;
1529#endif
1530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 /* If the cursor was moved we didn't just insert a space */
1532 if (arrow_used)
1533 inserted_space = FALSE;
1534
1535#ifdef FEAT_CINDENT
1536 if (can_cindent && cindent_on()
1537# ifdef FEAT_INS_EXPAND
1538 && ctrl_x_mode == 0
1539# endif
1540 )
1541 {
1542force_cindent:
1543 /*
1544 * Indent now if a key was typed that is in 'cinkeys'.
1545 */
1546 if (in_cinkeys(c, ' ', line_is_white))
1547 {
1548 if (stop_arrow() == OK)
1549 /* re-indent the current line */
1550 do_c_expr_indent();
1551 }
1552 }
1553#endif /* FEAT_CINDENT */
1554
1555 } /* for (;;) */
1556 /* NOTREACHED */
1557}
1558
1559/*
1560 * Redraw for Insert mode.
1561 * This is postponed until getting the next character to make '$' in the 'cpo'
1562 * option work correctly.
1563 * Only redraw when there are no characters available. This speeds up
1564 * inserting sequences of characters (e.g., for CTRL-R).
1565 */
1566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001567ins_redraw(
1568 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001570#ifdef FEAT_CONCEAL
1571 linenr_T conceal_old_cursor_line = 0;
1572 linenr_T conceal_new_cursor_line = 0;
1573 int conceal_update_lines = FALSE;
1574#endif
1575
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001576 if (char_avail())
1577 return;
1578
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001579#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001580 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1581 * visible, the command might delete it. */
1582 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001583# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001584 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001585# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001586# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001587 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001588# endif
1589# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001590 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001591# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001592 )
1593 && !equalpos(last_cursormoved, curwin->w_cursor)
1594# ifdef FEAT_INS_EXPAND
1595 && !pum_visible()
1596# endif
1597 )
1598 {
1599# ifdef FEAT_SYN_HL
1600 /* Need to update the screen first, to make sure syntax
1601 * highlighting is correct after making a change (e.g., inserting
1602 * a "(". The autocommand may also require a redraw, so it's done
1603 * again below, unfortunately. */
1604 if (syntax_present(curwin) && must_redraw)
1605 update_screen(0);
1606# endif
1607# ifdef FEAT_AUTOCMD
1608 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001609 {
1610 /* Make sure curswant is correct, an autocommand may call
1611 * getcurpos(). */
1612 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001613 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001614 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001615# endif
1616# ifdef FEAT_CONCEAL
1617 if (curwin->w_p_cole > 0)
1618 {
1619 conceal_old_cursor_line = last_cursormoved.lnum;
1620 conceal_new_cursor_line = curwin->w_cursor.lnum;
1621 conceal_update_lines = TRUE;
1622 }
1623# endif
1624 last_cursormoved = curwin->w_cursor;
1625 }
1626#endif
1627
1628#ifdef FEAT_AUTOCMD
1629 /* Trigger TextChangedI if b_changedtick differs. */
1630 if (ready && has_textchangedI()
1631 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001632# ifdef FEAT_INS_EXPAND
1633 && !pum_visible()
1634# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001635 )
1636 {
1637 if (last_changedtick_buf == curbuf)
1638 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1639 last_changedtick_buf = curbuf;
1640 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001642#endif
1643
1644 if (must_redraw)
1645 update_screen(0);
1646 else if (clear_cmdline || redraw_cmdline)
1647 showmode(); /* clear cmdline and show mode */
1648# if defined(FEAT_CONCEAL)
1649 if ((conceal_update_lines
1650 && (conceal_old_cursor_line != conceal_new_cursor_line
1651 || conceal_cursor_line(curwin)))
1652 || need_cursor_line_redraw)
1653 {
1654 if (conceal_old_cursor_line != conceal_new_cursor_line)
1655 update_single_line(curwin, conceal_old_cursor_line);
1656 update_single_line(curwin, conceal_new_cursor_line == 0
1657 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1658 curwin->w_valid &= ~VALID_CROW;
1659 }
1660# endif
1661 showruler(FALSE);
1662 setcursor();
1663 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664}
1665
1666/*
1667 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1668 */
1669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001670ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001673 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
1675 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001676 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001681 did_putchar = TRUE;
1682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1684
1685#ifdef FEAT_CMDL_INFO
1686 add_to_showcmd_c(Ctrl_V);
1687#endif
1688
1689 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001690 if (did_putchar)
1691 /* when the line fits in 'columns' the '^' is at the start of the next
1692 * line and will not removed by the redraw */
1693 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694#ifdef FEAT_CMDL_INFO
1695 clear_showcmd();
1696#endif
1697 insert_special(c, FALSE, TRUE);
1698#ifdef FEAT_RIGHTLEFT
1699 revins_chars++;
1700 revins_legal++;
1701#endif
1702}
1703
1704/*
1705 * Put a character directly onto the screen. It's not stored in a buffer.
1706 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1707 */
1708static int pc_status;
1709#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1710#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1711#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1712#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714static int pc_attr;
1715static int pc_row;
1716static int pc_col;
1717
1718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001719edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 int attr;
1722
1723 if (ScreenLines != NULL)
1724 {
1725 update_topline(); /* just in case w_topline isn't valid */
1726 validate_cursor();
1727 if (highlight)
1728 attr = hl_attr(HLF_8);
1729 else
1730 attr = 0;
1731 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1732 pc_col = W_WINCOL(curwin);
1733#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1734 pc_status = PC_STATUS_UNSET;
1735#endif
1736#ifdef FEAT_RIGHTLEFT
1737 if (curwin->w_p_rl)
1738 {
1739 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1740# ifdef FEAT_MBYTE
1741 if (has_mbyte)
1742 {
1743 int fix_col = mb_fix_col(pc_col, pc_row);
1744
1745 if (fix_col != pc_col)
1746 {
1747 screen_putchar(' ', pc_row, fix_col, attr);
1748 --curwin->w_wcol;
1749 pc_status = PC_STATUS_RIGHT;
1750 }
1751 }
1752# endif
1753 }
1754 else
1755#endif
1756 {
1757 pc_col += curwin->w_wcol;
1758#ifdef FEAT_MBYTE
1759 if (mb_lefthalve(pc_row, pc_col))
1760 pc_status = PC_STATUS_LEFT;
1761#endif
1762 }
1763
1764 /* save the character to be able to put it back */
1765#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1766 if (pc_status == PC_STATUS_UNSET)
1767#endif
1768 {
1769 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1770 pc_status = PC_STATUS_SET;
1771 }
1772 screen_putchar(c, pc_row, pc_col, attr);
1773 }
1774}
1775
1776/*
1777 * Undo the previous edit_putchar().
1778 */
1779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001780edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1783 {
1784#if defined(FEAT_MBYTE)
1785 if (pc_status == PC_STATUS_RIGHT)
1786 ++curwin->w_wcol;
1787 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1788 redrawWinline(curwin->w_cursor.lnum, FALSE);
1789 else
1790#endif
1791 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1792 }
1793}
1794
1795/*
1796 * Called when p_dollar is set: display a '$' at the end of the changed text
1797 * Only works when cursor is in the line that changes.
1798 */
1799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001800display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
1802 colnr_T save_col;
1803
1804 if (!redrawing())
1805 return;
1806
1807 cursor_off();
1808 save_col = curwin->w_cursor.col;
1809 curwin->w_cursor.col = col;
1810#ifdef FEAT_MBYTE
1811 if (has_mbyte)
1812 {
1813 char_u *p;
1814
1815 /* If on the last byte of a multi-byte move to the first byte. */
1816 p = ml_get_curline();
1817 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1818 }
1819#endif
1820 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1821 if (curwin->w_wcol < W_WIDTH(curwin))
1822 {
1823 edit_putchar('$', FALSE);
1824 dollar_vcol = curwin->w_virtcol;
1825 }
1826 curwin->w_cursor.col = save_col;
1827}
1828
1829/*
1830 * Call this function before moving the cursor from the normal insert position
1831 * in insert mode.
1832 */
1833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001834undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001836 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001838 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 redrawWinline(curwin->w_cursor.lnum, FALSE);
1840 }
1841}
1842
1843/*
1844 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1845 * Keep the cursor on the same character.
1846 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1847 * type == INDENT_DEC decrease indent (for CTRL-D)
1848 * type == INDENT_SET set indent to "amount"
1849 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1850 */
1851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001852change_indent(
1853 int type,
1854 int amount,
1855 int round,
1856 int replaced, /* replaced character, put on replace stack */
1857 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858{
1859 int vcol;
1860 int last_vcol;
1861 int insstart_less; /* reduction for Insstart.col */
1862 int new_cursor_col;
1863 int i;
1864 char_u *ptr;
1865 int save_p_list;
1866 int start_col;
1867 colnr_T vc;
1868#ifdef FEAT_VREPLACE
1869 colnr_T orig_col = 0; /* init for GCC */
1870 char_u *new_line, *orig_line = NULL; /* init for GCC */
1871
1872 /* VREPLACE mode needs to know what the line was like before changing */
1873 if (State & VREPLACE_FLAG)
1874 {
1875 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1876 orig_col = curwin->w_cursor.col;
1877 }
1878#endif
1879
1880 /* for the following tricks we don't want list mode */
1881 save_p_list = curwin->w_p_list;
1882 curwin->w_p_list = FALSE;
1883 vc = getvcol_nolist(&curwin->w_cursor);
1884 vcol = vc;
1885
1886 /*
1887 * For Replace mode we need to fix the replace stack later, which is only
1888 * possible when the cursor is in the indent. Remember the number of
1889 * characters before the cursor if it's possible.
1890 */
1891 start_col = curwin->w_cursor.col;
1892
1893 /* determine offset from first non-blank */
1894 new_cursor_col = curwin->w_cursor.col;
1895 beginline(BL_WHITE);
1896 new_cursor_col -= curwin->w_cursor.col;
1897
1898 insstart_less = curwin->w_cursor.col;
1899
1900 /*
1901 * If the cursor is in the indent, compute how many screen columns the
1902 * cursor is to the left of the first non-blank.
1903 */
1904 if (new_cursor_col < 0)
1905 vcol = get_indent() - vcol;
1906
1907 if (new_cursor_col > 0) /* can't fix replace stack */
1908 start_col = -1;
1909
1910 /*
1911 * Set the new indent. The cursor will be put on the first non-blank.
1912 */
1913 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001914 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 else
1916 {
1917#ifdef FEAT_VREPLACE
1918 int save_State = State;
1919
1920 /* Avoid being called recursively. */
1921 if (State & VREPLACE_FLAG)
1922 State = INSERT;
1923#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001924 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925#ifdef FEAT_VREPLACE
1926 State = save_State;
1927#endif
1928 }
1929 insstart_less -= curwin->w_cursor.col;
1930
1931 /*
1932 * Try to put cursor on same character.
1933 * If the cursor is at or after the first non-blank in the line,
1934 * compute the cursor column relative to the column of the first
1935 * non-blank character.
1936 * If we are not in insert mode, leave the cursor on the first non-blank.
1937 * If the cursor is before the first non-blank, position it relative
1938 * to the first non-blank, counted in screen columns.
1939 */
1940 if (new_cursor_col >= 0)
1941 {
1942 /*
1943 * When changing the indent while the cursor is touching it, reset
1944 * Insstart_col to 0.
1945 */
1946 if (new_cursor_col == 0)
1947 insstart_less = MAXCOL;
1948 new_cursor_col += curwin->w_cursor.col;
1949 }
1950 else if (!(State & INSERT))
1951 new_cursor_col = curwin->w_cursor.col;
1952 else
1953 {
1954 /*
1955 * Compute the screen column where the cursor should be.
1956 */
1957 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001958 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959
1960 /*
1961 * Advance the cursor until we reach the right screen column.
1962 */
1963 vcol = last_vcol = 0;
1964 new_cursor_col = -1;
1965 ptr = ml_get_curline();
1966 while (vcol <= (int)curwin->w_virtcol)
1967 {
1968 last_vcol = vcol;
1969#ifdef FEAT_MBYTE
1970 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001971 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 else
1973#endif
1974 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001975 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
1977 vcol = last_vcol;
1978
1979 /*
1980 * May need to insert spaces to be able to position the cursor on
1981 * the right screen column.
1982 */
1983 if (vcol != (int)curwin->w_virtcol)
1984 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001985 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001987 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (ptr != NULL)
1989 {
1990 new_cursor_col += i;
1991 ptr[i] = NUL;
1992 while (--i >= 0)
1993 ptr[i] = ' ';
1994 ins_str(ptr);
1995 vim_free(ptr);
1996 }
1997 }
1998
1999 /*
2000 * When changing the indent while the cursor is in it, reset
2001 * Insstart_col to 0.
2002 */
2003 insstart_less = MAXCOL;
2004 }
2005
2006 curwin->w_p_list = save_p_list;
2007
2008 if (new_cursor_col <= 0)
2009 curwin->w_cursor.col = 0;
2010 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002011 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 curwin->w_set_curswant = TRUE;
2013 changed_cline_bef_curs();
2014
2015 /*
2016 * May have to adjust the start of the insert.
2017 */
2018 if (State & INSERT)
2019 {
2020 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2021 {
2022 if ((int)Insstart.col <= insstart_less)
2023 Insstart.col = 0;
2024 else
2025 Insstart.col -= insstart_less;
2026 }
2027 if ((int)ai_col <= insstart_less)
2028 ai_col = 0;
2029 else
2030 ai_col -= insstart_less;
2031 }
2032
2033 /*
2034 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2035 * If the number of characters before the cursor decreased, need to pop a
2036 * few characters from the replace stack.
2037 * If the number of characters before the cursor increased, need to push a
2038 * few NULs onto the replace stack.
2039 */
2040 if (REPLACE_NORMAL(State) && start_col >= 0)
2041 {
2042 while (start_col > (int)curwin->w_cursor.col)
2043 {
2044 replace_join(0); /* remove a NUL from the replace stack */
2045 --start_col;
2046 }
2047 while (start_col < (int)curwin->w_cursor.col || replaced)
2048 {
2049 replace_push(NUL);
2050 if (replaced)
2051 {
2052 replace_push(replaced);
2053 replaced = NUL;
2054 }
2055 ++start_col;
2056 }
2057 }
2058
2059#ifdef FEAT_VREPLACE
2060 /*
2061 * For VREPLACE mode, we also have to fix the replace stack. In this case
2062 * it is always possible because we backspace over the whole line and then
2063 * put it back again the way we wanted it.
2064 */
2065 if (State & VREPLACE_FLAG)
2066 {
2067 /* If orig_line didn't allocate, just return. At least we did the job,
2068 * even if you can't backspace. */
2069 if (orig_line == NULL)
2070 return;
2071
2072 /* Save new line */
2073 new_line = vim_strsave(ml_get_curline());
2074 if (new_line == NULL)
2075 return;
2076
2077 /* We only put back the new line up to the cursor */
2078 new_line[curwin->w_cursor.col] = NUL;
2079
2080 /* Put back original line */
2081 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2082 curwin->w_cursor.col = orig_col;
2083
2084 /* Backspace from cursor to start of line */
2085 backspace_until_column(0);
2086
2087 /* Insert new stuff into line again */
2088 ins_bytes(new_line);
2089
2090 vim_free(new_line);
2091 }
2092#endif
2093}
2094
2095/*
2096 * Truncate the space at the end of a line. This is to be used only in an
2097 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2098 * modes.
2099 */
2100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002101truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102{
2103 int i;
2104
2105 /* find start of trailing white space */
2106 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2107 {
2108 if (State & REPLACE_FLAG)
2109 replace_join(0); /* remove a NUL from the replace stack */
2110 }
2111 line[i + 1] = NUL;
2112}
2113
2114#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2115 || defined(FEAT_COMMENTS) || defined(PROTO)
2116/*
2117 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2118 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002119 * Will attempt not to go before "col" even when there is a composing
2120 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 */
2122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002123backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 while ((int)curwin->w_cursor.col > col)
2126 {
2127 curwin->w_cursor.col--;
2128 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002129 replace_do_bs(col);
2130 else if (!del_char_after_col(col))
2131 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133}
2134#endif
2135
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002136/*
2137 * Like del_char(), but make sure not to go before column "limit_col".
2138 * Only matters when there are composing characters.
2139 * Return TRUE when something was deleted.
2140 */
2141 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002142del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002143{
2144#ifdef FEAT_MBYTE
2145 if (enc_utf8 && limit_col >= 0)
2146 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002147 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002148
2149 /* Make sure the cursor is at the start of a character, but
2150 * skip forward again when going too far back because of a
2151 * composing character. */
2152 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002153 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002154 {
2155 int l = utf_ptr2len(ml_get_cursor());
2156
2157 if (l == 0) /* end of line */
2158 break;
2159 curwin->w_cursor.col += l;
2160 }
2161 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2162 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002163 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002164 }
2165 else
2166#endif
2167 (void)del_char(FALSE);
2168 return TRUE;
2169}
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2172/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002173 * CTRL-X pressed in Insert mode.
2174 */
2175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002176ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002177{
2178 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2179 * CTRL-V works like CTRL-N */
2180 if (ctrl_x_mode != CTRL_X_CMDLINE)
2181 {
2182 /* if the next ^X<> won't ADD nothing, then reset
2183 * compl_cont_status */
2184 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002185 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002186 else
2187 compl_cont_status = 0;
2188 /* We're not sure which CTRL-X mode it will be yet */
2189 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2190 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2191 edit_submode_pre = NULL;
2192 showmode();
2193 }
2194}
2195
2196/*
2197 * Return TRUE if the 'dict' or 'tsr' option can be used.
2198 */
2199 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002200has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002201{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002202 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002203# ifdef FEAT_SPELL
2204 && !curwin->w_p_spell
2205# endif
2206 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002207 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2208 {
2209 ctrl_x_mode = 0;
2210 edit_submode = NULL;
2211 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2212 : (char_u *)_("'thesaurus' option is empty"),
2213 hl_attr(HLF_E));
2214 if (emsg_silent == 0)
2215 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002216 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002217 setcursor();
2218 out_flush();
2219 ui_delay(2000L, FALSE);
2220 }
2221 return FALSE;
2222 }
2223 return TRUE;
2224}
2225
2226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2228 * This depends on the current mode.
2229 */
2230 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002231vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
2233 /* Always allow ^R - let it's results then be checked */
2234 if (c == Ctrl_R)
2235 return TRUE;
2236
Bram Moolenaare3226be2005-12-18 22:10:00 +00002237 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002238 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002239 return TRUE;
2240
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 switch (ctrl_x_mode)
2242 {
2243 case 0: /* Not in any CTRL-X mode */
2244 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2245 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002246 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2248 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2249 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002250 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002251 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 case CTRL_X_SCROLL:
2253 return (c == Ctrl_Y || c == Ctrl_E);
2254 case CTRL_X_WHOLE_LINE:
2255 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2256 case CTRL_X_FILES:
2257 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2258 case CTRL_X_DICTIONARY:
2259 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2260 case CTRL_X_THESAURUS:
2261 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2262 case CTRL_X_TAGS:
2263 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2264#ifdef FEAT_FIND_ID
2265 case CTRL_X_PATH_PATTERNS:
2266 return (c == Ctrl_P || c == Ctrl_N);
2267 case CTRL_X_PATH_DEFINES:
2268 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2269#endif
2270 case CTRL_X_CMDLINE:
2271 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2272 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002273#ifdef FEAT_COMPL_FUNC
2274 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002275 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002276 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002277 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002278#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002279 case CTRL_X_SPELL:
2280 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002281 case CTRL_X_EVAL:
2282 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
2284 EMSG(_(e_internal));
2285 return FALSE;
2286}
2287
2288/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002289 * Return TRUE when character "c" is part of the item currently being
2290 * completed. Used to decide whether to abandon complete mode when the menu
2291 * is visible.
2292 */
2293 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002294ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002295{
2296 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2297 /* When expanding an identifier only accept identifier chars. */
2298 return vim_isIDc(c);
2299
2300 switch (ctrl_x_mode)
2301 {
2302 case CTRL_X_FILES:
2303 /* When expanding file name only accept file name chars. But not
2304 * path separators, so that "proto/<Tab>" expands files in
2305 * "proto", not "proto/" as a whole */
2306 return vim_isfilec(c) && !vim_ispathsep(c);
2307
2308 case CTRL_X_CMDLINE:
2309 case CTRL_X_OMNI:
2310 /* Command line and Omni completion can work with just about any
2311 * printable character, but do stop at white space. */
2312 return vim_isprintc(c) && !vim_iswhite(c);
2313
2314 case CTRL_X_WHOLE_LINE:
2315 /* For while line completion a space can be part of the line. */
2316 return vim_isprintc(c);
2317 }
2318 return vim_iswordc(c);
2319}
2320
2321/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002322 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002324 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 * the rest of the word to be in -- webb
2326 */
2327 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002328ins_compl_add_infercase(
2329 char_u *str,
2330 int len,
2331 int icase,
2332 char_u *fname,
2333 int dir,
2334 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002336 char_u *p;
2337 int i, c;
2338 int actual_len; /* Take multi-byte characters */
2339 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002340 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002341 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 int has_lower = FALSE;
2343 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002345 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002347 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348
Bram Moolenaara2993e12007-08-12 14:38:46 +00002349 /* Find actual length of completion. */
2350#ifdef FEAT_MBYTE
2351 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002353 p = str;
2354 actual_len = 0;
2355 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002357 mb_ptr_adv(p);
2358 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 }
2360 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002361 else
2362#endif
2363 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
Bram Moolenaara2993e12007-08-12 14:38:46 +00002365 /* Find actual length of original text. */
2366#ifdef FEAT_MBYTE
2367 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002369 p = compl_orig_text;
2370 actual_compl_length = 0;
2371 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002373 mb_ptr_adv(p);
2374 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
2376 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002377 else
2378#endif
2379 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002381 /* "actual_len" may be smaller than "actual_compl_length" when using
2382 * thesaurus, only use the minimum when comparing. */
2383 min_len = actual_len < actual_compl_length
2384 ? actual_len : actual_compl_length;
2385
Bram Moolenaara2993e12007-08-12 14:38:46 +00002386 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002387 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002388 if (wca != NULL)
2389 {
2390 p = str;
2391 for (i = 0; i < actual_len; ++i)
2392#ifdef FEAT_MBYTE
2393 if (has_mbyte)
2394 wca[i] = mb_ptr2char_adv(&p);
2395 else
2396#endif
2397 wca[i] = *(p++);
2398
2399 /* Rule 1: Were any chars converted to lower? */
2400 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002401 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002402 {
2403#ifdef FEAT_MBYTE
2404 if (has_mbyte)
2405 c = mb_ptr2char_adv(&p);
2406 else
2407#endif
2408 c = *(p++);
2409 if (MB_ISLOWER(c))
2410 {
2411 has_lower = TRUE;
2412 if (MB_ISUPPER(wca[i]))
2413 {
2414 /* Rule 1 is satisfied. */
2415 for (i = actual_compl_length; i < actual_len; ++i)
2416 wca[i] = MB_TOLOWER(wca[i]);
2417 break;
2418 }
2419 }
2420 }
2421
2422 /*
2423 * Rule 2: No lower case, 2nd consecutive letter converted to
2424 * upper case.
2425 */
2426 if (!has_lower)
2427 {
2428 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002429 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002430 {
2431#ifdef FEAT_MBYTE
2432 if (has_mbyte)
2433 c = mb_ptr2char_adv(&p);
2434 else
2435#endif
2436 c = *(p++);
2437 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2438 {
2439 /* Rule 2 is satisfied. */
2440 for (i = actual_compl_length; i < actual_len; ++i)
2441 wca[i] = MB_TOUPPER(wca[i]);
2442 break;
2443 }
2444 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2445 }
2446 }
2447
2448 /* Copy the original case of the part we typed. */
2449 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002450 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002451 {
2452#ifdef FEAT_MBYTE
2453 if (has_mbyte)
2454 c = mb_ptr2char_adv(&p);
2455 else
2456#endif
2457 c = *(p++);
2458 if (MB_ISLOWER(c))
2459 wca[i] = MB_TOLOWER(wca[i]);
2460 else if (MB_ISUPPER(c))
2461 wca[i] = MB_TOUPPER(wca[i]);
2462 }
2463
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002464 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002465 * Generate encoding specific output from wide character array.
2466 * Multi-byte characters can occupy up to five bytes more than
2467 * ASCII characters, and we also need one byte for NUL, so stay
2468 * six bytes away from the edge of IObuff.
2469 */
2470 p = IObuff;
2471 i = 0;
2472 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2473#ifdef FEAT_MBYTE
2474 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002475 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002476 else
2477#endif
2478 *(p++) = wca[i++];
2479 *p = NUL;
2480
2481 vim_free(wca);
2482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002484 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2485 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002487 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488}
2489
2490/*
2491 * Add a match to the list of matches.
2492 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002493 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002494 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002496 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002497ins_compl_add(
2498 char_u *str,
2499 int len,
2500 int icase,
2501 char_u *fname,
2502 char_u **cptext, /* extra text for popup menu or NULL */
2503 int cdir,
2504 int flags,
2505 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002507 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002508 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510 ui_breakcheck();
2511 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002512 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 if (len < 0)
2514 len = (int)STRLEN(str);
2515
2516 /*
2517 * If the same match is already present, don't add it.
2518 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002519 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002521 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 do
2523 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002524 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002525 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002526 && match->cp_str[len] == NUL)
2527 return NOTDONE;
2528 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002529 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 }
2531
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002532 /* Remove any popup menu before changing the list of matches. */
2533 ins_compl_del_pum();
2534
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 /*
2536 * Allocate a new match structure.
2537 * Copy the values to the new match structure.
2538 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002539 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002541 return FAIL;
2542 match->cp_number = -1;
2543 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002544 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002545 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
2547 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002548 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002550 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002551
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002553 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2555 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002556 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002557 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002558 && compl_curr_match->cp_fname != NULL
2559 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002560 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002561 else if (fname != NULL)
2562 {
2563 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002564 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002567 match->cp_fname = NULL;
2568 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002569
2570 if (cptext != NULL)
2571 {
2572 int i;
2573
2574 for (i = 0; i < CPT_COUNT; ++i)
2575 if (cptext[i] != NULL && *cptext[i] != NUL)
2576 match->cp_text[i] = vim_strsave(cptext[i]);
2577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
2579 /*
2580 * Link the new match structure in the list of matches.
2581 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002582 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002583 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 else if (dir == FORWARD)
2585 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002586 match->cp_next = compl_curr_match->cp_next;
2587 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
2589 else /* BACKWARD */
2590 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002591 match->cp_next = compl_curr_match;
2592 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002594 if (match->cp_next)
2595 match->cp_next->cp_prev = match;
2596 if (match->cp_prev)
2597 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002599 compl_first_match = match;
2600 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002602 /*
2603 * Find the longest common string if still doing that.
2604 */
2605 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2606 ins_compl_longest_match(match);
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 return OK;
2609}
2610
2611/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002612 * Return TRUE if "str[len]" matches with match->cp_str, considering
2613 * match->cp_icase.
2614 */
2615 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002616ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002617{
2618 if (match->cp_icase)
2619 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2620 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2621}
2622
2623/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002624 * Reduce the longest common string for match "match".
2625 */
2626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002627ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002628{
2629 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002630 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002631 int had_match;
2632
2633 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002634 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002635 /* First match, use it as a whole. */
2636 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002637 if (compl_leader != NULL)
2638 {
2639 had_match = (curwin->w_cursor.col > compl_col);
2640 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002641 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002642 ins_redraw(FALSE);
2643
2644 /* When the match isn't there (to avoid matching itself) remove it
2645 * again after redrawing. */
2646 if (!had_match)
2647 ins_compl_delete();
2648 compl_used_match = FALSE;
2649 }
2650 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002651 else
2652 {
2653 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002654 p = compl_leader;
2655 s = match->cp_str;
2656 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002657 {
2658#ifdef FEAT_MBYTE
2659 if (has_mbyte)
2660 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002661 c1 = mb_ptr2char(p);
2662 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002663 }
2664 else
2665#endif
2666 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002667 c1 = *p;
2668 c2 = *s;
2669 }
2670 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2671 : (c1 != c2))
2672 break;
2673#ifdef FEAT_MBYTE
2674 if (has_mbyte)
2675 {
2676 mb_ptr_adv(p);
2677 mb_ptr_adv(s);
2678 }
2679 else
2680#endif
2681 {
2682 ++p;
2683 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002684 }
2685 }
2686
2687 if (*p != NUL)
2688 {
2689 /* Leader was shortened, need to change the inserted text. */
2690 *p = NUL;
2691 had_match = (curwin->w_cursor.col > compl_col);
2692 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002693 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002694 ins_redraw(FALSE);
2695
2696 /* When the match isn't there (to avoid matching itself) remove it
2697 * again after redrawing. */
2698 if (!had_match)
2699 ins_compl_delete();
2700 }
2701
2702 compl_used_match = FALSE;
2703 }
2704}
2705
2706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 * Add an array of matches to the list of matches.
2708 * Frees matches[].
2709 */
2710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002711ins_compl_add_matches(
2712 int num_matches,
2713 char_u **matches,
2714 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715{
2716 int i;
2717 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002718 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
Bram Moolenaar572cb562005-08-05 21:35:02 +00002720 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002721 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002722 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002724 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 FreeWild(num_matches, matches);
2726}
2727
2728/* Make the completion list cyclic.
2729 * Return the number of matches (excluding the original).
2730 */
2731 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002732ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002734 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 int count = 0;
2736
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002737 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
2739 /*
2740 * Find the end of the list.
2741 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002742 match = compl_first_match;
2743 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002744 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002746 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 ++count;
2748 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002749 match->cp_next = compl_first_match;
2750 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 }
2752 return count;
2753}
2754
Bram Moolenaara94bc432006-03-10 21:42:59 +00002755/*
2756 * Start completion for the complete() function.
2757 * "startcol" is where the matched text starts (1 is first column).
2758 * "list" is the list of matches.
2759 */
2760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002761set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002762{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002763 int save_w_wrow = curwin->w_wrow;
2764
Bram Moolenaara94bc432006-03-10 21:42:59 +00002765 /* If already doing completions stop it. */
2766 if (ctrl_x_mode != 0)
2767 ins_compl_prep(' ');
2768 ins_compl_clear();
2769
2770 if (stop_arrow() == FAIL)
2771 return;
2772
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002773 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002774 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002775 startcol = curwin->w_cursor.col;
2776 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002777 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002778 /* compl_pattern doesn't need to be set */
2779 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2780 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002781 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002782 return;
2783
Bram Moolenaare4214502015-03-05 18:08:43 +01002784 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002785
2786 ins_compl_add_list(list);
2787 compl_matches = ins_compl_make_cyclic();
2788 compl_started = TRUE;
2789 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002790 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002791
2792 compl_curr_match = compl_first_match;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002793 if (compl_no_insert)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002794 ins_complete(K_DOWN, FALSE);
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002795 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002796 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar2cf69382015-07-17 13:42:23 +02002797 if (compl_no_select)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002798 ins_complete(Ctrl_P, FALSE);
2799
2800 /* Lazily show the popup menu, unless we got interrupted. */
2801 if (!compl_interrupted)
2802 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002803 out_flush();
2804}
2805
2806
Bram Moolenaar9372a112005-12-06 19:59:18 +00002807/* "compl_match_array" points the currently displayed list of entries in the
2808 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002809static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002810static int compl_match_arraysize;
2811
2812/*
2813 * Update the screen and when there is any scrolling remove the popup menu.
2814 */
2815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002816ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002817{
2818 int h;
2819
2820 if (compl_match_array != NULL)
2821 {
2822 h = curwin->w_cline_height;
2823 update_screen(0);
2824 if (h != curwin->w_cline_height)
2825 ins_compl_del_pum();
2826 }
2827}
2828
2829/*
2830 * Remove any popup menu.
2831 */
2832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002833ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002834{
2835 if (compl_match_array != NULL)
2836 {
2837 pum_undisplay();
2838 vim_free(compl_match_array);
2839 compl_match_array = NULL;
2840 }
2841}
2842
2843/*
2844 * Return TRUE if the popup menu should be displayed.
2845 */
2846 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002847pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002848{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002849 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002850 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002851 return FALSE;
2852
2853 /* The display looks bad on a B&W display. */
2854 if (t_colors < 8
2855#ifdef FEAT_GUI
2856 && !gui.in_use
2857#endif
2858 )
2859 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002860 return TRUE;
2861}
2862
2863/*
2864 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002865 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002866 */
2867 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002868pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002869{
2870 compl_T *compl;
2871 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002872
2873 /* Don't display the popup menu if there are no matches or there is only
2874 * one (ignoring the original text). */
2875 compl = compl_first_match;
2876 i = 0;
2877 do
2878 {
2879 if (compl == NULL
2880 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2881 break;
2882 compl = compl->cp_next;
2883 } while (compl != compl_first_match);
2884
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002885 if (strstr((char *)p_cot, "menuone") != NULL)
2886 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002887 return (i >= 2);
2888}
2889
2890/*
2891 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002892 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002893 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002895ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002896{
2897 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002898 compl_T *shown_compl = NULL;
2899 int did_find_shown_match = FALSE;
2900 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002901 int i;
2902 int cur = -1;
2903 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002904 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002905
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002906 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002907 return;
2908
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002909#if defined(FEAT_EVAL)
2910 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2911 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2912#endif
2913
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002914 /* Update the screen before drawing the popup menu over it. */
2915 update_screen(0);
2916
2917 if (compl_match_array == NULL)
2918 {
2919 /* Need to build the popup menu list. */
2920 compl_match_arraysize = 0;
2921 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002922 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002923 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002924 do
2925 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002926 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2927 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002928 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002929 ++compl_match_arraysize;
2930 compl = compl->cp_next;
2931 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002932 if (compl_match_arraysize == 0)
2933 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002934 compl_match_array = (pumitem_T *)alloc_clear(
2935 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002936 * compl_match_arraysize));
2937 if (compl_match_array != NULL)
2938 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002939 /* If the current match is the original text don't find the first
2940 * match after it, don't highlight anything. */
2941 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2942 shown_match_ok = TRUE;
2943
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002944 i = 0;
2945 compl = compl_first_match;
2946 do
2947 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002948 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2949 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002950 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002951 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002952 if (!shown_match_ok)
2953 {
2954 if (compl == compl_shown_match || did_find_shown_match)
2955 {
2956 /* This item is the shown match or this is the
2957 * first displayed item after the shown match. */
2958 compl_shown_match = compl;
2959 did_find_shown_match = TRUE;
2960 shown_match_ok = TRUE;
2961 }
2962 else
2963 /* Remember this displayed match for when the
2964 * shown match is just below it. */
2965 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002966 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002967 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002968
2969 if (compl->cp_text[CPT_ABBR] != NULL)
2970 compl_match_array[i].pum_text =
2971 compl->cp_text[CPT_ABBR];
2972 else
2973 compl_match_array[i].pum_text = compl->cp_str;
2974 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2975 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2976 if (compl->cp_text[CPT_MENU] != NULL)
2977 compl_match_array[i++].pum_extra =
2978 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002979 else
2980 compl_match_array[i++].pum_extra = compl->cp_fname;
2981 }
2982
2983 if (compl == compl_shown_match)
2984 {
2985 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002986
2987 /* When the original text is the shown match don't set
2988 * compl_shown_match. */
2989 if (compl->cp_flags & ORIGINAL_TEXT)
2990 shown_match_ok = TRUE;
2991
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002992 if (!shown_match_ok && shown_compl != NULL)
2993 {
2994 /* The shown match isn't displayed, set it to the
2995 * previously displayed match. */
2996 compl_shown_match = shown_compl;
2997 shown_match_ok = TRUE;
2998 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002999 }
3000 compl = compl->cp_next;
3001 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003002
3003 if (!shown_match_ok) /* no displayed match at all */
3004 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003005 }
3006 }
3007 else
3008 {
3009 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003010 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003011 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3012 || compl_match_array[i].pum_text
3013 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003014 {
3015 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003016 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003017 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003018 }
3019
3020 if (compl_match_array != NULL)
3021 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003022 /* In Replace mode when a $ is displayed at the end of the line only
3023 * part of the screen would be updated. We do need to redraw here. */
3024 dollar_vcol = -1;
3025
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003026 /* Compute the screen column of the start of the completed text.
3027 * Use the cursor to get all wrapping and other settings right. */
3028 col = curwin->w_cursor.col;
3029 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003030 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003031 curwin->w_cursor.col = col;
3032 }
3033}
3034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035#define DICT_FIRST (1) /* use just first element in "dict" */
3036#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003037
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003039 * Add any identifiers that match the given pattern in the list of dictionary
3040 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 */
3042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003043ins_compl_dictionaries(
3044 char_u *dict_start,
3045 char_u *pat,
3046 int flags, /* DICT_FIRST and/or DICT_EXACT */
3047 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003049 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 char_u *ptr;
3051 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 char_u **files;
3054 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003056 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
Bram Moolenaar0b238792006-03-02 22:49:12 +00003058 if (*dict == NUL)
3059 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003060#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003061 /* When 'dictionary' is empty and spell checking is enabled use
3062 * "spell". */
3063 if (!thesaurus && curwin->w_p_spell)
3064 dict = (char_u *)"spell";
3065 else
3066#endif
3067 return;
3068 }
3069
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003071 if (buf == NULL)
3072 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003073 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003074
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 /* If 'infercase' is set, don't use 'smartcase' here */
3076 save_p_scs = p_scs;
3077 if (curbuf->b_p_inf)
3078 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003079
3080 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3081 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003082 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003083 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003084 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003085 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003086 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003087
3088 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003089 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003090 len = STRLEN(pat_esc) + 10;
3091 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003092 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003093 {
3094 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003095 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003096 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003097 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003098 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3099 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003100 vim_free(ptr);
3101 }
3102 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003103 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003104 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003105 if (regmatch.regprog == NULL)
3106 goto theend;
3107 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3110 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003111 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 {
3113 /* copy one dictionary file name into buf */
3114 if (flags == DICT_EXACT)
3115 {
3116 count = 1;
3117 files = &dict;
3118 }
3119 else
3120 {
3121 /* Expand wildcards in the dictionary name, but do not allow
3122 * backticks (for security, the 'dict' option may have been set in
3123 * a modeline). */
3124 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003125# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003126 if (!thesaurus && STRCMP(buf, "spell") == 0)
3127 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003128 else
3129# endif
3130 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 || expand_wildcards(1, &buf, &count, &files,
3132 EW_FILE|EW_SILENT) != OK)
3133 count = 0;
3134 }
3135
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003136# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003137 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003139 /* Complete from active spelling. Skip "\<" in the pattern, we
3140 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003141 if (pat[0] == '\\' && pat[1] == '<')
3142 ptr = pat + 2;
3143 else
3144 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003145 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003147 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003148# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003149 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003150 {
3151 ins_compl_files(count, files, thesaurus, flags,
3152 &regmatch, buf, &dir);
3153 if (flags != DICT_EXACT)
3154 FreeWild(count, files);
3155 }
3156 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 break;
3158 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003159
3160theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003162 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 vim_free(buf);
3164}
3165
Bram Moolenaar0b238792006-03-02 22:49:12 +00003166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003167ins_compl_files(
3168 int count,
3169 char_u **files,
3170 int thesaurus,
3171 int flags,
3172 regmatch_T *regmatch,
3173 char_u *buf,
3174 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003175{
3176 char_u *ptr;
3177 int i;
3178 FILE *fp;
3179 int add_r;
3180
3181 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3182 {
3183 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3184 if (flags != DICT_EXACT)
3185 {
3186 vim_snprintf((char *)IObuff, IOSIZE,
3187 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003188 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003189 }
3190
3191 if (fp != NULL)
3192 {
3193 /*
3194 * Read dictionary file line by line.
3195 * Check each line for a match.
3196 */
3197 while (!got_int && !compl_interrupted
3198 && !vim_fgets(buf, LSIZE, fp))
3199 {
3200 ptr = buf;
3201 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3202 {
3203 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003204 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003205 ptr = find_line_end(ptr);
3206 else
3207 ptr = find_word_end(ptr);
3208 add_r = ins_compl_add_infercase(regmatch->startp[0],
3209 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003210 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003211 if (thesaurus)
3212 {
3213 char_u *wstart;
3214
3215 /*
3216 * Add the other matches on the line
3217 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003218 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003219 while (!got_int)
3220 {
3221 /* Find start of the next word. Skip white
3222 * space and punctuation. */
3223 ptr = find_word_start(ptr);
3224 if (*ptr == NUL || *ptr == NL)
3225 break;
3226 wstart = ptr;
3227
Bram Moolenaara2993e12007-08-12 14:38:46 +00003228 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003229#ifdef FEAT_MBYTE
3230 if (has_mbyte)
3231 /* Japanese words may have characters in
3232 * different classes, only separate words
3233 * with single-byte non-word characters. */
3234 while (*ptr != NUL)
3235 {
3236 int l = (*mb_ptr2len)(ptr);
3237
3238 if (l < 2 && !vim_iswordc(*ptr))
3239 break;
3240 ptr += l;
3241 }
3242 else
3243#endif
3244 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003245
3246 /* Add the word. Skip the regexp match. */
3247 if (wstart != regmatch->startp[0])
3248 add_r = ins_compl_add_infercase(wstart,
3249 (int)(ptr - wstart),
3250 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003251 }
3252 }
3253 if (add_r == OK)
3254 /* if dir was BACKWARD then honor it just once */
3255 *dir = FORWARD;
3256 else if (add_r == FAIL)
3257 break;
3258 /* avoid expensive call to vim_regexec() when at end
3259 * of line */
3260 if (*ptr == '\n' || got_int)
3261 break;
3262 }
3263 line_breakcheck();
3264 ins_compl_check_keys(50);
3265 }
3266 fclose(fp);
3267 }
3268 }
3269}
3270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271/*
3272 * Find the start of the next word.
3273 * Returns a pointer to the first char of the word. Also stops at a NUL.
3274 */
3275 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003276find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278#ifdef FEAT_MBYTE
3279 if (has_mbyte)
3280 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003281 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 else
3283#endif
3284 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3285 ++ptr;
3286 return ptr;
3287}
3288
3289/*
3290 * Find the end of the word. Assumes it starts inside a word.
3291 * Returns a pointer to just after the word.
3292 */
3293 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003294find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295{
3296#ifdef FEAT_MBYTE
3297 int start_class;
3298
3299 if (has_mbyte)
3300 {
3301 start_class = mb_get_class(ptr);
3302 if (start_class > 1)
3303 while (*ptr != NUL)
3304 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003305 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (mb_get_class(ptr) != start_class)
3307 break;
3308 }
3309 }
3310 else
3311#endif
3312 while (vim_iswordc(*ptr))
3313 ++ptr;
3314 return ptr;
3315}
3316
3317/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003318 * Find the end of the line, omitting CR and NL at the end.
3319 * Returns a pointer to just after the line.
3320 */
3321 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003322find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003323{
3324 char_u *s;
3325
3326 s = ptr + STRLEN(ptr);
3327 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3328 --s;
3329 return s;
3330}
3331
3332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 * Free the list of completions
3334 */
3335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003338 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003339 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003341 vim_free(compl_pattern);
3342 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003343 vim_free(compl_leader);
3344 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003346 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003348
3349 ins_compl_del_pum();
3350 pum_clear();
3351
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003352 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 do
3354 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003355 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003356 compl_curr_match = compl_curr_match->cp_next;
3357 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003359 if (match->cp_flags & FREE_FNAME)
3360 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003361 for (i = 0; i < CPT_COUNT; ++i)
3362 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003364 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3365 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003366 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367}
3368
3369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003370ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003372 compl_cont_status = 0;
3373 compl_started = FALSE;
3374 compl_matches = 0;
3375 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 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003380 vim_free(compl_orig_text);
3381 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003382 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003383 /* clear v:completed_item */
3384 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385}
3386
3387/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003388 * Return TRUE when Insert completion is active.
3389 */
3390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003391ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003392{
3393 return compl_started;
3394}
3395
3396/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003397 * Delete one character before the cursor and show the subset of the matches
3398 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003399 * Returns the character to be used, NUL if the work is done and another char
3400 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003401 */
3402 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003403ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003404{
3405 char_u *line;
3406 char_u *p;
3407
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003408 line = ml_get_curline();
3409 p = line + curwin->w_cursor.col;
3410 mb_ptr_back(line, p);
3411
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003412 /* Stop completion when the whole word was deleted. For Omni completion
3413 * allow the word to be deleted, we won't match everything. */
3414 if ((int)(p - line) - (int)compl_col < 0
3415 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003416 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003417 return K_BS;
3418
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003419 /* Deleted more than what was used to find matches or didn't finish
3420 * finding all matches: need to look for matches all over again. */
3421 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003422 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003423 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003424
Bram Moolenaara6557602006-02-04 22:43:20 +00003425 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003426 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003427 if (compl_leader != NULL)
3428 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003429 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003430 if (compl_shown_match != NULL)
3431 /* Make sure current match is not a hidden item. */
3432 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003433 return NUL;
3434 }
3435 return K_BS;
3436}
Bram Moolenaara6557602006-02-04 22:43:20 +00003437
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003438/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003439 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3440 * be called.
3441 */
3442 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003443ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003444{
3445 /* Return TRUE if we didn't complete finding matches or when the
3446 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3447 return compl_was_interrupted
3448 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3449 && compl_opt_refresh_always);
3450}
3451
3452/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003453 * Called after changing "compl_leader".
3454 * Show the popup menu with a different set of matches.
3455 * May also search for matches again if the previous search was interrupted.
3456 */
3457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003458ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003459{
3460 ins_compl_del_pum();
3461 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003462 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003463 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003464
3465 if (compl_started)
3466 ins_compl_set_original_text(compl_leader);
3467 else
3468 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003469#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003470 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003471#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003472 /*
3473 * Matches were cleared, need to search for them now. First display
3474 * the changed text before the cursor. Set "compl_restarting" to
3475 * avoid that the first match is inserted.
3476 */
3477 update_screen(0);
3478#ifdef FEAT_GUI
3479 if (gui.in_use)
3480 {
3481 /* Show the cursor after the match, not after the redrawn text. */
3482 setcursor();
3483 out_flush();
3484 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003485 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003486#endif
3487 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003488 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003489 compl_cont_status = 0;
3490 compl_restarting = FALSE;
3491 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003492
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003493 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003494
3495 /* Show the popup menu with a different set of matches. */
3496 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003497
3498 /* Don't let Enter select the original text when there is no popup menu. */
3499 if (compl_match_array == NULL)
3500 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003501}
3502
3503/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003504 * Return the length of the completion, from the completion start column to
3505 * the cursor column. Making sure it never goes below zero.
3506 */
3507 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003508ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003509{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003510 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003511
3512 if (off < 0)
3513 return 0;
3514 return off;
3515}
3516
3517/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003518 * Append one character to the match leader. May reduce the number of
3519 * matches.
3520 */
3521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003522ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003523{
3524#ifdef FEAT_MBYTE
3525 int cc;
3526
3527 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3528 {
3529 char_u buf[MB_MAXBYTES + 1];
3530
3531 (*mb_char2bytes)(c, buf);
3532 buf[cc] = NUL;
3533 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003534 if (compl_opt_refresh_always)
3535 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003536 }
3537 else
3538#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003539 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003540 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003541 if (compl_opt_refresh_always)
3542 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003543 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003544
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003545 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003546 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003547 ins_compl_restart();
3548
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003549 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003550 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003551 * break redo. */
3552 if (!compl_opt_refresh_always)
3553 {
3554 vim_free(compl_leader);
3555 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003556 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003557 if (compl_leader != NULL)
3558 ins_compl_new_leader();
3559 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003560}
3561
3562/*
3563 * Setup for finding completions again without leaving CTRL-X mode. Used when
3564 * BS or a key was typed while still searching for matches.
3565 */
3566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003567ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003568{
3569 ins_compl_free();
3570 compl_started = FALSE;
3571 compl_matches = 0;
3572 compl_cont_status = 0;
3573 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003574}
3575
3576/*
3577 * Set the first match, the original text.
3578 */
3579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003580ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003581{
3582 char_u *p;
3583
3584 /* Replace the original text entry. */
3585 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3586 {
3587 p = vim_strsave(str);
3588 if (p != NULL)
3589 {
3590 vim_free(compl_first_match->cp_str);
3591 compl_first_match->cp_str = p;
3592 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003593 }
3594}
3595
3596/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003597 * Append one character to the match leader. May reduce the number of
3598 * matches.
3599 */
3600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003602{
3603 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003604 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003605 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003606 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003607
3608 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003609 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003610 {
3611 /* When still at the original match use the first entry that matches
3612 * the leader. */
3613 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3614 {
3615 p = NULL;
3616 for (cp = compl_shown_match->cp_next; cp != NULL
3617 && cp != compl_first_match; cp = cp->cp_next)
3618 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003619 if (compl_leader == NULL
3620 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003621 (int)STRLEN(compl_leader)))
3622 {
3623 p = cp->cp_str;
3624 break;
3625 }
3626 }
3627 if (p == NULL || (int)STRLEN(p) <= len)
3628 return;
3629 }
3630 else
3631 return;
3632 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003633 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003634 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003635 ins_compl_addleader(c);
3636}
3637
3638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003640 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003641 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003643 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003644ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
3646 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003648 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
3650 /* Forget any previous 'special' messages if this is actually
3651 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3652 */
3653 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3654 edit_submode_extra = NULL;
3655
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003656 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003657 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3658 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003659 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003661 /* Set "compl_get_longest" when finding the first matches. */
3662 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3663 || (ctrl_x_mode == 0 && !compl_started))
3664 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003665 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003666 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003667
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003668 }
3669
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003670 compl_no_insert = FALSE;
3671 compl_no_select = FALSE;
3672 if (strstr((char *)p_cot, "noselect") != NULL)
3673 compl_no_select = TRUE;
3674 if (strstr((char *)p_cot, "noinsert") != NULL)
3675 compl_no_insert = TRUE;
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3678 {
3679 /*
3680 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3681 * it will be yet. Now we decide.
3682 */
3683 switch (c)
3684 {
3685 case Ctrl_E:
3686 case Ctrl_Y:
3687 ctrl_x_mode = CTRL_X_SCROLL;
3688 if (!(State & REPLACE_FLAG))
3689 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3690 else
3691 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3692 edit_submode_pre = NULL;
3693 showmode();
3694 break;
3695 case Ctrl_L:
3696 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3697 break;
3698 case Ctrl_F:
3699 ctrl_x_mode = CTRL_X_FILES;
3700 break;
3701 case Ctrl_K:
3702 ctrl_x_mode = CTRL_X_DICTIONARY;
3703 break;
3704 case Ctrl_R:
3705 /* Simply allow ^R to happen without affecting ^X mode */
3706 break;
3707 case Ctrl_T:
3708 ctrl_x_mode = CTRL_X_THESAURUS;
3709 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003710#ifdef FEAT_COMPL_FUNC
3711 case Ctrl_U:
3712 ctrl_x_mode = CTRL_X_FUNCTION;
3713 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003714 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003715 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003716 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003717#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003718 case 's':
3719 case Ctrl_S:
3720 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003721#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003722 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003723 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003724 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003725#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003726 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 case Ctrl_RSB:
3728 ctrl_x_mode = CTRL_X_TAGS;
3729 break;
3730#ifdef FEAT_FIND_ID
3731 case Ctrl_I:
3732 case K_S_TAB:
3733 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3734 break;
3735 case Ctrl_D:
3736 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3737 break;
3738#endif
3739 case Ctrl_V:
3740 case Ctrl_Q:
3741 ctrl_x_mode = CTRL_X_CMDLINE;
3742 break;
3743 case Ctrl_P:
3744 case Ctrl_N:
3745 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3746 * just started ^X mode, or there were enough ^X's to cancel
3747 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3748 * do normal expansion when interrupting a different mode (say
3749 * ^X^F^X^P or ^P^X^X^P, see below)
3750 * nothing changes if interrupting mode 0, (eg, the flag
3751 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003752 if (!(compl_cont_status & CONT_INTRPT))
3753 compl_cont_status |= CONT_LOCAL;
3754 else if (compl_cont_mode != 0)
3755 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 /* FALLTHROUGH */
3757 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003758 /* If we have typed at least 2 ^X's... for modes != 0, we set
3759 * compl_cont_status = 0 (eg, as if we had just started ^X
3760 * mode).
3761 * For mode 0, we set "compl_cont_mode" to an impossible
3762 * value, in both cases ^X^X can be used to restart the same
3763 * mode (avoiding ADDING mode).
3764 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3765 * 'complete' and local ^P expansions respectively.
3766 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3767 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 if (c == Ctrl_X)
3769 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003770 if (compl_cont_mode != 0)
3771 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003773 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775 ctrl_x_mode = 0;
3776 edit_submode = NULL;
3777 showmode();
3778 break;
3779 }
3780 }
3781 else if (ctrl_x_mode != 0)
3782 {
3783 /* We're already in CTRL-X mode, do we stay in it? */
3784 if (!vim_is_ctrl_x_key(c))
3785 {
3786 if (ctrl_x_mode == CTRL_X_SCROLL)
3787 ctrl_x_mode = 0;
3788 else
3789 ctrl_x_mode = CTRL_X_FINISHED;
3790 edit_submode = NULL;
3791 }
3792 showmode();
3793 }
3794
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003795 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
3797 /* Show error message from attempted keyword completion (probably
3798 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003799 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003801 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3802 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 || ctrl_x_mode == CTRL_X_FINISHED)
3804 {
3805 /* Get here when we have finished typing a sequence of ^N and
3806 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003808 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
3810 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003811 * If any of the original typed text has been changed, eg when
3812 * ignorecase is set, we must add back-spaces to the redo
3813 * buffer. We add as few as necessary to delete just the part
3814 * of the original text that has changed.
3815 * When using the longest match, edited the match or used
3816 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003818 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3819 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003820 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003821 ptr = NULL;
3822 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
3824
3825#ifdef FEAT_CINDENT
3826 want_cindent = (can_cindent && cindent_on());
3827#endif
3828 /*
3829 * When completing whole lines: fix indent for 'cindent'.
3830 * Otherwise, break line if it's too long.
3831 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003832 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 {
3834#ifdef FEAT_CINDENT
3835 /* re-indent the current line */
3836 if (want_cindent)
3837 {
3838 do_c_expr_indent();
3839 want_cindent = FALSE; /* don't do it again */
3840 }
3841#endif
3842 }
3843 else
3844 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003845 int prev_col = curwin->w_cursor.col;
3846
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003848 if (prev_col > 0)
3849 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 if (stop_arrow() == OK)
3851 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003852 if (prev_col > 0
3853 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3854 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
3856
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003857 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003858 * the selection without inserting anything. When
3859 * compl_enter_selects is set the Enter key does the same. */
3860 if ((c == Ctrl_Y || (compl_enter_selects
3861 && (c == CAR || c == K_KENTER || c == NL)))
3862 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003863 retval = TRUE;
3864
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003865 /* CTRL-E means completion is Ended, go back to the typed text. */
3866 if (c == Ctrl_E)
3867 {
3868 ins_compl_delete();
3869 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003870 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003871 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003872 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003873 retval = TRUE;
3874 }
3875
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003876 auto_format(FALSE, TRUE);
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003879 compl_started = FALSE;
3880 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003881 if (!shortmess(SHM_COMPLETIONMENU))
3882 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003884 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 if (edit_submode != NULL)
3886 {
3887 edit_submode = NULL;
3888 showmode();
3889 }
3890
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003891#ifdef FEAT_CMDWIN
3892 if (c == Ctrl_C && cmdwin_type != 0)
3893 /* Avoid the popup menu remains displayed when leaving the
3894 * command line window. */
3895 update_screen(0);
3896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897#ifdef FEAT_CINDENT
3898 /*
3899 * Indent now if a key was typed that is in 'cinkeys'.
3900 */
3901 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3902 do_c_expr_indent();
3903#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003904#ifdef FEAT_AUTOCMD
3905 /* Trigger the CompleteDone event to give scripts a chance to act
3906 * upon the completion. */
3907 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003911#ifdef FEAT_AUTOCMD
3912 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3913 /* Trigger the CompleteDone event to give scripts a chance to act
3914 * upon the (possibly failed) completion. */
3915 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
3918 /* reset continue_* if we left expansion-mode, if we stay they'll be
3919 * (re)set properly in ins_complete() */
3920 if (!vim_is_ctrl_x_key(c))
3921 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003922 compl_cont_status = 0;
3923 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003925
3926 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927}
3928
3929/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003930 * Fix the redo buffer for the completion leader replacing some of the typed
3931 * text. This inserts backspaces and appends the changed text.
3932 * "ptr" is the known leader text or NUL.
3933 */
3934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003935ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003936{
3937 int len;
3938 char_u *p;
3939 char_u *ptr = ptr_arg;
3940
3941 if (ptr == NULL)
3942 {
3943 if (compl_leader != NULL)
3944 ptr = compl_leader;
3945 else
3946 return; /* nothing to do */
3947 }
3948 if (compl_orig_text != NULL)
3949 {
3950 p = compl_orig_text;
3951 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3952 ;
3953#ifdef FEAT_MBYTE
3954 if (len > 0)
3955 len -= (*mb_head_off)(p, p + len);
3956#endif
3957 for (p += len; *p != NUL; mb_ptr_adv(p))
3958 AppendCharToRedobuff(K_BS);
3959 }
3960 else
3961 len = 0;
3962 if (ptr != NULL)
3963 AppendToRedobuffLit(ptr + len, -1);
3964}
3965
3966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3968 * (depending on flag) starting from buf and looking for a non-scanned
3969 * buffer (other than curbuf). curbuf is special, if it is called with
3970 * buf=curbuf then it has to be the first call for a given flag/expansion.
3971 *
3972 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3973 */
3974 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003975ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
3977#ifdef FEAT_WINDOWS
3978 static win_T *wp;
3979#endif
3980
3981 if (flag == 'w') /* just windows */
3982 {
3983#ifdef FEAT_WINDOWS
3984 if (buf == curbuf) /* first call for this flag/expansion */
3985 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003986 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 && wp->w_buffer->b_scanned)
3988 ;
3989 buf = wp->w_buffer;
3990#else
3991 buf = curbuf;
3992#endif
3993 }
3994 else
3995 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3996 * (unlisted buffers)
3997 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003998 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 && ((flag == 'U'
4000 ? buf->b_p_bl
4001 : (!buf->b_p_bl
4002 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004003 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 ;
4005 return buf;
4006}
4007
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004008#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004009static void expand_by_function(int type, char_u *base);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004010
4011/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004012 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004013 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004014 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004016expand_by_function(
4017 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4018 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004019{
Bram Moolenaar82139082011-09-14 16:52:09 +02004020 list_T *matchlist = NULL;
4021 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004022 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004023 char_u *funcname;
4024 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004025 win_T *curwin_save;
4026 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004027 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004028
Bram Moolenaare344bea2005-09-01 20:46:49 +00004029 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4030 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004031 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004032
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004033 /* Call 'completefunc' to obtain the list of matches. */
4034 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004035 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004036
Bram Moolenaare344bea2005-09-01 20:46:49 +00004037 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004038 curwin_save = curwin;
4039 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004040
4041 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004042 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004043 {
4044 switch (rettv.v_type)
4045 {
4046 case VAR_LIST:
4047 matchlist = rettv.vval.v_list;
4048 break;
4049 case VAR_DICT:
4050 matchdict = rettv.vval.v_dict;
4051 break;
4052 default:
4053 /* TODO: Give error message? */
4054 clear_tv(&rettv);
4055 break;
4056 }
4057 }
4058
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004059 if (curwin_save != curwin || curbuf_save != curbuf)
4060 {
4061 EMSG(_(e_complwin));
4062 goto theend;
4063 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004064 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004065 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004066 if (!equalpos(curwin->w_cursor, pos))
4067 {
4068 EMSG(_(e_compldel));
4069 goto theend;
4070 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004071
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004072 if (matchlist != NULL)
4073 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004074 else if (matchdict != NULL)
4075 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004076
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004077theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004078 if (matchdict != NULL)
4079 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004080 if (matchlist != NULL)
4081 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004082}
4083#endif /* FEAT_COMPL_FUNC */
4084
Bram Moolenaar39f05632006-03-19 22:15:26 +00004085#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004086/*
4087 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004088 */
4089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004090ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004091{
4092 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004093 int dir = compl_direction;
4094
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004095 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004096 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004097 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004098 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4099 /* if dir was BACKWARD then honor it just once */
4100 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004101 else if (did_emsg)
4102 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004103 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004104}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004105
4106/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004107 * Add completions from a dict.
4108 */
4109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004110ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004111{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004112 dictitem_T *di_refresh;
4113 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004114
4115 /* Check for optional "refresh" item. */
4116 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004117 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4118 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004119 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004120 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004121
4122 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4123 compl_opt_refresh_always = TRUE;
4124 }
4125
4126 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004127 di_words = dict_find(dict, (char_u *)"words", 5);
4128 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4129 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004130}
4131
4132/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004133 * Add a match to the list of matches from a typeval_T.
4134 * If the given string is already in the list of completions, then return
4135 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4136 * maybe because alloc() returns NULL, then FAIL is returned.
4137 */
4138 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004139ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004140{
4141 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004142 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004143 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004144 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004145 char_u *(cptext[CPT_COUNT]);
4146
4147 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4148 {
4149 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4150 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4151 (char_u *)"abbr", FALSE);
4152 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4153 (char_u *)"menu", FALSE);
4154 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4155 (char_u *)"kind", FALSE);
4156 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4157 (char_u *)"info", FALSE);
4158 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4159 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004160 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004161 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004162 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4163 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004164 }
4165 else
4166 {
4167 word = get_tv_string_chk(tv);
4168 vim_memset(cptext, 0, sizeof(cptext));
4169 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004170 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004171 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004172 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004173}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004174#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004175
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004176/*
4177 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004178 * The search starts at position "ini" in curbuf and in the direction
4179 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004180 * When "compl_started" is FALSE start at that position, otherwise continue
4181 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004182 * This may return before finding all the matches.
4183 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 */
4185 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004186ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187{
4188 static pos_T first_match_pos;
4189 static pos_T last_match_pos;
4190 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004191 static int found_all = FALSE; /* Found all matches of a
4192 certain type. */
4193 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
Bram Moolenaar572cb562005-08-05 21:35:02 +00004195 pos_T *pos;
4196 char_u **matches;
4197 int save_p_scs;
4198 int save_p_ws;
4199 int save_p_ic;
4200 int i;
4201 int num_matches;
4202 int len;
4203 int found_new_match;
4204 int type = ctrl_x_mode;
4205 char_u *ptr;
4206 char_u *dict = NULL;
4207 int dict_f = 0;
4208 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004209 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004211 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
4213 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4214 ins_buf->b_scanned = 0;
4215 found_all = FALSE;
4216 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004217 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004218 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 last_match_pos = first_match_pos = *ini;
4220 }
4221
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004222 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004223 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4225 for (;;)
4226 {
4227 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004228 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004230 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 * or if found_all says this entry is done. For ^X^L only use the
4232 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004233 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004234 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 {
4236 found_all = FALSE;
4237 while (*e_cpt == ',' || *e_cpt == ' ')
4238 e_cpt++;
4239 if (*e_cpt == '.' && !curbuf->b_scanned)
4240 {
4241 ins_buf = curbuf;
4242 first_match_pos = *ini;
4243 /* So that ^N can match word immediately after cursor */
4244 if (ctrl_x_mode == 0)
4245 dec(&first_match_pos);
4246 last_match_pos = first_match_pos;
4247 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004248
4249 /* Remember the first match so that the loop stops when we
4250 * wrap and come back there a second time. */
4251 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
4253 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4254 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4255 {
4256 /* Scan a buffer, but not the current one. */
4257 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4258 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004259 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 first_match_pos.col = last_match_pos.col = 0;
4261 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4262 last_match_pos.lnum = 0;
4263 type = 0;
4264 }
4265 else /* unloaded buffer, scan like dictionary */
4266 {
4267 found_all = TRUE;
4268 if (ins_buf->b_fname == NULL)
4269 continue;
4270 type = CTRL_X_DICTIONARY;
4271 dict = ins_buf->b_fname;
4272 dict_f = DICT_EXACT;
4273 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004274 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 ins_buf->b_fname == NULL
4276 ? buf_spname(ins_buf)
4277 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004278 ? ins_buf->b_fname
4279 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004280 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 else if (*e_cpt == NUL)
4283 break;
4284 else
4285 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004286 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 type = -1;
4288 else if (*e_cpt == 'k' || *e_cpt == 's')
4289 {
4290 if (*e_cpt == 'k')
4291 type = CTRL_X_DICTIONARY;
4292 else
4293 type = CTRL_X_THESAURUS;
4294 if (*++e_cpt != ',' && *e_cpt != NUL)
4295 {
4296 dict = e_cpt;
4297 dict_f = DICT_FIRST;
4298 }
4299 }
4300#ifdef FEAT_FIND_ID
4301 else if (*e_cpt == 'i')
4302 type = CTRL_X_PATH_PATTERNS;
4303 else if (*e_cpt == 'd')
4304 type = CTRL_X_PATH_DEFINES;
4305#endif
4306 else if (*e_cpt == ']' || *e_cpt == 't')
4307 {
4308 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004309 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004310 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
4312 else
4313 type = -1;
4314
4315 /* in any case e_cpt is advanced to the next entry */
4316 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4317
4318 found_all = TRUE;
4319 if (type == -1)
4320 continue;
4321 }
4322 }
4323
4324 switch (type)
4325 {
4326 case -1:
4327 break;
4328#ifdef FEAT_FIND_ID
4329 case CTRL_X_PATH_PATTERNS:
4330 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004331 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004332 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004334 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4336 (linenr_T)1, (linenr_T)MAXLNUM);
4337 break;
4338#endif
4339
4340 case CTRL_X_DICTIONARY:
4341 case CTRL_X_THESAURUS:
4342 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004343 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 : (type == CTRL_X_THESAURUS
4345 ? (*curbuf->b_p_tsr == NUL
4346 ? p_tsr
4347 : curbuf->b_p_tsr)
4348 : (*curbuf->b_p_dict == NUL
4349 ? p_dict
4350 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004351 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004352 dict != NULL ? dict_f
4353 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 dict = NULL;
4355 break;
4356
4357 case CTRL_X_TAGS:
4358 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4359 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004360 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004362 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004363 * of matches is found when compl_pattern is empty */
4364 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4366 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4367 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4368 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004369 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371 p_ic = save_p_ic;
4372 break;
4373
4374 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004375 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4377 {
4378
4379 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004380 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004381 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383 break;
4384
4385 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004386 if (expand_cmdline(&compl_xp, compl_pattern,
4387 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004389 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 break;
4391
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004392#ifdef FEAT_COMPL_FUNC
4393 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004394 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004395 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004396 break;
4397#endif
4398
Bram Moolenaar488c6512005-08-11 20:09:58 +00004399 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004400#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004401 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004402 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004403 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004404 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004405#endif
4406 break;
4407
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 default: /* normal ^P/^N and ^X^L */
4409 /*
4410 * If 'infercase' is set, don't use 'smartcase' here
4411 */
4412 save_p_scs = p_scs;
4413 if (ins_buf->b_p_inf)
4414 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004415
Bram Moolenaar361aa502014-01-23 22:45:58 +01004416 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 * end but never from the middle, thus setting nowrapscan in this
4418 * buffers is a good idea, on the other hand, we always set
4419 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4420 save_p_ws = p_ws;
4421 if (ins_buf != curbuf)
4422 p_ws = FALSE;
4423 else if (*e_cpt == '.')
4424 p_ws = TRUE;
4425 for (;;)
4426 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004427 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004429 ++msg_silent; /* Don't want messages for wrapscan. */
4430
Bram Moolenaare4214502015-03-05 18:08:43 +01004431 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4432 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004433 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004434 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004435 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004437 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004439 found_new_match = searchit(NULL, ins_buf, pos,
4440 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004441 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004442 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004443 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004444 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004446 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004447 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 first_match_pos = *pos;
4449 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004450 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
4452 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004453 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 found_new_match = FAIL;
4455 if (found_new_match == FAIL)
4456 {
4457 if (ins_buf == curbuf)
4458 found_all = TRUE;
4459 break;
4460 }
4461
4462 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 && ini->lnum == pos->lnum
4465 && ini->col == pos->col)
4466 continue;
4467 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004468 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004470 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
4472 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4473 continue;
4474 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4475 if (!p_paste)
4476 ptr = skipwhite(ptr);
4477 }
4478 len = (int)STRLEN(ptr);
4479 }
4480 else
4481 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004482 char_u *tmp_ptr = ptr;
4483
4484 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004486 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 /* Skip if already inside a word. */
4488 if (vim_iswordp(tmp_ptr))
4489 continue;
4490 /* Find start of next word. */
4491 tmp_ptr = find_word_start(tmp_ptr);
4492 }
4493 /* Find end of this word. */
4494 tmp_ptr = find_word_end(tmp_ptr);
4495 len = (int)(tmp_ptr - ptr);
4496
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004497 if ((compl_cont_status & CONT_ADDING)
4498 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 {
4500 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4501 {
4502 /* Try next line, if any. the new word will be
4503 * "join" as if the normal command "J" was used.
4504 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 * works -- Acevedo */
4507 STRNCPY(IObuff, ptr, len);
4508 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4509 tmp_ptr = ptr = skipwhite(ptr);
4510 /* Find start of next word. */
4511 tmp_ptr = find_word_start(tmp_ptr);
4512 /* Find end of next word. */
4513 tmp_ptr = find_word_end(tmp_ptr);
4514 if (tmp_ptr > ptr)
4515 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004516 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004518 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 IObuff[len++] = ' ';
4520 /* IObuf =~ "\k.* ", thus len >= 2 */
4521 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004522 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 || (vim_strchr(p_cpo, CPO_JOINSP)
4524 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004525 && (IObuff[len - 2] == '?'
4526 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 IObuff[len++] = ' ';
4528 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004529 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 if (tmp_ptr - ptr >= IOSIZE - len)
4531 tmp_ptr = ptr + IOSIZE - len - 1;
4532 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4533 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004534 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 }
4536 IObuff[len] = NUL;
4537 ptr = IObuff;
4538 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 continue;
4541 }
4542 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004543 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004544 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004545 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
4547 found_new_match = OK;
4548 break;
4549 }
4550 }
4551 p_scs = save_p_scs;
4552 p_ws = save_p_ws;
4553 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004554
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004556 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004557 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 found_new_match = OK;
4559
4560 /* break the loop for specialized modes (use 'complete' just for the
4561 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004562 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004563 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004564 {
4565 if (got_int)
4566 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004567 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004568 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004569 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004570
Bram Moolenaare4214502015-03-05 18:08:43 +01004571 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004572 || compl_interrupted)
4573 break;
4574 compl_started = TRUE;
4575 }
4576 else
4577 {
4578 /* Mark a buffer scanned when it has been scanned completely */
4579 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4580 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004582 compl_started = FALSE;
4583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004585 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
Bram Moolenaare4214502015-03-05 18:08:43 +01004587 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 && *e_cpt == NUL) /* Got to end of 'complete' */
4589 found_new_match = FAIL;
4590
4591 i = -1; /* total of matches, unknown */
4592 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004593 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 i = ins_compl_make_cyclic();
4595
4596 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004597 * just been made cyclic then we have to move compl_curr_match to the next
4598 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004599 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4600 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004601 if (compl_curr_match == NULL)
4602 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 return i;
4604}
4605
4606/* Delete the old text being completed. */
4607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004608ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609{
4610 int i;
4611
4612 /*
4613 * In insert mode: Delete the typed part.
4614 * In replace mode: Put the old characters back, if any.
4615 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 backspace_until_column(i);
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004618
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004619 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4620 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004622 /* clear v:completed_item */
4623 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624}
4625
4626/* Insert the new text being completed. */
4627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004628ins_compl_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004630 dict_T *dict;
4631
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004632 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004633 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4634 compl_used_match = FALSE;
4635 else
4636 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004637
4638 /* Set completed item. */
4639 /* { word, abbr, menu, kind, info } */
4640 dict = dict_alloc();
4641 if (dict != NULL)
4642 {
4643 dict_add_nr_str(dict, "word", 0L,
4644 EMPTY_IF_NULL(compl_shown_match->cp_str));
4645 dict_add_nr_str(dict, "abbr", 0L,
4646 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4647 dict_add_nr_str(dict, "menu", 0L,
4648 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4649 dict_add_nr_str(dict, "kind", 0L,
4650 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4651 dict_add_nr_str(dict, "info", 0L,
4652 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4653 }
4654 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655}
4656
4657/*
4658 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004659 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4660 * get more completions. If it is FALSE, then we just do nothing when there
4661 * are no more completions in a given direction. The latter case is used when
4662 * we are still in the middle of finding completions, to allow browsing
4663 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 * Return the total number of matches, or -1 if still unknown -- webb.
4665 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004666 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4667 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 *
4669 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004670 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4671 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 */
4673 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004674ins_compl_next(
4675 int allow_get_expansion,
4676 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004677 be at least 1 */
Bram Moolenaar7454a062016-01-30 15:14:10 +01004678 int insert_match) /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
4680 int num_matches = -1;
4681 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004682 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004683 compl_T *found_compl = NULL;
4684 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004685 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004686 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004688 /* When user complete function return -1 for findstart which is next
4689 * time of 'always', compl_shown_match become NULL. */
4690 if (compl_shown_match == NULL)
4691 return -1;
4692
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004693 if (compl_leader != NULL
4694 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004696 /* Set "compl_shown_match" to the actually shown match, it may differ
4697 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004698 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004699 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004700 && compl_shown_match->cp_next != NULL
4701 && compl_shown_match->cp_next != compl_first_match)
4702 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004703
4704 /* If we didn't find it searching forward, and compl_shows_dir is
4705 * backward, find the last match. */
4706 if (compl_shows_dir == BACKWARD
4707 && !ins_compl_equal(compl_shown_match,
4708 compl_leader, (int)STRLEN(compl_leader))
4709 && (compl_shown_match->cp_next == NULL
4710 || compl_shown_match->cp_next == compl_first_match))
4711 {
4712 while (!ins_compl_equal(compl_shown_match,
4713 compl_leader, (int)STRLEN(compl_leader))
4714 && compl_shown_match->cp_prev != NULL
4715 && compl_shown_match->cp_prev != compl_first_match)
4716 compl_shown_match = compl_shown_match->cp_prev;
4717 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004718 }
4719
4720 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004721 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 /* Delete old text to be replaced */
4723 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004724
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004725 /* When finding the longest common text we stick at the original text,
4726 * don't let CTRL-N or CTRL-P move to the first match. */
4727 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4728
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004729 /* When restarting the search don't insert the first match either. */
4730 if (compl_restarting)
4731 {
4732 advance = FALSE;
4733 compl_restarting = FALSE;
4734 }
4735
Bram Moolenaare3226be2005-12-18 22:10:00 +00004736 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4737 * around. */
4738 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004740 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004742 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004743 found_end = (compl_first_match != NULL
4744 && (compl_shown_match->cp_next == compl_first_match
4745 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004746 }
4747 else if (compl_shows_dir == BACKWARD
4748 && compl_shown_match->cp_prev != NULL)
4749 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004750 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004751 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004752 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
4754 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004755 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004756 if (!allow_get_expansion)
4757 {
4758 if (advance)
4759 {
4760 if (compl_shows_dir == BACKWARD)
4761 compl_pending -= todo + 1;
4762 else
4763 compl_pending += todo + 1;
4764 }
4765 return -1;
4766 }
4767
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004768 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004769 {
4770 if (compl_shows_dir == BACKWARD)
4771 --compl_pending;
4772 else
4773 ++compl_pending;
4774 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004775
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004776 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004777 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004778
4779 /* handle any pending completions */
4780 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004781 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004782 {
4783 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4784 {
4785 compl_shown_match = compl_shown_match->cp_next;
4786 --compl_pending;
4787 }
4788 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4789 {
4790 compl_shown_match = compl_shown_match->cp_prev;
4791 ++compl_pending;
4792 }
4793 else
4794 break;
4795 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004796 found_end = FALSE;
4797 }
4798 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4799 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004800 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004801 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004802 ++todo;
4803 else
4804 /* Remember a matching item. */
4805 found_compl = compl_shown_match;
4806
4807 /* Stop at the end of the list when we found a usable match. */
4808 if (found_end)
4809 {
4810 if (found_compl != NULL)
4811 {
4812 compl_shown_match = found_compl;
4813 break;
4814 }
4815 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
4818
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004819 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004820 if (compl_no_insert && !started)
4821 {
4822 ins_bytes(compl_orig_text + ins_compl_len());
4823 compl_used_match = FALSE;
4824 }
4825 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004826 {
4827 if (!compl_get_longest || compl_used_match)
4828 ins_compl_insert();
4829 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004830 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004831 }
4832 else
4833 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
4835 if (!allow_get_expansion)
4836 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004837 /* may undisplay the popup menu first */
4838 ins_compl_upd_pum();
4839
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004840 /* redraw to show the user what was inserted */
4841 update_screen(0);
4842
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004843 /* display the updated popup menu */
4844 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004845#ifdef FEAT_GUI
4846 if (gui.in_use)
4847 {
4848 /* Show the cursor after the match, not after the redrawn text. */
4849 setcursor();
4850 out_flush();
4851 gui_update_cursor(FALSE, FALSE);
4852 }
4853#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004854
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 /* Delete old text to be replaced, since we're still searching and
4856 * don't want to match ourselves! */
4857 ins_compl_delete();
4858 }
4859
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004860 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004861 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004862 if (compl_no_insert && !started)
4863 compl_enter_selects = TRUE;
4864 else
4865 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004866
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 /*
4868 * Show the file name for the match (if any)
4869 * Truncate the file name to avoid a wait for return.
4870 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004871 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
4873 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004874 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 if (i <= 0)
4876 i = 0;
4877 else
4878 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004879 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 msg(IObuff);
4881 redraw_cmdline = FALSE; /* don't overwrite! */
4882 }
4883
4884 return num_matches;
4885}
4886
4887/*
4888 * Call this while finding completions, to check whether the user has hit a key
4889 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004890 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004892 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
4894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004895ins_compl_check_keys(int frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
4897 static int count = 0;
4898
4899 int c;
4900
4901 /* Don't check when reading keys from a script. That would break the test
4902 * scripts */
4903 if (using_script())
4904 return;
4905
4906 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004907 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 return;
4909 count = 0;
4910
Bram Moolenaara260a972006-06-23 19:36:29 +00004911 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4912 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (c != NUL)
4915 {
4916 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4917 {
4918 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004919 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004920 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4921 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004923 else
4924 {
4925 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004926 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004927 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004928 if (c != K_IGNORE)
4929 {
4930 /* Don't interrupt completion when the character wasn't typed,
4931 * e.g., when doing @q to replay keys. */
4932 if (c != Ctrl_R && KeyTyped)
4933 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004934
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004935 vungetc(c);
4936 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004939 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00004940 {
4941 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4942
4943 compl_pending = 0;
4944 (void)ins_compl_next(FALSE, todo, TRUE);
4945 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004946}
4947
4948/*
4949 * Decide the direction of Insert mode complete from the key typed.
4950 * Returns BACKWARD or FORWARD.
4951 */
4952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004953ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004954{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004955 if (c == Ctrl_P || c == Ctrl_L
4956 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4957 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004958 return BACKWARD;
4959 return FORWARD;
4960}
4961
4962/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004963 * Return TRUE for keys that are used for completion only when the popup menu
4964 * is visible.
4965 */
4966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004967ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004968{
4969 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004970 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4971 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004972}
4973
4974/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004975 * Decide the number of completions to move forward.
4976 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4977 */
4978 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004979ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004980{
4981 int h;
4982
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004983 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004984 {
4985 h = pum_get_height();
4986 if (h > 3)
4987 h -= 2; /* keep some context */
4988 return h;
4989 }
4990 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991}
4992
4993/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004994 * Return TRUE if completion with "c" should insert the match, FALSE if only
4995 * to change the currently selected completion.
4996 */
4997 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004998ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004999{
5000 switch (c)
5001 {
5002 case K_UP:
5003 case K_DOWN:
5004 case K_PAGEDOWN:
5005 case K_KPAGEDOWN:
5006 case K_S_DOWN:
5007 case K_PAGEUP:
5008 case K_KPAGEUP:
5009 case K_S_UP:
5010 return FALSE;
5011 }
5012 return TRUE;
5013}
5014
5015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 * Do Insert mode completion.
5017 * Called when character "c" was typed, which has a meaning for completion.
5018 * Returns OK if completion was done, FAIL if something failed (out of mem).
5019 */
5020 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005021ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005023 char_u *line;
5024 int startcol = 0; /* column where searched text starts */
5025 colnr_T curs_col; /* cursor column */
5026 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005027 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
Bram Moolenaare3226be2005-12-18 22:10:00 +00005029 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005030 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
5032 /* First time we hit ^N or ^P (in a row, I mean) */
5033
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 did_ai = FALSE;
5035#ifdef FEAT_SMARTINDENT
5036 did_si = FALSE;
5037 can_si = FALSE;
5038 can_si_back = FALSE;
5039#endif
5040 if (stop_arrow() == FAIL)
5041 return FAIL;
5042
5043 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005044 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005045 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005047 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005048 * "compl_startpos" to the cursor as a pattern to add a new word
5049 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005050 * "compl_startpos" is not in the same line as the cursor then fix it
5051 * (the line has been split because it was longer than 'tw'). if SOL
5052 * is set then skip the previous pattern, a word at the beginning of
5053 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005054 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5055 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
5057 /*
5058 * it is a continued search
5059 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005060 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5062 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5063 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005064 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005066 /* line (probably) wrapped, set compl_startpos to the
5067 * first non_blank in the line, if it is not a wordchar
5068 * include it to get a better pattern, but then we don't
5069 * want the "\\<" prefix, check it bellow */
5070 compl_col = (colnr_T)(skipwhite(line) - line);
5071 compl_startpos.col = compl_col;
5072 compl_startpos.lnum = curwin->w_cursor.lnum;
5073 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075 else
5076 {
5077 /* S_IPOS was set when we inserted a word that was at the
5078 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005079 * mode but first we need to redefine compl_startpos */
5080 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005082 compl_cont_status |= CONT_SOL;
5083 compl_startpos.col = (colnr_T)(skipwhite(
5084 line + compl_length
5085 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005087 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005089 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005090 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005091 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005093 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005095 compl_cont_status &= ~CONT_SOL;
5096 compl_length = (IOSIZE - MIN_SPACE);
5097 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005099 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5100 if (compl_length < 1)
5101 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005103 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005104 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005106 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005109 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005111 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005113 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005115 compl_cont_status = 0;
5116 compl_cont_status |= CONT_N_ADDS;
5117 compl_startpos = curwin->w_cursor;
5118 startcol = (int)curs_col;
5119 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121
5122 /* Work out completion pattern and original text -- webb */
5123 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5124 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5127 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005128 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005130 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005132 compl_col += ++startcol;
5133 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005136 compl_pattern = str_foldcase(line + compl_col,
5137 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005139 compl_pattern = vim_strnsave(line + compl_col,
5140 compl_length);
5141 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 return FAIL;
5143 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005144 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
5146 char_u *prefix = (char_u *)"\\<";
5147
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005148 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005149 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005150 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005151 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005153 if (!vim_iswordp(line + compl_col)
5154 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 && (
5156#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005157 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005159 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160#endif
5161 )))
5162 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005163 STRCPY((char *)compl_pattern, prefix);
5164 (void)quote_meta(compl_pattern + STRLEN(prefix),
5165 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005169 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005171 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172#endif
5173 )
5174 {
5175 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005176 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5177 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005179 compl_col += curs_col;
5180 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182 else
5183 {
5184#ifdef FEAT_MBYTE
5185 /* Search the point of change class of multibyte character
5186 * or not a word single byte character backward. */
5187 if (has_mbyte)
5188 {
5189 int base_class;
5190 int head_off;
5191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005192 startcol -= (*mb_head_off)(line, line + startcol);
5193 base_class = mb_get_class(line + startcol);
5194 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005196 head_off = (*mb_head_off)(line, line + startcol);
5197 if (base_class != mb_get_class(line + startcol
5198 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005200 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202 }
5203 else
5204#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005205 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005207 compl_col += ++startcol;
5208 compl_length = (int)curs_col - startcol;
5209 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
5211 /* Only match word with at least two chars -- webb
5212 * there's no need to call quote_meta,
5213 * alloc(7) is enough -- Acevedo
5214 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005215 compl_pattern = alloc(7);
5216 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005218 STRCPY((char *)compl_pattern, "\\<");
5219 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5220 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
5222 else
5223 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005224 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005225 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005226 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 STRCPY((char *)compl_pattern, "\\<");
5229 (void)quote_meta(compl_pattern + 2, line + compl_col,
5230 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
5232 }
5233 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005234 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005236 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005237 compl_length = (int)curs_col - (int)compl_col;
5238 if (compl_length < 0) /* cursor in indent: empty pattern */
5239 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005241 compl_pattern = str_foldcase(line + compl_col, compl_length,
5242 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5245 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 return FAIL;
5247 }
5248 else if (ctrl_x_mode == CTRL_X_FILES)
5249 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005250 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005251 if (startcol > 0)
5252 {
5253 char_u *p = line + startcol;
5254
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005255 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005256 while (p > line && vim_isfilec(PTR2CHAR(p)))
5257 mb_ptr_back(line, p);
5258 if (p == line && vim_isfilec(PTR2CHAR(p)))
5259 startcol = 0;
5260 else
5261 startcol = (int)(p - line) + 1;
5262 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005263
Bram Moolenaar0300e462013-09-08 16:03:45 +02005264 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005265 compl_length = (int)curs_col - startcol;
5266 compl_pattern = addstar(line + compl_col, compl_length,
5267 EXPAND_FILES);
5268 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 return FAIL;
5270 }
5271 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5272 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005273 compl_pattern = vim_strnsave(line, curs_col);
5274 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005276 set_cmd_context(&compl_xp, compl_pattern,
5277 (int)STRLEN(compl_pattern), curs_col);
5278 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5279 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005280 /* No completion possible, use an empty pattern to get a
5281 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005282 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005283 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005284 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5285 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005287 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005288 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005289#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005290 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005291 * Call user defined function 'completefunc' with "a:findstart"
5292 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005293 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005294 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005295 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005296 char_u *funcname;
5297 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005298 win_T *curwin_save;
5299 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005300
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005301 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005302 * string */
5303 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5304 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5305 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005306 {
5307 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5308 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005309 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005310 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005311
5312 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005313 args[1] = NULL;
5314 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005315 curwin_save = curwin;
5316 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005317 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005318 if (curwin_save != curwin || curbuf_save != curbuf)
5319 {
5320 EMSG(_(e_complwin));
5321 return FAIL;
5322 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005323 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005324 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005325 if (!equalpos(curwin->w_cursor, pos))
5326 {
5327 EMSG(_(e_compldel));
5328 return FAIL;
5329 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005330
Bram Moolenaar6110a002012-01-26 18:58:38 +01005331 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005332 * cancel the complete without an error.
5333 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005334 if (col == -2)
5335 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005336 if (col == -3)
5337 {
5338 ctrl_x_mode = 0;
5339 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005340 if (!shortmess(SHM_COMPLETIONMENU))
5341 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005342 return FAIL;
5343 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005344
Bram Moolenaar82139082011-09-14 16:52:09 +02005345 /*
5346 * Reset extended parameters of completion, when start new
5347 * completion.
5348 */
5349 compl_opt_refresh_always = FALSE;
5350
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005351 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005352 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005353 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005354 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005355 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005356
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005357 /* Setup variables for completion. Need to obtain "line" again,
5358 * it may have become invalid. */
5359 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005360 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005361 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5362 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005363#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005364 return FAIL;
5365 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005366 else if (ctrl_x_mode == CTRL_X_SPELL)
5367 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005368#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005369 if (spell_bad_len > 0)
5370 compl_col = curs_col - spell_bad_len;
5371 else
5372 compl_col = spell_word_start(startcol);
5373 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005374 {
5375 compl_length = 0;
5376 compl_col = curs_col;
5377 }
5378 else
5379 {
5380 spell_expand_check_cap(compl_col);
5381 compl_length = (int)curs_col - compl_col;
5382 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005383 /* Need to obtain "line" again, it may have become invalid. */
5384 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005385 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5386 if (compl_pattern == NULL)
5387#endif
5388 return FAIL;
5389 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005390 else
5391 {
5392 EMSG2(_(e_intern2), "ins_complete()");
5393 return FAIL;
5394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005396 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {
5398 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005399 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {
5401 /* Insert a new line, keep indentation but ignore 'comments' */
5402#ifdef FEAT_COMMENTS
5403 char_u *old = curbuf->b_p_com;
5404
5405 curbuf->b_p_com = (char_u *)"";
5406#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005407 compl_startpos.lnum = curwin->w_cursor.lnum;
5408 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 ins_eol('\r');
5410#ifdef FEAT_COMMENTS
5411 curbuf->b_p_com = old;
5412#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005413 compl_length = 0;
5414 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 }
5416 }
5417 else
5418 {
5419 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005420 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
5422
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005423 if (compl_cont_status & CONT_LOCAL)
5424 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 else
5426 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5427
Bram Moolenaar62951b12011-09-21 18:23:05 +02005428 /* If any of the original typed text has been changed we need to fix
5429 * the redo buffer. */
5430 ins_compl_fixRedoBufForLeader(NULL);
5431
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005432 /* Always add completion for the original text. */
5433 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005434 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5435 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005436 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005438 vim_free(compl_pattern);
5439 compl_pattern = NULL;
5440 vim_free(compl_orig_text);
5441 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 return FAIL;
5443 }
5444
5445 /* showmode might reset the internal line pointers, so it must
5446 * be called before line = ml_get(), or when this address is no
5447 * longer needed. -- Acevedo.
5448 */
5449 edit_submode_extra = (char_u *)_("-- Searching...");
5450 edit_submode_highl = HLF_COUNT;
5451 showmode();
5452 edit_submode_extra = NULL;
5453 out_flush();
5454 }
5455
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005456 compl_shown_match = compl_curr_match;
5457 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458
5459 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005460 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005462 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005463 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005465 /* may undisplay the popup menu */
5466 ins_compl_upd_pum();
5467
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005468 if (n > 1) /* all matches have been found */
5469 compl_matches = n;
5470 compl_curr_match = compl_shown_match;
5471 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
Bram Moolenaard68071d2006-05-02 22:08:30 +00005473 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5474 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if (got_int && !global_busy)
5476 {
5477 (void)vgetc();
5478 got_int = FALSE;
5479 }
5480
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005481 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005482 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005484 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5485 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5487 edit_submode_highl = HLF_E;
5488 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5489 * because we couldn't expand anything at first place, but if we used
5490 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5491 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005492 if ( compl_length > 1
5493 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 || (ctrl_x_mode != 0
5495 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5496 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005497 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
5499
Bram Moolenaar572cb562005-08-05 21:35:02 +00005500 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005501 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005503 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505 if (edit_submode_extra == NULL)
5506 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005507 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 {
5509 edit_submode_extra = (char_u *)_("Back at original");
5510 edit_submode_highl = HLF_W;
5511 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005512 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 {
5514 edit_submode_extra = (char_u *)_("Word from other line");
5515 edit_submode_highl = HLF_COUNT;
5516 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005517 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 {
5519 edit_submode_extra = (char_u *)_("The only match");
5520 edit_submode_highl = HLF_COUNT;
5521 }
5522 else
5523 {
5524 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005525 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005527 int number = 0;
5528 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005530 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
5532 /* search backwards for the first valid (!= -1) number.
5533 * This should normally succeed already at the first loop
5534 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005535 for (match = compl_curr_match->cp_prev; match != NULL
5536 && match != compl_first_match;
5537 match = match->cp_prev)
5538 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005540 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 break;
5542 }
5543 if (match != NULL)
5544 /* go up and assign all numbers which are not assigned
5545 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005546 for (match = match->cp_next;
5547 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005548 match = match->cp_next)
5549 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
5551 else /* BACKWARD */
5552 {
5553 /* search forwards (upwards) for the first valid (!= -1)
5554 * number. This should normally succeed already at the
5555 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005556 for (match = compl_curr_match->cp_next; match != NULL
5557 && match != compl_first_match;
5558 match = match->cp_next)
5559 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005561 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 break;
5563 }
5564 if (match != NULL)
5565 /* go down and assign all numbers which are not
5566 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005567 for (match = match->cp_prev; match
5568 && match->cp_number == -1;
5569 match = match->cp_prev)
5570 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
5572 }
5573
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005574 /* The match should always have a sequence number now, this is
5575 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005576 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005578 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5579 * Translations may need more than twice that. */
5580 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005582 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005583 vim_snprintf((char *)match_ref, sizeof(match_ref),
5584 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005585 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005587 vim_snprintf((char *)match_ref, sizeof(match_ref),
5588 _("match %d"),
5589 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 edit_submode_extra = match_ref;
5591 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005592 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 curs_columns(FALSE);
5594 }
5595 }
5596 }
5597
5598 /* Show a message about what (completion) mode we're in. */
5599 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005600 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005602 if (edit_submode_extra != NULL)
5603 {
5604 if (!p_smd)
5605 msg_attr(edit_submode_extra,
5606 edit_submode_highl < HLF_COUNT
5607 ? hl_attr(edit_submode_highl) : 0);
5608 }
5609 else
5610 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612
Bram Moolenaard68071d2006-05-02 22:08:30 +00005613 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005614 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005615 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005616 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005617 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005618 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005619 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005620
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 return OK;
5622}
5623
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005624 static void
5625show_pum(int save_w_wrow)
5626{
5627 /* RedrawingDisabled may be set when invoked through complete(). */
5628 int n = RedrawingDisabled;
5629
5630 RedrawingDisabled = 0;
5631
5632 /* If the cursor moved we need to remove the pum first. */
5633 setcursor();
5634 if (save_w_wrow != curwin->w_wrow)
5635 ins_compl_del_pum();
5636
5637 ins_compl_show_pum();
5638 setcursor();
5639 RedrawingDisabled = n;
5640}
5641
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642/*
5643 * Looks in the first "len" chars. of "src" for search-metachars.
5644 * If dest is not NULL the chars. are copied there quoting (with
5645 * a backslash) the metachars, and dest would be NUL terminated.
5646 * Returns the length (needed) of dest
5647 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005648 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005649quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005651 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005653 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
5655 switch (*src)
5656 {
5657 case '.':
5658 case '*':
5659 case '[':
5660 if (ctrl_x_mode == CTRL_X_DICTIONARY
5661 || ctrl_x_mode == CTRL_X_THESAURUS)
5662 break;
5663 case '~':
5664 if (!p_magic) /* quote these only if magic is set */
5665 break;
5666 case '\\':
5667 if (ctrl_x_mode == CTRL_X_DICTIONARY
5668 || ctrl_x_mode == CTRL_X_THESAURUS)
5669 break;
5670 case '^': /* currently it's not needed. */
5671 case '$':
5672 m++;
5673 if (dest != NULL)
5674 *dest++ = '\\';
5675 break;
5676 }
5677 if (dest != NULL)
5678 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005679# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 /* Copy remaining bytes of a multibyte character. */
5681 if (has_mbyte)
5682 {
5683 int i, mb_len;
5684
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005685 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (mb_len > 0 && len >= mb_len)
5687 for (i = 0; i < mb_len; ++i)
5688 {
5689 --len;
5690 ++src;
5691 if (dest != NULL)
5692 *dest++ = *src;
5693 }
5694 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697 if (dest != NULL)
5698 *dest = NUL;
5699
5700 return m;
5701}
5702#endif /* FEAT_INS_EXPAND */
5703
5704/*
5705 * Next character is interpreted literally.
5706 * A one, two or three digit decimal number is interpreted as its byte value.
5707 * If one or two digits are entered, the next character is given to vungetc().
5708 * For Unicode a character > 255 may be returned.
5709 */
5710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005711get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 int cc;
5714 int nc;
5715 int i;
5716 int hex = FALSE;
5717 int octal = FALSE;
5718#ifdef FEAT_MBYTE
5719 int unicode = 0;
5720#endif
5721
5722 if (got_int)
5723 return Ctrl_C;
5724
5725#ifdef FEAT_GUI
5726 /*
5727 * In GUI there is no point inserting the internal code for a special key.
5728 * It is more useful to insert the string "<KEY>" instead. This would
5729 * probably be useful in a text window too, but it would not be
5730 * vi-compatible (maybe there should be an option for it?) -- webb
5731 */
5732 if (gui.in_use)
5733 ++allow_keys;
5734#endif
5735#ifdef USE_ON_FLY_SCROLL
5736 dont_scroll = TRUE; /* disallow scrolling here */
5737#endif
5738 ++no_mapping; /* don't map the next key hits */
5739 cc = 0;
5740 i = 0;
5741 for (;;)
5742 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005743 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744#ifdef FEAT_CMDL_INFO
5745 if (!(State & CMDLINE)
5746# ifdef FEAT_MBYTE
5747 && MB_BYTE2LEN_CHECK(nc) == 1
5748# endif
5749 )
5750 add_to_showcmd(nc);
5751#endif
5752 if (nc == 'x' || nc == 'X')
5753 hex = TRUE;
5754 else if (nc == 'o' || nc == 'O')
5755 octal = TRUE;
5756#ifdef FEAT_MBYTE
5757 else if (nc == 'u' || nc == 'U')
5758 unicode = nc;
5759#endif
5760 else
5761 {
5762 if (hex
5763#ifdef FEAT_MBYTE
5764 || unicode != 0
5765#endif
5766 )
5767 {
5768 if (!vim_isxdigit(nc))
5769 break;
5770 cc = cc * 16 + hex2nr(nc);
5771 }
5772 else if (octal)
5773 {
5774 if (nc < '0' || nc > '7')
5775 break;
5776 cc = cc * 8 + nc - '0';
5777 }
5778 else
5779 {
5780 if (!VIM_ISDIGIT(nc))
5781 break;
5782 cc = cc * 10 + nc - '0';
5783 }
5784
5785 ++i;
5786 }
5787
5788 if (cc > 255
5789#ifdef FEAT_MBYTE
5790 && unicode == 0
5791#endif
5792 )
5793 cc = 255; /* limit range to 0-255 */
5794 nc = 0;
5795
5796 if (hex) /* hex: up to two chars */
5797 {
5798 if (i >= 2)
5799 break;
5800 }
5801#ifdef FEAT_MBYTE
5802 else if (unicode) /* Unicode: up to four or eight chars */
5803 {
5804 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5805 break;
5806 }
5807#endif
5808 else if (i >= 3) /* decimal or octal: up to three chars */
5809 break;
5810 }
5811 if (i == 0) /* no number entered */
5812 {
5813 if (nc == K_ZERO) /* NUL is stored as NL */
5814 {
5815 cc = '\n';
5816 nc = 0;
5817 }
5818 else
5819 {
5820 cc = nc;
5821 nc = 0;
5822 }
5823 }
5824
5825 if (cc == 0) /* NUL is stored as NL */
5826 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005827#ifdef FEAT_MBYTE
5828 if (enc_dbcs && (cc & 0xff) == 0)
5829 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5830 second byte will cause trouble! */
5831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
5833 --no_mapping;
5834#ifdef FEAT_GUI
5835 if (gui.in_use)
5836 --allow_keys;
5837#endif
5838 if (nc)
5839 vungetc(nc);
5840 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5841 return cc;
5842}
5843
5844/*
5845 * Insert character, taking care of special keys and mod_mask
5846 */
5847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005848insert_special(
5849 int c,
5850 int allow_modmask,
5851 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852{
5853 char_u *p;
5854 int len;
5855
5856 /*
5857 * Special function key, translate into "<Key>". Up to the last '>' is
5858 * inserted with ins_str(), so as not to replace characters in replace
5859 * mode.
5860 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5861 * unless 'allow_modmask' is TRUE.
5862 */
5863#ifdef MACOS
5864 /* Command-key never produces a normal key */
5865 if (mod_mask & MOD_MASK_CMD)
5866 allow_modmask = TRUE;
5867#endif
5868 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5869 {
5870 p = get_special_key_name(c, mod_mask);
5871 len = (int)STRLEN(p);
5872 c = p[len - 1];
5873 if (len > 2)
5874 {
5875 if (stop_arrow() == FAIL)
5876 return;
5877 p[len - 1] = NUL;
5878 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005879 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 ctrlv = FALSE;
5881 }
5882 }
5883 if (stop_arrow() == OK)
5884 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5885}
5886
5887/*
5888 * Special characters in this context are those that need processing other
5889 * than the simple insertion that can be performed here. This includes ESC
5890 * which terminates the insert, and CR/NL which need special processing to
5891 * open up a new line. This routine tries to optimize insertions performed by
5892 * the "redo", "undo" or "put" commands, so it needs to know when it should
5893 * stop and defer processing to the "normal" mechanism.
5894 * '0' and '^' are special, because they can be followed by CTRL-D.
5895 */
5896#ifdef EBCDIC
5897# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5898#else
5899# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5900#endif
5901
5902#ifdef FEAT_MBYTE
5903# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5904#else
5905# define WHITECHAR(cc) vim_iswhite(cc)
5906#endif
5907
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005908/*
5909 * "flags": INSCHAR_FORMAT - force formatting
5910 * INSCHAR_CTRLV - char typed just after CTRL-V
5911 * INSCHAR_NO_FEX - don't use 'formatexpr'
5912 *
5913 * NOTE: passes the flags value straight through to internal_format() which,
5914 * beside INSCHAR_FORMAT (above), is also looking for these:
5915 * INSCHAR_DO_COM - format comments
5916 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005919insertchar(
5920 int c, /* character to insert or NUL */
5921 int flags, /* INSCHAR_FORMAT, etc. */
5922 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 int textwidth;
5925#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005929 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005931 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
5934 /*
5935 * Try to break the line in two or more pieces when:
5936 * - Always do this if we have been called to do formatting only.
5937 * - Always do this when 'formatoptions' has the 'a' flag and the line
5938 * ends in white space.
5939 * - Otherwise:
5940 * - Don't do this if inserting a blank
5941 * - Don't do this if an existing character is being replaced, unless
5942 * we're in VREPLACE mode.
5943 * - Do this if the cursor is not on the line where insert started
5944 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5945 * before the insert.
5946 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5947 * before 'textwidth'
5948 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005949 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005950 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 || (!vim_iswhite(c)
5952 && !((State & REPLACE_FLAG)
5953#ifdef FEAT_VREPLACE
5954 && !(State & VREPLACE_FLAG)
5955#endif
5956 && *ml_get_cursor() != NUL)
5957 && (curwin->w_cursor.lnum != Insstart.lnum
5958 || ((!has_format_option(FO_INS_LONG)
5959 || Insstart_textlen <= (colnr_T)textwidth)
5960 && (!fo_ins_blank
5961 || Insstart_blank_vcol <= (colnr_T)textwidth
5962 ))))))
5963 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005964 /* Format with 'formatexpr' when it's set. Use internal formatting
5965 * when 'formatexpr' isn't set or it returns non-zero. */
5966#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005967 int do_internal = TRUE;
5968 colnr_T virtcol = get_nolist_virtcol()
5969 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005970
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005971 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
5972 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005973 {
5974 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5975 /* It may be required to save for undo again, e.g. when setline()
5976 * was called. */
5977 ins_need_undo = TRUE;
5978 }
5979 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005981 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005983
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 if (c == NUL) /* only formatting was wanted */
5985 return;
5986
5987#ifdef FEAT_COMMENTS
5988 /* Check whether this character should end a comment. */
5989 if (did_ai && (int)c == end_comment_pending)
5990 {
5991 char_u *line;
5992 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5993 int middle_len, end_len;
5994 int i;
5995
5996 /*
5997 * Need to remove existing (middle) comment leader and insert end
5998 * comment leader. First, check what comment leader we can find.
5999 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006000 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6002 {
6003 /* Skip middle-comment string */
6004 while (*p && p[-1] != ':') /* find end of middle flags */
6005 ++p;
6006 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6007 /* Don't count trailing white space for middle_len */
6008 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6009 --middle_len;
6010
6011 /* Find the end-comment string */
6012 while (*p && p[-1] != ':') /* find end of end flags */
6013 ++p;
6014 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6015
6016 /* Skip white space before the cursor */
6017 i = curwin->w_cursor.col;
6018 while (--i >= 0 && vim_iswhite(line[i]))
6019 ;
6020 i++;
6021
6022 /* Skip to before the middle leader */
6023 i -= middle_len;
6024
6025 /* Check some expected things before we go on */
6026 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6027 {
6028 /* Backspace over all the stuff we want to replace */
6029 backspace_until_column(i);
6030
6031 /*
6032 * Insert the end-comment string, except for the last
6033 * character, which will get inserted as normal later.
6034 */
6035 ins_bytes_len(lead_end, end_len - 1);
6036 }
6037 }
6038 }
6039 end_comment_pending = NUL;
6040#endif
6041
6042 did_ai = FALSE;
6043#ifdef FEAT_SMARTINDENT
6044 did_si = FALSE;
6045 can_si = FALSE;
6046 can_si_back = FALSE;
6047#endif
6048
6049 /*
6050 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6051 * This speeds up normal text input considerably.
6052 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6053 * need to re-indent at a ':', or any other character (but not what
6054 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006055 * Don't do this when there an InsertCharPre autocommand is defined,
6056 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 */
6058#ifdef USE_ON_FLY_SCROLL
6059 dont_scroll = FALSE; /* allow scrolling here */
6060#endif
6061
6062 if ( !ISSPECIAL(c)
6063#ifdef FEAT_MBYTE
6064 && (!has_mbyte || (*mb_char2len)(c) == 1)
6065#endif
6066 && vpeekc() != NUL
6067 && !(State & REPLACE_FLAG)
6068#ifdef FEAT_CINDENT
6069 && !cindent_on()
6070#endif
6071#ifdef FEAT_RIGHTLEFT
6072 && !p_ri
6073#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006074#ifdef FEAT_AUTOCMD
6075 && !has_insertcharpre()
6076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 )
6078 {
6079#define INPUT_BUFLEN 100
6080 char_u buf[INPUT_BUFLEN + 1];
6081 int i;
6082 colnr_T virtcol = 0;
6083
6084 buf[0] = c;
6085 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006086 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 virtcol = get_nolist_virtcol();
6088 /*
6089 * Stop the string when:
6090 * - no more chars available
6091 * - finding a special character (command key)
6092 * - buffer is full
6093 * - running into the 'textwidth' boundary
6094 * - need to check for abbreviation: A non-word char after a word-char
6095 */
6096 while ( (c = vpeekc()) != NUL
6097 && !ISSPECIAL(c)
6098#ifdef FEAT_MBYTE
6099 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6100#endif
6101 && i < INPUT_BUFLEN
6102 && (textwidth == 0
6103 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6104 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6105 {
6106#ifdef FEAT_RIGHTLEFT
6107 c = vgetc();
6108 if (p_hkmap && KeyTyped)
6109 c = hkmap(c); /* Hebrew mode mapping */
6110# ifdef FEAT_FKMAP
6111 if (p_fkmap && KeyTyped)
6112 c = fkmap(c); /* Farsi mode mapping */
6113# endif
6114 buf[i++] = c;
6115#else
6116 buf[i++] = vgetc();
6117#endif
6118 }
6119
6120#ifdef FEAT_DIGRAPHS
6121 do_digraph(-1); /* clear digraphs */
6122 do_digraph(buf[i-1]); /* may be the start of a digraph */
6123#endif
6124 buf[i] = NUL;
6125 ins_str(buf);
6126 if (flags & INSCHAR_CTRLV)
6127 {
6128 redo_literal(*buf);
6129 i = 1;
6130 }
6131 else
6132 i = 0;
6133 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006134 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 }
6136 else
6137 {
6138#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006139 int cc;
6140
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6142 {
6143 char_u buf[MB_MAXBYTES + 1];
6144
6145 (*mb_char2bytes)(c, buf);
6146 buf[cc] = NUL;
6147 ins_char_bytes(buf, cc);
6148 AppendCharToRedobuff(c);
6149 }
6150 else
6151#endif
6152 {
6153 ins_char(c);
6154 if (flags & INSCHAR_CTRLV)
6155 redo_literal(c);
6156 else
6157 AppendCharToRedobuff(c);
6158 }
6159 }
6160}
6161
6162/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006163 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006164 *
6165 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6166 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006167 */
6168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006169internal_format(
6170 int textwidth,
6171 int second_indent,
6172 int flags,
6173 int format_only,
6174 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006175{
6176 int cc;
6177 int save_char = NUL;
6178 int haveto_redraw = FALSE;
6179 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6180#ifdef FEAT_MBYTE
6181 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6182#endif
6183 int fo_white_par = has_format_option(FO_WHITE_PAR);
6184 int first_line = TRUE;
6185#ifdef FEAT_COMMENTS
6186 colnr_T leader_len;
6187 int no_leader = FALSE;
6188 int do_comments = (flags & INSCHAR_DO_COM);
6189#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006190#ifdef FEAT_LINEBREAK
6191 int has_lbr = curwin->w_p_lbr;
6192
6193 /* make sure win_lbr_chartabsize() counts correctly */
6194 curwin->w_p_lbr = FALSE;
6195#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006196
6197 /*
6198 * When 'ai' is off we don't want a space under the cursor to be
6199 * deleted. Replace it with an 'x' temporarily.
6200 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006201 if (!curbuf->b_p_ai
6202#ifdef FEAT_VREPLACE
6203 && !(State & VREPLACE_FLAG)
6204#endif
6205 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206 {
6207 cc = gchar_cursor();
6208 if (vim_iswhite(cc))
6209 {
6210 save_char = cc;
6211 pchar_cursor('x');
6212 }
6213 }
6214
6215 /*
6216 * Repeat breaking lines, until the current line is not too long.
6217 */
6218 while (!got_int)
6219 {
6220 int startcol; /* Cursor column at entry */
6221 int wantcol; /* column at textwidth border */
6222 int foundcol; /* column for start of spaces */
6223 int end_foundcol = 0; /* column for start of word */
6224 colnr_T len;
6225 colnr_T virtcol;
6226#ifdef FEAT_VREPLACE
6227 int orig_col = 0;
6228 char_u *saved_text = NULL;
6229#endif
6230 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006231 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232
Bram Moolenaar97b98102009-11-17 16:41:01 +00006233 virtcol = get_nolist_virtcol()
6234 + char2cells(c != NUL ? c : gchar_cursor());
6235 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006236 break;
6237
6238#ifdef FEAT_COMMENTS
6239 if (no_leader)
6240 do_comments = FALSE;
6241 else if (!(flags & INSCHAR_FORMAT)
6242 && has_format_option(FO_WRAP_COMS))
6243 do_comments = TRUE;
6244
6245 /* Don't break until after the comment leader */
6246 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006247 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006248 else
6249 leader_len = 0;
6250
6251 /* If the line doesn't start with a comment leader, then don't
6252 * start one in a following broken line. Avoids that a %word
6253 * moved to the start of the next line causes all following lines
6254 * to start with %. */
6255 if (leader_len == 0)
6256 no_leader = TRUE;
6257#endif
6258 if (!(flags & INSCHAR_FORMAT)
6259#ifdef FEAT_COMMENTS
6260 && leader_len == 0
6261#endif
6262 && !has_format_option(FO_WRAP))
6263
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006264 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006265 if ((startcol = curwin->w_cursor.col) == 0)
6266 break;
6267
6268 /* find column of textwidth border */
6269 coladvance((colnr_T)textwidth);
6270 wantcol = curwin->w_cursor.col;
6271
Bram Moolenaar97b98102009-11-17 16:41:01 +00006272 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006273 foundcol = 0;
6274
6275 /*
6276 * Find position to break at.
6277 * Stop at first entered white when 'formatoptions' has 'v'
6278 */
6279 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006280 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006281 || curwin->w_cursor.lnum != Insstart.lnum
6282 || curwin->w_cursor.col >= Insstart.col)
6283 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006284 if (curwin->w_cursor.col == startcol && c != NUL)
6285 cc = c;
6286 else
6287 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006288 if (WHITECHAR(cc))
6289 {
6290 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006291 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006292
6293 /* find start of sequence of blanks */
6294 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6295 {
6296 dec_cursor();
6297 cc = gchar_cursor();
6298 }
6299 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6300 break; /* only spaces in front of text */
6301#ifdef FEAT_COMMENTS
6302 /* Don't break until after the comment leader */
6303 if (curwin->w_cursor.col < leader_len)
6304 break;
6305#endif
6306 if (has_format_option(FO_ONE_LETTER))
6307 {
6308 /* do not break after one-letter words */
6309 if (curwin->w_cursor.col == 0)
6310 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006311#ifdef FEAT_COMMENTS
6312 /* do not break "#a b" when 'tw' is 2 */
6313 if (curwin->w_cursor.col <= leader_len)
6314 break;
6315#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006316 col = curwin->w_cursor.col;
6317 dec_cursor();
6318 cc = gchar_cursor();
6319
6320 if (WHITECHAR(cc))
6321 continue; /* one-letter, continue */
6322 curwin->w_cursor.col = col;
6323 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006324
6325 inc_cursor();
6326
6327 end_foundcol = end_col + 1;
6328 foundcol = curwin->w_cursor.col;
6329 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006330 break;
6331 }
6332#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006333 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006334 {
6335 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006336 if (curwin->w_cursor.col != startcol)
6337 {
6338#ifdef FEAT_COMMENTS
6339 /* Don't break until after the comment leader */
6340 if (curwin->w_cursor.col < leader_len)
6341 break;
6342#endif
6343 col = curwin->w_cursor.col;
6344 inc_cursor();
6345 /* Don't change end_foundcol if already set. */
6346 if (foundcol != curwin->w_cursor.col)
6347 {
6348 foundcol = curwin->w_cursor.col;
6349 end_foundcol = foundcol;
6350 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6351 break;
6352 }
6353 curwin->w_cursor.col = col;
6354 }
6355
6356 if (curwin->w_cursor.col == 0)
6357 break;
6358
6359 col = curwin->w_cursor.col;
6360
6361 dec_cursor();
6362 cc = gchar_cursor();
6363
6364 if (WHITECHAR(cc))
6365 continue; /* break with space */
6366#ifdef FEAT_COMMENTS
6367 /* Don't break until after the comment leader */
6368 if (curwin->w_cursor.col < leader_len)
6369 break;
6370#endif
6371
6372 curwin->w_cursor.col = col;
6373
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006374 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006375 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006376 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6377 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006378 }
6379#endif
6380 if (curwin->w_cursor.col == 0)
6381 break;
6382 dec_cursor();
6383 }
6384
6385 if (foundcol == 0) /* no spaces, cannot break line */
6386 {
6387 curwin->w_cursor.col = startcol;
6388 break;
6389 }
6390
6391 /* Going to break the line, remove any "$" now. */
6392 undisplay_dollar();
6393
6394 /*
6395 * Offset between cursor position and line break is used by replace
6396 * stack functions. VREPLACE does not use this, and backspaces
6397 * over the text instead.
6398 */
6399#ifdef FEAT_VREPLACE
6400 if (State & VREPLACE_FLAG)
6401 orig_col = startcol; /* Will start backspacing from here */
6402 else
6403#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006404 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006405
6406 /*
6407 * adjust startcol for spaces that will be deleted and
6408 * characters that will remain on top line
6409 */
6410 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006411 while ((cc = gchar_cursor(), WHITECHAR(cc))
6412 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006413 inc_cursor();
6414 startcol -= curwin->w_cursor.col;
6415 if (startcol < 0)
6416 startcol = 0;
6417
6418#ifdef FEAT_VREPLACE
6419 if (State & VREPLACE_FLAG)
6420 {
6421 /*
6422 * In VREPLACE mode, we will backspace over the text to be
6423 * wrapped, so save a copy now to put on the next line.
6424 */
6425 saved_text = vim_strsave(ml_get_cursor());
6426 curwin->w_cursor.col = orig_col;
6427 if (saved_text == NULL)
6428 break; /* Can't do it, out of memory */
6429 saved_text[startcol] = NUL;
6430
6431 /* Backspace over characters that will move to the next line */
6432 if (!fo_white_par)
6433 backspace_until_column(foundcol);
6434 }
6435 else
6436#endif
6437 {
6438 /* put cursor after pos. to break line */
6439 if (!fo_white_par)
6440 curwin->w_cursor.col = foundcol;
6441 }
6442
6443 /*
6444 * Split the line just before the margin.
6445 * Only insert/delete lines, but don't really redraw the window.
6446 */
6447 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6448 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6449#ifdef FEAT_COMMENTS
6450 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006451 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006452#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006453 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6454 if (!(flags & INSCHAR_COM_LIST))
6455 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006456
6457 replace_offset = 0;
6458 if (first_line)
6459 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006460 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006461 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006462 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006463 * This section is for auto-wrap of numeric lists. When not
6464 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6465 * flag will be set and open_line() will handle it (as seen
6466 * above). The code here (and in get_number_indent()) will
6467 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006468 */
6469 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006470 second_indent =
6471 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006472 if (second_indent >= 0)
6473 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006474#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006475 if (State & VREPLACE_FLAG)
6476 change_indent(INDENT_SET, second_indent,
6477 FALSE, NUL, TRUE);
6478 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006479#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006480#ifdef FEAT_COMMENTS
6481 if (leader_len > 0 && second_indent - leader_len > 0)
6482 {
6483 int i;
6484 int padding = second_indent - leader_len;
6485
6486 /* We started at the first_line of a numbered list
6487 * that has a comment. the open_line() function has
6488 * inserted the proper comment leader and positioned
6489 * the cursor at the end of the split line. Now we
6490 * add the additional whitespace needed after the
6491 * comment leader for the numbered list. */
6492 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006493 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006494 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006495 }
6496 else
6497 {
6498#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006499 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006500#ifdef FEAT_COMMENTS
6501 }
6502#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006503 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006504 }
6505 first_line = FALSE;
6506 }
6507
6508#ifdef FEAT_VREPLACE
6509 if (State & VREPLACE_FLAG)
6510 {
6511 /*
6512 * In VREPLACE mode we have backspaced over the text to be
6513 * moved, now we re-insert it into the new line.
6514 */
6515 ins_bytes(saved_text);
6516 vim_free(saved_text);
6517 }
6518 else
6519#endif
6520 {
6521 /*
6522 * Check if cursor is not past the NUL off the line, cindent
6523 * may have added or removed indent.
6524 */
6525 curwin->w_cursor.col += startcol;
6526 len = (colnr_T)STRLEN(ml_get_curline());
6527 if (curwin->w_cursor.col > len)
6528 curwin->w_cursor.col = len;
6529 }
6530
6531 haveto_redraw = TRUE;
6532#ifdef FEAT_CINDENT
6533 can_cindent = TRUE;
6534#endif
6535 /* moved the cursor, don't autoindent or cindent now */
6536 did_ai = FALSE;
6537#ifdef FEAT_SMARTINDENT
6538 did_si = FALSE;
6539 can_si = FALSE;
6540 can_si_back = FALSE;
6541#endif
6542 line_breakcheck();
6543 }
6544
6545 if (save_char != NUL) /* put back space after cursor */
6546 pchar_cursor(save_char);
6547
Bram Moolenaar0026d472014-09-09 16:32:39 +02006548#ifdef FEAT_LINEBREAK
6549 curwin->w_p_lbr = has_lbr;
6550#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006551 if (!format_only && haveto_redraw)
6552 {
6553 update_topline();
6554 redraw_curbuf_later(VALID);
6555 }
6556}
6557
6558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 * Called after inserting or deleting text: When 'formatoptions' includes the
6560 * 'a' flag format from the current line until the end of the paragraph.
6561 * Keep the cursor at the same position relative to the text.
6562 * The caller must have saved the cursor line for undo, following ones will be
6563 * saved here.
6564 */
6565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006566auto_format(
6567 int trailblank, /* when TRUE also format with trailing blank */
6568 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
6570 pos_T pos;
6571 colnr_T len;
6572 char_u *old;
6573 char_u *new, *pnew;
6574 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006575 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577 if (!has_format_option(FO_AUTO))
6578 return;
6579
6580 pos = curwin->w_cursor;
6581 old = ml_get_curline();
6582
6583 /* may remove added space */
6584 check_auto_format(FALSE);
6585
6586 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6587 * user might insert normal text next. Also skip formatting when "1" is
6588 * in 'formatoptions' and there is a single character before the cursor.
6589 * Otherwise the line would be broken and when typing another non-white
6590 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006591 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 if (*old != NUL && !trailblank && wasatend)
6593 {
6594 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006595 cc = gchar_cursor();
6596 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6597 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006599 cc = gchar_cursor();
6600 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 {
6602 curwin->w_cursor = pos;
6603 return;
6604 }
6605 curwin->w_cursor = pos;
6606 }
6607
6608#ifdef FEAT_COMMENTS
6609 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6610 * comments. */
6611 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006612 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 return;
6614#endif
6615
6616 /*
6617 * May start formatting in a previous line, so that after "x" a word is
6618 * moved to the previous line if it fits there now. Only when this is not
6619 * the start of a paragraph.
6620 */
6621 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6622 {
6623 --curwin->w_cursor.lnum;
6624 if (u_save_cursor() == FAIL)
6625 return;
6626 }
6627
6628 /*
6629 * Do the formatting and restore the cursor position. "saved_cursor" will
6630 * be adjusted for the text formatting.
6631 */
6632 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006633 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 curwin->w_cursor = saved_cursor;
6635 saved_cursor.lnum = 0;
6636
6637 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6638 {
6639 /* "cannot happen" */
6640 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6641 coladvance((colnr_T)MAXCOL);
6642 }
6643 else
6644 check_cursor_col();
6645
6646 /* Insert mode: If the cursor is now after the end of the line while it
6647 * previously wasn't, the line was broken. Because of the rule above we
6648 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6649 * formatted. */
6650 if (!wasatend && has_format_option(FO_WHITE_PAR))
6651 {
6652 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006653 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 if (curwin->w_cursor.col == len)
6655 {
6656 pnew = vim_strnsave(new, len + 2);
6657 pnew[len] = ' ';
6658 pnew[len + 1] = NUL;
6659 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6660 /* remove the space later */
6661 did_add_space = TRUE;
6662 }
6663 else
6664 /* may remove added space */
6665 check_auto_format(FALSE);
6666 }
6667
6668 check_cursor();
6669}
6670
6671/*
6672 * When an extra space was added to continue a paragraph for auto-formatting,
6673 * delete it now. The space must be under the cursor, just after the insert
6674 * position.
6675 */
6676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006677check_auto_format(
6678 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679{
6680 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006681 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
6683 if (did_add_space)
6684 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006685 cc = gchar_cursor();
6686 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 /* Somehow the space was removed already. */
6688 did_add_space = FALSE;
6689 else
6690 {
6691 if (!end_insert)
6692 {
6693 inc_cursor();
6694 c = gchar_cursor();
6695 dec_cursor();
6696 }
6697 if (c != NUL)
6698 {
6699 /* The space is no longer at the end of the line, delete it. */
6700 del_char(FALSE);
6701 did_add_space = FALSE;
6702 }
6703 }
6704 }
6705}
6706
6707/*
6708 * Find out textwidth to be used for formatting:
6709 * if 'textwidth' option is set, use it
6710 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6711 * if invalid value, use 0.
6712 * Set default to window width (maximum 79) for "gq" operator.
6713 */
6714 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006715comp_textwidth(
6716 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717{
6718 int textwidth;
6719
6720 textwidth = curbuf->b_p_tw;
6721 if (textwidth == 0 && curbuf->b_p_wm)
6722 {
6723 /* The width is the window width minus 'wrapmargin' minus all the
6724 * things that add to the margin. */
6725 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6726#ifdef FEAT_CMDWIN
6727 if (cmdwin_type != 0)
6728 textwidth -= 1;
6729#endif
6730#ifdef FEAT_FOLDING
6731 textwidth -= curwin->w_p_fdc;
6732#endif
6733#ifdef FEAT_SIGNS
6734 if (curwin->w_buffer->b_signlist != NULL
6735# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01006736 || curwin->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737# endif
6738 )
6739 textwidth -= 1;
6740#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006741 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 textwidth -= 8;
6743 }
6744 if (textwidth < 0)
6745 textwidth = 0;
6746 if (ff && textwidth == 0)
6747 {
6748 textwidth = W_WIDTH(curwin) - 1;
6749 if (textwidth > 79)
6750 textwidth = 79;
6751 }
6752 return textwidth;
6753}
6754
6755/*
6756 * Put a character in the redo buffer, for when just after a CTRL-V.
6757 */
6758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006759redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
6761 char_u buf[10];
6762
6763 /* Only digits need special treatment. Translate them into a string of
6764 * three digits. */
6765 if (VIM_ISDIGIT(c))
6766 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006767 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 AppendToRedobuff(buf);
6769 }
6770 else
6771 AppendCharToRedobuff(c);
6772}
6773
6774/*
6775 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006776 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 */
6778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006779start_arrow(
6780 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006782 start_arrow_common(end_insert_pos, TRUE);
6783}
6784
6785/*
6786 * Like start_arrow() but with end_change argument.
6787 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6788 */
6789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006790start_arrow_with_change(
6791 pos_T *end_insert_pos, /* can be NULL */
6792 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006793{
6794 start_arrow_common(end_insert_pos, end_change);
6795 if (!end_change)
6796 {
6797 AppendCharToRedobuff(Ctrl_G);
6798 AppendCharToRedobuff('U');
6799 }
6800}
6801
6802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006803start_arrow_common(
6804 pos_T *end_insert_pos, /* can be NULL */
6805 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006806{
6807 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {
6809 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006810 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 arrow_used = TRUE; /* this means we stopped the current insert */
6812 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006813#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006814 check_spell_redraw();
6815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816}
6817
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006818#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006819/*
6820 * If we skipped highlighting word at cursor, do it now.
6821 * It may be skipped again, thus reset spell_redraw_lnum first.
6822 */
6823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006824check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006825{
6826 if (spell_redraw_lnum != 0)
6827 {
6828 linenr_T lnum = spell_redraw_lnum;
6829
6830 spell_redraw_lnum = 0;
6831 redrawWinline(lnum, FALSE);
6832 }
6833}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006834
6835/*
6836 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6837 * spelled word, if there is one.
6838 */
6839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006840spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006841{
6842 pos_T tpos = curwin->w_cursor;
6843
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006844 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006845 if (curwin->w_cursor.col != tpos.col)
6846 start_arrow(&tpos);
6847}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006848#endif
6849
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850/*
6851 * stop_arrow() is called before a change is made in insert mode.
6852 * If an arrow key has been used, start a new insertion.
6853 * Returns FAIL if undo is impossible, shouldn't insert then.
6854 */
6855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006856stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 if (arrow_used)
6859 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006860 Insstart = curwin->w_cursor; /* new insertion starts here */
6861 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6862 /* Don't update the original insert position when moved to the
6863 * right, except when nothing was inserted yet. */
6864 update_Insstart_orig = FALSE;
6865 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6866
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (u_save_cursor() == OK)
6868 {
6869 arrow_used = FALSE;
6870 ins_need_undo = FALSE;
6871 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006872
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 ai_col = 0;
6874#ifdef FEAT_VREPLACE
6875 if (State & VREPLACE_FLAG)
6876 {
6877 orig_line_count = curbuf->b_ml.ml_line_count;
6878 vr_lines_changed = 1;
6879 }
6880#endif
6881 ResetRedobuff();
6882 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006883 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 }
6885 else if (ins_need_undo)
6886 {
6887 if (u_save_cursor() == OK)
6888 ins_need_undo = FALSE;
6889 }
6890
6891#ifdef FEAT_FOLDING
6892 /* Always open fold at the cursor line when inserting something. */
6893 foldOpenCursor();
6894#endif
6895
6896 return (arrow_used || ins_need_undo ? FAIL : OK);
6897}
6898
6899/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006900 * Do a few things to stop inserting.
6901 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6902 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 */
6904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006905stop_insert(
6906 pos_T *end_insert_pos,
6907 int esc, /* called by ins_esc() */
6908 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006910 int cc;
6911 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
6913 stop_redo_ins();
6914 replace_flush(); /* abandon replace stack */
6915
6916 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006917 * Save the inserted text for later redo with ^@ and CTRL-A.
6918 * Don't do it when "restart_edit" was set and nothing was inserted,
6919 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006921 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006922 if (did_restart_edit == 0 || (ptr != NULL
6923 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006924 {
6925 vim_free(last_insert);
6926 last_insert = ptr;
6927 last_insert_skip = new_insert_skip;
6928 }
6929 else
6930 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006932 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
6934 /* Auto-format now. It may seem strange to do this when stopping an
6935 * insertion (or moving the cursor), but it's required when appending
6936 * a line and having it end in a space. But only do it when something
6937 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006938 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006940 pos_T tpos = curwin->w_cursor;
6941
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 /* When the cursor is at the end of the line after a space the
6943 * formatting will move it to the following word. Avoid that by
6944 * moving the cursor onto the space. */
6945 cc = 'x';
6946 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6947 {
6948 dec_cursor();
6949 cc = gchar_cursor();
6950 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006951 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 }
6953
6954 auto_format(TRUE, FALSE);
6955
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006956 if (vim_iswhite(cc))
6957 {
6958 if (gchar_cursor() != NUL)
6959 inc_cursor();
6960#ifdef FEAT_VIRTUALEDIT
6961 /* If the cursor is still at the same character, also keep
6962 * the "coladd". */
6963 if (gchar_cursor() == NUL
6964 && curwin->w_cursor.lnum == tpos.lnum
6965 && curwin->w_cursor.col == tpos.col)
6966 curwin->w_cursor.coladd = tpos.coladd;
6967#endif
6968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 }
6970
6971 /* If a space was inserted for auto-formatting, remove it now. */
6972 check_auto_format(TRUE);
6973
6974 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006975 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006976 * Do this when ESC was used or moving the cursor up/down.
6977 * Check for the old position still being valid, just in case the text
6978 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006979 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006980 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6981 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006983 pos_T tpos = curwin->w_cursor;
6984
6985 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006986 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006987 for (;;)
6988 {
6989 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6990 --curwin->w_cursor.col;
6991 cc = gchar_cursor();
6992 if (!vim_iswhite(cc))
6993 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006994 if (del_char(TRUE) == FAIL)
6995 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006996 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006997 if (curwin->w_cursor.lnum != tpos.lnum)
6998 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01006999 else
7000 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007001 /* reset tpos, could have been invalidated in the loop above */
7002 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007003 tpos.col++;
7004 if (cc != NUL && gchar_pos(&tpos) == NUL)
7005 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 /* <C-S-Right> may have started Visual mode, adjust the position for
7009 * deleted characters. */
7010 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7011 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007012 int len = (int)STRLEN(ml_get_curline());
7013
7014 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007016 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007017#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 }
7021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 }
7023 }
7024 did_ai = FALSE;
7025#ifdef FEAT_SMARTINDENT
7026 did_si = FALSE;
7027 can_si = FALSE;
7028 can_si_back = FALSE;
7029#endif
7030
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007031 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7032 * now in a different buffer. */
7033 if (end_insert_pos != NULL)
7034 {
7035 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007036 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007037 curbuf->b_op_end = *end_insert_pos;
7038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039}
7040
7041/*
7042 * Set the last inserted text to a single character.
7043 * Used for the replace command.
7044 */
7045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007046set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
7048 char_u *s;
7049
7050 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 if (last_insert != NULL)
7053 {
7054 s = last_insert;
7055 /* Use the CTRL-V only when entering a special char */
7056 if (c < ' ' || c == DEL)
7057 *s++ = Ctrl_V;
7058 s = add_char2buf(c, s);
7059 *s++ = ESC;
7060 *s++ = NUL;
7061 last_insert_skip = 0;
7062 }
7063}
7064
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007065#if defined(EXITFREE) || defined(PROTO)
7066 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007067free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007068{
7069 vim_free(last_insert);
7070 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007071# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007072 vim_free(compl_orig_text);
7073 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007074# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007075}
7076#endif
7077
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078/*
7079 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7080 * and CSI. Handle multi-byte characters.
7081 * Returns a pointer to after the added bytes.
7082 */
7083 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007084add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085{
7086#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007087 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 int i;
7089 int len;
7090
7091 len = (*mb_char2bytes)(c, temp);
7092 for (i = 0; i < len; ++i)
7093 {
7094 c = temp[i];
7095#endif
7096 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7097 if (c == K_SPECIAL)
7098 {
7099 *s++ = K_SPECIAL;
7100 *s++ = KS_SPECIAL;
7101 *s++ = KE_FILLER;
7102 }
7103#ifdef FEAT_GUI
7104 else if (c == CSI)
7105 {
7106 *s++ = CSI;
7107 *s++ = KS_EXTRA;
7108 *s++ = (int)KE_CSI;
7109 }
7110#endif
7111 else
7112 *s++ = c;
7113#ifdef FEAT_MBYTE
7114 }
7115#endif
7116 return s;
7117}
7118
7119/*
7120 * move cursor to start of line
7121 * if flags & BL_WHITE move to first non-white
7122 * if flags & BL_SOL move to first non-white if startofline is set,
7123 * otherwise keep "curswant" column
7124 * if flags & BL_FIX don't leave the cursor on a NUL.
7125 */
7126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007127beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 if ((flags & BL_SOL) && !p_sol)
7130 coladvance(curwin->w_curswant);
7131 else
7132 {
7133 curwin->w_cursor.col = 0;
7134#ifdef FEAT_VIRTUALEDIT
7135 curwin->w_cursor.coladd = 0;
7136#endif
7137
7138 if (flags & (BL_WHITE | BL_SOL))
7139 {
7140 char_u *ptr;
7141
7142 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7143 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7144 ++curwin->w_cursor.col;
7145 }
7146 curwin->w_set_curswant = TRUE;
7147 }
7148}
7149
7150/*
7151 * oneright oneleft cursor_down cursor_up
7152 *
7153 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007154 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 * Return OK when successful, FAIL when we hit a line of file boundary.
7156 */
7157
7158 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007159oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160{
7161 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
7164#ifdef FEAT_VIRTUALEDIT
7165 if (virtual_active())
7166 {
7167 pos_T prevpos = curwin->w_cursor;
7168
7169 /* Adjust for multi-wide char (excluding TAB) */
7170 ptr = ml_get_cursor();
7171 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007172# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007174# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 ))
7178 ? ptr2cells(ptr) : 1));
7179 curwin->w_set_curswant = TRUE;
7180 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7181 return (prevpos.col != curwin->w_cursor.col
7182 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7183 }
7184#endif
7185
7186 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007187 if (*ptr == NUL)
7188 return FAIL; /* already at the very end */
7189
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007191 if (has_mbyte)
7192 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 else
7194#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007195 l = 1;
7196
7197 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7198 * contains "onemore". */
7199 if (ptr[l] == NUL
7200#ifdef FEAT_VIRTUALEDIT
7201 && (ve_flags & VE_ONEMORE) == 0
7202#endif
7203 )
7204 return FAIL;
7205 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
7207 curwin->w_set_curswant = TRUE;
7208 return OK;
7209}
7210
7211 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007212oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213{
7214#ifdef FEAT_VIRTUALEDIT
7215 if (virtual_active())
7216 {
7217 int width;
7218 int v = getviscol();
7219
7220 if (v == 0)
7221 return FAIL;
7222
7223# ifdef FEAT_LINEBREAK
7224 /* We might get stuck on 'showbreak', skip over it. */
7225 width = 1;
7226 for (;;)
7227 {
7228 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007229 /* getviscol() is slow, skip it when 'showbreak' is empty,
7230 * 'breakindent' is not set and there are no multi-byte
7231 * characters */
7232 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233# ifdef FEAT_MBYTE
7234 && !has_mbyte
7235# endif
7236 ) || getviscol() < v)
7237 break;
7238 ++width;
7239 }
7240# else
7241 coladvance(v - 1);
7242# endif
7243
7244 if (curwin->w_cursor.coladd == 1)
7245 {
7246 char_u *ptr;
7247
7248 /* Adjust for multi-wide char (not a TAB) */
7249 ptr = ml_get_cursor();
7250 if (*ptr != TAB && vim_isprintc(
7251# ifdef FEAT_MBYTE
7252 (*mb_ptr2char)(ptr)
7253# else
7254 *ptr
7255# endif
7256 ) && ptr2cells(ptr) > 1)
7257 curwin->w_cursor.coladd = 0;
7258 }
7259
7260 curwin->w_set_curswant = TRUE;
7261 return OK;
7262 }
7263#endif
7264
7265 if (curwin->w_cursor.col == 0)
7266 return FAIL;
7267
7268 curwin->w_set_curswant = TRUE;
7269 --curwin->w_cursor.col;
7270
7271#ifdef FEAT_MBYTE
7272 /* if the character on the left of the current cursor is a multi-byte
7273 * character, move to its first byte */
7274 if (has_mbyte)
7275 mb_adjust_cursor();
7276#endif
7277 return OK;
7278}
7279
7280 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007281cursor_up(
7282 long n,
7283 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
7285 linenr_T lnum;
7286
7287 if (n > 0)
7288 {
7289 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007290 /* This fails if the cursor is already in the first line or the count
7291 * is larger than the line number and '-' is in 'cpoptions' */
7292 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 return FAIL;
7294 if (n >= lnum)
7295 lnum = 1;
7296 else
7297#ifdef FEAT_FOLDING
7298 if (hasAnyFolding(curwin))
7299 {
7300 /*
7301 * Count each sequence of folded lines as one logical line.
7302 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007303 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 (void)hasFolding(lnum, &lnum, NULL);
7305
7306 while (n--)
7307 {
7308 /* move up one line */
7309 --lnum;
7310 if (lnum <= 1)
7311 break;
7312 /* If we entered a fold, move to the beginning, unless in
7313 * Insert mode or when 'foldopen' contains "all": it will open
7314 * in a moment. */
7315 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7316 (void)hasFolding(lnum, &lnum, NULL);
7317 }
7318 if (lnum < 1)
7319 lnum = 1;
7320 }
7321 else
7322#endif
7323 lnum -= n;
7324 curwin->w_cursor.lnum = lnum;
7325 }
7326
7327 /* try to advance to the column we want to be at */
7328 coladvance(curwin->w_curswant);
7329
7330 if (upd_topline)
7331 update_topline(); /* make sure curwin->w_topline is valid */
7332
7333 return OK;
7334}
7335
7336/*
7337 * Cursor down a number of logical lines.
7338 */
7339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007340cursor_down(
7341 long n,
7342 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
7344 linenr_T lnum;
7345
7346 if (n > 0)
7347 {
7348 lnum = curwin->w_cursor.lnum;
7349#ifdef FEAT_FOLDING
7350 /* Move to last line of fold, will fail if it's the end-of-file. */
7351 (void)hasFolding(lnum, NULL, &lnum);
7352#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007353 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007354 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007355 if (lnum >= curbuf->b_ml.ml_line_count
7356 || (lnum + n > curbuf->b_ml.ml_line_count
7357 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 return FAIL;
7359 if (lnum + n >= curbuf->b_ml.ml_line_count)
7360 lnum = curbuf->b_ml.ml_line_count;
7361 else
7362#ifdef FEAT_FOLDING
7363 if (hasAnyFolding(curwin))
7364 {
7365 linenr_T last;
7366
7367 /* count each sequence of folded lines as one logical line */
7368 while (n--)
7369 {
7370 if (hasFolding(lnum, NULL, &last))
7371 lnum = last + 1;
7372 else
7373 ++lnum;
7374 if (lnum >= curbuf->b_ml.ml_line_count)
7375 break;
7376 }
7377 if (lnum > curbuf->b_ml.ml_line_count)
7378 lnum = curbuf->b_ml.ml_line_count;
7379 }
7380 else
7381#endif
7382 lnum += n;
7383 curwin->w_cursor.lnum = lnum;
7384 }
7385
7386 /* try to advance to the column we want to be at */
7387 coladvance(curwin->w_curswant);
7388
7389 if (upd_topline)
7390 update_topline(); /* make sure curwin->w_topline is valid */
7391
7392 return OK;
7393}
7394
7395/*
7396 * Stuff the last inserted text in the read buffer.
7397 * Last_insert actually is a copy of the redo buffer, so we
7398 * first have to remove the command.
7399 */
7400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007401stuff_inserted(
7402 int c, /* Command character to be inserted */
7403 long count, /* Repeat this many times */
7404 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405{
7406 char_u *esc_ptr;
7407 char_u *ptr;
7408 char_u *last_ptr;
7409 char_u last = NUL;
7410
7411 ptr = get_last_insert();
7412 if (ptr == NULL)
7413 {
7414 EMSG(_(e_noinstext));
7415 return FAIL;
7416 }
7417
7418 /* may want to stuff the command character, to start Insert mode */
7419 if (c != NUL)
7420 stuffcharReadbuff(c);
7421 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7422 *esc_ptr = NUL; /* remove the ESC */
7423
7424 /* when the last char is either "0" or "^" it will be quoted if no ESC
7425 * comes after it OR if it will inserted more than once and "ptr"
7426 * starts with ^D. -- Acevedo
7427 */
7428 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7429 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7430 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7431 {
7432 last = *last_ptr;
7433 *last_ptr = NUL;
7434 }
7435
7436 do
7437 {
7438 stuffReadbuff(ptr);
7439 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7440 if (last)
7441 stuffReadbuff((char_u *)(last == '0'
7442 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7443 : IF_EB("\026^", CTRL_V_STR "^")));
7444 }
7445 while (--count > 0);
7446
7447 if (last)
7448 *last_ptr = last;
7449
7450 if (esc_ptr != NULL)
7451 *esc_ptr = ESC; /* put the ESC back */
7452
7453 /* may want to stuff a trailing ESC, to get out of Insert mode */
7454 if (!no_esc)
7455 stuffcharReadbuff(ESC);
7456
7457 return OK;
7458}
7459
7460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007461get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462{
7463 if (last_insert == NULL)
7464 return NULL;
7465 return last_insert + last_insert_skip;
7466}
7467
7468/*
7469 * Get last inserted string, and remove trailing <Esc>.
7470 * Returns pointer to allocated memory (must be freed) or NULL.
7471 */
7472 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007473get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474{
7475 char_u *s;
7476 int len;
7477
7478 if (last_insert == NULL)
7479 return NULL;
7480 s = vim_strsave(last_insert + last_insert_skip);
7481 if (s != NULL)
7482 {
7483 len = (int)STRLEN(s);
7484 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7485 s[len - 1] = NUL;
7486 }
7487 return s;
7488}
7489
7490/*
7491 * Check the word in front of the cursor for an abbreviation.
7492 * Called when the non-id character "c" has been entered.
7493 * When an abbreviation is recognized it is removed from the text and
7494 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7495 */
7496 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007497echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
7499 /* Don't check for abbreviation in paste mode, when disabled and just
7500 * after moving around with cursor keys. */
7501 if (p_paste || no_abbr || arrow_used)
7502 return FALSE;
7503
7504 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7505 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7506}
7507
7508/*
7509 * replace-stack functions
7510 *
7511 * When replacing characters, the replaced characters are remembered for each
7512 * new character. This is used to re-insert the old text when backspacing.
7513 *
7514 * There is a NUL headed list of characters for each character that is
7515 * currently in the file after the insertion point. When BS is used, one NUL
7516 * headed list is put back for the deleted character.
7517 *
7518 * For a newline, there are two NUL headed lists. One contains the characters
7519 * that the NL replaced. The extra one stores the characters after the cursor
7520 * that were deleted (always white space).
7521 *
7522 * Replace_offset is normally 0, in which case replace_push will add a new
7523 * character at the end of the stack. If replace_offset is not 0, that many
7524 * characters will be left on the stack above the newly inserted character.
7525 */
7526
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007527static char_u *replace_stack = NULL;
7528static long replace_stack_nr = 0; /* next entry in replace stack */
7529static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530
7531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007532replace_push(
7533 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534{
7535 char_u *p;
7536
7537 if (replace_stack_nr < replace_offset) /* nothing to do */
7538 return;
7539 if (replace_stack_len <= replace_stack_nr)
7540 {
7541 replace_stack_len += 50;
7542 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7543 if (p == NULL) /* out of memory */
7544 {
7545 replace_stack_len -= 50;
7546 return;
7547 }
7548 if (replace_stack != NULL)
7549 {
7550 mch_memmove(p, replace_stack,
7551 (size_t)(replace_stack_nr * sizeof(char_u)));
7552 vim_free(replace_stack);
7553 }
7554 replace_stack = p;
7555 }
7556 p = replace_stack + replace_stack_nr - replace_offset;
7557 if (replace_offset)
7558 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7559 *p = c;
7560 ++replace_stack_nr;
7561}
7562
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007563#if defined(FEAT_MBYTE) || defined(PROTO)
7564/*
7565 * Push a character onto the replace stack. Handles a multi-byte character in
7566 * reverse byte order, so that the first byte is popped off first.
7567 * Return the number of bytes done (includes composing characters).
7568 */
7569 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007570replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007571{
7572 int l = (*mb_ptr2len)(p);
7573 int j;
7574
7575 for (j = l - 1; j >= 0; --j)
7576 replace_push(p[j]);
7577 return l;
7578}
7579#endif
7580
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581/*
7582 * Pop one item from the replace stack.
7583 * return -1 if stack empty
7584 * return replaced character or NUL otherwise
7585 */
7586 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007587replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
7589 if (replace_stack_nr == 0)
7590 return -1;
7591 return (int)replace_stack[--replace_stack_nr];
7592}
7593
7594/*
7595 * Join the top two items on the replace stack. This removes to "off"'th NUL
7596 * encountered.
7597 */
7598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007599replace_join(
7600 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601{
7602 int i;
7603
7604 for (i = replace_stack_nr; --i >= 0; )
7605 if (replace_stack[i] == NUL && off-- <= 0)
7606 {
7607 --replace_stack_nr;
7608 mch_memmove(replace_stack + i, replace_stack + i + 1,
7609 (size_t)(replace_stack_nr - i));
7610 return;
7611 }
7612}
7613
7614/*
7615 * Pop bytes from the replace stack until a NUL is found, and insert them
7616 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7617 */
7618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007619replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620{
7621 int cc;
7622 int oldState = State;
7623
7624 State = NORMAL; /* don't want REPLACE here */
7625 while ((cc = replace_pop()) > 0)
7626 {
7627#ifdef FEAT_MBYTE
7628 mb_replace_pop_ins(cc);
7629#else
7630 ins_char(cc);
7631#endif
7632 dec_cursor();
7633 }
7634 State = oldState;
7635}
7636
7637#ifdef FEAT_MBYTE
7638/*
7639 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7640 * indicates a multi-byte char, pop the other bytes too.
7641 */
7642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007643mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644{
7645 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007646 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 int i;
7648 int c;
7649
7650 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7651 {
7652 buf[0] = cc;
7653 for (i = 1; i < n; ++i)
7654 buf[i] = replace_pop();
7655 ins_bytes_len(buf, n);
7656 }
7657 else
7658 ins_char(cc);
7659
7660 if (enc_utf8)
7661 /* Handle composing chars. */
7662 for (;;)
7663 {
7664 c = replace_pop();
7665 if (c == -1) /* stack empty */
7666 break;
7667 if ((n = MB_BYTE2LEN(c)) == 1)
7668 {
7669 /* Not a multi-byte char, put it back. */
7670 replace_push(c);
7671 break;
7672 }
7673 else
7674 {
7675 buf[0] = c;
7676 for (i = 1; i < n; ++i)
7677 buf[i] = replace_pop();
7678 if (utf_iscomposing(utf_ptr2char(buf)))
7679 ins_bytes_len(buf, n);
7680 else
7681 {
7682 /* Not a composing char, put it back. */
7683 for (i = n - 1; i >= 0; --i)
7684 replace_push(buf[i]);
7685 break;
7686 }
7687 }
7688 }
7689}
7690#endif
7691
7692/*
7693 * make the replace stack empty
7694 * (called when exiting replace mode)
7695 */
7696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007697replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
7699 vim_free(replace_stack);
7700 replace_stack = NULL;
7701 replace_stack_len = 0;
7702 replace_stack_nr = 0;
7703}
7704
7705/*
7706 * Handle doing a BS for one character.
7707 * cc < 0: replace stack empty, just move cursor
7708 * cc == 0: character was inserted, delete it
7709 * cc > 0: character was replaced, put cc (first byte of original char) back
7710 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007711 * When "limit_col" is >= 0, don't delete before this column. Matters when
7712 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 */
7714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007715replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
7717 int cc;
7718#ifdef FEAT_VREPLACE
7719 int orig_len = 0;
7720 int ins_len;
7721 int orig_vcols = 0;
7722 colnr_T start_vcol;
7723 char_u *p;
7724 int i;
7725 int vcol;
7726#endif
7727
7728 cc = replace_pop();
7729 if (cc > 0)
7730 {
7731#ifdef FEAT_VREPLACE
7732 if (State & VREPLACE_FLAG)
7733 {
7734 /* Get the number of screen cells used by the character we are
7735 * going to delete. */
7736 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7737 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7738 }
7739#endif
7740#ifdef FEAT_MBYTE
7741 if (has_mbyte)
7742 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007743 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744# ifdef FEAT_VREPLACE
7745 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007746 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747# endif
7748 replace_push(cc);
7749 }
7750 else
7751#endif
7752 {
7753 pchar_cursor(cc);
7754#ifdef FEAT_VREPLACE
7755 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007756 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757#endif
7758 }
7759 replace_pop_ins();
7760
7761#ifdef FEAT_VREPLACE
7762 if (State & VREPLACE_FLAG)
7763 {
7764 /* Get the number of screen cells used by the inserted characters */
7765 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007766 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 vcol = start_vcol;
7768 for (i = 0; i < ins_len; ++i)
7769 {
7770 vcol += chartabsize(p + i, vcol);
7771#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007772 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773#endif
7774 }
7775 vcol -= start_vcol;
7776
7777 /* Delete spaces that were inserted after the cursor to keep the
7778 * text aligned. */
7779 curwin->w_cursor.col += ins_len;
7780 while (vcol > orig_vcols && gchar_cursor() == ' ')
7781 {
7782 del_char(FALSE);
7783 ++orig_vcols;
7784 }
7785 curwin->w_cursor.col -= ins_len;
7786 }
7787#endif
7788
7789 /* mark the buffer as changed and prepare for displaying */
7790 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7791 }
7792 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007793 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794}
7795
7796#ifdef FEAT_CINDENT
7797/*
7798 * Return TRUE if C-indenting is on.
7799 */
7800 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007801cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802{
7803 return (!p_paste && (curbuf->b_p_cin
7804# ifdef FEAT_EVAL
7805 || *curbuf->b_p_inde != NUL
7806# endif
7807 ));
7808}
7809#endif
7810
7811#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7812/*
7813 * Re-indent the current line, based on the current contents of it and the
7814 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7815 * confused what all the part that handles Control-T is doing that I'm not.
7816 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7817 */
7818
7819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007820fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007822 int amount = get_the_indent();
7823
7824 if (amount >= 0)
7825 {
7826 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7827 if (linewhite(curwin->w_cursor.lnum))
7828 did_ai = TRUE; /* delete the indent if the line stays empty */
7829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830}
7831
7832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007833fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
7835 if (p_paste)
7836 return;
7837# ifdef FEAT_LISP
7838 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7839 fixthisline(get_lisp_indent);
7840# endif
7841# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7842 else
7843# endif
7844# ifdef FEAT_CINDENT
7845 if (cindent_on())
7846 do_c_expr_indent();
7847# endif
7848}
7849
7850#endif
7851
7852#ifdef FEAT_CINDENT
7853/*
7854 * return TRUE if 'cinkeys' contains the key "keytyped",
7855 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007856 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7858 *
7859 * "keytyped" can have a few special values:
7860 * KEY_OPEN_FORW
7861 * KEY_OPEN_BACK
7862 * KEY_COMPLETE just finished completion.
7863 *
7864 * If line_is_empty is TRUE accept keys with '0' before them.
7865 */
7866 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007867in_cinkeys(
7868 int keytyped,
7869 int when,
7870 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871{
7872 char_u *look;
7873 int try_match;
7874 int try_match_word;
7875 char_u *p;
7876 char_u *line;
7877 int icase;
7878 int i;
7879
Bram Moolenaar30848942009-12-24 14:46:12 +00007880 if (keytyped == NUL)
7881 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7882 return FALSE;
7883
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884#ifdef FEAT_EVAL
7885 if (*curbuf->b_p_inde != NUL)
7886 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7887 else
7888#endif
7889 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7890 while (*look)
7891 {
7892 /*
7893 * Find out if we want to try a match with this key, depending on
7894 * 'when' and a '*' or '!' before the key.
7895 */
7896 switch (when)
7897 {
7898 case '*': try_match = (*look == '*'); break;
7899 case '!': try_match = (*look == '!'); break;
7900 default: try_match = (*look != '*'); break;
7901 }
7902 if (*look == '*' || *look == '!')
7903 ++look;
7904
7905 /*
7906 * If there is a '0', only accept a match if the line is empty.
7907 * But may still match when typing last char of a word.
7908 */
7909 if (*look == '0')
7910 {
7911 try_match_word = try_match;
7912 if (!line_is_empty)
7913 try_match = FALSE;
7914 ++look;
7915 }
7916 else
7917 try_match_word = FALSE;
7918
7919 /*
7920 * does it look like a control character?
7921 */
7922 if (*look == '^'
7923#ifdef EBCDIC
7924 && (Ctrl_chr(look[1]) != 0)
7925#else
7926 && look[1] >= '?' && look[1] <= '_'
7927#endif
7928 )
7929 {
7930 if (try_match && keytyped == Ctrl_chr(look[1]))
7931 return TRUE;
7932 look += 2;
7933 }
7934 /*
7935 * 'o' means "o" command, open forward.
7936 * 'O' means "O" command, open backward.
7937 */
7938 else if (*look == 'o')
7939 {
7940 if (try_match && keytyped == KEY_OPEN_FORW)
7941 return TRUE;
7942 ++look;
7943 }
7944 else if (*look == 'O')
7945 {
7946 if (try_match && keytyped == KEY_OPEN_BACK)
7947 return TRUE;
7948 ++look;
7949 }
7950
7951 /*
7952 * 'e' means to check for "else" at start of line and just before the
7953 * cursor.
7954 */
7955 else if (*look == 'e')
7956 {
7957 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7958 {
7959 p = ml_get_curline();
7960 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7961 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7962 return TRUE;
7963 }
7964 ++look;
7965 }
7966
7967 /*
7968 * ':' only causes an indent if it is at the end of a label or case
7969 * statement, or when it was before typing the ':' (to fix
7970 * class::method for C++).
7971 */
7972 else if (*look == ':')
7973 {
7974 if (try_match && keytyped == ':')
7975 {
7976 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007977 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007979 /* Need to get the line again after cin_islabel(). */
7980 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 if (curwin->w_cursor.col > 2
7982 && p[curwin->w_cursor.col - 1] == ':'
7983 && p[curwin->w_cursor.col - 2] == ':')
7984 {
7985 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007986 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007987 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 p = ml_get_curline();
7989 p[curwin->w_cursor.col - 1] = ':';
7990 if (i)
7991 return TRUE;
7992 }
7993 }
7994 ++look;
7995 }
7996
7997
7998 /*
7999 * Is it a key in <>, maybe?
8000 */
8001 else if (*look == '<')
8002 {
8003 if (try_match)
8004 {
8005 /*
8006 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8007 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8008 * >, *, : and ! keys if they really really want to.
8009 */
8010 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8011 && keytyped == look[1])
8012 return TRUE;
8013
8014 if (keytyped == get_special_key_code(look + 1))
8015 return TRUE;
8016 }
8017 while (*look && *look != '>')
8018 look++;
8019 while (*look == '>')
8020 look++;
8021 }
8022
8023 /*
8024 * Is it a word: "=word"?
8025 */
8026 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8027 {
8028 ++look;
8029 if (*look == '~')
8030 {
8031 icase = TRUE;
8032 ++look;
8033 }
8034 else
8035 icase = FALSE;
8036 p = vim_strchr(look, ',');
8037 if (p == NULL)
8038 p = look + STRLEN(look);
8039 if ((try_match || try_match_word)
8040 && curwin->w_cursor.col >= (colnr_T)(p - look))
8041 {
8042 int match = FALSE;
8043
8044#ifdef FEAT_INS_EXPAND
8045 if (keytyped == KEY_COMPLETE)
8046 {
8047 char_u *s;
8048
8049 /* Just completed a word, check if it starts with "look".
8050 * search back for the start of a word. */
8051 line = ml_get_curline();
8052# ifdef FEAT_MBYTE
8053 if (has_mbyte)
8054 {
8055 char_u *n;
8056
8057 for (s = line + curwin->w_cursor.col; s > line; s = n)
8058 {
8059 n = mb_prevptr(line, s);
8060 if (!vim_iswordp(n))
8061 break;
8062 }
8063 }
8064 else
8065# endif
8066 for (s = line + curwin->w_cursor.col; s > line; --s)
8067 if (!vim_iswordc(s[-1]))
8068 break;
8069 if (s + (p - look) <= line + curwin->w_cursor.col
8070 && (icase
8071 ? MB_STRNICMP(s, look, p - look)
8072 : STRNCMP(s, look, p - look)) == 0)
8073 match = TRUE;
8074 }
8075 else
8076#endif
8077 /* TODO: multi-byte */
8078 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8079 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8080 {
8081 line = ml_get_cursor();
8082 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8083 || !vim_iswordc(line[-(p - look) - 1]))
8084 && (icase
8085 ? MB_STRNICMP(line - (p - look), look, p - look)
8086 : STRNCMP(line - (p - look), look, p - look))
8087 == 0)
8088 match = TRUE;
8089 }
8090 if (match && try_match_word && !try_match)
8091 {
8092 /* "0=word": Check if there are only blanks before the
8093 * word. */
8094 line = ml_get_curline();
8095 if ((int)(skipwhite(line) - line) !=
8096 (int)(curwin->w_cursor.col - (p - look)))
8097 match = FALSE;
8098 }
8099 if (match)
8100 return TRUE;
8101 }
8102 look = p;
8103 }
8104
8105 /*
8106 * ok, it's a boring generic character.
8107 */
8108 else
8109 {
8110 if (try_match && *look == keytyped)
8111 return TRUE;
8112 ++look;
8113 }
8114
8115 /*
8116 * Skip over ", ".
8117 */
8118 look = skip_to_option_part(look);
8119 }
8120 return FALSE;
8121}
8122#endif /* FEAT_CINDENT */
8123
8124#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8125/*
8126 * Map Hebrew keyboard when in hkmap mode.
8127 */
8128 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008129hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130{
8131 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8132 {
8133 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8134 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8135 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8136 static char_u map[26] =
8137 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8138 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8139 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8140 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8141 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8142 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8143 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8144 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8145 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8146
8147 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8148 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8149 /* '-1'='sofit' */
8150 else if (c == 'x')
8151 return 'X';
8152 else if (c == 'q')
8153 return '\''; /* {geresh}={'} */
8154 else if (c == 246)
8155 return ' '; /* \"o --> ' ' for a german keyboard */
8156 else if (c == 228)
8157 return ' '; /* \"a --> ' ' -- / -- */
8158 else if (c == 252)
8159 return ' '; /* \"u --> ' ' -- / -- */
8160#ifdef EBCDIC
8161 else if (islower(c))
8162#else
8163 /* NOTE: islower() does not do the right thing for us on Linux so we
8164 * do this the same was as 5.7 and previous, so it works correctly on
8165 * all systems. Specifically, the e.g. Delete and Arrow keys are
8166 * munged and won't work if e.g. searching for Hebrew text.
8167 */
8168 else if (c >= 'a' && c <= 'z')
8169#endif
8170 return (int)(map[CharOrdLow(c)] + p_aleph);
8171 else
8172 return c;
8173 }
8174 else
8175 {
8176 switch (c)
8177 {
8178 case '`': return ';';
8179 case '/': return '.';
8180 case '\'': return ',';
8181 case 'q': return '/';
8182 case 'w': return '\'';
8183
8184 /* Hebrew letters - set offset from 'a' */
8185 case ',': c = '{'; break;
8186 case '.': c = 'v'; break;
8187 case ';': c = 't'; break;
8188 default: {
8189 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8190
8191#ifdef EBCDIC
8192 /* see note about islower() above */
8193 if (!islower(c))
8194#else
8195 if (c < 'a' || c > 'z')
8196#endif
8197 return c;
8198 c = str[CharOrdLow(c)];
8199 break;
8200 }
8201 }
8202
8203 return (int)(CharOrdLow(c) + p_aleph);
8204 }
8205}
8206#endif
8207
8208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008209ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210{
8211 int need_redraw = FALSE;
8212 int regname;
8213 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008214 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215
8216 /*
8217 * If we are going to wait for a character, show a '"'.
8218 */
8219 pc_status = PC_STATUS_UNSET;
8220 if (redrawing() && !char_avail())
8221 {
8222 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008223 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224
8225 edit_putchar('"', TRUE);
8226#ifdef FEAT_CMDL_INFO
8227 add_to_showcmd_c(Ctrl_R);
8228#endif
8229 }
8230
8231#ifdef USE_ON_FLY_SCROLL
8232 dont_scroll = TRUE; /* disallow scrolling here */
8233#endif
8234
8235 /*
8236 * Don't map the register name. This also prevents the mode message to be
8237 * deleted when ESC is hit.
8238 */
8239 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008240 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8243 {
8244 /* Get a third key for literal register insertion */
8245 literally = regname;
8246#ifdef FEAT_CMDL_INFO
8247 add_to_showcmd_c(literally);
8248#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008249 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 }
8252 --no_mapping;
8253
8254#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008255 /* Don't call u_sync() while typing the expression or giving an error
8256 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 ++no_u_sync;
8258 if (regname == '=')
8259 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008260# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008262# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008263 /* Sync undo when evaluating the expression calls setline() or
8264 * append(), so that it can be undone separately. */
8265 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008266
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008268# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 /* Restore the Input Method. */
8270 if (im_on)
8271 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008274 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8275 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008276 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 else
8280 {
8281#endif
8282 if (literally == Ctrl_O || literally == Ctrl_P)
8283 {
8284 /* Append the command to the redo buffer. */
8285 AppendCharToRedobuff(Ctrl_R);
8286 AppendCharToRedobuff(literally);
8287 AppendCharToRedobuff(regname);
8288
8289 do_put(regname, BACKWARD, 1L,
8290 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8291 }
8292 else if (insert_reg(regname, literally) == FAIL)
8293 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008294 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 need_redraw = TRUE; /* remove the '"' */
8296 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008297 else if (stop_insert_mode)
8298 /* When the '=' register was used and a function was invoked that
8299 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8300 * insert anything, need to remove the '"' */
8301 need_redraw = TRUE;
8302
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303#ifdef FEAT_EVAL
8304 }
8305 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008306 if (u_sync_once == 1)
8307 ins_need_undo = TRUE;
8308 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309#endif
8310#ifdef FEAT_CMDL_INFO
8311 clear_showcmd();
8312#endif
8313
8314 /* If the inserted register is empty, we need to remove the '"' */
8315 if (need_redraw || stuff_empty())
8316 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008317
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008318 /* Disallow starting Visual mode here, would get a weird mode. */
8319 if (!vis_active && VIsual_active)
8320 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321}
8322
8323/*
8324 * CTRL-G commands in Insert mode.
8325 */
8326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008327ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328{
8329 int c;
8330
8331#ifdef FEAT_INS_EXPAND
8332 /* Right after CTRL-X the cursor will be after the ruler. */
8333 setcursor();
8334#endif
8335
8336 /*
8337 * Don't map the second key. This also prevents the mode message to be
8338 * deleted when ESC is hit.
8339 */
8340 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008341 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 --no_mapping;
8343 switch (c)
8344 {
8345 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8346 case K_UP:
8347 case Ctrl_K:
8348 case 'k': ins_up(TRUE);
8349 break;
8350
8351 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8352 case K_DOWN:
8353 case Ctrl_J:
8354 case 'j': ins_down(TRUE);
8355 break;
8356
8357 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008358 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008360
8361 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008362 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008363 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008364 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 break;
8366
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008367 /* CTRL-G U: do not break undo with the next char */
8368 case 'U':
8369 /* Allow one left/right cursor movement with the next char,
8370 * without breaking undo. */
8371 dont_sync_undo = MAYBE;
8372 break;
8373
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008375 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 }
8377}
8378
8379/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008380 * CTRL-^ in Insert mode.
8381 */
8382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008383ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008384{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008385 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008386 {
8387 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8388 if (State & LANGMAP)
8389 {
8390 curbuf->b_p_iminsert = B_IMODE_NONE;
8391 State &= ~LANGMAP;
8392 }
8393 else
8394 {
8395 curbuf->b_p_iminsert = B_IMODE_LMAP;
8396 State |= LANGMAP;
8397#ifdef USE_IM_CONTROL
8398 im_set_active(FALSE);
8399#endif
8400 }
8401 }
8402#ifdef USE_IM_CONTROL
8403 else
8404 {
8405 /* There are no ":lmap" mappings, toggle IM */
8406 if (im_get_status())
8407 {
8408 curbuf->b_p_iminsert = B_IMODE_NONE;
8409 im_set_active(FALSE);
8410 }
8411 else
8412 {
8413 curbuf->b_p_iminsert = B_IMODE_IM;
8414 State &= ~LANGMAP;
8415 im_set_active(TRUE);
8416 }
8417 }
8418#endif
8419 set_iminsert_global();
8420 showmode();
8421#ifdef FEAT_GUI
8422 /* may show different cursor shape or color */
8423 if (gui.in_use)
8424 gui_update_cursor(TRUE, FALSE);
8425#endif
8426#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8427 /* Show/unshow value of 'keymap' in status lines. */
8428 status_redraw_curbuf();
8429#endif
8430}
8431
8432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 * Handle ESC in insert mode.
8434 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8435 * insert.
8436 */
8437 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008438ins_esc(
8439 long *count,
8440 int cmdchar,
8441 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442{
8443 int temp;
8444 static int disabled_redraw = FALSE;
8445
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008446#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008447 check_spell_redraw();
8448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449#if defined(FEAT_HANGULIN)
8450# if defined(ESC_CHG_TO_ENG_MODE)
8451 hangul_input_state_set(0);
8452# endif
8453 if (composing_hangul)
8454 {
8455 push_raw_key(composing_hangul_buffer, 2);
8456 composing_hangul = 0;
8457 }
8458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459
8460 temp = curwin->w_cursor.col;
8461 if (disabled_redraw)
8462 {
8463 --RedrawingDisabled;
8464 disabled_redraw = FALSE;
8465 }
8466 if (!arrow_used)
8467 {
8468 /*
8469 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008470 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8471 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 */
8473 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008474 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475
8476 /*
8477 * Repeating insert may take a long time. Check for
8478 * interrupt now and then.
8479 */
8480 if (*count > 0)
8481 {
8482 line_breakcheck();
8483 if (got_int)
8484 *count = 0;
8485 }
8486
8487 if (--*count > 0) /* repeat what was typed */
8488 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008489 /* Vi repeats the insert without replacing characters. */
8490 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8491 State &= ~REPLACE_FLAG;
8492
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 (void)start_redo_ins();
8494 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008495 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 ++RedrawingDisabled;
8497 disabled_redraw = TRUE;
8498 return FALSE; /* repeat the insert */
8499 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008500 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 undisplay_dollar();
8502 }
8503
8504 /* When an autoindent was removed, curswant stays after the
8505 * indent */
8506 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8507 curwin->w_set_curswant = TRUE;
8508
8509 /* Remember the last Insert position in the '^ mark. */
8510 if (!cmdmod.keepjumps)
8511 curbuf->b_last_insert = curwin->w_cursor;
8512
8513 /*
8514 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008515 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008517 if (!nomove
8518 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519#ifdef FEAT_VIRTUALEDIT
8520 || curwin->w_cursor.coladd > 0
8521#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008522 )
8523 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008524 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525#ifdef FEAT_RIGHTLEFT
8526 && !revins_on
8527#endif
8528 )
8529 {
8530#ifdef FEAT_VIRTUALEDIT
8531 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8532 {
8533 oneleft();
8534 if (restart_edit != NUL)
8535 ++curwin->w_cursor.coladd;
8536 }
8537 else
8538#endif
8539 {
8540 --curwin->w_cursor.col;
8541#ifdef FEAT_MBYTE
8542 /* Correct cursor for multi-byte character. */
8543 if (has_mbyte)
8544 mb_adjust_cursor();
8545#endif
8546 }
8547 }
8548
8549#ifdef USE_IM_CONTROL
8550 /* Disable IM to allow typing English directly for Normal mode commands.
8551 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8552 * well). */
8553 if (!(State & LANGMAP))
8554 im_save_status(&curbuf->b_p_iminsert);
8555 im_set_active(FALSE);
8556#endif
8557
8558 State = NORMAL;
8559 /* need to position cursor again (e.g. when on a TAB ) */
8560 changed_cline_bef_curs();
8561
8562#ifdef FEAT_MOUSE
8563 setmouse();
8564#endif
8565#ifdef CURSOR_SHAPE
8566 ui_cursor_shape(); /* may show different cursor shape */
8567#endif
8568
8569 /*
8570 * When recording or for CTRL-O, need to display the new mode.
8571 * Otherwise remove the mode message.
8572 */
8573 if (Recording || restart_edit != NUL)
8574 showmode();
8575 else if (p_smd)
8576 MSG("");
8577
8578 return TRUE; /* exit Insert mode */
8579}
8580
8581#ifdef FEAT_RIGHTLEFT
8582/*
8583 * Toggle language: hkmap and revins_on.
8584 * Move to end of reverse inserted text.
8585 */
8586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008587ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588{
8589 if (revins_on && revins_chars && revins_scol >= 0)
8590 {
8591 while (gchar_cursor() != NUL && revins_chars--)
8592 ++curwin->w_cursor.col;
8593 }
8594 p_ri = !p_ri;
8595 revins_on = (State == INSERT && p_ri);
8596 if (revins_on)
8597 {
8598 revins_scol = curwin->w_cursor.col;
8599 revins_legal++;
8600 revins_chars = 0;
8601 undisplay_dollar();
8602 }
8603 else
8604 revins_scol = -1;
8605#ifdef FEAT_FKMAP
8606 if (p_altkeymap)
8607 {
8608 /*
8609 * to be consistent also for redo command, using '.'
8610 * set arrow_used to true and stop it - causing to redo
8611 * characters entered in one mode (normal/reverse insert).
8612 */
8613 arrow_used = TRUE;
8614 (void)stop_arrow();
8615 p_fkmap = curwin->w_p_rl ^ p_ri;
8616 if (p_fkmap && p_ri)
8617 State = INSERT;
8618 }
8619 else
8620#endif
8621 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8622 showmode();
8623}
8624#endif
8625
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626/*
8627 * If 'keymodel' contains "startsel", may start selection.
8628 * Returns TRUE when a CTRL-O and other keys stuffed.
8629 */
8630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008631ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632{
8633 if (km_startsel)
8634 switch (c)
8635 {
8636 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 case K_PAGEUP:
8639 case K_KPAGEUP:
8640 case K_PAGEDOWN:
8641 case K_KPAGEDOWN:
8642# ifdef MACOS
8643 case K_LEFT:
8644 case K_RIGHT:
8645 case K_UP:
8646 case K_DOWN:
8647 case K_END:
8648 case K_HOME:
8649# endif
8650 if (!(mod_mask & MOD_MASK_SHIFT))
8651 break;
8652 /* FALLTHROUGH */
8653 case K_S_LEFT:
8654 case K_S_RIGHT:
8655 case K_S_UP:
8656 case K_S_DOWN:
8657 case K_S_END:
8658 case K_S_HOME:
8659 /* Start selection right away, the cursor can move with
8660 * CTRL-O when beyond the end of the line. */
8661 start_selection();
8662
8663 /* Execute the key in (insert) Select mode. */
8664 stuffcharReadbuff(Ctrl_O);
8665 if (mod_mask)
8666 {
8667 char_u buf[4];
8668
8669 buf[0] = K_SPECIAL;
8670 buf[1] = KS_MODIFIER;
8671 buf[2] = mod_mask;
8672 buf[3] = NUL;
8673 stuffReadbuff(buf);
8674 }
8675 stuffcharReadbuff(c);
8676 return TRUE;
8677 }
8678 return FALSE;
8679}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680
8681/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008682 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008683 */
8684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008685ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008686{
8687#ifdef FEAT_FKMAP
8688 if (p_fkmap && p_ri)
8689 {
8690 beep_flush();
8691 EMSG(farsi_text_3); /* encoded in Farsi */
8692 return;
8693 }
8694#endif
8695
8696#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008697# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008698 set_vim_var_string(VV_INSERTMODE,
8699 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008700# ifdef FEAT_VREPLACE
8701 replaceState == VREPLACE ? "v" :
8702# endif
8703 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008704# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008705 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8706#endif
8707 if (State & REPLACE_FLAG)
8708 State = INSERT | (State & LANGMAP);
8709 else
8710 State = replaceState | (State & LANGMAP);
8711 AppendCharToRedobuff(K_INS);
8712 showmode();
8713#ifdef CURSOR_SHAPE
8714 ui_cursor_shape(); /* may show different cursor shape */
8715#endif
8716}
8717
8718/*
8719 * Pressed CTRL-O in Insert mode.
8720 */
8721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008722ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008723{
8724#ifdef FEAT_VREPLACE
8725 if (State & VREPLACE_FLAG)
8726 restart_edit = 'V';
8727 else
8728#endif
8729 if (State & REPLACE_FLAG)
8730 restart_edit = 'R';
8731 else
8732 restart_edit = 'I';
8733#ifdef FEAT_VIRTUALEDIT
8734 if (virtual_active())
8735 ins_at_eol = FALSE; /* cursor always keeps its column */
8736 else
8737#endif
8738 ins_at_eol = (gchar_cursor() == NUL);
8739}
8740
8741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 * If the cursor is on an indent, ^T/^D insert/delete one
8743 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008744 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 * with vi. But vi only supports ^T and ^D after an
8746 * autoindent, we support it everywhere.
8747 */
8748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008749ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
8751 if (stop_arrow() == FAIL)
8752 return;
8753 AppendCharToRedobuff(c);
8754
8755 /*
8756 * 0^D and ^^D: remove all indent.
8757 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008758 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8759 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 {
8761 --curwin->w_cursor.col;
8762 (void)del_char(FALSE); /* delete the '^' or '0' */
8763 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8764 if (State & REPLACE_FLAG)
8765 replace_pop_ins();
8766 if (lastc == '^')
8767 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008768 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 }
8770 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008771 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772
8773 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8774 did_ai = FALSE;
8775#ifdef FEAT_SMARTINDENT
8776 did_si = FALSE;
8777 can_si = FALSE;
8778 can_si_back = FALSE;
8779#endif
8780#ifdef FEAT_CINDENT
8781 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8782#endif
8783}
8784
8785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008786ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
8788 int temp;
8789
8790 if (stop_arrow() == FAIL)
8791 return;
8792 if (gchar_cursor() == NUL) /* delete newline */
8793 {
8794 temp = curwin->w_cursor.col;
8795 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008796 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008797 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 else
8799 curwin->w_cursor.col = temp;
8800 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008801 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8802 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 did_ai = FALSE;
8804#ifdef FEAT_SMARTINDENT
8805 did_si = FALSE;
8806 can_si = FALSE;
8807 can_si_back = FALSE;
8808#endif
8809 AppendCharToRedobuff(K_DEL);
8810}
8811
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008812static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008813
8814/*
8815 * Delete one character for ins_bs().
8816 */
8817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008818ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008819{
8820 dec_cursor();
8821 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8822 if (State & REPLACE_FLAG)
8823 {
8824 /* Don't delete characters before the insert point when in
8825 * Replace mode */
8826 if (curwin->w_cursor.lnum != Insstart.lnum
8827 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008828 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008829 }
8830 else
8831 (void)del_char(FALSE);
8832}
8833
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834/*
8835 * Handle Backspace, delete-word and delete-line in Insert mode.
8836 * Return TRUE when backspace was actually used.
8837 */
8838 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008839ins_bs(
8840 int c,
8841 int mode,
8842 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843{
8844 linenr_T lnum;
8845 int cc;
8846 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008847 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 colnr_T mincol;
8849 int did_backspace = FALSE;
8850 int in_indent;
8851 int oldState;
8852#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008853 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854#endif
8855
8856 /*
8857 * can't delete anything in an empty file
8858 * can't backup past first character in buffer
8859 * can't backup past starting point unless 'backspace' > 1
8860 * can backup to a previous line if 'backspace' == 0
8861 */
8862 if ( bufempty()
8863 || (
8864#ifdef FEAT_RIGHTLEFT
8865 !revins_on &&
8866#endif
8867 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8868 || (!can_bs(BS_START)
8869 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008870 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8871 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8873 && curwin->w_cursor.col <= ai_col)
8874 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8875 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008876 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 return FALSE;
8878 }
8879
8880 if (stop_arrow() == FAIL)
8881 return FALSE;
8882 in_indent = inindent(0);
8883#ifdef FEAT_CINDENT
8884 if (in_indent)
8885 can_cindent = FALSE;
8886#endif
8887#ifdef FEAT_COMMENTS
8888 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8889#endif
8890#ifdef FEAT_RIGHTLEFT
8891 if (revins_on) /* put cursor after last inserted char */
8892 inc_cursor();
8893#endif
8894
8895#ifdef FEAT_VIRTUALEDIT
8896 /* Virtualedit:
8897 * BACKSPACE_CHAR eats a virtual space
8898 * BACKSPACE_WORD eats all coladd
8899 * BACKSPACE_LINE eats all coladd and keeps going
8900 */
8901 if (curwin->w_cursor.coladd > 0)
8902 {
8903 if (mode == BACKSPACE_CHAR)
8904 {
8905 --curwin->w_cursor.coladd;
8906 return TRUE;
8907 }
8908 if (mode == BACKSPACE_WORD)
8909 {
8910 curwin->w_cursor.coladd = 0;
8911 return TRUE;
8912 }
8913 curwin->w_cursor.coladd = 0;
8914 }
8915#endif
8916
8917 /*
8918 * delete newline!
8919 */
8920 if (curwin->w_cursor.col == 0)
8921 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008922 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008923 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924#ifdef FEAT_RIGHTLEFT
8925 || revins_on
8926#endif
8927 )
8928 {
8929 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8930 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8931 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008932 --Insstart.lnum;
8933 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 }
8935 /*
8936 * In replace mode:
8937 * cc < 0: NL was inserted, delete it
8938 * cc >= 0: NL was replaced, put original characters back
8939 */
8940 cc = -1;
8941 if (State & REPLACE_FLAG)
8942 cc = replace_pop(); /* returns -1 if NL was inserted */
8943 /*
8944 * In replace mode, in the line we started replacing, we only move the
8945 * cursor.
8946 */
8947 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8948 {
8949 dec_cursor();
8950 }
8951 else
8952 {
8953#ifdef FEAT_VREPLACE
8954 if (!(State & VREPLACE_FLAG)
8955 || curwin->w_cursor.lnum > orig_line_count)
8956#endif
8957 {
8958 temp = gchar_cursor(); /* remember current char */
8959 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008960
8961 /* When "aw" is in 'formatoptions' we must delete the space at
8962 * the end of the line, otherwise the line will be broken
8963 * again when auto-formatting. */
8964 if (has_format_option(FO_AUTO)
8965 && has_format_option(FO_WHITE_PAR))
8966 {
8967 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8968 TRUE);
8969 int len;
8970
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008971 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008972 if (len > 0 && ptr[len - 1] == ' ')
8973 ptr[len - 1] = NUL;
8974 }
8975
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008976 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 if (temp == NUL && gchar_cursor() != NUL)
8978 inc_cursor();
8979 }
8980#ifdef FEAT_VREPLACE
8981 else
8982 dec_cursor();
8983#endif
8984
8985 /*
8986 * In REPLACE mode we have to put back the text that was replaced
8987 * by the NL. On the replace stack is first a NUL-terminated
8988 * sequence of characters that were deleted and then the
8989 * characters that NL replaced.
8990 */
8991 if (State & REPLACE_FLAG)
8992 {
8993 /*
8994 * Do the next ins_char() in NORMAL state, to
8995 * prevent ins_char() from replacing characters and
8996 * avoiding showmatch().
8997 */
8998 oldState = State;
8999 State = NORMAL;
9000 /*
9001 * restore characters (blanks) deleted after cursor
9002 */
9003 while (cc > 0)
9004 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009005 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006#ifdef FEAT_MBYTE
9007 mb_replace_pop_ins(cc);
9008#else
9009 ins_char(cc);
9010#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009011 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 cc = replace_pop();
9013 }
9014 /* restore the characters that NL replaced */
9015 replace_pop_ins();
9016 State = oldState;
9017 }
9018 }
9019 did_ai = FALSE;
9020 }
9021 else
9022 {
9023 /*
9024 * Delete character(s) before the cursor.
9025 */
9026#ifdef FEAT_RIGHTLEFT
9027 if (revins_on) /* put cursor on last inserted char */
9028 dec_cursor();
9029#endif
9030 mincol = 0;
9031 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009032 if (mode == BACKSPACE_LINE
9033 && (curbuf->b_p_ai
9034#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009035 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009036#endif
9037 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038#ifdef FEAT_RIGHTLEFT
9039 && !revins_on
9040#endif
9041 )
9042 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009043 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009045 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009047 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 }
9049
9050 /*
9051 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9052 */
9053 if ( mode == BACKSPACE_CHAR
9054 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009055 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009056 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 && (*(ml_get_cursor() - 1) == TAB
9058 || (*(ml_get_cursor() - 1) == ' '
9059 && (!*inserted_space_p
9060 || arrow_used))))))
9061 {
9062 int ts;
9063 colnr_T vcol;
9064 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009065 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066
9067 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009068 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009069 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009071 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 /* Compute the virtual column where we want to be. Since
9073 * 'showbreak' may get in the way, need to get the last column of
9074 * the previous character. */
9075 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009076 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 dec_cursor();
9078 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9079 inc_cursor();
9080 want_vcol = (want_vcol / ts) * ts;
9081
9082 /* delete characters until we are at or before want_vcol */
9083 while (vcol > want_vcol
9084 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009085 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086
9087 /* insert extra spaces until we are at want_vcol */
9088 while (vcol < want_vcol)
9089 {
9090 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009091 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9092 && curwin->w_cursor.col < Insstart_orig.col)
9093 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094
9095#ifdef FEAT_VREPLACE
9096 if (State & VREPLACE_FLAG)
9097 ins_char(' ');
9098 else
9099#endif
9100 {
9101 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009102 if ((State & REPLACE_FLAG))
9103 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 }
9105 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9106 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009107
9108 /* If we are now back where we started delete one character. Can
9109 * happen when using 'sts' and 'linebreak'. */
9110 if (vcol >= start_vcol)
9111 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 }
9113
9114 /*
9115 * Delete upto starting point, start of line or previous word.
9116 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009119#ifdef FEAT_MBYTE
9120 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009122 if (has_mbyte)
9123 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009125 do
9126 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009128 if (!revins_on) /* put cursor on char to be deleted */
9129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009131
9132 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009134 /* look multi-byte character class */
9135 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009137 prev_cclass = cclass;
9138 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009141
9142 /* start of word? */
9143 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9144 {
9145 mode = BACKSPACE_WORD_NOT_SPACE;
9146 temp = vim_iswordc(cc);
9147 }
9148 /* end of word? */
9149 else if (mode == BACKSPACE_WORD_NOT_SPACE
9150 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9151#ifdef FEAT_MBYTE
9152 || prev_cclass != cclass
9153#endif
9154 ))
9155 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009157 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009159 inc_cursor();
9160#ifdef FEAT_RIGHTLEFT
9161 else if (State & REPLACE_FLAG)
9162 dec_cursor();
9163#endif
9164 break;
9165 }
9166 if (State & REPLACE_FLAG)
9167 replace_do_bs(-1);
9168 else
9169 {
9170#ifdef FEAT_MBYTE
9171 if (enc_utf8 && p_deco)
9172 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9173#endif
9174 (void)del_char(FALSE);
9175#ifdef FEAT_MBYTE
9176 /*
9177 * If there are combining characters and 'delcombine' is set
9178 * move the cursor back. Don't back up before the base
9179 * character.
9180 */
9181 if (enc_utf8 && p_deco && cpc[0] != NUL)
9182 inc_cursor();
9183#endif
9184#ifdef FEAT_RIGHTLEFT
9185 if (revins_chars)
9186 {
9187 revins_chars--;
9188 revins_legal++;
9189 }
9190 if (revins_on && gchar_cursor() == NUL)
9191 break;
9192#endif
9193 }
9194 /* Just a single backspace?: */
9195 if (mode == BACKSPACE_CHAR)
9196 break;
9197 } while (
9198#ifdef FEAT_RIGHTLEFT
9199 revins_on ||
9200#endif
9201 (curwin->w_cursor.col > mincol
9202 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9203 || curwin->w_cursor.col != Insstart_orig.col)));
9204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 did_backspace = TRUE;
9206 }
9207#ifdef FEAT_SMARTINDENT
9208 did_si = FALSE;
9209 can_si = FALSE;
9210 can_si_back = FALSE;
9211#endif
9212 if (curwin->w_cursor.col <= 1)
9213 did_ai = FALSE;
9214 /*
9215 * It's a little strange to put backspaces into the redo
9216 * buffer, but it makes auto-indent a lot easier to deal
9217 * with.
9218 */
9219 AppendCharToRedobuff(c);
9220
9221 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009222 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9223 && curwin->w_cursor.col < Insstart_orig.col)
9224 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225
9226 /* vi behaviour: the cursor moves backward but the character that
9227 * was there remains visible
9228 * Vim behaviour: the cursor moves backward and the character that
9229 * was there is erased from the screen.
9230 * We can emulate the vi behaviour by pretending there is a dollar
9231 * displayed even when there isn't.
9232 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009233 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 dollar_vcol = curwin->w_virtcol;
9235
Bram Moolenaarce3be472008-01-14 19:12:28 +00009236#ifdef FEAT_FOLDING
9237 /* When deleting a char the cursor line must never be in a closed fold.
9238 * E.g., when 'foldmethod' is indent and deleting the first non-white
9239 * char before a Tab. */
9240 if (did_backspace)
9241 foldOpenCursor();
9242#endif
9243
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 return did_backspace;
9245}
9246
9247#ifdef FEAT_MOUSE
9248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009249ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250{
9251 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009252 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253
9254# ifdef FEAT_GUI
9255 /* When GUI is active, also move/paste when 'mouse' is empty */
9256 if (!gui.in_use)
9257# endif
9258 if (!mouse_has(MOUSE_INSERT))
9259 return;
9260
9261 undisplay_dollar();
9262 tpos = curwin->w_cursor;
9263 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9264 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009265#ifdef FEAT_WINDOWS
9266 win_T *new_curwin = curwin;
9267
9268 if (curwin != old_curwin && win_valid(old_curwin))
9269 {
9270 /* Mouse took us to another window. We need to go back to the
9271 * previous one to stop insert there properly. */
9272 curwin = old_curwin;
9273 curbuf = curwin->w_buffer;
9274 }
9275#endif
9276 start_arrow(curwin == old_curwin ? &tpos : NULL);
9277#ifdef FEAT_WINDOWS
9278 if (curwin != new_curwin && win_valid(new_curwin))
9279 {
9280 curwin = new_curwin;
9281 curbuf = curwin->w_buffer;
9282 }
9283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284# ifdef FEAT_CINDENT
9285 can_cindent = TRUE;
9286# endif
9287 }
9288
9289#ifdef FEAT_WINDOWS
9290 /* redraw status lines (in case another window became active) */
9291 redraw_statuslines();
9292#endif
9293}
9294
9295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009296ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297{
9298 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009299# if defined(FEAT_WINDOWS)
9300 win_T *old_curwin = curwin;
9301# endif
9302# ifdef FEAT_INS_EXPAND
9303 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304# endif
9305
9306 tpos = curwin->w_cursor;
9307
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009308# ifdef FEAT_WINDOWS
9309 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 {
9311 int row, col;
9312
9313 row = mouse_row;
9314 col = mouse_col;
9315
9316 /* find the window at the pointer coordinates */
9317 curwin = mouse_find_win(&row, &col);
9318 curbuf = curwin->w_buffer;
9319 }
9320 if (curwin == old_curwin)
9321# endif
9322 undisplay_dollar();
9323
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009324# ifdef FEAT_INS_EXPAND
9325 /* Don't scroll the window in which completion is being done. */
9326 if (!pum_visible()
9327# if defined(FEAT_WINDOWS)
9328 || curwin != old_curwin
9329# endif
9330 )
9331# endif
9332 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009333 if (dir == MSCR_DOWN || dir == MSCR_UP)
9334 {
9335 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9336 scroll_redraw(dir,
9337 (long)(curwin->w_botline - curwin->w_topline));
9338 else
9339 scroll_redraw(dir, 3L);
9340 }
9341#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009342 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009343 {
9344 int val, step = 6;
9345
9346 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9347 step = W_WIDTH(curwin);
9348 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9349 if (val < 0)
9350 val = 0;
9351 gui_do_horiz_scroll(val, TRUE);
9352 }
9353#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009354# ifdef FEAT_INS_EXPAND
9355 did_scroll = TRUE;
9356# endif
9357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009359# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 curwin->w_redr_status = TRUE;
9361
9362 curwin = old_curwin;
9363 curbuf = curwin->w_buffer;
9364# endif
9365
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009366# ifdef FEAT_INS_EXPAND
9367 /* The popup menu may overlay the window, need to redraw it.
9368 * TODO: Would be more efficient to only redraw the windows that are
9369 * overlapped by the popup menu. */
9370 if (pum_visible() && did_scroll)
9371 {
9372 redraw_all_later(NOT_VALID);
9373 ins_compl_show_pum();
9374 }
9375# endif
9376
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 if (!equalpos(curwin->w_cursor, tpos))
9378 {
9379 start_arrow(&tpos);
9380# ifdef FEAT_CINDENT
9381 can_cindent = TRUE;
9382# endif
9383 }
9384}
9385#endif
9386
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009387#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009389ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009390{
9391 /* We will be leaving the current window, unless closing another tab. */
9392 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9393 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9394 {
9395 undisplay_dollar();
9396 start_arrow(&curwin->w_cursor);
9397# ifdef FEAT_CINDENT
9398 can_cindent = TRUE;
9399# endif
9400 }
9401
9402 if (c == K_TABLINE)
9403 goto_tabpage(current_tab);
9404 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009405 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009406 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009407 redraw_statuslines(); /* will redraw the tabline when needed */
9408 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009409}
9410#endif
9411
9412#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009414ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415{
9416 pos_T tpos;
9417
9418 undisplay_dollar();
9419 tpos = curwin->w_cursor;
9420 if (gui_do_scroll())
9421 {
9422 start_arrow(&tpos);
9423# ifdef FEAT_CINDENT
9424 can_cindent = TRUE;
9425# endif
9426 }
9427}
9428
9429 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009430ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431{
9432 pos_T tpos;
9433
9434 undisplay_dollar();
9435 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009436 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 {
9438 start_arrow(&tpos);
9439# ifdef FEAT_CINDENT
9440 can_cindent = TRUE;
9441# endif
9442 }
9443}
9444#endif
9445
9446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009447ins_left(
9448 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
9450 pos_T tpos;
9451
9452#ifdef FEAT_FOLDING
9453 if ((fdo_flags & FDO_HOR) && KeyTyped)
9454 foldOpenCursor();
9455#endif
9456 undisplay_dollar();
9457 tpos = curwin->w_cursor;
9458 if (oneleft() == OK)
9459 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009460#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9461 /* Only call start_arrow() when not busy with preediting, it will
9462 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9463 if (!im_is_preediting())
9464#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009465 {
9466 start_arrow_with_change(&tpos, end_change);
9467 if (!end_change)
9468 AppendCharToRedobuff(K_LEFT);
9469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470#ifdef FEAT_RIGHTLEFT
9471 /* If exit reversed string, position is fixed */
9472 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9473 revins_legal++;
9474 revins_chars++;
9475#endif
9476 }
9477
9478 /*
9479 * if 'whichwrap' set for cursor in insert mode may go to
9480 * previous line
9481 */
9482 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9483 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009484 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 start_arrow(&tpos);
9486 --(curwin->w_cursor.lnum);
9487 coladvance((colnr_T)MAXCOL);
9488 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9489 }
9490 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009491 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009492 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493}
9494
9495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009496ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497{
9498 pos_T tpos;
9499
9500#ifdef FEAT_FOLDING
9501 if ((fdo_flags & FDO_HOR) && KeyTyped)
9502 foldOpenCursor();
9503#endif
9504 undisplay_dollar();
9505 tpos = curwin->w_cursor;
9506 if (c == K_C_HOME)
9507 curwin->w_cursor.lnum = 1;
9508 curwin->w_cursor.col = 0;
9509#ifdef FEAT_VIRTUALEDIT
9510 curwin->w_cursor.coladd = 0;
9511#endif
9512 curwin->w_curswant = 0;
9513 start_arrow(&tpos);
9514}
9515
9516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009517ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518{
9519 pos_T tpos;
9520
9521#ifdef FEAT_FOLDING
9522 if ((fdo_flags & FDO_HOR) && KeyTyped)
9523 foldOpenCursor();
9524#endif
9525 undisplay_dollar();
9526 tpos = curwin->w_cursor;
9527 if (c == K_C_END)
9528 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9529 coladvance((colnr_T)MAXCOL);
9530 curwin->w_curswant = MAXCOL;
9531
9532 start_arrow(&tpos);
9533}
9534
9535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009536ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537{
9538#ifdef FEAT_FOLDING
9539 if ((fdo_flags & FDO_HOR) && KeyTyped)
9540 foldOpenCursor();
9541#endif
9542 undisplay_dollar();
9543 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9544 {
9545 start_arrow(&curwin->w_cursor);
9546 (void)bck_word(1L, FALSE, FALSE);
9547 curwin->w_set_curswant = TRUE;
9548 }
9549 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009550 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551}
9552
9553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009554ins_right(
9555 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556{
9557#ifdef FEAT_FOLDING
9558 if ((fdo_flags & FDO_HOR) && KeyTyped)
9559 foldOpenCursor();
9560#endif
9561 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009562 if (gchar_cursor() != NUL
9563#ifdef FEAT_VIRTUALEDIT
9564 || virtual_active()
9565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 )
9567 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009568 start_arrow_with_change(&curwin->w_cursor, end_change);
9569 if (!end_change)
9570 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 curwin->w_set_curswant = TRUE;
9572#ifdef FEAT_VIRTUALEDIT
9573 if (virtual_active())
9574 oneright();
9575 else
9576#endif
9577 {
9578#ifdef FEAT_MBYTE
9579 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009580 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 else
9582#endif
9583 ++curwin->w_cursor.col;
9584 }
9585
9586#ifdef FEAT_RIGHTLEFT
9587 revins_legal++;
9588 if (revins_chars)
9589 revins_chars--;
9590#endif
9591 }
9592 /* if 'whichwrap' set for cursor in insert mode, may move the
9593 * cursor to the next line */
9594 else if (vim_strchr(p_ww, ']') != NULL
9595 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9596 {
9597 start_arrow(&curwin->w_cursor);
9598 curwin->w_set_curswant = TRUE;
9599 ++curwin->w_cursor.lnum;
9600 curwin->w_cursor.col = 0;
9601 }
9602 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009603 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009604 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605}
9606
9607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009608ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
9610#ifdef FEAT_FOLDING
9611 if ((fdo_flags & FDO_HOR) && KeyTyped)
9612 foldOpenCursor();
9613#endif
9614 undisplay_dollar();
9615 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9616 || gchar_cursor() != NUL)
9617 {
9618 start_arrow(&curwin->w_cursor);
9619 (void)fwd_word(1L, FALSE, 0);
9620 curwin->w_set_curswant = TRUE;
9621 }
9622 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009623 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624}
9625
9626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009627ins_up(
9628 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629{
9630 pos_T tpos;
9631 linenr_T old_topline = curwin->w_topline;
9632#ifdef FEAT_DIFF
9633 int old_topfill = curwin->w_topfill;
9634#endif
9635
9636 undisplay_dollar();
9637 tpos = curwin->w_cursor;
9638 if (cursor_up(1L, TRUE) == OK)
9639 {
9640 if (startcol)
9641 coladvance(getvcol_nolist(&Insstart));
9642 if (old_topline != curwin->w_topline
9643#ifdef FEAT_DIFF
9644 || old_topfill != curwin->w_topfill
9645#endif
9646 )
9647 redraw_later(VALID);
9648 start_arrow(&tpos);
9649#ifdef FEAT_CINDENT
9650 can_cindent = TRUE;
9651#endif
9652 }
9653 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009654 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655}
9656
9657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009658ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659{
9660 pos_T tpos;
9661
9662 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009663
9664#ifdef FEAT_WINDOWS
9665 if (mod_mask & MOD_MASK_CTRL)
9666 {
9667 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009668 if (first_tabpage->tp_next != NULL)
9669 {
9670 start_arrow(&curwin->w_cursor);
9671 goto_tabpage(-1);
9672 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009673 return;
9674 }
9675#endif
9676
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 tpos = curwin->w_cursor;
9678 if (onepage(BACKWARD, 1L) == OK)
9679 {
9680 start_arrow(&tpos);
9681#ifdef FEAT_CINDENT
9682 can_cindent = TRUE;
9683#endif
9684 }
9685 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009686 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687}
9688
9689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009690ins_down(
9691 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692{
9693 pos_T tpos;
9694 linenr_T old_topline = curwin->w_topline;
9695#ifdef FEAT_DIFF
9696 int old_topfill = curwin->w_topfill;
9697#endif
9698
9699 undisplay_dollar();
9700 tpos = curwin->w_cursor;
9701 if (cursor_down(1L, TRUE) == OK)
9702 {
9703 if (startcol)
9704 coladvance(getvcol_nolist(&Insstart));
9705 if (old_topline != curwin->w_topline
9706#ifdef FEAT_DIFF
9707 || old_topfill != curwin->w_topfill
9708#endif
9709 )
9710 redraw_later(VALID);
9711 start_arrow(&tpos);
9712#ifdef FEAT_CINDENT
9713 can_cindent = TRUE;
9714#endif
9715 }
9716 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009717 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718}
9719
9720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009721ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722{
9723 pos_T tpos;
9724
9725 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009726
9727#ifdef FEAT_WINDOWS
9728 if (mod_mask & MOD_MASK_CTRL)
9729 {
9730 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009731 if (first_tabpage->tp_next != NULL)
9732 {
9733 start_arrow(&curwin->w_cursor);
9734 goto_tabpage(0);
9735 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009736 return;
9737 }
9738#endif
9739
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 tpos = curwin->w_cursor;
9741 if (onepage(FORWARD, 1L) == OK)
9742 {
9743 start_arrow(&tpos);
9744#ifdef FEAT_CINDENT
9745 can_cindent = TRUE;
9746#endif
9747 }
9748 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009749 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750}
9751
9752#ifdef FEAT_DND
9753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009754ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755{
9756 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9757}
9758#endif
9759
9760/*
9761 * Handle TAB in Insert or Replace mode.
9762 * Return TRUE when the TAB needs to be inserted like a normal character.
9763 */
9764 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009765ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
9767 int ind;
9768 int i;
9769 int temp;
9770
9771 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9772 Insstart_blank_vcol = get_nolist_virtcol();
9773 if (echeck_abbr(TAB + ABBR_OFF))
9774 return FALSE;
9775
9776 ind = inindent(0);
9777#ifdef FEAT_CINDENT
9778 if (ind)
9779 can_cindent = FALSE;
9780#endif
9781
9782 /*
9783 * When nothing special, insert TAB like a normal character
9784 */
9785 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009786 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009787 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 return TRUE;
9789
9790 if (stop_arrow() == FAIL)
9791 return TRUE;
9792
9793 did_ai = FALSE;
9794#ifdef FEAT_SMARTINDENT
9795 did_si = FALSE;
9796 can_si = FALSE;
9797 can_si_back = FALSE;
9798#endif
9799 AppendToRedobuff((char_u *)"\t");
9800
9801 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009802 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009803 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9804 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 else /* otherwise use 'tabstop' */
9806 temp = (int)curbuf->b_p_ts;
9807 temp -= get_nolist_virtcol() % temp;
9808
9809 /*
9810 * Insert the first space with ins_char(). It will delete one char in
9811 * replace mode. Insert the rest with ins_str(); it will not delete any
9812 * chars. For VREPLACE mode, we use ins_char() for all characters.
9813 */
9814 ins_char(' ');
9815 while (--temp > 0)
9816 {
9817#ifdef FEAT_VREPLACE
9818 if (State & VREPLACE_FLAG)
9819 ins_char(' ');
9820 else
9821#endif
9822 {
9823 ins_str((char_u *)" ");
9824 if (State & REPLACE_FLAG) /* no char replaced */
9825 replace_push(NUL);
9826 }
9827 }
9828
9829 /*
9830 * When 'expandtab' not set: Replace spaces by TABs where possible.
9831 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009832 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 {
9834 char_u *ptr;
9835#ifdef FEAT_VREPLACE
9836 char_u *saved_line = NULL; /* init for GCC */
9837 pos_T pos;
9838#endif
9839 pos_T fpos;
9840 pos_T *cursor;
9841 colnr_T want_vcol, vcol;
9842 int change_col = -1;
9843 int save_list = curwin->w_p_list;
9844
9845 /*
9846 * Get the current line. For VREPLACE mode, don't make real changes
9847 * yet, just work on a copy of the line.
9848 */
9849#ifdef FEAT_VREPLACE
9850 if (State & VREPLACE_FLAG)
9851 {
9852 pos = curwin->w_cursor;
9853 cursor = &pos;
9854 saved_line = vim_strsave(ml_get_curline());
9855 if (saved_line == NULL)
9856 return FALSE;
9857 ptr = saved_line + pos.col;
9858 }
9859 else
9860#endif
9861 {
9862 ptr = ml_get_cursor();
9863 cursor = &curwin->w_cursor;
9864 }
9865
9866 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9867 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9868 curwin->w_p_list = FALSE;
9869
9870 /* Find first white before the cursor */
9871 fpos = curwin->w_cursor;
9872 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9873 {
9874 --fpos.col;
9875 --ptr;
9876 }
9877
9878 /* In Replace mode, don't change characters before the insert point. */
9879 if ((State & REPLACE_FLAG)
9880 && fpos.lnum == Insstart.lnum
9881 && fpos.col < Insstart.col)
9882 {
9883 ptr += Insstart.col - fpos.col;
9884 fpos.col = Insstart.col;
9885 }
9886
9887 /* compute virtual column numbers of first white and cursor */
9888 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9889 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9890
Bram Moolenaar597a4222014-06-25 14:39:50 +02009891 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9892 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 while (vim_iswhite(*ptr))
9894 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009895 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 if (vcol + i > want_vcol)
9897 break;
9898 if (*ptr != TAB)
9899 {
9900 *ptr = TAB;
9901 if (change_col < 0)
9902 {
9903 change_col = fpos.col; /* Column of first change */
9904 /* May have to adjust Insstart */
9905 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9906 Insstart.col = fpos.col;
9907 }
9908 }
9909 ++fpos.col;
9910 ++ptr;
9911 vcol += i;
9912 }
9913
9914 if (change_col >= 0)
9915 {
9916 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009917 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918
9919 /* Skip over the spaces we need. */
9920 while (vcol < want_vcol && *ptr == ' ')
9921 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009922 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 ++ptr;
9924 ++repl_off;
9925 }
9926 if (vcol > want_vcol)
9927 {
9928 /* Must have a char with 'showbreak' just before it. */
9929 --ptr;
9930 --repl_off;
9931 }
9932 fpos.col += repl_off;
9933
9934 /* Delete following spaces. */
9935 i = cursor->col - fpos.col;
9936 if (i > 0)
9937 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009938 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 /* correct replace stack. */
9940 if ((State & REPLACE_FLAG)
9941#ifdef FEAT_VREPLACE
9942 && !(State & VREPLACE_FLAG)
9943#endif
9944 )
9945 for (temp = i; --temp >= 0; )
9946 replace_join(repl_off);
9947 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009948#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009949 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009950 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009951 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009952 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9953 (char_u *)"\t", 1);
9954 }
9955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 cursor->col -= i;
9957
9958#ifdef FEAT_VREPLACE
9959 /*
9960 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9961 * backspacing over the changed spacing and then inserting the new
9962 * spacing.
9963 */
9964 if (State & VREPLACE_FLAG)
9965 {
9966 /* Backspace from real cursor to change_col */
9967 backspace_until_column(change_col);
9968
9969 /* Insert each char in saved_line from changed_col to
9970 * ptr-cursor */
9971 ins_bytes_len(saved_line + change_col,
9972 cursor->col - change_col);
9973 }
9974#endif
9975 }
9976
9977#ifdef FEAT_VREPLACE
9978 if (State & VREPLACE_FLAG)
9979 vim_free(saved_line);
9980#endif
9981 curwin->w_p_list = save_list;
9982 }
9983
9984 return FALSE;
9985}
9986
9987/*
9988 * Handle CR or NL in insert mode.
9989 * Return TRUE when out of memory or can't undo.
9990 */
9991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009992ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993{
9994 int i;
9995
9996 if (echeck_abbr(c + ABBR_OFF))
9997 return FALSE;
9998 if (stop_arrow() == FAIL)
9999 return TRUE;
10000 undisplay_dollar();
10001
10002 /*
10003 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10004 * character under the cursor. Only push a NUL on the replace stack,
10005 * nothing to put back when the NL is deleted.
10006 */
10007 if ((State & REPLACE_FLAG)
10008#ifdef FEAT_VREPLACE
10009 && !(State & VREPLACE_FLAG)
10010#endif
10011 )
10012 replace_push(NUL);
10013
10014 /*
10015 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10016 * replacing the next line, so we push all of the characters left on the
10017 * line onto the replace stack. This is not done here though, it is done
10018 * in open_line().
10019 */
10020
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010021#ifdef FEAT_VIRTUALEDIT
10022 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10023 * CTRL-O). */
10024 if (virtual_active() && curwin->w_cursor.coladd > 0)
10025 coladvance(getviscol());
10026#endif
10027
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028#ifdef FEAT_RIGHTLEFT
10029# ifdef FEAT_FKMAP
10030 if (p_altkeymap && p_fkmap)
10031 fkmap(NL);
10032# endif
10033 /* NL in reverse insert will always start in the end of
10034 * current line. */
10035 if (revins_on)
10036 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10037#endif
10038
10039 AppendToRedobuff(NL_STR);
10040 i = open_line(FORWARD,
10041#ifdef FEAT_COMMENTS
10042 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10043#endif
10044 0, old_indent);
10045 old_indent = 0;
10046#ifdef FEAT_CINDENT
10047 can_cindent = TRUE;
10048#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010049#ifdef FEAT_FOLDING
10050 /* When inserting a line the cursor line must never be in a closed fold. */
10051 foldOpenCursor();
10052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053
10054 return (!i);
10055}
10056
10057#ifdef FEAT_DIGRAPHS
10058/*
10059 * Handle digraph in insert mode.
10060 * Returns character still to be inserted, or NUL when nothing remaining to be
10061 * done.
10062 */
10063 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010064ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065{
10066 int c;
10067 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010068 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069
10070 pc_status = PC_STATUS_UNSET;
10071 if (redrawing() && !char_avail())
10072 {
10073 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010074 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075
10076 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010077 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078#ifdef FEAT_CMDL_INFO
10079 add_to_showcmd_c(Ctrl_K);
10080#endif
10081 }
10082
10083#ifdef USE_ON_FLY_SCROLL
10084 dont_scroll = TRUE; /* disallow scrolling here */
10085#endif
10086
10087 /* don't map the digraph chars. This also prevents the
10088 * mode message to be deleted when ESC is hit */
10089 ++no_mapping;
10090 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010091 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 --no_mapping;
10093 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010094 if (did_putchar)
10095 /* when the line fits in 'columns' the '?' is at the start of the next
10096 * line and will not be removed by the redraw */
10097 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010098
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 if (IS_SPECIAL(c) || mod_mask) /* special key */
10100 {
10101#ifdef FEAT_CMDL_INFO
10102 clear_showcmd();
10103#endif
10104 insert_special(c, TRUE, FALSE);
10105 return NUL;
10106 }
10107 if (c != ESC)
10108 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010109 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 if (redrawing() && !char_avail())
10111 {
10112 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010113 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114
10115 if (char2cells(c) == 1)
10116 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010117 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010119 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 }
10121#ifdef FEAT_CMDL_INFO
10122 add_to_showcmd_c(c);
10123#endif
10124 }
10125 ++no_mapping;
10126 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010127 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 --no_mapping;
10129 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010130 if (did_putchar)
10131 /* when the line fits in 'columns' the '?' is at the start of the
10132 * next line and will not be removed by a redraw */
10133 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 if (cc != ESC)
10135 {
10136 AppendToRedobuff((char_u *)CTRL_V_STR);
10137 c = getdigraph(c, cc, TRUE);
10138#ifdef FEAT_CMDL_INFO
10139 clear_showcmd();
10140#endif
10141 return c;
10142 }
10143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144#ifdef FEAT_CMDL_INFO
10145 clear_showcmd();
10146#endif
10147 return NUL;
10148}
10149#endif
10150
10151/*
10152 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10153 * Returns the char to be inserted, or NUL if none found.
10154 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010155 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010156ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157{
10158 int c;
10159 int temp;
10160 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010161 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162
10163 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10164 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010165 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 return NUL;
10167 }
10168
10169 /* try to advance to the cursor column */
10170 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010171 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 prev_ptr = ptr;
10173 validate_virtcol();
10174 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10175 {
10176 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010177 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 }
10179 if ((colnr_T)temp > curwin->w_virtcol)
10180 ptr = prev_ptr;
10181
10182#ifdef FEAT_MBYTE
10183 c = (*mb_ptr2char)(ptr);
10184#else
10185 c = *ptr;
10186#endif
10187 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010188 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 return c;
10190}
10191
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010192/*
10193 * CTRL-Y or CTRL-E typed in Insert mode.
10194 */
10195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010196ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010197{
10198 int c = tc;
10199
10200#ifdef FEAT_INS_EXPAND
10201 if (ctrl_x_mode == CTRL_X_SCROLL)
10202 {
10203 if (c == Ctrl_Y)
10204 scrolldown_clamp();
10205 else
10206 scrollup_clamp();
10207 redraw_later(VALID);
10208 }
10209 else
10210#endif
10211 {
10212 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10213 if (c != NUL)
10214 {
10215 long tw_save;
10216
10217 /* The character must be taken literally, insert like it
10218 * was typed after a CTRL-V, and pretend 'textwidth'
10219 * wasn't set. Digits, 'o' and 'x' are special after a
10220 * CTRL-V, don't use it for these. */
10221 if (c < 256 && !isalnum(c))
10222 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10223 tw_save = curbuf->b_p_tw;
10224 curbuf->b_p_tw = -1;
10225 insert_special(c, TRUE, FALSE);
10226 curbuf->b_p_tw = tw_save;
10227#ifdef FEAT_RIGHTLEFT
10228 revins_chars++;
10229 revins_legal++;
10230#endif
10231 c = Ctrl_V; /* pretend CTRL-V is last character */
10232 auto_format(FALSE, TRUE);
10233 }
10234 }
10235 return c;
10236}
10237
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238#ifdef FEAT_SMARTINDENT
10239/*
10240 * Try to do some very smart auto-indenting.
10241 * Used when inserting a "normal" character.
10242 */
10243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010244ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245{
10246 pos_T *pos, old_pos;
10247 char_u *ptr;
10248 int i;
10249 int temp;
10250
10251 /*
10252 * do some very smart indenting when entering '{' or '}'
10253 */
10254 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10255 {
10256 /*
10257 * for '}' set indent equal to indent of line containing matching '{'
10258 */
10259 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10260 {
10261 old_pos = curwin->w_cursor;
10262 /*
10263 * If the matching '{' has a ')' immediately before it (ignoring
10264 * white-space), then line up with the start of the line
10265 * containing the matching '(' if there is one. This handles the
10266 * case where an "if (..\n..) {" statement continues over multiple
10267 * lines -- webb
10268 */
10269 ptr = ml_get(pos->lnum);
10270 i = pos->col;
10271 if (i > 0) /* skip blanks before '{' */
10272 while (--i > 0 && vim_iswhite(ptr[i]))
10273 ;
10274 curwin->w_cursor.lnum = pos->lnum;
10275 curwin->w_cursor.col = i;
10276 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10277 curwin->w_cursor = *pos;
10278 i = get_indent();
10279 curwin->w_cursor = old_pos;
10280#ifdef FEAT_VREPLACE
10281 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010282 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 else
10284#endif
10285 (void)set_indent(i, SIN_CHANGED);
10286 }
10287 else if (curwin->w_cursor.col > 0)
10288 {
10289 /*
10290 * when inserting '{' after "O" reduce indent, but not
10291 * more than indent of previous line
10292 */
10293 temp = TRUE;
10294 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10295 {
10296 old_pos = curwin->w_cursor;
10297 i = get_indent();
10298 while (curwin->w_cursor.lnum > 1)
10299 {
10300 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10301
10302 /* ignore empty lines and lines starting with '#'. */
10303 if (*ptr != '#' && *ptr != NUL)
10304 break;
10305 }
10306 if (get_indent() >= i)
10307 temp = FALSE;
10308 curwin->w_cursor = old_pos;
10309 }
10310 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010311 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 }
10313 }
10314
10315 /*
10316 * set indent of '#' always to 0
10317 */
10318 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10319 {
10320 /* remember current indent for next line */
10321 old_indent = get_indent();
10322 (void)set_indent(0, SIN_CHANGED);
10323 }
10324
10325 /* Adjust ai_col, the char at this position can be deleted. */
10326 if (ai_col > curwin->w_cursor.col)
10327 ai_col = curwin->w_cursor.col;
10328}
10329#endif
10330
10331/*
10332 * Get the value that w_virtcol would have when 'list' is off.
10333 * Unless 'cpo' contains the 'L' flag.
10334 */
10335 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010336get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337{
10338 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10339 return getvcol_nolist(&curwin->w_cursor);
10340 validate_virtcol();
10341 return curwin->w_virtcol;
10342}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010343
10344#ifdef FEAT_AUTOCMD
10345/*
10346 * Handle the InsertCharPre autocommand.
10347 * "c" is the character that was typed.
10348 * Return a pointer to allocated memory with the replacement string.
10349 * Return NULL to continue inserting "c".
10350 */
10351 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010352do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010353{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010354 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010355 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010356
10357 /* Return quickly when there is nothing to do. */
10358 if (!has_insertcharpre())
10359 return NULL;
10360
Bram Moolenaar704984a2012-06-01 14:57:51 +020010361#ifdef FEAT_MBYTE
10362 if (has_mbyte)
10363 buf[(*mb_char2bytes)(c, buf)] = NUL;
10364 else
10365#endif
10366 {
10367 buf[0] = c;
10368 buf[1] = NUL;
10369 }
10370
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010371 /* Lock the text to avoid weird things from happening. */
10372 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010373 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010374
Bram Moolenaar704984a2012-06-01 14:57:51 +020010375 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010376 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010377 {
10378 /* Get the value of v:char. It may be empty or more than one
10379 * character. Only use it when changed, otherwise continue with the
10380 * original character to avoid breaking autoindent. */
10381 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10382 res = vim_strsave(get_vim_var_str(VV_CHAR));
10383 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010384
10385 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10386 --textlock;
10387
10388 return res;
10389}
10390#endif