blob: 1164e2032a5aeb3ea9a2444718c21af88f0f5816 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaare4214502015-03-05 18:08:43 +010037#define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
Bram Moolenaare4214502015-03-05 18:08:43 +010040#define CTRL_X_MODE_LINE_OR_EVAL(m) (m == CTRL_X_WHOLE_LINE || m == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42static char *ctrl_x_msgs[] =
43{
44 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000046 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 N_(" Whole line completion (^L^N^P)"),
48 N_(" File name completion (^F^N^P)"),
49 N_(" Tag completion (^]^N^P)"),
50 N_(" Path pattern completion (^N^P)"),
51 N_(" Definition completion (^D^N^P)"),
52 NULL,
53 N_(" Dictionary completion (^K^N^P)"),
54 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000055 N_(" Command-line completion (^V^N^P)"),
56 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000057 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000058 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000059 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010060 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061};
62
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000063static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010064#ifdef FEAT_COMPL_FUNC
65static char e_complwin[] = N_("E839: Completion function changed window");
66static char e_compldel[] = N_("E840: Completion function deleted text");
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
69/*
70 * Structure used to store one match for insert completion.
71 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000072typedef struct compl_S compl_T;
73struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000074{
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 compl_T *cp_next;
76 compl_T *cp_prev;
77 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000078 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000079 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000080 char_u *cp_fname; /* file containing the match, allocated when
81 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000082 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
83 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084};
85
Bram Moolenaar572cb562005-08-05 21:35:02 +000086#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#define FREE_FNAME (2)
88
89/*
90 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000091 * "compl_first_match" points to the start of the list.
92 * "compl_curr_match" points to the currently selected entry.
93 * "compl_shown_match" is different from compl_curr_match during
94 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000096static compl_T *compl_first_match = NULL;
97static compl_T *compl_curr_match = NULL;
98static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000100/* After using a cursor key <Enter> selects a match in the popup menu,
101 * otherwise it inserts a line break. */
102static int compl_enter_selects = FALSE;
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104/* When "compl_leader" is not NULL only matches that start with this string
105 * are used. */
106static char_u *compl_leader = NULL;
107
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000108static int compl_get_longest = FALSE; /* put longest common string
109 in compl_leader */
110
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200111static int compl_no_insert = FALSE; /* FALSE: select & insert
112 TRUE: noinsert */
113static int compl_no_select = FALSE; /* FALSE: select & insert
114 TRUE: noselect */
115
Bram Moolenaara6557602006-02-04 22:43:20 +0000116static int compl_used_match; /* Selected one of the matches. When
117 FALSE the match was edited or using
118 the longest common string. */
119
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000120static int compl_was_interrupted = FALSE; /* didn't finish finding
121 completions. */
122
123static int compl_restarting = FALSE; /* don't insert match */
124
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000125/* When the first completion is done "compl_started" is set. When it's
126 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000127static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000129/* Set when doing something for completion that may call edit() recursively,
130 * which is not allowed. */
131static int compl_busy = FALSE;
132
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static int compl_matches = 0;
134static char_u *compl_pattern = NULL;
135static int compl_direction = FORWARD;
136static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000137static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000138static pos_T compl_startpos;
139static colnr_T compl_col = 0; /* column where the text starts
140 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000141static char_u *compl_orig_text = NULL; /* text as it was before
142 * completion started */
143static int compl_cont_mode = 0;
144static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar82139082011-09-14 16:52:09 +0200146static int compl_opt_refresh_always = FALSE;
147
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100148static void ins_ctrl_x(void);
149static int has_compl_option(int dict_opt);
150static int ins_compl_accept_char(int c);
151static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
152static int ins_compl_equal(compl_T *match, char_u *str, int len);
153static void ins_compl_longest_match(compl_T *match);
154static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
155static int ins_compl_make_cyclic(void);
156static void ins_compl_upd_pum(void);
157static void ins_compl_del_pum(void);
158static int pum_wanted(void);
159static int pum_enough_matches(void);
160static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
161static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
162static char_u *find_line_end(char_u *ptr);
163static void ins_compl_free(void);
164static void ins_compl_clear(void);
165static int ins_compl_bs(void);
166static int ins_compl_need_restart(void);
167static void ins_compl_new_leader(void);
168static void ins_compl_addleader(int c);
169static int ins_compl_len(void);
170static void ins_compl_restart(void);
171static void ins_compl_set_original_text(char_u *str);
172static void ins_compl_addfrommatch(void);
173static int ins_compl_prep(int c);
174static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
175static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000176#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100177static void ins_compl_add_list(list_T *list);
178static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000179#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static int ins_compl_get_exp(pos_T *ini);
181static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200182static void ins_compl_insert(int in_compl_func);
183static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100184static int ins_compl_key2dir(int c);
185static int ins_compl_pum_key(int c);
186static int ins_compl_key2count(int c);
187static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100188static int ins_complete(int c, int enable_pum);
189static void show_pum(int save_w_wrow);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#endif /* FEAT_INS_EXPAND */
192
193#define BACKSPACE_CHAR 1
194#define BACKSPACE_WORD 2
195#define BACKSPACE_WORD_NOT_SPACE 3
196#define BACKSPACE_LINE 4
197
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100198static void ins_redraw(int ready);
199static void ins_ctrl_v(void);
200static void undisplay_dollar(void);
201static void insert_special(int, int, int);
202static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
203static void check_auto_format(int);
204static void redo_literal(int c);
205static void start_arrow(pos_T *end_insert_pos);
206static void start_arrow_with_change(pos_T *end_insert_pos, int change);
207static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000208#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void check_spell_redraw(void);
210static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000211static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
214static int echeck_abbr(int);
215static int replace_pop(void);
216static void replace_join(int off);
217static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void replace_flush(void);
222static void replace_do_bs(int limit_col);
223static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ins_reg(void);
228static void ins_ctrl_g(void);
229static void ins_ctrl_hat(void);
230static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100234static int ins_start_select(int c);
235static void ins_insert(int replaceState);
236static void ins_ctrl_o(void);
237static void ins_shift(int c, int lastc);
238static void ins_del(void);
239static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_mouse(int c);
242static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000244#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000246#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ins_left(int end_change);
248static void ins_home(int c);
249static void ins_end(int c);
250static void ins_s_left(void);
251static void ins_right(int end_change);
252static void ins_s_right(void);
253static void ins_up(int startcol);
254static void ins_pageup(void);
255static void ins_down(int startcol);
256static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static int ins_tab(void);
261static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100270#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274static colnr_T Insstart_textlen; /* length of line when insert started */
275static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100276static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
278static char_u *last_insert = NULL; /* the text of the previous insert,
279 K_SPECIAL and CSI are escaped */
280static int last_insert_skip; /* nr of chars in front of previous insert */
281static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000282static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284#ifdef FEAT_CINDENT
285static int can_cindent; /* may do cindenting on this line */
286#endif
287
288static int old_indent = 0; /* for ^^D command in insert mode */
289
290#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000291static int revins_on; /* reverse insert mode on */
292static int revins_chars; /* how much to skip after edit */
293static int revins_legal; /* was the last char 'legal'? */
294static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static int ins_need_undo; /* call u_save() before inserting a
298 char. Set when edit() is called.
299 after that arrow_used is used. */
300
301static int did_add_space = FALSE; /* auto_format() added an extra space
302 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200303static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
304 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
306/*
307 * edit(): Start inserting text.
308 *
309 * "cmdchar" can be:
310 * 'i' normal insert command
311 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100312 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 * 'R' replace command
314 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
315 * but still only one <CR> is inserted. The <Esc> is not used for redo.
316 * 'g' "gI" command.
317 * 'V' "gR" command for Virtual Replace mode.
318 * 'v' "gr" command for single character Virtual Replace mode.
319 *
320 * This function is not called recursively. For CTRL-O commands, it returns
321 * and lets the caller handle the Normal-mode command.
322 *
323 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
324 */
325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100326edit(
327 int cmdchar,
328 int startln, /* if set, insert at start of line */
329 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330{
331 int c = 0;
332 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100333 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000334 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 int i;
337 int did_backspace = TRUE; /* previous char was backspace */
338#ifdef FEAT_CINDENT
339 int line_is_white = FALSE; /* line is empty before insert */
340#endif
341 linenr_T old_topline = 0; /* topline before insertion */
342#ifdef FEAT_DIFF
343 int old_topfill = -1;
344#endif
345 int inserted_space = FALSE; /* just inserted a space */
346 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000347 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000349 /* Remember whether editing was restarted after CTRL-O. */
350 did_restart_edit = restart_edit;
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 /* sleep before redrawing, needed for "CTRL-O :" that results in an
353 * error message */
354 check_for_delay(TRUE);
355
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100356 /* set Insstart_orig to Insstart */
357 update_Insstart_orig = TRUE;
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef HAVE_SANDBOX
360 /* Don't allow inserting in the sandbox. */
361 if (sandbox != 0)
362 {
363 EMSG(_(e_sandbox));
364 return FALSE;
365 }
366#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000367 /* Don't allow changes in the buffer while editing the cmdline. The
368 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000369 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000370 {
371 EMSG(_(e_secure));
372 return FALSE;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000376 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000377 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000378 {
379 EMSG(_(e_secure));
380 return FALSE;
381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 ins_compl_clear(); /* clear stuff for CTRL-X mode */
383#endif
384
Bram Moolenaar843ee412004-06-30 16:16:41 +0000385#ifdef FEAT_AUTOCMD
386 /*
387 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
388 */
389 if (cmdchar != 'r' && cmdchar != 'v')
390 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100391 pos_T save_cursor = curwin->w_cursor;
392
Bram Moolenaar1e015462005-09-25 22:16:38 +0000393# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000394 if (cmdchar == 'R')
395 ptr = (char_u *)"r";
396 else if (cmdchar == 'V')
397 ptr = (char_u *)"v";
398 else
399 ptr = (char_u *)"i";
400 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200401 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000402# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000403 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100404
Bram Moolenaar097c9922013-05-19 21:15:15 +0200405 /* Make sure the cursor didn't move. Do call check_cursor_col() in
406 * case the text was modified. Since Insert mode was not started yet
407 * a call to check_cursor_col() may move the cursor, especially with
408 * the "A" command, thus set State to avoid that. Also check that the
409 * line number is still valid (lines may have been deleted).
410 * Do not restore if v:char was set to a non-empty string. */
411 if (!equalpos(curwin->w_cursor, save_cursor)
412# ifdef FEAT_EVAL
413 && *get_vim_var_str(VV_CHAR) == NUL
414# endif
415 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100416 {
417 int save_state = State;
418
419 curwin->w_cursor = save_cursor;
420 State = INSERT;
421 check_cursor_col();
422 State = save_state;
423 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000424 }
425#endif
426
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200427#ifdef FEAT_CONCEAL
428 /* Check if the cursor line needs redrawing before changing State. If
429 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200430 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200431#endif
432
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433#ifdef FEAT_MOUSE
434 /*
435 * When doing a paste with the middle mouse button, Insstart is set to
436 * where the paste started.
437 */
438 if (where_paste_started.lnum != 0)
439 Insstart = where_paste_started;
440 else
441#endif
442 {
443 Insstart = curwin->w_cursor;
444 if (startln)
445 Insstart.col = 0;
446 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000447 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 Insstart_blank_vcol = MAXCOL;
449 if (!did_ai)
450 ai_col = 0;
451
452 if (cmdchar != NUL && restart_edit == 0)
453 {
454 ResetRedobuff();
455 AppendNumberToRedobuff(count);
456#ifdef FEAT_VREPLACE
457 if (cmdchar == 'V' || cmdchar == 'v')
458 {
459 /* "gR" or "gr" command */
460 AppendCharToRedobuff('g');
461 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
462 }
463 else
464#endif
465 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100466 if (cmdchar == K_PS)
467 AppendCharToRedobuff('a');
468 else
469 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 if (cmdchar == 'g') /* "gI" command */
471 AppendCharToRedobuff('I');
472 else if (cmdchar == 'r') /* "r<CR>" command */
473 count = 1; /* insert only one <CR> */
474 }
475 }
476
477 if (cmdchar == 'R')
478 {
479#ifdef FEAT_FKMAP
480 if (p_fkmap && p_ri)
481 {
482 beep_flush();
483 EMSG(farsi_text_3); /* encoded in Farsi */
484 State = INSERT;
485 }
486 else
487#endif
488 State = REPLACE;
489 }
490#ifdef FEAT_VREPLACE
491 else if (cmdchar == 'V' || cmdchar == 'v')
492 {
493 State = VREPLACE;
494 replaceState = VREPLACE;
495 orig_line_count = curbuf->b_ml.ml_line_count;
496 vr_lines_changed = 1;
497 }
498#endif
499 else
500 State = INSERT;
501
502 stop_insert_mode = FALSE;
503
504 /*
505 * Need to recompute the cursor position, it might move when the cursor is
506 * on a TAB or special character.
507 */
508 curs_columns(TRUE);
509
510 /*
511 * Enable langmap or IME, indicated by 'iminsert'.
512 * Note that IME may enabled/disabled without us noticing here, thus the
513 * 'iminsert' value may not reflect what is actually used. It is updated
514 * when hitting <Esc>.
515 */
516 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
517 State |= LANGMAP;
518#ifdef USE_IM_CONTROL
519 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
520#endif
521
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522#ifdef FEAT_MOUSE
523 setmouse();
524#endif
525#ifdef FEAT_CMDL_INFO
526 clear_showcmd();
527#endif
528#ifdef FEAT_RIGHTLEFT
529 /* there is no reverse replace mode */
530 revins_on = (State == INSERT && p_ri);
531 if (revins_on)
532 undisplay_dollar();
533 revins_chars = 0;
534 revins_legal = 0;
535 revins_scol = -1;
536#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100537 if (!p_ek)
538 /* Disable bracketed paste mode, we won't recognize the escape
539 * sequences. */
540 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542 /*
543 * Handle restarting Insert mode.
544 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
545 * restart_edit non-zero, and something in the stuff buffer.
546 */
547 if (restart_edit != 0 && stuff_empty())
548 {
549#ifdef FEAT_MOUSE
550 /*
551 * After a paste we consider text typed to be part of the insert for
552 * the pasted text. You can backspace over the pasted text too.
553 */
554 if (where_paste_started.lnum)
555 arrow_used = FALSE;
556 else
557#endif
558 arrow_used = TRUE;
559 restart_edit = 0;
560
561 /*
562 * If the cursor was after the end-of-line before the CTRL-O and it is
563 * now at the end-of-line, put it after the end-of-line (this is not
564 * correct in very rare cases).
565 * Also do this if curswant is greater than the current virtual
566 * column. Eg after "^O$" or "^O80|".
567 */
568 validate_virtcol();
569 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000570 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 || curwin->w_curswant > curwin->w_virtcol)
572 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
573 {
574 if (ptr[1] == NUL)
575 ++curwin->w_cursor.col;
576#ifdef FEAT_MBYTE
577 else if (has_mbyte)
578 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000579 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (ptr[i] == NUL)
581 curwin->w_cursor.col += i;
582 }
583#endif
584 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000585 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 else
588 arrow_used = FALSE;
589
590 /* we are in insert mode now, don't need to start it anymore */
591 need_start_insertmode = FALSE;
592
593 /* Need to save the line for undo before inserting the first char. */
594 ins_need_undo = TRUE;
595
596#ifdef FEAT_MOUSE
597 where_paste_started.lnum = 0;
598#endif
599#ifdef FEAT_CINDENT
600 can_cindent = TRUE;
601#endif
602#ifdef FEAT_FOLDING
603 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
604 * restarting. */
605 if (!p_im && did_restart_edit == 0)
606 foldOpenCursor();
607#endif
608
609 /*
610 * If 'showmode' is set, show the current (insert/replace/..) mode.
611 * A warning message for changing a readonly file is given here, before
612 * actually changing anything. It's put after the mode, if any.
613 */
614 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000615 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 i = showmode();
617
618 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000619 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621#ifdef CURSOR_SHAPE
622 ui_cursor_shape(); /* may show different cursor shape */
623#endif
624#ifdef FEAT_DIGRAPHS
625 do_digraph(-1); /* clear digraphs */
626#endif
627
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000628 /*
629 * Get the current length of the redo buffer, those characters have to be
630 * skipped if we want to get to the inserted characters.
631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 ptr = get_inserted();
633 if (ptr == NULL)
634 new_insert_skip = 0;
635 else
636 {
637 new_insert_skip = (int)STRLEN(ptr);
638 vim_free(ptr);
639 }
640
641 old_indent = 0;
642
643 /*
644 * Main loop in Insert mode: repeat until Insert mode is left.
645 */
646 for (;;)
647 {
648#ifdef FEAT_RIGHTLEFT
649 if (!revins_legal)
650 revins_scol = -1; /* reset on illegal motions */
651 else
652 revins_legal = 0;
653#endif
654 if (arrow_used) /* don't repeat insert when arrow key used */
655 count = 0;
656
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100657 if (update_Insstart_orig)
658 Insstart_orig = Insstart;
659
Bram Moolenaar00672e12016-06-26 18:38:13 +0200660 if (stop_insert_mode
661#ifdef FEAT_INS_EXPAND
662 && !pum_visible()
663#endif
664 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 {
666 /* ":stopinsert" used or 'insertmode' reset */
667 count = 0;
668 goto doESCkey;
669 }
670
671 /* set curwin->w_curswant for next K_DOWN or K_UP */
672 if (!arrow_used)
673 curwin->w_set_curswant = TRUE;
674
675 /* If there is no typeahead may check for timestamps (e.g., for when a
676 * menu invoked a shell command). */
677 if (stuff_empty())
678 {
679 did_check_timestamps = FALSE;
680 if (need_check_timestamps)
681 check_timestamps(FALSE);
682 }
683
684 /*
685 * When emsg() was called msg_scroll will have been set.
686 */
687 msg_scroll = FALSE;
688
689#ifdef FEAT_GUI
690 /* When 'mousefocus' is set a mouse movement may have taken us to
691 * another window. "need_mouse_correct" may then be set because of an
692 * autocommand. */
693 if (need_mouse_correct)
694 gui_mouse_correct();
695#endif
696
697#ifdef FEAT_FOLDING
698 /* Open fold at the cursor line, according to 'foldopen'. */
699 if (fdo_flags & FDO_INSERT)
700 foldOpenCursor();
701 /* Close folds where the cursor isn't, according to 'foldclose' */
702 if (!char_avail())
703 foldCheckClose();
704#endif
705
706 /*
707 * If we inserted a character at the last position of the last line in
708 * the window, scroll the window one line up. This avoids an extra
709 * redraw.
710 * This is detected when the cursor column is smaller after inserting
711 * something.
712 * Don't do this when the topline changed already, it has
713 * already been adjusted (by insertchar() calling open_line())).
714 */
715 if (curbuf->b_mod_set
716 && curwin->w_p_wrap
717 && !did_backspace
718 && curwin->w_topline == old_topline
719#ifdef FEAT_DIFF
720 && curwin->w_topfill == old_topfill
721#endif
722 )
723 {
724 mincol = curwin->w_wcol;
725 validate_cursor_col();
726
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000727 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 && curwin->w_wrow == W_WINROW(curwin)
729 + curwin->w_height - 1 - p_so
730 && (curwin->w_cursor.lnum != curwin->w_topline
731#ifdef FEAT_DIFF
732 || curwin->w_topfill > 0
733#endif
734 ))
735 {
736#ifdef FEAT_DIFF
737 if (curwin->w_topfill > 0)
738 --curwin->w_topfill;
739 else
740#endif
741#ifdef FEAT_FOLDING
742 if (hasFolding(curwin->w_topline, NULL, &old_topline))
743 set_topline(curwin, old_topline + 1);
744 else
745#endif
746 set_topline(curwin, curwin->w_topline + 1);
747 }
748 }
749
750 /* May need to adjust w_topline to show the cursor. */
751 update_topline();
752
753 did_backspace = FALSE;
754
755 validate_cursor(); /* may set must_redraw */
756
757 /*
758 * Redraw the display when no characters are waiting.
759 * Also shows mode, ruler and positions cursor.
760 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000761 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
763#ifdef FEAT_SCROLLBIND
764 if (curwin->w_p_scb)
765 do_check_scrollbind(TRUE);
766#endif
767
Bram Moolenaar860cae12010-06-05 23:22:07 +0200768#ifdef FEAT_CURSORBIND
769 if (curwin->w_p_crb)
770 do_check_cursorbind();
771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 update_curswant();
773 old_topline = curwin->w_topline;
774#ifdef FEAT_DIFF
775 old_topfill = curwin->w_topfill;
776#endif
777
778#ifdef USE_ON_FLY_SCROLL
779 dont_scroll = FALSE; /* allow scrolling here */
780#endif
781
782 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000783 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100785 if (c != K_CURSORHOLD)
786 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200787
788 /* After using CTRL-G U the next cursor key will not break undo. */
789 if (dont_sync_undo == MAYBE)
790 dont_sync_undo = TRUE;
791 else
792 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100793 if (cmdchar == K_PS)
794 /* Got here from normal mode when bracketed paste started. */
795 c = K_PS;
796 else
797 do
798 {
799 c = safe_vgetc();
800 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000802#ifdef FEAT_AUTOCMD
803 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
804 did_cursorhold = TRUE;
805#endif
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#ifdef FEAT_RIGHTLEFT
808 if (p_hkmap && KeyTyped)
809 c = hkmap(c); /* Hebrew mode mapping */
810#endif
811#ifdef FEAT_FKMAP
812 if (p_fkmap && KeyTyped)
813 c = fkmap(c); /* Farsi mode mapping */
814#endif
815
816#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000817 /*
818 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000819 * and the cursor is still in the completed word. Only when there is
820 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000821 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000822 if (compl_started
823 && pum_wanted()
824 && curwin->w_cursor.col >= compl_col
825 && (compl_shown_match == NULL
826 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000827 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000828 /* BS: Delete one character from "compl_leader". */
829 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000830 && curwin->w_cursor.col > compl_col
831 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000832 continue;
833
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000834 /* When no match was selected or it was edited. */
835 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000836 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000837 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000838 * "compl_leader". Except when at the original match and
839 * there is nothing to add, CTRL-L works like CTRL-P then. */
840 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100841 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000842 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000843 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 {
845 ins_compl_addfrommatch();
846 continue;
847 }
848
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000849 /* A non-white character that fits in with the current
850 * completion: Add to "compl_leader". */
851 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000852 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100853#ifdef FEAT_AUTOCMD
854 /* Trigger InsertCharPre. */
855 char_u *str = do_insert_char_pre(c);
856 char_u *p;
857
858 if (str != NULL)
859 {
860 for (p = str; *p != NUL; mb_ptr_adv(p))
861 ins_compl_addleader(PTR2CHAR(p));
862 vim_free(str);
863 }
864 else
865#endif
866 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000867 continue;
868 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000869
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000870 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000871 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200872 if ((c == Ctrl_Y || (compl_enter_selects
873 && (c == CAR || c == K_KENTER || c == NL)))
874 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000875 {
876 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200877 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000878 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000879 }
880 }
881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
883 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000884 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000885 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000886 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#endif
888
Bram Moolenaar488c6512005-08-11 20:09:58 +0000889 /* CTRL-\ CTRL-N goes to Normal mode,
890 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
891 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 if (c == Ctrl_BSL)
893 {
894 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000895 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 ++no_mapping;
897 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000898 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 --no_mapping;
900 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000901 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000903 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 vungetc(c);
905 c = Ctrl_BSL;
906 }
907 else if (c == Ctrl_G && p_im)
908 continue;
909 else
910 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000911 if (c == Ctrl_O)
912 {
913 ins_ctrl_o();
914 ins_at_eol = FALSE; /* cursor keeps its column */
915 nomove = TRUE;
916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 count = 0;
918 goto doESCkey;
919 }
920 }
921
922#ifdef FEAT_DIGRAPHS
923 c = do_digraph(c);
924#endif
925
926#ifdef FEAT_INS_EXPAND
927 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
928 goto docomplete;
929#endif
930 if (c == Ctrl_V || c == Ctrl_Q)
931 {
932 ins_ctrl_v();
933 c = Ctrl_V; /* pretend CTRL-V is last typed character */
934 continue;
935 }
936
937#ifdef FEAT_CINDENT
938 if (cindent_on()
939# ifdef FEAT_INS_EXPAND
940 && ctrl_x_mode == 0
941# endif
942 )
943 {
944 /* A key name preceded by a bang means this key is not to be
945 * inserted. Skip ahead to the re-indenting below.
946 * A key name preceded by a star means that indenting has to be
947 * done before inserting the key. */
948 line_is_white = inindent(0);
949 if (in_cinkeys(c, '!', line_is_white))
950 goto force_cindent;
951 if (can_cindent && in_cinkeys(c, '*', line_is_white)
952 && stop_arrow() == OK)
953 do_c_expr_indent();
954 }
955#endif
956
957#ifdef FEAT_RIGHTLEFT
958 if (curwin->w_p_rl)
959 switch (c)
960 {
961 case K_LEFT: c = K_RIGHT; break;
962 case K_S_LEFT: c = K_S_RIGHT; break;
963 case K_C_LEFT: c = K_C_RIGHT; break;
964 case K_RIGHT: c = K_LEFT; break;
965 case K_S_RIGHT: c = K_S_LEFT; break;
966 case K_C_RIGHT: c = K_C_LEFT; break;
967 }
968#endif
969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 /*
971 * If 'keymodel' contains "startsel", may start selection. If it
972 * does, a CTRL-O and c will be stuffed, we need to get these
973 * characters.
974 */
975 if (ins_start_select(c))
976 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 /*
979 * The big switch to handle a character in insert mode.
980 */
981 switch (c)
982 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000983 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (echeck_abbr(ESC + ABBR_OFF))
985 break;
986 /*FALLTHROUGH*/
987
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#ifdef FEAT_CMDWIN
990 if (c == Ctrl_C && cmdwin_type != 0)
991 {
992 /* Close the cmdline window. */
993 cmdwin_result = K_IGNORE;
994 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000995 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 goto doESCkey;
997 }
998#endif
999
1000#ifdef UNIX
1001do_intr:
1002#endif
1003 /* when 'insertmode' set, and not halfway a mapping, don't leave
1004 * Insert mode */
1005 if (goto_im())
1006 {
1007 if (got_int)
1008 {
1009 (void)vgetc(); /* flush all buffers */
1010 got_int = FALSE;
1011 }
1012 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001013 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 break;
1015 }
1016doESCkey:
1017 /*
1018 * This is the ONLY return from edit()!
1019 */
1020 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1021 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001022 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 o_lnum = curwin->w_cursor.lnum;
1024
Bram Moolenaar488c6512005-08-11 20:09:58 +00001025 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001026 {
1027#ifdef FEAT_AUTOCMD
1028 if (cmdchar != 'r' && cmdchar != 'v')
1029 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1030 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001031 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 continue;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case Ctrl_Z: /* suspend when 'insertmode' set */
1038 if (!p_im)
1039 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001040 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001041 c = Ctrl_O;
1042 /*FALLTHROUGH*/
1043
1044 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001045#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001046 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001047 goto docomplete;
1048#endif
1049 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1050 break;
1051 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001052
1053#ifdef FEAT_VIRTUALEDIT
1054 /* don't move the cursor left when 'virtualedit' has "onemore". */
1055 if (ve_flags & VE_ONEMORE)
1056 {
1057 ins_at_eol = FALSE;
1058 nomove = TRUE;
1059 }
1060#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 count = 0;
1062 goto doESCkey;
1063
Bram Moolenaar572cb562005-08-05 21:35:02 +00001064 case K_INS: /* toggle insert/replace mode */
1065 case K_KINS:
1066 ins_insert(replaceState);
1067 break;
1068
1069 case K_SELECT: /* end of Select mode mapping - ignore */
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case K_HELP: /* Help key works like <ESC> <Help> */
1073 case K_F1:
1074 case K_XF1:
1075 stuffcharReadbuff(K_HELP);
1076 if (p_im)
1077 need_start_insertmode = TRUE;
1078 goto doESCkey;
1079
1080#ifdef FEAT_NETBEANS_INTG
1081 case K_F21: /* NetBeans command */
1082 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001083 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001084 --no_mapping;
1085 netbeans_keycommand(i);
1086 break;
1087#endif
1088
1089 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 case NUL:
1091 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 /* For ^@ the trailing ESC will end the insert, unless there is an
1093 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1095 && c != Ctrl_A && !p_im)
1096 goto doESCkey; /* quit insert mode */
1097 inserted_space = FALSE;
1098 break;
1099
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 ins_reg();
1102 auto_format(FALSE, TRUE);
1103 inserted_space = FALSE;
1104 break;
1105
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001106 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 ins_ctrl_g();
1108 break;
1109
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001110 case Ctrl_HAT: /* switch input mode and/or langmap */
1111 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 break;
1113
1114#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (!p_ari)
1117 goto normalchar;
1118 ins_ctrl_();
1119 break;
1120#endif
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1124 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1125 goto docomplete;
1126#endif
1127 /* FALLTHROUGH */
1128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001129 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130# ifdef FEAT_INS_EXPAND
1131 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1132 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001133 if (has_compl_option(FALSE))
1134 goto docomplete;
1135 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
1137# endif
1138 ins_shift(c, lastc);
1139 auto_format(FALSE, TRUE);
1140 inserted_space = FALSE;
1141 break;
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 case K_KDEL:
1145 ins_del();
1146 auto_format(FALSE, TRUE);
1147 break;
1148
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001149 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 case Ctrl_H:
1151 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1152 auto_format(FALSE, TRUE);
1153 break;
1154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1157 auto_format(FALSE, TRUE);
1158 break;
1159
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001160 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001161# ifdef FEAT_COMPL_FUNC
1162 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001164 goto docomplete;
1165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1167 auto_format(FALSE, TRUE);
1168 inserted_space = FALSE;
1169 break;
1170
1171#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001172 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 case K_LEFTMOUSE_NM:
1174 case K_LEFTDRAG:
1175 case K_LEFTRELEASE:
1176 case K_LEFTRELEASE_NM:
1177 case K_MIDDLEMOUSE:
1178 case K_MIDDLEDRAG:
1179 case K_MIDDLERELEASE:
1180 case K_RIGHTMOUSE:
1181 case K_RIGHTDRAG:
1182 case K_RIGHTRELEASE:
1183 case K_X1MOUSE:
1184 case K_X1DRAG:
1185 case K_X1RELEASE:
1186 case K_X2MOUSE:
1187 case K_X2DRAG:
1188 case K_X2RELEASE:
1189 ins_mouse(c);
1190 break;
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001193 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 break;
1195
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001196 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001197 ins_mousescroll(MSCR_UP);
1198 break;
1199
1200 case K_MOUSELEFT: /* Scroll wheel left */
1201 ins_mousescroll(MSCR_LEFT);
1202 break;
1203
1204 case K_MOUSERIGHT: /* Scroll wheel right */
1205 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 break;
1207#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001208 case K_PS:
1209 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1210 if (cmdchar == K_PS)
1211 /* invoked from normal mode, bail out */
1212 goto doESCkey;
1213 break;
1214 case K_PE:
1215 /* Got K_PE without K_PS, ignore. */
1216 break;
1217
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001218#ifdef FEAT_GUI_TABLINE
1219 case K_TABLINE:
1220 case K_TABMENU:
1221 ins_tabline(c);
1222 break;
1223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 break;
1227
Bram Moolenaar754b5602006-02-09 23:53:20 +00001228#ifdef FEAT_AUTOCMD
1229 case K_CURSORHOLD: /* Didn't type something for a while. */
1230 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1231 did_cursorhold = TRUE;
1232 break;
1233#endif
1234
Bram Moolenaar4770d092006-01-12 23:22:24 +00001235#ifdef FEAT_GUI_W32
1236 /* On Win32 ignore <M-F4>, we get it when closing the window was
1237 * cancelled. */
1238 case K_F4:
1239 if (mod_mask != MOD_MASK_ALT)
1240 goto normalchar;
1241 break;
1242#endif
1243
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244#ifdef FEAT_GUI
1245 case K_VER_SCROLLBAR:
1246 ins_scroll();
1247 break;
1248
1249 case K_HOR_SCROLLBAR:
1250 ins_horscroll();
1251 break;
1252#endif
1253
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001254 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 case K_S_HOME:
1257 case K_C_HOME:
1258 ins_home(c);
1259 break;
1260
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001261 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 case K_S_END:
1264 case K_C_END:
1265 ins_end(c);
1266 break;
1267
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001268 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001269 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1270 ins_s_left();
1271 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001272 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 break;
1274
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001275 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 case K_C_LEFT:
1277 ins_s_left();
1278 break;
1279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001280 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001281 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1282 ins_s_right();
1283 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001284 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 break;
1286
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001287 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 case K_C_RIGHT:
1289 ins_s_right();
1290 break;
1291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001292 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001293#ifdef FEAT_INS_EXPAND
1294 if (pum_visible())
1295 goto docomplete;
1296#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001297 if (mod_mask & MOD_MASK_SHIFT)
1298 ins_pageup();
1299 else
1300 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 break;
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 case K_PAGEUP:
1305 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001306#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001307 if (pum_visible())
1308 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 ins_pageup();
1311 break;
1312
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001313 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001314#ifdef FEAT_INS_EXPAND
1315 if (pum_visible())
1316 goto docomplete;
1317#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001318 if (mod_mask & MOD_MASK_SHIFT)
1319 ins_pagedown();
1320 else
1321 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 break;
1323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001324 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 case K_PAGEDOWN:
1326 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001327#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001328 if (pum_visible())
1329 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 ins_pagedown();
1332 break;
1333
1334#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001335 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 ins_drop();
1337 break;
1338#endif
1339
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001340 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 c = TAB;
1342 /* FALLTHROUGH */
1343
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1346 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1347 goto docomplete;
1348#endif
1349 inserted_space = FALSE;
1350 if (ins_tab())
1351 goto normalchar; /* insert TAB as a normal char */
1352 auto_format(FALSE, TRUE);
1353 break;
1354
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001355 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 c = CAR;
1357 /* FALLTHROUGH */
1358 case CAR:
1359 case NL:
1360#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1361 /* In a quickfix window a <CR> jumps to the error under the
1362 * cursor. */
1363 if (bt_quickfix(curbuf) && c == CAR)
1364 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001365 if (curwin->w_llist_ref == NULL) /* quickfix window */
1366 do_cmdline_cmd((char_u *)".cc");
1367 else /* location list window */
1368 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 break;
1370 }
1371#endif
1372#ifdef FEAT_CMDWIN
1373 if (cmdwin_type != 0)
1374 {
1375 /* Execute the command in the cmdline window. */
1376 cmdwin_result = CAR;
1377 goto doESCkey;
1378 }
1379#endif
1380 if (ins_eol(c) && !p_im)
1381 goto doESCkey; /* out of memory */
1382 auto_format(FALSE, FALSE);
1383 inserted_space = FALSE;
1384 break;
1385
Bram Moolenaar860cae12010-06-05 23:22:07 +02001386#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001387 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388# ifdef FEAT_INS_EXPAND
1389 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1390 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001391 if (has_compl_option(TRUE))
1392 goto docomplete;
1393 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395# endif
1396# ifdef FEAT_DIGRAPHS
1397 c = ins_digraph();
1398 if (c == NUL)
1399 break;
1400# endif
1401 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
1404#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001405 case Ctrl_X: /* Enter CTRL-X mode */
1406 ins_ctrl_x();
1407 break;
1408
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001409 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (ctrl_x_mode != CTRL_X_TAGS)
1411 goto normalchar;
1412 goto docomplete;
1413
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001414 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (ctrl_x_mode != CTRL_X_FILES)
1416 goto normalchar;
1417 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001418
1419 case 's': /* Spelling completion after ^X */
1420 case Ctrl_S:
1421 if (ctrl_x_mode != CTRL_X_SPELL)
1422 goto normalchar;
1423 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424#endif
1425
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001426 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427#ifdef FEAT_INS_EXPAND
1428 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1429#endif
1430 {
1431 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1432 if (p_im)
1433 {
1434 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1435 break;
1436 goto doESCkey;
1437 }
1438 goto normalchar;
1439 }
1440#ifdef FEAT_INS_EXPAND
1441 /* FALLTHROUGH */
1442
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001443 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 case Ctrl_N:
1445 /* if 'complete' is empty then plain ^P is no longer special,
1446 * but it is under other ^X modes */
1447 if (*curbuf->b_p_cpt == NUL
1448 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001449 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 goto normalchar;
1451
1452docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001453 compl_busy = TRUE;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001454 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001455 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001456 compl_cont_status = 0;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001457 disable_fold_update--;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001458 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 break;
1460#endif /* FEAT_INS_EXPAND */
1461
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001462 case Ctrl_Y: /* copy from previous line or scroll down */
1463 case Ctrl_E: /* copy from next line or scroll up */
1464 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 break;
1466
1467 default:
1468#ifdef UNIX
1469 if (c == intr_char) /* special interrupt char */
1470 goto do_intr;
1471#endif
1472
Bram Moolenaare659c952011-05-19 17:25:41 +02001473normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001475 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001477#ifdef FEAT_AUTOCMD
1478 if (!p_paste)
1479 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001480 /* Trigger InsertCharPre. */
1481 char_u *str = do_insert_char_pre(c);
1482 char_u *p;
1483
1484 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001485 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001486 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001487 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001488 /* Insert the new value of v:char literally. */
1489 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001490 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001491 c = PTR2CHAR(p);
1492 if (c == CAR || c == K_KENTER || c == NL)
1493 ins_eol(c);
1494 else
1495 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001496 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001497 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001498 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001499 vim_free(str);
1500 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001501 }
1502
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001503 /* If the new value is already inserted or an empty string
1504 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001505 if (c == NUL)
1506 break;
1507 }
1508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509#ifdef FEAT_SMARTINDENT
1510 /* Try to perform smart-indenting. */
1511 ins_try_si(c);
1512#endif
1513
1514 if (c == ' ')
1515 {
1516 inserted_space = TRUE;
1517#ifdef FEAT_CINDENT
1518 if (inindent(0))
1519 can_cindent = FALSE;
1520#endif
1521 if (Insstart_blank_vcol == MAXCOL
1522 && curwin->w_cursor.lnum == Insstart.lnum)
1523 Insstart_blank_vcol = get_nolist_virtcol();
1524 }
1525
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001526 /* Insert a normal character and check for abbreviations on a
1527 * special character. Let CTRL-] expand abbreviations without
1528 * inserting it. */
1529 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530#ifdef FEAT_MBYTE
1531 /* Add ABBR_OFF for characters above 0x100, this is
1532 * what check_abbr() expects. */
1533 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1534#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001535 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 insert_special(c, FALSE, FALSE);
1538#ifdef FEAT_RIGHTLEFT
1539 revins_legal++;
1540 revins_chars++;
1541#endif
1542 }
1543
1544 auto_format(FALSE, TRUE);
1545
1546#ifdef FEAT_FOLDING
1547 /* When inserting a character the cursor line must never be in a
1548 * closed fold. */
1549 foldOpenCursor();
1550#endif
1551 break;
1552 } /* end of switch (c) */
1553
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001554#ifdef FEAT_AUTOCMD
1555 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001556 if (c != K_CURSORHOLD
1557# ifdef FEAT_COMPL_FUNC
1558 /* but not in CTRL-X mode, a script can't restore the state */
1559 && ctrl_x_mode == 0
1560# endif
1561 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001562 did_cursorhold = FALSE;
1563#endif
1564
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 /* If the cursor was moved we didn't just insert a space */
1566 if (arrow_used)
1567 inserted_space = FALSE;
1568
1569#ifdef FEAT_CINDENT
1570 if (can_cindent && cindent_on()
1571# ifdef FEAT_INS_EXPAND
1572 && ctrl_x_mode == 0
1573# endif
1574 )
1575 {
1576force_cindent:
1577 /*
1578 * Indent now if a key was typed that is in 'cinkeys'.
1579 */
1580 if (in_cinkeys(c, ' ', line_is_white))
1581 {
1582 if (stop_arrow() == OK)
1583 /* re-indent the current line */
1584 do_c_expr_indent();
1585 }
1586 }
1587#endif /* FEAT_CINDENT */
1588
1589 } /* for (;;) */
1590 /* NOTREACHED */
1591}
1592
1593/*
1594 * Redraw for Insert mode.
1595 * This is postponed until getting the next character to make '$' in the 'cpo'
1596 * option work correctly.
1597 * Only redraw when there are no characters available. This speeds up
1598 * inserting sequences of characters (e.g., for CTRL-R).
1599 */
1600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601ins_redraw(
1602 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001604#ifdef FEAT_CONCEAL
1605 linenr_T conceal_old_cursor_line = 0;
1606 linenr_T conceal_new_cursor_line = 0;
1607 int conceal_update_lines = FALSE;
1608#endif
1609
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001610 if (char_avail())
1611 return;
1612
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001613#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001614 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1615 * visible, the command might delete it. */
1616 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001617# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001618 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001619# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001620# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001621 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001622# endif
1623# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001625# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001626 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001627# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001628 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001629# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001630# ifdef FEAT_INS_EXPAND
1631 && !pum_visible()
1632# endif
1633 )
1634 {
1635# ifdef FEAT_SYN_HL
1636 /* Need to update the screen first, to make sure syntax
1637 * highlighting is correct after making a change (e.g., inserting
1638 * a "(". The autocommand may also require a redraw, so it's done
1639 * again below, unfortunately. */
1640 if (syntax_present(curwin) && must_redraw)
1641 update_screen(0);
1642# endif
1643# ifdef FEAT_AUTOCMD
1644 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001645 {
1646 /* Make sure curswant is correct, an autocommand may call
1647 * getcurpos(). */
1648 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001649 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001650 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001651# endif
1652# ifdef FEAT_CONCEAL
1653 if (curwin->w_p_cole > 0)
1654 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001655# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001656 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001657# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001658 conceal_new_cursor_line = curwin->w_cursor.lnum;
1659 conceal_update_lines = TRUE;
1660 }
1661# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001662# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001663 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001664# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665 }
1666#endif
1667
1668#ifdef FEAT_AUTOCMD
1669 /* Trigger TextChangedI if b_changedtick differs. */
1670 if (ready && has_textchangedI()
1671 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001672# ifdef FEAT_INS_EXPAND
1673 && !pum_visible()
1674# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001675 )
1676 {
1677 if (last_changedtick_buf == curbuf)
1678 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1679 last_changedtick_buf = curbuf;
1680 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001682#endif
1683
1684 if (must_redraw)
1685 update_screen(0);
1686 else if (clear_cmdline || redraw_cmdline)
1687 showmode(); /* clear cmdline and show mode */
1688# if defined(FEAT_CONCEAL)
1689 if ((conceal_update_lines
1690 && (conceal_old_cursor_line != conceal_new_cursor_line
1691 || conceal_cursor_line(curwin)))
1692 || need_cursor_line_redraw)
1693 {
1694 if (conceal_old_cursor_line != conceal_new_cursor_line)
1695 update_single_line(curwin, conceal_old_cursor_line);
1696 update_single_line(curwin, conceal_new_cursor_line == 0
1697 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1698 curwin->w_valid &= ~VALID_CROW;
1699 }
1700# endif
1701 showruler(FALSE);
1702 setcursor();
1703 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704}
1705
1706/*
1707 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1708 */
1709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001710ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
1712 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001713 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001716 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
1718 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001721 did_putchar = TRUE;
1722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1724
1725#ifdef FEAT_CMDL_INFO
1726 add_to_showcmd_c(Ctrl_V);
1727#endif
1728
1729 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001730 if (did_putchar)
1731 /* when the line fits in 'columns' the '^' is at the start of the next
1732 * line and will not removed by the redraw */
1733 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734#ifdef FEAT_CMDL_INFO
1735 clear_showcmd();
1736#endif
1737 insert_special(c, FALSE, TRUE);
1738#ifdef FEAT_RIGHTLEFT
1739 revins_chars++;
1740 revins_legal++;
1741#endif
1742}
1743
1744/*
1745 * Put a character directly onto the screen. It's not stored in a buffer.
1746 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1747 */
1748static int pc_status;
1749#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1750#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1751#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1752#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754static int pc_attr;
1755static int pc_row;
1756static int pc_col;
1757
1758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001759edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 int attr;
1762
1763 if (ScreenLines != NULL)
1764 {
1765 update_topline(); /* just in case w_topline isn't valid */
1766 validate_cursor();
1767 if (highlight)
1768 attr = hl_attr(HLF_8);
1769 else
1770 attr = 0;
1771 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1772 pc_col = W_WINCOL(curwin);
1773#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1774 pc_status = PC_STATUS_UNSET;
1775#endif
1776#ifdef FEAT_RIGHTLEFT
1777 if (curwin->w_p_rl)
1778 {
1779 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1780# ifdef FEAT_MBYTE
1781 if (has_mbyte)
1782 {
1783 int fix_col = mb_fix_col(pc_col, pc_row);
1784
1785 if (fix_col != pc_col)
1786 {
1787 screen_putchar(' ', pc_row, fix_col, attr);
1788 --curwin->w_wcol;
1789 pc_status = PC_STATUS_RIGHT;
1790 }
1791 }
1792# endif
1793 }
1794 else
1795#endif
1796 {
1797 pc_col += curwin->w_wcol;
1798#ifdef FEAT_MBYTE
1799 if (mb_lefthalve(pc_row, pc_col))
1800 pc_status = PC_STATUS_LEFT;
1801#endif
1802 }
1803
1804 /* save the character to be able to put it back */
1805#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1806 if (pc_status == PC_STATUS_UNSET)
1807#endif
1808 {
1809 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1810 pc_status = PC_STATUS_SET;
1811 }
1812 screen_putchar(c, pc_row, pc_col, attr);
1813 }
1814}
1815
1816/*
1817 * Undo the previous edit_putchar().
1818 */
1819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001820edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821{
1822 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1823 {
1824#if defined(FEAT_MBYTE)
1825 if (pc_status == PC_STATUS_RIGHT)
1826 ++curwin->w_wcol;
1827 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1828 redrawWinline(curwin->w_cursor.lnum, FALSE);
1829 else
1830#endif
1831 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1832 }
1833}
1834
1835/*
1836 * Called when p_dollar is set: display a '$' at the end of the changed text
1837 * Only works when cursor is in the line that changes.
1838 */
1839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
1842 colnr_T save_col;
1843
1844 if (!redrawing())
1845 return;
1846
1847 cursor_off();
1848 save_col = curwin->w_cursor.col;
1849 curwin->w_cursor.col = col;
1850#ifdef FEAT_MBYTE
1851 if (has_mbyte)
1852 {
1853 char_u *p;
1854
1855 /* If on the last byte of a multi-byte move to the first byte. */
1856 p = ml_get_curline();
1857 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1858 }
1859#endif
1860 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1861 if (curwin->w_wcol < W_WIDTH(curwin))
1862 {
1863 edit_putchar('$', FALSE);
1864 dollar_vcol = curwin->w_virtcol;
1865 }
1866 curwin->w_cursor.col = save_col;
1867}
1868
1869/*
1870 * Call this function before moving the cursor from the normal insert position
1871 * in insert mode.
1872 */
1873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001874undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001876 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001878 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 redrawWinline(curwin->w_cursor.lnum, FALSE);
1880 }
1881}
1882
1883/*
1884 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1885 * Keep the cursor on the same character.
1886 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1887 * type == INDENT_DEC decrease indent (for CTRL-D)
1888 * type == INDENT_SET set indent to "amount"
1889 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1890 */
1891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001892change_indent(
1893 int type,
1894 int amount,
1895 int round,
1896 int replaced, /* replaced character, put on replace stack */
1897 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898{
1899 int vcol;
1900 int last_vcol;
1901 int insstart_less; /* reduction for Insstart.col */
1902 int new_cursor_col;
1903 int i;
1904 char_u *ptr;
1905 int save_p_list;
1906 int start_col;
1907 colnr_T vc;
1908#ifdef FEAT_VREPLACE
1909 colnr_T orig_col = 0; /* init for GCC */
1910 char_u *new_line, *orig_line = NULL; /* init for GCC */
1911
1912 /* VREPLACE mode needs to know what the line was like before changing */
1913 if (State & VREPLACE_FLAG)
1914 {
1915 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1916 orig_col = curwin->w_cursor.col;
1917 }
1918#endif
1919
1920 /* for the following tricks we don't want list mode */
1921 save_p_list = curwin->w_p_list;
1922 curwin->w_p_list = FALSE;
1923 vc = getvcol_nolist(&curwin->w_cursor);
1924 vcol = vc;
1925
1926 /*
1927 * For Replace mode we need to fix the replace stack later, which is only
1928 * possible when the cursor is in the indent. Remember the number of
1929 * characters before the cursor if it's possible.
1930 */
1931 start_col = curwin->w_cursor.col;
1932
1933 /* determine offset from first non-blank */
1934 new_cursor_col = curwin->w_cursor.col;
1935 beginline(BL_WHITE);
1936 new_cursor_col -= curwin->w_cursor.col;
1937
1938 insstart_less = curwin->w_cursor.col;
1939
1940 /*
1941 * If the cursor is in the indent, compute how many screen columns the
1942 * cursor is to the left of the first non-blank.
1943 */
1944 if (new_cursor_col < 0)
1945 vcol = get_indent() - vcol;
1946
1947 if (new_cursor_col > 0) /* can't fix replace stack */
1948 start_col = -1;
1949
1950 /*
1951 * Set the new indent. The cursor will be put on the first non-blank.
1952 */
1953 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001954 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 else
1956 {
1957#ifdef FEAT_VREPLACE
1958 int save_State = State;
1959
1960 /* Avoid being called recursively. */
1961 if (State & VREPLACE_FLAG)
1962 State = INSERT;
1963#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001964 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965#ifdef FEAT_VREPLACE
1966 State = save_State;
1967#endif
1968 }
1969 insstart_less -= curwin->w_cursor.col;
1970
1971 /*
1972 * Try to put cursor on same character.
1973 * If the cursor is at or after the first non-blank in the line,
1974 * compute the cursor column relative to the column of the first
1975 * non-blank character.
1976 * If we are not in insert mode, leave the cursor on the first non-blank.
1977 * If the cursor is before the first non-blank, position it relative
1978 * to the first non-blank, counted in screen columns.
1979 */
1980 if (new_cursor_col >= 0)
1981 {
1982 /*
1983 * When changing the indent while the cursor is touching it, reset
1984 * Insstart_col to 0.
1985 */
1986 if (new_cursor_col == 0)
1987 insstart_less = MAXCOL;
1988 new_cursor_col += curwin->w_cursor.col;
1989 }
1990 else if (!(State & INSERT))
1991 new_cursor_col = curwin->w_cursor.col;
1992 else
1993 {
1994 /*
1995 * Compute the screen column where the cursor should be.
1996 */
1997 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001998 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000 /*
2001 * Advance the cursor until we reach the right screen column.
2002 */
2003 vcol = last_vcol = 0;
2004 new_cursor_col = -1;
2005 ptr = ml_get_curline();
2006 while (vcol <= (int)curwin->w_virtcol)
2007 {
2008 last_vcol = vcol;
2009#ifdef FEAT_MBYTE
2010 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002011 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 else
2013#endif
2014 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002015 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
2017 vcol = last_vcol;
2018
2019 /*
2020 * May need to insert spaces to be able to position the cursor on
2021 * the right screen column.
2022 */
2023 if (vcol != (int)curwin->w_virtcol)
2024 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002025 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002027 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 if (ptr != NULL)
2029 {
2030 new_cursor_col += i;
2031 ptr[i] = NUL;
2032 while (--i >= 0)
2033 ptr[i] = ' ';
2034 ins_str(ptr);
2035 vim_free(ptr);
2036 }
2037 }
2038
2039 /*
2040 * When changing the indent while the cursor is in it, reset
2041 * Insstart_col to 0.
2042 */
2043 insstart_less = MAXCOL;
2044 }
2045
2046 curwin->w_p_list = save_p_list;
2047
2048 if (new_cursor_col <= 0)
2049 curwin->w_cursor.col = 0;
2050 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002051 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 curwin->w_set_curswant = TRUE;
2053 changed_cline_bef_curs();
2054
2055 /*
2056 * May have to adjust the start of the insert.
2057 */
2058 if (State & INSERT)
2059 {
2060 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2061 {
2062 if ((int)Insstart.col <= insstart_less)
2063 Insstart.col = 0;
2064 else
2065 Insstart.col -= insstart_less;
2066 }
2067 if ((int)ai_col <= insstart_less)
2068 ai_col = 0;
2069 else
2070 ai_col -= insstart_less;
2071 }
2072
2073 /*
2074 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2075 * If the number of characters before the cursor decreased, need to pop a
2076 * few characters from the replace stack.
2077 * If the number of characters before the cursor increased, need to push a
2078 * few NULs onto the replace stack.
2079 */
2080 if (REPLACE_NORMAL(State) && start_col >= 0)
2081 {
2082 while (start_col > (int)curwin->w_cursor.col)
2083 {
2084 replace_join(0); /* remove a NUL from the replace stack */
2085 --start_col;
2086 }
2087 while (start_col < (int)curwin->w_cursor.col || replaced)
2088 {
2089 replace_push(NUL);
2090 if (replaced)
2091 {
2092 replace_push(replaced);
2093 replaced = NUL;
2094 }
2095 ++start_col;
2096 }
2097 }
2098
2099#ifdef FEAT_VREPLACE
2100 /*
2101 * For VREPLACE mode, we also have to fix the replace stack. In this case
2102 * it is always possible because we backspace over the whole line and then
2103 * put it back again the way we wanted it.
2104 */
2105 if (State & VREPLACE_FLAG)
2106 {
2107 /* If orig_line didn't allocate, just return. At least we did the job,
2108 * even if you can't backspace. */
2109 if (orig_line == NULL)
2110 return;
2111
2112 /* Save new line */
2113 new_line = vim_strsave(ml_get_curline());
2114 if (new_line == NULL)
2115 return;
2116
2117 /* We only put back the new line up to the cursor */
2118 new_line[curwin->w_cursor.col] = NUL;
2119
2120 /* Put back original line */
2121 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2122 curwin->w_cursor.col = orig_col;
2123
2124 /* Backspace from cursor to start of line */
2125 backspace_until_column(0);
2126
2127 /* Insert new stuff into line again */
2128 ins_bytes(new_line);
2129
2130 vim_free(new_line);
2131 }
2132#endif
2133}
2134
2135/*
2136 * Truncate the space at the end of a line. This is to be used only in an
2137 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2138 * modes.
2139 */
2140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002141truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142{
2143 int i;
2144
2145 /* find start of trailing white space */
2146 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2147 {
2148 if (State & REPLACE_FLAG)
2149 replace_join(0); /* remove a NUL from the replace stack */
2150 }
2151 line[i + 1] = NUL;
2152}
2153
2154#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2155 || defined(FEAT_COMMENTS) || defined(PROTO)
2156/*
2157 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2158 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002159 * Will attempt not to go before "col" even when there is a composing
2160 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 */
2162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002163backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164{
2165 while ((int)curwin->w_cursor.col > col)
2166 {
2167 curwin->w_cursor.col--;
2168 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002169 replace_do_bs(col);
2170 else if (!del_char_after_col(col))
2171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
2173}
2174#endif
2175
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002176/*
2177 * Like del_char(), but make sure not to go before column "limit_col".
2178 * Only matters when there are composing characters.
2179 * Return TRUE when something was deleted.
2180 */
2181 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002182del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002183{
2184#ifdef FEAT_MBYTE
2185 if (enc_utf8 && limit_col >= 0)
2186 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002187 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002188
2189 /* Make sure the cursor is at the start of a character, but
2190 * skip forward again when going too far back because of a
2191 * composing character. */
2192 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002193 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002194 {
2195 int l = utf_ptr2len(ml_get_cursor());
2196
2197 if (l == 0) /* end of line */
2198 break;
2199 curwin->w_cursor.col += l;
2200 }
2201 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2202 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002203 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002204 }
2205 else
2206#endif
2207 (void)del_char(FALSE);
2208 return TRUE;
2209}
2210
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2212/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002213 * CTRL-X pressed in Insert mode.
2214 */
2215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002216ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002217{
2218 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2219 * CTRL-V works like CTRL-N */
2220 if (ctrl_x_mode != CTRL_X_CMDLINE)
2221 {
2222 /* if the next ^X<> won't ADD nothing, then reset
2223 * compl_cont_status */
2224 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002225 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002226 else
2227 compl_cont_status = 0;
2228 /* We're not sure which CTRL-X mode it will be yet */
2229 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2230 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2231 edit_submode_pre = NULL;
2232 showmode();
2233 }
2234}
2235
2236/*
2237 * Return TRUE if the 'dict' or 'tsr' option can be used.
2238 */
2239 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002240has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002241{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002242 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002243# ifdef FEAT_SPELL
2244 && !curwin->w_p_spell
2245# endif
2246 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002247 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2248 {
2249 ctrl_x_mode = 0;
2250 edit_submode = NULL;
2251 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2252 : (char_u *)_("'thesaurus' option is empty"),
2253 hl_attr(HLF_E));
2254 if (emsg_silent == 0)
2255 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002256 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002257 setcursor();
2258 out_flush();
2259 ui_delay(2000L, FALSE);
2260 }
2261 return FALSE;
2262 }
2263 return TRUE;
2264}
2265
2266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2268 * This depends on the current mode.
2269 */
2270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002271vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
2273 /* Always allow ^R - let it's results then be checked */
2274 if (c == Ctrl_R)
2275 return TRUE;
2276
Bram Moolenaare3226be2005-12-18 22:10:00 +00002277 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002278 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002279 return TRUE;
2280
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 switch (ctrl_x_mode)
2282 {
2283 case 0: /* Not in any CTRL-X mode */
2284 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2285 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002286 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2288 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2289 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002290 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002291 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 case CTRL_X_SCROLL:
2293 return (c == Ctrl_Y || c == Ctrl_E);
2294 case CTRL_X_WHOLE_LINE:
2295 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2296 case CTRL_X_FILES:
2297 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2298 case CTRL_X_DICTIONARY:
2299 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2300 case CTRL_X_THESAURUS:
2301 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2302 case CTRL_X_TAGS:
2303 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2304#ifdef FEAT_FIND_ID
2305 case CTRL_X_PATH_PATTERNS:
2306 return (c == Ctrl_P || c == Ctrl_N);
2307 case CTRL_X_PATH_DEFINES:
2308 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2309#endif
2310 case CTRL_X_CMDLINE:
2311 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2312 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002313#ifdef FEAT_COMPL_FUNC
2314 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002315 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002316 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002317 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002318#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002319 case CTRL_X_SPELL:
2320 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002321 case CTRL_X_EVAL:
2322 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002324 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 return FALSE;
2326}
2327
2328/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002329 * Return TRUE when character "c" is part of the item currently being
2330 * completed. Used to decide whether to abandon complete mode when the menu
2331 * is visible.
2332 */
2333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002334ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002335{
2336 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2337 /* When expanding an identifier only accept identifier chars. */
2338 return vim_isIDc(c);
2339
2340 switch (ctrl_x_mode)
2341 {
2342 case CTRL_X_FILES:
2343 /* When expanding file name only accept file name chars. But not
2344 * path separators, so that "proto/<Tab>" expands files in
2345 * "proto", not "proto/" as a whole */
2346 return vim_isfilec(c) && !vim_ispathsep(c);
2347
2348 case CTRL_X_CMDLINE:
2349 case CTRL_X_OMNI:
2350 /* Command line and Omni completion can work with just about any
2351 * printable character, but do stop at white space. */
2352 return vim_isprintc(c) && !vim_iswhite(c);
2353
2354 case CTRL_X_WHOLE_LINE:
2355 /* For while line completion a space can be part of the line. */
2356 return vim_isprintc(c);
2357 }
2358 return vim_iswordc(c);
2359}
2360
2361/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002362 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002364 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 * the rest of the word to be in -- webb
2366 */
2367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002368ins_compl_add_infercase(
2369 char_u *str,
2370 int len,
2371 int icase,
2372 char_u *fname,
2373 int dir,
2374 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002376 char_u *p;
2377 int i, c;
2378 int actual_len; /* Take multi-byte characters */
2379 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002380 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002381 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 int has_lower = FALSE;
2383 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002385 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002387 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaara2993e12007-08-12 14:38:46 +00002389 /* Find actual length of completion. */
2390#ifdef FEAT_MBYTE
2391 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002393 p = str;
2394 actual_len = 0;
2395 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002397 mb_ptr_adv(p);
2398 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002401 else
2402#endif
2403 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
Bram Moolenaara2993e12007-08-12 14:38:46 +00002405 /* Find actual length of original text. */
2406#ifdef FEAT_MBYTE
2407 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002409 p = compl_orig_text;
2410 actual_compl_length = 0;
2411 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002413 mb_ptr_adv(p);
2414 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002417 else
2418#endif
2419 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002421 /* "actual_len" may be smaller than "actual_compl_length" when using
2422 * thesaurus, only use the minimum when comparing. */
2423 min_len = actual_len < actual_compl_length
2424 ? actual_len : actual_compl_length;
2425
Bram Moolenaara2993e12007-08-12 14:38:46 +00002426 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002427 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002428 if (wca != NULL)
2429 {
2430 p = str;
2431 for (i = 0; i < actual_len; ++i)
2432#ifdef FEAT_MBYTE
2433 if (has_mbyte)
2434 wca[i] = mb_ptr2char_adv(&p);
2435 else
2436#endif
2437 wca[i] = *(p++);
2438
2439 /* Rule 1: Were any chars converted to lower? */
2440 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002441 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002442 {
2443#ifdef FEAT_MBYTE
2444 if (has_mbyte)
2445 c = mb_ptr2char_adv(&p);
2446 else
2447#endif
2448 c = *(p++);
2449 if (MB_ISLOWER(c))
2450 {
2451 has_lower = TRUE;
2452 if (MB_ISUPPER(wca[i]))
2453 {
2454 /* Rule 1 is satisfied. */
2455 for (i = actual_compl_length; i < actual_len; ++i)
2456 wca[i] = MB_TOLOWER(wca[i]);
2457 break;
2458 }
2459 }
2460 }
2461
2462 /*
2463 * Rule 2: No lower case, 2nd consecutive letter converted to
2464 * upper case.
2465 */
2466 if (!has_lower)
2467 {
2468 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002469 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002470 {
2471#ifdef FEAT_MBYTE
2472 if (has_mbyte)
2473 c = mb_ptr2char_adv(&p);
2474 else
2475#endif
2476 c = *(p++);
2477 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2478 {
2479 /* Rule 2 is satisfied. */
2480 for (i = actual_compl_length; i < actual_len; ++i)
2481 wca[i] = MB_TOUPPER(wca[i]);
2482 break;
2483 }
2484 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2485 }
2486 }
2487
2488 /* Copy the original case of the part we typed. */
2489 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002490 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002491 {
2492#ifdef FEAT_MBYTE
2493 if (has_mbyte)
2494 c = mb_ptr2char_adv(&p);
2495 else
2496#endif
2497 c = *(p++);
2498 if (MB_ISLOWER(c))
2499 wca[i] = MB_TOLOWER(wca[i]);
2500 else if (MB_ISUPPER(c))
2501 wca[i] = MB_TOUPPER(wca[i]);
2502 }
2503
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002504 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002505 * Generate encoding specific output from wide character array.
2506 * Multi-byte characters can occupy up to five bytes more than
2507 * ASCII characters, and we also need one byte for NUL, so stay
2508 * six bytes away from the edge of IObuff.
2509 */
2510 p = IObuff;
2511 i = 0;
2512 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2513#ifdef FEAT_MBYTE
2514 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002515 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002516 else
2517#endif
2518 *(p++) = wca[i++];
2519 *p = NUL;
2520
2521 vim_free(wca);
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002524 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2525 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002527 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528}
2529
2530/*
2531 * Add a match to the list of matches.
2532 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002533 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002537ins_compl_add(
2538 char_u *str,
2539 int len,
2540 int icase,
2541 char_u *fname,
2542 char_u **cptext, /* extra text for popup menu or NULL */
2543 int cdir,
2544 int flags,
2545 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002547 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002548 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
2550 ui_breakcheck();
2551 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002552 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (len < 0)
2554 len = (int)STRLEN(str);
2555
2556 /*
2557 * If the same match is already present, don't add it.
2558 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002559 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002561 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 do
2563 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002564 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002565 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002566 && match->cp_str[len] == NUL)
2567 return NOTDONE;
2568 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002569 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002572 /* Remove any popup menu before changing the list of matches. */
2573 ins_compl_del_pum();
2574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 /*
2576 * Allocate a new match structure.
2577 * Copy the values to the new match structure.
2578 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002579 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002581 return FAIL;
2582 match->cp_number = -1;
2583 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002584 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002585 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
2587 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002590 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002593 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2595 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002596 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002597 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002598 && compl_curr_match->cp_fname != NULL
2599 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002600 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002601 else if (fname != NULL)
2602 {
2603 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002604 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002607 match->cp_fname = NULL;
2608 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002609
2610 if (cptext != NULL)
2611 {
2612 int i;
2613
2614 for (i = 0; i < CPT_COUNT; ++i)
2615 if (cptext[i] != NULL && *cptext[i] != NUL)
2616 match->cp_text[i] = vim_strsave(cptext[i]);
2617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 /*
2620 * Link the new match structure in the list of matches.
2621 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002622 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002623 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 else if (dir == FORWARD)
2625 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002626 match->cp_next = compl_curr_match->cp_next;
2627 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
2629 else /* BACKWARD */
2630 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002631 match->cp_next = compl_curr_match;
2632 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002634 if (match->cp_next)
2635 match->cp_next->cp_prev = match;
2636 if (match->cp_prev)
2637 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002639 compl_first_match = match;
2640 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002642 /*
2643 * Find the longest common string if still doing that.
2644 */
2645 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2646 ins_compl_longest_match(match);
2647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 return OK;
2649}
2650
2651/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002652 * Return TRUE if "str[len]" matches with match->cp_str, considering
2653 * match->cp_icase.
2654 */
2655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002656ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002657{
2658 if (match->cp_icase)
2659 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2660 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2661}
2662
2663/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002664 * Reduce the longest common string for match "match".
2665 */
2666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002667ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002668{
2669 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002670 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002671 int had_match;
2672
2673 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002674 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002675 /* First match, use it as a whole. */
2676 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002677 if (compl_leader != NULL)
2678 {
2679 had_match = (curwin->w_cursor.col > compl_col);
2680 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002681 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002682 ins_redraw(FALSE);
2683
2684 /* When the match isn't there (to avoid matching itself) remove it
2685 * again after redrawing. */
2686 if (!had_match)
2687 ins_compl_delete();
2688 compl_used_match = FALSE;
2689 }
2690 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002691 else
2692 {
2693 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002694 p = compl_leader;
2695 s = match->cp_str;
2696 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002697 {
2698#ifdef FEAT_MBYTE
2699 if (has_mbyte)
2700 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002701 c1 = mb_ptr2char(p);
2702 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002703 }
2704 else
2705#endif
2706 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002707 c1 = *p;
2708 c2 = *s;
2709 }
2710 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2711 : (c1 != c2))
2712 break;
2713#ifdef FEAT_MBYTE
2714 if (has_mbyte)
2715 {
2716 mb_ptr_adv(p);
2717 mb_ptr_adv(s);
2718 }
2719 else
2720#endif
2721 {
2722 ++p;
2723 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002724 }
2725 }
2726
2727 if (*p != NUL)
2728 {
2729 /* Leader was shortened, need to change the inserted text. */
2730 *p = NUL;
2731 had_match = (curwin->w_cursor.col > compl_col);
2732 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002733 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002734 ins_redraw(FALSE);
2735
2736 /* When the match isn't there (to avoid matching itself) remove it
2737 * again after redrawing. */
2738 if (!had_match)
2739 ins_compl_delete();
2740 }
2741
2742 compl_used_match = FALSE;
2743 }
2744}
2745
2746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 * Add an array of matches to the list of matches.
2748 * Frees matches[].
2749 */
2750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002751ins_compl_add_matches(
2752 int num_matches,
2753 char_u **matches,
2754 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 int i;
2757 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002758 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
Bram Moolenaar572cb562005-08-05 21:35:02 +00002760 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002761 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002762 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002764 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 FreeWild(num_matches, matches);
2766}
2767
2768/* Make the completion list cyclic.
2769 * Return the number of matches (excluding the original).
2770 */
2771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002772ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002774 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 int count = 0;
2776
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002777 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
2779 /*
2780 * Find the end of the list.
2781 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002782 match = compl_first_match;
2783 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002784 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002786 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 ++count;
2788 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002789 match->cp_next = compl_first_match;
2790 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
2792 return count;
2793}
2794
Bram Moolenaara94bc432006-03-10 21:42:59 +00002795/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002796 * Set variables that store noselect and noinsert behavior from the
2797 * 'completeopt' value.
2798 */
2799 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002800completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002801{
2802 compl_no_insert = FALSE;
2803 compl_no_select = FALSE;
2804 if (strstr((char *)p_cot, "noselect") != NULL)
2805 compl_no_select = TRUE;
2806 if (strstr((char *)p_cot, "noinsert") != NULL)
2807 compl_no_insert = TRUE;
2808}
2809
2810/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002811 * Start completion for the complete() function.
2812 * "startcol" is where the matched text starts (1 is first column).
2813 * "list" is the list of matches.
2814 */
2815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002816set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002817{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002818 int save_w_wrow = curwin->w_wrow;
2819
Bram Moolenaara94bc432006-03-10 21:42:59 +00002820 /* If already doing completions stop it. */
2821 if (ctrl_x_mode != 0)
2822 ins_compl_prep(' ');
2823 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002824 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002825
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002826 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002827 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002828 startcol = curwin->w_cursor.col;
2829 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002830 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002831 /* compl_pattern doesn't need to be set */
2832 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2833 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002834 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002835 return;
2836
Bram Moolenaare4214502015-03-05 18:08:43 +01002837 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002838
2839 ins_compl_add_list(list);
2840 compl_matches = ins_compl_make_cyclic();
2841 compl_started = TRUE;
2842 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002843 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002844
2845 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002846 if (compl_no_insert || compl_no_select)
2847 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002848 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002849 if (compl_no_select)
2850 /* Down/Up has no real effect. */
2851 ins_complete(K_UP, FALSE);
2852 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002853 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002854 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002855 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002856
2857 /* Lazily show the popup menu, unless we got interrupted. */
2858 if (!compl_interrupted)
2859 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002860 out_flush();
2861}
2862
2863
Bram Moolenaar9372a112005-12-06 19:59:18 +00002864/* "compl_match_array" points the currently displayed list of entries in the
2865 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002866static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002867static int compl_match_arraysize;
2868
2869/*
2870 * Update the screen and when there is any scrolling remove the popup menu.
2871 */
2872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002873ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002874{
2875 int h;
2876
2877 if (compl_match_array != NULL)
2878 {
2879 h = curwin->w_cline_height;
2880 update_screen(0);
2881 if (h != curwin->w_cline_height)
2882 ins_compl_del_pum();
2883 }
2884}
2885
2886/*
2887 * Remove any popup menu.
2888 */
2889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002890ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002891{
2892 if (compl_match_array != NULL)
2893 {
2894 pum_undisplay();
2895 vim_free(compl_match_array);
2896 compl_match_array = NULL;
2897 }
2898}
2899
2900/*
2901 * Return TRUE if the popup menu should be displayed.
2902 */
2903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002904pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002905{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002906 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002907 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002908 return FALSE;
2909
2910 /* The display looks bad on a B&W display. */
2911 if (t_colors < 8
2912#ifdef FEAT_GUI
2913 && !gui.in_use
2914#endif
2915 )
2916 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002917 return TRUE;
2918}
2919
2920/*
2921 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002922 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002923 */
2924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002925pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002926{
2927 compl_T *compl;
2928 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002929
2930 /* Don't display the popup menu if there are no matches or there is only
2931 * one (ignoring the original text). */
2932 compl = compl_first_match;
2933 i = 0;
2934 do
2935 {
2936 if (compl == NULL
2937 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2938 break;
2939 compl = compl->cp_next;
2940 } while (compl != compl_first_match);
2941
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002942 if (strstr((char *)p_cot, "menuone") != NULL)
2943 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002944 return (i >= 2);
2945}
2946
2947/*
2948 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002949 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002950 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002951 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002952ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002953{
2954 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002955 compl_T *shown_compl = NULL;
2956 int did_find_shown_match = FALSE;
2957 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002958 int i;
2959 int cur = -1;
2960 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002961 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002962
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002963 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002964 return;
2965
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002966#if defined(FEAT_EVAL)
2967 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2968 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2969#endif
2970
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002971 /* Update the screen before drawing the popup menu over it. */
2972 update_screen(0);
2973
2974 if (compl_match_array == NULL)
2975 {
2976 /* Need to build the popup menu list. */
2977 compl_match_arraysize = 0;
2978 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002979 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002980 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002981 do
2982 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002983 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2984 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002985 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002986 ++compl_match_arraysize;
2987 compl = compl->cp_next;
2988 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002989 if (compl_match_arraysize == 0)
2990 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002991 compl_match_array = (pumitem_T *)alloc_clear(
2992 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002993 * compl_match_arraysize));
2994 if (compl_match_array != NULL)
2995 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002996 /* If the current match is the original text don't find the first
2997 * match after it, don't highlight anything. */
2998 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2999 shown_match_ok = TRUE;
3000
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003001 i = 0;
3002 compl = compl_first_match;
3003 do
3004 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003005 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3006 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003007 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003008 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003009 if (!shown_match_ok)
3010 {
3011 if (compl == compl_shown_match || did_find_shown_match)
3012 {
3013 /* This item is the shown match or this is the
3014 * first displayed item after the shown match. */
3015 compl_shown_match = compl;
3016 did_find_shown_match = TRUE;
3017 shown_match_ok = TRUE;
3018 }
3019 else
3020 /* Remember this displayed match for when the
3021 * shown match is just below it. */
3022 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003023 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003024 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003025
3026 if (compl->cp_text[CPT_ABBR] != NULL)
3027 compl_match_array[i].pum_text =
3028 compl->cp_text[CPT_ABBR];
3029 else
3030 compl_match_array[i].pum_text = compl->cp_str;
3031 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3032 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3033 if (compl->cp_text[CPT_MENU] != NULL)
3034 compl_match_array[i++].pum_extra =
3035 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003036 else
3037 compl_match_array[i++].pum_extra = compl->cp_fname;
3038 }
3039
3040 if (compl == compl_shown_match)
3041 {
3042 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003043
3044 /* When the original text is the shown match don't set
3045 * compl_shown_match. */
3046 if (compl->cp_flags & ORIGINAL_TEXT)
3047 shown_match_ok = TRUE;
3048
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003049 if (!shown_match_ok && shown_compl != NULL)
3050 {
3051 /* The shown match isn't displayed, set it to the
3052 * previously displayed match. */
3053 compl_shown_match = shown_compl;
3054 shown_match_ok = TRUE;
3055 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003056 }
3057 compl = compl->cp_next;
3058 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003059
3060 if (!shown_match_ok) /* no displayed match at all */
3061 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003062 }
3063 }
3064 else
3065 {
3066 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003067 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003068 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3069 || compl_match_array[i].pum_text
3070 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003071 {
3072 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003073 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003074 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003075 }
3076
3077 if (compl_match_array != NULL)
3078 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003079 /* In Replace mode when a $ is displayed at the end of the line only
3080 * part of the screen would be updated. We do need to redraw here. */
3081 dollar_vcol = -1;
3082
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003083 /* Compute the screen column of the start of the completed text.
3084 * Use the cursor to get all wrapping and other settings right. */
3085 col = curwin->w_cursor.col;
3086 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003087 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003088 curwin->w_cursor.col = col;
3089 }
3090}
3091
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092#define DICT_FIRST (1) /* use just first element in "dict" */
3093#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003094
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003096 * Add any identifiers that match the given pattern in the list of dictionary
3097 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 */
3099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003100ins_compl_dictionaries(
3101 char_u *dict_start,
3102 char_u *pat,
3103 int flags, /* DICT_FIRST and/or DICT_EXACT */
3104 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003106 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 char_u *ptr;
3108 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 char_u **files;
3111 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003113 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
Bram Moolenaar0b238792006-03-02 22:49:12 +00003115 if (*dict == NUL)
3116 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003117#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003118 /* When 'dictionary' is empty and spell checking is enabled use
3119 * "spell". */
3120 if (!thesaurus && curwin->w_p_spell)
3121 dict = (char_u *)"spell";
3122 else
3123#endif
3124 return;
3125 }
3126
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003128 if (buf == NULL)
3129 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003130 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 /* If 'infercase' is set, don't use 'smartcase' here */
3133 save_p_scs = p_scs;
3134 if (curbuf->b_p_inf)
3135 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003136
3137 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3138 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003139 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003140 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003141 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003142 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003143 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003144
3145 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003146 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003147 len = STRLEN(pat_esc) + 10;
3148 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003149 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003150 {
3151 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003152 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003153 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003154 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003155 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3156 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003157 vim_free(ptr);
3158 }
3159 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003160 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003161 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003162 if (regmatch.regprog == NULL)
3163 goto theend;
3164 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3167 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003168 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
3170 /* copy one dictionary file name into buf */
3171 if (flags == DICT_EXACT)
3172 {
3173 count = 1;
3174 files = &dict;
3175 }
3176 else
3177 {
3178 /* Expand wildcards in the dictionary name, but do not allow
3179 * backticks (for security, the 'dict' option may have been set in
3180 * a modeline). */
3181 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003182# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003183 if (!thesaurus && STRCMP(buf, "spell") == 0)
3184 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003185 else
3186# endif
3187 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 || expand_wildcards(1, &buf, &count, &files,
3189 EW_FILE|EW_SILENT) != OK)
3190 count = 0;
3191 }
3192
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003193# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003194 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003196 /* Complete from active spelling. Skip "\<" in the pattern, we
3197 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003198 if (pat[0] == '\\' && pat[1] == '<')
3199 ptr = pat + 2;
3200 else
3201 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003202 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003204 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003205# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003206 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003207 {
3208 ins_compl_files(count, files, thesaurus, flags,
3209 &regmatch, buf, &dir);
3210 if (flags != DICT_EXACT)
3211 FreeWild(count, files);
3212 }
3213 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 break;
3215 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003216
3217theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003219 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 vim_free(buf);
3221}
3222
Bram Moolenaar0b238792006-03-02 22:49:12 +00003223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003224ins_compl_files(
3225 int count,
3226 char_u **files,
3227 int thesaurus,
3228 int flags,
3229 regmatch_T *regmatch,
3230 char_u *buf,
3231 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003232{
3233 char_u *ptr;
3234 int i;
3235 FILE *fp;
3236 int add_r;
3237
3238 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3239 {
3240 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3241 if (flags != DICT_EXACT)
3242 {
3243 vim_snprintf((char *)IObuff, IOSIZE,
3244 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003245 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003246 }
3247
3248 if (fp != NULL)
3249 {
3250 /*
3251 * Read dictionary file line by line.
3252 * Check each line for a match.
3253 */
3254 while (!got_int && !compl_interrupted
3255 && !vim_fgets(buf, LSIZE, fp))
3256 {
3257 ptr = buf;
3258 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3259 {
3260 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003261 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003262 ptr = find_line_end(ptr);
3263 else
3264 ptr = find_word_end(ptr);
3265 add_r = ins_compl_add_infercase(regmatch->startp[0],
3266 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003267 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003268 if (thesaurus)
3269 {
3270 char_u *wstart;
3271
3272 /*
3273 * Add the other matches on the line
3274 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003275 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003276 while (!got_int)
3277 {
3278 /* Find start of the next word. Skip white
3279 * space and punctuation. */
3280 ptr = find_word_start(ptr);
3281 if (*ptr == NUL || *ptr == NL)
3282 break;
3283 wstart = ptr;
3284
Bram Moolenaara2993e12007-08-12 14:38:46 +00003285 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003286#ifdef FEAT_MBYTE
3287 if (has_mbyte)
3288 /* Japanese words may have characters in
3289 * different classes, only separate words
3290 * with single-byte non-word characters. */
3291 while (*ptr != NUL)
3292 {
3293 int l = (*mb_ptr2len)(ptr);
3294
3295 if (l < 2 && !vim_iswordc(*ptr))
3296 break;
3297 ptr += l;
3298 }
3299 else
3300#endif
3301 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003302
3303 /* Add the word. Skip the regexp match. */
3304 if (wstart != regmatch->startp[0])
3305 add_r = ins_compl_add_infercase(wstart,
3306 (int)(ptr - wstart),
3307 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003308 }
3309 }
3310 if (add_r == OK)
3311 /* if dir was BACKWARD then honor it just once */
3312 *dir = FORWARD;
3313 else if (add_r == FAIL)
3314 break;
3315 /* avoid expensive call to vim_regexec() when at end
3316 * of line */
3317 if (*ptr == '\n' || got_int)
3318 break;
3319 }
3320 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003321 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003322 }
3323 fclose(fp);
3324 }
3325 }
3326}
3327
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328/*
3329 * Find the start of the next word.
3330 * Returns a pointer to the first char of the word. Also stops at a NUL.
3331 */
3332 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003333find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335#ifdef FEAT_MBYTE
3336 if (has_mbyte)
3337 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003338 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 else
3340#endif
3341 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3342 ++ptr;
3343 return ptr;
3344}
3345
3346/*
3347 * Find the end of the word. Assumes it starts inside a word.
3348 * Returns a pointer to just after the word.
3349 */
3350 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003351find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353#ifdef FEAT_MBYTE
3354 int start_class;
3355
3356 if (has_mbyte)
3357 {
3358 start_class = mb_get_class(ptr);
3359 if (start_class > 1)
3360 while (*ptr != NUL)
3361 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003362 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (mb_get_class(ptr) != start_class)
3364 break;
3365 }
3366 }
3367 else
3368#endif
3369 while (vim_iswordc(*ptr))
3370 ++ptr;
3371 return ptr;
3372}
3373
3374/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003375 * Find the end of the line, omitting CR and NL at the end.
3376 * Returns a pointer to just after the line.
3377 */
3378 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003379find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003380{
3381 char_u *s;
3382
3383 s = ptr + STRLEN(ptr);
3384 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3385 --s;
3386 return s;
3387}
3388
3389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 * Free the list of completions
3391 */
3392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003393ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003395 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003396 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003398 vim_free(compl_pattern);
3399 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003400 vim_free(compl_leader);
3401 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003403 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003405
3406 ins_compl_del_pum();
3407 pum_clear();
3408
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003409 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 do
3411 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003412 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003413 compl_curr_match = compl_curr_match->cp_next;
3414 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003416 if (match->cp_flags & FREE_FNAME)
3417 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003418 for (i = 0; i < CPT_COUNT; ++i)
3419 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003421 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3422 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003423 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424}
3425
3426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003427ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003429 compl_cont_status = 0;
3430 compl_started = FALSE;
3431 compl_matches = 0;
3432 vim_free(compl_pattern);
3433 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003434 vim_free(compl_leader);
3435 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003437 vim_free(compl_orig_text);
3438 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003439 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003440 /* clear v:completed_item */
3441 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442}
3443
3444/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003445 * Return TRUE when Insert completion is active.
3446 */
3447 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003448ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003449{
3450 return compl_started;
3451}
3452
3453/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003454 * Delete one character before the cursor and show the subset of the matches
3455 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003456 * Returns the character to be used, NUL if the work is done and another char
3457 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003458 */
3459 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003460ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003461{
3462 char_u *line;
3463 char_u *p;
3464
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003465 line = ml_get_curline();
3466 p = line + curwin->w_cursor.col;
3467 mb_ptr_back(line, p);
3468
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003469 /* Stop completion when the whole word was deleted. For Omni completion
3470 * allow the word to be deleted, we won't match everything. */
3471 if ((int)(p - line) - (int)compl_col < 0
3472 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003473 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003474 return K_BS;
3475
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003476 /* Deleted more than what was used to find matches or didn't finish
3477 * finding all matches: need to look for matches all over again. */
3478 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003479 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003480 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003481
Bram Moolenaara6557602006-02-04 22:43:20 +00003482 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003483 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003484 if (compl_leader != NULL)
3485 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003486 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003487 if (compl_shown_match != NULL)
3488 /* Make sure current match is not a hidden item. */
3489 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003490 return NUL;
3491 }
3492 return K_BS;
3493}
Bram Moolenaara6557602006-02-04 22:43:20 +00003494
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003495/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003496 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3497 * be called.
3498 */
3499 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003500ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003501{
3502 /* Return TRUE if we didn't complete finding matches or when the
3503 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3504 return compl_was_interrupted
3505 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3506 && compl_opt_refresh_always);
3507}
3508
3509/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003510 * Called after changing "compl_leader".
3511 * Show the popup menu with a different set of matches.
3512 * May also search for matches again if the previous search was interrupted.
3513 */
3514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003515ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003516{
3517 ins_compl_del_pum();
3518 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003519 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003520 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003521
3522 if (compl_started)
3523 ins_compl_set_original_text(compl_leader);
3524 else
3525 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003526#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003527 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003528#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003529 /*
3530 * Matches were cleared, need to search for them now. First display
3531 * the changed text before the cursor. Set "compl_restarting" to
3532 * avoid that the first match is inserted.
3533 */
3534 update_screen(0);
3535#ifdef FEAT_GUI
3536 if (gui.in_use)
3537 {
3538 /* Show the cursor after the match, not after the redrawn text. */
3539 setcursor();
3540 out_flush();
3541 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003542 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003543#endif
3544 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003545 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003546 compl_cont_status = 0;
3547 compl_restarting = FALSE;
3548 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003549
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003550 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003551
3552 /* Show the popup menu with a different set of matches. */
3553 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003554
3555 /* Don't let Enter select the original text when there is no popup menu. */
3556 if (compl_match_array == NULL)
3557 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003558}
3559
3560/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003561 * Return the length of the completion, from the completion start column to
3562 * the cursor column. Making sure it never goes below zero.
3563 */
3564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003565ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003566{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003567 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003568
3569 if (off < 0)
3570 return 0;
3571 return off;
3572}
3573
3574/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003575 * Append one character to the match leader. May reduce the number of
3576 * matches.
3577 */
3578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003579ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003580{
3581#ifdef FEAT_MBYTE
3582 int cc;
3583
3584 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3585 {
3586 char_u buf[MB_MAXBYTES + 1];
3587
3588 (*mb_char2bytes)(c, buf);
3589 buf[cc] = NUL;
3590 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003591 if (compl_opt_refresh_always)
3592 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003593 }
3594 else
3595#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003596 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003597 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003598 if (compl_opt_refresh_always)
3599 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003600 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003601
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003602 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003603 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003604 ins_compl_restart();
3605
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003606 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003607 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003608 * break redo. */
3609 if (!compl_opt_refresh_always)
3610 {
3611 vim_free(compl_leader);
3612 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003613 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003614 if (compl_leader != NULL)
3615 ins_compl_new_leader();
3616 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003617}
3618
3619/*
3620 * Setup for finding completions again without leaving CTRL-X mode. Used when
3621 * BS or a key was typed while still searching for matches.
3622 */
3623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003624ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003625{
3626 ins_compl_free();
3627 compl_started = FALSE;
3628 compl_matches = 0;
3629 compl_cont_status = 0;
3630 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003631}
3632
3633/*
3634 * Set the first match, the original text.
3635 */
3636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003637ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003638{
3639 char_u *p;
3640
3641 /* Replace the original text entry. */
3642 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3643 {
3644 p = vim_strsave(str);
3645 if (p != NULL)
3646 {
3647 vim_free(compl_first_match->cp_str);
3648 compl_first_match->cp_str = p;
3649 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003650 }
3651}
3652
3653/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003654 * Append one character to the match leader. May reduce the number of
3655 * matches.
3656 */
3657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003658ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003659{
3660 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003661 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003662 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003663 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003664
3665 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003666 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003667 {
3668 /* When still at the original match use the first entry that matches
3669 * the leader. */
3670 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3671 {
3672 p = NULL;
3673 for (cp = compl_shown_match->cp_next; cp != NULL
3674 && cp != compl_first_match; cp = cp->cp_next)
3675 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003676 if (compl_leader == NULL
3677 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003678 (int)STRLEN(compl_leader)))
3679 {
3680 p = cp->cp_str;
3681 break;
3682 }
3683 }
3684 if (p == NULL || (int)STRLEN(p) <= len)
3685 return;
3686 }
3687 else
3688 return;
3689 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003690 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003691 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003692 ins_compl_addleader(c);
3693}
3694
3695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003697 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003698 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003700 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003701ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702{
3703 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003705 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
3707 /* Forget any previous 'special' messages if this is actually
3708 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3709 */
3710 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3711 edit_submode_extra = NULL;
3712
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003713 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003714 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3715 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003716 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003718 /* Set "compl_get_longest" when finding the first matches. */
3719 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3720 || (ctrl_x_mode == 0 && !compl_started))
3721 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003722 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003723 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003724
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003725 }
3726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3728 {
3729 /*
3730 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3731 * it will be yet. Now we decide.
3732 */
3733 switch (c)
3734 {
3735 case Ctrl_E:
3736 case Ctrl_Y:
3737 ctrl_x_mode = CTRL_X_SCROLL;
3738 if (!(State & REPLACE_FLAG))
3739 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3740 else
3741 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3742 edit_submode_pre = NULL;
3743 showmode();
3744 break;
3745 case Ctrl_L:
3746 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3747 break;
3748 case Ctrl_F:
3749 ctrl_x_mode = CTRL_X_FILES;
3750 break;
3751 case Ctrl_K:
3752 ctrl_x_mode = CTRL_X_DICTIONARY;
3753 break;
3754 case Ctrl_R:
3755 /* Simply allow ^R to happen without affecting ^X mode */
3756 break;
3757 case Ctrl_T:
3758 ctrl_x_mode = CTRL_X_THESAURUS;
3759 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003760#ifdef FEAT_COMPL_FUNC
3761 case Ctrl_U:
3762 ctrl_x_mode = CTRL_X_FUNCTION;
3763 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003764 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003765 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003766 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003767#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003768 case 's':
3769 case Ctrl_S:
3770 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003771#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003772 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003773 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003774 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003775#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003776 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 case Ctrl_RSB:
3778 ctrl_x_mode = CTRL_X_TAGS;
3779 break;
3780#ifdef FEAT_FIND_ID
3781 case Ctrl_I:
3782 case K_S_TAB:
3783 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3784 break;
3785 case Ctrl_D:
3786 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3787 break;
3788#endif
3789 case Ctrl_V:
3790 case Ctrl_Q:
3791 ctrl_x_mode = CTRL_X_CMDLINE;
3792 break;
3793 case Ctrl_P:
3794 case Ctrl_N:
3795 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3796 * just started ^X mode, or there were enough ^X's to cancel
3797 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3798 * do normal expansion when interrupting a different mode (say
3799 * ^X^F^X^P or ^P^X^X^P, see below)
3800 * nothing changes if interrupting mode 0, (eg, the flag
3801 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003802 if (!(compl_cont_status & CONT_INTRPT))
3803 compl_cont_status |= CONT_LOCAL;
3804 else if (compl_cont_mode != 0)
3805 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 /* FALLTHROUGH */
3807 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003808 /* If we have typed at least 2 ^X's... for modes != 0, we set
3809 * compl_cont_status = 0 (eg, as if we had just started ^X
3810 * mode).
3811 * For mode 0, we set "compl_cont_mode" to an impossible
3812 * value, in both cases ^X^X can be used to restart the same
3813 * mode (avoiding ADDING mode).
3814 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3815 * 'complete' and local ^P expansions respectively.
3816 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3817 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if (c == Ctrl_X)
3819 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003820 if (compl_cont_mode != 0)
3821 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003823 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825 ctrl_x_mode = 0;
3826 edit_submode = NULL;
3827 showmode();
3828 break;
3829 }
3830 }
3831 else if (ctrl_x_mode != 0)
3832 {
3833 /* We're already in CTRL-X mode, do we stay in it? */
3834 if (!vim_is_ctrl_x_key(c))
3835 {
3836 if (ctrl_x_mode == CTRL_X_SCROLL)
3837 ctrl_x_mode = 0;
3838 else
3839 ctrl_x_mode = CTRL_X_FINISHED;
3840 edit_submode = NULL;
3841 }
3842 showmode();
3843 }
3844
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003845 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
3847 /* Show error message from attempted keyword completion (probably
3848 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003849 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003851 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3852 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 || ctrl_x_mode == CTRL_X_FINISHED)
3854 {
3855 /* Get here when we have finished typing a sequence of ^N and
3856 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003857 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003858 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
3860 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003861 * If any of the original typed text has been changed, eg when
3862 * ignorecase is set, we must add back-spaces to the redo
3863 * buffer. We add as few as necessary to delete just the part
3864 * of the original text that has changed.
3865 * When using the longest match, edited the match or used
3866 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003868 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3869 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003870 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003871 ptr = NULL;
3872 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
3874
3875#ifdef FEAT_CINDENT
3876 want_cindent = (can_cindent && cindent_on());
3877#endif
3878 /*
3879 * When completing whole lines: fix indent for 'cindent'.
3880 * Otherwise, break line if it's too long.
3881 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003882 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
3884#ifdef FEAT_CINDENT
3885 /* re-indent the current line */
3886 if (want_cindent)
3887 {
3888 do_c_expr_indent();
3889 want_cindent = FALSE; /* don't do it again */
3890 }
3891#endif
3892 }
3893 else
3894 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003895 int prev_col = curwin->w_cursor.col;
3896
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003898 if (prev_col > 0)
3899 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003900 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003901 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003903 if (prev_col > 0
3904 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3905 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003908 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003909 * the selection without inserting anything. When
3910 * compl_enter_selects is set the Enter key does the same. */
3911 if ((c == Ctrl_Y || (compl_enter_selects
3912 && (c == CAR || c == K_KENTER || c == NL)))
3913 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003914 retval = TRUE;
3915
Bram Moolenaar47247282016-08-02 22:36:02 +02003916 /* CTRL-E means completion is Ended, go back to the typed text.
3917 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003918 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003919 {
3920 ins_compl_delete();
3921 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003922 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003923 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003924 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003925 retval = TRUE;
3926 }
3927
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003928 auto_format(FALSE, TRUE);
3929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003931 compl_started = FALSE;
3932 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003933 if (!shortmess(SHM_COMPLETIONMENU))
3934 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003936 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 if (edit_submode != NULL)
3938 {
3939 edit_submode = NULL;
3940 showmode();
3941 }
3942
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003943#ifdef FEAT_CMDWIN
3944 if (c == Ctrl_C && cmdwin_type != 0)
3945 /* Avoid the popup menu remains displayed when leaving the
3946 * command line window. */
3947 update_screen(0);
3948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949#ifdef FEAT_CINDENT
3950 /*
3951 * Indent now if a key was typed that is in 'cinkeys'.
3952 */
3953 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3954 do_c_expr_indent();
3955#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003956#ifdef FEAT_AUTOCMD
3957 /* Trigger the CompleteDone event to give scripts a chance to act
3958 * upon the completion. */
3959 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
3962 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003963#ifdef FEAT_AUTOCMD
3964 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3965 /* Trigger the CompleteDone event to give scripts a chance to act
3966 * upon the (possibly failed) completion. */
3967 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970 /* reset continue_* if we left expansion-mode, if we stay they'll be
3971 * (re)set properly in ins_complete() */
3972 if (!vim_is_ctrl_x_key(c))
3973 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003974 compl_cont_status = 0;
3975 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003977
3978 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979}
3980
3981/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003982 * Fix the redo buffer for the completion leader replacing some of the typed
3983 * text. This inserts backspaces and appends the changed text.
3984 * "ptr" is the known leader text or NUL.
3985 */
3986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003987ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003988{
3989 int len;
3990 char_u *p;
3991 char_u *ptr = ptr_arg;
3992
3993 if (ptr == NULL)
3994 {
3995 if (compl_leader != NULL)
3996 ptr = compl_leader;
3997 else
3998 return; /* nothing to do */
3999 }
4000 if (compl_orig_text != NULL)
4001 {
4002 p = compl_orig_text;
4003 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4004 ;
4005#ifdef FEAT_MBYTE
4006 if (len > 0)
4007 len -= (*mb_head_off)(p, p + len);
4008#endif
4009 for (p += len; *p != NUL; mb_ptr_adv(p))
4010 AppendCharToRedobuff(K_BS);
4011 }
4012 else
4013 len = 0;
4014 if (ptr != NULL)
4015 AppendToRedobuffLit(ptr + len, -1);
4016}
4017
4018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4020 * (depending on flag) starting from buf and looking for a non-scanned
4021 * buffer (other than curbuf). curbuf is special, if it is called with
4022 * buf=curbuf then it has to be the first call for a given flag/expansion.
4023 *
4024 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4025 */
4026 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004027ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
4029#ifdef FEAT_WINDOWS
4030 static win_T *wp;
4031#endif
4032
4033 if (flag == 'w') /* just windows */
4034 {
4035#ifdef FEAT_WINDOWS
4036 if (buf == curbuf) /* first call for this flag/expansion */
4037 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004038 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 && wp->w_buffer->b_scanned)
4040 ;
4041 buf = wp->w_buffer;
4042#else
4043 buf = curbuf;
4044#endif
4045 }
4046 else
4047 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4048 * (unlisted buffers)
4049 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004050 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 && ((flag == 'U'
4052 ? buf->b_p_bl
4053 : (!buf->b_p_bl
4054 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004055 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 ;
4057 return buf;
4058}
4059
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004060#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004061/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004062 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004063 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004064 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004066expand_by_function(
4067 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4068 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004069{
Bram Moolenaar82139082011-09-14 16:52:09 +02004070 list_T *matchlist = NULL;
4071 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004072 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004073 char_u *funcname;
4074 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004075 win_T *curwin_save;
4076 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004077 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004078
Bram Moolenaare344bea2005-09-01 20:46:49 +00004079 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4080 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004081 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004082
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004083 /* Call 'completefunc' to obtain the list of matches. */
4084 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004085 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004086
Bram Moolenaare344bea2005-09-01 20:46:49 +00004087 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004088 curwin_save = curwin;
4089 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004090
4091 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004092 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004093 {
4094 switch (rettv.v_type)
4095 {
4096 case VAR_LIST:
4097 matchlist = rettv.vval.v_list;
4098 break;
4099 case VAR_DICT:
4100 matchdict = rettv.vval.v_dict;
4101 break;
4102 default:
4103 /* TODO: Give error message? */
4104 clear_tv(&rettv);
4105 break;
4106 }
4107 }
4108
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004109 if (curwin_save != curwin || curbuf_save != curbuf)
4110 {
4111 EMSG(_(e_complwin));
4112 goto theend;
4113 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004114 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004115 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004116 if (!equalpos(curwin->w_cursor, pos))
4117 {
4118 EMSG(_(e_compldel));
4119 goto theend;
4120 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004121
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004122 if (matchlist != NULL)
4123 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004124 else if (matchdict != NULL)
4125 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004126
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004127theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004128 if (matchdict != NULL)
4129 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004130 if (matchlist != NULL)
4131 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004132}
4133#endif /* FEAT_COMPL_FUNC */
4134
Bram Moolenaar39f05632006-03-19 22:15:26 +00004135#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004136/*
4137 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004138 */
4139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004140ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004141{
4142 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004143 int dir = compl_direction;
4144
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004145 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004146 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004147 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004148 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4149 /* if dir was BACKWARD then honor it just once */
4150 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004151 else if (did_emsg)
4152 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004153 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004154}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004155
4156/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004157 * Add completions from a dict.
4158 */
4159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004160ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004161{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004162 dictitem_T *di_refresh;
4163 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004164
4165 /* Check for optional "refresh" item. */
4166 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004167 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4168 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004169 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004170 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004171
4172 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4173 compl_opt_refresh_always = TRUE;
4174 }
4175
4176 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004177 di_words = dict_find(dict, (char_u *)"words", 5);
4178 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4179 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004180}
4181
4182/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004183 * Add a match to the list of matches from a typeval_T.
4184 * If the given string is already in the list of completions, then return
4185 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4186 * maybe because alloc() returns NULL, then FAIL is returned.
4187 */
4188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004189ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004190{
4191 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004192 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004193 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004194 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004195 char_u *(cptext[CPT_COUNT]);
4196
4197 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4198 {
4199 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4200 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4201 (char_u *)"abbr", FALSE);
4202 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4203 (char_u *)"menu", FALSE);
4204 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4205 (char_u *)"kind", FALSE);
4206 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4207 (char_u *)"info", FALSE);
4208 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4209 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004210 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004211 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004212 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4213 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004214 }
4215 else
4216 {
4217 word = get_tv_string_chk(tv);
4218 vim_memset(cptext, 0, sizeof(cptext));
4219 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004220 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004221 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004222 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004223}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004224#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004225
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004226/*
4227 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004228 * The search starts at position "ini" in curbuf and in the direction
4229 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004230 * When "compl_started" is FALSE start at that position, otherwise continue
4231 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004232 * This may return before finding all the matches.
4233 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 */
4235 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004236ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
4238 static pos_T first_match_pos;
4239 static pos_T last_match_pos;
4240 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241 static int found_all = FALSE; /* Found all matches of a
4242 certain type. */
4243 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
Bram Moolenaar572cb562005-08-05 21:35:02 +00004245 pos_T *pos;
4246 char_u **matches;
4247 int save_p_scs;
4248 int save_p_ws;
4249 int save_p_ic;
4250 int i;
4251 int num_matches;
4252 int len;
4253 int found_new_match;
4254 int type = ctrl_x_mode;
4255 char_u *ptr;
4256 char_u *dict = NULL;
4257 int dict_f = 0;
4258 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004259 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004261 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004263 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 ins_buf->b_scanned = 0;
4265 found_all = FALSE;
4266 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004267 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004268 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 last_match_pos = first_match_pos = *ini;
4270 }
4271
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004273 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4275 for (;;)
4276 {
4277 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004278 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004280 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 * or if found_all says this entry is done. For ^X^L only use the
4282 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004283 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004284 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
4286 found_all = FALSE;
4287 while (*e_cpt == ',' || *e_cpt == ' ')
4288 e_cpt++;
4289 if (*e_cpt == '.' && !curbuf->b_scanned)
4290 {
4291 ins_buf = curbuf;
4292 first_match_pos = *ini;
4293 /* So that ^N can match word immediately after cursor */
4294 if (ctrl_x_mode == 0)
4295 dec(&first_match_pos);
4296 last_match_pos = first_match_pos;
4297 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004298
4299 /* Remember the first match so that the loop stops when we
4300 * wrap and come back there a second time. */
4301 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4304 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4305 {
4306 /* Scan a buffer, but not the current one. */
4307 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4308 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004309 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 first_match_pos.col = last_match_pos.col = 0;
4311 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4312 last_match_pos.lnum = 0;
4313 type = 0;
4314 }
4315 else /* unloaded buffer, scan like dictionary */
4316 {
4317 found_all = TRUE;
4318 if (ins_buf->b_fname == NULL)
4319 continue;
4320 type = CTRL_X_DICTIONARY;
4321 dict = ins_buf->b_fname;
4322 dict_f = DICT_EXACT;
4323 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004324 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 ins_buf->b_fname == NULL
4326 ? buf_spname(ins_buf)
4327 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004328 ? ins_buf->b_fname
4329 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004330 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332 else if (*e_cpt == NUL)
4333 break;
4334 else
4335 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004336 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 type = -1;
4338 else if (*e_cpt == 'k' || *e_cpt == 's')
4339 {
4340 if (*e_cpt == 'k')
4341 type = CTRL_X_DICTIONARY;
4342 else
4343 type = CTRL_X_THESAURUS;
4344 if (*++e_cpt != ',' && *e_cpt != NUL)
4345 {
4346 dict = e_cpt;
4347 dict_f = DICT_FIRST;
4348 }
4349 }
4350#ifdef FEAT_FIND_ID
4351 else if (*e_cpt == 'i')
4352 type = CTRL_X_PATH_PATTERNS;
4353 else if (*e_cpt == 'd')
4354 type = CTRL_X_PATH_DEFINES;
4355#endif
4356 else if (*e_cpt == ']' || *e_cpt == 't')
4357 {
4358 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004359 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004360 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 else
4363 type = -1;
4364
4365 /* in any case e_cpt is advanced to the next entry */
4366 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4367
4368 found_all = TRUE;
4369 if (type == -1)
4370 continue;
4371 }
4372 }
4373
4374 switch (type)
4375 {
4376 case -1:
4377 break;
4378#ifdef FEAT_FIND_ID
4379 case CTRL_X_PATH_PATTERNS:
4380 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004381 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004382 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004384 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4386 (linenr_T)1, (linenr_T)MAXLNUM);
4387 break;
4388#endif
4389
4390 case CTRL_X_DICTIONARY:
4391 case CTRL_X_THESAURUS:
4392 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004393 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 : (type == CTRL_X_THESAURUS
4395 ? (*curbuf->b_p_tsr == NUL
4396 ? p_tsr
4397 : curbuf->b_p_tsr)
4398 : (*curbuf->b_p_dict == NUL
4399 ? p_dict
4400 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004401 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004402 dict != NULL ? dict_f
4403 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 dict = NULL;
4405 break;
4406
4407 case CTRL_X_TAGS:
4408 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4409 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004410 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004412 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413 * of matches is found when compl_pattern is empty */
4414 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4416 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4417 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4418 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004419 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 }
4421 p_ic = save_p_ic;
4422 break;
4423
4424 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4427 {
4428
4429 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004431 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
4433 break;
4434
4435 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004436 if (expand_cmdline(&compl_xp, compl_pattern,
4437 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004439 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 break;
4441
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004442#ifdef FEAT_COMPL_FUNC
4443 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004444 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004445 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004446 break;
4447#endif
4448
Bram Moolenaar488c6512005-08-11 20:09:58 +00004449 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004450#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004451 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004452 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004453 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004454 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004455#endif
4456 break;
4457
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 default: /* normal ^P/^N and ^X^L */
4459 /*
4460 * If 'infercase' is set, don't use 'smartcase' here
4461 */
4462 save_p_scs = p_scs;
4463 if (ins_buf->b_p_inf)
4464 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004465
Bram Moolenaar361aa502014-01-23 22:45:58 +01004466 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 * end but never from the middle, thus setting nowrapscan in this
4468 * buffers is a good idea, on the other hand, we always set
4469 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4470 save_p_ws = p_ws;
4471 if (ins_buf != curbuf)
4472 p_ws = FALSE;
4473 else if (*e_cpt == '.')
4474 p_ws = TRUE;
4475 for (;;)
4476 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004477 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004479 ++msg_silent; /* Don't want messages for wrapscan. */
4480
Bram Moolenaare4214502015-03-05 18:08:43 +01004481 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4482 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004483 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004484 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004485 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004487 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004489 found_new_match = searchit(NULL, ins_buf, pos,
4490 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004491 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004492 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004493 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004494 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004496 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004497 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 first_match_pos = *pos;
4499 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004500 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004503 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 found_new_match = FAIL;
4505 if (found_new_match == FAIL)
4506 {
4507 if (ins_buf == curbuf)
4508 found_all = TRUE;
4509 break;
4510 }
4511
4512 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004513 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 && ini->lnum == pos->lnum
4515 && ini->col == pos->col)
4516 continue;
4517 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004518 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004520 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 {
4522 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4523 continue;
4524 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4525 if (!p_paste)
4526 ptr = skipwhite(ptr);
4527 }
4528 len = (int)STRLEN(ptr);
4529 }
4530 else
4531 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004532 char_u *tmp_ptr = ptr;
4533
4534 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 /* Skip if already inside a word. */
4538 if (vim_iswordp(tmp_ptr))
4539 continue;
4540 /* Find start of next word. */
4541 tmp_ptr = find_word_start(tmp_ptr);
4542 }
4543 /* Find end of this word. */
4544 tmp_ptr = find_word_end(tmp_ptr);
4545 len = (int)(tmp_ptr - ptr);
4546
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004547 if ((compl_cont_status & CONT_ADDING)
4548 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 {
4550 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4551 {
4552 /* Try next line, if any. the new word will be
4553 * "join" as if the normal command "J" was used.
4554 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 * works -- Acevedo */
4557 STRNCPY(IObuff, ptr, len);
4558 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4559 tmp_ptr = ptr = skipwhite(ptr);
4560 /* Find start of next word. */
4561 tmp_ptr = find_word_start(tmp_ptr);
4562 /* Find end of next word. */
4563 tmp_ptr = find_word_end(tmp_ptr);
4564 if (tmp_ptr > ptr)
4565 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004566 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004568 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 IObuff[len++] = ' ';
4570 /* IObuf =~ "\k.* ", thus len >= 2 */
4571 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004572 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 || (vim_strchr(p_cpo, CPO_JOINSP)
4574 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004575 && (IObuff[len - 2] == '?'
4576 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 IObuff[len++] = ' ';
4578 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004579 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (tmp_ptr - ptr >= IOSIZE - len)
4581 tmp_ptr = ptr + IOSIZE - len - 1;
4582 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4583 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004584 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 IObuff[len] = NUL;
4587 ptr = IObuff;
4588 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004589 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 continue;
4591 }
4592 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004593 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004594 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004595 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
4597 found_new_match = OK;
4598 break;
4599 }
4600 }
4601 p_scs = save_p_scs;
4602 p_ws = save_p_ws;
4603 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004604
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004605 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004606 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004607 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 found_new_match = OK;
4609
4610 /* break the loop for specialized modes (use 'complete' just for the
4611 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004612 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004613 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004614 {
4615 if (got_int)
4616 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004617 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004618 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004619 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004620
Bram Moolenaare4214502015-03-05 18:08:43 +01004621 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004622 || compl_interrupted)
4623 break;
4624 compl_started = TRUE;
4625 }
4626 else
4627 {
4628 /* Mark a buffer scanned when it has been scanned completely */
4629 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4630 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004632 compl_started = FALSE;
4633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004635 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636
Bram Moolenaare4214502015-03-05 18:08:43 +01004637 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 && *e_cpt == NUL) /* Got to end of 'complete' */
4639 found_new_match = FAIL;
4640
4641 i = -1; /* total of matches, unknown */
4642 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004643 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 i = ins_compl_make_cyclic();
4645
4646 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004647 * just been made cyclic then we have to move compl_curr_match to the next
4648 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004649 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4650 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004651 if (compl_curr_match == NULL)
4652 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 return i;
4654}
4655
4656/* Delete the old text being completed. */
4657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004658ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004660 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662 /*
4663 * In insert mode: Delete the typed part.
4664 * In replace mode: Put the old characters back, if any.
4665 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004666 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4667 if ((int)curwin->w_cursor.col > col)
4668 {
4669 if (stop_arrow() == FAIL)
4670 return;
4671 backspace_until_column(col);
4672 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004673
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004674 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4675 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004677 /* clear v:completed_item */
4678 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679}
4680
Bram Moolenaar472e8592016-10-15 17:06:47 +02004681/*
4682 * Insert the new text being completed.
4683 * "in_compl_func" is TRUE when called from complete_check().
4684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004686ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004688 dict_T *dict;
4689
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004690 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004691 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4692 compl_used_match = FALSE;
4693 else
4694 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004695
4696 /* Set completed item. */
4697 /* { word, abbr, menu, kind, info } */
4698 dict = dict_alloc();
4699 if (dict != NULL)
4700 {
4701 dict_add_nr_str(dict, "word", 0L,
4702 EMPTY_IF_NULL(compl_shown_match->cp_str));
4703 dict_add_nr_str(dict, "abbr", 0L,
4704 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4705 dict_add_nr_str(dict, "menu", 0L,
4706 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4707 dict_add_nr_str(dict, "kind", 0L,
4708 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4709 dict_add_nr_str(dict, "info", 0L,
4710 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4711 }
4712 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004713 if (!in_compl_func)
4714 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715}
4716
4717/*
4718 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004719 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4720 * get more completions. If it is FALSE, then we just do nothing when there
4721 * are no more completions in a given direction. The latter case is used when
4722 * we are still in the middle of finding completions, to allow browsing
4723 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 * Return the total number of matches, or -1 if still unknown -- webb.
4725 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004726 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4727 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 *
4729 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004730 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4731 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 */
4733 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004734ins_compl_next(
4735 int allow_get_expansion,
4736 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004737 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004738 int insert_match, /* Insert the newly selected match */
4739 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
4741 int num_matches = -1;
4742 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004743 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004744 compl_T *found_compl = NULL;
4745 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004746 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004747 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004749 /* When user complete function return -1 for findstart which is next
4750 * time of 'always', compl_shown_match become NULL. */
4751 if (compl_shown_match == NULL)
4752 return -1;
4753
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004754 if (compl_leader != NULL
4755 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004757 /* Set "compl_shown_match" to the actually shown match, it may differ
4758 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004759 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004760 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004761 && compl_shown_match->cp_next != NULL
4762 && compl_shown_match->cp_next != compl_first_match)
4763 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004764
4765 /* If we didn't find it searching forward, and compl_shows_dir is
4766 * backward, find the last match. */
4767 if (compl_shows_dir == BACKWARD
4768 && !ins_compl_equal(compl_shown_match,
4769 compl_leader, (int)STRLEN(compl_leader))
4770 && (compl_shown_match->cp_next == NULL
4771 || compl_shown_match->cp_next == compl_first_match))
4772 {
4773 while (!ins_compl_equal(compl_shown_match,
4774 compl_leader, (int)STRLEN(compl_leader))
4775 && compl_shown_match->cp_prev != NULL
4776 && compl_shown_match->cp_prev != compl_first_match)
4777 compl_shown_match = compl_shown_match->cp_prev;
4778 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004779 }
4780
4781 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004782 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 /* Delete old text to be replaced */
4784 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004785
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004786 /* When finding the longest common text we stick at the original text,
4787 * don't let CTRL-N or CTRL-P move to the first match. */
4788 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4789
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004790 /* When restarting the search don't insert the first match either. */
4791 if (compl_restarting)
4792 {
4793 advance = FALSE;
4794 compl_restarting = FALSE;
4795 }
4796
Bram Moolenaare3226be2005-12-18 22:10:00 +00004797 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4798 * around. */
4799 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004801 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004803 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004804 found_end = (compl_first_match != NULL
4805 && (compl_shown_match->cp_next == compl_first_match
4806 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004807 }
4808 else if (compl_shows_dir == BACKWARD
4809 && compl_shown_match->cp_prev != NULL)
4810 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004811 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004812 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004813 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
4815 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004816 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004817 if (!allow_get_expansion)
4818 {
4819 if (advance)
4820 {
4821 if (compl_shows_dir == BACKWARD)
4822 compl_pending -= todo + 1;
4823 else
4824 compl_pending += todo + 1;
4825 }
4826 return -1;
4827 }
4828
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004829 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004830 {
4831 if (compl_shows_dir == BACKWARD)
4832 --compl_pending;
4833 else
4834 ++compl_pending;
4835 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004836
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004837 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004838 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004839
4840 /* handle any pending completions */
4841 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004842 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004843 {
4844 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4845 {
4846 compl_shown_match = compl_shown_match->cp_next;
4847 --compl_pending;
4848 }
4849 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4850 {
4851 compl_shown_match = compl_shown_match->cp_prev;
4852 ++compl_pending;
4853 }
4854 else
4855 break;
4856 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004857 found_end = FALSE;
4858 }
4859 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4860 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004861 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004862 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004863 ++todo;
4864 else
4865 /* Remember a matching item. */
4866 found_compl = compl_shown_match;
4867
4868 /* Stop at the end of the list when we found a usable match. */
4869 if (found_end)
4870 {
4871 if (found_compl != NULL)
4872 {
4873 compl_shown_match = found_compl;
4874 break;
4875 }
4876 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004880 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004881 if (compl_no_insert && !started)
4882 {
4883 ins_bytes(compl_orig_text + ins_compl_len());
4884 compl_used_match = FALSE;
4885 }
4886 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004887 {
4888 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004889 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004890 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004891 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004892 }
4893 else
4894 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
4896 if (!allow_get_expansion)
4897 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004898 /* may undisplay the popup menu first */
4899 ins_compl_upd_pum();
4900
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004901 /* redraw to show the user what was inserted */
4902 update_screen(0);
4903
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004904 /* display the updated popup menu */
4905 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004906#ifdef FEAT_GUI
4907 if (gui.in_use)
4908 {
4909 /* Show the cursor after the match, not after the redrawn text. */
4910 setcursor();
4911 out_flush();
4912 gui_update_cursor(FALSE, FALSE);
4913 }
4914#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004915
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 /* Delete old text to be replaced, since we're still searching and
4917 * don't want to match ourselves! */
4918 ins_compl_delete();
4919 }
4920
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004921 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004922 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004923 if (compl_no_insert && !started)
4924 compl_enter_selects = TRUE;
4925 else
4926 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004927
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 /*
4929 * Show the file name for the match (if any)
4930 * Truncate the file name to avoid a wait for return.
4931 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004932 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 {
4934 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004935 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (i <= 0)
4937 i = 0;
4938 else
4939 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004940 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 msg(IObuff);
4942 redraw_cmdline = FALSE; /* don't overwrite! */
4943 }
4944
4945 return num_matches;
4946}
4947
4948/*
4949 * Call this while finding completions, to check whether the user has hit a key
4950 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004951 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004953 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004954 * "in_compl_func" is TRUE when called from complete_check(), don't set
4955 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 */
4957 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004958ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959{
4960 static int count = 0;
4961
4962 int c;
4963
4964 /* Don't check when reading keys from a script. That would break the test
4965 * scripts */
4966 if (using_script())
4967 return;
4968
4969 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004970 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 return;
4972 count = 0;
4973
Bram Moolenaara260a972006-06-23 19:36:29 +00004974 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4975 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (c != NUL)
4978 {
4979 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4980 {
4981 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004982 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004983 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02004984 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004986 else
4987 {
4988 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004989 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004990 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004991 if (c != K_IGNORE)
4992 {
4993 /* Don't interrupt completion when the character wasn't typed,
4994 * e.g., when doing @q to replay keys. */
4995 if (c != Ctrl_R && KeyTyped)
4996 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004997
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004998 vungetc(c);
4999 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005002 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005003 {
5004 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5005
5006 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005007 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005008 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005009}
5010
5011/*
5012 * Decide the direction of Insert mode complete from the key typed.
5013 * Returns BACKWARD or FORWARD.
5014 */
5015 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005016ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005017{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005018 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005019 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005020 return BACKWARD;
5021 return FORWARD;
5022}
5023
5024/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005025 * Return TRUE for keys that are used for completion only when the popup menu
5026 * is visible.
5027 */
5028 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005029ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005030{
5031 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005032 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5033 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005034}
5035
5036/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005037 * Decide the number of completions to move forward.
5038 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5039 */
5040 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005041ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005042{
5043 int h;
5044
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005045 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005046 {
5047 h = pum_get_height();
5048 if (h > 3)
5049 h -= 2; /* keep some context */
5050 return h;
5051 }
5052 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053}
5054
5055/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005056 * Return TRUE if completion with "c" should insert the match, FALSE if only
5057 * to change the currently selected completion.
5058 */
5059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005060ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005061{
5062 switch (c)
5063 {
5064 case K_UP:
5065 case K_DOWN:
5066 case K_PAGEDOWN:
5067 case K_KPAGEDOWN:
5068 case K_S_DOWN:
5069 case K_PAGEUP:
5070 case K_KPAGEUP:
5071 case K_S_UP:
5072 return FALSE;
5073 }
5074 return TRUE;
5075}
5076
5077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 * Do Insert mode completion.
5079 * Called when character "c" was typed, which has a meaning for completion.
5080 * Returns OK if completion was done, FAIL if something failed (out of mem).
5081 */
5082 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005083ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005085 char_u *line;
5086 int startcol = 0; /* column where searched text starts */
5087 colnr_T curs_col; /* cursor column */
5088 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005089 int save_w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005090 int insert_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
Bram Moolenaare3226be2005-12-18 22:10:00 +00005092 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005093 insert_match = ins_compl_use_match(c);
5094
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005095 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 /* First time we hit ^N or ^P (in a row, I mean) */
5098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 did_ai = FALSE;
5100#ifdef FEAT_SMARTINDENT
5101 did_si = FALSE;
5102 can_si = FALSE;
5103 can_si_back = FALSE;
5104#endif
5105 if (stop_arrow() == FAIL)
5106 return FAIL;
5107
5108 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005109 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005110 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005112 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005113 * "compl_startpos" to the cursor as a pattern to add a new word
5114 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005115 * "compl_startpos" is not in the same line as the cursor then fix it
5116 * (the line has been split because it was longer than 'tw'). if SOL
5117 * is set then skip the previous pattern, a word at the beginning of
5118 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005119 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5120 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 /*
5123 * it is a continued search
5124 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5127 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5128 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005131 /* line (probably) wrapped, set compl_startpos to the
5132 * first non_blank in the line, if it is not a wordchar
5133 * include it to get a better pattern, but then we don't
5134 * want the "\\<" prefix, check it bellow */
5135 compl_col = (colnr_T)(skipwhite(line) - line);
5136 compl_startpos.col = compl_col;
5137 compl_startpos.lnum = curwin->w_cursor.lnum;
5138 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 else
5141 {
5142 /* S_IPOS was set when we inserted a word that was at the
5143 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005144 * mode but first we need to redefine compl_startpos */
5145 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005147 compl_cont_status |= CONT_SOL;
5148 compl_startpos.col = (colnr_T)(skipwhite(
5149 line + compl_length
5150 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005154 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005155 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005156 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005158 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005160 compl_cont_status &= ~CONT_SOL;
5161 compl_length = (IOSIZE - MIN_SPACE);
5162 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005164 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5165 if (compl_length < 1)
5166 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005168 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005169 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005171 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005174 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005176 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005178 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005180 compl_cont_status = 0;
5181 compl_cont_status |= CONT_N_ADDS;
5182 compl_startpos = curwin->w_cursor;
5183 startcol = (int)curs_col;
5184 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186
5187 /* Work out completion pattern and original text -- webb */
5188 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5189 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5192 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005193 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005195 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005197 compl_col += ++startcol;
5198 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005201 compl_pattern = str_foldcase(line + compl_col,
5202 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005204 compl_pattern = vim_strnsave(line + compl_col,
5205 compl_length);
5206 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 return FAIL;
5208 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005209 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
5211 char_u *prefix = (char_u *)"\\<";
5212
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005213 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005214 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005215 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005216 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005218 if (!vim_iswordp(line + compl_col)
5219 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 && (
5221#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005222 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005224 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225#endif
5226 )))
5227 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 STRCPY((char *)compl_pattern, prefix);
5229 (void)quote_meta(compl_pattern + STRLEN(prefix),
5230 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005232 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005234 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237#endif
5238 )
5239 {
5240 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005241 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5242 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 compl_col += curs_col;
5245 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 }
5247 else
5248 {
5249#ifdef FEAT_MBYTE
5250 /* Search the point of change class of multibyte character
5251 * or not a word single byte character backward. */
5252 if (has_mbyte)
5253 {
5254 int base_class;
5255 int head_off;
5256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 startcol -= (*mb_head_off)(line, line + startcol);
5258 base_class = mb_get_class(line + startcol);
5259 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005261 head_off = (*mb_head_off)(line, line + startcol);
5262 if (base_class != mb_get_class(line + startcol
5263 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005265 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
5267 }
5268 else
5269#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005270 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005272 compl_col += ++startcol;
5273 compl_length = (int)curs_col - startcol;
5274 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
5276 /* Only match word with at least two chars -- webb
5277 * there's no need to call quote_meta,
5278 * alloc(7) is enough -- Acevedo
5279 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 compl_pattern = alloc(7);
5281 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005283 STRCPY((char *)compl_pattern, "\\<");
5284 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5285 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 }
5287 else
5288 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005289 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005290 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005293 STRCPY((char *)compl_pattern, "\\<");
5294 (void)quote_meta(compl_pattern + 2, line + compl_col,
5295 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
5297 }
5298 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005299 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005301 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005302 compl_length = (int)curs_col - (int)compl_col;
5303 if (compl_length < 0) /* cursor in indent: empty pattern */
5304 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005306 compl_pattern = str_foldcase(line + compl_col, compl_length,
5307 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005309 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5310 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 return FAIL;
5312 }
5313 else if (ctrl_x_mode == CTRL_X_FILES)
5314 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005315 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005316 if (startcol > 0)
5317 {
5318 char_u *p = line + startcol;
5319
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005320 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005321 while (p > line && vim_isfilec(PTR2CHAR(p)))
5322 mb_ptr_back(line, p);
5323 if (p == line && vim_isfilec(PTR2CHAR(p)))
5324 startcol = 0;
5325 else
5326 startcol = (int)(p - line) + 1;
5327 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005328
Bram Moolenaar0300e462013-09-08 16:03:45 +02005329 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005330 compl_length = (int)curs_col - startcol;
5331 compl_pattern = addstar(line + compl_col, compl_length,
5332 EXPAND_FILES);
5333 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 return FAIL;
5335 }
5336 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5337 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 compl_pattern = vim_strnsave(line, curs_col);
5339 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005341 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005342 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005343 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5344 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005345 /* No completion possible, use an empty pattern to get a
5346 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005347 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005348 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005349 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5350 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005352 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005353 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005354#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005355 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005356 * Call user defined function 'completefunc' with "a:findstart"
5357 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005358 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005359 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005360 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005361 char_u *funcname;
5362 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005363 win_T *curwin_save;
5364 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005365
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005366 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005367 * string */
5368 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5369 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5370 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005371 {
5372 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5373 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005374 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005375 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005376
5377 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005378 args[1] = NULL;
5379 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005380 curwin_save = curwin;
5381 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005382 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005383 if (curwin_save != curwin || curbuf_save != curbuf)
5384 {
5385 EMSG(_(e_complwin));
5386 return FAIL;
5387 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005388 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005389 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005390 if (!equalpos(curwin->w_cursor, pos))
5391 {
5392 EMSG(_(e_compldel));
5393 return FAIL;
5394 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005395
Bram Moolenaar6110a002012-01-26 18:58:38 +01005396 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005397 * cancel the complete without an error.
5398 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005399 if (col == -2)
5400 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005401 if (col == -3)
5402 {
5403 ctrl_x_mode = 0;
5404 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005405 if (!shortmess(SHM_COMPLETIONMENU))
5406 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005407 return FAIL;
5408 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005409
Bram Moolenaar82139082011-09-14 16:52:09 +02005410 /*
5411 * Reset extended parameters of completion, when start new
5412 * completion.
5413 */
5414 compl_opt_refresh_always = FALSE;
5415
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005416 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005417 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005418 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005419 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005420 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005421
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005422 /* Setup variables for completion. Need to obtain "line" again,
5423 * it may have become invalid. */
5424 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005425 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005426 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5427 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005428#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005429 return FAIL;
5430 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005431 else if (ctrl_x_mode == CTRL_X_SPELL)
5432 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005433#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005434 if (spell_bad_len > 0)
5435 compl_col = curs_col - spell_bad_len;
5436 else
5437 compl_col = spell_word_start(startcol);
5438 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005439 {
5440 compl_length = 0;
5441 compl_col = curs_col;
5442 }
5443 else
5444 {
5445 spell_expand_check_cap(compl_col);
5446 compl_length = (int)curs_col - compl_col;
5447 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005448 /* Need to obtain "line" again, it may have become invalid. */
5449 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005450 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5451 if (compl_pattern == NULL)
5452#endif
5453 return FAIL;
5454 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005455 else
5456 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005457 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005458 return FAIL;
5459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005461 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
5463 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005464 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
5466 /* Insert a new line, keep indentation but ignore 'comments' */
5467#ifdef FEAT_COMMENTS
5468 char_u *old = curbuf->b_p_com;
5469
5470 curbuf->b_p_com = (char_u *)"";
5471#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005472 compl_startpos.lnum = curwin->w_cursor.lnum;
5473 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 ins_eol('\r');
5475#ifdef FEAT_COMMENTS
5476 curbuf->b_p_com = old;
5477#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005478 compl_length = 0;
5479 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
5481 }
5482 else
5483 {
5484 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005485 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005488 if (compl_cont_status & CONT_LOCAL)
5489 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 else
5491 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5492
Bram Moolenaar62951b12011-09-21 18:23:05 +02005493 /* If any of the original typed text has been changed we need to fix
5494 * the redo buffer. */
5495 ins_compl_fixRedoBufForLeader(NULL);
5496
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005497 /* Always add completion for the original text. */
5498 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005499 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5500 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005501 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005503 vim_free(compl_pattern);
5504 compl_pattern = NULL;
5505 vim_free(compl_orig_text);
5506 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 return FAIL;
5508 }
5509
5510 /* showmode might reset the internal line pointers, so it must
5511 * be called before line = ml_get(), or when this address is no
5512 * longer needed. -- Acevedo.
5513 */
5514 edit_submode_extra = (char_u *)_("-- Searching...");
5515 edit_submode_highl = HLF_COUNT;
5516 showmode();
5517 edit_submode_extra = NULL;
5518 out_flush();
5519 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005520 else if (insert_match && stop_arrow() == FAIL)
5521 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005523 compl_shown_match = compl_curr_match;
5524 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
5526 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005527 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005529 save_w_wrow = curwin->w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005530 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005532 /* may undisplay the popup menu */
5533 ins_compl_upd_pum();
5534
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005535 if (n > 1) /* all matches have been found */
5536 compl_matches = n;
5537 compl_curr_match = compl_shown_match;
5538 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
Bram Moolenaard68071d2006-05-02 22:08:30 +00005540 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5541 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 if (got_int && !global_busy)
5543 {
5544 (void)vgetc();
5545 got_int = FALSE;
5546 }
5547
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005548 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005549 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005551 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5552 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5554 edit_submode_highl = HLF_E;
5555 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5556 * because we couldn't expand anything at first place, but if we used
5557 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5558 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005559 if ( compl_length > 1
5560 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 || (ctrl_x_mode != 0
5562 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5563 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005564 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566
Bram Moolenaar572cb562005-08-05 21:35:02 +00005567 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005568 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005570 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
5572 if (edit_submode_extra == NULL)
5573 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005574 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
5576 edit_submode_extra = (char_u *)_("Back at original");
5577 edit_submode_highl = HLF_W;
5578 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005579 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 {
5581 edit_submode_extra = (char_u *)_("Word from other line");
5582 edit_submode_highl = HLF_COUNT;
5583 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005584 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
5586 edit_submode_extra = (char_u *)_("The only match");
5587 edit_submode_highl = HLF_COUNT;
5588 }
5589 else
5590 {
5591 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005592 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005594 int number = 0;
5595 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005597 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
5599 /* search backwards for the first valid (!= -1) number.
5600 * This should normally succeed already at the first loop
5601 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005602 for (match = compl_curr_match->cp_prev; match != NULL
5603 && match != compl_first_match;
5604 match = match->cp_prev)
5605 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005607 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 break;
5609 }
5610 if (match != NULL)
5611 /* go up and assign all numbers which are not assigned
5612 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005613 for (match = match->cp_next;
5614 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005615 match = match->cp_next)
5616 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 }
5618 else /* BACKWARD */
5619 {
5620 /* search forwards (upwards) for the first valid (!= -1)
5621 * number. This should normally succeed already at the
5622 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005623 for (match = compl_curr_match->cp_next; match != NULL
5624 && match != compl_first_match;
5625 match = match->cp_next)
5626 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005628 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 break;
5630 }
5631 if (match != NULL)
5632 /* go down and assign all numbers which are not
5633 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005634 for (match = match->cp_prev; match
5635 && match->cp_number == -1;
5636 match = match->cp_prev)
5637 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 }
5639 }
5640
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005641 /* The match should always have a sequence number now, this is
5642 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005643 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005645 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5646 * Translations may need more than twice that. */
5647 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005649 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005650 vim_snprintf((char *)match_ref, sizeof(match_ref),
5651 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005652 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005654 vim_snprintf((char *)match_ref, sizeof(match_ref),
5655 _("match %d"),
5656 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 edit_submode_extra = match_ref;
5658 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005659 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 curs_columns(FALSE);
5661 }
5662 }
5663 }
5664
5665 /* Show a message about what (completion) mode we're in. */
5666 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005667 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005669 if (edit_submode_extra != NULL)
5670 {
5671 if (!p_smd)
5672 msg_attr(edit_submode_extra,
5673 edit_submode_highl < HLF_COUNT
5674 ? hl_attr(edit_submode_highl) : 0);
5675 }
5676 else
5677 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
Bram Moolenaard68071d2006-05-02 22:08:30 +00005680 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005681 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005682 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005683 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005684 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005685 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005686 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005687
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 return OK;
5689}
5690
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005691 static void
5692show_pum(int save_w_wrow)
5693{
5694 /* RedrawingDisabled may be set when invoked through complete(). */
5695 int n = RedrawingDisabled;
5696
5697 RedrawingDisabled = 0;
5698
5699 /* If the cursor moved we need to remove the pum first. */
5700 setcursor();
5701 if (save_w_wrow != curwin->w_wrow)
5702 ins_compl_del_pum();
5703
5704 ins_compl_show_pum();
5705 setcursor();
5706 RedrawingDisabled = n;
5707}
5708
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709/*
5710 * Looks in the first "len" chars. of "src" for search-metachars.
5711 * If dest is not NULL the chars. are copied there quoting (with
5712 * a backslash) the metachars, and dest would be NUL terminated.
5713 * Returns the length (needed) of dest
5714 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005715 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005716quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005718 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005720 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 switch (*src)
5723 {
5724 case '.':
5725 case '*':
5726 case '[':
5727 if (ctrl_x_mode == CTRL_X_DICTIONARY
5728 || ctrl_x_mode == CTRL_X_THESAURUS)
5729 break;
5730 case '~':
5731 if (!p_magic) /* quote these only if magic is set */
5732 break;
5733 case '\\':
5734 if (ctrl_x_mode == CTRL_X_DICTIONARY
5735 || ctrl_x_mode == CTRL_X_THESAURUS)
5736 break;
5737 case '^': /* currently it's not needed. */
5738 case '$':
5739 m++;
5740 if (dest != NULL)
5741 *dest++ = '\\';
5742 break;
5743 }
5744 if (dest != NULL)
5745 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005746# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 /* Copy remaining bytes of a multibyte character. */
5748 if (has_mbyte)
5749 {
5750 int i, mb_len;
5751
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005752 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (mb_len > 0 && len >= mb_len)
5754 for (i = 0; i < mb_len; ++i)
5755 {
5756 --len;
5757 ++src;
5758 if (dest != NULL)
5759 *dest++ = *src;
5760 }
5761 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 }
5764 if (dest != NULL)
5765 *dest = NUL;
5766
5767 return m;
5768}
5769#endif /* FEAT_INS_EXPAND */
5770
5771/*
5772 * Next character is interpreted literally.
5773 * A one, two or three digit decimal number is interpreted as its byte value.
5774 * If one or two digits are entered, the next character is given to vungetc().
5775 * For Unicode a character > 255 may be returned.
5776 */
5777 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005778get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
5780 int cc;
5781 int nc;
5782 int i;
5783 int hex = FALSE;
5784 int octal = FALSE;
5785#ifdef FEAT_MBYTE
5786 int unicode = 0;
5787#endif
5788
5789 if (got_int)
5790 return Ctrl_C;
5791
5792#ifdef FEAT_GUI
5793 /*
5794 * In GUI there is no point inserting the internal code for a special key.
5795 * It is more useful to insert the string "<KEY>" instead. This would
5796 * probably be useful in a text window too, but it would not be
5797 * vi-compatible (maybe there should be an option for it?) -- webb
5798 */
5799 if (gui.in_use)
5800 ++allow_keys;
5801#endif
5802#ifdef USE_ON_FLY_SCROLL
5803 dont_scroll = TRUE; /* disallow scrolling here */
5804#endif
5805 ++no_mapping; /* don't map the next key hits */
5806 cc = 0;
5807 i = 0;
5808 for (;;)
5809 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005810 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811#ifdef FEAT_CMDL_INFO
5812 if (!(State & CMDLINE)
5813# ifdef FEAT_MBYTE
5814 && MB_BYTE2LEN_CHECK(nc) == 1
5815# endif
5816 )
5817 add_to_showcmd(nc);
5818#endif
5819 if (nc == 'x' || nc == 'X')
5820 hex = TRUE;
5821 else if (nc == 'o' || nc == 'O')
5822 octal = TRUE;
5823#ifdef FEAT_MBYTE
5824 else if (nc == 'u' || nc == 'U')
5825 unicode = nc;
5826#endif
5827 else
5828 {
5829 if (hex
5830#ifdef FEAT_MBYTE
5831 || unicode != 0
5832#endif
5833 )
5834 {
5835 if (!vim_isxdigit(nc))
5836 break;
5837 cc = cc * 16 + hex2nr(nc);
5838 }
5839 else if (octal)
5840 {
5841 if (nc < '0' || nc > '7')
5842 break;
5843 cc = cc * 8 + nc - '0';
5844 }
5845 else
5846 {
5847 if (!VIM_ISDIGIT(nc))
5848 break;
5849 cc = cc * 10 + nc - '0';
5850 }
5851
5852 ++i;
5853 }
5854
5855 if (cc > 255
5856#ifdef FEAT_MBYTE
5857 && unicode == 0
5858#endif
5859 )
5860 cc = 255; /* limit range to 0-255 */
5861 nc = 0;
5862
5863 if (hex) /* hex: up to two chars */
5864 {
5865 if (i >= 2)
5866 break;
5867 }
5868#ifdef FEAT_MBYTE
5869 else if (unicode) /* Unicode: up to four or eight chars */
5870 {
5871 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5872 break;
5873 }
5874#endif
5875 else if (i >= 3) /* decimal or octal: up to three chars */
5876 break;
5877 }
5878 if (i == 0) /* no number entered */
5879 {
5880 if (nc == K_ZERO) /* NUL is stored as NL */
5881 {
5882 cc = '\n';
5883 nc = 0;
5884 }
5885 else
5886 {
5887 cc = nc;
5888 nc = 0;
5889 }
5890 }
5891
5892 if (cc == 0) /* NUL is stored as NL */
5893 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005894#ifdef FEAT_MBYTE
5895 if (enc_dbcs && (cc & 0xff) == 0)
5896 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5897 second byte will cause trouble! */
5898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
5900 --no_mapping;
5901#ifdef FEAT_GUI
5902 if (gui.in_use)
5903 --allow_keys;
5904#endif
5905 if (nc)
5906 vungetc(nc);
5907 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5908 return cc;
5909}
5910
5911/*
5912 * Insert character, taking care of special keys and mod_mask
5913 */
5914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005915insert_special(
5916 int c,
5917 int allow_modmask,
5918 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919{
5920 char_u *p;
5921 int len;
5922
5923 /*
5924 * Special function key, translate into "<Key>". Up to the last '>' is
5925 * inserted with ins_str(), so as not to replace characters in replace
5926 * mode.
5927 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5928 * unless 'allow_modmask' is TRUE.
5929 */
5930#ifdef MACOS
5931 /* Command-key never produces a normal key */
5932 if (mod_mask & MOD_MASK_CMD)
5933 allow_modmask = TRUE;
5934#endif
5935 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5936 {
5937 p = get_special_key_name(c, mod_mask);
5938 len = (int)STRLEN(p);
5939 c = p[len - 1];
5940 if (len > 2)
5941 {
5942 if (stop_arrow() == FAIL)
5943 return;
5944 p[len - 1] = NUL;
5945 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005946 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 ctrlv = FALSE;
5948 }
5949 }
5950 if (stop_arrow() == OK)
5951 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5952}
5953
5954/*
5955 * Special characters in this context are those that need processing other
5956 * than the simple insertion that can be performed here. This includes ESC
5957 * which terminates the insert, and CR/NL which need special processing to
5958 * open up a new line. This routine tries to optimize insertions performed by
5959 * the "redo", "undo" or "put" commands, so it needs to know when it should
5960 * stop and defer processing to the "normal" mechanism.
5961 * '0' and '^' are special, because they can be followed by CTRL-D.
5962 */
5963#ifdef EBCDIC
5964# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5965#else
5966# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5967#endif
5968
5969#ifdef FEAT_MBYTE
5970# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5971#else
5972# define WHITECHAR(cc) vim_iswhite(cc)
5973#endif
5974
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005975/*
5976 * "flags": INSCHAR_FORMAT - force formatting
5977 * INSCHAR_CTRLV - char typed just after CTRL-V
5978 * INSCHAR_NO_FEX - don't use 'formatexpr'
5979 *
5980 * NOTE: passes the flags value straight through to internal_format() which,
5981 * beside INSCHAR_FORMAT (above), is also looking for these:
5982 * INSCHAR_DO_COM - format comments
5983 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005986insertchar(
5987 int c, /* character to insert or NUL */
5988 int flags, /* INSCHAR_FORMAT, etc. */
5989 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 int textwidth;
5992#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005996 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005998 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000
6001 /*
6002 * Try to break the line in two or more pieces when:
6003 * - Always do this if we have been called to do formatting only.
6004 * - Always do this when 'formatoptions' has the 'a' flag and the line
6005 * ends in white space.
6006 * - Otherwise:
6007 * - Don't do this if inserting a blank
6008 * - Don't do this if an existing character is being replaced, unless
6009 * we're in VREPLACE mode.
6010 * - Do this if the cursor is not on the line where insert started
6011 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6012 * before the insert.
6013 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6014 * before 'textwidth'
6015 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006016 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006017 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 || (!vim_iswhite(c)
6019 && !((State & REPLACE_FLAG)
6020#ifdef FEAT_VREPLACE
6021 && !(State & VREPLACE_FLAG)
6022#endif
6023 && *ml_get_cursor() != NUL)
6024 && (curwin->w_cursor.lnum != Insstart.lnum
6025 || ((!has_format_option(FO_INS_LONG)
6026 || Insstart_textlen <= (colnr_T)textwidth)
6027 && (!fo_ins_blank
6028 || Insstart_blank_vcol <= (colnr_T)textwidth
6029 ))))))
6030 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006031 /* Format with 'formatexpr' when it's set. Use internal formatting
6032 * when 'formatexpr' isn't set or it returns non-zero. */
6033#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006034 int do_internal = TRUE;
6035 colnr_T virtcol = get_nolist_virtcol()
6036 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006037
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006038 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6039 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006040 {
6041 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6042 /* It may be required to save for undo again, e.g. when setline()
6043 * was called. */
6044 ins_need_undo = TRUE;
6045 }
6046 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006048 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 if (c == NUL) /* only formatting was wanted */
6052 return;
6053
6054#ifdef FEAT_COMMENTS
6055 /* Check whether this character should end a comment. */
6056 if (did_ai && (int)c == end_comment_pending)
6057 {
6058 char_u *line;
6059 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6060 int middle_len, end_len;
6061 int i;
6062
6063 /*
6064 * Need to remove existing (middle) comment leader and insert end
6065 * comment leader. First, check what comment leader we can find.
6066 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006067 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6069 {
6070 /* Skip middle-comment string */
6071 while (*p && p[-1] != ':') /* find end of middle flags */
6072 ++p;
6073 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6074 /* Don't count trailing white space for middle_len */
6075 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6076 --middle_len;
6077
6078 /* Find the end-comment string */
6079 while (*p && p[-1] != ':') /* find end of end flags */
6080 ++p;
6081 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6082
6083 /* Skip white space before the cursor */
6084 i = curwin->w_cursor.col;
6085 while (--i >= 0 && vim_iswhite(line[i]))
6086 ;
6087 i++;
6088
6089 /* Skip to before the middle leader */
6090 i -= middle_len;
6091
6092 /* Check some expected things before we go on */
6093 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6094 {
6095 /* Backspace over all the stuff we want to replace */
6096 backspace_until_column(i);
6097
6098 /*
6099 * Insert the end-comment string, except for the last
6100 * character, which will get inserted as normal later.
6101 */
6102 ins_bytes_len(lead_end, end_len - 1);
6103 }
6104 }
6105 }
6106 end_comment_pending = NUL;
6107#endif
6108
6109 did_ai = FALSE;
6110#ifdef FEAT_SMARTINDENT
6111 did_si = FALSE;
6112 can_si = FALSE;
6113 can_si_back = FALSE;
6114#endif
6115
6116 /*
6117 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6118 * This speeds up normal text input considerably.
6119 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6120 * need to re-indent at a ':', or any other character (but not what
6121 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006122 * Don't do this when there an InsertCharPre autocommand is defined,
6123 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 */
6125#ifdef USE_ON_FLY_SCROLL
6126 dont_scroll = FALSE; /* allow scrolling here */
6127#endif
6128
6129 if ( !ISSPECIAL(c)
6130#ifdef FEAT_MBYTE
6131 && (!has_mbyte || (*mb_char2len)(c) == 1)
6132#endif
6133 && vpeekc() != NUL
6134 && !(State & REPLACE_FLAG)
6135#ifdef FEAT_CINDENT
6136 && !cindent_on()
6137#endif
6138#ifdef FEAT_RIGHTLEFT
6139 && !p_ri
6140#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006141#ifdef FEAT_AUTOCMD
6142 && !has_insertcharpre()
6143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 )
6145 {
6146#define INPUT_BUFLEN 100
6147 char_u buf[INPUT_BUFLEN + 1];
6148 int i;
6149 colnr_T virtcol = 0;
6150
6151 buf[0] = c;
6152 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006153 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 virtcol = get_nolist_virtcol();
6155 /*
6156 * Stop the string when:
6157 * - no more chars available
6158 * - finding a special character (command key)
6159 * - buffer is full
6160 * - running into the 'textwidth' boundary
6161 * - need to check for abbreviation: A non-word char after a word-char
6162 */
6163 while ( (c = vpeekc()) != NUL
6164 && !ISSPECIAL(c)
6165#ifdef FEAT_MBYTE
6166 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6167#endif
6168 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006169# ifdef FEAT_FKMAP
6170 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6171# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 && (textwidth == 0
6173 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6174 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6175 {
6176#ifdef FEAT_RIGHTLEFT
6177 c = vgetc();
6178 if (p_hkmap && KeyTyped)
6179 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 buf[i++] = c;
6181#else
6182 buf[i++] = vgetc();
6183#endif
6184 }
6185
6186#ifdef FEAT_DIGRAPHS
6187 do_digraph(-1); /* clear digraphs */
6188 do_digraph(buf[i-1]); /* may be the start of a digraph */
6189#endif
6190 buf[i] = NUL;
6191 ins_str(buf);
6192 if (flags & INSCHAR_CTRLV)
6193 {
6194 redo_literal(*buf);
6195 i = 1;
6196 }
6197 else
6198 i = 0;
6199 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006200 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 }
6202 else
6203 {
6204#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006205 int cc;
6206
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6208 {
6209 char_u buf[MB_MAXBYTES + 1];
6210
6211 (*mb_char2bytes)(c, buf);
6212 buf[cc] = NUL;
6213 ins_char_bytes(buf, cc);
6214 AppendCharToRedobuff(c);
6215 }
6216 else
6217#endif
6218 {
6219 ins_char(c);
6220 if (flags & INSCHAR_CTRLV)
6221 redo_literal(c);
6222 else
6223 AppendCharToRedobuff(c);
6224 }
6225 }
6226}
6227
6228/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006229 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006230 *
6231 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6232 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006233 */
6234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006235internal_format(
6236 int textwidth,
6237 int second_indent,
6238 int flags,
6239 int format_only,
6240 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006241{
6242 int cc;
6243 int save_char = NUL;
6244 int haveto_redraw = FALSE;
6245 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6246#ifdef FEAT_MBYTE
6247 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6248#endif
6249 int fo_white_par = has_format_option(FO_WHITE_PAR);
6250 int first_line = TRUE;
6251#ifdef FEAT_COMMENTS
6252 colnr_T leader_len;
6253 int no_leader = FALSE;
6254 int do_comments = (flags & INSCHAR_DO_COM);
6255#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006256#ifdef FEAT_LINEBREAK
6257 int has_lbr = curwin->w_p_lbr;
6258
6259 /* make sure win_lbr_chartabsize() counts correctly */
6260 curwin->w_p_lbr = FALSE;
6261#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006262
6263 /*
6264 * When 'ai' is off we don't want a space under the cursor to be
6265 * deleted. Replace it with an 'x' temporarily.
6266 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006267 if (!curbuf->b_p_ai
6268#ifdef FEAT_VREPLACE
6269 && !(State & VREPLACE_FLAG)
6270#endif
6271 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006272 {
6273 cc = gchar_cursor();
6274 if (vim_iswhite(cc))
6275 {
6276 save_char = cc;
6277 pchar_cursor('x');
6278 }
6279 }
6280
6281 /*
6282 * Repeat breaking lines, until the current line is not too long.
6283 */
6284 while (!got_int)
6285 {
6286 int startcol; /* Cursor column at entry */
6287 int wantcol; /* column at textwidth border */
6288 int foundcol; /* column for start of spaces */
6289 int end_foundcol = 0; /* column for start of word */
6290 colnr_T len;
6291 colnr_T virtcol;
6292#ifdef FEAT_VREPLACE
6293 int orig_col = 0;
6294 char_u *saved_text = NULL;
6295#endif
6296 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006297 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006298
Bram Moolenaar97b98102009-11-17 16:41:01 +00006299 virtcol = get_nolist_virtcol()
6300 + char2cells(c != NUL ? c : gchar_cursor());
6301 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006302 break;
6303
6304#ifdef FEAT_COMMENTS
6305 if (no_leader)
6306 do_comments = FALSE;
6307 else if (!(flags & INSCHAR_FORMAT)
6308 && has_format_option(FO_WRAP_COMS))
6309 do_comments = TRUE;
6310
6311 /* Don't break until after the comment leader */
6312 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006313 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006314 else
6315 leader_len = 0;
6316
6317 /* If the line doesn't start with a comment leader, then don't
6318 * start one in a following broken line. Avoids that a %word
6319 * moved to the start of the next line causes all following lines
6320 * to start with %. */
6321 if (leader_len == 0)
6322 no_leader = TRUE;
6323#endif
6324 if (!(flags & INSCHAR_FORMAT)
6325#ifdef FEAT_COMMENTS
6326 && leader_len == 0
6327#endif
6328 && !has_format_option(FO_WRAP))
6329
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006330 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006331 if ((startcol = curwin->w_cursor.col) == 0)
6332 break;
6333
6334 /* find column of textwidth border */
6335 coladvance((colnr_T)textwidth);
6336 wantcol = curwin->w_cursor.col;
6337
Bram Moolenaar97b98102009-11-17 16:41:01 +00006338 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006339 foundcol = 0;
6340
6341 /*
6342 * Find position to break at.
6343 * Stop at first entered white when 'formatoptions' has 'v'
6344 */
6345 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006346 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006347 || curwin->w_cursor.lnum != Insstart.lnum
6348 || curwin->w_cursor.col >= Insstart.col)
6349 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006350 if (curwin->w_cursor.col == startcol && c != NUL)
6351 cc = c;
6352 else
6353 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006354 if (WHITECHAR(cc))
6355 {
6356 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006357 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006358
6359 /* find start of sequence of blanks */
6360 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6361 {
6362 dec_cursor();
6363 cc = gchar_cursor();
6364 }
6365 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6366 break; /* only spaces in front of text */
6367#ifdef FEAT_COMMENTS
6368 /* Don't break until after the comment leader */
6369 if (curwin->w_cursor.col < leader_len)
6370 break;
6371#endif
6372 if (has_format_option(FO_ONE_LETTER))
6373 {
6374 /* do not break after one-letter words */
6375 if (curwin->w_cursor.col == 0)
6376 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006377#ifdef FEAT_COMMENTS
6378 /* do not break "#a b" when 'tw' is 2 */
6379 if (curwin->w_cursor.col <= leader_len)
6380 break;
6381#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006382 col = curwin->w_cursor.col;
6383 dec_cursor();
6384 cc = gchar_cursor();
6385
6386 if (WHITECHAR(cc))
6387 continue; /* one-letter, continue */
6388 curwin->w_cursor.col = col;
6389 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006390
6391 inc_cursor();
6392
6393 end_foundcol = end_col + 1;
6394 foundcol = curwin->w_cursor.col;
6395 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006396 break;
6397 }
6398#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006399 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006400 {
6401 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006402 if (curwin->w_cursor.col != startcol)
6403 {
6404#ifdef FEAT_COMMENTS
6405 /* Don't break until after the comment leader */
6406 if (curwin->w_cursor.col < leader_len)
6407 break;
6408#endif
6409 col = curwin->w_cursor.col;
6410 inc_cursor();
6411 /* Don't change end_foundcol if already set. */
6412 if (foundcol != curwin->w_cursor.col)
6413 {
6414 foundcol = curwin->w_cursor.col;
6415 end_foundcol = foundcol;
6416 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6417 break;
6418 }
6419 curwin->w_cursor.col = col;
6420 }
6421
6422 if (curwin->w_cursor.col == 0)
6423 break;
6424
6425 col = curwin->w_cursor.col;
6426
6427 dec_cursor();
6428 cc = gchar_cursor();
6429
6430 if (WHITECHAR(cc))
6431 continue; /* break with space */
6432#ifdef FEAT_COMMENTS
6433 /* Don't break until after the comment leader */
6434 if (curwin->w_cursor.col < leader_len)
6435 break;
6436#endif
6437
6438 curwin->w_cursor.col = col;
6439
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006441 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006442 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6443 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006444 }
6445#endif
6446 if (curwin->w_cursor.col == 0)
6447 break;
6448 dec_cursor();
6449 }
6450
6451 if (foundcol == 0) /* no spaces, cannot break line */
6452 {
6453 curwin->w_cursor.col = startcol;
6454 break;
6455 }
6456
6457 /* Going to break the line, remove any "$" now. */
6458 undisplay_dollar();
6459
6460 /*
6461 * Offset between cursor position and line break is used by replace
6462 * stack functions. VREPLACE does not use this, and backspaces
6463 * over the text instead.
6464 */
6465#ifdef FEAT_VREPLACE
6466 if (State & VREPLACE_FLAG)
6467 orig_col = startcol; /* Will start backspacing from here */
6468 else
6469#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006470 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006471
6472 /*
6473 * adjust startcol for spaces that will be deleted and
6474 * characters that will remain on top line
6475 */
6476 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006477 while ((cc = gchar_cursor(), WHITECHAR(cc))
6478 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006479 inc_cursor();
6480 startcol -= curwin->w_cursor.col;
6481 if (startcol < 0)
6482 startcol = 0;
6483
6484#ifdef FEAT_VREPLACE
6485 if (State & VREPLACE_FLAG)
6486 {
6487 /*
6488 * In VREPLACE mode, we will backspace over the text to be
6489 * wrapped, so save a copy now to put on the next line.
6490 */
6491 saved_text = vim_strsave(ml_get_cursor());
6492 curwin->w_cursor.col = orig_col;
6493 if (saved_text == NULL)
6494 break; /* Can't do it, out of memory */
6495 saved_text[startcol] = NUL;
6496
6497 /* Backspace over characters that will move to the next line */
6498 if (!fo_white_par)
6499 backspace_until_column(foundcol);
6500 }
6501 else
6502#endif
6503 {
6504 /* put cursor after pos. to break line */
6505 if (!fo_white_par)
6506 curwin->w_cursor.col = foundcol;
6507 }
6508
6509 /*
6510 * Split the line just before the margin.
6511 * Only insert/delete lines, but don't really redraw the window.
6512 */
6513 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6514 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6515#ifdef FEAT_COMMENTS
6516 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006517 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006518#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006519 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6520 if (!(flags & INSCHAR_COM_LIST))
6521 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006522
6523 replace_offset = 0;
6524 if (first_line)
6525 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006526 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006527 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006528 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006529 * This section is for auto-wrap of numeric lists. When not
6530 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6531 * flag will be set and open_line() will handle it (as seen
6532 * above). The code here (and in get_number_indent()) will
6533 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006534 */
6535 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006536 second_indent =
6537 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006538 if (second_indent >= 0)
6539 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006540#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006541 if (State & VREPLACE_FLAG)
6542 change_indent(INDENT_SET, second_indent,
6543 FALSE, NUL, TRUE);
6544 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006545#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006546#ifdef FEAT_COMMENTS
6547 if (leader_len > 0 && second_indent - leader_len > 0)
6548 {
6549 int i;
6550 int padding = second_indent - leader_len;
6551
6552 /* We started at the first_line of a numbered list
6553 * that has a comment. the open_line() function has
6554 * inserted the proper comment leader and positioned
6555 * the cursor at the end of the split line. Now we
6556 * add the additional whitespace needed after the
6557 * comment leader for the numbered list. */
6558 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006559 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006560 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006561 }
6562 else
6563 {
6564#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006565 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006566#ifdef FEAT_COMMENTS
6567 }
6568#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006569 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006570 }
6571 first_line = FALSE;
6572 }
6573
6574#ifdef FEAT_VREPLACE
6575 if (State & VREPLACE_FLAG)
6576 {
6577 /*
6578 * In VREPLACE mode we have backspaced over the text to be
6579 * moved, now we re-insert it into the new line.
6580 */
6581 ins_bytes(saved_text);
6582 vim_free(saved_text);
6583 }
6584 else
6585#endif
6586 {
6587 /*
6588 * Check if cursor is not past the NUL off the line, cindent
6589 * may have added or removed indent.
6590 */
6591 curwin->w_cursor.col += startcol;
6592 len = (colnr_T)STRLEN(ml_get_curline());
6593 if (curwin->w_cursor.col > len)
6594 curwin->w_cursor.col = len;
6595 }
6596
6597 haveto_redraw = TRUE;
6598#ifdef FEAT_CINDENT
6599 can_cindent = TRUE;
6600#endif
6601 /* moved the cursor, don't autoindent or cindent now */
6602 did_ai = FALSE;
6603#ifdef FEAT_SMARTINDENT
6604 did_si = FALSE;
6605 can_si = FALSE;
6606 can_si_back = FALSE;
6607#endif
6608 line_breakcheck();
6609 }
6610
6611 if (save_char != NUL) /* put back space after cursor */
6612 pchar_cursor(save_char);
6613
Bram Moolenaar0026d472014-09-09 16:32:39 +02006614#ifdef FEAT_LINEBREAK
6615 curwin->w_p_lbr = has_lbr;
6616#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006617 if (!format_only && haveto_redraw)
6618 {
6619 update_topline();
6620 redraw_curbuf_later(VALID);
6621 }
6622}
6623
6624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 * Called after inserting or deleting text: When 'formatoptions' includes the
6626 * 'a' flag format from the current line until the end of the paragraph.
6627 * Keep the cursor at the same position relative to the text.
6628 * The caller must have saved the cursor line for undo, following ones will be
6629 * saved here.
6630 */
6631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632auto_format(
6633 int trailblank, /* when TRUE also format with trailing blank */
6634 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
6636 pos_T pos;
6637 colnr_T len;
6638 char_u *old;
6639 char_u *new, *pnew;
6640 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006641 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642
6643 if (!has_format_option(FO_AUTO))
6644 return;
6645
6646 pos = curwin->w_cursor;
6647 old = ml_get_curline();
6648
6649 /* may remove added space */
6650 check_auto_format(FALSE);
6651
6652 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6653 * user might insert normal text next. Also skip formatting when "1" is
6654 * in 'formatoptions' and there is a single character before the cursor.
6655 * Otherwise the line would be broken and when typing another non-white
6656 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006657 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 if (*old != NUL && !trailblank && wasatend)
6659 {
6660 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006661 cc = gchar_cursor();
6662 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6663 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006665 cc = gchar_cursor();
6666 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {
6668 curwin->w_cursor = pos;
6669 return;
6670 }
6671 curwin->w_cursor = pos;
6672 }
6673
6674#ifdef FEAT_COMMENTS
6675 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6676 * comments. */
6677 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006678 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 return;
6680#endif
6681
6682 /*
6683 * May start formatting in a previous line, so that after "x" a word is
6684 * moved to the previous line if it fits there now. Only when this is not
6685 * the start of a paragraph.
6686 */
6687 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6688 {
6689 --curwin->w_cursor.lnum;
6690 if (u_save_cursor() == FAIL)
6691 return;
6692 }
6693
6694 /*
6695 * Do the formatting and restore the cursor position. "saved_cursor" will
6696 * be adjusted for the text formatting.
6697 */
6698 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006699 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 curwin->w_cursor = saved_cursor;
6701 saved_cursor.lnum = 0;
6702
6703 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6704 {
6705 /* "cannot happen" */
6706 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6707 coladvance((colnr_T)MAXCOL);
6708 }
6709 else
6710 check_cursor_col();
6711
6712 /* Insert mode: If the cursor is now after the end of the line while it
6713 * previously wasn't, the line was broken. Because of the rule above we
6714 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6715 * formatted. */
6716 if (!wasatend && has_format_option(FO_WHITE_PAR))
6717 {
6718 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006719 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 if (curwin->w_cursor.col == len)
6721 {
6722 pnew = vim_strnsave(new, len + 2);
6723 pnew[len] = ' ';
6724 pnew[len + 1] = NUL;
6725 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6726 /* remove the space later */
6727 did_add_space = TRUE;
6728 }
6729 else
6730 /* may remove added space */
6731 check_auto_format(FALSE);
6732 }
6733
6734 check_cursor();
6735}
6736
6737/*
6738 * When an extra space was added to continue a paragraph for auto-formatting,
6739 * delete it now. The space must be under the cursor, just after the insert
6740 * position.
6741 */
6742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006743check_auto_format(
6744 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745{
6746 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006747 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
6749 if (did_add_space)
6750 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006751 cc = gchar_cursor();
6752 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 /* Somehow the space was removed already. */
6754 did_add_space = FALSE;
6755 else
6756 {
6757 if (!end_insert)
6758 {
6759 inc_cursor();
6760 c = gchar_cursor();
6761 dec_cursor();
6762 }
6763 if (c != NUL)
6764 {
6765 /* The space is no longer at the end of the line, delete it. */
6766 del_char(FALSE);
6767 did_add_space = FALSE;
6768 }
6769 }
6770 }
6771}
6772
6773/*
6774 * Find out textwidth to be used for formatting:
6775 * if 'textwidth' option is set, use it
6776 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6777 * if invalid value, use 0.
6778 * Set default to window width (maximum 79) for "gq" operator.
6779 */
6780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006781comp_textwidth(
6782 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
6784 int textwidth;
6785
6786 textwidth = curbuf->b_p_tw;
6787 if (textwidth == 0 && curbuf->b_p_wm)
6788 {
6789 /* The width is the window width minus 'wrapmargin' minus all the
6790 * things that add to the margin. */
6791 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6792#ifdef FEAT_CMDWIN
6793 if (cmdwin_type != 0)
6794 textwidth -= 1;
6795#endif
6796#ifdef FEAT_FOLDING
6797 textwidth -= curwin->w_p_fdc;
6798#endif
6799#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006800 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 textwidth -= 1;
6802#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006803 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 textwidth -= 8;
6805 }
6806 if (textwidth < 0)
6807 textwidth = 0;
6808 if (ff && textwidth == 0)
6809 {
6810 textwidth = W_WIDTH(curwin) - 1;
6811 if (textwidth > 79)
6812 textwidth = 79;
6813 }
6814 return textwidth;
6815}
6816
6817/*
6818 * Put a character in the redo buffer, for when just after a CTRL-V.
6819 */
6820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006821redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
6823 char_u buf[10];
6824
6825 /* Only digits need special treatment. Translate them into a string of
6826 * three digits. */
6827 if (VIM_ISDIGIT(c))
6828 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006829 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 AppendToRedobuff(buf);
6831 }
6832 else
6833 AppendCharToRedobuff(c);
6834}
6835
6836/*
6837 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006838 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 */
6840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006841start_arrow(
6842 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006844 start_arrow_common(end_insert_pos, TRUE);
6845}
6846
6847/*
6848 * Like start_arrow() but with end_change argument.
6849 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6850 */
6851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006852start_arrow_with_change(
6853 pos_T *end_insert_pos, /* can be NULL */
6854 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006855{
6856 start_arrow_common(end_insert_pos, end_change);
6857 if (!end_change)
6858 {
6859 AppendCharToRedobuff(Ctrl_G);
6860 AppendCharToRedobuff('U');
6861 }
6862}
6863
6864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006865start_arrow_common(
6866 pos_T *end_insert_pos, /* can be NULL */
6867 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006868{
6869 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 {
6871 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006872 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 arrow_used = TRUE; /* this means we stopped the current insert */
6874 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006875#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006876 check_spell_redraw();
6877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878}
6879
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006880#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006881/*
6882 * If we skipped highlighting word at cursor, do it now.
6883 * It may be skipped again, thus reset spell_redraw_lnum first.
6884 */
6885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006886check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006887{
6888 if (spell_redraw_lnum != 0)
6889 {
6890 linenr_T lnum = spell_redraw_lnum;
6891
6892 spell_redraw_lnum = 0;
6893 redrawWinline(lnum, FALSE);
6894 }
6895}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006896
6897/*
6898 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6899 * spelled word, if there is one.
6900 */
6901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006902spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006903{
6904 pos_T tpos = curwin->w_cursor;
6905
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006906 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006907 if (curwin->w_cursor.col != tpos.col)
6908 start_arrow(&tpos);
6909}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006910#endif
6911
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912/*
6913 * stop_arrow() is called before a change is made in insert mode.
6914 * If an arrow key has been used, start a new insertion.
6915 * Returns FAIL if undo is impossible, shouldn't insert then.
6916 */
6917 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006918stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919{
6920 if (arrow_used)
6921 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006922 Insstart = curwin->w_cursor; /* new insertion starts here */
6923 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6924 /* Don't update the original insert position when moved to the
6925 * right, except when nothing was inserted yet. */
6926 update_Insstart_orig = FALSE;
6927 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6928
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 if (u_save_cursor() == OK)
6930 {
6931 arrow_used = FALSE;
6932 ins_need_undo = FALSE;
6933 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006934
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 ai_col = 0;
6936#ifdef FEAT_VREPLACE
6937 if (State & VREPLACE_FLAG)
6938 {
6939 orig_line_count = curbuf->b_ml.ml_line_count;
6940 vr_lines_changed = 1;
6941 }
6942#endif
6943 ResetRedobuff();
6944 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006945 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 }
6947 else if (ins_need_undo)
6948 {
6949 if (u_save_cursor() == OK)
6950 ins_need_undo = FALSE;
6951 }
6952
6953#ifdef FEAT_FOLDING
6954 /* Always open fold at the cursor line when inserting something. */
6955 foldOpenCursor();
6956#endif
6957
6958 return (arrow_used || ins_need_undo ? FAIL : OK);
6959}
6960
6961/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006962 * Do a few things to stop inserting.
6963 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6964 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 */
6966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006967stop_insert(
6968 pos_T *end_insert_pos,
6969 int esc, /* called by ins_esc() */
6970 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006972 int cc;
6973 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
6975 stop_redo_ins();
6976 replace_flush(); /* abandon replace stack */
6977
6978 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006979 * Save the inserted text for later redo with ^@ and CTRL-A.
6980 * Don't do it when "restart_edit" was set and nothing was inserted,
6981 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006983 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006984 if (did_restart_edit == 0 || (ptr != NULL
6985 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006986 {
6987 vim_free(last_insert);
6988 last_insert = ptr;
6989 last_insert_skip = new_insert_skip;
6990 }
6991 else
6992 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006994 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {
6996 /* Auto-format now. It may seem strange to do this when stopping an
6997 * insertion (or moving the cursor), but it's required when appending
6998 * a line and having it end in a space. But only do it when something
6999 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007000 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007002 pos_T tpos = curwin->w_cursor;
7003
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 /* When the cursor is at the end of the line after a space the
7005 * formatting will move it to the following word. Avoid that by
7006 * moving the cursor onto the space. */
7007 cc = 'x';
7008 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7009 {
7010 dec_cursor();
7011 cc = gchar_cursor();
7012 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007013 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 }
7015
7016 auto_format(TRUE, FALSE);
7017
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007018 if (vim_iswhite(cc))
7019 {
7020 if (gchar_cursor() != NUL)
7021 inc_cursor();
7022#ifdef FEAT_VIRTUALEDIT
7023 /* If the cursor is still at the same character, also keep
7024 * the "coladd". */
7025 if (gchar_cursor() == NUL
7026 && curwin->w_cursor.lnum == tpos.lnum
7027 && curwin->w_cursor.col == tpos.col)
7028 curwin->w_cursor.coladd = tpos.coladd;
7029#endif
7030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 }
7032
7033 /* If a space was inserted for auto-formatting, remove it now. */
7034 check_auto_format(TRUE);
7035
7036 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007037 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007038 * Do this when ESC was used or moving the cursor up/down.
7039 * Check for the old position still being valid, just in case the text
7040 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007041 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007042 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7043 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007045 pos_T tpos = curwin->w_cursor;
7046
7047 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007048 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007049 for (;;)
7050 {
7051 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7052 --curwin->w_cursor.col;
7053 cc = gchar_cursor();
7054 if (!vim_iswhite(cc))
7055 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007056 if (del_char(TRUE) == FAIL)
7057 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007058 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007059 if (curwin->w_cursor.lnum != tpos.lnum)
7060 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007061 else
7062 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007063 /* reset tpos, could have been invalidated in the loop above */
7064 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007065 tpos.col++;
7066 if (cc != NUL && gchar_pos(&tpos) == NUL)
7067 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 /* <C-S-Right> may have started Visual mode, adjust the position for
7071 * deleted characters. */
7072 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7073 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007074 int len = (int)STRLEN(ml_get_curline());
7075
7076 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007078 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007079#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 }
7083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
7085 }
7086 did_ai = FALSE;
7087#ifdef FEAT_SMARTINDENT
7088 did_si = FALSE;
7089 can_si = FALSE;
7090 can_si_back = FALSE;
7091#endif
7092
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007093 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7094 * now in a different buffer. */
7095 if (end_insert_pos != NULL)
7096 {
7097 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007098 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007099 curbuf->b_op_end = *end_insert_pos;
7100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101}
7102
7103/*
7104 * Set the last inserted text to a single character.
7105 * Used for the replace command.
7106 */
7107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007108set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
7110 char_u *s;
7111
7112 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (last_insert != NULL)
7115 {
7116 s = last_insert;
7117 /* Use the CTRL-V only when entering a special char */
7118 if (c < ' ' || c == DEL)
7119 *s++ = Ctrl_V;
7120 s = add_char2buf(c, s);
7121 *s++ = ESC;
7122 *s++ = NUL;
7123 last_insert_skip = 0;
7124 }
7125}
7126
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007127#if defined(EXITFREE) || defined(PROTO)
7128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007129free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007130{
7131 vim_free(last_insert);
7132 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007133# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007134 vim_free(compl_orig_text);
7135 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007136# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007137}
7138#endif
7139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140/*
7141 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7142 * and CSI. Handle multi-byte characters.
7143 * Returns a pointer to after the added bytes.
7144 */
7145 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007146add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
7148#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007149 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 int i;
7151 int len;
7152
7153 len = (*mb_char2bytes)(c, temp);
7154 for (i = 0; i < len; ++i)
7155 {
7156 c = temp[i];
7157#endif
7158 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7159 if (c == K_SPECIAL)
7160 {
7161 *s++ = K_SPECIAL;
7162 *s++ = KS_SPECIAL;
7163 *s++ = KE_FILLER;
7164 }
7165#ifdef FEAT_GUI
7166 else if (c == CSI)
7167 {
7168 *s++ = CSI;
7169 *s++ = KS_EXTRA;
7170 *s++ = (int)KE_CSI;
7171 }
7172#endif
7173 else
7174 *s++ = c;
7175#ifdef FEAT_MBYTE
7176 }
7177#endif
7178 return s;
7179}
7180
7181/*
7182 * move cursor to start of line
7183 * if flags & BL_WHITE move to first non-white
7184 * if flags & BL_SOL move to first non-white if startofline is set,
7185 * otherwise keep "curswant" column
7186 * if flags & BL_FIX don't leave the cursor on a NUL.
7187 */
7188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007189beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190{
7191 if ((flags & BL_SOL) && !p_sol)
7192 coladvance(curwin->w_curswant);
7193 else
7194 {
7195 curwin->w_cursor.col = 0;
7196#ifdef FEAT_VIRTUALEDIT
7197 curwin->w_cursor.coladd = 0;
7198#endif
7199
7200 if (flags & (BL_WHITE | BL_SOL))
7201 {
7202 char_u *ptr;
7203
7204 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7205 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7206 ++curwin->w_cursor.col;
7207 }
7208 curwin->w_set_curswant = TRUE;
7209 }
7210}
7211
7212/*
7213 * oneright oneleft cursor_down cursor_up
7214 *
7215 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007216 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 * Return OK when successful, FAIL when we hit a line of file boundary.
7218 */
7219
7220 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007221oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
7226#ifdef FEAT_VIRTUALEDIT
7227 if (virtual_active())
7228 {
7229 pos_T prevpos = curwin->w_cursor;
7230
7231 /* Adjust for multi-wide char (excluding TAB) */
7232 ptr = ml_get_cursor();
7233 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007234# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007236# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 ))
7240 ? ptr2cells(ptr) : 1));
7241 curwin->w_set_curswant = TRUE;
7242 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7243 return (prevpos.col != curwin->w_cursor.col
7244 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7245 }
7246#endif
7247
7248 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007249 if (*ptr == NUL)
7250 return FAIL; /* already at the very end */
7251
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007253 if (has_mbyte)
7254 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 else
7256#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007257 l = 1;
7258
7259 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7260 * contains "onemore". */
7261 if (ptr[l] == NUL
7262#ifdef FEAT_VIRTUALEDIT
7263 && (ve_flags & VE_ONEMORE) == 0
7264#endif
7265 )
7266 return FAIL;
7267 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
7269 curwin->w_set_curswant = TRUE;
7270 return OK;
7271}
7272
7273 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007274oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
7276#ifdef FEAT_VIRTUALEDIT
7277 if (virtual_active())
7278 {
7279 int width;
7280 int v = getviscol();
7281
7282 if (v == 0)
7283 return FAIL;
7284
7285# ifdef FEAT_LINEBREAK
7286 /* We might get stuck on 'showbreak', skip over it. */
7287 width = 1;
7288 for (;;)
7289 {
7290 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007291 /* getviscol() is slow, skip it when 'showbreak' is empty,
7292 * 'breakindent' is not set and there are no multi-byte
7293 * characters */
7294 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295# ifdef FEAT_MBYTE
7296 && !has_mbyte
7297# endif
7298 ) || getviscol() < v)
7299 break;
7300 ++width;
7301 }
7302# else
7303 coladvance(v - 1);
7304# endif
7305
7306 if (curwin->w_cursor.coladd == 1)
7307 {
7308 char_u *ptr;
7309
7310 /* Adjust for multi-wide char (not a TAB) */
7311 ptr = ml_get_cursor();
7312 if (*ptr != TAB && vim_isprintc(
7313# ifdef FEAT_MBYTE
7314 (*mb_ptr2char)(ptr)
7315# else
7316 *ptr
7317# endif
7318 ) && ptr2cells(ptr) > 1)
7319 curwin->w_cursor.coladd = 0;
7320 }
7321
7322 curwin->w_set_curswant = TRUE;
7323 return OK;
7324 }
7325#endif
7326
7327 if (curwin->w_cursor.col == 0)
7328 return FAIL;
7329
7330 curwin->w_set_curswant = TRUE;
7331 --curwin->w_cursor.col;
7332
7333#ifdef FEAT_MBYTE
7334 /* if the character on the left of the current cursor is a multi-byte
7335 * character, move to its first byte */
7336 if (has_mbyte)
7337 mb_adjust_cursor();
7338#endif
7339 return OK;
7340}
7341
7342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007343cursor_up(
7344 long n,
7345 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346{
7347 linenr_T lnum;
7348
7349 if (n > 0)
7350 {
7351 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007352 /* This fails if the cursor is already in the first line or the count
7353 * is larger than the line number and '-' is in 'cpoptions' */
7354 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 return FAIL;
7356 if (n >= lnum)
7357 lnum = 1;
7358 else
7359#ifdef FEAT_FOLDING
7360 if (hasAnyFolding(curwin))
7361 {
7362 /*
7363 * Count each sequence of folded lines as one logical line.
7364 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007365 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 (void)hasFolding(lnum, &lnum, NULL);
7367
7368 while (n--)
7369 {
7370 /* move up one line */
7371 --lnum;
7372 if (lnum <= 1)
7373 break;
7374 /* If we entered a fold, move to the beginning, unless in
7375 * Insert mode or when 'foldopen' contains "all": it will open
7376 * in a moment. */
7377 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7378 (void)hasFolding(lnum, &lnum, NULL);
7379 }
7380 if (lnum < 1)
7381 lnum = 1;
7382 }
7383 else
7384#endif
7385 lnum -= n;
7386 curwin->w_cursor.lnum = lnum;
7387 }
7388
7389 /* try to advance to the column we want to be at */
7390 coladvance(curwin->w_curswant);
7391
7392 if (upd_topline)
7393 update_topline(); /* make sure curwin->w_topline is valid */
7394
7395 return OK;
7396}
7397
7398/*
7399 * Cursor down a number of logical lines.
7400 */
7401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007402cursor_down(
7403 long n,
7404 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405{
7406 linenr_T lnum;
7407
7408 if (n > 0)
7409 {
7410 lnum = curwin->w_cursor.lnum;
7411#ifdef FEAT_FOLDING
7412 /* Move to last line of fold, will fail if it's the end-of-file. */
7413 (void)hasFolding(lnum, NULL, &lnum);
7414#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007415 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007416 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007417 if (lnum >= curbuf->b_ml.ml_line_count
7418 || (lnum + n > curbuf->b_ml.ml_line_count
7419 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 return FAIL;
7421 if (lnum + n >= curbuf->b_ml.ml_line_count)
7422 lnum = curbuf->b_ml.ml_line_count;
7423 else
7424#ifdef FEAT_FOLDING
7425 if (hasAnyFolding(curwin))
7426 {
7427 linenr_T last;
7428
7429 /* count each sequence of folded lines as one logical line */
7430 while (n--)
7431 {
7432 if (hasFolding(lnum, NULL, &last))
7433 lnum = last + 1;
7434 else
7435 ++lnum;
7436 if (lnum >= curbuf->b_ml.ml_line_count)
7437 break;
7438 }
7439 if (lnum > curbuf->b_ml.ml_line_count)
7440 lnum = curbuf->b_ml.ml_line_count;
7441 }
7442 else
7443#endif
7444 lnum += n;
7445 curwin->w_cursor.lnum = lnum;
7446 }
7447
7448 /* try to advance to the column we want to be at */
7449 coladvance(curwin->w_curswant);
7450
7451 if (upd_topline)
7452 update_topline(); /* make sure curwin->w_topline is valid */
7453
7454 return OK;
7455}
7456
7457/*
7458 * Stuff the last inserted text in the read buffer.
7459 * Last_insert actually is a copy of the redo buffer, so we
7460 * first have to remove the command.
7461 */
7462 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007463stuff_inserted(
7464 int c, /* Command character to be inserted */
7465 long count, /* Repeat this many times */
7466 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467{
7468 char_u *esc_ptr;
7469 char_u *ptr;
7470 char_u *last_ptr;
7471 char_u last = NUL;
7472
7473 ptr = get_last_insert();
7474 if (ptr == NULL)
7475 {
7476 EMSG(_(e_noinstext));
7477 return FAIL;
7478 }
7479
7480 /* may want to stuff the command character, to start Insert mode */
7481 if (c != NUL)
7482 stuffcharReadbuff(c);
7483 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7484 *esc_ptr = NUL; /* remove the ESC */
7485
7486 /* when the last char is either "0" or "^" it will be quoted if no ESC
7487 * comes after it OR if it will inserted more than once and "ptr"
7488 * starts with ^D. -- Acevedo
7489 */
7490 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7491 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7492 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7493 {
7494 last = *last_ptr;
7495 *last_ptr = NUL;
7496 }
7497
7498 do
7499 {
7500 stuffReadbuff(ptr);
7501 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7502 if (last)
7503 stuffReadbuff((char_u *)(last == '0'
7504 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7505 : IF_EB("\026^", CTRL_V_STR "^")));
7506 }
7507 while (--count > 0);
7508
7509 if (last)
7510 *last_ptr = last;
7511
7512 if (esc_ptr != NULL)
7513 *esc_ptr = ESC; /* put the ESC back */
7514
7515 /* may want to stuff a trailing ESC, to get out of Insert mode */
7516 if (!no_esc)
7517 stuffcharReadbuff(ESC);
7518
7519 return OK;
7520}
7521
7522 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007523get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524{
7525 if (last_insert == NULL)
7526 return NULL;
7527 return last_insert + last_insert_skip;
7528}
7529
7530/*
7531 * Get last inserted string, and remove trailing <Esc>.
7532 * Returns pointer to allocated memory (must be freed) or NULL.
7533 */
7534 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007535get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536{
7537 char_u *s;
7538 int len;
7539
7540 if (last_insert == NULL)
7541 return NULL;
7542 s = vim_strsave(last_insert + last_insert_skip);
7543 if (s != NULL)
7544 {
7545 len = (int)STRLEN(s);
7546 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7547 s[len - 1] = NUL;
7548 }
7549 return s;
7550}
7551
7552/*
7553 * Check the word in front of the cursor for an abbreviation.
7554 * Called when the non-id character "c" has been entered.
7555 * When an abbreviation is recognized it is removed from the text and
7556 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7557 */
7558 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007559echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560{
7561 /* Don't check for abbreviation in paste mode, when disabled and just
7562 * after moving around with cursor keys. */
7563 if (p_paste || no_abbr || arrow_used)
7564 return FALSE;
7565
7566 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7567 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7568}
7569
7570/*
7571 * replace-stack functions
7572 *
7573 * When replacing characters, the replaced characters are remembered for each
7574 * new character. This is used to re-insert the old text when backspacing.
7575 *
7576 * There is a NUL headed list of characters for each character that is
7577 * currently in the file after the insertion point. When BS is used, one NUL
7578 * headed list is put back for the deleted character.
7579 *
7580 * For a newline, there are two NUL headed lists. One contains the characters
7581 * that the NL replaced. The extra one stores the characters after the cursor
7582 * that were deleted (always white space).
7583 *
7584 * Replace_offset is normally 0, in which case replace_push will add a new
7585 * character at the end of the stack. If replace_offset is not 0, that many
7586 * characters will be left on the stack above the newly inserted character.
7587 */
7588
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007589static char_u *replace_stack = NULL;
7590static long replace_stack_nr = 0; /* next entry in replace stack */
7591static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592
7593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007594replace_push(
7595 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 char_u *p;
7598
7599 if (replace_stack_nr < replace_offset) /* nothing to do */
7600 return;
7601 if (replace_stack_len <= replace_stack_nr)
7602 {
7603 replace_stack_len += 50;
7604 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7605 if (p == NULL) /* out of memory */
7606 {
7607 replace_stack_len -= 50;
7608 return;
7609 }
7610 if (replace_stack != NULL)
7611 {
7612 mch_memmove(p, replace_stack,
7613 (size_t)(replace_stack_nr * sizeof(char_u)));
7614 vim_free(replace_stack);
7615 }
7616 replace_stack = p;
7617 }
7618 p = replace_stack + replace_stack_nr - replace_offset;
7619 if (replace_offset)
7620 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7621 *p = c;
7622 ++replace_stack_nr;
7623}
7624
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007625#if defined(FEAT_MBYTE) || defined(PROTO)
7626/*
7627 * Push a character onto the replace stack. Handles a multi-byte character in
7628 * reverse byte order, so that the first byte is popped off first.
7629 * Return the number of bytes done (includes composing characters).
7630 */
7631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007632replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007633{
7634 int l = (*mb_ptr2len)(p);
7635 int j;
7636
7637 for (j = l - 1; j >= 0; --j)
7638 replace_push(p[j]);
7639 return l;
7640}
7641#endif
7642
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643/*
7644 * Pop one item from the replace stack.
7645 * return -1 if stack empty
7646 * return replaced character or NUL otherwise
7647 */
7648 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007649replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650{
7651 if (replace_stack_nr == 0)
7652 return -1;
7653 return (int)replace_stack[--replace_stack_nr];
7654}
7655
7656/*
7657 * Join the top two items on the replace stack. This removes to "off"'th NUL
7658 * encountered.
7659 */
7660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007661replace_join(
7662 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
7664 int i;
7665
7666 for (i = replace_stack_nr; --i >= 0; )
7667 if (replace_stack[i] == NUL && off-- <= 0)
7668 {
7669 --replace_stack_nr;
7670 mch_memmove(replace_stack + i, replace_stack + i + 1,
7671 (size_t)(replace_stack_nr - i));
7672 return;
7673 }
7674}
7675
7676/*
7677 * Pop bytes from the replace stack until a NUL is found, and insert them
7678 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7679 */
7680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007681replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
7683 int cc;
7684 int oldState = State;
7685
7686 State = NORMAL; /* don't want REPLACE here */
7687 while ((cc = replace_pop()) > 0)
7688 {
7689#ifdef FEAT_MBYTE
7690 mb_replace_pop_ins(cc);
7691#else
7692 ins_char(cc);
7693#endif
7694 dec_cursor();
7695 }
7696 State = oldState;
7697}
7698
7699#ifdef FEAT_MBYTE
7700/*
7701 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7702 * indicates a multi-byte char, pop the other bytes too.
7703 */
7704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007705mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
7707 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007708 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 int i;
7710 int c;
7711
7712 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7713 {
7714 buf[0] = cc;
7715 for (i = 1; i < n; ++i)
7716 buf[i] = replace_pop();
7717 ins_bytes_len(buf, n);
7718 }
7719 else
7720 ins_char(cc);
7721
7722 if (enc_utf8)
7723 /* Handle composing chars. */
7724 for (;;)
7725 {
7726 c = replace_pop();
7727 if (c == -1) /* stack empty */
7728 break;
7729 if ((n = MB_BYTE2LEN(c)) == 1)
7730 {
7731 /* Not a multi-byte char, put it back. */
7732 replace_push(c);
7733 break;
7734 }
7735 else
7736 {
7737 buf[0] = c;
7738 for (i = 1; i < n; ++i)
7739 buf[i] = replace_pop();
7740 if (utf_iscomposing(utf_ptr2char(buf)))
7741 ins_bytes_len(buf, n);
7742 else
7743 {
7744 /* Not a composing char, put it back. */
7745 for (i = n - 1; i >= 0; --i)
7746 replace_push(buf[i]);
7747 break;
7748 }
7749 }
7750 }
7751}
7752#endif
7753
7754/*
7755 * make the replace stack empty
7756 * (called when exiting replace mode)
7757 */
7758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007759replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
7761 vim_free(replace_stack);
7762 replace_stack = NULL;
7763 replace_stack_len = 0;
7764 replace_stack_nr = 0;
7765}
7766
7767/*
7768 * Handle doing a BS for one character.
7769 * cc < 0: replace stack empty, just move cursor
7770 * cc == 0: character was inserted, delete it
7771 * cc > 0: character was replaced, put cc (first byte of original char) back
7772 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007773 * When "limit_col" is >= 0, don't delete before this column. Matters when
7774 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 */
7776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007777replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 int cc;
7780#ifdef FEAT_VREPLACE
7781 int orig_len = 0;
7782 int ins_len;
7783 int orig_vcols = 0;
7784 colnr_T start_vcol;
7785 char_u *p;
7786 int i;
7787 int vcol;
7788#endif
7789
7790 cc = replace_pop();
7791 if (cc > 0)
7792 {
7793#ifdef FEAT_VREPLACE
7794 if (State & VREPLACE_FLAG)
7795 {
7796 /* Get the number of screen cells used by the character we are
7797 * going to delete. */
7798 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7799 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7800 }
7801#endif
7802#ifdef FEAT_MBYTE
7803 if (has_mbyte)
7804 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007805 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806# ifdef FEAT_VREPLACE
7807 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007808 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809# endif
7810 replace_push(cc);
7811 }
7812 else
7813#endif
7814 {
7815 pchar_cursor(cc);
7816#ifdef FEAT_VREPLACE
7817 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007818 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819#endif
7820 }
7821 replace_pop_ins();
7822
7823#ifdef FEAT_VREPLACE
7824 if (State & VREPLACE_FLAG)
7825 {
7826 /* Get the number of screen cells used by the inserted characters */
7827 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007828 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 vcol = start_vcol;
7830 for (i = 0; i < ins_len; ++i)
7831 {
7832 vcol += chartabsize(p + i, vcol);
7833#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007834 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835#endif
7836 }
7837 vcol -= start_vcol;
7838
7839 /* Delete spaces that were inserted after the cursor to keep the
7840 * text aligned. */
7841 curwin->w_cursor.col += ins_len;
7842 while (vcol > orig_vcols && gchar_cursor() == ' ')
7843 {
7844 del_char(FALSE);
7845 ++orig_vcols;
7846 }
7847 curwin->w_cursor.col -= ins_len;
7848 }
7849#endif
7850
7851 /* mark the buffer as changed and prepare for displaying */
7852 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7853 }
7854 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007855 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856}
7857
7858#ifdef FEAT_CINDENT
7859/*
7860 * Return TRUE if C-indenting is on.
7861 */
7862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007863cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864{
7865 return (!p_paste && (curbuf->b_p_cin
7866# ifdef FEAT_EVAL
7867 || *curbuf->b_p_inde != NUL
7868# endif
7869 ));
7870}
7871#endif
7872
7873#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7874/*
7875 * Re-indent the current line, based on the current contents of it and the
7876 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7877 * confused what all the part that handles Control-T is doing that I'm not.
7878 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7879 */
7880
7881 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007882fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007884 int amount = get_the_indent();
7885
7886 if (amount >= 0)
7887 {
7888 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7889 if (linewhite(curwin->w_cursor.lnum))
7890 did_ai = TRUE; /* delete the indent if the line stays empty */
7891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892}
7893
7894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007895fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
7897 if (p_paste)
7898 return;
7899# ifdef FEAT_LISP
7900 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7901 fixthisline(get_lisp_indent);
7902# endif
7903# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7904 else
7905# endif
7906# ifdef FEAT_CINDENT
7907 if (cindent_on())
7908 do_c_expr_indent();
7909# endif
7910}
7911
7912#endif
7913
7914#ifdef FEAT_CINDENT
7915/*
7916 * return TRUE if 'cinkeys' contains the key "keytyped",
7917 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007918 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7920 *
7921 * "keytyped" can have a few special values:
7922 * KEY_OPEN_FORW
7923 * KEY_OPEN_BACK
7924 * KEY_COMPLETE just finished completion.
7925 *
7926 * If line_is_empty is TRUE accept keys with '0' before them.
7927 */
7928 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007929in_cinkeys(
7930 int keytyped,
7931 int when,
7932 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934 char_u *look;
7935 int try_match;
7936 int try_match_word;
7937 char_u *p;
7938 char_u *line;
7939 int icase;
7940 int i;
7941
Bram Moolenaar30848942009-12-24 14:46:12 +00007942 if (keytyped == NUL)
7943 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7944 return FALSE;
7945
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946#ifdef FEAT_EVAL
7947 if (*curbuf->b_p_inde != NUL)
7948 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7949 else
7950#endif
7951 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7952 while (*look)
7953 {
7954 /*
7955 * Find out if we want to try a match with this key, depending on
7956 * 'when' and a '*' or '!' before the key.
7957 */
7958 switch (when)
7959 {
7960 case '*': try_match = (*look == '*'); break;
7961 case '!': try_match = (*look == '!'); break;
7962 default: try_match = (*look != '*'); break;
7963 }
7964 if (*look == '*' || *look == '!')
7965 ++look;
7966
7967 /*
7968 * If there is a '0', only accept a match if the line is empty.
7969 * But may still match when typing last char of a word.
7970 */
7971 if (*look == '0')
7972 {
7973 try_match_word = try_match;
7974 if (!line_is_empty)
7975 try_match = FALSE;
7976 ++look;
7977 }
7978 else
7979 try_match_word = FALSE;
7980
7981 /*
7982 * does it look like a control character?
7983 */
7984 if (*look == '^'
7985#ifdef EBCDIC
7986 && (Ctrl_chr(look[1]) != 0)
7987#else
7988 && look[1] >= '?' && look[1] <= '_'
7989#endif
7990 )
7991 {
7992 if (try_match && keytyped == Ctrl_chr(look[1]))
7993 return TRUE;
7994 look += 2;
7995 }
7996 /*
7997 * 'o' means "o" command, open forward.
7998 * 'O' means "O" command, open backward.
7999 */
8000 else if (*look == 'o')
8001 {
8002 if (try_match && keytyped == KEY_OPEN_FORW)
8003 return TRUE;
8004 ++look;
8005 }
8006 else if (*look == 'O')
8007 {
8008 if (try_match && keytyped == KEY_OPEN_BACK)
8009 return TRUE;
8010 ++look;
8011 }
8012
8013 /*
8014 * 'e' means to check for "else" at start of line and just before the
8015 * cursor.
8016 */
8017 else if (*look == 'e')
8018 {
8019 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8020 {
8021 p = ml_get_curline();
8022 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8023 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8024 return TRUE;
8025 }
8026 ++look;
8027 }
8028
8029 /*
8030 * ':' only causes an indent if it is at the end of a label or case
8031 * statement, or when it was before typing the ':' (to fix
8032 * class::method for C++).
8033 */
8034 else if (*look == ':')
8035 {
8036 if (try_match && keytyped == ':')
8037 {
8038 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008039 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008041 /* Need to get the line again after cin_islabel(). */
8042 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 if (curwin->w_cursor.col > 2
8044 && p[curwin->w_cursor.col - 1] == ':'
8045 && p[curwin->w_cursor.col - 2] == ':')
8046 {
8047 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008048 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008049 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 p = ml_get_curline();
8051 p[curwin->w_cursor.col - 1] = ':';
8052 if (i)
8053 return TRUE;
8054 }
8055 }
8056 ++look;
8057 }
8058
8059
8060 /*
8061 * Is it a key in <>, maybe?
8062 */
8063 else if (*look == '<')
8064 {
8065 if (try_match)
8066 {
8067 /*
8068 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8069 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8070 * >, *, : and ! keys if they really really want to.
8071 */
8072 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8073 && keytyped == look[1])
8074 return TRUE;
8075
8076 if (keytyped == get_special_key_code(look + 1))
8077 return TRUE;
8078 }
8079 while (*look && *look != '>')
8080 look++;
8081 while (*look == '>')
8082 look++;
8083 }
8084
8085 /*
8086 * Is it a word: "=word"?
8087 */
8088 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8089 {
8090 ++look;
8091 if (*look == '~')
8092 {
8093 icase = TRUE;
8094 ++look;
8095 }
8096 else
8097 icase = FALSE;
8098 p = vim_strchr(look, ',');
8099 if (p == NULL)
8100 p = look + STRLEN(look);
8101 if ((try_match || try_match_word)
8102 && curwin->w_cursor.col >= (colnr_T)(p - look))
8103 {
8104 int match = FALSE;
8105
8106#ifdef FEAT_INS_EXPAND
8107 if (keytyped == KEY_COMPLETE)
8108 {
8109 char_u *s;
8110
8111 /* Just completed a word, check if it starts with "look".
8112 * search back for the start of a word. */
8113 line = ml_get_curline();
8114# ifdef FEAT_MBYTE
8115 if (has_mbyte)
8116 {
8117 char_u *n;
8118
8119 for (s = line + curwin->w_cursor.col; s > line; s = n)
8120 {
8121 n = mb_prevptr(line, s);
8122 if (!vim_iswordp(n))
8123 break;
8124 }
8125 }
8126 else
8127# endif
8128 for (s = line + curwin->w_cursor.col; s > line; --s)
8129 if (!vim_iswordc(s[-1]))
8130 break;
8131 if (s + (p - look) <= line + curwin->w_cursor.col
8132 && (icase
8133 ? MB_STRNICMP(s, look, p - look)
8134 : STRNCMP(s, look, p - look)) == 0)
8135 match = TRUE;
8136 }
8137 else
8138#endif
8139 /* TODO: multi-byte */
8140 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8141 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8142 {
8143 line = ml_get_cursor();
8144 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8145 || !vim_iswordc(line[-(p - look) - 1]))
8146 && (icase
8147 ? MB_STRNICMP(line - (p - look), look, p - look)
8148 : STRNCMP(line - (p - look), look, p - look))
8149 == 0)
8150 match = TRUE;
8151 }
8152 if (match && try_match_word && !try_match)
8153 {
8154 /* "0=word": Check if there are only blanks before the
8155 * word. */
8156 line = ml_get_curline();
8157 if ((int)(skipwhite(line) - line) !=
8158 (int)(curwin->w_cursor.col - (p - look)))
8159 match = FALSE;
8160 }
8161 if (match)
8162 return TRUE;
8163 }
8164 look = p;
8165 }
8166
8167 /*
8168 * ok, it's a boring generic character.
8169 */
8170 else
8171 {
8172 if (try_match && *look == keytyped)
8173 return TRUE;
8174 ++look;
8175 }
8176
8177 /*
8178 * Skip over ", ".
8179 */
8180 look = skip_to_option_part(look);
8181 }
8182 return FALSE;
8183}
8184#endif /* FEAT_CINDENT */
8185
8186#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8187/*
8188 * Map Hebrew keyboard when in hkmap mode.
8189 */
8190 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008191hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
8193 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8194 {
8195 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8196 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8197 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8198 static char_u map[26] =
8199 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8200 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8201 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8202 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8203 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8204 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8205 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8206 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8207 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8208
8209 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8210 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8211 /* '-1'='sofit' */
8212 else if (c == 'x')
8213 return 'X';
8214 else if (c == 'q')
8215 return '\''; /* {geresh}={'} */
8216 else if (c == 246)
8217 return ' '; /* \"o --> ' ' for a german keyboard */
8218 else if (c == 228)
8219 return ' '; /* \"a --> ' ' -- / -- */
8220 else if (c == 252)
8221 return ' '; /* \"u --> ' ' -- / -- */
8222#ifdef EBCDIC
8223 else if (islower(c))
8224#else
8225 /* NOTE: islower() does not do the right thing for us on Linux so we
8226 * do this the same was as 5.7 and previous, so it works correctly on
8227 * all systems. Specifically, the e.g. Delete and Arrow keys are
8228 * munged and won't work if e.g. searching for Hebrew text.
8229 */
8230 else if (c >= 'a' && c <= 'z')
8231#endif
8232 return (int)(map[CharOrdLow(c)] + p_aleph);
8233 else
8234 return c;
8235 }
8236 else
8237 {
8238 switch (c)
8239 {
8240 case '`': return ';';
8241 case '/': return '.';
8242 case '\'': return ',';
8243 case 'q': return '/';
8244 case 'w': return '\'';
8245
8246 /* Hebrew letters - set offset from 'a' */
8247 case ',': c = '{'; break;
8248 case '.': c = 'v'; break;
8249 case ';': c = 't'; break;
8250 default: {
8251 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8252
8253#ifdef EBCDIC
8254 /* see note about islower() above */
8255 if (!islower(c))
8256#else
8257 if (c < 'a' || c > 'z')
8258#endif
8259 return c;
8260 c = str[CharOrdLow(c)];
8261 break;
8262 }
8263 }
8264
8265 return (int)(CharOrdLow(c) + p_aleph);
8266 }
8267}
8268#endif
8269
8270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008271ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272{
8273 int need_redraw = FALSE;
8274 int regname;
8275 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008276 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277
8278 /*
8279 * If we are going to wait for a character, show a '"'.
8280 */
8281 pc_status = PC_STATUS_UNSET;
8282 if (redrawing() && !char_avail())
8283 {
8284 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008285 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286
8287 edit_putchar('"', TRUE);
8288#ifdef FEAT_CMDL_INFO
8289 add_to_showcmd_c(Ctrl_R);
8290#endif
8291 }
8292
8293#ifdef USE_ON_FLY_SCROLL
8294 dont_scroll = TRUE; /* disallow scrolling here */
8295#endif
8296
8297 /*
8298 * Don't map the register name. This also prevents the mode message to be
8299 * deleted when ESC is hit.
8300 */
8301 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008302 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8305 {
8306 /* Get a third key for literal register insertion */
8307 literally = regname;
8308#ifdef FEAT_CMDL_INFO
8309 add_to_showcmd_c(literally);
8310#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008311 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 }
8314 --no_mapping;
8315
8316#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008317 /* Don't call u_sync() while typing the expression or giving an error
8318 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 ++no_u_sync;
8320 if (regname == '=')
8321 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008322# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008324# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008325 /* Sync undo when evaluating the expression calls setline() or
8326 * append(), so that it can be undone separately. */
8327 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008328
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008330# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 /* Restore the Input Method. */
8332 if (im_on)
8333 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008334# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008336 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8337 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008338 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 else
8342 {
8343#endif
8344 if (literally == Ctrl_O || literally == Ctrl_P)
8345 {
8346 /* Append the command to the redo buffer. */
8347 AppendCharToRedobuff(Ctrl_R);
8348 AppendCharToRedobuff(literally);
8349 AppendCharToRedobuff(regname);
8350
8351 do_put(regname, BACKWARD, 1L,
8352 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8353 }
8354 else if (insert_reg(regname, literally) == FAIL)
8355 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008356 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 need_redraw = TRUE; /* remove the '"' */
8358 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008359 else if (stop_insert_mode)
8360 /* When the '=' register was used and a function was invoked that
8361 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8362 * insert anything, need to remove the '"' */
8363 need_redraw = TRUE;
8364
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365#ifdef FEAT_EVAL
8366 }
8367 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008368 if (u_sync_once == 1)
8369 ins_need_undo = TRUE;
8370 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371#endif
8372#ifdef FEAT_CMDL_INFO
8373 clear_showcmd();
8374#endif
8375
8376 /* If the inserted register is empty, we need to remove the '"' */
8377 if (need_redraw || stuff_empty())
8378 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008379
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008380 /* Disallow starting Visual mode here, would get a weird mode. */
8381 if (!vis_active && VIsual_active)
8382 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383}
8384
8385/*
8386 * CTRL-G commands in Insert mode.
8387 */
8388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008389ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390{
8391 int c;
8392
8393#ifdef FEAT_INS_EXPAND
8394 /* Right after CTRL-X the cursor will be after the ruler. */
8395 setcursor();
8396#endif
8397
8398 /*
8399 * Don't map the second key. This also prevents the mode message to be
8400 * deleted when ESC is hit.
8401 */
8402 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008403 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 --no_mapping;
8405 switch (c)
8406 {
8407 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8408 case K_UP:
8409 case Ctrl_K:
8410 case 'k': ins_up(TRUE);
8411 break;
8412
8413 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8414 case K_DOWN:
8415 case Ctrl_J:
8416 case 'j': ins_down(TRUE);
8417 break;
8418
8419 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008420 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008422
8423 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008424 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008425 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008426 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 break;
8428
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008429 /* CTRL-G U: do not break undo with the next char */
8430 case 'U':
8431 /* Allow one left/right cursor movement with the next char,
8432 * without breaking undo. */
8433 dont_sync_undo = MAYBE;
8434 break;
8435
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008437 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 }
8439}
8440
8441/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008442 * CTRL-^ in Insert mode.
8443 */
8444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008445ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008446{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008447 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008448 {
8449 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8450 if (State & LANGMAP)
8451 {
8452 curbuf->b_p_iminsert = B_IMODE_NONE;
8453 State &= ~LANGMAP;
8454 }
8455 else
8456 {
8457 curbuf->b_p_iminsert = B_IMODE_LMAP;
8458 State |= LANGMAP;
8459#ifdef USE_IM_CONTROL
8460 im_set_active(FALSE);
8461#endif
8462 }
8463 }
8464#ifdef USE_IM_CONTROL
8465 else
8466 {
8467 /* There are no ":lmap" mappings, toggle IM */
8468 if (im_get_status())
8469 {
8470 curbuf->b_p_iminsert = B_IMODE_NONE;
8471 im_set_active(FALSE);
8472 }
8473 else
8474 {
8475 curbuf->b_p_iminsert = B_IMODE_IM;
8476 State &= ~LANGMAP;
8477 im_set_active(TRUE);
8478 }
8479 }
8480#endif
8481 set_iminsert_global();
8482 showmode();
8483#ifdef FEAT_GUI
8484 /* may show different cursor shape or color */
8485 if (gui.in_use)
8486 gui_update_cursor(TRUE, FALSE);
8487#endif
8488#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8489 /* Show/unshow value of 'keymap' in status lines. */
8490 status_redraw_curbuf();
8491#endif
8492}
8493
8494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 * Handle ESC in insert mode.
8496 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8497 * insert.
8498 */
8499 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008500ins_esc(
8501 long *count,
8502 int cmdchar,
8503 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504{
8505 int temp;
8506 static int disabled_redraw = FALSE;
8507
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008508#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008509 check_spell_redraw();
8510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511#if defined(FEAT_HANGULIN)
8512# if defined(ESC_CHG_TO_ENG_MODE)
8513 hangul_input_state_set(0);
8514# endif
8515 if (composing_hangul)
8516 {
8517 push_raw_key(composing_hangul_buffer, 2);
8518 composing_hangul = 0;
8519 }
8520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521
8522 temp = curwin->w_cursor.col;
8523 if (disabled_redraw)
8524 {
8525 --RedrawingDisabled;
8526 disabled_redraw = FALSE;
8527 }
8528 if (!arrow_used)
8529 {
8530 /*
8531 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008532 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8533 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 */
8535 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008536 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537
8538 /*
8539 * Repeating insert may take a long time. Check for
8540 * interrupt now and then.
8541 */
8542 if (*count > 0)
8543 {
8544 line_breakcheck();
8545 if (got_int)
8546 *count = 0;
8547 }
8548
8549 if (--*count > 0) /* repeat what was typed */
8550 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008551 /* Vi repeats the insert without replacing characters. */
8552 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8553 State &= ~REPLACE_FLAG;
8554
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 (void)start_redo_ins();
8556 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008557 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 ++RedrawingDisabled;
8559 disabled_redraw = TRUE;
8560 return FALSE; /* repeat the insert */
8561 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008562 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 undisplay_dollar();
8564 }
8565
8566 /* When an autoindent was removed, curswant stays after the
8567 * indent */
8568 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8569 curwin->w_set_curswant = TRUE;
8570
8571 /* Remember the last Insert position in the '^ mark. */
8572 if (!cmdmod.keepjumps)
8573 curbuf->b_last_insert = curwin->w_cursor;
8574
8575 /*
8576 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008577 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008579 if (!nomove
8580 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581#ifdef FEAT_VIRTUALEDIT
8582 || curwin->w_cursor.coladd > 0
8583#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008584 )
8585 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008586 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587#ifdef FEAT_RIGHTLEFT
8588 && !revins_on
8589#endif
8590 )
8591 {
8592#ifdef FEAT_VIRTUALEDIT
8593 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8594 {
8595 oneleft();
8596 if (restart_edit != NUL)
8597 ++curwin->w_cursor.coladd;
8598 }
8599 else
8600#endif
8601 {
8602 --curwin->w_cursor.col;
8603#ifdef FEAT_MBYTE
8604 /* Correct cursor for multi-byte character. */
8605 if (has_mbyte)
8606 mb_adjust_cursor();
8607#endif
8608 }
8609 }
8610
8611#ifdef USE_IM_CONTROL
8612 /* Disable IM to allow typing English directly for Normal mode commands.
8613 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8614 * well). */
8615 if (!(State & LANGMAP))
8616 im_save_status(&curbuf->b_p_iminsert);
8617 im_set_active(FALSE);
8618#endif
8619
8620 State = NORMAL;
8621 /* need to position cursor again (e.g. when on a TAB ) */
8622 changed_cline_bef_curs();
8623
8624#ifdef FEAT_MOUSE
8625 setmouse();
8626#endif
8627#ifdef CURSOR_SHAPE
8628 ui_cursor_shape(); /* may show different cursor shape */
8629#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008630 if (!p_ek)
8631 /* Re-enable bracketed paste mode. */
8632 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633
8634 /*
8635 * When recording or for CTRL-O, need to display the new mode.
8636 * Otherwise remove the mode message.
8637 */
8638 if (Recording || restart_edit != NUL)
8639 showmode();
8640 else if (p_smd)
8641 MSG("");
8642
8643 return TRUE; /* exit Insert mode */
8644}
8645
8646#ifdef FEAT_RIGHTLEFT
8647/*
8648 * Toggle language: hkmap and revins_on.
8649 * Move to end of reverse inserted text.
8650 */
8651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008652ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653{
8654 if (revins_on && revins_chars && revins_scol >= 0)
8655 {
8656 while (gchar_cursor() != NUL && revins_chars--)
8657 ++curwin->w_cursor.col;
8658 }
8659 p_ri = !p_ri;
8660 revins_on = (State == INSERT && p_ri);
8661 if (revins_on)
8662 {
8663 revins_scol = curwin->w_cursor.col;
8664 revins_legal++;
8665 revins_chars = 0;
8666 undisplay_dollar();
8667 }
8668 else
8669 revins_scol = -1;
8670#ifdef FEAT_FKMAP
8671 if (p_altkeymap)
8672 {
8673 /*
8674 * to be consistent also for redo command, using '.'
8675 * set arrow_used to true and stop it - causing to redo
8676 * characters entered in one mode (normal/reverse insert).
8677 */
8678 arrow_used = TRUE;
8679 (void)stop_arrow();
8680 p_fkmap = curwin->w_p_rl ^ p_ri;
8681 if (p_fkmap && p_ri)
8682 State = INSERT;
8683 }
8684 else
8685#endif
8686 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8687 showmode();
8688}
8689#endif
8690
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691/*
8692 * If 'keymodel' contains "startsel", may start selection.
8693 * Returns TRUE when a CTRL-O and other keys stuffed.
8694 */
8695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008696ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697{
8698 if (km_startsel)
8699 switch (c)
8700 {
8701 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 case K_PAGEUP:
8704 case K_KPAGEUP:
8705 case K_PAGEDOWN:
8706 case K_KPAGEDOWN:
8707# ifdef MACOS
8708 case K_LEFT:
8709 case K_RIGHT:
8710 case K_UP:
8711 case K_DOWN:
8712 case K_END:
8713 case K_HOME:
8714# endif
8715 if (!(mod_mask & MOD_MASK_SHIFT))
8716 break;
8717 /* FALLTHROUGH */
8718 case K_S_LEFT:
8719 case K_S_RIGHT:
8720 case K_S_UP:
8721 case K_S_DOWN:
8722 case K_S_END:
8723 case K_S_HOME:
8724 /* Start selection right away, the cursor can move with
8725 * CTRL-O when beyond the end of the line. */
8726 start_selection();
8727
8728 /* Execute the key in (insert) Select mode. */
8729 stuffcharReadbuff(Ctrl_O);
8730 if (mod_mask)
8731 {
8732 char_u buf[4];
8733
8734 buf[0] = K_SPECIAL;
8735 buf[1] = KS_MODIFIER;
8736 buf[2] = mod_mask;
8737 buf[3] = NUL;
8738 stuffReadbuff(buf);
8739 }
8740 stuffcharReadbuff(c);
8741 return TRUE;
8742 }
8743 return FALSE;
8744}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745
8746/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008747 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008748 */
8749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008750ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008751{
8752#ifdef FEAT_FKMAP
8753 if (p_fkmap && p_ri)
8754 {
8755 beep_flush();
8756 EMSG(farsi_text_3); /* encoded in Farsi */
8757 return;
8758 }
8759#endif
8760
8761#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008762# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008763 set_vim_var_string(VV_INSERTMODE,
8764 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008765# ifdef FEAT_VREPLACE
8766 replaceState == VREPLACE ? "v" :
8767# endif
8768 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008769# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008770 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8771#endif
8772 if (State & REPLACE_FLAG)
8773 State = INSERT | (State & LANGMAP);
8774 else
8775 State = replaceState | (State & LANGMAP);
8776 AppendCharToRedobuff(K_INS);
8777 showmode();
8778#ifdef CURSOR_SHAPE
8779 ui_cursor_shape(); /* may show different cursor shape */
8780#endif
8781}
8782
8783/*
8784 * Pressed CTRL-O in Insert mode.
8785 */
8786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008787ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008788{
8789#ifdef FEAT_VREPLACE
8790 if (State & VREPLACE_FLAG)
8791 restart_edit = 'V';
8792 else
8793#endif
8794 if (State & REPLACE_FLAG)
8795 restart_edit = 'R';
8796 else
8797 restart_edit = 'I';
8798#ifdef FEAT_VIRTUALEDIT
8799 if (virtual_active())
8800 ins_at_eol = FALSE; /* cursor always keeps its column */
8801 else
8802#endif
8803 ins_at_eol = (gchar_cursor() == NUL);
8804}
8805
8806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 * If the cursor is on an indent, ^T/^D insert/delete one
8808 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008809 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 * with vi. But vi only supports ^T and ^D after an
8811 * autoindent, we support it everywhere.
8812 */
8813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008814ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
8816 if (stop_arrow() == FAIL)
8817 return;
8818 AppendCharToRedobuff(c);
8819
8820 /*
8821 * 0^D and ^^D: remove all indent.
8822 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008823 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8824 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 {
8826 --curwin->w_cursor.col;
8827 (void)del_char(FALSE); /* delete the '^' or '0' */
8828 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8829 if (State & REPLACE_FLAG)
8830 replace_pop_ins();
8831 if (lastc == '^')
8832 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008833 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 }
8835 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008836 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837
8838 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8839 did_ai = FALSE;
8840#ifdef FEAT_SMARTINDENT
8841 did_si = FALSE;
8842 can_si = FALSE;
8843 can_si_back = FALSE;
8844#endif
8845#ifdef FEAT_CINDENT
8846 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8847#endif
8848}
8849
8850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008851ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
8853 int temp;
8854
8855 if (stop_arrow() == FAIL)
8856 return;
8857 if (gchar_cursor() == NUL) /* delete newline */
8858 {
8859 temp = curwin->w_cursor.col;
8860 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008861 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008862 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 else
8864 curwin->w_cursor.col = temp;
8865 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008866 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8867 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 did_ai = FALSE;
8869#ifdef FEAT_SMARTINDENT
8870 did_si = FALSE;
8871 can_si = FALSE;
8872 can_si_back = FALSE;
8873#endif
8874 AppendCharToRedobuff(K_DEL);
8875}
8876
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008877static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008878
8879/*
8880 * Delete one character for ins_bs().
8881 */
8882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008883ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008884{
8885 dec_cursor();
8886 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8887 if (State & REPLACE_FLAG)
8888 {
8889 /* Don't delete characters before the insert point when in
8890 * Replace mode */
8891 if (curwin->w_cursor.lnum != Insstart.lnum
8892 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008893 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008894 }
8895 else
8896 (void)del_char(FALSE);
8897}
8898
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899/*
8900 * Handle Backspace, delete-word and delete-line in Insert mode.
8901 * Return TRUE when backspace was actually used.
8902 */
8903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008904ins_bs(
8905 int c,
8906 int mode,
8907 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909 linenr_T lnum;
8910 int cc;
8911 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008912 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 colnr_T mincol;
8914 int did_backspace = FALSE;
8915 int in_indent;
8916 int oldState;
8917#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008918 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919#endif
8920
8921 /*
8922 * can't delete anything in an empty file
8923 * can't backup past first character in buffer
8924 * can't backup past starting point unless 'backspace' > 1
8925 * can backup to a previous line if 'backspace' == 0
8926 */
8927 if ( bufempty()
8928 || (
8929#ifdef FEAT_RIGHTLEFT
8930 !revins_on &&
8931#endif
8932 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8933 || (!can_bs(BS_START)
8934 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008935 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8936 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8938 && curwin->w_cursor.col <= ai_col)
8939 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8940 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008941 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 return FALSE;
8943 }
8944
8945 if (stop_arrow() == FAIL)
8946 return FALSE;
8947 in_indent = inindent(0);
8948#ifdef FEAT_CINDENT
8949 if (in_indent)
8950 can_cindent = FALSE;
8951#endif
8952#ifdef FEAT_COMMENTS
8953 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8954#endif
8955#ifdef FEAT_RIGHTLEFT
8956 if (revins_on) /* put cursor after last inserted char */
8957 inc_cursor();
8958#endif
8959
8960#ifdef FEAT_VIRTUALEDIT
8961 /* Virtualedit:
8962 * BACKSPACE_CHAR eats a virtual space
8963 * BACKSPACE_WORD eats all coladd
8964 * BACKSPACE_LINE eats all coladd and keeps going
8965 */
8966 if (curwin->w_cursor.coladd > 0)
8967 {
8968 if (mode == BACKSPACE_CHAR)
8969 {
8970 --curwin->w_cursor.coladd;
8971 return TRUE;
8972 }
8973 if (mode == BACKSPACE_WORD)
8974 {
8975 curwin->w_cursor.coladd = 0;
8976 return TRUE;
8977 }
8978 curwin->w_cursor.coladd = 0;
8979 }
8980#endif
8981
8982 /*
8983 * delete newline!
8984 */
8985 if (curwin->w_cursor.col == 0)
8986 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008987 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008988 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989#ifdef FEAT_RIGHTLEFT
8990 || revins_on
8991#endif
8992 )
8993 {
8994 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8995 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8996 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008997 --Insstart.lnum;
8998 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 }
9000 /*
9001 * In replace mode:
9002 * cc < 0: NL was inserted, delete it
9003 * cc >= 0: NL was replaced, put original characters back
9004 */
9005 cc = -1;
9006 if (State & REPLACE_FLAG)
9007 cc = replace_pop(); /* returns -1 if NL was inserted */
9008 /*
9009 * In replace mode, in the line we started replacing, we only move the
9010 * cursor.
9011 */
9012 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9013 {
9014 dec_cursor();
9015 }
9016 else
9017 {
9018#ifdef FEAT_VREPLACE
9019 if (!(State & VREPLACE_FLAG)
9020 || curwin->w_cursor.lnum > orig_line_count)
9021#endif
9022 {
9023 temp = gchar_cursor(); /* remember current char */
9024 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009025
9026 /* When "aw" is in 'formatoptions' we must delete the space at
9027 * the end of the line, otherwise the line will be broken
9028 * again when auto-formatting. */
9029 if (has_format_option(FO_AUTO)
9030 && has_format_option(FO_WHITE_PAR))
9031 {
9032 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9033 TRUE);
9034 int len;
9035
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009036 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009037 if (len > 0 && ptr[len - 1] == ' ')
9038 ptr[len - 1] = NUL;
9039 }
9040
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009041 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 if (temp == NUL && gchar_cursor() != NUL)
9043 inc_cursor();
9044 }
9045#ifdef FEAT_VREPLACE
9046 else
9047 dec_cursor();
9048#endif
9049
9050 /*
9051 * In REPLACE mode we have to put back the text that was replaced
9052 * by the NL. On the replace stack is first a NUL-terminated
9053 * sequence of characters that were deleted and then the
9054 * characters that NL replaced.
9055 */
9056 if (State & REPLACE_FLAG)
9057 {
9058 /*
9059 * Do the next ins_char() in NORMAL state, to
9060 * prevent ins_char() from replacing characters and
9061 * avoiding showmatch().
9062 */
9063 oldState = State;
9064 State = NORMAL;
9065 /*
9066 * restore characters (blanks) deleted after cursor
9067 */
9068 while (cc > 0)
9069 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009070 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071#ifdef FEAT_MBYTE
9072 mb_replace_pop_ins(cc);
9073#else
9074 ins_char(cc);
9075#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009076 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 cc = replace_pop();
9078 }
9079 /* restore the characters that NL replaced */
9080 replace_pop_ins();
9081 State = oldState;
9082 }
9083 }
9084 did_ai = FALSE;
9085 }
9086 else
9087 {
9088 /*
9089 * Delete character(s) before the cursor.
9090 */
9091#ifdef FEAT_RIGHTLEFT
9092 if (revins_on) /* put cursor on last inserted char */
9093 dec_cursor();
9094#endif
9095 mincol = 0;
9096 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009097 if (mode == BACKSPACE_LINE
9098 && (curbuf->b_p_ai
9099#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009100 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009101#endif
9102 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103#ifdef FEAT_RIGHTLEFT
9104 && !revins_on
9105#endif
9106 )
9107 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009108 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009110 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009112 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 }
9114
9115 /*
9116 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9117 */
9118 if ( mode == BACKSPACE_CHAR
9119 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009120 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009121 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 && (*(ml_get_cursor() - 1) == TAB
9123 || (*(ml_get_cursor() - 1) == ' '
9124 && (!*inserted_space_p
9125 || arrow_used))))))
9126 {
9127 int ts;
9128 colnr_T vcol;
9129 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009130 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131
9132 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009133 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009134 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009136 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 /* Compute the virtual column where we want to be. Since
9138 * 'showbreak' may get in the way, need to get the last column of
9139 * the previous character. */
9140 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009141 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 dec_cursor();
9143 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9144 inc_cursor();
9145 want_vcol = (want_vcol / ts) * ts;
9146
9147 /* delete characters until we are at or before want_vcol */
9148 while (vcol > want_vcol
9149 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009150 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151
9152 /* insert extra spaces until we are at want_vcol */
9153 while (vcol < want_vcol)
9154 {
9155 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009156 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9157 && curwin->w_cursor.col < Insstart_orig.col)
9158 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159
9160#ifdef FEAT_VREPLACE
9161 if (State & VREPLACE_FLAG)
9162 ins_char(' ');
9163 else
9164#endif
9165 {
9166 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009167 if ((State & REPLACE_FLAG))
9168 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 }
9170 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9171 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009172
9173 /* If we are now back where we started delete one character. Can
9174 * happen when using 'sts' and 'linebreak'. */
9175 if (vcol >= start_vcol)
9176 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 }
9178
9179 /*
9180 * Delete upto starting point, start of line or previous word.
9181 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009182 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009184#ifdef FEAT_MBYTE
9185 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009187 if (has_mbyte)
9188 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009190 do
9191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009193 if (!revins_on) /* put cursor on char to be deleted */
9194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009196
9197 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009199 /* look multi-byte character class */
9200 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009202 prev_cclass = cclass;
9203 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009206
9207 /* start of word? */
9208 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9209 {
9210 mode = BACKSPACE_WORD_NOT_SPACE;
9211 temp = vim_iswordc(cc);
9212 }
9213 /* end of word? */
9214 else if (mode == BACKSPACE_WORD_NOT_SPACE
9215 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9216#ifdef FEAT_MBYTE
9217 || prev_cclass != cclass
9218#endif
9219 ))
9220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009222 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009224 inc_cursor();
9225#ifdef FEAT_RIGHTLEFT
9226 else if (State & REPLACE_FLAG)
9227 dec_cursor();
9228#endif
9229 break;
9230 }
9231 if (State & REPLACE_FLAG)
9232 replace_do_bs(-1);
9233 else
9234 {
9235#ifdef FEAT_MBYTE
9236 if (enc_utf8 && p_deco)
9237 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9238#endif
9239 (void)del_char(FALSE);
9240#ifdef FEAT_MBYTE
9241 /*
9242 * If there are combining characters and 'delcombine' is set
9243 * move the cursor back. Don't back up before the base
9244 * character.
9245 */
9246 if (enc_utf8 && p_deco && cpc[0] != NUL)
9247 inc_cursor();
9248#endif
9249#ifdef FEAT_RIGHTLEFT
9250 if (revins_chars)
9251 {
9252 revins_chars--;
9253 revins_legal++;
9254 }
9255 if (revins_on && gchar_cursor() == NUL)
9256 break;
9257#endif
9258 }
9259 /* Just a single backspace?: */
9260 if (mode == BACKSPACE_CHAR)
9261 break;
9262 } while (
9263#ifdef FEAT_RIGHTLEFT
9264 revins_on ||
9265#endif
9266 (curwin->w_cursor.col > mincol
9267 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9268 || curwin->w_cursor.col != Insstart_orig.col)));
9269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 did_backspace = TRUE;
9271 }
9272#ifdef FEAT_SMARTINDENT
9273 did_si = FALSE;
9274 can_si = FALSE;
9275 can_si_back = FALSE;
9276#endif
9277 if (curwin->w_cursor.col <= 1)
9278 did_ai = FALSE;
9279 /*
9280 * It's a little strange to put backspaces into the redo
9281 * buffer, but it makes auto-indent a lot easier to deal
9282 * with.
9283 */
9284 AppendCharToRedobuff(c);
9285
9286 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009287 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9288 && curwin->w_cursor.col < Insstart_orig.col)
9289 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290
9291 /* vi behaviour: the cursor moves backward but the character that
9292 * was there remains visible
9293 * Vim behaviour: the cursor moves backward and the character that
9294 * was there is erased from the screen.
9295 * We can emulate the vi behaviour by pretending there is a dollar
9296 * displayed even when there isn't.
9297 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009298 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 dollar_vcol = curwin->w_virtcol;
9300
Bram Moolenaarce3be472008-01-14 19:12:28 +00009301#ifdef FEAT_FOLDING
9302 /* When deleting a char the cursor line must never be in a closed fold.
9303 * E.g., when 'foldmethod' is indent and deleting the first non-white
9304 * char before a Tab. */
9305 if (did_backspace)
9306 foldOpenCursor();
9307#endif
9308
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 return did_backspace;
9310}
9311
9312#ifdef FEAT_MOUSE
9313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009314ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315{
9316 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009317 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318
9319# ifdef FEAT_GUI
9320 /* When GUI is active, also move/paste when 'mouse' is empty */
9321 if (!gui.in_use)
9322# endif
9323 if (!mouse_has(MOUSE_INSERT))
9324 return;
9325
9326 undisplay_dollar();
9327 tpos = curwin->w_cursor;
9328 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9329 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009330#ifdef FEAT_WINDOWS
9331 win_T *new_curwin = curwin;
9332
9333 if (curwin != old_curwin && win_valid(old_curwin))
9334 {
9335 /* Mouse took us to another window. We need to go back to the
9336 * previous one to stop insert there properly. */
9337 curwin = old_curwin;
9338 curbuf = curwin->w_buffer;
9339 }
9340#endif
9341 start_arrow(curwin == old_curwin ? &tpos : NULL);
9342#ifdef FEAT_WINDOWS
9343 if (curwin != new_curwin && win_valid(new_curwin))
9344 {
9345 curwin = new_curwin;
9346 curbuf = curwin->w_buffer;
9347 }
9348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349# ifdef FEAT_CINDENT
9350 can_cindent = TRUE;
9351# endif
9352 }
9353
9354#ifdef FEAT_WINDOWS
9355 /* redraw status lines (in case another window became active) */
9356 redraw_statuslines();
9357#endif
9358}
9359
9360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009361ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362{
9363 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009364# if defined(FEAT_WINDOWS)
9365 win_T *old_curwin = curwin;
9366# endif
9367# ifdef FEAT_INS_EXPAND
9368 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369# endif
9370
9371 tpos = curwin->w_cursor;
9372
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009373# ifdef FEAT_WINDOWS
9374 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 {
9376 int row, col;
9377
9378 row = mouse_row;
9379 col = mouse_col;
9380
9381 /* find the window at the pointer coordinates */
9382 curwin = mouse_find_win(&row, &col);
9383 curbuf = curwin->w_buffer;
9384 }
9385 if (curwin == old_curwin)
9386# endif
9387 undisplay_dollar();
9388
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009389# ifdef FEAT_INS_EXPAND
9390 /* Don't scroll the window in which completion is being done. */
9391 if (!pum_visible()
9392# if defined(FEAT_WINDOWS)
9393 || curwin != old_curwin
9394# endif
9395 )
9396# endif
9397 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009398 if (dir == MSCR_DOWN || dir == MSCR_UP)
9399 {
9400 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9401 scroll_redraw(dir,
9402 (long)(curwin->w_botline - curwin->w_topline));
9403 else
9404 scroll_redraw(dir, 3L);
9405 }
9406#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009407 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009408 {
9409 int val, step = 6;
9410
9411 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9412 step = W_WIDTH(curwin);
9413 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9414 if (val < 0)
9415 val = 0;
9416 gui_do_horiz_scroll(val, TRUE);
9417 }
9418#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009419# ifdef FEAT_INS_EXPAND
9420 did_scroll = TRUE;
9421# endif
9422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009424# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 curwin->w_redr_status = TRUE;
9426
9427 curwin = old_curwin;
9428 curbuf = curwin->w_buffer;
9429# endif
9430
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009431# ifdef FEAT_INS_EXPAND
9432 /* The popup menu may overlay the window, need to redraw it.
9433 * TODO: Would be more efficient to only redraw the windows that are
9434 * overlapped by the popup menu. */
9435 if (pum_visible() && did_scroll)
9436 {
9437 redraw_all_later(NOT_VALID);
9438 ins_compl_show_pum();
9439 }
9440# endif
9441
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 if (!equalpos(curwin->w_cursor, tpos))
9443 {
9444 start_arrow(&tpos);
9445# ifdef FEAT_CINDENT
9446 can_cindent = TRUE;
9447# endif
9448 }
9449}
9450#endif
9451
Bram Moolenaarec2da362017-01-21 20:04:22 +01009452/*
9453 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9454 * P_PE literally.
9455 * When "drop" is TRUE then consume the text and drop it.
9456 */
9457 int
9458bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9459{
9460 int c;
9461 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9462 int idx = 0;
9463 char_u *end = find_termcode((char_u *)"PE");
9464 int ret_char = -1;
9465 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009466 int save_paste = p_paste;
9467 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009468
9469 /* If the end code is too long we can't detect it, read everything. */
9470 if (STRLEN(end) >= NUMBUFLEN)
9471 end = NULL;
9472 ++no_mapping;
9473 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009474 p_paste = TRUE;
9475 curbuf->b_p_ai = FALSE;
9476
Bram Moolenaarec2da362017-01-21 20:04:22 +01009477 for (;;)
9478 {
9479 /* When the end is not defined read everything. */
9480 if (end == NULL && vpeekc() == NUL)
9481 break;
9482 c = plain_vgetc();
9483#ifdef FEAT_MBYTE
9484 if (has_mbyte)
9485 idx += (*mb_char2bytes)(c, buf + idx);
9486 else
9487#endif
9488 buf[idx++] = c;
9489 buf[idx] = NUL;
9490 if (end != NUL && STRNCMP(buf, end, idx) == 0)
9491 {
9492 if (end[idx] == NUL)
9493 break; /* Found the end of paste code. */
9494 continue;
9495 }
9496 if (!drop)
9497 {
9498 switch (mode)
9499 {
9500 case PASTE_CMDLINE:
9501 put_on_cmdline(buf, idx, TRUE);
9502 break;
9503
9504 case PASTE_EX:
9505 if (gap != NULL && ga_grow(gap, idx) == OK)
9506 {
9507 mch_memmove((char *)gap->ga_data + gap->ga_len,
9508 buf, (size_t)idx);
9509 gap->ga_len += idx;
9510 }
9511 break;
9512
9513 case PASTE_INSERT:
9514 if (stop_arrow() == OK)
9515 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009516 c = buf[0];
9517 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9518 ins_eol(c);
9519 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009520 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009521 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009522 AppendToRedobuffLit(buf, idx);
9523 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009524 }
9525 break;
9526
9527 case PASTE_ONE_CHAR:
9528 if (ret_char == -1)
9529 {
9530#ifdef FEAT_MBYTE
9531 if (has_mbyte)
9532 ret_char = (*mb_ptr2char)(buf);
9533 else
9534#endif
9535 ret_char = buf[0];
9536 }
9537 break;
9538 }
9539 }
9540 idx = 0;
9541 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009542
Bram Moolenaarec2da362017-01-21 20:04:22 +01009543 --no_mapping;
9544 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009545 p_paste = save_paste;
9546 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009547
9548 return ret_char;
9549}
9550
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009551#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009553ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009554{
9555 /* We will be leaving the current window, unless closing another tab. */
9556 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9557 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9558 {
9559 undisplay_dollar();
9560 start_arrow(&curwin->w_cursor);
9561# ifdef FEAT_CINDENT
9562 can_cindent = TRUE;
9563# endif
9564 }
9565
9566 if (c == K_TABLINE)
9567 goto_tabpage(current_tab);
9568 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009569 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009570 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009571 redraw_statuslines(); /* will redraw the tabline when needed */
9572 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009573}
9574#endif
9575
9576#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009578ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
9580 pos_T tpos;
9581
9582 undisplay_dollar();
9583 tpos = curwin->w_cursor;
9584 if (gui_do_scroll())
9585 {
9586 start_arrow(&tpos);
9587# ifdef FEAT_CINDENT
9588 can_cindent = TRUE;
9589# endif
9590 }
9591}
9592
9593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009594ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595{
9596 pos_T tpos;
9597
9598 undisplay_dollar();
9599 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009600 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 {
9602 start_arrow(&tpos);
9603# ifdef FEAT_CINDENT
9604 can_cindent = TRUE;
9605# endif
9606 }
9607}
9608#endif
9609
9610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009611ins_left(
9612 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
9614 pos_T tpos;
9615
9616#ifdef FEAT_FOLDING
9617 if ((fdo_flags & FDO_HOR) && KeyTyped)
9618 foldOpenCursor();
9619#endif
9620 undisplay_dollar();
9621 tpos = curwin->w_cursor;
9622 if (oneleft() == OK)
9623 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009624#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9625 /* Only call start_arrow() when not busy with preediting, it will
9626 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9627 if (!im_is_preediting())
9628#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009629 {
9630 start_arrow_with_change(&tpos, end_change);
9631 if (!end_change)
9632 AppendCharToRedobuff(K_LEFT);
9633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634#ifdef FEAT_RIGHTLEFT
9635 /* If exit reversed string, position is fixed */
9636 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9637 revins_legal++;
9638 revins_chars++;
9639#endif
9640 }
9641
9642 /*
9643 * if 'whichwrap' set for cursor in insert mode may go to
9644 * previous line
9645 */
9646 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9647 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009648 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 start_arrow(&tpos);
9650 --(curwin->w_cursor.lnum);
9651 coladvance((colnr_T)MAXCOL);
9652 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9653 }
9654 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009655 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009656 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657}
9658
9659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009660ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662 pos_T tpos;
9663
9664#ifdef FEAT_FOLDING
9665 if ((fdo_flags & FDO_HOR) && KeyTyped)
9666 foldOpenCursor();
9667#endif
9668 undisplay_dollar();
9669 tpos = curwin->w_cursor;
9670 if (c == K_C_HOME)
9671 curwin->w_cursor.lnum = 1;
9672 curwin->w_cursor.col = 0;
9673#ifdef FEAT_VIRTUALEDIT
9674 curwin->w_cursor.coladd = 0;
9675#endif
9676 curwin->w_curswant = 0;
9677 start_arrow(&tpos);
9678}
9679
9680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009681ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682{
9683 pos_T tpos;
9684
9685#ifdef FEAT_FOLDING
9686 if ((fdo_flags & FDO_HOR) && KeyTyped)
9687 foldOpenCursor();
9688#endif
9689 undisplay_dollar();
9690 tpos = curwin->w_cursor;
9691 if (c == K_C_END)
9692 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9693 coladvance((colnr_T)MAXCOL);
9694 curwin->w_curswant = MAXCOL;
9695
9696 start_arrow(&tpos);
9697}
9698
9699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009700ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
9702#ifdef FEAT_FOLDING
9703 if ((fdo_flags & FDO_HOR) && KeyTyped)
9704 foldOpenCursor();
9705#endif
9706 undisplay_dollar();
9707 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9708 {
9709 start_arrow(&curwin->w_cursor);
9710 (void)bck_word(1L, FALSE, FALSE);
9711 curwin->w_set_curswant = TRUE;
9712 }
9713 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009714 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715}
9716
9717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009718ins_right(
9719 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
9721#ifdef FEAT_FOLDING
9722 if ((fdo_flags & FDO_HOR) && KeyTyped)
9723 foldOpenCursor();
9724#endif
9725 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009726 if (gchar_cursor() != NUL
9727#ifdef FEAT_VIRTUALEDIT
9728 || virtual_active()
9729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 )
9731 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009732 start_arrow_with_change(&curwin->w_cursor, end_change);
9733 if (!end_change)
9734 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 curwin->w_set_curswant = TRUE;
9736#ifdef FEAT_VIRTUALEDIT
9737 if (virtual_active())
9738 oneright();
9739 else
9740#endif
9741 {
9742#ifdef FEAT_MBYTE
9743 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009744 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 else
9746#endif
9747 ++curwin->w_cursor.col;
9748 }
9749
9750#ifdef FEAT_RIGHTLEFT
9751 revins_legal++;
9752 if (revins_chars)
9753 revins_chars--;
9754#endif
9755 }
9756 /* if 'whichwrap' set for cursor in insert mode, may move the
9757 * cursor to the next line */
9758 else if (vim_strchr(p_ww, ']') != NULL
9759 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9760 {
9761 start_arrow(&curwin->w_cursor);
9762 curwin->w_set_curswant = TRUE;
9763 ++curwin->w_cursor.lnum;
9764 curwin->w_cursor.col = 0;
9765 }
9766 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009767 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009768 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769}
9770
9771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009772ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773{
9774#ifdef FEAT_FOLDING
9775 if ((fdo_flags & FDO_HOR) && KeyTyped)
9776 foldOpenCursor();
9777#endif
9778 undisplay_dollar();
9779 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9780 || gchar_cursor() != NUL)
9781 {
9782 start_arrow(&curwin->w_cursor);
9783 (void)fwd_word(1L, FALSE, 0);
9784 curwin->w_set_curswant = TRUE;
9785 }
9786 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009787 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788}
9789
9790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009791ins_up(
9792 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793{
9794 pos_T tpos;
9795 linenr_T old_topline = curwin->w_topline;
9796#ifdef FEAT_DIFF
9797 int old_topfill = curwin->w_topfill;
9798#endif
9799
9800 undisplay_dollar();
9801 tpos = curwin->w_cursor;
9802 if (cursor_up(1L, TRUE) == OK)
9803 {
9804 if (startcol)
9805 coladvance(getvcol_nolist(&Insstart));
9806 if (old_topline != curwin->w_topline
9807#ifdef FEAT_DIFF
9808 || old_topfill != curwin->w_topfill
9809#endif
9810 )
9811 redraw_later(VALID);
9812 start_arrow(&tpos);
9813#ifdef FEAT_CINDENT
9814 can_cindent = TRUE;
9815#endif
9816 }
9817 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009818 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819}
9820
9821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009822ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823{
9824 pos_T tpos;
9825
9826 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009827
9828#ifdef FEAT_WINDOWS
9829 if (mod_mask & MOD_MASK_CTRL)
9830 {
9831 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009832 if (first_tabpage->tp_next != NULL)
9833 {
9834 start_arrow(&curwin->w_cursor);
9835 goto_tabpage(-1);
9836 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009837 return;
9838 }
9839#endif
9840
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 tpos = curwin->w_cursor;
9842 if (onepage(BACKWARD, 1L) == OK)
9843 {
9844 start_arrow(&tpos);
9845#ifdef FEAT_CINDENT
9846 can_cindent = TRUE;
9847#endif
9848 }
9849 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009850 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851}
9852
9853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009854ins_down(
9855 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856{
9857 pos_T tpos;
9858 linenr_T old_topline = curwin->w_topline;
9859#ifdef FEAT_DIFF
9860 int old_topfill = curwin->w_topfill;
9861#endif
9862
9863 undisplay_dollar();
9864 tpos = curwin->w_cursor;
9865 if (cursor_down(1L, TRUE) == OK)
9866 {
9867 if (startcol)
9868 coladvance(getvcol_nolist(&Insstart));
9869 if (old_topline != curwin->w_topline
9870#ifdef FEAT_DIFF
9871 || old_topfill != curwin->w_topfill
9872#endif
9873 )
9874 redraw_later(VALID);
9875 start_arrow(&tpos);
9876#ifdef FEAT_CINDENT
9877 can_cindent = TRUE;
9878#endif
9879 }
9880 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009881 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882}
9883
9884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009885ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886{
9887 pos_T tpos;
9888
9889 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009890
9891#ifdef FEAT_WINDOWS
9892 if (mod_mask & MOD_MASK_CTRL)
9893 {
9894 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009895 if (first_tabpage->tp_next != NULL)
9896 {
9897 start_arrow(&curwin->w_cursor);
9898 goto_tabpage(0);
9899 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009900 return;
9901 }
9902#endif
9903
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 tpos = curwin->w_cursor;
9905 if (onepage(FORWARD, 1L) == OK)
9906 {
9907 start_arrow(&tpos);
9908#ifdef FEAT_CINDENT
9909 can_cindent = TRUE;
9910#endif
9911 }
9912 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009913 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914}
9915
9916#ifdef FEAT_DND
9917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009918ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919{
9920 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9921}
9922#endif
9923
9924/*
9925 * Handle TAB in Insert or Replace mode.
9926 * Return TRUE when the TAB needs to be inserted like a normal character.
9927 */
9928 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009929ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930{
9931 int ind;
9932 int i;
9933 int temp;
9934
9935 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9936 Insstart_blank_vcol = get_nolist_virtcol();
9937 if (echeck_abbr(TAB + ABBR_OFF))
9938 return FALSE;
9939
9940 ind = inindent(0);
9941#ifdef FEAT_CINDENT
9942 if (ind)
9943 can_cindent = FALSE;
9944#endif
9945
9946 /*
9947 * When nothing special, insert TAB like a normal character
9948 */
9949 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009950 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009951 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 return TRUE;
9953
9954 if (stop_arrow() == FAIL)
9955 return TRUE;
9956
9957 did_ai = FALSE;
9958#ifdef FEAT_SMARTINDENT
9959 did_si = FALSE;
9960 can_si = FALSE;
9961 can_si_back = FALSE;
9962#endif
9963 AppendToRedobuff((char_u *)"\t");
9964
9965 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009966 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009967 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9968 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 else /* otherwise use 'tabstop' */
9970 temp = (int)curbuf->b_p_ts;
9971 temp -= get_nolist_virtcol() % temp;
9972
9973 /*
9974 * Insert the first space with ins_char(). It will delete one char in
9975 * replace mode. Insert the rest with ins_str(); it will not delete any
9976 * chars. For VREPLACE mode, we use ins_char() for all characters.
9977 */
9978 ins_char(' ');
9979 while (--temp > 0)
9980 {
9981#ifdef FEAT_VREPLACE
9982 if (State & VREPLACE_FLAG)
9983 ins_char(' ');
9984 else
9985#endif
9986 {
9987 ins_str((char_u *)" ");
9988 if (State & REPLACE_FLAG) /* no char replaced */
9989 replace_push(NUL);
9990 }
9991 }
9992
9993 /*
9994 * When 'expandtab' not set: Replace spaces by TABs where possible.
9995 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009996 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 {
9998 char_u *ptr;
9999#ifdef FEAT_VREPLACE
10000 char_u *saved_line = NULL; /* init for GCC */
10001 pos_T pos;
10002#endif
10003 pos_T fpos;
10004 pos_T *cursor;
10005 colnr_T want_vcol, vcol;
10006 int change_col = -1;
10007 int save_list = curwin->w_p_list;
10008
10009 /*
10010 * Get the current line. For VREPLACE mode, don't make real changes
10011 * yet, just work on a copy of the line.
10012 */
10013#ifdef FEAT_VREPLACE
10014 if (State & VREPLACE_FLAG)
10015 {
10016 pos = curwin->w_cursor;
10017 cursor = &pos;
10018 saved_line = vim_strsave(ml_get_curline());
10019 if (saved_line == NULL)
10020 return FALSE;
10021 ptr = saved_line + pos.col;
10022 }
10023 else
10024#endif
10025 {
10026 ptr = ml_get_cursor();
10027 cursor = &curwin->w_cursor;
10028 }
10029
10030 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10031 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10032 curwin->w_p_list = FALSE;
10033
10034 /* Find first white before the cursor */
10035 fpos = curwin->w_cursor;
10036 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
10037 {
10038 --fpos.col;
10039 --ptr;
10040 }
10041
10042 /* In Replace mode, don't change characters before the insert point. */
10043 if ((State & REPLACE_FLAG)
10044 && fpos.lnum == Insstart.lnum
10045 && fpos.col < Insstart.col)
10046 {
10047 ptr += Insstart.col - fpos.col;
10048 fpos.col = Insstart.col;
10049 }
10050
10051 /* compute virtual column numbers of first white and cursor */
10052 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10053 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10054
Bram Moolenaar597a4222014-06-25 14:39:50 +020010055 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10056 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 while (vim_iswhite(*ptr))
10058 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010059 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 if (vcol + i > want_vcol)
10061 break;
10062 if (*ptr != TAB)
10063 {
10064 *ptr = TAB;
10065 if (change_col < 0)
10066 {
10067 change_col = fpos.col; /* Column of first change */
10068 /* May have to adjust Insstart */
10069 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10070 Insstart.col = fpos.col;
10071 }
10072 }
10073 ++fpos.col;
10074 ++ptr;
10075 vcol += i;
10076 }
10077
10078 if (change_col >= 0)
10079 {
10080 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010081 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082
10083 /* Skip over the spaces we need. */
10084 while (vcol < want_vcol && *ptr == ' ')
10085 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010086 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 ++ptr;
10088 ++repl_off;
10089 }
10090 if (vcol > want_vcol)
10091 {
10092 /* Must have a char with 'showbreak' just before it. */
10093 --ptr;
10094 --repl_off;
10095 }
10096 fpos.col += repl_off;
10097
10098 /* Delete following spaces. */
10099 i = cursor->col - fpos.col;
10100 if (i > 0)
10101 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010102 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 /* correct replace stack. */
10104 if ((State & REPLACE_FLAG)
10105#ifdef FEAT_VREPLACE
10106 && !(State & VREPLACE_FLAG)
10107#endif
10108 )
10109 for (temp = i; --temp >= 0; )
10110 replace_join(repl_off);
10111 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010112#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010113 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010114 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010115 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010116 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10117 (char_u *)"\t", 1);
10118 }
10119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 cursor->col -= i;
10121
10122#ifdef FEAT_VREPLACE
10123 /*
10124 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10125 * backspacing over the changed spacing and then inserting the new
10126 * spacing.
10127 */
10128 if (State & VREPLACE_FLAG)
10129 {
10130 /* Backspace from real cursor to change_col */
10131 backspace_until_column(change_col);
10132
10133 /* Insert each char in saved_line from changed_col to
10134 * ptr-cursor */
10135 ins_bytes_len(saved_line + change_col,
10136 cursor->col - change_col);
10137 }
10138#endif
10139 }
10140
10141#ifdef FEAT_VREPLACE
10142 if (State & VREPLACE_FLAG)
10143 vim_free(saved_line);
10144#endif
10145 curwin->w_p_list = save_list;
10146 }
10147
10148 return FALSE;
10149}
10150
10151/*
10152 * Handle CR or NL in insert mode.
10153 * Return TRUE when out of memory or can't undo.
10154 */
10155 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010156ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157{
10158 int i;
10159
10160 if (echeck_abbr(c + ABBR_OFF))
10161 return FALSE;
10162 if (stop_arrow() == FAIL)
10163 return TRUE;
10164 undisplay_dollar();
10165
10166 /*
10167 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10168 * character under the cursor. Only push a NUL on the replace stack,
10169 * nothing to put back when the NL is deleted.
10170 */
10171 if ((State & REPLACE_FLAG)
10172#ifdef FEAT_VREPLACE
10173 && !(State & VREPLACE_FLAG)
10174#endif
10175 )
10176 replace_push(NUL);
10177
10178 /*
10179 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10180 * replacing the next line, so we push all of the characters left on the
10181 * line onto the replace stack. This is not done here though, it is done
10182 * in open_line().
10183 */
10184
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010185#ifdef FEAT_VIRTUALEDIT
10186 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10187 * CTRL-O). */
10188 if (virtual_active() && curwin->w_cursor.coladd > 0)
10189 coladvance(getviscol());
10190#endif
10191
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192#ifdef FEAT_RIGHTLEFT
10193# ifdef FEAT_FKMAP
10194 if (p_altkeymap && p_fkmap)
10195 fkmap(NL);
10196# endif
10197 /* NL in reverse insert will always start in the end of
10198 * current line. */
10199 if (revins_on)
10200 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10201#endif
10202
10203 AppendToRedobuff(NL_STR);
10204 i = open_line(FORWARD,
10205#ifdef FEAT_COMMENTS
10206 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10207#endif
10208 0, old_indent);
10209 old_indent = 0;
10210#ifdef FEAT_CINDENT
10211 can_cindent = TRUE;
10212#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010213#ifdef FEAT_FOLDING
10214 /* When inserting a line the cursor line must never be in a closed fold. */
10215 foldOpenCursor();
10216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217
10218 return (!i);
10219}
10220
10221#ifdef FEAT_DIGRAPHS
10222/*
10223 * Handle digraph in insert mode.
10224 * Returns character still to be inserted, or NUL when nothing remaining to be
10225 * done.
10226 */
10227 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010228ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229{
10230 int c;
10231 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010232 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233
10234 pc_status = PC_STATUS_UNSET;
10235 if (redrawing() && !char_avail())
10236 {
10237 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010238 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239
10240 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010241 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242#ifdef FEAT_CMDL_INFO
10243 add_to_showcmd_c(Ctrl_K);
10244#endif
10245 }
10246
10247#ifdef USE_ON_FLY_SCROLL
10248 dont_scroll = TRUE; /* disallow scrolling here */
10249#endif
10250
10251 /* don't map the digraph chars. This also prevents the
10252 * mode message to be deleted when ESC is hit */
10253 ++no_mapping;
10254 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010255 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 --no_mapping;
10257 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010258 if (did_putchar)
10259 /* when the line fits in 'columns' the '?' is at the start of the next
10260 * line and will not be removed by the redraw */
10261 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010262
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 if (IS_SPECIAL(c) || mod_mask) /* special key */
10264 {
10265#ifdef FEAT_CMDL_INFO
10266 clear_showcmd();
10267#endif
10268 insert_special(c, TRUE, FALSE);
10269 return NUL;
10270 }
10271 if (c != ESC)
10272 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010273 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 if (redrawing() && !char_avail())
10275 {
10276 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010277 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278
10279 if (char2cells(c) == 1)
10280 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010281 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010283 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 }
10285#ifdef FEAT_CMDL_INFO
10286 add_to_showcmd_c(c);
10287#endif
10288 }
10289 ++no_mapping;
10290 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010291 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 --no_mapping;
10293 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010294 if (did_putchar)
10295 /* when the line fits in 'columns' the '?' is at the start of the
10296 * next line and will not be removed by a redraw */
10297 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 if (cc != ESC)
10299 {
10300 AppendToRedobuff((char_u *)CTRL_V_STR);
10301 c = getdigraph(c, cc, TRUE);
10302#ifdef FEAT_CMDL_INFO
10303 clear_showcmd();
10304#endif
10305 return c;
10306 }
10307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308#ifdef FEAT_CMDL_INFO
10309 clear_showcmd();
10310#endif
10311 return NUL;
10312}
10313#endif
10314
10315/*
10316 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10317 * Returns the char to be inserted, or NUL if none found.
10318 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010320ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321{
10322 int c;
10323 int temp;
10324 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010325 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326
10327 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10328 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010329 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 return NUL;
10331 }
10332
10333 /* try to advance to the cursor column */
10334 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010335 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 prev_ptr = ptr;
10337 validate_virtcol();
10338 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10339 {
10340 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010341 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 }
10343 if ((colnr_T)temp > curwin->w_virtcol)
10344 ptr = prev_ptr;
10345
10346#ifdef FEAT_MBYTE
10347 c = (*mb_ptr2char)(ptr);
10348#else
10349 c = *ptr;
10350#endif
10351 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010352 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 return c;
10354}
10355
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010356/*
10357 * CTRL-Y or CTRL-E typed in Insert mode.
10358 */
10359 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010360ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010361{
10362 int c = tc;
10363
10364#ifdef FEAT_INS_EXPAND
10365 if (ctrl_x_mode == CTRL_X_SCROLL)
10366 {
10367 if (c == Ctrl_Y)
10368 scrolldown_clamp();
10369 else
10370 scrollup_clamp();
10371 redraw_later(VALID);
10372 }
10373 else
10374#endif
10375 {
10376 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10377 if (c != NUL)
10378 {
10379 long tw_save;
10380
10381 /* The character must be taken literally, insert like it
10382 * was typed after a CTRL-V, and pretend 'textwidth'
10383 * wasn't set. Digits, 'o' and 'x' are special after a
10384 * CTRL-V, don't use it for these. */
10385 if (c < 256 && !isalnum(c))
10386 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10387 tw_save = curbuf->b_p_tw;
10388 curbuf->b_p_tw = -1;
10389 insert_special(c, TRUE, FALSE);
10390 curbuf->b_p_tw = tw_save;
10391#ifdef FEAT_RIGHTLEFT
10392 revins_chars++;
10393 revins_legal++;
10394#endif
10395 c = Ctrl_V; /* pretend CTRL-V is last character */
10396 auto_format(FALSE, TRUE);
10397 }
10398 }
10399 return c;
10400}
10401
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402#ifdef FEAT_SMARTINDENT
10403/*
10404 * Try to do some very smart auto-indenting.
10405 * Used when inserting a "normal" character.
10406 */
10407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010408ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409{
10410 pos_T *pos, old_pos;
10411 char_u *ptr;
10412 int i;
10413 int temp;
10414
10415 /*
10416 * do some very smart indenting when entering '{' or '}'
10417 */
10418 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10419 {
10420 /*
10421 * for '}' set indent equal to indent of line containing matching '{'
10422 */
10423 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10424 {
10425 old_pos = curwin->w_cursor;
10426 /*
10427 * If the matching '{' has a ')' immediately before it (ignoring
10428 * white-space), then line up with the start of the line
10429 * containing the matching '(' if there is one. This handles the
10430 * case where an "if (..\n..) {" statement continues over multiple
10431 * lines -- webb
10432 */
10433 ptr = ml_get(pos->lnum);
10434 i = pos->col;
10435 if (i > 0) /* skip blanks before '{' */
10436 while (--i > 0 && vim_iswhite(ptr[i]))
10437 ;
10438 curwin->w_cursor.lnum = pos->lnum;
10439 curwin->w_cursor.col = i;
10440 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10441 curwin->w_cursor = *pos;
10442 i = get_indent();
10443 curwin->w_cursor = old_pos;
10444#ifdef FEAT_VREPLACE
10445 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010446 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 else
10448#endif
10449 (void)set_indent(i, SIN_CHANGED);
10450 }
10451 else if (curwin->w_cursor.col > 0)
10452 {
10453 /*
10454 * when inserting '{' after "O" reduce indent, but not
10455 * more than indent of previous line
10456 */
10457 temp = TRUE;
10458 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10459 {
10460 old_pos = curwin->w_cursor;
10461 i = get_indent();
10462 while (curwin->w_cursor.lnum > 1)
10463 {
10464 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10465
10466 /* ignore empty lines and lines starting with '#'. */
10467 if (*ptr != '#' && *ptr != NUL)
10468 break;
10469 }
10470 if (get_indent() >= i)
10471 temp = FALSE;
10472 curwin->w_cursor = old_pos;
10473 }
10474 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010475 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 }
10477 }
10478
10479 /*
10480 * set indent of '#' always to 0
10481 */
10482 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10483 {
10484 /* remember current indent for next line */
10485 old_indent = get_indent();
10486 (void)set_indent(0, SIN_CHANGED);
10487 }
10488
10489 /* Adjust ai_col, the char at this position can be deleted. */
10490 if (ai_col > curwin->w_cursor.col)
10491 ai_col = curwin->w_cursor.col;
10492}
10493#endif
10494
10495/*
10496 * Get the value that w_virtcol would have when 'list' is off.
10497 * Unless 'cpo' contains the 'L' flag.
10498 */
10499 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010500get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501{
10502 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10503 return getvcol_nolist(&curwin->w_cursor);
10504 validate_virtcol();
10505 return curwin->w_virtcol;
10506}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010507
10508#ifdef FEAT_AUTOCMD
10509/*
10510 * Handle the InsertCharPre autocommand.
10511 * "c" is the character that was typed.
10512 * Return a pointer to allocated memory with the replacement string.
10513 * Return NULL to continue inserting "c".
10514 */
10515 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010516do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010517{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010518 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010519 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010520
10521 /* Return quickly when there is nothing to do. */
10522 if (!has_insertcharpre())
10523 return NULL;
10524
Bram Moolenaar704984a2012-06-01 14:57:51 +020010525#ifdef FEAT_MBYTE
10526 if (has_mbyte)
10527 buf[(*mb_char2bytes)(c, buf)] = NUL;
10528 else
10529#endif
10530 {
10531 buf[0] = c;
10532 buf[1] = NUL;
10533 }
10534
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010535 /* Lock the text to avoid weird things from happening. */
10536 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010537 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010538
Bram Moolenaar704984a2012-06-01 14:57:51 +020010539 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010540 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010541 {
10542 /* Get the value of v:char. It may be empty or more than one
10543 * character. Only use it when changed, otherwise continue with the
10544 * original character to avoid breaking autoindent. */
10545 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10546 res = vim_strsave(get_vim_var_str(VV_CHAR));
10547 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010548
10549 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10550 --textlock;
10551
10552 return res;
10553}
10554#endif