blob: 47c24f9d2f173ac77f6b072f8425e65c8deff846 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaare4214502015-03-05 18:08:43 +010037#define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
Bram Moolenaare4214502015-03-05 18:08:43 +010040#define CTRL_X_MODE_LINE_OR_EVAL(m) (m == CTRL_X_WHOLE_LINE || m == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42static char *ctrl_x_msgs[] =
43{
44 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000046 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 N_(" Whole line completion (^L^N^P)"),
48 N_(" File name completion (^F^N^P)"),
49 N_(" Tag completion (^]^N^P)"),
50 N_(" Path pattern completion (^N^P)"),
51 N_(" Definition completion (^D^N^P)"),
52 NULL,
53 N_(" Dictionary completion (^K^N^P)"),
54 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000055 N_(" Command-line completion (^V^N^P)"),
56 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000057 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000058 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000059 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010060 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061};
62
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000063static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010064#ifdef FEAT_COMPL_FUNC
65static char e_complwin[] = N_("E839: Completion function changed window");
66static char e_compldel[] = N_("E840: Completion function deleted text");
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
69/*
70 * Structure used to store one match for insert completion.
71 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000072typedef struct compl_S compl_T;
73struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000074{
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 compl_T *cp_next;
76 compl_T *cp_prev;
77 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000078 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000079 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000080 char_u *cp_fname; /* file containing the match, allocated when
81 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000082 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
83 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084};
85
Bram Moolenaar572cb562005-08-05 21:35:02 +000086#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#define FREE_FNAME (2)
88
89/*
90 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000091 * "compl_first_match" points to the start of the list.
92 * "compl_curr_match" points to the currently selected entry.
93 * "compl_shown_match" is different from compl_curr_match during
94 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000096static compl_T *compl_first_match = NULL;
97static compl_T *compl_curr_match = NULL;
98static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000100/* After using a cursor key <Enter> selects a match in the popup menu,
101 * otherwise it inserts a line break. */
102static int compl_enter_selects = FALSE;
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104/* When "compl_leader" is not NULL only matches that start with this string
105 * are used. */
106static char_u *compl_leader = NULL;
107
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000108static int compl_get_longest = FALSE; /* put longest common string
109 in compl_leader */
110
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200111static int compl_no_insert = FALSE; /* FALSE: select & insert
112 TRUE: noinsert */
113static int compl_no_select = FALSE; /* FALSE: select & insert
114 TRUE: noselect */
115
Bram Moolenaara6557602006-02-04 22:43:20 +0000116static int compl_used_match; /* Selected one of the matches. When
117 FALSE the match was edited or using
118 the longest common string. */
119
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000120static int compl_was_interrupted = FALSE; /* didn't finish finding
121 completions. */
122
123static int compl_restarting = FALSE; /* don't insert match */
124
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000125/* When the first completion is done "compl_started" is set. When it's
126 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000127static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000129/* Set when doing something for completion that may call edit() recursively,
130 * which is not allowed. */
131static int compl_busy = FALSE;
132
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static int compl_matches = 0;
134static char_u *compl_pattern = NULL;
135static int compl_direction = FORWARD;
136static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000137static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000138static pos_T compl_startpos;
139static colnr_T compl_col = 0; /* column where the text starts
140 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000141static char_u *compl_orig_text = NULL; /* text as it was before
142 * completion started */
143static int compl_cont_mode = 0;
144static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar82139082011-09-14 16:52:09 +0200146static int compl_opt_refresh_always = FALSE;
147
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100148static void ins_ctrl_x(void);
149static int has_compl_option(int dict_opt);
150static int ins_compl_accept_char(int c);
151static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
152static int ins_compl_equal(compl_T *match, char_u *str, int len);
153static void ins_compl_longest_match(compl_T *match);
154static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
155static int ins_compl_make_cyclic(void);
156static void ins_compl_upd_pum(void);
157static void ins_compl_del_pum(void);
158static int pum_wanted(void);
159static int pum_enough_matches(void);
160static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
161static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
162static char_u *find_line_end(char_u *ptr);
163static void ins_compl_free(void);
164static void ins_compl_clear(void);
165static int ins_compl_bs(void);
166static int ins_compl_need_restart(void);
167static void ins_compl_new_leader(void);
168static void ins_compl_addleader(int c);
169static int ins_compl_len(void);
170static void ins_compl_restart(void);
171static void ins_compl_set_original_text(char_u *str);
172static void ins_compl_addfrommatch(void);
173static int ins_compl_prep(int c);
174static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
175static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000176#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100177static void ins_compl_add_list(list_T *list);
178static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000179#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static int ins_compl_get_exp(pos_T *ini);
181static void ins_compl_delete(void);
182static void ins_compl_insert(void);
183static int ins_compl_next(int allow_get_expansion, int count, int insert_match);
184static int ins_compl_key2dir(int c);
185static int ins_compl_pum_key(int c);
186static int ins_compl_key2count(int c);
187static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100188static int ins_complete(int c, int enable_pum);
189static void show_pum(int save_w_wrow);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#endif /* FEAT_INS_EXPAND */
192
193#define BACKSPACE_CHAR 1
194#define BACKSPACE_WORD 2
195#define BACKSPACE_WORD_NOT_SPACE 3
196#define BACKSPACE_LINE 4
197
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100198static void ins_redraw(int ready);
199static void ins_ctrl_v(void);
200static void undisplay_dollar(void);
201static void insert_special(int, int, int);
202static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
203static void check_auto_format(int);
204static void redo_literal(int c);
205static void start_arrow(pos_T *end_insert_pos);
206static void start_arrow_with_change(pos_T *end_insert_pos, int change);
207static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000208#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void check_spell_redraw(void);
210static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000211static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
214static int echeck_abbr(int);
215static int replace_pop(void);
216static void replace_join(int off);
217static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void replace_flush(void);
222static void replace_do_bs(int limit_col);
223static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ins_reg(void);
228static void ins_ctrl_g(void);
229static void ins_ctrl_hat(void);
230static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100234static int ins_start_select(int c);
235static void ins_insert(int replaceState);
236static void ins_ctrl_o(void);
237static void ins_shift(int c, int lastc);
238static void ins_del(void);
239static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_mouse(int c);
242static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000244#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000246#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ins_left(int end_change);
248static void ins_home(int c);
249static void ins_end(int c);
250static void ins_s_left(void);
251static void ins_right(int end_change);
252static void ins_s_right(void);
253static void ins_up(int startcol);
254static void ins_pageup(void);
255static void ins_down(int startcol);
256static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static int ins_tab(void);
261static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100270#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274static colnr_T Insstart_textlen; /* length of line when insert started */
275static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100276static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
278static char_u *last_insert = NULL; /* the text of the previous insert,
279 K_SPECIAL and CSI are escaped */
280static int last_insert_skip; /* nr of chars in front of previous insert */
281static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000282static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284#ifdef FEAT_CINDENT
285static int can_cindent; /* may do cindenting on this line */
286#endif
287
288static int old_indent = 0; /* for ^^D command in insert mode */
289
290#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000291static int revins_on; /* reverse insert mode on */
292static int revins_chars; /* how much to skip after edit */
293static int revins_legal; /* was the last char 'legal'? */
294static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static int ins_need_undo; /* call u_save() before inserting a
298 char. Set when edit() is called.
299 after that arrow_used is used. */
300
301static int did_add_space = FALSE; /* auto_format() added an extra space
302 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200303static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
304 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
306/*
307 * edit(): Start inserting text.
308 *
309 * "cmdchar" can be:
310 * 'i' normal insert command
311 * 'a' normal append command
312 * 'R' replace command
313 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
314 * but still only one <CR> is inserted. The <Esc> is not used for redo.
315 * 'g' "gI" command.
316 * 'V' "gR" command for Virtual Replace mode.
317 * 'v' "gr" command for single character Virtual Replace mode.
318 *
319 * This function is not called recursively. For CTRL-O commands, it returns
320 * and lets the caller handle the Normal-mode command.
321 *
322 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
323 */
324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100325edit(
326 int cmdchar,
327 int startln, /* if set, insert at start of line */
328 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329{
330 int c = 0;
331 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100332 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000333 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 int i;
336 int did_backspace = TRUE; /* previous char was backspace */
337#ifdef FEAT_CINDENT
338 int line_is_white = FALSE; /* line is empty before insert */
339#endif
340 linenr_T old_topline = 0; /* topline before insertion */
341#ifdef FEAT_DIFF
342 int old_topfill = -1;
343#endif
344 int inserted_space = FALSE; /* just inserted a space */
345 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000346 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000348 /* Remember whether editing was restarted after CTRL-O. */
349 did_restart_edit = restart_edit;
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 /* sleep before redrawing, needed for "CTRL-O :" that results in an
352 * error message */
353 check_for_delay(TRUE);
354
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100355 /* set Insstart_orig to Insstart */
356 update_Insstart_orig = TRUE;
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifdef HAVE_SANDBOX
359 /* Don't allow inserting in the sandbox. */
360 if (sandbox != 0)
361 {
362 EMSG(_(e_sandbox));
363 return FALSE;
364 }
365#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000366 /* Don't allow changes in the buffer while editing the cmdline. The
367 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000368 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000369 {
370 EMSG(_(e_secure));
371 return FALSE;
372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
374#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000375 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000376 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000377 {
378 EMSG(_(e_secure));
379 return FALSE;
380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 ins_compl_clear(); /* clear stuff for CTRL-X mode */
382#endif
383
Bram Moolenaar843ee412004-06-30 16:16:41 +0000384#ifdef FEAT_AUTOCMD
385 /*
386 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
387 */
388 if (cmdchar != 'r' && cmdchar != 'v')
389 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100390 pos_T save_cursor = curwin->w_cursor;
391
Bram Moolenaar1e015462005-09-25 22:16:38 +0000392# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000393 if (cmdchar == 'R')
394 ptr = (char_u *)"r";
395 else if (cmdchar == 'V')
396 ptr = (char_u *)"v";
397 else
398 ptr = (char_u *)"i";
399 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200400 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000401# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000402 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100403
Bram Moolenaar097c9922013-05-19 21:15:15 +0200404 /* Make sure the cursor didn't move. Do call check_cursor_col() in
405 * case the text was modified. Since Insert mode was not started yet
406 * a call to check_cursor_col() may move the cursor, especially with
407 * the "A" command, thus set State to avoid that. Also check that the
408 * line number is still valid (lines may have been deleted).
409 * Do not restore if v:char was set to a non-empty string. */
410 if (!equalpos(curwin->w_cursor, save_cursor)
411# ifdef FEAT_EVAL
412 && *get_vim_var_str(VV_CHAR) == NUL
413# endif
414 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100415 {
416 int save_state = State;
417
418 curwin->w_cursor = save_cursor;
419 State = INSERT;
420 check_cursor_col();
421 State = save_state;
422 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000423 }
424#endif
425
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200426#ifdef FEAT_CONCEAL
427 /* Check if the cursor line needs redrawing before changing State. If
428 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200429 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200430#endif
431
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#ifdef FEAT_MOUSE
433 /*
434 * When doing a paste with the middle mouse button, Insstart is set to
435 * where the paste started.
436 */
437 if (where_paste_started.lnum != 0)
438 Insstart = where_paste_started;
439 else
440#endif
441 {
442 Insstart = curwin->w_cursor;
443 if (startln)
444 Insstart.col = 0;
445 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000446 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 Insstart_blank_vcol = MAXCOL;
448 if (!did_ai)
449 ai_col = 0;
450
451 if (cmdchar != NUL && restart_edit == 0)
452 {
453 ResetRedobuff();
454 AppendNumberToRedobuff(count);
455#ifdef FEAT_VREPLACE
456 if (cmdchar == 'V' || cmdchar == 'v')
457 {
458 /* "gR" or "gr" command */
459 AppendCharToRedobuff('g');
460 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
461 }
462 else
463#endif
464 {
465 AppendCharToRedobuff(cmdchar);
466 if (cmdchar == 'g') /* "gI" command */
467 AppendCharToRedobuff('I');
468 else if (cmdchar == 'r') /* "r<CR>" command */
469 count = 1; /* insert only one <CR> */
470 }
471 }
472
473 if (cmdchar == 'R')
474 {
475#ifdef FEAT_FKMAP
476 if (p_fkmap && p_ri)
477 {
478 beep_flush();
479 EMSG(farsi_text_3); /* encoded in Farsi */
480 State = INSERT;
481 }
482 else
483#endif
484 State = REPLACE;
485 }
486#ifdef FEAT_VREPLACE
487 else if (cmdchar == 'V' || cmdchar == 'v')
488 {
489 State = VREPLACE;
490 replaceState = VREPLACE;
491 orig_line_count = curbuf->b_ml.ml_line_count;
492 vr_lines_changed = 1;
493 }
494#endif
495 else
496 State = INSERT;
497
498 stop_insert_mode = FALSE;
499
500 /*
501 * Need to recompute the cursor position, it might move when the cursor is
502 * on a TAB or special character.
503 */
504 curs_columns(TRUE);
505
506 /*
507 * Enable langmap or IME, indicated by 'iminsert'.
508 * Note that IME may enabled/disabled without us noticing here, thus the
509 * 'iminsert' value may not reflect what is actually used. It is updated
510 * when hitting <Esc>.
511 */
512 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
513 State |= LANGMAP;
514#ifdef USE_IM_CONTROL
515 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
516#endif
517
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518#ifdef FEAT_MOUSE
519 setmouse();
520#endif
521#ifdef FEAT_CMDL_INFO
522 clear_showcmd();
523#endif
524#ifdef FEAT_RIGHTLEFT
525 /* there is no reverse replace mode */
526 revins_on = (State == INSERT && p_ri);
527 if (revins_on)
528 undisplay_dollar();
529 revins_chars = 0;
530 revins_legal = 0;
531 revins_scol = -1;
532#endif
533
534 /*
535 * Handle restarting Insert mode.
536 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
537 * restart_edit non-zero, and something in the stuff buffer.
538 */
539 if (restart_edit != 0 && stuff_empty())
540 {
541#ifdef FEAT_MOUSE
542 /*
543 * After a paste we consider text typed to be part of the insert for
544 * the pasted text. You can backspace over the pasted text too.
545 */
546 if (where_paste_started.lnum)
547 arrow_used = FALSE;
548 else
549#endif
550 arrow_used = TRUE;
551 restart_edit = 0;
552
553 /*
554 * If the cursor was after the end-of-line before the CTRL-O and it is
555 * now at the end-of-line, put it after the end-of-line (this is not
556 * correct in very rare cases).
557 * Also do this if curswant is greater than the current virtual
558 * column. Eg after "^O$" or "^O80|".
559 */
560 validate_virtcol();
561 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000562 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 || curwin->w_curswant > curwin->w_virtcol)
564 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
565 {
566 if (ptr[1] == NUL)
567 ++curwin->w_cursor.col;
568#ifdef FEAT_MBYTE
569 else if (has_mbyte)
570 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000571 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (ptr[i] == NUL)
573 curwin->w_cursor.col += i;
574 }
575#endif
576 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000577 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
579 else
580 arrow_used = FALSE;
581
582 /* we are in insert mode now, don't need to start it anymore */
583 need_start_insertmode = FALSE;
584
585 /* Need to save the line for undo before inserting the first char. */
586 ins_need_undo = TRUE;
587
588#ifdef FEAT_MOUSE
589 where_paste_started.lnum = 0;
590#endif
591#ifdef FEAT_CINDENT
592 can_cindent = TRUE;
593#endif
594#ifdef FEAT_FOLDING
595 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
596 * restarting. */
597 if (!p_im && did_restart_edit == 0)
598 foldOpenCursor();
599#endif
600
601 /*
602 * If 'showmode' is set, show the current (insert/replace/..) mode.
603 * A warning message for changing a readonly file is given here, before
604 * actually changing anything. It's put after the mode, if any.
605 */
606 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000607 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 i = showmode();
609
610 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000611 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613#ifdef CURSOR_SHAPE
614 ui_cursor_shape(); /* may show different cursor shape */
615#endif
616#ifdef FEAT_DIGRAPHS
617 do_digraph(-1); /* clear digraphs */
618#endif
619
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000620 /*
621 * Get the current length of the redo buffer, those characters have to be
622 * skipped if we want to get to the inserted characters.
623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 ptr = get_inserted();
625 if (ptr == NULL)
626 new_insert_skip = 0;
627 else
628 {
629 new_insert_skip = (int)STRLEN(ptr);
630 vim_free(ptr);
631 }
632
633 old_indent = 0;
634
635 /*
636 * Main loop in Insert mode: repeat until Insert mode is left.
637 */
638 for (;;)
639 {
640#ifdef FEAT_RIGHTLEFT
641 if (!revins_legal)
642 revins_scol = -1; /* reset on illegal motions */
643 else
644 revins_legal = 0;
645#endif
646 if (arrow_used) /* don't repeat insert when arrow key used */
647 count = 0;
648
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100649 if (update_Insstart_orig)
650 Insstart_orig = Insstart;
651
Bram Moolenaar00672e12016-06-26 18:38:13 +0200652 if (stop_insert_mode
653#ifdef FEAT_INS_EXPAND
654 && !pum_visible()
655#endif
656 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 {
658 /* ":stopinsert" used or 'insertmode' reset */
659 count = 0;
660 goto doESCkey;
661 }
662
663 /* set curwin->w_curswant for next K_DOWN or K_UP */
664 if (!arrow_used)
665 curwin->w_set_curswant = TRUE;
666
667 /* If there is no typeahead may check for timestamps (e.g., for when a
668 * menu invoked a shell command). */
669 if (stuff_empty())
670 {
671 did_check_timestamps = FALSE;
672 if (need_check_timestamps)
673 check_timestamps(FALSE);
674 }
675
676 /*
677 * When emsg() was called msg_scroll will have been set.
678 */
679 msg_scroll = FALSE;
680
681#ifdef FEAT_GUI
682 /* When 'mousefocus' is set a mouse movement may have taken us to
683 * another window. "need_mouse_correct" may then be set because of an
684 * autocommand. */
685 if (need_mouse_correct)
686 gui_mouse_correct();
687#endif
688
689#ifdef FEAT_FOLDING
690 /* Open fold at the cursor line, according to 'foldopen'. */
691 if (fdo_flags & FDO_INSERT)
692 foldOpenCursor();
693 /* Close folds where the cursor isn't, according to 'foldclose' */
694 if (!char_avail())
695 foldCheckClose();
696#endif
697
698 /*
699 * If we inserted a character at the last position of the last line in
700 * the window, scroll the window one line up. This avoids an extra
701 * redraw.
702 * This is detected when the cursor column is smaller after inserting
703 * something.
704 * Don't do this when the topline changed already, it has
705 * already been adjusted (by insertchar() calling open_line())).
706 */
707 if (curbuf->b_mod_set
708 && curwin->w_p_wrap
709 && !did_backspace
710 && curwin->w_topline == old_topline
711#ifdef FEAT_DIFF
712 && curwin->w_topfill == old_topfill
713#endif
714 )
715 {
716 mincol = curwin->w_wcol;
717 validate_cursor_col();
718
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000719 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 && curwin->w_wrow == W_WINROW(curwin)
721 + curwin->w_height - 1 - p_so
722 && (curwin->w_cursor.lnum != curwin->w_topline
723#ifdef FEAT_DIFF
724 || curwin->w_topfill > 0
725#endif
726 ))
727 {
728#ifdef FEAT_DIFF
729 if (curwin->w_topfill > 0)
730 --curwin->w_topfill;
731 else
732#endif
733#ifdef FEAT_FOLDING
734 if (hasFolding(curwin->w_topline, NULL, &old_topline))
735 set_topline(curwin, old_topline + 1);
736 else
737#endif
738 set_topline(curwin, curwin->w_topline + 1);
739 }
740 }
741
742 /* May need to adjust w_topline to show the cursor. */
743 update_topline();
744
745 did_backspace = FALSE;
746
747 validate_cursor(); /* may set must_redraw */
748
749 /*
750 * Redraw the display when no characters are waiting.
751 * Also shows mode, ruler and positions cursor.
752 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000753 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755#ifdef FEAT_SCROLLBIND
756 if (curwin->w_p_scb)
757 do_check_scrollbind(TRUE);
758#endif
759
Bram Moolenaar860cae12010-06-05 23:22:07 +0200760#ifdef FEAT_CURSORBIND
761 if (curwin->w_p_crb)
762 do_check_cursorbind();
763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 update_curswant();
765 old_topline = curwin->w_topline;
766#ifdef FEAT_DIFF
767 old_topfill = curwin->w_topfill;
768#endif
769
770#ifdef USE_ON_FLY_SCROLL
771 dont_scroll = FALSE; /* allow scrolling here */
772#endif
773
774 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000775 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100777 if (c != K_CURSORHOLD)
778 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200779
780 /* After using CTRL-G U the next cursor key will not break undo. */
781 if (dont_sync_undo == MAYBE)
782 dont_sync_undo = TRUE;
783 else
784 dont_sync_undo = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000785 do
786 {
787 c = safe_vgetc();
788 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000790#ifdef FEAT_AUTOCMD
791 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
792 did_cursorhold = TRUE;
793#endif
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795#ifdef FEAT_RIGHTLEFT
796 if (p_hkmap && KeyTyped)
797 c = hkmap(c); /* Hebrew mode mapping */
798#endif
799#ifdef FEAT_FKMAP
800 if (p_fkmap && KeyTyped)
801 c = fkmap(c); /* Farsi mode mapping */
802#endif
803
804#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000805 /*
806 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000807 * and the cursor is still in the completed word. Only when there is
808 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000809 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000810 if (compl_started
811 && pum_wanted()
812 && curwin->w_cursor.col >= compl_col
813 && (compl_shown_match == NULL
814 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000815 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000816 /* BS: Delete one character from "compl_leader". */
817 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000818 && curwin->w_cursor.col > compl_col
819 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000820 continue;
821
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000822 /* When no match was selected or it was edited. */
823 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000824 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000825 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000826 * "compl_leader". Except when at the original match and
827 * there is nothing to add, CTRL-L works like CTRL-P then. */
828 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100829 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000830 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000831 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000832 {
833 ins_compl_addfrommatch();
834 continue;
835 }
836
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000837 /* A non-white character that fits in with the current
838 * completion: Add to "compl_leader". */
839 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000840 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100841#ifdef FEAT_AUTOCMD
842 /* Trigger InsertCharPre. */
843 char_u *str = do_insert_char_pre(c);
844 char_u *p;
845
846 if (str != NULL)
847 {
848 for (p = str; *p != NUL; mb_ptr_adv(p))
849 ins_compl_addleader(PTR2CHAR(p));
850 vim_free(str);
851 }
852 else
853#endif
854 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000855 continue;
856 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000857
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000858 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000859 * compl_enter_selects is set the Enter key does the same. */
860 if (c == Ctrl_Y || (compl_enter_selects
861 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000862 {
863 ins_compl_delete();
864 ins_compl_insert();
865 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000866 }
867 }
868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
870 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000871 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000872 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000873 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874#endif
875
Bram Moolenaar488c6512005-08-11 20:09:58 +0000876 /* CTRL-\ CTRL-N goes to Normal mode,
877 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
878 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (c == Ctrl_BSL)
880 {
881 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000882 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 ++no_mapping;
884 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000885 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 --no_mapping;
887 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000888 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000890 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 vungetc(c);
892 c = Ctrl_BSL;
893 }
894 else if (c == Ctrl_G && p_im)
895 continue;
896 else
897 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000898 if (c == Ctrl_O)
899 {
900 ins_ctrl_o();
901 ins_at_eol = FALSE; /* cursor keeps its column */
902 nomove = TRUE;
903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 count = 0;
905 goto doESCkey;
906 }
907 }
908
909#ifdef FEAT_DIGRAPHS
910 c = do_digraph(c);
911#endif
912
913#ifdef FEAT_INS_EXPAND
914 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
915 goto docomplete;
916#endif
917 if (c == Ctrl_V || c == Ctrl_Q)
918 {
919 ins_ctrl_v();
920 c = Ctrl_V; /* pretend CTRL-V is last typed character */
921 continue;
922 }
923
924#ifdef FEAT_CINDENT
925 if (cindent_on()
926# ifdef FEAT_INS_EXPAND
927 && ctrl_x_mode == 0
928# endif
929 )
930 {
931 /* A key name preceded by a bang means this key is not to be
932 * inserted. Skip ahead to the re-indenting below.
933 * A key name preceded by a star means that indenting has to be
934 * done before inserting the key. */
935 line_is_white = inindent(0);
936 if (in_cinkeys(c, '!', line_is_white))
937 goto force_cindent;
938 if (can_cindent && in_cinkeys(c, '*', line_is_white)
939 && stop_arrow() == OK)
940 do_c_expr_indent();
941 }
942#endif
943
944#ifdef FEAT_RIGHTLEFT
945 if (curwin->w_p_rl)
946 switch (c)
947 {
948 case K_LEFT: c = K_RIGHT; break;
949 case K_S_LEFT: c = K_S_RIGHT; break;
950 case K_C_LEFT: c = K_C_RIGHT; break;
951 case K_RIGHT: c = K_LEFT; break;
952 case K_S_RIGHT: c = K_S_LEFT; break;
953 case K_C_RIGHT: c = K_C_LEFT; break;
954 }
955#endif
956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 /*
958 * If 'keymodel' contains "startsel", may start selection. If it
959 * does, a CTRL-O and c will be stuffed, we need to get these
960 * characters.
961 */
962 if (ins_start_select(c))
963 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
965 /*
966 * The big switch to handle a character in insert mode.
967 */
968 switch (c)
969 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000970 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (echeck_abbr(ESC + ABBR_OFF))
972 break;
973 /*FALLTHROUGH*/
974
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000975 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976#ifdef FEAT_CMDWIN
977 if (c == Ctrl_C && cmdwin_type != 0)
978 {
979 /* Close the cmdline window. */
980 cmdwin_result = K_IGNORE;
981 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000982 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 goto doESCkey;
984 }
985#endif
986
987#ifdef UNIX
988do_intr:
989#endif
990 /* when 'insertmode' set, and not halfway a mapping, don't leave
991 * Insert mode */
992 if (goto_im())
993 {
994 if (got_int)
995 {
996 (void)vgetc(); /* flush all buffers */
997 got_int = FALSE;
998 }
999 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001000 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 break;
1002 }
1003doESCkey:
1004 /*
1005 * This is the ONLY return from edit()!
1006 */
1007 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1008 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001009 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 o_lnum = curwin->w_cursor.lnum;
1011
Bram Moolenaar488c6512005-08-11 20:09:58 +00001012 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001013 {
1014#ifdef FEAT_AUTOCMD
1015 if (cmdchar != 'r' && cmdchar != 'v')
1016 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1017 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001018 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 continue;
1023
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001024 case Ctrl_Z: /* suspend when 'insertmode' set */
1025 if (!p_im)
1026 goto normalchar; /* insert CTRL-Z as normal char */
1027 stuffReadbuff((char_u *)":st\r");
1028 c = Ctrl_O;
1029 /*FALLTHROUGH*/
1030
1031 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001032#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001033 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001034 goto docomplete;
1035#endif
1036 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1037 break;
1038 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001039
1040#ifdef FEAT_VIRTUALEDIT
1041 /* don't move the cursor left when 'virtualedit' has "onemore". */
1042 if (ve_flags & VE_ONEMORE)
1043 {
1044 ins_at_eol = FALSE;
1045 nomove = TRUE;
1046 }
1047#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001048 count = 0;
1049 goto doESCkey;
1050
Bram Moolenaar572cb562005-08-05 21:35:02 +00001051 case K_INS: /* toggle insert/replace mode */
1052 case K_KINS:
1053 ins_insert(replaceState);
1054 break;
1055
1056 case K_SELECT: /* end of Select mode mapping - ignore */
1057 break;
1058
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001059 case K_HELP: /* Help key works like <ESC> <Help> */
1060 case K_F1:
1061 case K_XF1:
1062 stuffcharReadbuff(K_HELP);
1063 if (p_im)
1064 need_start_insertmode = TRUE;
1065 goto doESCkey;
1066
1067#ifdef FEAT_NETBEANS_INTG
1068 case K_F21: /* NetBeans command */
1069 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001070 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 --no_mapping;
1072 netbeans_keycommand(i);
1073 break;
1074#endif
1075
1076 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 case NUL:
1078 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001079 /* For ^@ the trailing ESC will end the insert, unless there is an
1080 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1082 && c != Ctrl_A && !p_im)
1083 goto doESCkey; /* quit insert mode */
1084 inserted_space = FALSE;
1085 break;
1086
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001087 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 ins_reg();
1089 auto_format(FALSE, TRUE);
1090 inserted_space = FALSE;
1091 break;
1092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001093 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 ins_ctrl_g();
1095 break;
1096
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001097 case Ctrl_HAT: /* switch input mode and/or langmap */
1098 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 break;
1100
1101#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (!p_ari)
1104 goto normalchar;
1105 ins_ctrl_();
1106 break;
1107#endif
1108
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1111 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1112 goto docomplete;
1113#endif
1114 /* FALLTHROUGH */
1115
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001116 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117# ifdef FEAT_INS_EXPAND
1118 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1119 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001120 if (has_compl_option(FALSE))
1121 goto docomplete;
1122 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 }
1124# endif
1125 ins_shift(c, lastc);
1126 auto_format(FALSE, TRUE);
1127 inserted_space = FALSE;
1128 break;
1129
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001130 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 case K_KDEL:
1132 ins_del();
1133 auto_format(FALSE, TRUE);
1134 break;
1135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 case Ctrl_H:
1138 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1139 auto_format(FALSE, TRUE);
1140 break;
1141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1144 auto_format(FALSE, TRUE);
1145 break;
1146
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001147 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001148# ifdef FEAT_COMPL_FUNC
1149 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001150 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001151 goto docomplete;
1152# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1154 auto_format(FALSE, TRUE);
1155 inserted_space = FALSE;
1156 break;
1157
1158#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001159 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 case K_LEFTMOUSE_NM:
1161 case K_LEFTDRAG:
1162 case K_LEFTRELEASE:
1163 case K_LEFTRELEASE_NM:
1164 case K_MIDDLEMOUSE:
1165 case K_MIDDLEDRAG:
1166 case K_MIDDLERELEASE:
1167 case K_RIGHTMOUSE:
1168 case K_RIGHTDRAG:
1169 case K_RIGHTRELEASE:
1170 case K_X1MOUSE:
1171 case K_X1DRAG:
1172 case K_X1RELEASE:
1173 case K_X2MOUSE:
1174 case K_X2DRAG:
1175 case K_X2RELEASE:
1176 ins_mouse(c);
1177 break;
1178
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001180 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 break;
1182
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001183 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001184 ins_mousescroll(MSCR_UP);
1185 break;
1186
1187 case K_MOUSELEFT: /* Scroll wheel left */
1188 ins_mousescroll(MSCR_LEFT);
1189 break;
1190
1191 case K_MOUSERIGHT: /* Scroll wheel right */
1192 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 break;
1194#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001195#ifdef FEAT_GUI_TABLINE
1196 case K_TABLINE:
1197 case K_TABMENU:
1198 ins_tabline(c);
1199 break;
1200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001202 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 break;
1204
Bram Moolenaar754b5602006-02-09 23:53:20 +00001205#ifdef FEAT_AUTOCMD
1206 case K_CURSORHOLD: /* Didn't type something for a while. */
1207 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1208 did_cursorhold = TRUE;
1209 break;
1210#endif
1211
Bram Moolenaar4770d092006-01-12 23:22:24 +00001212#ifdef FEAT_GUI_W32
1213 /* On Win32 ignore <M-F4>, we get it when closing the window was
1214 * cancelled. */
1215 case K_F4:
1216 if (mod_mask != MOD_MASK_ALT)
1217 goto normalchar;
1218 break;
1219#endif
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#ifdef FEAT_GUI
1222 case K_VER_SCROLLBAR:
1223 ins_scroll();
1224 break;
1225
1226 case K_HOR_SCROLLBAR:
1227 ins_horscroll();
1228 break;
1229#endif
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 case K_S_HOME:
1234 case K_C_HOME:
1235 ins_home(c);
1236 break;
1237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001238 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 case K_S_END:
1241 case K_C_END:
1242 ins_end(c);
1243 break;
1244
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001245 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001246 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1247 ins_s_left();
1248 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001249 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 break;
1251
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001252 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 case K_C_LEFT:
1254 ins_s_left();
1255 break;
1256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001257 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001258 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1259 ins_s_right();
1260 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001261 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 break;
1263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001264 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 case K_C_RIGHT:
1266 ins_s_right();
1267 break;
1268
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001269 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001270#ifdef FEAT_INS_EXPAND
1271 if (pum_visible())
1272 goto docomplete;
1273#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001274 if (mod_mask & MOD_MASK_SHIFT)
1275 ins_pageup();
1276 else
1277 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 break;
1279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001280 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 case K_PAGEUP:
1282 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001283#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001284 if (pum_visible())
1285 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 ins_pageup();
1288 break;
1289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001290 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001291#ifdef FEAT_INS_EXPAND
1292 if (pum_visible())
1293 goto docomplete;
1294#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001295 if (mod_mask & MOD_MASK_SHIFT)
1296 ins_pagedown();
1297 else
1298 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 break;
1300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001301 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 case K_PAGEDOWN:
1303 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001304#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001305 if (pum_visible())
1306 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 ins_pagedown();
1309 break;
1310
1311#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001312 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 ins_drop();
1314 break;
1315#endif
1316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001317 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 c = TAB;
1319 /* FALLTHROUGH */
1320
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001321 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1323 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1324 goto docomplete;
1325#endif
1326 inserted_space = FALSE;
1327 if (ins_tab())
1328 goto normalchar; /* insert TAB as a normal char */
1329 auto_format(FALSE, TRUE);
1330 break;
1331
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001332 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 c = CAR;
1334 /* FALLTHROUGH */
1335 case CAR:
1336 case NL:
1337#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1338 /* In a quickfix window a <CR> jumps to the error under the
1339 * cursor. */
1340 if (bt_quickfix(curbuf) && c == CAR)
1341 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001342 if (curwin->w_llist_ref == NULL) /* quickfix window */
1343 do_cmdline_cmd((char_u *)".cc");
1344 else /* location list window */
1345 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 break;
1347 }
1348#endif
1349#ifdef FEAT_CMDWIN
1350 if (cmdwin_type != 0)
1351 {
1352 /* Execute the command in the cmdline window. */
1353 cmdwin_result = CAR;
1354 goto doESCkey;
1355 }
1356#endif
1357 if (ins_eol(c) && !p_im)
1358 goto doESCkey; /* out of memory */
1359 auto_format(FALSE, FALSE);
1360 inserted_space = FALSE;
1361 break;
1362
Bram Moolenaar860cae12010-06-05 23:22:07 +02001363#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001364 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365# ifdef FEAT_INS_EXPAND
1366 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1367 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001368 if (has_compl_option(TRUE))
1369 goto docomplete;
1370 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 }
1372# endif
1373# ifdef FEAT_DIGRAPHS
1374 c = ins_digraph();
1375 if (c == NUL)
1376 break;
1377# endif
1378 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
1381#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001382 case Ctrl_X: /* Enter CTRL-X mode */
1383 ins_ctrl_x();
1384 break;
1385
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001386 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (ctrl_x_mode != CTRL_X_TAGS)
1388 goto normalchar;
1389 goto docomplete;
1390
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001391 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 if (ctrl_x_mode != CTRL_X_FILES)
1393 goto normalchar;
1394 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001395
1396 case 's': /* Spelling completion after ^X */
1397 case Ctrl_S:
1398 if (ctrl_x_mode != CTRL_X_SPELL)
1399 goto normalchar;
1400 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401#endif
1402
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001403 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifdef FEAT_INS_EXPAND
1405 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1406#endif
1407 {
1408 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1409 if (p_im)
1410 {
1411 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1412 break;
1413 goto doESCkey;
1414 }
1415 goto normalchar;
1416 }
1417#ifdef FEAT_INS_EXPAND
1418 /* FALLTHROUGH */
1419
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001420 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 case Ctrl_N:
1422 /* if 'complete' is empty then plain ^P is no longer special,
1423 * but it is under other ^X modes */
1424 if (*curbuf->b_p_cpt == NUL
1425 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001426 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 goto normalchar;
1428
1429docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001430 compl_busy = TRUE;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001431 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001432 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001433 compl_cont_status = 0;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001434 disable_fold_update--;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001435 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 break;
1437#endif /* FEAT_INS_EXPAND */
1438
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001439 case Ctrl_Y: /* copy from previous line or scroll down */
1440 case Ctrl_E: /* copy from next line or scroll up */
1441 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 break;
1443
1444 default:
1445#ifdef UNIX
1446 if (c == intr_char) /* special interrupt char */
1447 goto do_intr;
1448#endif
1449
Bram Moolenaare659c952011-05-19 17:25:41 +02001450normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001452 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001454#ifdef FEAT_AUTOCMD
1455 if (!p_paste)
1456 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001457 /* Trigger InsertCharPre. */
1458 char_u *str = do_insert_char_pre(c);
1459 char_u *p;
1460
1461 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001462 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001463 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001464 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001465 /* Insert the new value of v:char literally. */
1466 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001467 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001468 c = PTR2CHAR(p);
1469 if (c == CAR || c == K_KENTER || c == NL)
1470 ins_eol(c);
1471 else
1472 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001473 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001474 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001475 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001476 vim_free(str);
1477 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001478 }
1479
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001480 /* If the new value is already inserted or an empty string
1481 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001482 if (c == NUL)
1483 break;
1484 }
1485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486#ifdef FEAT_SMARTINDENT
1487 /* Try to perform smart-indenting. */
1488 ins_try_si(c);
1489#endif
1490
1491 if (c == ' ')
1492 {
1493 inserted_space = TRUE;
1494#ifdef FEAT_CINDENT
1495 if (inindent(0))
1496 can_cindent = FALSE;
1497#endif
1498 if (Insstart_blank_vcol == MAXCOL
1499 && curwin->w_cursor.lnum == Insstart.lnum)
1500 Insstart_blank_vcol = get_nolist_virtcol();
1501 }
1502
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001503 /* Insert a normal character and check for abbreviations on a
1504 * special character. Let CTRL-] expand abbreviations without
1505 * inserting it. */
1506 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507#ifdef FEAT_MBYTE
1508 /* Add ABBR_OFF for characters above 0x100, this is
1509 * what check_abbr() expects. */
1510 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1511#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001512 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
1514 insert_special(c, FALSE, FALSE);
1515#ifdef FEAT_RIGHTLEFT
1516 revins_legal++;
1517 revins_chars++;
1518#endif
1519 }
1520
1521 auto_format(FALSE, TRUE);
1522
1523#ifdef FEAT_FOLDING
1524 /* When inserting a character the cursor line must never be in a
1525 * closed fold. */
1526 foldOpenCursor();
1527#endif
1528 break;
1529 } /* end of switch (c) */
1530
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001531#ifdef FEAT_AUTOCMD
1532 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001533 if (c != K_CURSORHOLD
1534# ifdef FEAT_COMPL_FUNC
1535 /* but not in CTRL-X mode, a script can't restore the state */
1536 && ctrl_x_mode == 0
1537# endif
1538 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001539 did_cursorhold = FALSE;
1540#endif
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 /* If the cursor was moved we didn't just insert a space */
1543 if (arrow_used)
1544 inserted_space = FALSE;
1545
1546#ifdef FEAT_CINDENT
1547 if (can_cindent && cindent_on()
1548# ifdef FEAT_INS_EXPAND
1549 && ctrl_x_mode == 0
1550# endif
1551 )
1552 {
1553force_cindent:
1554 /*
1555 * Indent now if a key was typed that is in 'cinkeys'.
1556 */
1557 if (in_cinkeys(c, ' ', line_is_white))
1558 {
1559 if (stop_arrow() == OK)
1560 /* re-indent the current line */
1561 do_c_expr_indent();
1562 }
1563 }
1564#endif /* FEAT_CINDENT */
1565
1566 } /* for (;;) */
1567 /* NOTREACHED */
1568}
1569
1570/*
1571 * Redraw for Insert mode.
1572 * This is postponed until getting the next character to make '$' in the 'cpo'
1573 * option work correctly.
1574 * Only redraw when there are no characters available. This speeds up
1575 * inserting sequences of characters (e.g., for CTRL-R).
1576 */
1577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001578ins_redraw(
1579 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001581#ifdef FEAT_CONCEAL
1582 linenr_T conceal_old_cursor_line = 0;
1583 linenr_T conceal_new_cursor_line = 0;
1584 int conceal_update_lines = FALSE;
1585#endif
1586
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001587 if (char_avail())
1588 return;
1589
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001590#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001591 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1592 * visible, the command might delete it. */
1593 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001594# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001595 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001596# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001597# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001598 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001599# endif
1600# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001601 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001602# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001603 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001604# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001605 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001606# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001607# ifdef FEAT_INS_EXPAND
1608 && !pum_visible()
1609# endif
1610 )
1611 {
1612# ifdef FEAT_SYN_HL
1613 /* Need to update the screen first, to make sure syntax
1614 * highlighting is correct after making a change (e.g., inserting
1615 * a "(". The autocommand may also require a redraw, so it's done
1616 * again below, unfortunately. */
1617 if (syntax_present(curwin) && must_redraw)
1618 update_screen(0);
1619# endif
1620# ifdef FEAT_AUTOCMD
1621 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001622 {
1623 /* Make sure curswant is correct, an autocommand may call
1624 * getcurpos(). */
1625 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001626 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001627 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001628# endif
1629# ifdef FEAT_CONCEAL
1630 if (curwin->w_p_cole > 0)
1631 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001632# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001633 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001634# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001635 conceal_new_cursor_line = curwin->w_cursor.lnum;
1636 conceal_update_lines = TRUE;
1637 }
1638# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001639# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001640 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001641# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001642 }
1643#endif
1644
1645#ifdef FEAT_AUTOCMD
1646 /* Trigger TextChangedI if b_changedtick differs. */
1647 if (ready && has_textchangedI()
1648 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001649# ifdef FEAT_INS_EXPAND
1650 && !pum_visible()
1651# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001652 )
1653 {
1654 if (last_changedtick_buf == curbuf)
1655 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1656 last_changedtick_buf = curbuf;
1657 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001659#endif
1660
1661 if (must_redraw)
1662 update_screen(0);
1663 else if (clear_cmdline || redraw_cmdline)
1664 showmode(); /* clear cmdline and show mode */
1665# if defined(FEAT_CONCEAL)
1666 if ((conceal_update_lines
1667 && (conceal_old_cursor_line != conceal_new_cursor_line
1668 || conceal_cursor_line(curwin)))
1669 || need_cursor_line_redraw)
1670 {
1671 if (conceal_old_cursor_line != conceal_new_cursor_line)
1672 update_single_line(curwin, conceal_old_cursor_line);
1673 update_single_line(curwin, conceal_new_cursor_line == 0
1674 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1675 curwin->w_valid &= ~VALID_CROW;
1676 }
1677# endif
1678 showruler(FALSE);
1679 setcursor();
1680 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681}
1682
1683/*
1684 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1685 */
1686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001687ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688{
1689 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001690 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
1692 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001693 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
1695 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001698 did_putchar = TRUE;
1699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1701
1702#ifdef FEAT_CMDL_INFO
1703 add_to_showcmd_c(Ctrl_V);
1704#endif
1705
1706 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001707 if (did_putchar)
1708 /* when the line fits in 'columns' the '^' is at the start of the next
1709 * line and will not removed by the redraw */
1710 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#ifdef FEAT_CMDL_INFO
1712 clear_showcmd();
1713#endif
1714 insert_special(c, FALSE, TRUE);
1715#ifdef FEAT_RIGHTLEFT
1716 revins_chars++;
1717 revins_legal++;
1718#endif
1719}
1720
1721/*
1722 * Put a character directly onto the screen. It's not stored in a buffer.
1723 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1724 */
1725static int pc_status;
1726#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1727#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1728#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1729#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731static int pc_attr;
1732static int pc_row;
1733static int pc_col;
1734
1735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001736edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
1738 int attr;
1739
1740 if (ScreenLines != NULL)
1741 {
1742 update_topline(); /* just in case w_topline isn't valid */
1743 validate_cursor();
1744 if (highlight)
1745 attr = hl_attr(HLF_8);
1746 else
1747 attr = 0;
1748 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1749 pc_col = W_WINCOL(curwin);
1750#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1751 pc_status = PC_STATUS_UNSET;
1752#endif
1753#ifdef FEAT_RIGHTLEFT
1754 if (curwin->w_p_rl)
1755 {
1756 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1757# ifdef FEAT_MBYTE
1758 if (has_mbyte)
1759 {
1760 int fix_col = mb_fix_col(pc_col, pc_row);
1761
1762 if (fix_col != pc_col)
1763 {
1764 screen_putchar(' ', pc_row, fix_col, attr);
1765 --curwin->w_wcol;
1766 pc_status = PC_STATUS_RIGHT;
1767 }
1768 }
1769# endif
1770 }
1771 else
1772#endif
1773 {
1774 pc_col += curwin->w_wcol;
1775#ifdef FEAT_MBYTE
1776 if (mb_lefthalve(pc_row, pc_col))
1777 pc_status = PC_STATUS_LEFT;
1778#endif
1779 }
1780
1781 /* save the character to be able to put it back */
1782#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1783 if (pc_status == PC_STATUS_UNSET)
1784#endif
1785 {
1786 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1787 pc_status = PC_STATUS_SET;
1788 }
1789 screen_putchar(c, pc_row, pc_col, attr);
1790 }
1791}
1792
1793/*
1794 * Undo the previous edit_putchar().
1795 */
1796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001797edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
1799 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1800 {
1801#if defined(FEAT_MBYTE)
1802 if (pc_status == PC_STATUS_RIGHT)
1803 ++curwin->w_wcol;
1804 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1805 redrawWinline(curwin->w_cursor.lnum, FALSE);
1806 else
1807#endif
1808 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1809 }
1810}
1811
1812/*
1813 * Called when p_dollar is set: display a '$' at the end of the changed text
1814 * Only works when cursor is in the line that changes.
1815 */
1816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
1819 colnr_T save_col;
1820
1821 if (!redrawing())
1822 return;
1823
1824 cursor_off();
1825 save_col = curwin->w_cursor.col;
1826 curwin->w_cursor.col = col;
1827#ifdef FEAT_MBYTE
1828 if (has_mbyte)
1829 {
1830 char_u *p;
1831
1832 /* If on the last byte of a multi-byte move to the first byte. */
1833 p = ml_get_curline();
1834 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1835 }
1836#endif
1837 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1838 if (curwin->w_wcol < W_WIDTH(curwin))
1839 {
1840 edit_putchar('$', FALSE);
1841 dollar_vcol = curwin->w_virtcol;
1842 }
1843 curwin->w_cursor.col = save_col;
1844}
1845
1846/*
1847 * Call this function before moving the cursor from the normal insert position
1848 * in insert mode.
1849 */
1850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001851undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001853 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001855 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 redrawWinline(curwin->w_cursor.lnum, FALSE);
1857 }
1858}
1859
1860/*
1861 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1862 * Keep the cursor on the same character.
1863 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1864 * type == INDENT_DEC decrease indent (for CTRL-D)
1865 * type == INDENT_SET set indent to "amount"
1866 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1867 */
1868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001869change_indent(
1870 int type,
1871 int amount,
1872 int round,
1873 int replaced, /* replaced character, put on replace stack */
1874 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875{
1876 int vcol;
1877 int last_vcol;
1878 int insstart_less; /* reduction for Insstart.col */
1879 int new_cursor_col;
1880 int i;
1881 char_u *ptr;
1882 int save_p_list;
1883 int start_col;
1884 colnr_T vc;
1885#ifdef FEAT_VREPLACE
1886 colnr_T orig_col = 0; /* init for GCC */
1887 char_u *new_line, *orig_line = NULL; /* init for GCC */
1888
1889 /* VREPLACE mode needs to know what the line was like before changing */
1890 if (State & VREPLACE_FLAG)
1891 {
1892 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1893 orig_col = curwin->w_cursor.col;
1894 }
1895#endif
1896
1897 /* for the following tricks we don't want list mode */
1898 save_p_list = curwin->w_p_list;
1899 curwin->w_p_list = FALSE;
1900 vc = getvcol_nolist(&curwin->w_cursor);
1901 vcol = vc;
1902
1903 /*
1904 * For Replace mode we need to fix the replace stack later, which is only
1905 * possible when the cursor is in the indent. Remember the number of
1906 * characters before the cursor if it's possible.
1907 */
1908 start_col = curwin->w_cursor.col;
1909
1910 /* determine offset from first non-blank */
1911 new_cursor_col = curwin->w_cursor.col;
1912 beginline(BL_WHITE);
1913 new_cursor_col -= curwin->w_cursor.col;
1914
1915 insstart_less = curwin->w_cursor.col;
1916
1917 /*
1918 * If the cursor is in the indent, compute how many screen columns the
1919 * cursor is to the left of the first non-blank.
1920 */
1921 if (new_cursor_col < 0)
1922 vcol = get_indent() - vcol;
1923
1924 if (new_cursor_col > 0) /* can't fix replace stack */
1925 start_col = -1;
1926
1927 /*
1928 * Set the new indent. The cursor will be put on the first non-blank.
1929 */
1930 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001931 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 else
1933 {
1934#ifdef FEAT_VREPLACE
1935 int save_State = State;
1936
1937 /* Avoid being called recursively. */
1938 if (State & VREPLACE_FLAG)
1939 State = INSERT;
1940#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001941 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942#ifdef FEAT_VREPLACE
1943 State = save_State;
1944#endif
1945 }
1946 insstart_less -= curwin->w_cursor.col;
1947
1948 /*
1949 * Try to put cursor on same character.
1950 * If the cursor is at or after the first non-blank in the line,
1951 * compute the cursor column relative to the column of the first
1952 * non-blank character.
1953 * If we are not in insert mode, leave the cursor on the first non-blank.
1954 * If the cursor is before the first non-blank, position it relative
1955 * to the first non-blank, counted in screen columns.
1956 */
1957 if (new_cursor_col >= 0)
1958 {
1959 /*
1960 * When changing the indent while the cursor is touching it, reset
1961 * Insstart_col to 0.
1962 */
1963 if (new_cursor_col == 0)
1964 insstart_less = MAXCOL;
1965 new_cursor_col += curwin->w_cursor.col;
1966 }
1967 else if (!(State & INSERT))
1968 new_cursor_col = curwin->w_cursor.col;
1969 else
1970 {
1971 /*
1972 * Compute the screen column where the cursor should be.
1973 */
1974 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001975 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976
1977 /*
1978 * Advance the cursor until we reach the right screen column.
1979 */
1980 vcol = last_vcol = 0;
1981 new_cursor_col = -1;
1982 ptr = ml_get_curline();
1983 while (vcol <= (int)curwin->w_virtcol)
1984 {
1985 last_vcol = vcol;
1986#ifdef FEAT_MBYTE
1987 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001988 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 else
1990#endif
1991 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001992 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 }
1994 vcol = last_vcol;
1995
1996 /*
1997 * May need to insert spaces to be able to position the cursor on
1998 * the right screen column.
1999 */
2000 if (vcol != (int)curwin->w_virtcol)
2001 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002002 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002004 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 if (ptr != NULL)
2006 {
2007 new_cursor_col += i;
2008 ptr[i] = NUL;
2009 while (--i >= 0)
2010 ptr[i] = ' ';
2011 ins_str(ptr);
2012 vim_free(ptr);
2013 }
2014 }
2015
2016 /*
2017 * When changing the indent while the cursor is in it, reset
2018 * Insstart_col to 0.
2019 */
2020 insstart_less = MAXCOL;
2021 }
2022
2023 curwin->w_p_list = save_p_list;
2024
2025 if (new_cursor_col <= 0)
2026 curwin->w_cursor.col = 0;
2027 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002028 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 curwin->w_set_curswant = TRUE;
2030 changed_cline_bef_curs();
2031
2032 /*
2033 * May have to adjust the start of the insert.
2034 */
2035 if (State & INSERT)
2036 {
2037 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2038 {
2039 if ((int)Insstart.col <= insstart_less)
2040 Insstart.col = 0;
2041 else
2042 Insstart.col -= insstart_less;
2043 }
2044 if ((int)ai_col <= insstart_less)
2045 ai_col = 0;
2046 else
2047 ai_col -= insstart_less;
2048 }
2049
2050 /*
2051 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2052 * If the number of characters before the cursor decreased, need to pop a
2053 * few characters from the replace stack.
2054 * If the number of characters before the cursor increased, need to push a
2055 * few NULs onto the replace stack.
2056 */
2057 if (REPLACE_NORMAL(State) && start_col >= 0)
2058 {
2059 while (start_col > (int)curwin->w_cursor.col)
2060 {
2061 replace_join(0); /* remove a NUL from the replace stack */
2062 --start_col;
2063 }
2064 while (start_col < (int)curwin->w_cursor.col || replaced)
2065 {
2066 replace_push(NUL);
2067 if (replaced)
2068 {
2069 replace_push(replaced);
2070 replaced = NUL;
2071 }
2072 ++start_col;
2073 }
2074 }
2075
2076#ifdef FEAT_VREPLACE
2077 /*
2078 * For VREPLACE mode, we also have to fix the replace stack. In this case
2079 * it is always possible because we backspace over the whole line and then
2080 * put it back again the way we wanted it.
2081 */
2082 if (State & VREPLACE_FLAG)
2083 {
2084 /* If orig_line didn't allocate, just return. At least we did the job,
2085 * even if you can't backspace. */
2086 if (orig_line == NULL)
2087 return;
2088
2089 /* Save new line */
2090 new_line = vim_strsave(ml_get_curline());
2091 if (new_line == NULL)
2092 return;
2093
2094 /* We only put back the new line up to the cursor */
2095 new_line[curwin->w_cursor.col] = NUL;
2096
2097 /* Put back original line */
2098 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2099 curwin->w_cursor.col = orig_col;
2100
2101 /* Backspace from cursor to start of line */
2102 backspace_until_column(0);
2103
2104 /* Insert new stuff into line again */
2105 ins_bytes(new_line);
2106
2107 vim_free(new_line);
2108 }
2109#endif
2110}
2111
2112/*
2113 * Truncate the space at the end of a line. This is to be used only in an
2114 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2115 * modes.
2116 */
2117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002118truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 int i;
2121
2122 /* find start of trailing white space */
2123 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2124 {
2125 if (State & REPLACE_FLAG)
2126 replace_join(0); /* remove a NUL from the replace stack */
2127 }
2128 line[i + 1] = NUL;
2129}
2130
2131#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2132 || defined(FEAT_COMMENTS) || defined(PROTO)
2133/*
2134 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2135 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002136 * Will attempt not to go before "col" even when there is a composing
2137 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 */
2139 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002140backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141{
2142 while ((int)curwin->w_cursor.col > col)
2143 {
2144 curwin->w_cursor.col--;
2145 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002146 replace_do_bs(col);
2147 else if (!del_char_after_col(col))
2148 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 }
2150}
2151#endif
2152
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002153/*
2154 * Like del_char(), but make sure not to go before column "limit_col".
2155 * Only matters when there are composing characters.
2156 * Return TRUE when something was deleted.
2157 */
2158 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002159del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002160{
2161#ifdef FEAT_MBYTE
2162 if (enc_utf8 && limit_col >= 0)
2163 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002164 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002165
2166 /* Make sure the cursor is at the start of a character, but
2167 * skip forward again when going too far back because of a
2168 * composing character. */
2169 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002170 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002171 {
2172 int l = utf_ptr2len(ml_get_cursor());
2173
2174 if (l == 0) /* end of line */
2175 break;
2176 curwin->w_cursor.col += l;
2177 }
2178 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2179 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002180 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002181 }
2182 else
2183#endif
2184 (void)del_char(FALSE);
2185 return TRUE;
2186}
2187
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2189/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002190 * CTRL-X pressed in Insert mode.
2191 */
2192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002193ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002194{
2195 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2196 * CTRL-V works like CTRL-N */
2197 if (ctrl_x_mode != CTRL_X_CMDLINE)
2198 {
2199 /* if the next ^X<> won't ADD nothing, then reset
2200 * compl_cont_status */
2201 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002202 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002203 else
2204 compl_cont_status = 0;
2205 /* We're not sure which CTRL-X mode it will be yet */
2206 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2207 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2208 edit_submode_pre = NULL;
2209 showmode();
2210 }
2211}
2212
2213/*
2214 * Return TRUE if the 'dict' or 'tsr' option can be used.
2215 */
2216 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002218{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002219 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002220# ifdef FEAT_SPELL
2221 && !curwin->w_p_spell
2222# endif
2223 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002224 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2225 {
2226 ctrl_x_mode = 0;
2227 edit_submode = NULL;
2228 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2229 : (char_u *)_("'thesaurus' option is empty"),
2230 hl_attr(HLF_E));
2231 if (emsg_silent == 0)
2232 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002233 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002234 setcursor();
2235 out_flush();
2236 ui_delay(2000L, FALSE);
2237 }
2238 return FALSE;
2239 }
2240 return TRUE;
2241}
2242
2243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2245 * This depends on the current mode.
2246 */
2247 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249{
2250 /* Always allow ^R - let it's results then be checked */
2251 if (c == Ctrl_R)
2252 return TRUE;
2253
Bram Moolenaare3226be2005-12-18 22:10:00 +00002254 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002255 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002256 return TRUE;
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 switch (ctrl_x_mode)
2259 {
2260 case 0: /* Not in any CTRL-X mode */
2261 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2262 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002263 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2265 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2266 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002267 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002268 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 case CTRL_X_SCROLL:
2270 return (c == Ctrl_Y || c == Ctrl_E);
2271 case CTRL_X_WHOLE_LINE:
2272 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2273 case CTRL_X_FILES:
2274 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2275 case CTRL_X_DICTIONARY:
2276 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2277 case CTRL_X_THESAURUS:
2278 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2279 case CTRL_X_TAGS:
2280 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2281#ifdef FEAT_FIND_ID
2282 case CTRL_X_PATH_PATTERNS:
2283 return (c == Ctrl_P || c == Ctrl_N);
2284 case CTRL_X_PATH_DEFINES:
2285 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2286#endif
2287 case CTRL_X_CMDLINE:
2288 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2289 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002290#ifdef FEAT_COMPL_FUNC
2291 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002292 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002293 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002294 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002295#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002296 case CTRL_X_SPELL:
2297 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002298 case CTRL_X_EVAL:
2299 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301 EMSG(_(e_internal));
2302 return FALSE;
2303}
2304
2305/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002306 * Return TRUE when character "c" is part of the item currently being
2307 * completed. Used to decide whether to abandon complete mode when the menu
2308 * is visible.
2309 */
2310 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002311ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002312{
2313 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2314 /* When expanding an identifier only accept identifier chars. */
2315 return vim_isIDc(c);
2316
2317 switch (ctrl_x_mode)
2318 {
2319 case CTRL_X_FILES:
2320 /* When expanding file name only accept file name chars. But not
2321 * path separators, so that "proto/<Tab>" expands files in
2322 * "proto", not "proto/" as a whole */
2323 return vim_isfilec(c) && !vim_ispathsep(c);
2324
2325 case CTRL_X_CMDLINE:
2326 case CTRL_X_OMNI:
2327 /* Command line and Omni completion can work with just about any
2328 * printable character, but do stop at white space. */
2329 return vim_isprintc(c) && !vim_iswhite(c);
2330
2331 case CTRL_X_WHOLE_LINE:
2332 /* For while line completion a space can be part of the line. */
2333 return vim_isprintc(c);
2334 }
2335 return vim_iswordc(c);
2336}
2337
2338/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002339 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002341 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 * the rest of the word to be in -- webb
2343 */
2344 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002345ins_compl_add_infercase(
2346 char_u *str,
2347 int len,
2348 int icase,
2349 char_u *fname,
2350 int dir,
2351 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002353 char_u *p;
2354 int i, c;
2355 int actual_len; /* Take multi-byte characters */
2356 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002357 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002358 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 int has_lower = FALSE;
2360 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002362 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002364 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
Bram Moolenaara2993e12007-08-12 14:38:46 +00002366 /* Find actual length of completion. */
2367#ifdef FEAT_MBYTE
2368 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002370 p = str;
2371 actual_len = 0;
2372 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002374 mb_ptr_adv(p);
2375 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002378 else
2379#endif
2380 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
Bram Moolenaara2993e12007-08-12 14:38:46 +00002382 /* Find actual length of original text. */
2383#ifdef FEAT_MBYTE
2384 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002386 p = compl_orig_text;
2387 actual_compl_length = 0;
2388 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002390 mb_ptr_adv(p);
2391 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 }
2393 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002394 else
2395#endif
2396 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002398 /* "actual_len" may be smaller than "actual_compl_length" when using
2399 * thesaurus, only use the minimum when comparing. */
2400 min_len = actual_len < actual_compl_length
2401 ? actual_len : actual_compl_length;
2402
Bram Moolenaara2993e12007-08-12 14:38:46 +00002403 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002404 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002405 if (wca != NULL)
2406 {
2407 p = str;
2408 for (i = 0; i < actual_len; ++i)
2409#ifdef FEAT_MBYTE
2410 if (has_mbyte)
2411 wca[i] = mb_ptr2char_adv(&p);
2412 else
2413#endif
2414 wca[i] = *(p++);
2415
2416 /* Rule 1: Were any chars converted to lower? */
2417 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002418 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002419 {
2420#ifdef FEAT_MBYTE
2421 if (has_mbyte)
2422 c = mb_ptr2char_adv(&p);
2423 else
2424#endif
2425 c = *(p++);
2426 if (MB_ISLOWER(c))
2427 {
2428 has_lower = TRUE;
2429 if (MB_ISUPPER(wca[i]))
2430 {
2431 /* Rule 1 is satisfied. */
2432 for (i = actual_compl_length; i < actual_len; ++i)
2433 wca[i] = MB_TOLOWER(wca[i]);
2434 break;
2435 }
2436 }
2437 }
2438
2439 /*
2440 * Rule 2: No lower case, 2nd consecutive letter converted to
2441 * upper case.
2442 */
2443 if (!has_lower)
2444 {
2445 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002446 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002447 {
2448#ifdef FEAT_MBYTE
2449 if (has_mbyte)
2450 c = mb_ptr2char_adv(&p);
2451 else
2452#endif
2453 c = *(p++);
2454 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2455 {
2456 /* Rule 2 is satisfied. */
2457 for (i = actual_compl_length; i < actual_len; ++i)
2458 wca[i] = MB_TOUPPER(wca[i]);
2459 break;
2460 }
2461 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2462 }
2463 }
2464
2465 /* Copy the original case of the part we typed. */
2466 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002467 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002468 {
2469#ifdef FEAT_MBYTE
2470 if (has_mbyte)
2471 c = mb_ptr2char_adv(&p);
2472 else
2473#endif
2474 c = *(p++);
2475 if (MB_ISLOWER(c))
2476 wca[i] = MB_TOLOWER(wca[i]);
2477 else if (MB_ISUPPER(c))
2478 wca[i] = MB_TOUPPER(wca[i]);
2479 }
2480
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002481 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002482 * Generate encoding specific output from wide character array.
2483 * Multi-byte characters can occupy up to five bytes more than
2484 * ASCII characters, and we also need one byte for NUL, so stay
2485 * six bytes away from the edge of IObuff.
2486 */
2487 p = IObuff;
2488 i = 0;
2489 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2490#ifdef FEAT_MBYTE
2491 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002492 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002493 else
2494#endif
2495 *(p++) = wca[i++];
2496 *p = NUL;
2497
2498 vim_free(wca);
2499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002501 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2502 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002504 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505}
2506
2507/*
2508 * Add a match to the list of matches.
2509 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002510 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002511 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002513 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002514ins_compl_add(
2515 char_u *str,
2516 int len,
2517 int icase,
2518 char_u *fname,
2519 char_u **cptext, /* extra text for popup menu or NULL */
2520 int cdir,
2521 int flags,
2522 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002524 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002525 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
2527 ui_breakcheck();
2528 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002529 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 if (len < 0)
2531 len = (int)STRLEN(str);
2532
2533 /*
2534 * If the same match is already present, don't add it.
2535 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002536 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002538 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 do
2540 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002541 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002542 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002543 && match->cp_str[len] == NUL)
2544 return NOTDONE;
2545 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002546 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 }
2548
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002549 /* Remove any popup menu before changing the list of matches. */
2550 ins_compl_del_pum();
2551
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 /*
2553 * Allocate a new match structure.
2554 * Copy the values to the new match structure.
2555 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002556 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002558 return FAIL;
2559 match->cp_number = -1;
2560 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002561 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002562 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
2564 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002565 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002567 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002570 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2572 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002573 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002574 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002575 && compl_curr_match->cp_fname != NULL
2576 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002577 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002578 else if (fname != NULL)
2579 {
2580 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002581 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002584 match->cp_fname = NULL;
2585 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002586
2587 if (cptext != NULL)
2588 {
2589 int i;
2590
2591 for (i = 0; i < CPT_COUNT; ++i)
2592 if (cptext[i] != NULL && *cptext[i] != NUL)
2593 match->cp_text[i] = vim_strsave(cptext[i]);
2594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
2596 /*
2597 * Link the new match structure in the list of matches.
2598 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002599 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002600 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 else if (dir == FORWARD)
2602 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002603 match->cp_next = compl_curr_match->cp_next;
2604 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606 else /* BACKWARD */
2607 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002608 match->cp_next = compl_curr_match;
2609 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002611 if (match->cp_next)
2612 match->cp_next->cp_prev = match;
2613 if (match->cp_prev)
2614 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002616 compl_first_match = match;
2617 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002619 /*
2620 * Find the longest common string if still doing that.
2621 */
2622 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2623 ins_compl_longest_match(match);
2624
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 return OK;
2626}
2627
2628/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002629 * Return TRUE if "str[len]" matches with match->cp_str, considering
2630 * match->cp_icase.
2631 */
2632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002633ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002634{
2635 if (match->cp_icase)
2636 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2637 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2638}
2639
2640/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002641 * Reduce the longest common string for match "match".
2642 */
2643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002644ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002645{
2646 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002647 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002648 int had_match;
2649
2650 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002651 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002652 /* First match, use it as a whole. */
2653 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002654 if (compl_leader != NULL)
2655 {
2656 had_match = (curwin->w_cursor.col > compl_col);
2657 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002658 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002659 ins_redraw(FALSE);
2660
2661 /* When the match isn't there (to avoid matching itself) remove it
2662 * again after redrawing. */
2663 if (!had_match)
2664 ins_compl_delete();
2665 compl_used_match = FALSE;
2666 }
2667 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002668 else
2669 {
2670 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002671 p = compl_leader;
2672 s = match->cp_str;
2673 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002674 {
2675#ifdef FEAT_MBYTE
2676 if (has_mbyte)
2677 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002678 c1 = mb_ptr2char(p);
2679 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002680 }
2681 else
2682#endif
2683 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002684 c1 = *p;
2685 c2 = *s;
2686 }
2687 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2688 : (c1 != c2))
2689 break;
2690#ifdef FEAT_MBYTE
2691 if (has_mbyte)
2692 {
2693 mb_ptr_adv(p);
2694 mb_ptr_adv(s);
2695 }
2696 else
2697#endif
2698 {
2699 ++p;
2700 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002701 }
2702 }
2703
2704 if (*p != NUL)
2705 {
2706 /* Leader was shortened, need to change the inserted text. */
2707 *p = NUL;
2708 had_match = (curwin->w_cursor.col > compl_col);
2709 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002710 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002711 ins_redraw(FALSE);
2712
2713 /* When the match isn't there (to avoid matching itself) remove it
2714 * again after redrawing. */
2715 if (!had_match)
2716 ins_compl_delete();
2717 }
2718
2719 compl_used_match = FALSE;
2720 }
2721}
2722
2723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 * Add an array of matches to the list of matches.
2725 * Frees matches[].
2726 */
2727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002728ins_compl_add_matches(
2729 int num_matches,
2730 char_u **matches,
2731 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732{
2733 int i;
2734 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002735 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
Bram Moolenaar572cb562005-08-05 21:35:02 +00002737 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002738 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002739 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002741 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 FreeWild(num_matches, matches);
2743}
2744
2745/* Make the completion list cyclic.
2746 * Return the number of matches (excluding the original).
2747 */
2748 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002749ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002751 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 int count = 0;
2753
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002754 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
2756 /*
2757 * Find the end of the list.
2758 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002759 match = compl_first_match;
2760 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002761 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002763 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 ++count;
2765 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002766 match->cp_next = compl_first_match;
2767 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 return count;
2770}
2771
Bram Moolenaara94bc432006-03-10 21:42:59 +00002772/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002773 * Set variables that store noselect and noinsert behavior from the
2774 * 'completeopt' value.
2775 */
2776 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002777completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002778{
2779 compl_no_insert = FALSE;
2780 compl_no_select = FALSE;
2781 if (strstr((char *)p_cot, "noselect") != NULL)
2782 compl_no_select = TRUE;
2783 if (strstr((char *)p_cot, "noinsert") != NULL)
2784 compl_no_insert = TRUE;
2785}
2786
2787/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002788 * Start completion for the complete() function.
2789 * "startcol" is where the matched text starts (1 is first column).
2790 * "list" is the list of matches.
2791 */
2792 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002793set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002794{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002795 int save_w_wrow = curwin->w_wrow;
2796
Bram Moolenaara94bc432006-03-10 21:42:59 +00002797 /* If already doing completions stop it. */
2798 if (ctrl_x_mode != 0)
2799 ins_compl_prep(' ');
2800 ins_compl_clear();
2801
2802 if (stop_arrow() == FAIL)
2803 return;
2804
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002805 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002806 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002807 startcol = curwin->w_cursor.col;
2808 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002809 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002810 /* compl_pattern doesn't need to be set */
2811 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2812 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002813 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002814 return;
2815
Bram Moolenaare4214502015-03-05 18:08:43 +01002816 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002817
2818 ins_compl_add_list(list);
2819 compl_matches = ins_compl_make_cyclic();
2820 compl_started = TRUE;
2821 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002822 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002823
2824 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002825 if (compl_no_insert || compl_no_select)
2826 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002827 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002828 if (compl_no_select)
2829 /* Down/Up has no real effect. */
2830 ins_complete(K_UP, FALSE);
2831 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002832 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002833 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002834
2835 /* Lazily show the popup menu, unless we got interrupted. */
2836 if (!compl_interrupted)
2837 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002838 out_flush();
2839}
2840
2841
Bram Moolenaar9372a112005-12-06 19:59:18 +00002842/* "compl_match_array" points the currently displayed list of entries in the
2843 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002844static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002845static int compl_match_arraysize;
2846
2847/*
2848 * Update the screen and when there is any scrolling remove the popup menu.
2849 */
2850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002851ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002852{
2853 int h;
2854
2855 if (compl_match_array != NULL)
2856 {
2857 h = curwin->w_cline_height;
2858 update_screen(0);
2859 if (h != curwin->w_cline_height)
2860 ins_compl_del_pum();
2861 }
2862}
2863
2864/*
2865 * Remove any popup menu.
2866 */
2867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002868ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002869{
2870 if (compl_match_array != NULL)
2871 {
2872 pum_undisplay();
2873 vim_free(compl_match_array);
2874 compl_match_array = NULL;
2875 }
2876}
2877
2878/*
2879 * Return TRUE if the popup menu should be displayed.
2880 */
2881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002882pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002883{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002884 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002885 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002886 return FALSE;
2887
2888 /* The display looks bad on a B&W display. */
2889 if (t_colors < 8
2890#ifdef FEAT_GUI
2891 && !gui.in_use
2892#endif
2893 )
2894 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002895 return TRUE;
2896}
2897
2898/*
2899 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002900 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002901 */
2902 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002903pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002904{
2905 compl_T *compl;
2906 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002907
2908 /* Don't display the popup menu if there are no matches or there is only
2909 * one (ignoring the original text). */
2910 compl = compl_first_match;
2911 i = 0;
2912 do
2913 {
2914 if (compl == NULL
2915 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2916 break;
2917 compl = compl->cp_next;
2918 } while (compl != compl_first_match);
2919
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002920 if (strstr((char *)p_cot, "menuone") != NULL)
2921 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002922 return (i >= 2);
2923}
2924
2925/*
2926 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002927 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002928 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002929 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002930ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002931{
2932 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002933 compl_T *shown_compl = NULL;
2934 int did_find_shown_match = FALSE;
2935 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002936 int i;
2937 int cur = -1;
2938 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002939 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002940
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002941 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002942 return;
2943
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002944#if defined(FEAT_EVAL)
2945 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2946 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2947#endif
2948
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002949 /* Update the screen before drawing the popup menu over it. */
2950 update_screen(0);
2951
2952 if (compl_match_array == NULL)
2953 {
2954 /* Need to build the popup menu list. */
2955 compl_match_arraysize = 0;
2956 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002957 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002958 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002959 do
2960 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002961 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2962 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002963 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002964 ++compl_match_arraysize;
2965 compl = compl->cp_next;
2966 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002967 if (compl_match_arraysize == 0)
2968 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002969 compl_match_array = (pumitem_T *)alloc_clear(
2970 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002971 * compl_match_arraysize));
2972 if (compl_match_array != NULL)
2973 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002974 /* If the current match is the original text don't find the first
2975 * match after it, don't highlight anything. */
2976 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2977 shown_match_ok = TRUE;
2978
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002979 i = 0;
2980 compl = compl_first_match;
2981 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 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002987 if (!shown_match_ok)
2988 {
2989 if (compl == compl_shown_match || did_find_shown_match)
2990 {
2991 /* This item is the shown match or this is the
2992 * first displayed item after the shown match. */
2993 compl_shown_match = compl;
2994 did_find_shown_match = TRUE;
2995 shown_match_ok = TRUE;
2996 }
2997 else
2998 /* Remember this displayed match for when the
2999 * shown match is just below it. */
3000 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003001 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003002 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003003
3004 if (compl->cp_text[CPT_ABBR] != NULL)
3005 compl_match_array[i].pum_text =
3006 compl->cp_text[CPT_ABBR];
3007 else
3008 compl_match_array[i].pum_text = compl->cp_str;
3009 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3010 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3011 if (compl->cp_text[CPT_MENU] != NULL)
3012 compl_match_array[i++].pum_extra =
3013 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003014 else
3015 compl_match_array[i++].pum_extra = compl->cp_fname;
3016 }
3017
3018 if (compl == compl_shown_match)
3019 {
3020 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003021
3022 /* When the original text is the shown match don't set
3023 * compl_shown_match. */
3024 if (compl->cp_flags & ORIGINAL_TEXT)
3025 shown_match_ok = TRUE;
3026
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003027 if (!shown_match_ok && shown_compl != NULL)
3028 {
3029 /* The shown match isn't displayed, set it to the
3030 * previously displayed match. */
3031 compl_shown_match = shown_compl;
3032 shown_match_ok = TRUE;
3033 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003034 }
3035 compl = compl->cp_next;
3036 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003037
3038 if (!shown_match_ok) /* no displayed match at all */
3039 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003040 }
3041 }
3042 else
3043 {
3044 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003045 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003046 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3047 || compl_match_array[i].pum_text
3048 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003049 {
3050 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003051 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003052 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003053 }
3054
3055 if (compl_match_array != NULL)
3056 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003057 /* In Replace mode when a $ is displayed at the end of the line only
3058 * part of the screen would be updated. We do need to redraw here. */
3059 dollar_vcol = -1;
3060
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003061 /* Compute the screen column of the start of the completed text.
3062 * Use the cursor to get all wrapping and other settings right. */
3063 col = curwin->w_cursor.col;
3064 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003065 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003066 curwin->w_cursor.col = col;
3067 }
3068}
3069
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070#define DICT_FIRST (1) /* use just first element in "dict" */
3071#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003072
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003074 * Add any identifiers that match the given pattern in the list of dictionary
3075 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 */
3077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003078ins_compl_dictionaries(
3079 char_u *dict_start,
3080 char_u *pat,
3081 int flags, /* DICT_FIRST and/or DICT_EXACT */
3082 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003084 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 char_u *ptr;
3086 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 char_u **files;
3089 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003091 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
Bram Moolenaar0b238792006-03-02 22:49:12 +00003093 if (*dict == NUL)
3094 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003095#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003096 /* When 'dictionary' is empty and spell checking is enabled use
3097 * "spell". */
3098 if (!thesaurus && curwin->w_p_spell)
3099 dict = (char_u *)"spell";
3100 else
3101#endif
3102 return;
3103 }
3104
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003106 if (buf == NULL)
3107 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003108 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003109
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 /* If 'infercase' is set, don't use 'smartcase' here */
3111 save_p_scs = p_scs;
3112 if (curbuf->b_p_inf)
3113 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003114
3115 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3116 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003117 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003118 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003119 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003120 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003121 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003122
3123 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003124 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003125 len = STRLEN(pat_esc) + 10;
3126 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003127 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003128 {
3129 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003130 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003131 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003132 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003133 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3134 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003135 vim_free(ptr);
3136 }
3137 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003138 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003139 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003140 if (regmatch.regprog == NULL)
3141 goto theend;
3142 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3145 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003146 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 /* copy one dictionary file name into buf */
3149 if (flags == DICT_EXACT)
3150 {
3151 count = 1;
3152 files = &dict;
3153 }
3154 else
3155 {
3156 /* Expand wildcards in the dictionary name, but do not allow
3157 * backticks (for security, the 'dict' option may have been set in
3158 * a modeline). */
3159 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003160# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003161 if (!thesaurus && STRCMP(buf, "spell") == 0)
3162 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003163 else
3164# endif
3165 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 || expand_wildcards(1, &buf, &count, &files,
3167 EW_FILE|EW_SILENT) != OK)
3168 count = 0;
3169 }
3170
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003171# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003172 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003174 /* Complete from active spelling. Skip "\<" in the pattern, we
3175 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003176 if (pat[0] == '\\' && pat[1] == '<')
3177 ptr = pat + 2;
3178 else
3179 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003180 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003182 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003183# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003184 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003185 {
3186 ins_compl_files(count, files, thesaurus, flags,
3187 &regmatch, buf, &dir);
3188 if (flags != DICT_EXACT)
3189 FreeWild(count, files);
3190 }
3191 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 break;
3193 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003194
3195theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003197 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 vim_free(buf);
3199}
3200
Bram Moolenaar0b238792006-03-02 22:49:12 +00003201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003202ins_compl_files(
3203 int count,
3204 char_u **files,
3205 int thesaurus,
3206 int flags,
3207 regmatch_T *regmatch,
3208 char_u *buf,
3209 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003210{
3211 char_u *ptr;
3212 int i;
3213 FILE *fp;
3214 int add_r;
3215
3216 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3217 {
3218 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3219 if (flags != DICT_EXACT)
3220 {
3221 vim_snprintf((char *)IObuff, IOSIZE,
3222 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003223 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003224 }
3225
3226 if (fp != NULL)
3227 {
3228 /*
3229 * Read dictionary file line by line.
3230 * Check each line for a match.
3231 */
3232 while (!got_int && !compl_interrupted
3233 && !vim_fgets(buf, LSIZE, fp))
3234 {
3235 ptr = buf;
3236 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3237 {
3238 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003239 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003240 ptr = find_line_end(ptr);
3241 else
3242 ptr = find_word_end(ptr);
3243 add_r = ins_compl_add_infercase(regmatch->startp[0],
3244 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003245 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003246 if (thesaurus)
3247 {
3248 char_u *wstart;
3249
3250 /*
3251 * Add the other matches on the line
3252 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003253 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003254 while (!got_int)
3255 {
3256 /* Find start of the next word. Skip white
3257 * space and punctuation. */
3258 ptr = find_word_start(ptr);
3259 if (*ptr == NUL || *ptr == NL)
3260 break;
3261 wstart = ptr;
3262
Bram Moolenaara2993e12007-08-12 14:38:46 +00003263 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003264#ifdef FEAT_MBYTE
3265 if (has_mbyte)
3266 /* Japanese words may have characters in
3267 * different classes, only separate words
3268 * with single-byte non-word characters. */
3269 while (*ptr != NUL)
3270 {
3271 int l = (*mb_ptr2len)(ptr);
3272
3273 if (l < 2 && !vim_iswordc(*ptr))
3274 break;
3275 ptr += l;
3276 }
3277 else
3278#endif
3279 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003280
3281 /* Add the word. Skip the regexp match. */
3282 if (wstart != regmatch->startp[0])
3283 add_r = ins_compl_add_infercase(wstart,
3284 (int)(ptr - wstart),
3285 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003286 }
3287 }
3288 if (add_r == OK)
3289 /* if dir was BACKWARD then honor it just once */
3290 *dir = FORWARD;
3291 else if (add_r == FAIL)
3292 break;
3293 /* avoid expensive call to vim_regexec() when at end
3294 * of line */
3295 if (*ptr == '\n' || got_int)
3296 break;
3297 }
3298 line_breakcheck();
3299 ins_compl_check_keys(50);
3300 }
3301 fclose(fp);
3302 }
3303 }
3304}
3305
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306/*
3307 * Find the start of the next word.
3308 * Returns a pointer to the first char of the word. Also stops at a NUL.
3309 */
3310 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003311find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313#ifdef FEAT_MBYTE
3314 if (has_mbyte)
3315 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003316 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 else
3318#endif
3319 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3320 ++ptr;
3321 return ptr;
3322}
3323
3324/*
3325 * Find the end of the word. Assumes it starts inside a word.
3326 * Returns a pointer to just after the word.
3327 */
3328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003329find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330{
3331#ifdef FEAT_MBYTE
3332 int start_class;
3333
3334 if (has_mbyte)
3335 {
3336 start_class = mb_get_class(ptr);
3337 if (start_class > 1)
3338 while (*ptr != NUL)
3339 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003340 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 if (mb_get_class(ptr) != start_class)
3342 break;
3343 }
3344 }
3345 else
3346#endif
3347 while (vim_iswordc(*ptr))
3348 ++ptr;
3349 return ptr;
3350}
3351
3352/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003353 * Find the end of the line, omitting CR and NL at the end.
3354 * Returns a pointer to just after the line.
3355 */
3356 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003357find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003358{
3359 char_u *s;
3360
3361 s = ptr + STRLEN(ptr);
3362 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3363 --s;
3364 return s;
3365}
3366
3367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 * Free the list of completions
3369 */
3370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003371ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003373 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003374 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003376 vim_free(compl_pattern);
3377 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003378 vim_free(compl_leader);
3379 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003381 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003383
3384 ins_compl_del_pum();
3385 pum_clear();
3386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003387 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 do
3389 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003390 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003391 compl_curr_match = compl_curr_match->cp_next;
3392 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003394 if (match->cp_flags & FREE_FNAME)
3395 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003396 for (i = 0; i < CPT_COUNT; ++i)
3397 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003399 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3400 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003401 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402}
3403
3404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003405ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003407 compl_cont_status = 0;
3408 compl_started = FALSE;
3409 compl_matches = 0;
3410 vim_free(compl_pattern);
3411 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003412 vim_free(compl_leader);
3413 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003415 vim_free(compl_orig_text);
3416 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003417 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003418 /* clear v:completed_item */
3419 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420}
3421
3422/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003423 * Return TRUE when Insert completion is active.
3424 */
3425 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003426ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003427{
3428 return compl_started;
3429}
3430
3431/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003432 * Delete one character before the cursor and show the subset of the matches
3433 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003434 * Returns the character to be used, NUL if the work is done and another char
3435 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003436 */
3437 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003438ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003439{
3440 char_u *line;
3441 char_u *p;
3442
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003443 line = ml_get_curline();
3444 p = line + curwin->w_cursor.col;
3445 mb_ptr_back(line, p);
3446
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003447 /* Stop completion when the whole word was deleted. For Omni completion
3448 * allow the word to be deleted, we won't match everything. */
3449 if ((int)(p - line) - (int)compl_col < 0
3450 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003451 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003452 return K_BS;
3453
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003454 /* Deleted more than what was used to find matches or didn't finish
3455 * finding all matches: need to look for matches all over again. */
3456 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003457 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003458 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003459
Bram Moolenaara6557602006-02-04 22:43:20 +00003460 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003461 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003462 if (compl_leader != NULL)
3463 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003464 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003465 if (compl_shown_match != NULL)
3466 /* Make sure current match is not a hidden item. */
3467 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003468 return NUL;
3469 }
3470 return K_BS;
3471}
Bram Moolenaara6557602006-02-04 22:43:20 +00003472
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003473/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003474 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3475 * be called.
3476 */
3477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003478ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003479{
3480 /* Return TRUE if we didn't complete finding matches or when the
3481 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3482 return compl_was_interrupted
3483 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3484 && compl_opt_refresh_always);
3485}
3486
3487/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003488 * Called after changing "compl_leader".
3489 * Show the popup menu with a different set of matches.
3490 * May also search for matches again if the previous search was interrupted.
3491 */
3492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003493ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003494{
3495 ins_compl_del_pum();
3496 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003497 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003498 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003499
3500 if (compl_started)
3501 ins_compl_set_original_text(compl_leader);
3502 else
3503 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003504#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003505 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003506#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003507 /*
3508 * Matches were cleared, need to search for them now. First display
3509 * the changed text before the cursor. Set "compl_restarting" to
3510 * avoid that the first match is inserted.
3511 */
3512 update_screen(0);
3513#ifdef FEAT_GUI
3514 if (gui.in_use)
3515 {
3516 /* Show the cursor after the match, not after the redrawn text. */
3517 setcursor();
3518 out_flush();
3519 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003520 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003521#endif
3522 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003523 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003524 compl_cont_status = 0;
3525 compl_restarting = FALSE;
3526 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003527
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003528 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003529
3530 /* Show the popup menu with a different set of matches. */
3531 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003532
3533 /* Don't let Enter select the original text when there is no popup menu. */
3534 if (compl_match_array == NULL)
3535 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003536}
3537
3538/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003539 * Return the length of the completion, from the completion start column to
3540 * the cursor column. Making sure it never goes below zero.
3541 */
3542 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003543ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003544{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003545 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003546
3547 if (off < 0)
3548 return 0;
3549 return off;
3550}
3551
3552/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003553 * Append one character to the match leader. May reduce the number of
3554 * matches.
3555 */
3556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003557ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003558{
3559#ifdef FEAT_MBYTE
3560 int cc;
3561
3562 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3563 {
3564 char_u buf[MB_MAXBYTES + 1];
3565
3566 (*mb_char2bytes)(c, buf);
3567 buf[cc] = NUL;
3568 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003569 if (compl_opt_refresh_always)
3570 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003571 }
3572 else
3573#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003574 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003575 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003576 if (compl_opt_refresh_always)
3577 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003578 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003579
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003580 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003581 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003582 ins_compl_restart();
3583
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003584 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003585 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003586 * break redo. */
3587 if (!compl_opt_refresh_always)
3588 {
3589 vim_free(compl_leader);
3590 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003591 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003592 if (compl_leader != NULL)
3593 ins_compl_new_leader();
3594 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003595}
3596
3597/*
3598 * Setup for finding completions again without leaving CTRL-X mode. Used when
3599 * BS or a key was typed while still searching for matches.
3600 */
3601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003602ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003603{
3604 ins_compl_free();
3605 compl_started = FALSE;
3606 compl_matches = 0;
3607 compl_cont_status = 0;
3608 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003609}
3610
3611/*
3612 * Set the first match, the original text.
3613 */
3614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003615ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003616{
3617 char_u *p;
3618
3619 /* Replace the original text entry. */
3620 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3621 {
3622 p = vim_strsave(str);
3623 if (p != NULL)
3624 {
3625 vim_free(compl_first_match->cp_str);
3626 compl_first_match->cp_str = p;
3627 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003628 }
3629}
3630
3631/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003632 * Append one character to the match leader. May reduce the number of
3633 * matches.
3634 */
3635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003636ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003637{
3638 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003639 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003640 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003641 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003642
3643 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003644 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003645 {
3646 /* When still at the original match use the first entry that matches
3647 * the leader. */
3648 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3649 {
3650 p = NULL;
3651 for (cp = compl_shown_match->cp_next; cp != NULL
3652 && cp != compl_first_match; cp = cp->cp_next)
3653 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003654 if (compl_leader == NULL
3655 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003656 (int)STRLEN(compl_leader)))
3657 {
3658 p = cp->cp_str;
3659 break;
3660 }
3661 }
3662 if (p == NULL || (int)STRLEN(p) <= len)
3663 return;
3664 }
3665 else
3666 return;
3667 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003668 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003669 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003670 ins_compl_addleader(c);
3671}
3672
3673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003675 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003676 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003678 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003679ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003683 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
3685 /* Forget any previous 'special' messages if this is actually
3686 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3687 */
3688 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3689 edit_submode_extra = NULL;
3690
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003691 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003692 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3693 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003694 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003696 /* Set "compl_get_longest" when finding the first matches. */
3697 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3698 || (ctrl_x_mode == 0 && !compl_started))
3699 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003700 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003701 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003702
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003703 }
3704
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3706 {
3707 /*
3708 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3709 * it will be yet. Now we decide.
3710 */
3711 switch (c)
3712 {
3713 case Ctrl_E:
3714 case Ctrl_Y:
3715 ctrl_x_mode = CTRL_X_SCROLL;
3716 if (!(State & REPLACE_FLAG))
3717 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3718 else
3719 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3720 edit_submode_pre = NULL;
3721 showmode();
3722 break;
3723 case Ctrl_L:
3724 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3725 break;
3726 case Ctrl_F:
3727 ctrl_x_mode = CTRL_X_FILES;
3728 break;
3729 case Ctrl_K:
3730 ctrl_x_mode = CTRL_X_DICTIONARY;
3731 break;
3732 case Ctrl_R:
3733 /* Simply allow ^R to happen without affecting ^X mode */
3734 break;
3735 case Ctrl_T:
3736 ctrl_x_mode = CTRL_X_THESAURUS;
3737 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003738#ifdef FEAT_COMPL_FUNC
3739 case Ctrl_U:
3740 ctrl_x_mode = CTRL_X_FUNCTION;
3741 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003742 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003743 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003744 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003745#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003746 case 's':
3747 case Ctrl_S:
3748 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003749#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003750 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003751 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003752 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003753#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003754 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 case Ctrl_RSB:
3756 ctrl_x_mode = CTRL_X_TAGS;
3757 break;
3758#ifdef FEAT_FIND_ID
3759 case Ctrl_I:
3760 case K_S_TAB:
3761 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3762 break;
3763 case Ctrl_D:
3764 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3765 break;
3766#endif
3767 case Ctrl_V:
3768 case Ctrl_Q:
3769 ctrl_x_mode = CTRL_X_CMDLINE;
3770 break;
3771 case Ctrl_P:
3772 case Ctrl_N:
3773 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3774 * just started ^X mode, or there were enough ^X's to cancel
3775 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3776 * do normal expansion when interrupting a different mode (say
3777 * ^X^F^X^P or ^P^X^X^P, see below)
3778 * nothing changes if interrupting mode 0, (eg, the flag
3779 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003780 if (!(compl_cont_status & CONT_INTRPT))
3781 compl_cont_status |= CONT_LOCAL;
3782 else if (compl_cont_mode != 0)
3783 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 /* FALLTHROUGH */
3785 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003786 /* If we have typed at least 2 ^X's... for modes != 0, we set
3787 * compl_cont_status = 0 (eg, as if we had just started ^X
3788 * mode).
3789 * For mode 0, we set "compl_cont_mode" to an impossible
3790 * value, in both cases ^X^X can be used to restart the same
3791 * mode (avoiding ADDING mode).
3792 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3793 * 'complete' and local ^P expansions respectively.
3794 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3795 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (c == Ctrl_X)
3797 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003798 if (compl_cont_mode != 0)
3799 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003801 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803 ctrl_x_mode = 0;
3804 edit_submode = NULL;
3805 showmode();
3806 break;
3807 }
3808 }
3809 else if (ctrl_x_mode != 0)
3810 {
3811 /* We're already in CTRL-X mode, do we stay in it? */
3812 if (!vim_is_ctrl_x_key(c))
3813 {
3814 if (ctrl_x_mode == CTRL_X_SCROLL)
3815 ctrl_x_mode = 0;
3816 else
3817 ctrl_x_mode = CTRL_X_FINISHED;
3818 edit_submode = NULL;
3819 }
3820 showmode();
3821 }
3822
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003823 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
3825 /* Show error message from attempted keyword completion (probably
3826 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003827 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003829 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3830 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 || ctrl_x_mode == CTRL_X_FINISHED)
3832 {
3833 /* Get here when we have finished typing a sequence of ^N and
3834 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003835 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003836 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
3838 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003839 * If any of the original typed text has been changed, eg when
3840 * ignorecase is set, we must add back-spaces to the redo
3841 * buffer. We add as few as necessary to delete just the part
3842 * of the original text that has changed.
3843 * When using the longest match, edited the match or used
3844 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003846 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3847 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003848 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003849 ptr = NULL;
3850 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852
3853#ifdef FEAT_CINDENT
3854 want_cindent = (can_cindent && cindent_on());
3855#endif
3856 /*
3857 * When completing whole lines: fix indent for 'cindent'.
3858 * Otherwise, break line if it's too long.
3859 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003860 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
3862#ifdef FEAT_CINDENT
3863 /* re-indent the current line */
3864 if (want_cindent)
3865 {
3866 do_c_expr_indent();
3867 want_cindent = FALSE; /* don't do it again */
3868 }
3869#endif
3870 }
3871 else
3872 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003873 int prev_col = curwin->w_cursor.col;
3874
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003876 if (prev_col > 0)
3877 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (stop_arrow() == OK)
3879 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003880 if (prev_col > 0
3881 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3882 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003885 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003886 * the selection without inserting anything. When
3887 * compl_enter_selects is set the Enter key does the same. */
3888 if ((c == Ctrl_Y || (compl_enter_selects
3889 && (c == CAR || c == K_KENTER || c == NL)))
3890 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003891 retval = TRUE;
3892
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003893 /* CTRL-E means completion is Ended, go back to the typed text. */
3894 if (c == Ctrl_E)
3895 {
3896 ins_compl_delete();
3897 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003898 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003899 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003900 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003901 retval = TRUE;
3902 }
3903
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003904 auto_format(FALSE, TRUE);
3905
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003907 compl_started = FALSE;
3908 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003909 if (!shortmess(SHM_COMPLETIONMENU))
3910 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003912 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if (edit_submode != NULL)
3914 {
3915 edit_submode = NULL;
3916 showmode();
3917 }
3918
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003919#ifdef FEAT_CMDWIN
3920 if (c == Ctrl_C && cmdwin_type != 0)
3921 /* Avoid the popup menu remains displayed when leaving the
3922 * command line window. */
3923 update_screen(0);
3924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925#ifdef FEAT_CINDENT
3926 /*
3927 * Indent now if a key was typed that is in 'cinkeys'.
3928 */
3929 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3930 do_c_expr_indent();
3931#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003932#ifdef FEAT_AUTOCMD
3933 /* Trigger the CompleteDone event to give scripts a chance to act
3934 * upon the completion. */
3935 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003939#ifdef FEAT_AUTOCMD
3940 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3941 /* Trigger the CompleteDone event to give scripts a chance to act
3942 * upon the (possibly failed) completion. */
3943 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945
3946 /* reset continue_* if we left expansion-mode, if we stay they'll be
3947 * (re)set properly in ins_complete() */
3948 if (!vim_is_ctrl_x_key(c))
3949 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003950 compl_cont_status = 0;
3951 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003953
3954 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955}
3956
3957/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003958 * Fix the redo buffer for the completion leader replacing some of the typed
3959 * text. This inserts backspaces and appends the changed text.
3960 * "ptr" is the known leader text or NUL.
3961 */
3962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003963ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003964{
3965 int len;
3966 char_u *p;
3967 char_u *ptr = ptr_arg;
3968
3969 if (ptr == NULL)
3970 {
3971 if (compl_leader != NULL)
3972 ptr = compl_leader;
3973 else
3974 return; /* nothing to do */
3975 }
3976 if (compl_orig_text != NULL)
3977 {
3978 p = compl_orig_text;
3979 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3980 ;
3981#ifdef FEAT_MBYTE
3982 if (len > 0)
3983 len -= (*mb_head_off)(p, p + len);
3984#endif
3985 for (p += len; *p != NUL; mb_ptr_adv(p))
3986 AppendCharToRedobuff(K_BS);
3987 }
3988 else
3989 len = 0;
3990 if (ptr != NULL)
3991 AppendToRedobuffLit(ptr + len, -1);
3992}
3993
3994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3996 * (depending on flag) starting from buf and looking for a non-scanned
3997 * buffer (other than curbuf). curbuf is special, if it is called with
3998 * buf=curbuf then it has to be the first call for a given flag/expansion.
3999 *
4000 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4001 */
4002 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004003ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
4005#ifdef FEAT_WINDOWS
4006 static win_T *wp;
4007#endif
4008
4009 if (flag == 'w') /* just windows */
4010 {
4011#ifdef FEAT_WINDOWS
4012 if (buf == curbuf) /* first call for this flag/expansion */
4013 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004014 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 && wp->w_buffer->b_scanned)
4016 ;
4017 buf = wp->w_buffer;
4018#else
4019 buf = curbuf;
4020#endif
4021 }
4022 else
4023 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4024 * (unlisted buffers)
4025 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004026 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 && ((flag == 'U'
4028 ? buf->b_p_bl
4029 : (!buf->b_p_bl
4030 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004031 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 ;
4033 return buf;
4034}
4035
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004036#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004037static void expand_by_function(int type, char_u *base);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004038
4039/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004040 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004041 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004042 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004044expand_by_function(
4045 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4046 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004047{
Bram Moolenaar82139082011-09-14 16:52:09 +02004048 list_T *matchlist = NULL;
4049 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004050 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004051 char_u *funcname;
4052 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004053 win_T *curwin_save;
4054 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004055 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004056
Bram Moolenaare344bea2005-09-01 20:46:49 +00004057 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4058 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004059 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004060
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004061 /* Call 'completefunc' to obtain the list of matches. */
4062 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004063 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004064
Bram Moolenaare344bea2005-09-01 20:46:49 +00004065 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004066 curwin_save = curwin;
4067 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004068
4069 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004070 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004071 {
4072 switch (rettv.v_type)
4073 {
4074 case VAR_LIST:
4075 matchlist = rettv.vval.v_list;
4076 break;
4077 case VAR_DICT:
4078 matchdict = rettv.vval.v_dict;
4079 break;
4080 default:
4081 /* TODO: Give error message? */
4082 clear_tv(&rettv);
4083 break;
4084 }
4085 }
4086
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004087 if (curwin_save != curwin || curbuf_save != curbuf)
4088 {
4089 EMSG(_(e_complwin));
4090 goto theend;
4091 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004092 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004093 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004094 if (!equalpos(curwin->w_cursor, pos))
4095 {
4096 EMSG(_(e_compldel));
4097 goto theend;
4098 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004099
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004100 if (matchlist != NULL)
4101 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004102 else if (matchdict != NULL)
4103 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004104
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004105theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004106 if (matchdict != NULL)
4107 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004108 if (matchlist != NULL)
4109 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004110}
4111#endif /* FEAT_COMPL_FUNC */
4112
Bram Moolenaar39f05632006-03-19 22:15:26 +00004113#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004114/*
4115 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004116 */
4117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004118ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004119{
4120 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004121 int dir = compl_direction;
4122
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004123 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004124 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004125 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004126 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4127 /* if dir was BACKWARD then honor it just once */
4128 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004129 else if (did_emsg)
4130 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004131 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004132}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004133
4134/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004135 * Add completions from a dict.
4136 */
4137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004138ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004139{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004140 dictitem_T *di_refresh;
4141 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004142
4143 /* Check for optional "refresh" item. */
4144 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004145 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4146 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004147 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004148 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004149
4150 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4151 compl_opt_refresh_always = TRUE;
4152 }
4153
4154 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004155 di_words = dict_find(dict, (char_u *)"words", 5);
4156 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4157 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004158}
4159
4160/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004161 * Add a match to the list of matches from a typeval_T.
4162 * If the given string is already in the list of completions, then return
4163 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4164 * maybe because alloc() returns NULL, then FAIL is returned.
4165 */
4166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004167ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004168{
4169 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004170 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004171 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004172 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004173 char_u *(cptext[CPT_COUNT]);
4174
4175 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4176 {
4177 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4178 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4179 (char_u *)"abbr", FALSE);
4180 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4181 (char_u *)"menu", FALSE);
4182 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4183 (char_u *)"kind", FALSE);
4184 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4185 (char_u *)"info", FALSE);
4186 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4187 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004188 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004189 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004190 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4191 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004192 }
4193 else
4194 {
4195 word = get_tv_string_chk(tv);
4196 vim_memset(cptext, 0, sizeof(cptext));
4197 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004198 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004199 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004200 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004201}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004202#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004203
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004204/*
4205 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004206 * The search starts at position "ini" in curbuf and in the direction
4207 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004208 * When "compl_started" is FALSE start at that position, otherwise continue
4209 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210 * This may return before finding all the matches.
4211 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 */
4213 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004214ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215{
4216 static pos_T first_match_pos;
4217 static pos_T last_match_pos;
4218 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004219 static int found_all = FALSE; /* Found all matches of a
4220 certain type. */
4221 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
Bram Moolenaar572cb562005-08-05 21:35:02 +00004223 pos_T *pos;
4224 char_u **matches;
4225 int save_p_scs;
4226 int save_p_ws;
4227 int save_p_ic;
4228 int i;
4229 int num_matches;
4230 int len;
4231 int found_new_match;
4232 int type = ctrl_x_mode;
4233 char_u *ptr;
4234 char_u *dict = NULL;
4235 int dict_f = 0;
4236 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004237 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004239 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {
4241 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4242 ins_buf->b_scanned = 0;
4243 found_all = FALSE;
4244 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004245 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004246 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 last_match_pos = first_match_pos = *ini;
4248 }
4249
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004250 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004251 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4253 for (;;)
4254 {
4255 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004256 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004258 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 * or if found_all says this entry is done. For ^X^L only use the
4260 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004261 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004262 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 {
4264 found_all = FALSE;
4265 while (*e_cpt == ',' || *e_cpt == ' ')
4266 e_cpt++;
4267 if (*e_cpt == '.' && !curbuf->b_scanned)
4268 {
4269 ins_buf = curbuf;
4270 first_match_pos = *ini;
4271 /* So that ^N can match word immediately after cursor */
4272 if (ctrl_x_mode == 0)
4273 dec(&first_match_pos);
4274 last_match_pos = first_match_pos;
4275 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004276
4277 /* Remember the first match so that the loop stops when we
4278 * wrap and come back there a second time. */
4279 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4282 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4283 {
4284 /* Scan a buffer, but not the current one. */
4285 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4286 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004287 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 first_match_pos.col = last_match_pos.col = 0;
4289 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4290 last_match_pos.lnum = 0;
4291 type = 0;
4292 }
4293 else /* unloaded buffer, scan like dictionary */
4294 {
4295 found_all = TRUE;
4296 if (ins_buf->b_fname == NULL)
4297 continue;
4298 type = CTRL_X_DICTIONARY;
4299 dict = ins_buf->b_fname;
4300 dict_f = DICT_EXACT;
4301 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004302 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 ins_buf->b_fname == NULL
4304 ? buf_spname(ins_buf)
4305 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004306 ? ins_buf->b_fname
4307 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004308 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
4310 else if (*e_cpt == NUL)
4311 break;
4312 else
4313 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004314 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 type = -1;
4316 else if (*e_cpt == 'k' || *e_cpt == 's')
4317 {
4318 if (*e_cpt == 'k')
4319 type = CTRL_X_DICTIONARY;
4320 else
4321 type = CTRL_X_THESAURUS;
4322 if (*++e_cpt != ',' && *e_cpt != NUL)
4323 {
4324 dict = e_cpt;
4325 dict_f = DICT_FIRST;
4326 }
4327 }
4328#ifdef FEAT_FIND_ID
4329 else if (*e_cpt == 'i')
4330 type = CTRL_X_PATH_PATTERNS;
4331 else if (*e_cpt == 'd')
4332 type = CTRL_X_PATH_DEFINES;
4333#endif
4334 else if (*e_cpt == ']' || *e_cpt == 't')
4335 {
4336 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004337 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004338 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340 else
4341 type = -1;
4342
4343 /* in any case e_cpt is advanced to the next entry */
4344 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4345
4346 found_all = TRUE;
4347 if (type == -1)
4348 continue;
4349 }
4350 }
4351
4352 switch (type)
4353 {
4354 case -1:
4355 break;
4356#ifdef FEAT_FIND_ID
4357 case CTRL_X_PATH_PATTERNS:
4358 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004359 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004360 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004362 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4364 (linenr_T)1, (linenr_T)MAXLNUM);
4365 break;
4366#endif
4367
4368 case CTRL_X_DICTIONARY:
4369 case CTRL_X_THESAURUS:
4370 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004371 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 : (type == CTRL_X_THESAURUS
4373 ? (*curbuf->b_p_tsr == NUL
4374 ? p_tsr
4375 : curbuf->b_p_tsr)
4376 : (*curbuf->b_p_dict == NUL
4377 ? p_dict
4378 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004379 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004380 dict != NULL ? dict_f
4381 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 dict = NULL;
4383 break;
4384
4385 case CTRL_X_TAGS:
4386 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4387 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004388 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004390 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391 * of matches is found when compl_pattern is empty */
4392 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4394 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4395 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4396 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004397 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 }
4399 p_ic = save_p_ic;
4400 break;
4401
4402 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004403 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4405 {
4406
4407 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004408 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004409 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411 break;
4412
4413 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004414 if (expand_cmdline(&compl_xp, compl_pattern,
4415 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004417 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 break;
4419
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004420#ifdef FEAT_COMPL_FUNC
4421 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004422 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004423 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004424 break;
4425#endif
4426
Bram Moolenaar488c6512005-08-11 20:09:58 +00004427 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004428#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004429 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004430 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004431 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004432 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004433#endif
4434 break;
4435
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 default: /* normal ^P/^N and ^X^L */
4437 /*
4438 * If 'infercase' is set, don't use 'smartcase' here
4439 */
4440 save_p_scs = p_scs;
4441 if (ins_buf->b_p_inf)
4442 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004443
Bram Moolenaar361aa502014-01-23 22:45:58 +01004444 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 * end but never from the middle, thus setting nowrapscan in this
4446 * buffers is a good idea, on the other hand, we always set
4447 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4448 save_p_ws = p_ws;
4449 if (ins_buf != curbuf)
4450 p_ws = FALSE;
4451 else if (*e_cpt == '.')
4452 p_ws = TRUE;
4453 for (;;)
4454 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004455 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004457 ++msg_silent; /* Don't want messages for wrapscan. */
4458
Bram Moolenaare4214502015-03-05 18:08:43 +01004459 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4460 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004461 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004462 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004465 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004467 found_new_match = searchit(NULL, ins_buf, pos,
4468 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004469 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004470 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004471 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004472 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004474 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004475 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 first_match_pos = *pos;
4477 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004478 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
4480 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004481 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 found_new_match = FAIL;
4483 if (found_new_match == FAIL)
4484 {
4485 if (ins_buf == curbuf)
4486 found_all = TRUE;
4487 break;
4488 }
4489
4490 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004491 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 && ini->lnum == pos->lnum
4493 && ini->col == pos->col)
4494 continue;
4495 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004496 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004498 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 {
4500 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4501 continue;
4502 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4503 if (!p_paste)
4504 ptr = skipwhite(ptr);
4505 }
4506 len = (int)STRLEN(ptr);
4507 }
4508 else
4509 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004510 char_u *tmp_ptr = ptr;
4511
4512 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004514 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 /* Skip if already inside a word. */
4516 if (vim_iswordp(tmp_ptr))
4517 continue;
4518 /* Find start of next word. */
4519 tmp_ptr = find_word_start(tmp_ptr);
4520 }
4521 /* Find end of this word. */
4522 tmp_ptr = find_word_end(tmp_ptr);
4523 len = (int)(tmp_ptr - ptr);
4524
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004525 if ((compl_cont_status & CONT_ADDING)
4526 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 {
4528 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4529 {
4530 /* Try next line, if any. the new word will be
4531 * "join" as if the normal command "J" was used.
4532 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004533 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 * works -- Acevedo */
4535 STRNCPY(IObuff, ptr, len);
4536 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4537 tmp_ptr = ptr = skipwhite(ptr);
4538 /* Find start of next word. */
4539 tmp_ptr = find_word_start(tmp_ptr);
4540 /* Find end of next word. */
4541 tmp_ptr = find_word_end(tmp_ptr);
4542 if (tmp_ptr > ptr)
4543 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004544 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004546 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 IObuff[len++] = ' ';
4548 /* IObuf =~ "\k.* ", thus len >= 2 */
4549 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004550 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 || (vim_strchr(p_cpo, CPO_JOINSP)
4552 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004553 && (IObuff[len - 2] == '?'
4554 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 IObuff[len++] = ' ';
4556 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004557 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (tmp_ptr - ptr >= IOSIZE - len)
4559 tmp_ptr = ptr + IOSIZE - len - 1;
4560 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4561 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004562 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
4564 IObuff[len] = NUL;
4565 ptr = IObuff;
4566 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004567 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 continue;
4569 }
4570 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004571 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004572 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004573 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
4575 found_new_match = OK;
4576 break;
4577 }
4578 }
4579 p_scs = save_p_scs;
4580 p_ws = save_p_ws;
4581 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004582
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004583 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004584 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004585 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 found_new_match = OK;
4587
4588 /* break the loop for specialized modes (use 'complete' just for the
4589 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004590 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004591 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004592 {
4593 if (got_int)
4594 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004595 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004596 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004597 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004598
Bram Moolenaare4214502015-03-05 18:08:43 +01004599 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004600 || compl_interrupted)
4601 break;
4602 compl_started = TRUE;
4603 }
4604 else
4605 {
4606 /* Mark a buffer scanned when it has been scanned completely */
4607 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4608 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004610 compl_started = FALSE;
4611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004613 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
Bram Moolenaare4214502015-03-05 18:08:43 +01004615 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 && *e_cpt == NUL) /* Got to end of 'complete' */
4617 found_new_match = FAIL;
4618
4619 i = -1; /* total of matches, unknown */
4620 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004621 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 i = ins_compl_make_cyclic();
4623
4624 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004625 * just been made cyclic then we have to move compl_curr_match to the next
4626 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004627 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4628 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004629 if (compl_curr_match == NULL)
4630 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 return i;
4632}
4633
4634/* Delete the old text being completed. */
4635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004636ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637{
4638 int i;
4639
4640 /*
4641 * In insert mode: Delete the typed part.
4642 * In replace mode: Put the old characters back, if any.
4643 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004644 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 backspace_until_column(i);
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004646
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004647 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4648 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004650 /* clear v:completed_item */
4651 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652}
4653
4654/* Insert the new text being completed. */
4655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004656ins_compl_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004658 dict_T *dict;
4659
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004660 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004661 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4662 compl_used_match = FALSE;
4663 else
4664 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004665
4666 /* Set completed item. */
4667 /* { word, abbr, menu, kind, info } */
4668 dict = dict_alloc();
4669 if (dict != NULL)
4670 {
4671 dict_add_nr_str(dict, "word", 0L,
4672 EMPTY_IF_NULL(compl_shown_match->cp_str));
4673 dict_add_nr_str(dict, "abbr", 0L,
4674 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4675 dict_add_nr_str(dict, "menu", 0L,
4676 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4677 dict_add_nr_str(dict, "kind", 0L,
4678 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4679 dict_add_nr_str(dict, "info", 0L,
4680 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4681 }
4682 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar67081e52016-07-09 21:49:03 +02004683 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684}
4685
4686/*
4687 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004688 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4689 * get more completions. If it is FALSE, then we just do nothing when there
4690 * are no more completions in a given direction. The latter case is used when
4691 * we are still in the middle of finding completions, to allow browsing
4692 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 * Return the total number of matches, or -1 if still unknown -- webb.
4694 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004695 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4696 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 *
4698 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004699 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4700 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 */
4702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004703ins_compl_next(
4704 int allow_get_expansion,
4705 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004706 be at least 1 */
Bram Moolenaar7454a062016-01-30 15:14:10 +01004707 int insert_match) /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708{
4709 int num_matches = -1;
4710 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004711 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004712 compl_T *found_compl = NULL;
4713 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004714 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004715 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004717 /* When user complete function return -1 for findstart which is next
4718 * time of 'always', compl_shown_match become NULL. */
4719 if (compl_shown_match == NULL)
4720 return -1;
4721
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004722 if (compl_leader != NULL
4723 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004725 /* Set "compl_shown_match" to the actually shown match, it may differ
4726 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004727 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004728 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004729 && compl_shown_match->cp_next != NULL
4730 && compl_shown_match->cp_next != compl_first_match)
4731 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004732
4733 /* If we didn't find it searching forward, and compl_shows_dir is
4734 * backward, find the last match. */
4735 if (compl_shows_dir == BACKWARD
4736 && !ins_compl_equal(compl_shown_match,
4737 compl_leader, (int)STRLEN(compl_leader))
4738 && (compl_shown_match->cp_next == NULL
4739 || compl_shown_match->cp_next == compl_first_match))
4740 {
4741 while (!ins_compl_equal(compl_shown_match,
4742 compl_leader, (int)STRLEN(compl_leader))
4743 && compl_shown_match->cp_prev != NULL
4744 && compl_shown_match->cp_prev != compl_first_match)
4745 compl_shown_match = compl_shown_match->cp_prev;
4746 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004747 }
4748
4749 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004750 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 /* Delete old text to be replaced */
4752 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004753
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004754 /* When finding the longest common text we stick at the original text,
4755 * don't let CTRL-N or CTRL-P move to the first match. */
4756 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4757
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004758 /* When restarting the search don't insert the first match either. */
4759 if (compl_restarting)
4760 {
4761 advance = FALSE;
4762 compl_restarting = FALSE;
4763 }
4764
Bram Moolenaare3226be2005-12-18 22:10:00 +00004765 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4766 * around. */
4767 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004769 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004771 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004772 found_end = (compl_first_match != NULL
4773 && (compl_shown_match->cp_next == compl_first_match
4774 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004775 }
4776 else if (compl_shows_dir == BACKWARD
4777 && compl_shown_match->cp_prev != NULL)
4778 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004779 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004780 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004781 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
4783 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004784 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004785 if (!allow_get_expansion)
4786 {
4787 if (advance)
4788 {
4789 if (compl_shows_dir == BACKWARD)
4790 compl_pending -= todo + 1;
4791 else
4792 compl_pending += todo + 1;
4793 }
4794 return -1;
4795 }
4796
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004797 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004798 {
4799 if (compl_shows_dir == BACKWARD)
4800 --compl_pending;
4801 else
4802 ++compl_pending;
4803 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004804
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004805 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004806 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004807
4808 /* handle any pending completions */
4809 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004810 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004811 {
4812 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4813 {
4814 compl_shown_match = compl_shown_match->cp_next;
4815 --compl_pending;
4816 }
4817 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4818 {
4819 compl_shown_match = compl_shown_match->cp_prev;
4820 ++compl_pending;
4821 }
4822 else
4823 break;
4824 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004825 found_end = FALSE;
4826 }
4827 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4828 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004829 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004830 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004831 ++todo;
4832 else
4833 /* Remember a matching item. */
4834 found_compl = compl_shown_match;
4835
4836 /* Stop at the end of the list when we found a usable match. */
4837 if (found_end)
4838 {
4839 if (found_compl != NULL)
4840 {
4841 compl_shown_match = found_compl;
4842 break;
4843 }
4844 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
4847
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004848 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004849 if (compl_no_insert && !started)
4850 {
4851 ins_bytes(compl_orig_text + ins_compl_len());
4852 compl_used_match = FALSE;
4853 }
4854 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004855 {
4856 if (!compl_get_longest || compl_used_match)
4857 ins_compl_insert();
4858 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004859 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004860 }
4861 else
4862 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
4864 if (!allow_get_expansion)
4865 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004866 /* may undisplay the popup menu first */
4867 ins_compl_upd_pum();
4868
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004869 /* redraw to show the user what was inserted */
4870 update_screen(0);
4871
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004872 /* display the updated popup menu */
4873 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004874#ifdef FEAT_GUI
4875 if (gui.in_use)
4876 {
4877 /* Show the cursor after the match, not after the redrawn text. */
4878 setcursor();
4879 out_flush();
4880 gui_update_cursor(FALSE, FALSE);
4881 }
4882#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 /* Delete old text to be replaced, since we're still searching and
4885 * don't want to match ourselves! */
4886 ins_compl_delete();
4887 }
4888
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004889 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004890 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004891 if (compl_no_insert && !started)
4892 compl_enter_selects = TRUE;
4893 else
4894 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004895
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 /*
4897 * Show the file name for the match (if any)
4898 * Truncate the file name to avoid a wait for return.
4899 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004900 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 {
4902 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004903 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 if (i <= 0)
4905 i = 0;
4906 else
4907 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004908 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 msg(IObuff);
4910 redraw_cmdline = FALSE; /* don't overwrite! */
4911 }
4912
4913 return num_matches;
4914}
4915
4916/*
4917 * Call this while finding completions, to check whether the user has hit a key
4918 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004919 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004921 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 */
4923 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004924ins_compl_check_keys(int frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
4926 static int count = 0;
4927
4928 int c;
4929
4930 /* Don't check when reading keys from a script. That would break the test
4931 * scripts */
4932 if (using_script())
4933 return;
4934
4935 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004936 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 return;
4938 count = 0;
4939
Bram Moolenaara260a972006-06-23 19:36:29 +00004940 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4941 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 if (c != NUL)
4944 {
4945 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4946 {
4947 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004948 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004949 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4950 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004952 else
4953 {
4954 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004955 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004956 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004957 if (c != K_IGNORE)
4958 {
4959 /* Don't interrupt completion when the character wasn't typed,
4960 * e.g., when doing @q to replay keys. */
4961 if (c != Ctrl_R && KeyTyped)
4962 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004963
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004964 vungetc(c);
4965 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004968 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00004969 {
4970 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4971
4972 compl_pending = 0;
4973 (void)ins_compl_next(FALSE, todo, TRUE);
4974 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004975}
4976
4977/*
4978 * Decide the direction of Insert mode complete from the key typed.
4979 * Returns BACKWARD or FORWARD.
4980 */
4981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004982ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004983{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004984 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02004985 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004986 return BACKWARD;
4987 return FORWARD;
4988}
4989
4990/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004991 * Return TRUE for keys that are used for completion only when the popup menu
4992 * is visible.
4993 */
4994 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004995ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004996{
4997 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004998 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4999 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005000}
5001
5002/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005003 * Decide the number of completions to move forward.
5004 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5005 */
5006 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005007ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005008{
5009 int h;
5010
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005011 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005012 {
5013 h = pum_get_height();
5014 if (h > 3)
5015 h -= 2; /* keep some context */
5016 return h;
5017 }
5018 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019}
5020
5021/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005022 * Return TRUE if completion with "c" should insert the match, FALSE if only
5023 * to change the currently selected completion.
5024 */
5025 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005026ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005027{
5028 switch (c)
5029 {
5030 case K_UP:
5031 case K_DOWN:
5032 case K_PAGEDOWN:
5033 case K_KPAGEDOWN:
5034 case K_S_DOWN:
5035 case K_PAGEUP:
5036 case K_KPAGEUP:
5037 case K_S_UP:
5038 return FALSE;
5039 }
5040 return TRUE;
5041}
5042
5043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 * Do Insert mode completion.
5045 * Called when character "c" was typed, which has a meaning for completion.
5046 * Returns OK if completion was done, FAIL if something failed (out of mem).
5047 */
5048 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005049ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005051 char_u *line;
5052 int startcol = 0; /* column where searched text starts */
5053 colnr_T curs_col; /* cursor column */
5054 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005055 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056
Bram Moolenaare3226be2005-12-18 22:10:00 +00005057 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005058 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
5060 /* First time we hit ^N or ^P (in a row, I mean) */
5061
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 did_ai = FALSE;
5063#ifdef FEAT_SMARTINDENT
5064 did_si = FALSE;
5065 can_si = FALSE;
5066 can_si_back = FALSE;
5067#endif
5068 if (stop_arrow() == FAIL)
5069 return FAIL;
5070
5071 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005072 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005073 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005075 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005076 * "compl_startpos" to the cursor as a pattern to add a new word
5077 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005078 * "compl_startpos" is not in the same line as the cursor then fix it
5079 * (the line has been split because it was longer than 'tw'). if SOL
5080 * is set then skip the previous pattern, a word at the beginning of
5081 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005082 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5083 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085 /*
5086 * it is a continued search
5087 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005088 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5090 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5091 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005092 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005094 /* line (probably) wrapped, set compl_startpos to the
5095 * first non_blank in the line, if it is not a wordchar
5096 * include it to get a better pattern, but then we don't
5097 * want the "\\<" prefix, check it bellow */
5098 compl_col = (colnr_T)(skipwhite(line) - line);
5099 compl_startpos.col = compl_col;
5100 compl_startpos.lnum = curwin->w_cursor.lnum;
5101 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
5103 else
5104 {
5105 /* S_IPOS was set when we inserted a word that was at the
5106 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005107 * mode but first we need to redefine compl_startpos */
5108 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005110 compl_cont_status |= CONT_SOL;
5111 compl_startpos.col = (colnr_T)(skipwhite(
5112 line + compl_length
5113 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005115 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005117 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005118 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005119 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005121 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005123 compl_cont_status &= ~CONT_SOL;
5124 compl_length = (IOSIZE - MIN_SPACE);
5125 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005127 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5128 if (compl_length < 1)
5129 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005131 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005132 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005134 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005137 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005139 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005141 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005143 compl_cont_status = 0;
5144 compl_cont_status |= CONT_N_ADDS;
5145 compl_startpos = curwin->w_cursor;
5146 startcol = (int)curs_col;
5147 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 }
5149
5150 /* Work out completion pattern and original text -- webb */
5151 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5152 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005153 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5155 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005156 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005158 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005160 compl_col += ++startcol;
5161 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005164 compl_pattern = str_foldcase(line + compl_col,
5165 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 compl_pattern = vim_strnsave(line + compl_col,
5168 compl_length);
5169 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 return FAIL;
5171 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005172 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
5174 char_u *prefix = (char_u *)"\\<";
5175
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005176 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005177 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005178 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005179 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005181 if (!vim_iswordp(line + compl_col)
5182 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 && (
5184#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005185 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005187 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188#endif
5189 )))
5190 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005191 STRCPY((char *)compl_pattern, prefix);
5192 (void)quote_meta(compl_pattern + STRLEN(prefix),
5193 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005195 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005197 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005199 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200#endif
5201 )
5202 {
5203 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005204 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5205 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005207 compl_col += curs_col;
5208 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210 else
5211 {
5212#ifdef FEAT_MBYTE
5213 /* Search the point of change class of multibyte character
5214 * or not a word single byte character backward. */
5215 if (has_mbyte)
5216 {
5217 int base_class;
5218 int head_off;
5219
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005220 startcol -= (*mb_head_off)(line, line + startcol);
5221 base_class = mb_get_class(line + startcol);
5222 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005224 head_off = (*mb_head_off)(line, line + startcol);
5225 if (base_class != mb_get_class(line + startcol
5226 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230 }
5231 else
5232#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005233 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005235 compl_col += ++startcol;
5236 compl_length = (int)curs_col - startcol;
5237 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
5239 /* Only match word with at least two chars -- webb
5240 * there's no need to call quote_meta,
5241 * alloc(7) is enough -- Acevedo
5242 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005243 compl_pattern = alloc(7);
5244 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005246 STRCPY((char *)compl_pattern, "\\<");
5247 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5248 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250 else
5251 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005252 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005253 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005254 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005256 STRCPY((char *)compl_pattern, "\\<");
5257 (void)quote_meta(compl_pattern + 2, line + compl_col,
5258 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 }
5260 }
5261 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005262 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005264 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005265 compl_length = (int)curs_col - (int)compl_col;
5266 if (compl_length < 0) /* cursor in indent: empty pattern */
5267 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005269 compl_pattern = str_foldcase(line + compl_col, compl_length,
5270 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005272 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5273 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 return FAIL;
5275 }
5276 else if (ctrl_x_mode == CTRL_X_FILES)
5277 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005278 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005279 if (startcol > 0)
5280 {
5281 char_u *p = line + startcol;
5282
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005283 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005284 while (p > line && vim_isfilec(PTR2CHAR(p)))
5285 mb_ptr_back(line, p);
5286 if (p == line && vim_isfilec(PTR2CHAR(p)))
5287 startcol = 0;
5288 else
5289 startcol = (int)(p - line) + 1;
5290 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005291
Bram Moolenaar0300e462013-09-08 16:03:45 +02005292 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005293 compl_length = (int)curs_col - startcol;
5294 compl_pattern = addstar(line + compl_col, compl_length,
5295 EXPAND_FILES);
5296 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 return FAIL;
5298 }
5299 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5300 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 compl_pattern = vim_strnsave(line, curs_col);
5302 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005304 set_cmd_context(&compl_xp, compl_pattern,
5305 (int)STRLEN(compl_pattern), curs_col);
5306 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5307 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005308 /* No completion possible, use an empty pattern to get a
5309 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005310 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005311 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005312 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5313 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005315 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005316 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005317#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005318 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005319 * Call user defined function 'completefunc' with "a:findstart"
5320 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005321 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005322 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005323 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005324 char_u *funcname;
5325 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005326 win_T *curwin_save;
5327 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005328
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005329 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005330 * string */
5331 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5332 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5333 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005334 {
5335 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5336 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005337 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005338 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005339
5340 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005341 args[1] = NULL;
5342 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005343 curwin_save = curwin;
5344 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005345 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005346 if (curwin_save != curwin || curbuf_save != curbuf)
5347 {
5348 EMSG(_(e_complwin));
5349 return FAIL;
5350 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005351 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005352 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005353 if (!equalpos(curwin->w_cursor, pos))
5354 {
5355 EMSG(_(e_compldel));
5356 return FAIL;
5357 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005358
Bram Moolenaar6110a002012-01-26 18:58:38 +01005359 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005360 * cancel the complete without an error.
5361 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005362 if (col == -2)
5363 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005364 if (col == -3)
5365 {
5366 ctrl_x_mode = 0;
5367 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005368 if (!shortmess(SHM_COMPLETIONMENU))
5369 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005370 return FAIL;
5371 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005372
Bram Moolenaar82139082011-09-14 16:52:09 +02005373 /*
5374 * Reset extended parameters of completion, when start new
5375 * completion.
5376 */
5377 compl_opt_refresh_always = FALSE;
5378
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005379 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005380 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005381 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005382 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005383 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005384
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005385 /* Setup variables for completion. Need to obtain "line" again,
5386 * it may have become invalid. */
5387 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005388 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005389 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5390 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005391#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005392 return FAIL;
5393 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005394 else if (ctrl_x_mode == CTRL_X_SPELL)
5395 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005396#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005397 if (spell_bad_len > 0)
5398 compl_col = curs_col - spell_bad_len;
5399 else
5400 compl_col = spell_word_start(startcol);
5401 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005402 {
5403 compl_length = 0;
5404 compl_col = curs_col;
5405 }
5406 else
5407 {
5408 spell_expand_check_cap(compl_col);
5409 compl_length = (int)curs_col - compl_col;
5410 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005411 /* Need to obtain "line" again, it may have become invalid. */
5412 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005413 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5414 if (compl_pattern == NULL)
5415#endif
5416 return FAIL;
5417 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005418 else
5419 {
5420 EMSG2(_(e_intern2), "ins_complete()");
5421 return FAIL;
5422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005424 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {
5426 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005427 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {
5429 /* Insert a new line, keep indentation but ignore 'comments' */
5430#ifdef FEAT_COMMENTS
5431 char_u *old = curbuf->b_p_com;
5432
5433 curbuf->b_p_com = (char_u *)"";
5434#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005435 compl_startpos.lnum = curwin->w_cursor.lnum;
5436 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 ins_eol('\r');
5438#ifdef FEAT_COMMENTS
5439 curbuf->b_p_com = old;
5440#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005441 compl_length = 0;
5442 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
5444 }
5445 else
5446 {
5447 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005448 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 }
5450
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005451 if (compl_cont_status & CONT_LOCAL)
5452 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 else
5454 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5455
Bram Moolenaar62951b12011-09-21 18:23:05 +02005456 /* If any of the original typed text has been changed we need to fix
5457 * the redo buffer. */
5458 ins_compl_fixRedoBufForLeader(NULL);
5459
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005460 /* Always add completion for the original text. */
5461 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005462 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5463 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005464 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005466 vim_free(compl_pattern);
5467 compl_pattern = NULL;
5468 vim_free(compl_orig_text);
5469 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 return FAIL;
5471 }
5472
5473 /* showmode might reset the internal line pointers, so it must
5474 * be called before line = ml_get(), or when this address is no
5475 * longer needed. -- Acevedo.
5476 */
5477 edit_submode_extra = (char_u *)_("-- Searching...");
5478 edit_submode_highl = HLF_COUNT;
5479 showmode();
5480 edit_submode_extra = NULL;
5481 out_flush();
5482 }
5483
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005484 compl_shown_match = compl_curr_match;
5485 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
5487 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005488 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005490 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005491 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005493 /* may undisplay the popup menu */
5494 ins_compl_upd_pum();
5495
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005496 if (n > 1) /* all matches have been found */
5497 compl_matches = n;
5498 compl_curr_match = compl_shown_match;
5499 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500
Bram Moolenaard68071d2006-05-02 22:08:30 +00005501 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5502 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 if (got_int && !global_busy)
5504 {
5505 (void)vgetc();
5506 got_int = FALSE;
5507 }
5508
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005509 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005510 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005512 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5513 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5515 edit_submode_highl = HLF_E;
5516 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5517 * because we couldn't expand anything at first place, but if we used
5518 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5519 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005520 if ( compl_length > 1
5521 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 || (ctrl_x_mode != 0
5523 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5524 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005525 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 }
5527
Bram Moolenaar572cb562005-08-05 21:35:02 +00005528 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005529 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005531 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
5533 if (edit_submode_extra == NULL)
5534 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005535 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
5537 edit_submode_extra = (char_u *)_("Back at original");
5538 edit_submode_highl = HLF_W;
5539 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005540 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
5542 edit_submode_extra = (char_u *)_("Word from other line");
5543 edit_submode_highl = HLF_COUNT;
5544 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005545 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 {
5547 edit_submode_extra = (char_u *)_("The only match");
5548 edit_submode_highl = HLF_COUNT;
5549 }
5550 else
5551 {
5552 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005553 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005555 int number = 0;
5556 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005558 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 {
5560 /* search backwards for the first valid (!= -1) number.
5561 * This should normally succeed already at the first loop
5562 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005563 for (match = compl_curr_match->cp_prev; match != NULL
5564 && match != compl_first_match;
5565 match = match->cp_prev)
5566 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005568 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 break;
5570 }
5571 if (match != NULL)
5572 /* go up and assign all numbers which are not assigned
5573 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005574 for (match = match->cp_next;
5575 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005576 match = match->cp_next)
5577 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 }
5579 else /* BACKWARD */
5580 {
5581 /* search forwards (upwards) for the first valid (!= -1)
5582 * number. This should normally succeed already at the
5583 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005584 for (match = compl_curr_match->cp_next; match != NULL
5585 && match != compl_first_match;
5586 match = match->cp_next)
5587 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005589 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 break;
5591 }
5592 if (match != NULL)
5593 /* go down and assign all numbers which are not
5594 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005595 for (match = match->cp_prev; match
5596 && match->cp_number == -1;
5597 match = match->cp_prev)
5598 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 }
5600 }
5601
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005602 /* The match should always have a sequence number now, this is
5603 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005604 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005606 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5607 * Translations may need more than twice that. */
5608 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005610 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005611 vim_snprintf((char *)match_ref, sizeof(match_ref),
5612 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005613 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005615 vim_snprintf((char *)match_ref, sizeof(match_ref),
5616 _("match %d"),
5617 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 edit_submode_extra = match_ref;
5619 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005620 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 curs_columns(FALSE);
5622 }
5623 }
5624 }
5625
5626 /* Show a message about what (completion) mode we're in. */
5627 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005628 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005630 if (edit_submode_extra != NULL)
5631 {
5632 if (!p_smd)
5633 msg_attr(edit_submode_extra,
5634 edit_submode_highl < HLF_COUNT
5635 ? hl_attr(edit_submode_highl) : 0);
5636 }
5637 else
5638 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640
Bram Moolenaard68071d2006-05-02 22:08:30 +00005641 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005642 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005643 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005644 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005645 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005646 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005647 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 return OK;
5650}
5651
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005652 static void
5653show_pum(int save_w_wrow)
5654{
5655 /* RedrawingDisabled may be set when invoked through complete(). */
5656 int n = RedrawingDisabled;
5657
5658 RedrawingDisabled = 0;
5659
5660 /* If the cursor moved we need to remove the pum first. */
5661 setcursor();
5662 if (save_w_wrow != curwin->w_wrow)
5663 ins_compl_del_pum();
5664
5665 ins_compl_show_pum();
5666 setcursor();
5667 RedrawingDisabled = n;
5668}
5669
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670/*
5671 * Looks in the first "len" chars. of "src" for search-metachars.
5672 * If dest is not NULL the chars. are copied there quoting (with
5673 * a backslash) the metachars, and dest would be NUL terminated.
5674 * Returns the length (needed) of dest
5675 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005676 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005677quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005679 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005681 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 {
5683 switch (*src)
5684 {
5685 case '.':
5686 case '*':
5687 case '[':
5688 if (ctrl_x_mode == CTRL_X_DICTIONARY
5689 || ctrl_x_mode == CTRL_X_THESAURUS)
5690 break;
5691 case '~':
5692 if (!p_magic) /* quote these only if magic is set */
5693 break;
5694 case '\\':
5695 if (ctrl_x_mode == CTRL_X_DICTIONARY
5696 || ctrl_x_mode == CTRL_X_THESAURUS)
5697 break;
5698 case '^': /* currently it's not needed. */
5699 case '$':
5700 m++;
5701 if (dest != NULL)
5702 *dest++ = '\\';
5703 break;
5704 }
5705 if (dest != NULL)
5706 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005707# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 /* Copy remaining bytes of a multibyte character. */
5709 if (has_mbyte)
5710 {
5711 int i, mb_len;
5712
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005713 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 if (mb_len > 0 && len >= mb_len)
5715 for (i = 0; i < mb_len; ++i)
5716 {
5717 --len;
5718 ++src;
5719 if (dest != NULL)
5720 *dest++ = *src;
5721 }
5722 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
5725 if (dest != NULL)
5726 *dest = NUL;
5727
5728 return m;
5729}
5730#endif /* FEAT_INS_EXPAND */
5731
5732/*
5733 * Next character is interpreted literally.
5734 * A one, two or three digit decimal number is interpreted as its byte value.
5735 * If one or two digits are entered, the next character is given to vungetc().
5736 * For Unicode a character > 255 may be returned.
5737 */
5738 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005739get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 int cc;
5742 int nc;
5743 int i;
5744 int hex = FALSE;
5745 int octal = FALSE;
5746#ifdef FEAT_MBYTE
5747 int unicode = 0;
5748#endif
5749
5750 if (got_int)
5751 return Ctrl_C;
5752
5753#ifdef FEAT_GUI
5754 /*
5755 * In GUI there is no point inserting the internal code for a special key.
5756 * It is more useful to insert the string "<KEY>" instead. This would
5757 * probably be useful in a text window too, but it would not be
5758 * vi-compatible (maybe there should be an option for it?) -- webb
5759 */
5760 if (gui.in_use)
5761 ++allow_keys;
5762#endif
5763#ifdef USE_ON_FLY_SCROLL
5764 dont_scroll = TRUE; /* disallow scrolling here */
5765#endif
5766 ++no_mapping; /* don't map the next key hits */
5767 cc = 0;
5768 i = 0;
5769 for (;;)
5770 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005771 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772#ifdef FEAT_CMDL_INFO
5773 if (!(State & CMDLINE)
5774# ifdef FEAT_MBYTE
5775 && MB_BYTE2LEN_CHECK(nc) == 1
5776# endif
5777 )
5778 add_to_showcmd(nc);
5779#endif
5780 if (nc == 'x' || nc == 'X')
5781 hex = TRUE;
5782 else if (nc == 'o' || nc == 'O')
5783 octal = TRUE;
5784#ifdef FEAT_MBYTE
5785 else if (nc == 'u' || nc == 'U')
5786 unicode = nc;
5787#endif
5788 else
5789 {
5790 if (hex
5791#ifdef FEAT_MBYTE
5792 || unicode != 0
5793#endif
5794 )
5795 {
5796 if (!vim_isxdigit(nc))
5797 break;
5798 cc = cc * 16 + hex2nr(nc);
5799 }
5800 else if (octal)
5801 {
5802 if (nc < '0' || nc > '7')
5803 break;
5804 cc = cc * 8 + nc - '0';
5805 }
5806 else
5807 {
5808 if (!VIM_ISDIGIT(nc))
5809 break;
5810 cc = cc * 10 + nc - '0';
5811 }
5812
5813 ++i;
5814 }
5815
5816 if (cc > 255
5817#ifdef FEAT_MBYTE
5818 && unicode == 0
5819#endif
5820 )
5821 cc = 255; /* limit range to 0-255 */
5822 nc = 0;
5823
5824 if (hex) /* hex: up to two chars */
5825 {
5826 if (i >= 2)
5827 break;
5828 }
5829#ifdef FEAT_MBYTE
5830 else if (unicode) /* Unicode: up to four or eight chars */
5831 {
5832 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5833 break;
5834 }
5835#endif
5836 else if (i >= 3) /* decimal or octal: up to three chars */
5837 break;
5838 }
5839 if (i == 0) /* no number entered */
5840 {
5841 if (nc == K_ZERO) /* NUL is stored as NL */
5842 {
5843 cc = '\n';
5844 nc = 0;
5845 }
5846 else
5847 {
5848 cc = nc;
5849 nc = 0;
5850 }
5851 }
5852
5853 if (cc == 0) /* NUL is stored as NL */
5854 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005855#ifdef FEAT_MBYTE
5856 if (enc_dbcs && (cc & 0xff) == 0)
5857 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5858 second byte will cause trouble! */
5859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
5861 --no_mapping;
5862#ifdef FEAT_GUI
5863 if (gui.in_use)
5864 --allow_keys;
5865#endif
5866 if (nc)
5867 vungetc(nc);
5868 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5869 return cc;
5870}
5871
5872/*
5873 * Insert character, taking care of special keys and mod_mask
5874 */
5875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005876insert_special(
5877 int c,
5878 int allow_modmask,
5879 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
5881 char_u *p;
5882 int len;
5883
5884 /*
5885 * Special function key, translate into "<Key>". Up to the last '>' is
5886 * inserted with ins_str(), so as not to replace characters in replace
5887 * mode.
5888 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5889 * unless 'allow_modmask' is TRUE.
5890 */
5891#ifdef MACOS
5892 /* Command-key never produces a normal key */
5893 if (mod_mask & MOD_MASK_CMD)
5894 allow_modmask = TRUE;
5895#endif
5896 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5897 {
5898 p = get_special_key_name(c, mod_mask);
5899 len = (int)STRLEN(p);
5900 c = p[len - 1];
5901 if (len > 2)
5902 {
5903 if (stop_arrow() == FAIL)
5904 return;
5905 p[len - 1] = NUL;
5906 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005907 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 ctrlv = FALSE;
5909 }
5910 }
5911 if (stop_arrow() == OK)
5912 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5913}
5914
5915/*
5916 * Special characters in this context are those that need processing other
5917 * than the simple insertion that can be performed here. This includes ESC
5918 * which terminates the insert, and CR/NL which need special processing to
5919 * open up a new line. This routine tries to optimize insertions performed by
5920 * the "redo", "undo" or "put" commands, so it needs to know when it should
5921 * stop and defer processing to the "normal" mechanism.
5922 * '0' and '^' are special, because they can be followed by CTRL-D.
5923 */
5924#ifdef EBCDIC
5925# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5926#else
5927# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5928#endif
5929
5930#ifdef FEAT_MBYTE
5931# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5932#else
5933# define WHITECHAR(cc) vim_iswhite(cc)
5934#endif
5935
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005936/*
5937 * "flags": INSCHAR_FORMAT - force formatting
5938 * INSCHAR_CTRLV - char typed just after CTRL-V
5939 * INSCHAR_NO_FEX - don't use 'formatexpr'
5940 *
5941 * NOTE: passes the flags value straight through to internal_format() which,
5942 * beside INSCHAR_FORMAT (above), is also looking for these:
5943 * INSCHAR_DO_COM - format comments
5944 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005947insertchar(
5948 int c, /* character to insert or NUL */
5949 int flags, /* INSCHAR_FORMAT, etc. */
5950 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 int textwidth;
5953#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005957 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005959 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961
5962 /*
5963 * Try to break the line in two or more pieces when:
5964 * - Always do this if we have been called to do formatting only.
5965 * - Always do this when 'formatoptions' has the 'a' flag and the line
5966 * ends in white space.
5967 * - Otherwise:
5968 * - Don't do this if inserting a blank
5969 * - Don't do this if an existing character is being replaced, unless
5970 * we're in VREPLACE mode.
5971 * - Do this if the cursor is not on the line where insert started
5972 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5973 * before the insert.
5974 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5975 * before 'textwidth'
5976 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005977 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005978 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 || (!vim_iswhite(c)
5980 && !((State & REPLACE_FLAG)
5981#ifdef FEAT_VREPLACE
5982 && !(State & VREPLACE_FLAG)
5983#endif
5984 && *ml_get_cursor() != NUL)
5985 && (curwin->w_cursor.lnum != Insstart.lnum
5986 || ((!has_format_option(FO_INS_LONG)
5987 || Insstart_textlen <= (colnr_T)textwidth)
5988 && (!fo_ins_blank
5989 || Insstart_blank_vcol <= (colnr_T)textwidth
5990 ))))))
5991 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005992 /* Format with 'formatexpr' when it's set. Use internal formatting
5993 * when 'formatexpr' isn't set or it returns non-zero. */
5994#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005995 int do_internal = TRUE;
5996 colnr_T virtcol = get_nolist_virtcol()
5997 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005998
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005999 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6000 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006001 {
6002 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6003 /* It may be required to save for undo again, e.g. when setline()
6004 * was called. */
6005 ins_need_undo = TRUE;
6006 }
6007 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006009 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006011
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 if (c == NUL) /* only formatting was wanted */
6013 return;
6014
6015#ifdef FEAT_COMMENTS
6016 /* Check whether this character should end a comment. */
6017 if (did_ai && (int)c == end_comment_pending)
6018 {
6019 char_u *line;
6020 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6021 int middle_len, end_len;
6022 int i;
6023
6024 /*
6025 * Need to remove existing (middle) comment leader and insert end
6026 * comment leader. First, check what comment leader we can find.
6027 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006028 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6030 {
6031 /* Skip middle-comment string */
6032 while (*p && p[-1] != ':') /* find end of middle flags */
6033 ++p;
6034 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6035 /* Don't count trailing white space for middle_len */
6036 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6037 --middle_len;
6038
6039 /* Find the end-comment string */
6040 while (*p && p[-1] != ':') /* find end of end flags */
6041 ++p;
6042 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6043
6044 /* Skip white space before the cursor */
6045 i = curwin->w_cursor.col;
6046 while (--i >= 0 && vim_iswhite(line[i]))
6047 ;
6048 i++;
6049
6050 /* Skip to before the middle leader */
6051 i -= middle_len;
6052
6053 /* Check some expected things before we go on */
6054 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6055 {
6056 /* Backspace over all the stuff we want to replace */
6057 backspace_until_column(i);
6058
6059 /*
6060 * Insert the end-comment string, except for the last
6061 * character, which will get inserted as normal later.
6062 */
6063 ins_bytes_len(lead_end, end_len - 1);
6064 }
6065 }
6066 }
6067 end_comment_pending = NUL;
6068#endif
6069
6070 did_ai = FALSE;
6071#ifdef FEAT_SMARTINDENT
6072 did_si = FALSE;
6073 can_si = FALSE;
6074 can_si_back = FALSE;
6075#endif
6076
6077 /*
6078 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6079 * This speeds up normal text input considerably.
6080 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6081 * need to re-indent at a ':', or any other character (but not what
6082 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006083 * Don't do this when there an InsertCharPre autocommand is defined,
6084 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 */
6086#ifdef USE_ON_FLY_SCROLL
6087 dont_scroll = FALSE; /* allow scrolling here */
6088#endif
6089
6090 if ( !ISSPECIAL(c)
6091#ifdef FEAT_MBYTE
6092 && (!has_mbyte || (*mb_char2len)(c) == 1)
6093#endif
6094 && vpeekc() != NUL
6095 && !(State & REPLACE_FLAG)
6096#ifdef FEAT_CINDENT
6097 && !cindent_on()
6098#endif
6099#ifdef FEAT_RIGHTLEFT
6100 && !p_ri
6101#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006102#ifdef FEAT_AUTOCMD
6103 && !has_insertcharpre()
6104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 )
6106 {
6107#define INPUT_BUFLEN 100
6108 char_u buf[INPUT_BUFLEN + 1];
6109 int i;
6110 colnr_T virtcol = 0;
6111
6112 buf[0] = c;
6113 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006114 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 virtcol = get_nolist_virtcol();
6116 /*
6117 * Stop the string when:
6118 * - no more chars available
6119 * - finding a special character (command key)
6120 * - buffer is full
6121 * - running into the 'textwidth' boundary
6122 * - need to check for abbreviation: A non-word char after a word-char
6123 */
6124 while ( (c = vpeekc()) != NUL
6125 && !ISSPECIAL(c)
6126#ifdef FEAT_MBYTE
6127 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6128#endif
6129 && i < INPUT_BUFLEN
6130 && (textwidth == 0
6131 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6132 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6133 {
6134#ifdef FEAT_RIGHTLEFT
6135 c = vgetc();
6136 if (p_hkmap && KeyTyped)
6137 c = hkmap(c); /* Hebrew mode mapping */
6138# ifdef FEAT_FKMAP
6139 if (p_fkmap && KeyTyped)
6140 c = fkmap(c); /* Farsi mode mapping */
6141# endif
6142 buf[i++] = c;
6143#else
6144 buf[i++] = vgetc();
6145#endif
6146 }
6147
6148#ifdef FEAT_DIGRAPHS
6149 do_digraph(-1); /* clear digraphs */
6150 do_digraph(buf[i-1]); /* may be the start of a digraph */
6151#endif
6152 buf[i] = NUL;
6153 ins_str(buf);
6154 if (flags & INSCHAR_CTRLV)
6155 {
6156 redo_literal(*buf);
6157 i = 1;
6158 }
6159 else
6160 i = 0;
6161 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006162 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 }
6164 else
6165 {
6166#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006167 int cc;
6168
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6170 {
6171 char_u buf[MB_MAXBYTES + 1];
6172
6173 (*mb_char2bytes)(c, buf);
6174 buf[cc] = NUL;
6175 ins_char_bytes(buf, cc);
6176 AppendCharToRedobuff(c);
6177 }
6178 else
6179#endif
6180 {
6181 ins_char(c);
6182 if (flags & INSCHAR_CTRLV)
6183 redo_literal(c);
6184 else
6185 AppendCharToRedobuff(c);
6186 }
6187 }
6188}
6189
6190/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006191 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006192 *
6193 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6194 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006195 */
6196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006197internal_format(
6198 int textwidth,
6199 int second_indent,
6200 int flags,
6201 int format_only,
6202 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006203{
6204 int cc;
6205 int save_char = NUL;
6206 int haveto_redraw = FALSE;
6207 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6208#ifdef FEAT_MBYTE
6209 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6210#endif
6211 int fo_white_par = has_format_option(FO_WHITE_PAR);
6212 int first_line = TRUE;
6213#ifdef FEAT_COMMENTS
6214 colnr_T leader_len;
6215 int no_leader = FALSE;
6216 int do_comments = (flags & INSCHAR_DO_COM);
6217#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006218#ifdef FEAT_LINEBREAK
6219 int has_lbr = curwin->w_p_lbr;
6220
6221 /* make sure win_lbr_chartabsize() counts correctly */
6222 curwin->w_p_lbr = FALSE;
6223#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006224
6225 /*
6226 * When 'ai' is off we don't want a space under the cursor to be
6227 * deleted. Replace it with an 'x' temporarily.
6228 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006229 if (!curbuf->b_p_ai
6230#ifdef FEAT_VREPLACE
6231 && !(State & VREPLACE_FLAG)
6232#endif
6233 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006234 {
6235 cc = gchar_cursor();
6236 if (vim_iswhite(cc))
6237 {
6238 save_char = cc;
6239 pchar_cursor('x');
6240 }
6241 }
6242
6243 /*
6244 * Repeat breaking lines, until the current line is not too long.
6245 */
6246 while (!got_int)
6247 {
6248 int startcol; /* Cursor column at entry */
6249 int wantcol; /* column at textwidth border */
6250 int foundcol; /* column for start of spaces */
6251 int end_foundcol = 0; /* column for start of word */
6252 colnr_T len;
6253 colnr_T virtcol;
6254#ifdef FEAT_VREPLACE
6255 int orig_col = 0;
6256 char_u *saved_text = NULL;
6257#endif
6258 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006259 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006260
Bram Moolenaar97b98102009-11-17 16:41:01 +00006261 virtcol = get_nolist_virtcol()
6262 + char2cells(c != NUL ? c : gchar_cursor());
6263 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006264 break;
6265
6266#ifdef FEAT_COMMENTS
6267 if (no_leader)
6268 do_comments = FALSE;
6269 else if (!(flags & INSCHAR_FORMAT)
6270 && has_format_option(FO_WRAP_COMS))
6271 do_comments = TRUE;
6272
6273 /* Don't break until after the comment leader */
6274 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006275 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006276 else
6277 leader_len = 0;
6278
6279 /* If the line doesn't start with a comment leader, then don't
6280 * start one in a following broken line. Avoids that a %word
6281 * moved to the start of the next line causes all following lines
6282 * to start with %. */
6283 if (leader_len == 0)
6284 no_leader = TRUE;
6285#endif
6286 if (!(flags & INSCHAR_FORMAT)
6287#ifdef FEAT_COMMENTS
6288 && leader_len == 0
6289#endif
6290 && !has_format_option(FO_WRAP))
6291
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006292 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006293 if ((startcol = curwin->w_cursor.col) == 0)
6294 break;
6295
6296 /* find column of textwidth border */
6297 coladvance((colnr_T)textwidth);
6298 wantcol = curwin->w_cursor.col;
6299
Bram Moolenaar97b98102009-11-17 16:41:01 +00006300 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006301 foundcol = 0;
6302
6303 /*
6304 * Find position to break at.
6305 * Stop at first entered white when 'formatoptions' has 'v'
6306 */
6307 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006308 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006309 || curwin->w_cursor.lnum != Insstart.lnum
6310 || curwin->w_cursor.col >= Insstart.col)
6311 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006312 if (curwin->w_cursor.col == startcol && c != NUL)
6313 cc = c;
6314 else
6315 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006316 if (WHITECHAR(cc))
6317 {
6318 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006319 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006320
6321 /* find start of sequence of blanks */
6322 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6323 {
6324 dec_cursor();
6325 cc = gchar_cursor();
6326 }
6327 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6328 break; /* only spaces in front of text */
6329#ifdef FEAT_COMMENTS
6330 /* Don't break until after the comment leader */
6331 if (curwin->w_cursor.col < leader_len)
6332 break;
6333#endif
6334 if (has_format_option(FO_ONE_LETTER))
6335 {
6336 /* do not break after one-letter words */
6337 if (curwin->w_cursor.col == 0)
6338 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006339#ifdef FEAT_COMMENTS
6340 /* do not break "#a b" when 'tw' is 2 */
6341 if (curwin->w_cursor.col <= leader_len)
6342 break;
6343#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006344 col = curwin->w_cursor.col;
6345 dec_cursor();
6346 cc = gchar_cursor();
6347
6348 if (WHITECHAR(cc))
6349 continue; /* one-letter, continue */
6350 curwin->w_cursor.col = col;
6351 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006352
6353 inc_cursor();
6354
6355 end_foundcol = end_col + 1;
6356 foundcol = curwin->w_cursor.col;
6357 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006358 break;
6359 }
6360#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006361 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006362 {
6363 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006364 if (curwin->w_cursor.col != startcol)
6365 {
6366#ifdef FEAT_COMMENTS
6367 /* Don't break until after the comment leader */
6368 if (curwin->w_cursor.col < leader_len)
6369 break;
6370#endif
6371 col = curwin->w_cursor.col;
6372 inc_cursor();
6373 /* Don't change end_foundcol if already set. */
6374 if (foundcol != curwin->w_cursor.col)
6375 {
6376 foundcol = curwin->w_cursor.col;
6377 end_foundcol = foundcol;
6378 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6379 break;
6380 }
6381 curwin->w_cursor.col = col;
6382 }
6383
6384 if (curwin->w_cursor.col == 0)
6385 break;
6386
6387 col = curwin->w_cursor.col;
6388
6389 dec_cursor();
6390 cc = gchar_cursor();
6391
6392 if (WHITECHAR(cc))
6393 continue; /* break with space */
6394#ifdef FEAT_COMMENTS
6395 /* Don't break until after the comment leader */
6396 if (curwin->w_cursor.col < leader_len)
6397 break;
6398#endif
6399
6400 curwin->w_cursor.col = col;
6401
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006402 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006403 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006404 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6405 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006406 }
6407#endif
6408 if (curwin->w_cursor.col == 0)
6409 break;
6410 dec_cursor();
6411 }
6412
6413 if (foundcol == 0) /* no spaces, cannot break line */
6414 {
6415 curwin->w_cursor.col = startcol;
6416 break;
6417 }
6418
6419 /* Going to break the line, remove any "$" now. */
6420 undisplay_dollar();
6421
6422 /*
6423 * Offset between cursor position and line break is used by replace
6424 * stack functions. VREPLACE does not use this, and backspaces
6425 * over the text instead.
6426 */
6427#ifdef FEAT_VREPLACE
6428 if (State & VREPLACE_FLAG)
6429 orig_col = startcol; /* Will start backspacing from here */
6430 else
6431#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006432 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006433
6434 /*
6435 * adjust startcol for spaces that will be deleted and
6436 * characters that will remain on top line
6437 */
6438 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006439 while ((cc = gchar_cursor(), WHITECHAR(cc))
6440 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006441 inc_cursor();
6442 startcol -= curwin->w_cursor.col;
6443 if (startcol < 0)
6444 startcol = 0;
6445
6446#ifdef FEAT_VREPLACE
6447 if (State & VREPLACE_FLAG)
6448 {
6449 /*
6450 * In VREPLACE mode, we will backspace over the text to be
6451 * wrapped, so save a copy now to put on the next line.
6452 */
6453 saved_text = vim_strsave(ml_get_cursor());
6454 curwin->w_cursor.col = orig_col;
6455 if (saved_text == NULL)
6456 break; /* Can't do it, out of memory */
6457 saved_text[startcol] = NUL;
6458
6459 /* Backspace over characters that will move to the next line */
6460 if (!fo_white_par)
6461 backspace_until_column(foundcol);
6462 }
6463 else
6464#endif
6465 {
6466 /* put cursor after pos. to break line */
6467 if (!fo_white_par)
6468 curwin->w_cursor.col = foundcol;
6469 }
6470
6471 /*
6472 * Split the line just before the margin.
6473 * Only insert/delete lines, but don't really redraw the window.
6474 */
6475 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6476 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6477#ifdef FEAT_COMMENTS
6478 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006479 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006480#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006481 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6482 if (!(flags & INSCHAR_COM_LIST))
6483 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006484
6485 replace_offset = 0;
6486 if (first_line)
6487 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006488 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006489 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006490 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006491 * This section is for auto-wrap of numeric lists. When not
6492 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6493 * flag will be set and open_line() will handle it (as seen
6494 * above). The code here (and in get_number_indent()) will
6495 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006496 */
6497 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006498 second_indent =
6499 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006500 if (second_indent >= 0)
6501 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006502#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006503 if (State & VREPLACE_FLAG)
6504 change_indent(INDENT_SET, second_indent,
6505 FALSE, NUL, TRUE);
6506 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006507#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006508#ifdef FEAT_COMMENTS
6509 if (leader_len > 0 && second_indent - leader_len > 0)
6510 {
6511 int i;
6512 int padding = second_indent - leader_len;
6513
6514 /* We started at the first_line of a numbered list
6515 * that has a comment. the open_line() function has
6516 * inserted the proper comment leader and positioned
6517 * the cursor at the end of the split line. Now we
6518 * add the additional whitespace needed after the
6519 * comment leader for the numbered list. */
6520 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006521 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006522 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006523 }
6524 else
6525 {
6526#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006527 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006528#ifdef FEAT_COMMENTS
6529 }
6530#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006531 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006532 }
6533 first_line = FALSE;
6534 }
6535
6536#ifdef FEAT_VREPLACE
6537 if (State & VREPLACE_FLAG)
6538 {
6539 /*
6540 * In VREPLACE mode we have backspaced over the text to be
6541 * moved, now we re-insert it into the new line.
6542 */
6543 ins_bytes(saved_text);
6544 vim_free(saved_text);
6545 }
6546 else
6547#endif
6548 {
6549 /*
6550 * Check if cursor is not past the NUL off the line, cindent
6551 * may have added or removed indent.
6552 */
6553 curwin->w_cursor.col += startcol;
6554 len = (colnr_T)STRLEN(ml_get_curline());
6555 if (curwin->w_cursor.col > len)
6556 curwin->w_cursor.col = len;
6557 }
6558
6559 haveto_redraw = TRUE;
6560#ifdef FEAT_CINDENT
6561 can_cindent = TRUE;
6562#endif
6563 /* moved the cursor, don't autoindent or cindent now */
6564 did_ai = FALSE;
6565#ifdef FEAT_SMARTINDENT
6566 did_si = FALSE;
6567 can_si = FALSE;
6568 can_si_back = FALSE;
6569#endif
6570 line_breakcheck();
6571 }
6572
6573 if (save_char != NUL) /* put back space after cursor */
6574 pchar_cursor(save_char);
6575
Bram Moolenaar0026d472014-09-09 16:32:39 +02006576#ifdef FEAT_LINEBREAK
6577 curwin->w_p_lbr = has_lbr;
6578#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006579 if (!format_only && haveto_redraw)
6580 {
6581 update_topline();
6582 redraw_curbuf_later(VALID);
6583 }
6584}
6585
6586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 * Called after inserting or deleting text: When 'formatoptions' includes the
6588 * 'a' flag format from the current line until the end of the paragraph.
6589 * Keep the cursor at the same position relative to the text.
6590 * The caller must have saved the cursor line for undo, following ones will be
6591 * saved here.
6592 */
6593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006594auto_format(
6595 int trailblank, /* when TRUE also format with trailing blank */
6596 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597{
6598 pos_T pos;
6599 colnr_T len;
6600 char_u *old;
6601 char_u *new, *pnew;
6602 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006603 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604
6605 if (!has_format_option(FO_AUTO))
6606 return;
6607
6608 pos = curwin->w_cursor;
6609 old = ml_get_curline();
6610
6611 /* may remove added space */
6612 check_auto_format(FALSE);
6613
6614 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6615 * user might insert normal text next. Also skip formatting when "1" is
6616 * in 'formatoptions' and there is a single character before the cursor.
6617 * Otherwise the line would be broken and when typing another non-white
6618 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006619 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 if (*old != NUL && !trailblank && wasatend)
6621 {
6622 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006623 cc = gchar_cursor();
6624 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6625 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006627 cc = gchar_cursor();
6628 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 {
6630 curwin->w_cursor = pos;
6631 return;
6632 }
6633 curwin->w_cursor = pos;
6634 }
6635
6636#ifdef FEAT_COMMENTS
6637 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6638 * comments. */
6639 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006640 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 return;
6642#endif
6643
6644 /*
6645 * May start formatting in a previous line, so that after "x" a word is
6646 * moved to the previous line if it fits there now. Only when this is not
6647 * the start of a paragraph.
6648 */
6649 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6650 {
6651 --curwin->w_cursor.lnum;
6652 if (u_save_cursor() == FAIL)
6653 return;
6654 }
6655
6656 /*
6657 * Do the formatting and restore the cursor position. "saved_cursor" will
6658 * be adjusted for the text formatting.
6659 */
6660 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006661 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 curwin->w_cursor = saved_cursor;
6663 saved_cursor.lnum = 0;
6664
6665 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6666 {
6667 /* "cannot happen" */
6668 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6669 coladvance((colnr_T)MAXCOL);
6670 }
6671 else
6672 check_cursor_col();
6673
6674 /* Insert mode: If the cursor is now after the end of the line while it
6675 * previously wasn't, the line was broken. Because of the rule above we
6676 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6677 * formatted. */
6678 if (!wasatend && has_format_option(FO_WHITE_PAR))
6679 {
6680 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006681 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 if (curwin->w_cursor.col == len)
6683 {
6684 pnew = vim_strnsave(new, len + 2);
6685 pnew[len] = ' ';
6686 pnew[len + 1] = NUL;
6687 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6688 /* remove the space later */
6689 did_add_space = TRUE;
6690 }
6691 else
6692 /* may remove added space */
6693 check_auto_format(FALSE);
6694 }
6695
6696 check_cursor();
6697}
6698
6699/*
6700 * When an extra space was added to continue a paragraph for auto-formatting,
6701 * delete it now. The space must be under the cursor, just after the insert
6702 * position.
6703 */
6704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006705check_auto_format(
6706 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006709 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
6711 if (did_add_space)
6712 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006713 cc = gchar_cursor();
6714 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 /* Somehow the space was removed already. */
6716 did_add_space = FALSE;
6717 else
6718 {
6719 if (!end_insert)
6720 {
6721 inc_cursor();
6722 c = gchar_cursor();
6723 dec_cursor();
6724 }
6725 if (c != NUL)
6726 {
6727 /* The space is no longer at the end of the line, delete it. */
6728 del_char(FALSE);
6729 did_add_space = FALSE;
6730 }
6731 }
6732 }
6733}
6734
6735/*
6736 * Find out textwidth to be used for formatting:
6737 * if 'textwidth' option is set, use it
6738 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6739 * if invalid value, use 0.
6740 * Set default to window width (maximum 79) for "gq" operator.
6741 */
6742 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006743comp_textwidth(
6744 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745{
6746 int textwidth;
6747
6748 textwidth = curbuf->b_p_tw;
6749 if (textwidth == 0 && curbuf->b_p_wm)
6750 {
6751 /* The width is the window width minus 'wrapmargin' minus all the
6752 * things that add to the margin. */
6753 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6754#ifdef FEAT_CMDWIN
6755 if (cmdwin_type != 0)
6756 textwidth -= 1;
6757#endif
6758#ifdef FEAT_FOLDING
6759 textwidth -= curwin->w_p_fdc;
6760#endif
6761#ifdef FEAT_SIGNS
6762 if (curwin->w_buffer->b_signlist != NULL
6763# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01006764 || curwin->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765# endif
6766 )
6767 textwidth -= 1;
6768#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006769 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 textwidth -= 8;
6771 }
6772 if (textwidth < 0)
6773 textwidth = 0;
6774 if (ff && textwidth == 0)
6775 {
6776 textwidth = W_WIDTH(curwin) - 1;
6777 if (textwidth > 79)
6778 textwidth = 79;
6779 }
6780 return textwidth;
6781}
6782
6783/*
6784 * Put a character in the redo buffer, for when just after a CTRL-V.
6785 */
6786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006787redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
6789 char_u buf[10];
6790
6791 /* Only digits need special treatment. Translate them into a string of
6792 * three digits. */
6793 if (VIM_ISDIGIT(c))
6794 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006795 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 AppendToRedobuff(buf);
6797 }
6798 else
6799 AppendCharToRedobuff(c);
6800}
6801
6802/*
6803 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006804 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 */
6806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006807start_arrow(
6808 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006810 start_arrow_common(end_insert_pos, TRUE);
6811}
6812
6813/*
6814 * Like start_arrow() but with end_change argument.
6815 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6816 */
6817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006818start_arrow_with_change(
6819 pos_T *end_insert_pos, /* can be NULL */
6820 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006821{
6822 start_arrow_common(end_insert_pos, end_change);
6823 if (!end_change)
6824 {
6825 AppendCharToRedobuff(Ctrl_G);
6826 AppendCharToRedobuff('U');
6827 }
6828}
6829
6830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006831start_arrow_common(
6832 pos_T *end_insert_pos, /* can be NULL */
6833 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006834{
6835 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 {
6837 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006838 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 arrow_used = TRUE; /* this means we stopped the current insert */
6840 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006841#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006842 check_spell_redraw();
6843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844}
6845
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006846#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006847/*
6848 * If we skipped highlighting word at cursor, do it now.
6849 * It may be skipped again, thus reset spell_redraw_lnum first.
6850 */
6851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006852check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006853{
6854 if (spell_redraw_lnum != 0)
6855 {
6856 linenr_T lnum = spell_redraw_lnum;
6857
6858 spell_redraw_lnum = 0;
6859 redrawWinline(lnum, FALSE);
6860 }
6861}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006862
6863/*
6864 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6865 * spelled word, if there is one.
6866 */
6867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006868spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006869{
6870 pos_T tpos = curwin->w_cursor;
6871
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006872 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006873 if (curwin->w_cursor.col != tpos.col)
6874 start_arrow(&tpos);
6875}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006876#endif
6877
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878/*
6879 * stop_arrow() is called before a change is made in insert mode.
6880 * If an arrow key has been used, start a new insertion.
6881 * Returns FAIL if undo is impossible, shouldn't insert then.
6882 */
6883 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006884stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
6886 if (arrow_used)
6887 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006888 Insstart = curwin->w_cursor; /* new insertion starts here */
6889 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6890 /* Don't update the original insert position when moved to the
6891 * right, except when nothing was inserted yet. */
6892 update_Insstart_orig = FALSE;
6893 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6894
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 if (u_save_cursor() == OK)
6896 {
6897 arrow_used = FALSE;
6898 ins_need_undo = FALSE;
6899 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006900
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 ai_col = 0;
6902#ifdef FEAT_VREPLACE
6903 if (State & VREPLACE_FLAG)
6904 {
6905 orig_line_count = curbuf->b_ml.ml_line_count;
6906 vr_lines_changed = 1;
6907 }
6908#endif
6909 ResetRedobuff();
6910 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006911 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 }
6913 else if (ins_need_undo)
6914 {
6915 if (u_save_cursor() == OK)
6916 ins_need_undo = FALSE;
6917 }
6918
6919#ifdef FEAT_FOLDING
6920 /* Always open fold at the cursor line when inserting something. */
6921 foldOpenCursor();
6922#endif
6923
6924 return (arrow_used || ins_need_undo ? FAIL : OK);
6925}
6926
6927/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006928 * Do a few things to stop inserting.
6929 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6930 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 */
6932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006933stop_insert(
6934 pos_T *end_insert_pos,
6935 int esc, /* called by ins_esc() */
6936 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006938 int cc;
6939 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
6941 stop_redo_ins();
6942 replace_flush(); /* abandon replace stack */
6943
6944 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006945 * Save the inserted text for later redo with ^@ and CTRL-A.
6946 * Don't do it when "restart_edit" was set and nothing was inserted,
6947 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006949 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006950 if (did_restart_edit == 0 || (ptr != NULL
6951 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006952 {
6953 vim_free(last_insert);
6954 last_insert = ptr;
6955 last_insert_skip = new_insert_skip;
6956 }
6957 else
6958 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006960 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {
6962 /* Auto-format now. It may seem strange to do this when stopping an
6963 * insertion (or moving the cursor), but it's required when appending
6964 * a line and having it end in a space. But only do it when something
6965 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006966 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006968 pos_T tpos = curwin->w_cursor;
6969
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 /* When the cursor is at the end of the line after a space the
6971 * formatting will move it to the following word. Avoid that by
6972 * moving the cursor onto the space. */
6973 cc = 'x';
6974 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6975 {
6976 dec_cursor();
6977 cc = gchar_cursor();
6978 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006979 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 }
6981
6982 auto_format(TRUE, FALSE);
6983
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006984 if (vim_iswhite(cc))
6985 {
6986 if (gchar_cursor() != NUL)
6987 inc_cursor();
6988#ifdef FEAT_VIRTUALEDIT
6989 /* If the cursor is still at the same character, also keep
6990 * the "coladd". */
6991 if (gchar_cursor() == NUL
6992 && curwin->w_cursor.lnum == tpos.lnum
6993 && curwin->w_cursor.col == tpos.col)
6994 curwin->w_cursor.coladd = tpos.coladd;
6995#endif
6996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 }
6998
6999 /* If a space was inserted for auto-formatting, remove it now. */
7000 check_auto_format(TRUE);
7001
7002 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007003 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007004 * Do this when ESC was used or moving the cursor up/down.
7005 * Check for the old position still being valid, just in case the text
7006 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007007 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007008 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7009 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007011 pos_T tpos = curwin->w_cursor;
7012
7013 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007014 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007015 for (;;)
7016 {
7017 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7018 --curwin->w_cursor.col;
7019 cc = gchar_cursor();
7020 if (!vim_iswhite(cc))
7021 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007022 if (del_char(TRUE) == FAIL)
7023 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007024 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007025 if (curwin->w_cursor.lnum != tpos.lnum)
7026 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007027 else
7028 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007029 /* reset tpos, could have been invalidated in the loop above */
7030 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007031 tpos.col++;
7032 if (cc != NUL && gchar_pos(&tpos) == NUL)
7033 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 /* <C-S-Right> may have started Visual mode, adjust the position for
7037 * deleted characters. */
7038 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7039 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007040 int len = (int)STRLEN(ml_get_curline());
7041
7042 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007044 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007045#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 }
7049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
7051 }
7052 did_ai = FALSE;
7053#ifdef FEAT_SMARTINDENT
7054 did_si = FALSE;
7055 can_si = FALSE;
7056 can_si_back = FALSE;
7057#endif
7058
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007059 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7060 * now in a different buffer. */
7061 if (end_insert_pos != NULL)
7062 {
7063 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007064 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007065 curbuf->b_op_end = *end_insert_pos;
7066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067}
7068
7069/*
7070 * Set the last inserted text to a single character.
7071 * Used for the replace command.
7072 */
7073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007074set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 char_u *s;
7077
7078 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 if (last_insert != NULL)
7081 {
7082 s = last_insert;
7083 /* Use the CTRL-V only when entering a special char */
7084 if (c < ' ' || c == DEL)
7085 *s++ = Ctrl_V;
7086 s = add_char2buf(c, s);
7087 *s++ = ESC;
7088 *s++ = NUL;
7089 last_insert_skip = 0;
7090 }
7091}
7092
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007093#if defined(EXITFREE) || defined(PROTO)
7094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007095free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007096{
7097 vim_free(last_insert);
7098 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007099# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007100 vim_free(compl_orig_text);
7101 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007102# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007103}
7104#endif
7105
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106/*
7107 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7108 * and CSI. Handle multi-byte characters.
7109 * Returns a pointer to after the added bytes.
7110 */
7111 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007112add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
7114#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007115 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 int i;
7117 int len;
7118
7119 len = (*mb_char2bytes)(c, temp);
7120 for (i = 0; i < len; ++i)
7121 {
7122 c = temp[i];
7123#endif
7124 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7125 if (c == K_SPECIAL)
7126 {
7127 *s++ = K_SPECIAL;
7128 *s++ = KS_SPECIAL;
7129 *s++ = KE_FILLER;
7130 }
7131#ifdef FEAT_GUI
7132 else if (c == CSI)
7133 {
7134 *s++ = CSI;
7135 *s++ = KS_EXTRA;
7136 *s++ = (int)KE_CSI;
7137 }
7138#endif
7139 else
7140 *s++ = c;
7141#ifdef FEAT_MBYTE
7142 }
7143#endif
7144 return s;
7145}
7146
7147/*
7148 * move cursor to start of line
7149 * if flags & BL_WHITE move to first non-white
7150 * if flags & BL_SOL move to first non-white if startofline is set,
7151 * otherwise keep "curswant" column
7152 * if flags & BL_FIX don't leave the cursor on a NUL.
7153 */
7154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007155beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
7157 if ((flags & BL_SOL) && !p_sol)
7158 coladvance(curwin->w_curswant);
7159 else
7160 {
7161 curwin->w_cursor.col = 0;
7162#ifdef FEAT_VIRTUALEDIT
7163 curwin->w_cursor.coladd = 0;
7164#endif
7165
7166 if (flags & (BL_WHITE | BL_SOL))
7167 {
7168 char_u *ptr;
7169
7170 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7171 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7172 ++curwin->w_cursor.col;
7173 }
7174 curwin->w_set_curswant = TRUE;
7175 }
7176}
7177
7178/*
7179 * oneright oneleft cursor_down cursor_up
7180 *
7181 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007182 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 * Return OK when successful, FAIL when we hit a line of file boundary.
7184 */
7185
7186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007187oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188{
7189 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
7192#ifdef FEAT_VIRTUALEDIT
7193 if (virtual_active())
7194 {
7195 pos_T prevpos = curwin->w_cursor;
7196
7197 /* Adjust for multi-wide char (excluding TAB) */
7198 ptr = ml_get_cursor();
7199 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007200# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007202# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 ))
7206 ? ptr2cells(ptr) : 1));
7207 curwin->w_set_curswant = TRUE;
7208 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7209 return (prevpos.col != curwin->w_cursor.col
7210 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7211 }
7212#endif
7213
7214 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007215 if (*ptr == NUL)
7216 return FAIL; /* already at the very end */
7217
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007219 if (has_mbyte)
7220 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 else
7222#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007223 l = 1;
7224
7225 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7226 * contains "onemore". */
7227 if (ptr[l] == NUL
7228#ifdef FEAT_VIRTUALEDIT
7229 && (ve_flags & VE_ONEMORE) == 0
7230#endif
7231 )
7232 return FAIL;
7233 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
7235 curwin->w_set_curswant = TRUE;
7236 return OK;
7237}
7238
7239 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007240oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241{
7242#ifdef FEAT_VIRTUALEDIT
7243 if (virtual_active())
7244 {
7245 int width;
7246 int v = getviscol();
7247
7248 if (v == 0)
7249 return FAIL;
7250
7251# ifdef FEAT_LINEBREAK
7252 /* We might get stuck on 'showbreak', skip over it. */
7253 width = 1;
7254 for (;;)
7255 {
7256 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007257 /* getviscol() is slow, skip it when 'showbreak' is empty,
7258 * 'breakindent' is not set and there are no multi-byte
7259 * characters */
7260 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261# ifdef FEAT_MBYTE
7262 && !has_mbyte
7263# endif
7264 ) || getviscol() < v)
7265 break;
7266 ++width;
7267 }
7268# else
7269 coladvance(v - 1);
7270# endif
7271
7272 if (curwin->w_cursor.coladd == 1)
7273 {
7274 char_u *ptr;
7275
7276 /* Adjust for multi-wide char (not a TAB) */
7277 ptr = ml_get_cursor();
7278 if (*ptr != TAB && vim_isprintc(
7279# ifdef FEAT_MBYTE
7280 (*mb_ptr2char)(ptr)
7281# else
7282 *ptr
7283# endif
7284 ) && ptr2cells(ptr) > 1)
7285 curwin->w_cursor.coladd = 0;
7286 }
7287
7288 curwin->w_set_curswant = TRUE;
7289 return OK;
7290 }
7291#endif
7292
7293 if (curwin->w_cursor.col == 0)
7294 return FAIL;
7295
7296 curwin->w_set_curswant = TRUE;
7297 --curwin->w_cursor.col;
7298
7299#ifdef FEAT_MBYTE
7300 /* if the character on the left of the current cursor is a multi-byte
7301 * character, move to its first byte */
7302 if (has_mbyte)
7303 mb_adjust_cursor();
7304#endif
7305 return OK;
7306}
7307
7308 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007309cursor_up(
7310 long n,
7311 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312{
7313 linenr_T lnum;
7314
7315 if (n > 0)
7316 {
7317 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007318 /* This fails if the cursor is already in the first line or the count
7319 * is larger than the line number and '-' is in 'cpoptions' */
7320 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 return FAIL;
7322 if (n >= lnum)
7323 lnum = 1;
7324 else
7325#ifdef FEAT_FOLDING
7326 if (hasAnyFolding(curwin))
7327 {
7328 /*
7329 * Count each sequence of folded lines as one logical line.
7330 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007331 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 (void)hasFolding(lnum, &lnum, NULL);
7333
7334 while (n--)
7335 {
7336 /* move up one line */
7337 --lnum;
7338 if (lnum <= 1)
7339 break;
7340 /* If we entered a fold, move to the beginning, unless in
7341 * Insert mode or when 'foldopen' contains "all": it will open
7342 * in a moment. */
7343 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7344 (void)hasFolding(lnum, &lnum, NULL);
7345 }
7346 if (lnum < 1)
7347 lnum = 1;
7348 }
7349 else
7350#endif
7351 lnum -= n;
7352 curwin->w_cursor.lnum = lnum;
7353 }
7354
7355 /* try to advance to the column we want to be at */
7356 coladvance(curwin->w_curswant);
7357
7358 if (upd_topline)
7359 update_topline(); /* make sure curwin->w_topline is valid */
7360
7361 return OK;
7362}
7363
7364/*
7365 * Cursor down a number of logical lines.
7366 */
7367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007368cursor_down(
7369 long n,
7370 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371{
7372 linenr_T lnum;
7373
7374 if (n > 0)
7375 {
7376 lnum = curwin->w_cursor.lnum;
7377#ifdef FEAT_FOLDING
7378 /* Move to last line of fold, will fail if it's the end-of-file. */
7379 (void)hasFolding(lnum, NULL, &lnum);
7380#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007381 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007382 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007383 if (lnum >= curbuf->b_ml.ml_line_count
7384 || (lnum + n > curbuf->b_ml.ml_line_count
7385 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 return FAIL;
7387 if (lnum + n >= curbuf->b_ml.ml_line_count)
7388 lnum = curbuf->b_ml.ml_line_count;
7389 else
7390#ifdef FEAT_FOLDING
7391 if (hasAnyFolding(curwin))
7392 {
7393 linenr_T last;
7394
7395 /* count each sequence of folded lines as one logical line */
7396 while (n--)
7397 {
7398 if (hasFolding(lnum, NULL, &last))
7399 lnum = last + 1;
7400 else
7401 ++lnum;
7402 if (lnum >= curbuf->b_ml.ml_line_count)
7403 break;
7404 }
7405 if (lnum > curbuf->b_ml.ml_line_count)
7406 lnum = curbuf->b_ml.ml_line_count;
7407 }
7408 else
7409#endif
7410 lnum += n;
7411 curwin->w_cursor.lnum = lnum;
7412 }
7413
7414 /* try to advance to the column we want to be at */
7415 coladvance(curwin->w_curswant);
7416
7417 if (upd_topline)
7418 update_topline(); /* make sure curwin->w_topline is valid */
7419
7420 return OK;
7421}
7422
7423/*
7424 * Stuff the last inserted text in the read buffer.
7425 * Last_insert actually is a copy of the redo buffer, so we
7426 * first have to remove the command.
7427 */
7428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007429stuff_inserted(
7430 int c, /* Command character to be inserted */
7431 long count, /* Repeat this many times */
7432 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433{
7434 char_u *esc_ptr;
7435 char_u *ptr;
7436 char_u *last_ptr;
7437 char_u last = NUL;
7438
7439 ptr = get_last_insert();
7440 if (ptr == NULL)
7441 {
7442 EMSG(_(e_noinstext));
7443 return FAIL;
7444 }
7445
7446 /* may want to stuff the command character, to start Insert mode */
7447 if (c != NUL)
7448 stuffcharReadbuff(c);
7449 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7450 *esc_ptr = NUL; /* remove the ESC */
7451
7452 /* when the last char is either "0" or "^" it will be quoted if no ESC
7453 * comes after it OR if it will inserted more than once and "ptr"
7454 * starts with ^D. -- Acevedo
7455 */
7456 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7457 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7458 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7459 {
7460 last = *last_ptr;
7461 *last_ptr = NUL;
7462 }
7463
7464 do
7465 {
7466 stuffReadbuff(ptr);
7467 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7468 if (last)
7469 stuffReadbuff((char_u *)(last == '0'
7470 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7471 : IF_EB("\026^", CTRL_V_STR "^")));
7472 }
7473 while (--count > 0);
7474
7475 if (last)
7476 *last_ptr = last;
7477
7478 if (esc_ptr != NULL)
7479 *esc_ptr = ESC; /* put the ESC back */
7480
7481 /* may want to stuff a trailing ESC, to get out of Insert mode */
7482 if (!no_esc)
7483 stuffcharReadbuff(ESC);
7484
7485 return OK;
7486}
7487
7488 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007489get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490{
7491 if (last_insert == NULL)
7492 return NULL;
7493 return last_insert + last_insert_skip;
7494}
7495
7496/*
7497 * Get last inserted string, and remove trailing <Esc>.
7498 * Returns pointer to allocated memory (must be freed) or NULL.
7499 */
7500 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007501get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502{
7503 char_u *s;
7504 int len;
7505
7506 if (last_insert == NULL)
7507 return NULL;
7508 s = vim_strsave(last_insert + last_insert_skip);
7509 if (s != NULL)
7510 {
7511 len = (int)STRLEN(s);
7512 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7513 s[len - 1] = NUL;
7514 }
7515 return s;
7516}
7517
7518/*
7519 * Check the word in front of the cursor for an abbreviation.
7520 * Called when the non-id character "c" has been entered.
7521 * When an abbreviation is recognized it is removed from the text and
7522 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7523 */
7524 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007525echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526{
7527 /* Don't check for abbreviation in paste mode, when disabled and just
7528 * after moving around with cursor keys. */
7529 if (p_paste || no_abbr || arrow_used)
7530 return FALSE;
7531
7532 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7533 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7534}
7535
7536/*
7537 * replace-stack functions
7538 *
7539 * When replacing characters, the replaced characters are remembered for each
7540 * new character. This is used to re-insert the old text when backspacing.
7541 *
7542 * There is a NUL headed list of characters for each character that is
7543 * currently in the file after the insertion point. When BS is used, one NUL
7544 * headed list is put back for the deleted character.
7545 *
7546 * For a newline, there are two NUL headed lists. One contains the characters
7547 * that the NL replaced. The extra one stores the characters after the cursor
7548 * that were deleted (always white space).
7549 *
7550 * Replace_offset is normally 0, in which case replace_push will add a new
7551 * character at the end of the stack. If replace_offset is not 0, that many
7552 * characters will be left on the stack above the newly inserted character.
7553 */
7554
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007555static char_u *replace_stack = NULL;
7556static long replace_stack_nr = 0; /* next entry in replace stack */
7557static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558
7559 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007560replace_push(
7561 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562{
7563 char_u *p;
7564
7565 if (replace_stack_nr < replace_offset) /* nothing to do */
7566 return;
7567 if (replace_stack_len <= replace_stack_nr)
7568 {
7569 replace_stack_len += 50;
7570 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7571 if (p == NULL) /* out of memory */
7572 {
7573 replace_stack_len -= 50;
7574 return;
7575 }
7576 if (replace_stack != NULL)
7577 {
7578 mch_memmove(p, replace_stack,
7579 (size_t)(replace_stack_nr * sizeof(char_u)));
7580 vim_free(replace_stack);
7581 }
7582 replace_stack = p;
7583 }
7584 p = replace_stack + replace_stack_nr - replace_offset;
7585 if (replace_offset)
7586 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7587 *p = c;
7588 ++replace_stack_nr;
7589}
7590
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007591#if defined(FEAT_MBYTE) || defined(PROTO)
7592/*
7593 * Push a character onto the replace stack. Handles a multi-byte character in
7594 * reverse byte order, so that the first byte is popped off first.
7595 * Return the number of bytes done (includes composing characters).
7596 */
7597 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007598replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007599{
7600 int l = (*mb_ptr2len)(p);
7601 int j;
7602
7603 for (j = l - 1; j >= 0; --j)
7604 replace_push(p[j]);
7605 return l;
7606}
7607#endif
7608
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609/*
7610 * Pop one item from the replace stack.
7611 * return -1 if stack empty
7612 * return replaced character or NUL otherwise
7613 */
7614 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007615replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616{
7617 if (replace_stack_nr == 0)
7618 return -1;
7619 return (int)replace_stack[--replace_stack_nr];
7620}
7621
7622/*
7623 * Join the top two items on the replace stack. This removes to "off"'th NUL
7624 * encountered.
7625 */
7626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007627replace_join(
7628 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629{
7630 int i;
7631
7632 for (i = replace_stack_nr; --i >= 0; )
7633 if (replace_stack[i] == NUL && off-- <= 0)
7634 {
7635 --replace_stack_nr;
7636 mch_memmove(replace_stack + i, replace_stack + i + 1,
7637 (size_t)(replace_stack_nr - i));
7638 return;
7639 }
7640}
7641
7642/*
7643 * Pop bytes from the replace stack until a NUL is found, and insert them
7644 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7645 */
7646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007647replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648{
7649 int cc;
7650 int oldState = State;
7651
7652 State = NORMAL; /* don't want REPLACE here */
7653 while ((cc = replace_pop()) > 0)
7654 {
7655#ifdef FEAT_MBYTE
7656 mb_replace_pop_ins(cc);
7657#else
7658 ins_char(cc);
7659#endif
7660 dec_cursor();
7661 }
7662 State = oldState;
7663}
7664
7665#ifdef FEAT_MBYTE
7666/*
7667 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7668 * indicates a multi-byte char, pop the other bytes too.
7669 */
7670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007671mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
7673 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007674 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 int i;
7676 int c;
7677
7678 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7679 {
7680 buf[0] = cc;
7681 for (i = 1; i < n; ++i)
7682 buf[i] = replace_pop();
7683 ins_bytes_len(buf, n);
7684 }
7685 else
7686 ins_char(cc);
7687
7688 if (enc_utf8)
7689 /* Handle composing chars. */
7690 for (;;)
7691 {
7692 c = replace_pop();
7693 if (c == -1) /* stack empty */
7694 break;
7695 if ((n = MB_BYTE2LEN(c)) == 1)
7696 {
7697 /* Not a multi-byte char, put it back. */
7698 replace_push(c);
7699 break;
7700 }
7701 else
7702 {
7703 buf[0] = c;
7704 for (i = 1; i < n; ++i)
7705 buf[i] = replace_pop();
7706 if (utf_iscomposing(utf_ptr2char(buf)))
7707 ins_bytes_len(buf, n);
7708 else
7709 {
7710 /* Not a composing char, put it back. */
7711 for (i = n - 1; i >= 0; --i)
7712 replace_push(buf[i]);
7713 break;
7714 }
7715 }
7716 }
7717}
7718#endif
7719
7720/*
7721 * make the replace stack empty
7722 * (called when exiting replace mode)
7723 */
7724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007725replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726{
7727 vim_free(replace_stack);
7728 replace_stack = NULL;
7729 replace_stack_len = 0;
7730 replace_stack_nr = 0;
7731}
7732
7733/*
7734 * Handle doing a BS for one character.
7735 * cc < 0: replace stack empty, just move cursor
7736 * cc == 0: character was inserted, delete it
7737 * cc > 0: character was replaced, put cc (first byte of original char) back
7738 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007739 * When "limit_col" is >= 0, don't delete before this column. Matters when
7740 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 */
7742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007743replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744{
7745 int cc;
7746#ifdef FEAT_VREPLACE
7747 int orig_len = 0;
7748 int ins_len;
7749 int orig_vcols = 0;
7750 colnr_T start_vcol;
7751 char_u *p;
7752 int i;
7753 int vcol;
7754#endif
7755
7756 cc = replace_pop();
7757 if (cc > 0)
7758 {
7759#ifdef FEAT_VREPLACE
7760 if (State & VREPLACE_FLAG)
7761 {
7762 /* Get the number of screen cells used by the character we are
7763 * going to delete. */
7764 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7765 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7766 }
7767#endif
7768#ifdef FEAT_MBYTE
7769 if (has_mbyte)
7770 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007771 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772# ifdef FEAT_VREPLACE
7773 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007774 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775# endif
7776 replace_push(cc);
7777 }
7778 else
7779#endif
7780 {
7781 pchar_cursor(cc);
7782#ifdef FEAT_VREPLACE
7783 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007784 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785#endif
7786 }
7787 replace_pop_ins();
7788
7789#ifdef FEAT_VREPLACE
7790 if (State & VREPLACE_FLAG)
7791 {
7792 /* Get the number of screen cells used by the inserted characters */
7793 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007794 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 vcol = start_vcol;
7796 for (i = 0; i < ins_len; ++i)
7797 {
7798 vcol += chartabsize(p + i, vcol);
7799#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007800 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801#endif
7802 }
7803 vcol -= start_vcol;
7804
7805 /* Delete spaces that were inserted after the cursor to keep the
7806 * text aligned. */
7807 curwin->w_cursor.col += ins_len;
7808 while (vcol > orig_vcols && gchar_cursor() == ' ')
7809 {
7810 del_char(FALSE);
7811 ++orig_vcols;
7812 }
7813 curwin->w_cursor.col -= ins_len;
7814 }
7815#endif
7816
7817 /* mark the buffer as changed and prepare for displaying */
7818 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7819 }
7820 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007821 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822}
7823
7824#ifdef FEAT_CINDENT
7825/*
7826 * Return TRUE if C-indenting is on.
7827 */
7828 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007829cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830{
7831 return (!p_paste && (curbuf->b_p_cin
7832# ifdef FEAT_EVAL
7833 || *curbuf->b_p_inde != NUL
7834# endif
7835 ));
7836}
7837#endif
7838
7839#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7840/*
7841 * Re-indent the current line, based on the current contents of it and the
7842 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7843 * confused what all the part that handles Control-T is doing that I'm not.
7844 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7845 */
7846
7847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007848fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007850 int amount = get_the_indent();
7851
7852 if (amount >= 0)
7853 {
7854 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7855 if (linewhite(curwin->w_cursor.lnum))
7856 did_ai = TRUE; /* delete the indent if the line stays empty */
7857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858}
7859
7860 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007861fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862{
7863 if (p_paste)
7864 return;
7865# ifdef FEAT_LISP
7866 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7867 fixthisline(get_lisp_indent);
7868# endif
7869# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7870 else
7871# endif
7872# ifdef FEAT_CINDENT
7873 if (cindent_on())
7874 do_c_expr_indent();
7875# endif
7876}
7877
7878#endif
7879
7880#ifdef FEAT_CINDENT
7881/*
7882 * return TRUE if 'cinkeys' contains the key "keytyped",
7883 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007884 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7886 *
7887 * "keytyped" can have a few special values:
7888 * KEY_OPEN_FORW
7889 * KEY_OPEN_BACK
7890 * KEY_COMPLETE just finished completion.
7891 *
7892 * If line_is_empty is TRUE accept keys with '0' before them.
7893 */
7894 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007895in_cinkeys(
7896 int keytyped,
7897 int when,
7898 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899{
7900 char_u *look;
7901 int try_match;
7902 int try_match_word;
7903 char_u *p;
7904 char_u *line;
7905 int icase;
7906 int i;
7907
Bram Moolenaar30848942009-12-24 14:46:12 +00007908 if (keytyped == NUL)
7909 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7910 return FALSE;
7911
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912#ifdef FEAT_EVAL
7913 if (*curbuf->b_p_inde != NUL)
7914 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7915 else
7916#endif
7917 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7918 while (*look)
7919 {
7920 /*
7921 * Find out if we want to try a match with this key, depending on
7922 * 'when' and a '*' or '!' before the key.
7923 */
7924 switch (when)
7925 {
7926 case '*': try_match = (*look == '*'); break;
7927 case '!': try_match = (*look == '!'); break;
7928 default: try_match = (*look != '*'); break;
7929 }
7930 if (*look == '*' || *look == '!')
7931 ++look;
7932
7933 /*
7934 * If there is a '0', only accept a match if the line is empty.
7935 * But may still match when typing last char of a word.
7936 */
7937 if (*look == '0')
7938 {
7939 try_match_word = try_match;
7940 if (!line_is_empty)
7941 try_match = FALSE;
7942 ++look;
7943 }
7944 else
7945 try_match_word = FALSE;
7946
7947 /*
7948 * does it look like a control character?
7949 */
7950 if (*look == '^'
7951#ifdef EBCDIC
7952 && (Ctrl_chr(look[1]) != 0)
7953#else
7954 && look[1] >= '?' && look[1] <= '_'
7955#endif
7956 )
7957 {
7958 if (try_match && keytyped == Ctrl_chr(look[1]))
7959 return TRUE;
7960 look += 2;
7961 }
7962 /*
7963 * 'o' means "o" command, open forward.
7964 * 'O' means "O" command, open backward.
7965 */
7966 else if (*look == 'o')
7967 {
7968 if (try_match && keytyped == KEY_OPEN_FORW)
7969 return TRUE;
7970 ++look;
7971 }
7972 else if (*look == 'O')
7973 {
7974 if (try_match && keytyped == KEY_OPEN_BACK)
7975 return TRUE;
7976 ++look;
7977 }
7978
7979 /*
7980 * 'e' means to check for "else" at start of line and just before the
7981 * cursor.
7982 */
7983 else if (*look == 'e')
7984 {
7985 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7986 {
7987 p = ml_get_curline();
7988 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7989 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7990 return TRUE;
7991 }
7992 ++look;
7993 }
7994
7995 /*
7996 * ':' only causes an indent if it is at the end of a label or case
7997 * statement, or when it was before typing the ':' (to fix
7998 * class::method for C++).
7999 */
8000 else if (*look == ':')
8001 {
8002 if (try_match && keytyped == ':')
8003 {
8004 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008005 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008007 /* Need to get the line again after cin_islabel(). */
8008 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 if (curwin->w_cursor.col > 2
8010 && p[curwin->w_cursor.col - 1] == ':'
8011 && p[curwin->w_cursor.col - 2] == ':')
8012 {
8013 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008014 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008015 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 p = ml_get_curline();
8017 p[curwin->w_cursor.col - 1] = ':';
8018 if (i)
8019 return TRUE;
8020 }
8021 }
8022 ++look;
8023 }
8024
8025
8026 /*
8027 * Is it a key in <>, maybe?
8028 */
8029 else if (*look == '<')
8030 {
8031 if (try_match)
8032 {
8033 /*
8034 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8035 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8036 * >, *, : and ! keys if they really really want to.
8037 */
8038 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8039 && keytyped == look[1])
8040 return TRUE;
8041
8042 if (keytyped == get_special_key_code(look + 1))
8043 return TRUE;
8044 }
8045 while (*look && *look != '>')
8046 look++;
8047 while (*look == '>')
8048 look++;
8049 }
8050
8051 /*
8052 * Is it a word: "=word"?
8053 */
8054 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8055 {
8056 ++look;
8057 if (*look == '~')
8058 {
8059 icase = TRUE;
8060 ++look;
8061 }
8062 else
8063 icase = FALSE;
8064 p = vim_strchr(look, ',');
8065 if (p == NULL)
8066 p = look + STRLEN(look);
8067 if ((try_match || try_match_word)
8068 && curwin->w_cursor.col >= (colnr_T)(p - look))
8069 {
8070 int match = FALSE;
8071
8072#ifdef FEAT_INS_EXPAND
8073 if (keytyped == KEY_COMPLETE)
8074 {
8075 char_u *s;
8076
8077 /* Just completed a word, check if it starts with "look".
8078 * search back for the start of a word. */
8079 line = ml_get_curline();
8080# ifdef FEAT_MBYTE
8081 if (has_mbyte)
8082 {
8083 char_u *n;
8084
8085 for (s = line + curwin->w_cursor.col; s > line; s = n)
8086 {
8087 n = mb_prevptr(line, s);
8088 if (!vim_iswordp(n))
8089 break;
8090 }
8091 }
8092 else
8093# endif
8094 for (s = line + curwin->w_cursor.col; s > line; --s)
8095 if (!vim_iswordc(s[-1]))
8096 break;
8097 if (s + (p - look) <= line + curwin->w_cursor.col
8098 && (icase
8099 ? MB_STRNICMP(s, look, p - look)
8100 : STRNCMP(s, look, p - look)) == 0)
8101 match = TRUE;
8102 }
8103 else
8104#endif
8105 /* TODO: multi-byte */
8106 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8107 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8108 {
8109 line = ml_get_cursor();
8110 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8111 || !vim_iswordc(line[-(p - look) - 1]))
8112 && (icase
8113 ? MB_STRNICMP(line - (p - look), look, p - look)
8114 : STRNCMP(line - (p - look), look, p - look))
8115 == 0)
8116 match = TRUE;
8117 }
8118 if (match && try_match_word && !try_match)
8119 {
8120 /* "0=word": Check if there are only blanks before the
8121 * word. */
8122 line = ml_get_curline();
8123 if ((int)(skipwhite(line) - line) !=
8124 (int)(curwin->w_cursor.col - (p - look)))
8125 match = FALSE;
8126 }
8127 if (match)
8128 return TRUE;
8129 }
8130 look = p;
8131 }
8132
8133 /*
8134 * ok, it's a boring generic character.
8135 */
8136 else
8137 {
8138 if (try_match && *look == keytyped)
8139 return TRUE;
8140 ++look;
8141 }
8142
8143 /*
8144 * Skip over ", ".
8145 */
8146 look = skip_to_option_part(look);
8147 }
8148 return FALSE;
8149}
8150#endif /* FEAT_CINDENT */
8151
8152#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8153/*
8154 * Map Hebrew keyboard when in hkmap mode.
8155 */
8156 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008157hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
8159 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8160 {
8161 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8162 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8163 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8164 static char_u map[26] =
8165 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8166 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8167 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8168 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8169 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8170 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8171 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8172 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8173 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8174
8175 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8176 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8177 /* '-1'='sofit' */
8178 else if (c == 'x')
8179 return 'X';
8180 else if (c == 'q')
8181 return '\''; /* {geresh}={'} */
8182 else if (c == 246)
8183 return ' '; /* \"o --> ' ' for a german keyboard */
8184 else if (c == 228)
8185 return ' '; /* \"a --> ' ' -- / -- */
8186 else if (c == 252)
8187 return ' '; /* \"u --> ' ' -- / -- */
8188#ifdef EBCDIC
8189 else if (islower(c))
8190#else
8191 /* NOTE: islower() does not do the right thing for us on Linux so we
8192 * do this the same was as 5.7 and previous, so it works correctly on
8193 * all systems. Specifically, the e.g. Delete and Arrow keys are
8194 * munged and won't work if e.g. searching for Hebrew text.
8195 */
8196 else if (c >= 'a' && c <= 'z')
8197#endif
8198 return (int)(map[CharOrdLow(c)] + p_aleph);
8199 else
8200 return c;
8201 }
8202 else
8203 {
8204 switch (c)
8205 {
8206 case '`': return ';';
8207 case '/': return '.';
8208 case '\'': return ',';
8209 case 'q': return '/';
8210 case 'w': return '\'';
8211
8212 /* Hebrew letters - set offset from 'a' */
8213 case ',': c = '{'; break;
8214 case '.': c = 'v'; break;
8215 case ';': c = 't'; break;
8216 default: {
8217 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8218
8219#ifdef EBCDIC
8220 /* see note about islower() above */
8221 if (!islower(c))
8222#else
8223 if (c < 'a' || c > 'z')
8224#endif
8225 return c;
8226 c = str[CharOrdLow(c)];
8227 break;
8228 }
8229 }
8230
8231 return (int)(CharOrdLow(c) + p_aleph);
8232 }
8233}
8234#endif
8235
8236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008237ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238{
8239 int need_redraw = FALSE;
8240 int regname;
8241 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008242 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243
8244 /*
8245 * If we are going to wait for a character, show a '"'.
8246 */
8247 pc_status = PC_STATUS_UNSET;
8248 if (redrawing() && !char_avail())
8249 {
8250 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008251 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252
8253 edit_putchar('"', TRUE);
8254#ifdef FEAT_CMDL_INFO
8255 add_to_showcmd_c(Ctrl_R);
8256#endif
8257 }
8258
8259#ifdef USE_ON_FLY_SCROLL
8260 dont_scroll = TRUE; /* disallow scrolling here */
8261#endif
8262
8263 /*
8264 * Don't map the register name. This also prevents the mode message to be
8265 * deleted when ESC is hit.
8266 */
8267 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008268 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8271 {
8272 /* Get a third key for literal register insertion */
8273 literally = regname;
8274#ifdef FEAT_CMDL_INFO
8275 add_to_showcmd_c(literally);
8276#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008277 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 }
8280 --no_mapping;
8281
8282#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008283 /* Don't call u_sync() while typing the expression or giving an error
8284 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 ++no_u_sync;
8286 if (regname == '=')
8287 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008288# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008290# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008291 /* Sync undo when evaluating the expression calls setline() or
8292 * append(), so that it can be undone separately. */
8293 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008294
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008296# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 /* Restore the Input Method. */
8298 if (im_on)
8299 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008300# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008302 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8303 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008304 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 else
8308 {
8309#endif
8310 if (literally == Ctrl_O || literally == Ctrl_P)
8311 {
8312 /* Append the command to the redo buffer. */
8313 AppendCharToRedobuff(Ctrl_R);
8314 AppendCharToRedobuff(literally);
8315 AppendCharToRedobuff(regname);
8316
8317 do_put(regname, BACKWARD, 1L,
8318 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8319 }
8320 else if (insert_reg(regname, literally) == FAIL)
8321 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008322 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 need_redraw = TRUE; /* remove the '"' */
8324 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008325 else if (stop_insert_mode)
8326 /* When the '=' register was used and a function was invoked that
8327 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8328 * insert anything, need to remove the '"' */
8329 need_redraw = TRUE;
8330
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331#ifdef FEAT_EVAL
8332 }
8333 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008334 if (u_sync_once == 1)
8335 ins_need_undo = TRUE;
8336 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337#endif
8338#ifdef FEAT_CMDL_INFO
8339 clear_showcmd();
8340#endif
8341
8342 /* If the inserted register is empty, we need to remove the '"' */
8343 if (need_redraw || stuff_empty())
8344 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008345
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008346 /* Disallow starting Visual mode here, would get a weird mode. */
8347 if (!vis_active && VIsual_active)
8348 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349}
8350
8351/*
8352 * CTRL-G commands in Insert mode.
8353 */
8354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008355ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
8357 int c;
8358
8359#ifdef FEAT_INS_EXPAND
8360 /* Right after CTRL-X the cursor will be after the ruler. */
8361 setcursor();
8362#endif
8363
8364 /*
8365 * Don't map the second key. This also prevents the mode message to be
8366 * deleted when ESC is hit.
8367 */
8368 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008369 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 --no_mapping;
8371 switch (c)
8372 {
8373 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8374 case K_UP:
8375 case Ctrl_K:
8376 case 'k': ins_up(TRUE);
8377 break;
8378
8379 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8380 case K_DOWN:
8381 case Ctrl_J:
8382 case 'j': ins_down(TRUE);
8383 break;
8384
8385 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008386 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008388
8389 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008390 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008391 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008392 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 break;
8394
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008395 /* CTRL-G U: do not break undo with the next char */
8396 case 'U':
8397 /* Allow one left/right cursor movement with the next char,
8398 * without breaking undo. */
8399 dont_sync_undo = MAYBE;
8400 break;
8401
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008403 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 }
8405}
8406
8407/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008408 * CTRL-^ in Insert mode.
8409 */
8410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008411ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008412{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008413 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008414 {
8415 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8416 if (State & LANGMAP)
8417 {
8418 curbuf->b_p_iminsert = B_IMODE_NONE;
8419 State &= ~LANGMAP;
8420 }
8421 else
8422 {
8423 curbuf->b_p_iminsert = B_IMODE_LMAP;
8424 State |= LANGMAP;
8425#ifdef USE_IM_CONTROL
8426 im_set_active(FALSE);
8427#endif
8428 }
8429 }
8430#ifdef USE_IM_CONTROL
8431 else
8432 {
8433 /* There are no ":lmap" mappings, toggle IM */
8434 if (im_get_status())
8435 {
8436 curbuf->b_p_iminsert = B_IMODE_NONE;
8437 im_set_active(FALSE);
8438 }
8439 else
8440 {
8441 curbuf->b_p_iminsert = B_IMODE_IM;
8442 State &= ~LANGMAP;
8443 im_set_active(TRUE);
8444 }
8445 }
8446#endif
8447 set_iminsert_global();
8448 showmode();
8449#ifdef FEAT_GUI
8450 /* may show different cursor shape or color */
8451 if (gui.in_use)
8452 gui_update_cursor(TRUE, FALSE);
8453#endif
8454#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8455 /* Show/unshow value of 'keymap' in status lines. */
8456 status_redraw_curbuf();
8457#endif
8458}
8459
8460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 * Handle ESC in insert mode.
8462 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8463 * insert.
8464 */
8465 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008466ins_esc(
8467 long *count,
8468 int cmdchar,
8469 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
8471 int temp;
8472 static int disabled_redraw = FALSE;
8473
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008474#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008475 check_spell_redraw();
8476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477#if defined(FEAT_HANGULIN)
8478# if defined(ESC_CHG_TO_ENG_MODE)
8479 hangul_input_state_set(0);
8480# endif
8481 if (composing_hangul)
8482 {
8483 push_raw_key(composing_hangul_buffer, 2);
8484 composing_hangul = 0;
8485 }
8486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487
8488 temp = curwin->w_cursor.col;
8489 if (disabled_redraw)
8490 {
8491 --RedrawingDisabled;
8492 disabled_redraw = FALSE;
8493 }
8494 if (!arrow_used)
8495 {
8496 /*
8497 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008498 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8499 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 */
8501 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008502 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
8504 /*
8505 * Repeating insert may take a long time. Check for
8506 * interrupt now and then.
8507 */
8508 if (*count > 0)
8509 {
8510 line_breakcheck();
8511 if (got_int)
8512 *count = 0;
8513 }
8514
8515 if (--*count > 0) /* repeat what was typed */
8516 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008517 /* Vi repeats the insert without replacing characters. */
8518 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8519 State &= ~REPLACE_FLAG;
8520
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 (void)start_redo_ins();
8522 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008523 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 ++RedrawingDisabled;
8525 disabled_redraw = TRUE;
8526 return FALSE; /* repeat the insert */
8527 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008528 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 undisplay_dollar();
8530 }
8531
8532 /* When an autoindent was removed, curswant stays after the
8533 * indent */
8534 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8535 curwin->w_set_curswant = TRUE;
8536
8537 /* Remember the last Insert position in the '^ mark. */
8538 if (!cmdmod.keepjumps)
8539 curbuf->b_last_insert = curwin->w_cursor;
8540
8541 /*
8542 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008543 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008545 if (!nomove
8546 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547#ifdef FEAT_VIRTUALEDIT
8548 || curwin->w_cursor.coladd > 0
8549#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008550 )
8551 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008552 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553#ifdef FEAT_RIGHTLEFT
8554 && !revins_on
8555#endif
8556 )
8557 {
8558#ifdef FEAT_VIRTUALEDIT
8559 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8560 {
8561 oneleft();
8562 if (restart_edit != NUL)
8563 ++curwin->w_cursor.coladd;
8564 }
8565 else
8566#endif
8567 {
8568 --curwin->w_cursor.col;
8569#ifdef FEAT_MBYTE
8570 /* Correct cursor for multi-byte character. */
8571 if (has_mbyte)
8572 mb_adjust_cursor();
8573#endif
8574 }
8575 }
8576
8577#ifdef USE_IM_CONTROL
8578 /* Disable IM to allow typing English directly for Normal mode commands.
8579 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8580 * well). */
8581 if (!(State & LANGMAP))
8582 im_save_status(&curbuf->b_p_iminsert);
8583 im_set_active(FALSE);
8584#endif
8585
8586 State = NORMAL;
8587 /* need to position cursor again (e.g. when on a TAB ) */
8588 changed_cline_bef_curs();
8589
8590#ifdef FEAT_MOUSE
8591 setmouse();
8592#endif
8593#ifdef CURSOR_SHAPE
8594 ui_cursor_shape(); /* may show different cursor shape */
8595#endif
8596
8597 /*
8598 * When recording or for CTRL-O, need to display the new mode.
8599 * Otherwise remove the mode message.
8600 */
8601 if (Recording || restart_edit != NUL)
8602 showmode();
8603 else if (p_smd)
8604 MSG("");
8605
8606 return TRUE; /* exit Insert mode */
8607}
8608
8609#ifdef FEAT_RIGHTLEFT
8610/*
8611 * Toggle language: hkmap and revins_on.
8612 * Move to end of reverse inserted text.
8613 */
8614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008615ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 if (revins_on && revins_chars && revins_scol >= 0)
8618 {
8619 while (gchar_cursor() != NUL && revins_chars--)
8620 ++curwin->w_cursor.col;
8621 }
8622 p_ri = !p_ri;
8623 revins_on = (State == INSERT && p_ri);
8624 if (revins_on)
8625 {
8626 revins_scol = curwin->w_cursor.col;
8627 revins_legal++;
8628 revins_chars = 0;
8629 undisplay_dollar();
8630 }
8631 else
8632 revins_scol = -1;
8633#ifdef FEAT_FKMAP
8634 if (p_altkeymap)
8635 {
8636 /*
8637 * to be consistent also for redo command, using '.'
8638 * set arrow_used to true and stop it - causing to redo
8639 * characters entered in one mode (normal/reverse insert).
8640 */
8641 arrow_used = TRUE;
8642 (void)stop_arrow();
8643 p_fkmap = curwin->w_p_rl ^ p_ri;
8644 if (p_fkmap && p_ri)
8645 State = INSERT;
8646 }
8647 else
8648#endif
8649 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8650 showmode();
8651}
8652#endif
8653
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654/*
8655 * If 'keymodel' contains "startsel", may start selection.
8656 * Returns TRUE when a CTRL-O and other keys stuffed.
8657 */
8658 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008659ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
8661 if (km_startsel)
8662 switch (c)
8663 {
8664 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 case K_PAGEUP:
8667 case K_KPAGEUP:
8668 case K_PAGEDOWN:
8669 case K_KPAGEDOWN:
8670# ifdef MACOS
8671 case K_LEFT:
8672 case K_RIGHT:
8673 case K_UP:
8674 case K_DOWN:
8675 case K_END:
8676 case K_HOME:
8677# endif
8678 if (!(mod_mask & MOD_MASK_SHIFT))
8679 break;
8680 /* FALLTHROUGH */
8681 case K_S_LEFT:
8682 case K_S_RIGHT:
8683 case K_S_UP:
8684 case K_S_DOWN:
8685 case K_S_END:
8686 case K_S_HOME:
8687 /* Start selection right away, the cursor can move with
8688 * CTRL-O when beyond the end of the line. */
8689 start_selection();
8690
8691 /* Execute the key in (insert) Select mode. */
8692 stuffcharReadbuff(Ctrl_O);
8693 if (mod_mask)
8694 {
8695 char_u buf[4];
8696
8697 buf[0] = K_SPECIAL;
8698 buf[1] = KS_MODIFIER;
8699 buf[2] = mod_mask;
8700 buf[3] = NUL;
8701 stuffReadbuff(buf);
8702 }
8703 stuffcharReadbuff(c);
8704 return TRUE;
8705 }
8706 return FALSE;
8707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708
8709/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008710 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008711 */
8712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008713ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008714{
8715#ifdef FEAT_FKMAP
8716 if (p_fkmap && p_ri)
8717 {
8718 beep_flush();
8719 EMSG(farsi_text_3); /* encoded in Farsi */
8720 return;
8721 }
8722#endif
8723
8724#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008725# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008726 set_vim_var_string(VV_INSERTMODE,
8727 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008728# ifdef FEAT_VREPLACE
8729 replaceState == VREPLACE ? "v" :
8730# endif
8731 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008732# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008733 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8734#endif
8735 if (State & REPLACE_FLAG)
8736 State = INSERT | (State & LANGMAP);
8737 else
8738 State = replaceState | (State & LANGMAP);
8739 AppendCharToRedobuff(K_INS);
8740 showmode();
8741#ifdef CURSOR_SHAPE
8742 ui_cursor_shape(); /* may show different cursor shape */
8743#endif
8744}
8745
8746/*
8747 * Pressed CTRL-O in Insert mode.
8748 */
8749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008750ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008751{
8752#ifdef FEAT_VREPLACE
8753 if (State & VREPLACE_FLAG)
8754 restart_edit = 'V';
8755 else
8756#endif
8757 if (State & REPLACE_FLAG)
8758 restart_edit = 'R';
8759 else
8760 restart_edit = 'I';
8761#ifdef FEAT_VIRTUALEDIT
8762 if (virtual_active())
8763 ins_at_eol = FALSE; /* cursor always keeps its column */
8764 else
8765#endif
8766 ins_at_eol = (gchar_cursor() == NUL);
8767}
8768
8769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 * If the cursor is on an indent, ^T/^D insert/delete one
8771 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008772 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 * with vi. But vi only supports ^T and ^D after an
8774 * autoindent, we support it everywhere.
8775 */
8776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008777ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778{
8779 if (stop_arrow() == FAIL)
8780 return;
8781 AppendCharToRedobuff(c);
8782
8783 /*
8784 * 0^D and ^^D: remove all indent.
8785 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008786 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8787 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 {
8789 --curwin->w_cursor.col;
8790 (void)del_char(FALSE); /* delete the '^' or '0' */
8791 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8792 if (State & REPLACE_FLAG)
8793 replace_pop_ins();
8794 if (lastc == '^')
8795 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008796 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 }
8798 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008799 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800
8801 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8802 did_ai = FALSE;
8803#ifdef FEAT_SMARTINDENT
8804 did_si = FALSE;
8805 can_si = FALSE;
8806 can_si_back = FALSE;
8807#endif
8808#ifdef FEAT_CINDENT
8809 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8810#endif
8811}
8812
8813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008814ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
8816 int temp;
8817
8818 if (stop_arrow() == FAIL)
8819 return;
8820 if (gchar_cursor() == NUL) /* delete newline */
8821 {
8822 temp = curwin->w_cursor.col;
8823 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008824 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008825 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 else
8827 curwin->w_cursor.col = temp;
8828 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008829 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8830 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 did_ai = FALSE;
8832#ifdef FEAT_SMARTINDENT
8833 did_si = FALSE;
8834 can_si = FALSE;
8835 can_si_back = FALSE;
8836#endif
8837 AppendCharToRedobuff(K_DEL);
8838}
8839
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008840static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008841
8842/*
8843 * Delete one character for ins_bs().
8844 */
8845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008846ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008847{
8848 dec_cursor();
8849 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8850 if (State & REPLACE_FLAG)
8851 {
8852 /* Don't delete characters before the insert point when in
8853 * Replace mode */
8854 if (curwin->w_cursor.lnum != Insstart.lnum
8855 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008856 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008857 }
8858 else
8859 (void)del_char(FALSE);
8860}
8861
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862/*
8863 * Handle Backspace, delete-word and delete-line in Insert mode.
8864 * Return TRUE when backspace was actually used.
8865 */
8866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008867ins_bs(
8868 int c,
8869 int mode,
8870 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
8872 linenr_T lnum;
8873 int cc;
8874 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008875 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 colnr_T mincol;
8877 int did_backspace = FALSE;
8878 int in_indent;
8879 int oldState;
8880#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008881 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882#endif
8883
8884 /*
8885 * can't delete anything in an empty file
8886 * can't backup past first character in buffer
8887 * can't backup past starting point unless 'backspace' > 1
8888 * can backup to a previous line if 'backspace' == 0
8889 */
8890 if ( bufempty()
8891 || (
8892#ifdef FEAT_RIGHTLEFT
8893 !revins_on &&
8894#endif
8895 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8896 || (!can_bs(BS_START)
8897 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008898 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8899 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8901 && curwin->w_cursor.col <= ai_col)
8902 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8903 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008904 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 return FALSE;
8906 }
8907
8908 if (stop_arrow() == FAIL)
8909 return FALSE;
8910 in_indent = inindent(0);
8911#ifdef FEAT_CINDENT
8912 if (in_indent)
8913 can_cindent = FALSE;
8914#endif
8915#ifdef FEAT_COMMENTS
8916 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8917#endif
8918#ifdef FEAT_RIGHTLEFT
8919 if (revins_on) /* put cursor after last inserted char */
8920 inc_cursor();
8921#endif
8922
8923#ifdef FEAT_VIRTUALEDIT
8924 /* Virtualedit:
8925 * BACKSPACE_CHAR eats a virtual space
8926 * BACKSPACE_WORD eats all coladd
8927 * BACKSPACE_LINE eats all coladd and keeps going
8928 */
8929 if (curwin->w_cursor.coladd > 0)
8930 {
8931 if (mode == BACKSPACE_CHAR)
8932 {
8933 --curwin->w_cursor.coladd;
8934 return TRUE;
8935 }
8936 if (mode == BACKSPACE_WORD)
8937 {
8938 curwin->w_cursor.coladd = 0;
8939 return TRUE;
8940 }
8941 curwin->w_cursor.coladd = 0;
8942 }
8943#endif
8944
8945 /*
8946 * delete newline!
8947 */
8948 if (curwin->w_cursor.col == 0)
8949 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008950 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008951 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952#ifdef FEAT_RIGHTLEFT
8953 || revins_on
8954#endif
8955 )
8956 {
8957 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8958 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8959 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008960 --Insstart.lnum;
8961 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 }
8963 /*
8964 * In replace mode:
8965 * cc < 0: NL was inserted, delete it
8966 * cc >= 0: NL was replaced, put original characters back
8967 */
8968 cc = -1;
8969 if (State & REPLACE_FLAG)
8970 cc = replace_pop(); /* returns -1 if NL was inserted */
8971 /*
8972 * In replace mode, in the line we started replacing, we only move the
8973 * cursor.
8974 */
8975 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8976 {
8977 dec_cursor();
8978 }
8979 else
8980 {
8981#ifdef FEAT_VREPLACE
8982 if (!(State & VREPLACE_FLAG)
8983 || curwin->w_cursor.lnum > orig_line_count)
8984#endif
8985 {
8986 temp = gchar_cursor(); /* remember current char */
8987 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008988
8989 /* When "aw" is in 'formatoptions' we must delete the space at
8990 * the end of the line, otherwise the line will be broken
8991 * again when auto-formatting. */
8992 if (has_format_option(FO_AUTO)
8993 && has_format_option(FO_WHITE_PAR))
8994 {
8995 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8996 TRUE);
8997 int len;
8998
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008999 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009000 if (len > 0 && ptr[len - 1] == ' ')
9001 ptr[len - 1] = NUL;
9002 }
9003
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009004 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 if (temp == NUL && gchar_cursor() != NUL)
9006 inc_cursor();
9007 }
9008#ifdef FEAT_VREPLACE
9009 else
9010 dec_cursor();
9011#endif
9012
9013 /*
9014 * In REPLACE mode we have to put back the text that was replaced
9015 * by the NL. On the replace stack is first a NUL-terminated
9016 * sequence of characters that were deleted and then the
9017 * characters that NL replaced.
9018 */
9019 if (State & REPLACE_FLAG)
9020 {
9021 /*
9022 * Do the next ins_char() in NORMAL state, to
9023 * prevent ins_char() from replacing characters and
9024 * avoiding showmatch().
9025 */
9026 oldState = State;
9027 State = NORMAL;
9028 /*
9029 * restore characters (blanks) deleted after cursor
9030 */
9031 while (cc > 0)
9032 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009033 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034#ifdef FEAT_MBYTE
9035 mb_replace_pop_ins(cc);
9036#else
9037 ins_char(cc);
9038#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009039 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 cc = replace_pop();
9041 }
9042 /* restore the characters that NL replaced */
9043 replace_pop_ins();
9044 State = oldState;
9045 }
9046 }
9047 did_ai = FALSE;
9048 }
9049 else
9050 {
9051 /*
9052 * Delete character(s) before the cursor.
9053 */
9054#ifdef FEAT_RIGHTLEFT
9055 if (revins_on) /* put cursor on last inserted char */
9056 dec_cursor();
9057#endif
9058 mincol = 0;
9059 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009060 if (mode == BACKSPACE_LINE
9061 && (curbuf->b_p_ai
9062#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009063 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009064#endif
9065 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066#ifdef FEAT_RIGHTLEFT
9067 && !revins_on
9068#endif
9069 )
9070 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009071 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009073 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009075 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 }
9077
9078 /*
9079 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9080 */
9081 if ( mode == BACKSPACE_CHAR
9082 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009083 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009084 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 && (*(ml_get_cursor() - 1) == TAB
9086 || (*(ml_get_cursor() - 1) == ' '
9087 && (!*inserted_space_p
9088 || arrow_used))))))
9089 {
9090 int ts;
9091 colnr_T vcol;
9092 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009093 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094
9095 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009096 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009097 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009099 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 /* Compute the virtual column where we want to be. Since
9101 * 'showbreak' may get in the way, need to get the last column of
9102 * the previous character. */
9103 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009104 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 dec_cursor();
9106 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9107 inc_cursor();
9108 want_vcol = (want_vcol / ts) * ts;
9109
9110 /* delete characters until we are at or before want_vcol */
9111 while (vcol > want_vcol
9112 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009113 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114
9115 /* insert extra spaces until we are at want_vcol */
9116 while (vcol < want_vcol)
9117 {
9118 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009119 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9120 && curwin->w_cursor.col < Insstart_orig.col)
9121 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122
9123#ifdef FEAT_VREPLACE
9124 if (State & VREPLACE_FLAG)
9125 ins_char(' ');
9126 else
9127#endif
9128 {
9129 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009130 if ((State & REPLACE_FLAG))
9131 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 }
9133 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9134 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009135
9136 /* If we are now back where we started delete one character. Can
9137 * happen when using 'sts' and 'linebreak'. */
9138 if (vcol >= start_vcol)
9139 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 }
9141
9142 /*
9143 * Delete upto starting point, start of line or previous word.
9144 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009145 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009147#ifdef FEAT_MBYTE
9148 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009150 if (has_mbyte)
9151 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009153 do
9154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009156 if (!revins_on) /* put cursor on char to be deleted */
9157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009159
9160 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009162 /* look multi-byte character class */
9163 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009165 prev_cclass = cclass;
9166 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009169
9170 /* start of word? */
9171 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9172 {
9173 mode = BACKSPACE_WORD_NOT_SPACE;
9174 temp = vim_iswordc(cc);
9175 }
9176 /* end of word? */
9177 else if (mode == BACKSPACE_WORD_NOT_SPACE
9178 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9179#ifdef FEAT_MBYTE
9180 || prev_cclass != cclass
9181#endif
9182 ))
9183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009185 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009187 inc_cursor();
9188#ifdef FEAT_RIGHTLEFT
9189 else if (State & REPLACE_FLAG)
9190 dec_cursor();
9191#endif
9192 break;
9193 }
9194 if (State & REPLACE_FLAG)
9195 replace_do_bs(-1);
9196 else
9197 {
9198#ifdef FEAT_MBYTE
9199 if (enc_utf8 && p_deco)
9200 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9201#endif
9202 (void)del_char(FALSE);
9203#ifdef FEAT_MBYTE
9204 /*
9205 * If there are combining characters and 'delcombine' is set
9206 * move the cursor back. Don't back up before the base
9207 * character.
9208 */
9209 if (enc_utf8 && p_deco && cpc[0] != NUL)
9210 inc_cursor();
9211#endif
9212#ifdef FEAT_RIGHTLEFT
9213 if (revins_chars)
9214 {
9215 revins_chars--;
9216 revins_legal++;
9217 }
9218 if (revins_on && gchar_cursor() == NUL)
9219 break;
9220#endif
9221 }
9222 /* Just a single backspace?: */
9223 if (mode == BACKSPACE_CHAR)
9224 break;
9225 } while (
9226#ifdef FEAT_RIGHTLEFT
9227 revins_on ||
9228#endif
9229 (curwin->w_cursor.col > mincol
9230 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9231 || curwin->w_cursor.col != Insstart_orig.col)));
9232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 did_backspace = TRUE;
9234 }
9235#ifdef FEAT_SMARTINDENT
9236 did_si = FALSE;
9237 can_si = FALSE;
9238 can_si_back = FALSE;
9239#endif
9240 if (curwin->w_cursor.col <= 1)
9241 did_ai = FALSE;
9242 /*
9243 * It's a little strange to put backspaces into the redo
9244 * buffer, but it makes auto-indent a lot easier to deal
9245 * with.
9246 */
9247 AppendCharToRedobuff(c);
9248
9249 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009250 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9251 && curwin->w_cursor.col < Insstart_orig.col)
9252 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253
9254 /* vi behaviour: the cursor moves backward but the character that
9255 * was there remains visible
9256 * Vim behaviour: the cursor moves backward and the character that
9257 * was there is erased from the screen.
9258 * We can emulate the vi behaviour by pretending there is a dollar
9259 * displayed even when there isn't.
9260 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009261 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 dollar_vcol = curwin->w_virtcol;
9263
Bram Moolenaarce3be472008-01-14 19:12:28 +00009264#ifdef FEAT_FOLDING
9265 /* When deleting a char the cursor line must never be in a closed fold.
9266 * E.g., when 'foldmethod' is indent and deleting the first non-white
9267 * char before a Tab. */
9268 if (did_backspace)
9269 foldOpenCursor();
9270#endif
9271
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 return did_backspace;
9273}
9274
9275#ifdef FEAT_MOUSE
9276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009277ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
9279 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009280 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281
9282# ifdef FEAT_GUI
9283 /* When GUI is active, also move/paste when 'mouse' is empty */
9284 if (!gui.in_use)
9285# endif
9286 if (!mouse_has(MOUSE_INSERT))
9287 return;
9288
9289 undisplay_dollar();
9290 tpos = curwin->w_cursor;
9291 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9292 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009293#ifdef FEAT_WINDOWS
9294 win_T *new_curwin = curwin;
9295
9296 if (curwin != old_curwin && win_valid(old_curwin))
9297 {
9298 /* Mouse took us to another window. We need to go back to the
9299 * previous one to stop insert there properly. */
9300 curwin = old_curwin;
9301 curbuf = curwin->w_buffer;
9302 }
9303#endif
9304 start_arrow(curwin == old_curwin ? &tpos : NULL);
9305#ifdef FEAT_WINDOWS
9306 if (curwin != new_curwin && win_valid(new_curwin))
9307 {
9308 curwin = new_curwin;
9309 curbuf = curwin->w_buffer;
9310 }
9311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312# ifdef FEAT_CINDENT
9313 can_cindent = TRUE;
9314# endif
9315 }
9316
9317#ifdef FEAT_WINDOWS
9318 /* redraw status lines (in case another window became active) */
9319 redraw_statuslines();
9320#endif
9321}
9322
9323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009324ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325{
9326 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009327# if defined(FEAT_WINDOWS)
9328 win_T *old_curwin = curwin;
9329# endif
9330# ifdef FEAT_INS_EXPAND
9331 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332# endif
9333
9334 tpos = curwin->w_cursor;
9335
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009336# ifdef FEAT_WINDOWS
9337 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 {
9339 int row, col;
9340
9341 row = mouse_row;
9342 col = mouse_col;
9343
9344 /* find the window at the pointer coordinates */
9345 curwin = mouse_find_win(&row, &col);
9346 curbuf = curwin->w_buffer;
9347 }
9348 if (curwin == old_curwin)
9349# endif
9350 undisplay_dollar();
9351
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009352# ifdef FEAT_INS_EXPAND
9353 /* Don't scroll the window in which completion is being done. */
9354 if (!pum_visible()
9355# if defined(FEAT_WINDOWS)
9356 || curwin != old_curwin
9357# endif
9358 )
9359# endif
9360 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009361 if (dir == MSCR_DOWN || dir == MSCR_UP)
9362 {
9363 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9364 scroll_redraw(dir,
9365 (long)(curwin->w_botline - curwin->w_topline));
9366 else
9367 scroll_redraw(dir, 3L);
9368 }
9369#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009370 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009371 {
9372 int val, step = 6;
9373
9374 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9375 step = W_WIDTH(curwin);
9376 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9377 if (val < 0)
9378 val = 0;
9379 gui_do_horiz_scroll(val, TRUE);
9380 }
9381#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009382# ifdef FEAT_INS_EXPAND
9383 did_scroll = TRUE;
9384# endif
9385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009387# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 curwin->w_redr_status = TRUE;
9389
9390 curwin = old_curwin;
9391 curbuf = curwin->w_buffer;
9392# endif
9393
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009394# ifdef FEAT_INS_EXPAND
9395 /* The popup menu may overlay the window, need to redraw it.
9396 * TODO: Would be more efficient to only redraw the windows that are
9397 * overlapped by the popup menu. */
9398 if (pum_visible() && did_scroll)
9399 {
9400 redraw_all_later(NOT_VALID);
9401 ins_compl_show_pum();
9402 }
9403# endif
9404
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 if (!equalpos(curwin->w_cursor, tpos))
9406 {
9407 start_arrow(&tpos);
9408# ifdef FEAT_CINDENT
9409 can_cindent = TRUE;
9410# endif
9411 }
9412}
9413#endif
9414
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009415#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009417ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009418{
9419 /* We will be leaving the current window, unless closing another tab. */
9420 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9421 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9422 {
9423 undisplay_dollar();
9424 start_arrow(&curwin->w_cursor);
9425# ifdef FEAT_CINDENT
9426 can_cindent = TRUE;
9427# endif
9428 }
9429
9430 if (c == K_TABLINE)
9431 goto_tabpage(current_tab);
9432 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009433 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009434 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009435 redraw_statuslines(); /* will redraw the tabline when needed */
9436 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009437}
9438#endif
9439
9440#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009442ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443{
9444 pos_T tpos;
9445
9446 undisplay_dollar();
9447 tpos = curwin->w_cursor;
9448 if (gui_do_scroll())
9449 {
9450 start_arrow(&tpos);
9451# ifdef FEAT_CINDENT
9452 can_cindent = TRUE;
9453# endif
9454 }
9455}
9456
9457 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009458ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459{
9460 pos_T tpos;
9461
9462 undisplay_dollar();
9463 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009464 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 {
9466 start_arrow(&tpos);
9467# ifdef FEAT_CINDENT
9468 can_cindent = TRUE;
9469# endif
9470 }
9471}
9472#endif
9473
9474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009475ins_left(
9476 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477{
9478 pos_T tpos;
9479
9480#ifdef FEAT_FOLDING
9481 if ((fdo_flags & FDO_HOR) && KeyTyped)
9482 foldOpenCursor();
9483#endif
9484 undisplay_dollar();
9485 tpos = curwin->w_cursor;
9486 if (oneleft() == OK)
9487 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009488#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9489 /* Only call start_arrow() when not busy with preediting, it will
9490 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9491 if (!im_is_preediting())
9492#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009493 {
9494 start_arrow_with_change(&tpos, end_change);
9495 if (!end_change)
9496 AppendCharToRedobuff(K_LEFT);
9497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498#ifdef FEAT_RIGHTLEFT
9499 /* If exit reversed string, position is fixed */
9500 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9501 revins_legal++;
9502 revins_chars++;
9503#endif
9504 }
9505
9506 /*
9507 * if 'whichwrap' set for cursor in insert mode may go to
9508 * previous line
9509 */
9510 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9511 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009512 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 start_arrow(&tpos);
9514 --(curwin->w_cursor.lnum);
9515 coladvance((colnr_T)MAXCOL);
9516 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9517 }
9518 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009519 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009520 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521}
9522
9523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009524ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525{
9526 pos_T tpos;
9527
9528#ifdef FEAT_FOLDING
9529 if ((fdo_flags & FDO_HOR) && KeyTyped)
9530 foldOpenCursor();
9531#endif
9532 undisplay_dollar();
9533 tpos = curwin->w_cursor;
9534 if (c == K_C_HOME)
9535 curwin->w_cursor.lnum = 1;
9536 curwin->w_cursor.col = 0;
9537#ifdef FEAT_VIRTUALEDIT
9538 curwin->w_cursor.coladd = 0;
9539#endif
9540 curwin->w_curswant = 0;
9541 start_arrow(&tpos);
9542}
9543
9544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009545ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546{
9547 pos_T tpos;
9548
9549#ifdef FEAT_FOLDING
9550 if ((fdo_flags & FDO_HOR) && KeyTyped)
9551 foldOpenCursor();
9552#endif
9553 undisplay_dollar();
9554 tpos = curwin->w_cursor;
9555 if (c == K_C_END)
9556 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9557 coladvance((colnr_T)MAXCOL);
9558 curwin->w_curswant = MAXCOL;
9559
9560 start_arrow(&tpos);
9561}
9562
9563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009564ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565{
9566#ifdef FEAT_FOLDING
9567 if ((fdo_flags & FDO_HOR) && KeyTyped)
9568 foldOpenCursor();
9569#endif
9570 undisplay_dollar();
9571 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9572 {
9573 start_arrow(&curwin->w_cursor);
9574 (void)bck_word(1L, FALSE, FALSE);
9575 curwin->w_set_curswant = TRUE;
9576 }
9577 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009578 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579}
9580
9581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009582ins_right(
9583 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584{
9585#ifdef FEAT_FOLDING
9586 if ((fdo_flags & FDO_HOR) && KeyTyped)
9587 foldOpenCursor();
9588#endif
9589 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009590 if (gchar_cursor() != NUL
9591#ifdef FEAT_VIRTUALEDIT
9592 || virtual_active()
9593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 )
9595 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009596 start_arrow_with_change(&curwin->w_cursor, end_change);
9597 if (!end_change)
9598 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 curwin->w_set_curswant = TRUE;
9600#ifdef FEAT_VIRTUALEDIT
9601 if (virtual_active())
9602 oneright();
9603 else
9604#endif
9605 {
9606#ifdef FEAT_MBYTE
9607 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009608 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 else
9610#endif
9611 ++curwin->w_cursor.col;
9612 }
9613
9614#ifdef FEAT_RIGHTLEFT
9615 revins_legal++;
9616 if (revins_chars)
9617 revins_chars--;
9618#endif
9619 }
9620 /* if 'whichwrap' set for cursor in insert mode, may move the
9621 * cursor to the next line */
9622 else if (vim_strchr(p_ww, ']') != NULL
9623 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9624 {
9625 start_arrow(&curwin->w_cursor);
9626 curwin->w_set_curswant = TRUE;
9627 ++curwin->w_cursor.lnum;
9628 curwin->w_cursor.col = 0;
9629 }
9630 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009631 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009632 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633}
9634
9635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009636ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637{
9638#ifdef FEAT_FOLDING
9639 if ((fdo_flags & FDO_HOR) && KeyTyped)
9640 foldOpenCursor();
9641#endif
9642 undisplay_dollar();
9643 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9644 || gchar_cursor() != NUL)
9645 {
9646 start_arrow(&curwin->w_cursor);
9647 (void)fwd_word(1L, FALSE, 0);
9648 curwin->w_set_curswant = TRUE;
9649 }
9650 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009651 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652}
9653
9654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009655ins_up(
9656 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
9658 pos_T tpos;
9659 linenr_T old_topline = curwin->w_topline;
9660#ifdef FEAT_DIFF
9661 int old_topfill = curwin->w_topfill;
9662#endif
9663
9664 undisplay_dollar();
9665 tpos = curwin->w_cursor;
9666 if (cursor_up(1L, TRUE) == OK)
9667 {
9668 if (startcol)
9669 coladvance(getvcol_nolist(&Insstart));
9670 if (old_topline != curwin->w_topline
9671#ifdef FEAT_DIFF
9672 || old_topfill != curwin->w_topfill
9673#endif
9674 )
9675 redraw_later(VALID);
9676 start_arrow(&tpos);
9677#ifdef FEAT_CINDENT
9678 can_cindent = TRUE;
9679#endif
9680 }
9681 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009682 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683}
9684
9685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009686ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
9688 pos_T tpos;
9689
9690 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009691
9692#ifdef FEAT_WINDOWS
9693 if (mod_mask & MOD_MASK_CTRL)
9694 {
9695 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009696 if (first_tabpage->tp_next != NULL)
9697 {
9698 start_arrow(&curwin->w_cursor);
9699 goto_tabpage(-1);
9700 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009701 return;
9702 }
9703#endif
9704
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 tpos = curwin->w_cursor;
9706 if (onepage(BACKWARD, 1L) == OK)
9707 {
9708 start_arrow(&tpos);
9709#ifdef FEAT_CINDENT
9710 can_cindent = TRUE;
9711#endif
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_down(
9719 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
9721 pos_T tpos;
9722 linenr_T old_topline = curwin->w_topline;
9723#ifdef FEAT_DIFF
9724 int old_topfill = curwin->w_topfill;
9725#endif
9726
9727 undisplay_dollar();
9728 tpos = curwin->w_cursor;
9729 if (cursor_down(1L, TRUE) == OK)
9730 {
9731 if (startcol)
9732 coladvance(getvcol_nolist(&Insstart));
9733 if (old_topline != curwin->w_topline
9734#ifdef FEAT_DIFF
9735 || old_topfill != curwin->w_topfill
9736#endif
9737 )
9738 redraw_later(VALID);
9739 start_arrow(&tpos);
9740#ifdef FEAT_CINDENT
9741 can_cindent = TRUE;
9742#endif
9743 }
9744 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009745 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746}
9747
9748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009749ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750{
9751 pos_T tpos;
9752
9753 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009754
9755#ifdef FEAT_WINDOWS
9756 if (mod_mask & MOD_MASK_CTRL)
9757 {
9758 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009759 if (first_tabpage->tp_next != NULL)
9760 {
9761 start_arrow(&curwin->w_cursor);
9762 goto_tabpage(0);
9763 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009764 return;
9765 }
9766#endif
9767
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 tpos = curwin->w_cursor;
9769 if (onepage(FORWARD, 1L) == OK)
9770 {
9771 start_arrow(&tpos);
9772#ifdef FEAT_CINDENT
9773 can_cindent = TRUE;
9774#endif
9775 }
9776 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009777 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778}
9779
9780#ifdef FEAT_DND
9781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009782ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
9784 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9785}
9786#endif
9787
9788/*
9789 * Handle TAB in Insert or Replace mode.
9790 * Return TRUE when the TAB needs to be inserted like a normal character.
9791 */
9792 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009793ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794{
9795 int ind;
9796 int i;
9797 int temp;
9798
9799 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9800 Insstart_blank_vcol = get_nolist_virtcol();
9801 if (echeck_abbr(TAB + ABBR_OFF))
9802 return FALSE;
9803
9804 ind = inindent(0);
9805#ifdef FEAT_CINDENT
9806 if (ind)
9807 can_cindent = FALSE;
9808#endif
9809
9810 /*
9811 * When nothing special, insert TAB like a normal character
9812 */
9813 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009814 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009815 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 return TRUE;
9817
9818 if (stop_arrow() == FAIL)
9819 return TRUE;
9820
9821 did_ai = FALSE;
9822#ifdef FEAT_SMARTINDENT
9823 did_si = FALSE;
9824 can_si = FALSE;
9825 can_si_back = FALSE;
9826#endif
9827 AppendToRedobuff((char_u *)"\t");
9828
9829 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009830 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009831 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9832 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 else /* otherwise use 'tabstop' */
9834 temp = (int)curbuf->b_p_ts;
9835 temp -= get_nolist_virtcol() % temp;
9836
9837 /*
9838 * Insert the first space with ins_char(). It will delete one char in
9839 * replace mode. Insert the rest with ins_str(); it will not delete any
9840 * chars. For VREPLACE mode, we use ins_char() for all characters.
9841 */
9842 ins_char(' ');
9843 while (--temp > 0)
9844 {
9845#ifdef FEAT_VREPLACE
9846 if (State & VREPLACE_FLAG)
9847 ins_char(' ');
9848 else
9849#endif
9850 {
9851 ins_str((char_u *)" ");
9852 if (State & REPLACE_FLAG) /* no char replaced */
9853 replace_push(NUL);
9854 }
9855 }
9856
9857 /*
9858 * When 'expandtab' not set: Replace spaces by TABs where possible.
9859 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009860 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 {
9862 char_u *ptr;
9863#ifdef FEAT_VREPLACE
9864 char_u *saved_line = NULL; /* init for GCC */
9865 pos_T pos;
9866#endif
9867 pos_T fpos;
9868 pos_T *cursor;
9869 colnr_T want_vcol, vcol;
9870 int change_col = -1;
9871 int save_list = curwin->w_p_list;
9872
9873 /*
9874 * Get the current line. For VREPLACE mode, don't make real changes
9875 * yet, just work on a copy of the line.
9876 */
9877#ifdef FEAT_VREPLACE
9878 if (State & VREPLACE_FLAG)
9879 {
9880 pos = curwin->w_cursor;
9881 cursor = &pos;
9882 saved_line = vim_strsave(ml_get_curline());
9883 if (saved_line == NULL)
9884 return FALSE;
9885 ptr = saved_line + pos.col;
9886 }
9887 else
9888#endif
9889 {
9890 ptr = ml_get_cursor();
9891 cursor = &curwin->w_cursor;
9892 }
9893
9894 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9895 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9896 curwin->w_p_list = FALSE;
9897
9898 /* Find first white before the cursor */
9899 fpos = curwin->w_cursor;
9900 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9901 {
9902 --fpos.col;
9903 --ptr;
9904 }
9905
9906 /* In Replace mode, don't change characters before the insert point. */
9907 if ((State & REPLACE_FLAG)
9908 && fpos.lnum == Insstart.lnum
9909 && fpos.col < Insstart.col)
9910 {
9911 ptr += Insstart.col - fpos.col;
9912 fpos.col = Insstart.col;
9913 }
9914
9915 /* compute virtual column numbers of first white and cursor */
9916 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9917 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9918
Bram Moolenaar597a4222014-06-25 14:39:50 +02009919 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9920 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 while (vim_iswhite(*ptr))
9922 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009923 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 if (vcol + i > want_vcol)
9925 break;
9926 if (*ptr != TAB)
9927 {
9928 *ptr = TAB;
9929 if (change_col < 0)
9930 {
9931 change_col = fpos.col; /* Column of first change */
9932 /* May have to adjust Insstart */
9933 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9934 Insstart.col = fpos.col;
9935 }
9936 }
9937 ++fpos.col;
9938 ++ptr;
9939 vcol += i;
9940 }
9941
9942 if (change_col >= 0)
9943 {
9944 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009945 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946
9947 /* Skip over the spaces we need. */
9948 while (vcol < want_vcol && *ptr == ' ')
9949 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009950 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 ++ptr;
9952 ++repl_off;
9953 }
9954 if (vcol > want_vcol)
9955 {
9956 /* Must have a char with 'showbreak' just before it. */
9957 --ptr;
9958 --repl_off;
9959 }
9960 fpos.col += repl_off;
9961
9962 /* Delete following spaces. */
9963 i = cursor->col - fpos.col;
9964 if (i > 0)
9965 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009966 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 /* correct replace stack. */
9968 if ((State & REPLACE_FLAG)
9969#ifdef FEAT_VREPLACE
9970 && !(State & VREPLACE_FLAG)
9971#endif
9972 )
9973 for (temp = i; --temp >= 0; )
9974 replace_join(repl_off);
9975 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009976#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009977 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009978 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009979 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009980 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9981 (char_u *)"\t", 1);
9982 }
9983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 cursor->col -= i;
9985
9986#ifdef FEAT_VREPLACE
9987 /*
9988 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9989 * backspacing over the changed spacing and then inserting the new
9990 * spacing.
9991 */
9992 if (State & VREPLACE_FLAG)
9993 {
9994 /* Backspace from real cursor to change_col */
9995 backspace_until_column(change_col);
9996
9997 /* Insert each char in saved_line from changed_col to
9998 * ptr-cursor */
9999 ins_bytes_len(saved_line + change_col,
10000 cursor->col - change_col);
10001 }
10002#endif
10003 }
10004
10005#ifdef FEAT_VREPLACE
10006 if (State & VREPLACE_FLAG)
10007 vim_free(saved_line);
10008#endif
10009 curwin->w_p_list = save_list;
10010 }
10011
10012 return FALSE;
10013}
10014
10015/*
10016 * Handle CR or NL in insert mode.
10017 * Return TRUE when out of memory or can't undo.
10018 */
10019 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010020ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021{
10022 int i;
10023
10024 if (echeck_abbr(c + ABBR_OFF))
10025 return FALSE;
10026 if (stop_arrow() == FAIL)
10027 return TRUE;
10028 undisplay_dollar();
10029
10030 /*
10031 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10032 * character under the cursor. Only push a NUL on the replace stack,
10033 * nothing to put back when the NL is deleted.
10034 */
10035 if ((State & REPLACE_FLAG)
10036#ifdef FEAT_VREPLACE
10037 && !(State & VREPLACE_FLAG)
10038#endif
10039 )
10040 replace_push(NUL);
10041
10042 /*
10043 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10044 * replacing the next line, so we push all of the characters left on the
10045 * line onto the replace stack. This is not done here though, it is done
10046 * in open_line().
10047 */
10048
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010049#ifdef FEAT_VIRTUALEDIT
10050 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10051 * CTRL-O). */
10052 if (virtual_active() && curwin->w_cursor.coladd > 0)
10053 coladvance(getviscol());
10054#endif
10055
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056#ifdef FEAT_RIGHTLEFT
10057# ifdef FEAT_FKMAP
10058 if (p_altkeymap && p_fkmap)
10059 fkmap(NL);
10060# endif
10061 /* NL in reverse insert will always start in the end of
10062 * current line. */
10063 if (revins_on)
10064 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10065#endif
10066
10067 AppendToRedobuff(NL_STR);
10068 i = open_line(FORWARD,
10069#ifdef FEAT_COMMENTS
10070 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10071#endif
10072 0, old_indent);
10073 old_indent = 0;
10074#ifdef FEAT_CINDENT
10075 can_cindent = TRUE;
10076#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010077#ifdef FEAT_FOLDING
10078 /* When inserting a line the cursor line must never be in a closed fold. */
10079 foldOpenCursor();
10080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081
10082 return (!i);
10083}
10084
10085#ifdef FEAT_DIGRAPHS
10086/*
10087 * Handle digraph in insert mode.
10088 * Returns character still to be inserted, or NUL when nothing remaining to be
10089 * done.
10090 */
10091 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010092ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093{
10094 int c;
10095 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010096 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097
10098 pc_status = PC_STATUS_UNSET;
10099 if (redrawing() && !char_avail())
10100 {
10101 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010102 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103
10104 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010105 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106#ifdef FEAT_CMDL_INFO
10107 add_to_showcmd_c(Ctrl_K);
10108#endif
10109 }
10110
10111#ifdef USE_ON_FLY_SCROLL
10112 dont_scroll = TRUE; /* disallow scrolling here */
10113#endif
10114
10115 /* don't map the digraph chars. This also prevents the
10116 * mode message to be deleted when ESC is hit */
10117 ++no_mapping;
10118 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010119 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 --no_mapping;
10121 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010122 if (did_putchar)
10123 /* when the line fits in 'columns' the '?' is at the start of the next
10124 * line and will not be removed by the redraw */
10125 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010126
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 if (IS_SPECIAL(c) || mod_mask) /* special key */
10128 {
10129#ifdef FEAT_CMDL_INFO
10130 clear_showcmd();
10131#endif
10132 insert_special(c, TRUE, FALSE);
10133 return NUL;
10134 }
10135 if (c != ESC)
10136 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010137 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 if (redrawing() && !char_avail())
10139 {
10140 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010141 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142
10143 if (char2cells(c) == 1)
10144 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010145 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010147 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 }
10149#ifdef FEAT_CMDL_INFO
10150 add_to_showcmd_c(c);
10151#endif
10152 }
10153 ++no_mapping;
10154 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010155 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 --no_mapping;
10157 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010158 if (did_putchar)
10159 /* when the line fits in 'columns' the '?' is at the start of the
10160 * next line and will not be removed by a redraw */
10161 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 if (cc != ESC)
10163 {
10164 AppendToRedobuff((char_u *)CTRL_V_STR);
10165 c = getdigraph(c, cc, TRUE);
10166#ifdef FEAT_CMDL_INFO
10167 clear_showcmd();
10168#endif
10169 return c;
10170 }
10171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172#ifdef FEAT_CMDL_INFO
10173 clear_showcmd();
10174#endif
10175 return NUL;
10176}
10177#endif
10178
10179/*
10180 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10181 * Returns the char to be inserted, or NUL if none found.
10182 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010183 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010184ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185{
10186 int c;
10187 int temp;
10188 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010189 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190
10191 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10192 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010193 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 return NUL;
10195 }
10196
10197 /* try to advance to the cursor column */
10198 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010199 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 prev_ptr = ptr;
10201 validate_virtcol();
10202 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10203 {
10204 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010205 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 }
10207 if ((colnr_T)temp > curwin->w_virtcol)
10208 ptr = prev_ptr;
10209
10210#ifdef FEAT_MBYTE
10211 c = (*mb_ptr2char)(ptr);
10212#else
10213 c = *ptr;
10214#endif
10215 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010216 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 return c;
10218}
10219
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010220/*
10221 * CTRL-Y or CTRL-E typed in Insert mode.
10222 */
10223 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010224ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010225{
10226 int c = tc;
10227
10228#ifdef FEAT_INS_EXPAND
10229 if (ctrl_x_mode == CTRL_X_SCROLL)
10230 {
10231 if (c == Ctrl_Y)
10232 scrolldown_clamp();
10233 else
10234 scrollup_clamp();
10235 redraw_later(VALID);
10236 }
10237 else
10238#endif
10239 {
10240 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10241 if (c != NUL)
10242 {
10243 long tw_save;
10244
10245 /* The character must be taken literally, insert like it
10246 * was typed after a CTRL-V, and pretend 'textwidth'
10247 * wasn't set. Digits, 'o' and 'x' are special after a
10248 * CTRL-V, don't use it for these. */
10249 if (c < 256 && !isalnum(c))
10250 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10251 tw_save = curbuf->b_p_tw;
10252 curbuf->b_p_tw = -1;
10253 insert_special(c, TRUE, FALSE);
10254 curbuf->b_p_tw = tw_save;
10255#ifdef FEAT_RIGHTLEFT
10256 revins_chars++;
10257 revins_legal++;
10258#endif
10259 c = Ctrl_V; /* pretend CTRL-V is last character */
10260 auto_format(FALSE, TRUE);
10261 }
10262 }
10263 return c;
10264}
10265
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266#ifdef FEAT_SMARTINDENT
10267/*
10268 * Try to do some very smart auto-indenting.
10269 * Used when inserting a "normal" character.
10270 */
10271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010272ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273{
10274 pos_T *pos, old_pos;
10275 char_u *ptr;
10276 int i;
10277 int temp;
10278
10279 /*
10280 * do some very smart indenting when entering '{' or '}'
10281 */
10282 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10283 {
10284 /*
10285 * for '}' set indent equal to indent of line containing matching '{'
10286 */
10287 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10288 {
10289 old_pos = curwin->w_cursor;
10290 /*
10291 * If the matching '{' has a ')' immediately before it (ignoring
10292 * white-space), then line up with the start of the line
10293 * containing the matching '(' if there is one. This handles the
10294 * case where an "if (..\n..) {" statement continues over multiple
10295 * lines -- webb
10296 */
10297 ptr = ml_get(pos->lnum);
10298 i = pos->col;
10299 if (i > 0) /* skip blanks before '{' */
10300 while (--i > 0 && vim_iswhite(ptr[i]))
10301 ;
10302 curwin->w_cursor.lnum = pos->lnum;
10303 curwin->w_cursor.col = i;
10304 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10305 curwin->w_cursor = *pos;
10306 i = get_indent();
10307 curwin->w_cursor = old_pos;
10308#ifdef FEAT_VREPLACE
10309 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010310 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 else
10312#endif
10313 (void)set_indent(i, SIN_CHANGED);
10314 }
10315 else if (curwin->w_cursor.col > 0)
10316 {
10317 /*
10318 * when inserting '{' after "O" reduce indent, but not
10319 * more than indent of previous line
10320 */
10321 temp = TRUE;
10322 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10323 {
10324 old_pos = curwin->w_cursor;
10325 i = get_indent();
10326 while (curwin->w_cursor.lnum > 1)
10327 {
10328 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10329
10330 /* ignore empty lines and lines starting with '#'. */
10331 if (*ptr != '#' && *ptr != NUL)
10332 break;
10333 }
10334 if (get_indent() >= i)
10335 temp = FALSE;
10336 curwin->w_cursor = old_pos;
10337 }
10338 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010339 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 }
10341 }
10342
10343 /*
10344 * set indent of '#' always to 0
10345 */
10346 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10347 {
10348 /* remember current indent for next line */
10349 old_indent = get_indent();
10350 (void)set_indent(0, SIN_CHANGED);
10351 }
10352
10353 /* Adjust ai_col, the char at this position can be deleted. */
10354 if (ai_col > curwin->w_cursor.col)
10355 ai_col = curwin->w_cursor.col;
10356}
10357#endif
10358
10359/*
10360 * Get the value that w_virtcol would have when 'list' is off.
10361 * Unless 'cpo' contains the 'L' flag.
10362 */
10363 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010364get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365{
10366 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10367 return getvcol_nolist(&curwin->w_cursor);
10368 validate_virtcol();
10369 return curwin->w_virtcol;
10370}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010371
10372#ifdef FEAT_AUTOCMD
10373/*
10374 * Handle the InsertCharPre autocommand.
10375 * "c" is the character that was typed.
10376 * Return a pointer to allocated memory with the replacement string.
10377 * Return NULL to continue inserting "c".
10378 */
10379 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010380do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010381{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010382 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010383 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010384
10385 /* Return quickly when there is nothing to do. */
10386 if (!has_insertcharpre())
10387 return NULL;
10388
Bram Moolenaar704984a2012-06-01 14:57:51 +020010389#ifdef FEAT_MBYTE
10390 if (has_mbyte)
10391 buf[(*mb_char2bytes)(c, buf)] = NUL;
10392 else
10393#endif
10394 {
10395 buf[0] = c;
10396 buf[1] = NUL;
10397 }
10398
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010399 /* Lock the text to avoid weird things from happening. */
10400 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010401 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010402
Bram Moolenaar704984a2012-06-01 14:57:51 +020010403 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010404 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010405 {
10406 /* Get the value of v:char. It may be empty or more than one
10407 * character. Only use it when changed, otherwise continue with the
10408 * original character to avoid breaking autoindent. */
10409 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10410 res = vim_strsave(get_vim_var_str(VV_CHAR));
10411 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010412
10413 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10414 --textlock;
10415
10416 return res;
10417}
10418#endif